Click here to Skip to main content
15,881,089 members
Articles / Database Development / NoSQL

RaptorDB - The Key Value Store V2

Rate me:
Please Sign up or sign in to vote.
4.96/5 (101 votes)
10 Oct 2013CPOL15 min read 634K   6.9K   168  
Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. (with MonoDroid support)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RaptorDB
{
    internal interface IGetBytes<T>
    {
        byte[] GetBytes(T obj);
        T GetObject(byte[] buffer, int offset, int count);
    }

    internal class RDBDataType<T>
    {
        public static IGetBytes<T> ByteHandler()
        {
            Type type = typeof(T);

            if (type == typeof(int))
                return (IGetBytes<T>)new inthandler<T>();

            else if (type == typeof(uint))
                return (IGetBytes<T>)new uinthandler<T>();

            else if (type == typeof(long))
                return (IGetBytes<T>)new longhandler<T>();

            else if (type == typeof(Guid))
                return (IGetBytes<T>)new guidhandler<T>();

            else if (type == typeof(string))
                return (IGetBytes<T>)new stringhandler<T>();

            return null;
        }

        public static byte GetByteSize(byte keysize)
        {
            byte size = 4;
            Type t = typeof(T);

            if (t == typeof(int))
                size = 4;
            if (t == typeof(uint))
                size = 4;
            if (t == typeof(long))
                size = 8;
            if (t == typeof(Guid))
                size = 16;
            if (t == typeof(string))
                size = keysize;

            return size;
        }
    }

    internal class stringhandler<T> : IGetBytes<string>
    {
        public byte[] GetBytes(string obj)
        {
            return Helper.GetBytes(obj);
        }

        public string GetObject(byte[] buffer, int offset, int count)
        {
            return Helper.GetString(buffer, offset, (short)count);
        }
    }

    internal class inthandler<T> : IGetBytes<int>
    {
        public byte[] GetBytes(int obj)
        {
            return Helper.GetBytes(obj, false);
        }

        public int GetObject(byte[] buffer, int offset, int count)
        {
            return Helper.ToInt32(buffer, offset);
        }
    }

    internal class uinthandler<T> : IGetBytes<uint>
    {
        public byte[] GetBytes(uint obj)
        {
            return Helper.GetBytes(obj, false);
        }

        public uint GetObject(byte[] buffer, int offset, int count)
        {
            return (uint)Helper.ToInt32(buffer, offset);
        }
    }

    internal class longhandler<T> : IGetBytes<long>
    {
        public byte[] GetBytes(long obj)
        {
            return Helper.GetBytes(obj, false);
        }

        public long GetObject(byte[] buffer, int offset, int count)
        {
            return Helper.ToInt64(buffer, offset);
        }
    }

    internal class guidhandler<T> : IGetBytes<Guid>
    {
        public byte[] GetBytes(Guid obj)
        {
            return obj.ToByteArray();
        }

        public Guid GetObject(byte[] buffer, int offset, int count)
        {
            byte[] b = new byte[16];
            Buffer.BlockCopy(buffer, offset, b, 0, 16);
            return new Guid(b);
        }
    }
}

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