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

RaptorDB - the Key Value Store

, 22 Jan 2012 CPOL
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.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)

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)

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