Click here to Skip to main content
15,867,568 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 906.9K   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;

namespace RaptorDB
{
    public interface IRDBDataType<T> : IComparable<T>, IGetBytes<T>, IEqualityComparer<T>, IEquatable<T> 
    {
    }
    
    //------------------------------------------------------------------------------------------------------------------

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

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

    public class rdbByteArray : IRDBDataType<rdbByteArray>
    {
        public rdbByteArray()
        {

        }

        public rdbByteArray(byte[] key)
        {
            val = key;
        }
        public byte[] val;

        public int CompareTo(rdbByteArray obj)
        {
            return Helper.CompareUnSafe(val, obj.val);
        }

        public byte[] GetBytes()
        {
            return val;
        }

        public bool Equals(rdbByteArray a, rdbByteArray b)
        {
            if (a == null || b == null)
                return a == b;
            if (a.val.Length != b.val.Length)
                return false;
            int i = Helper.CompareUnSafe(a.val, b.val);
            return i==0?true:false;
        }

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

        public bool Equals(rdbByteArray other)
        {
            if (other == null)
                return false;
            if (this.val.Length != other.val.Length)
                return false;
            int i = Helper.CompareUnSafe(this.val, other.val);
            return i == 0 ? true : false;
        }

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

        public override bool Equals(object obj)
        {
            int i = Helper.CompareUnSafe(this.val, ((rdbByteArray)obj).val);
            return i == 0 ? true : false;
        }

        public override int GetHashCode()
        {
            return this.GetHashCode(this);
        }
    }

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

    public class rdbInt : IRDBDataType<rdbInt>
    {
        public rdbInt()
        {

        }
        public rdbInt(int i)
        {
            _i = i;
        }
        public rdbInt(uint i)
        {
            _i = (int)i;
        }
        private int _i;

        public int CompareTo(rdbInt other)
        {
            return _i.CompareTo(other._i);
        }

        public byte[] GetBytes()
        {
            return Helper.GetBytes(_i, false);
        }

        public bool Equals(rdbInt x, rdbInt y)
        {
            return x._i == y._i;
        }

        public int GetHashCode(rdbInt obj)
        {
            return obj._i;
        }

        public bool Equals(rdbInt other)
        {
            return this._i == other._i;
        }

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

        public override bool Equals(object obj)
        {
            return this._i == ((rdbInt)obj)._i;
        }

        public override int GetHashCode()
        {
            return _i;
        }
    }

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

    public class rdbLong : IRDBDataType<rdbLong>
    {
        public rdbLong()
        {

        }
        public rdbLong(long i)
        {
            _i = i;
        }
        private long _i;

        public int CompareTo(rdbLong other)
        {
            return _i.CompareTo(other._i);
        }

        public byte[] GetBytes()
        {
            return Helper.GetBytes(_i, false);
        }

        public bool Equals(rdbLong x, rdbLong y)
        {
            return x._i == y._i;
        }

        public int GetHashCode(rdbLong obj)
        {
            return obj._i.GetHashCode();
        }

        public bool Equals(rdbLong other)
        {
            return this._i == other._i;
        }

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

        public override bool Equals(object obj)
        {
            return this._i == ((rdbLong)obj)._i;
        }

        public override int GetHashCode()
        {
            return _i.GetHashCode();
        }
    }
}

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