Click here to Skip to main content
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads
 
Add your own
alternative version
Go to top

RaptorDB - the Key Value Store

, 22 Jan 2012
Smallest, fastest embedded nosql persisted dictionary using b+tree or MurMur hash indexing. (Now with Hybrid WAH bitmap indexes)
RaptorDB_v1.0.zip
RaptorDB
testing
RaptorDB_v1.1.zip
Indexes
Storage
Views
RaptorDB_v1.2.zip
RaptorDB_v1.3.zip
RaptorDB_v1.4.1.zip
Properties
RaptorDB_v1.4.zip
RaptorDB_v1.5.1.zip
RaptorDB_v1.5.2.zip
RaptorDB_v1.5.zip
RaptorDB_v1.6.zip
RaptorDB_v1.7.5.zip
UnitTests
Properties
UnitTests.nunit
RaptorDB_v1.7.zip
_svn
all-wcprops
dir-prop-base
entries
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
UnitTests.nunit
RaptorDB_v1.8.zip
DataTypes
UnitTests release.nunit
UnitTests.nunit
UnitTests.nunit
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RaptorDB
{
    public class ByteArrayComparer : IEqualityComparer<byte[]>
    {
        public bool Equals(byte[] a, byte[] b)
        {
            if (a == null || b == null)
                return a == b;
            if (a.Length != b.Length)
                return false;
            for (int i = 0; i < a.Length; i++)
                if (a[i] != b[i])
                    return false;
            return true;
        }

        public int GetHashCode(byte[] x)
        {
            if (x == null)
                throw new ArgumentNullException();
            int iHash = 0;
            for (int i = 0; i < x.Length; ++i)
                iHash ^= (x[i] << ((0x03 & i) << 3));
            return iHash;
        }
    }

    internal class LogFile
    {
        public LogFile()
        {
        }

        public LogFile(string filename, int number, int maxkeylen, string logNumberFormat)
        {
            // create a log file 
            FileName = filename + number.ToString(logNumberFormat);
            Number = number;
            _maxKeyLen = maxkeylen;

            _file = new FileStream(FileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read);
            WriteLogHeader();
        }

        public int Number;
        public string FileName;
        public bool Readonly;
        public int CurrentCount = 0;

        byte[] _header = new byte[] { 
            (byte)'M', (byte)'G', (byte)'L', 
            0 // [maxkeylen] 
        };

        internal SafeDictionary<byte[], long> _memCache = new SafeDictionary<byte[], long>(
                    Global.MaxItemsBeforeIndexing, 
                    new ByteArrayComparer());
        private Stream _file;
        private int _maxKeyLen;


        private void WriteLogHeader()
        {
            // write log file header;
            _header[3] = (byte)_maxKeyLen;
            _file.Write(_header, 0, _header.Length);
            _file.Flush();
        }

        public void Shutdown()
        {
            if (_file != null)
            {
                _file.Close();
                _file = null;
                _memCache = null;
            }
        }

        public void ReadLogFile(string filename)
        {
            if (File.Exists(filename))
            {
                _file = new FileStream(filename, FileMode.Open, FileAccess.Read);
                // read log header and check ok
                if (ReadLogHeader())
                {
                    // read data and put in cache
                    ReadData();
                }
                _file.Close();
                _file = null;
            }
        }

        public void DeleteLog()
        {
            Shutdown();
            Console.Write(" D" + Number + " ");
            File.Delete(FileName);
        }

        private void ReadData()
        {
            int len = 8 + 1 + _maxKeyLen;
            byte[] rec = new byte[len];
            while (_file.Read(rec, 0, len) == len)
            {
                CurrentCount++;
                long l = Helper.ToInt64(rec, 0);
                short of = (short)rec[8];
                byte[] k = new byte[of];
                Buffer.BlockCopy(rec, 9, k, 0, of);
                _memCache.Add(k, l);
            }
        }

        private bool ReadLogHeader()
        {
            bool ok = true;
            _file.Seek(0L, SeekOrigin.Begin);
            byte[] hdr = new byte[_header.Length];
            _file.Read(hdr, 0, _header.Length);
            for (int i = 0; i < _header.Length - 1; i++)
            {
                if (hdr[i] != _header[i])
                    ok = false;
            }
            if (ok)
                _maxKeyLen = (int)hdr[_header.Length - 1];
            return ok;
        }

        private void SaveToLogFile(byte[] key, long offset)
        {
            if (Readonly == false)
            {
                if (_file != null)
                {
                    byte[] b = CreateRecord(key, offset);
                    _file.Write(b, 0, b.Length);
                    _file.Flush();
                }
            }
        }

        private byte[] CreateRecord(byte[] key, long offset)
        {
            // create record
            byte[] rec = new byte[8 + 1 + _maxKeyLen];
            byte[] off = Helper.GetBytes(offset);
            Buffer.BlockCopy(off,0, rec,0, off.Length);
            byte[] str = key;
            rec[8] = (byte)str.Length;
            int len = (str.Length < _maxKeyLen ? str.Length : _maxKeyLen);
            Buffer.BlockCopy(str, 0, rec, off.Length + 1, len);
            return rec;
        }

        public long Get(byte[] k)
        {
            long l = -1;
            _memCache.TryGetValue(k, out l);
            return l;
        }

        public void Set(byte[] k, long val)
        {
            if (Readonly == false)
            {
                CurrentCount++;
                _memCache.Add(k, val);
                // write log file
                SaveToLogFile(k, val);
            }
        }

        public void Close()
        {
            if (_file != null)
            {
                _file.Flush();
                _file.Close();
                _file = null;
            }
        }

        public IEnumerator<KeyValuePair<byte[], long>> GetEnumerator()
        {
            return _memCache.GetEnumerator();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mehdi Gholam
Architect
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.
 
* Mehdi is the 5th person to get 6 out of 7 Platinums on CodeProject (13th Jan'12)

You may also be interested in...

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 23 Jan 2012
Article Copyright 2011 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid