Click here to Skip to main content
15,884,628 members
Articles / Database Development / NoSQL

RaptorDB - the Key Value Store

Rate me:
Please Sign up or sign in to vote.
4.89/5 (118 votes)
22 Jan 2012CPOL22 min read 914.8K   9.9K   266  
Smallest, fastest embedded nosql persisted dictionary using b+tree or MurMur hash indexing. (Now with Hybrid WAH bitmap indexes)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RaptorDB
{
    public class RaptorDBString : RaptorDBbase
    {
        private bool _caseSensitive = false;

        public RaptorDBString(string filename, bool caseSensitive)
            : base(filename)
        {
            _caseSensitive = caseSensitive;
        }

        public void Set(string key, string val)
        {
            this.Set(key, Encoding.Unicode.GetBytes(val));
        }

        public void Set(string key, byte[] val)
        {
            string str = (_caseSensitive ? key : key.ToLower());
            byte[] ustr = Encoding.Unicode.GetBytes(str);

            base.Set(ustr, val);
        }

        public bool Get(string key, out string val)
        {
            byte[] bval = null;
            val = null;

            if (this.Get(key, out bval))
            {
                val = Encoding.Unicode.GetString(bval);
                return true;
            }
            return false;
        }

        public bool Get(string key, out byte[] val)
        {
            string str = (_caseSensitive ? key : key.ToLower());
            byte[] ustr = Encoding.Unicode.GetBytes(str);

            return base.Get(ustr, out val);
        }
    }

    //----------------------------------------------------------------------------------------------------

    public class RaptorDBGuid : RaptorDBbase
    {
        public RaptorDBGuid(string filename)
            : base(filename)
        {
        }

        public void Set(Guid key, string val)
        {
            this.Set(key, Encoding.Unicode.GetBytes(val));
        }

        public void Set(Guid key, byte[] val)
        {
            base.Set(key.ToByteArray(), val);
        }

        public bool Get(Guid key, out string val)
        {
            byte[] bval = null;
            val = null;

            if (this.Get(key.ToByteArray(), out bval))
            {
                val = Encoding.Unicode.GetString(bval);
                return true;
            }
            return false;
        }

        public bool Get(Guid key, out byte[] val)
        {
            return base.Get(key.ToByteArray(), out val);
        }

        public IEnumerable<int> GetDuplicates(Guid key)
        {
            return base.GetDuplicates(key.ToByteArray());
        }
    }

    //----------------------------------------------------------------------------------------------------

    public abstract class RaptorDBbase
    {
        internal RaptorDB<rdbInt> _rap;
        private MurmurHash2Unsafe _mur = new MurmurHash2Unsafe();

        public RaptorDBbase(string filename)
        {
            Global.DEFAULTNODESIZE = 1000;
            _rap = new RaptorDB<rdbInt>(filename, 4, true, INDEXTYPE.BTREE);
            _rap.InMemoryIndex = true;
        }


        public void Set(byte[] key, byte[] val)
        {
            uint hc = _mur.Hash(key);

            MemoryStream ms = new MemoryStream();
            ms.Write(Helper.GetBytes(key.Length, false), 0, 4);
            ms.Write(key, 0, key.Length);
            ms.Write(val, 0, val.Length);

            _rap.Set(new rdbInt(hc), ms.ToArray());
        }

        public bool Get(byte[] key, out byte[] val)
        {
            uint hc = _mur.Hash(key);

            if (_rap.Get(new rdbInt(hc), out val))
            {
                // unpack data
                byte[] g = null;
                if (UnpackData(val, out val, out g))
                {
                    if (Helper.CompareMemCmp(key, g) != 0)
                    {
                        // if data not equal check duplicates (hash conflict)
                        List<int> ints = new List<int>(_rap.GetDuplicates(new rdbInt((int)hc)));
                        ints.Reverse();
                        foreach (int i in ints)
                        {
                            byte[] bb = _rap.FetchDuplicate(i);
                            if (UnpackData(bb, out val, out g))
                            {
                                if (Helper.CompareMemCmp(key, g) == 0)
                                    return true;
                            }
                        }
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }

        public void Shutdown()
        {
            _rap.Shutdown();
        }

        public void SaveIndex()
        {
            _rap.SaveIndex();
        }

        public long Count()
        {
            return _rap.Count();
        }

        private bool UnpackData(byte[] buffer, out byte[] val, out byte[] key)
        {
            int len = Helper.ToInt32(buffer, 0, false);
            key = new byte[len];
            Buffer.BlockCopy(buffer, 4, key, 0, len);
            val = new byte[buffer.Length - 4 - len];
            Buffer.BlockCopy(buffer, 4 + len, val, 0, buffer.Length - 4 - len);

            return true;
        }

        public IEnumerable<int> GetDuplicates(byte[] key)
        {
            uint hc = _mur.Hash(key);

            return _rap.GetDuplicates(new rdbInt(hc));
        }

        public byte[] FetchDuplicate(int rec)
        {
            byte[] b = _rap.FetchDuplicate(rec);
            byte[] val;
            byte[] key;
            if (UnpackData(b, out val, out key))
            {
                return val;
            }
            return null;
        }
    }
}

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)


Written By
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 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions