Click here to Skip to main content
12,502,104 members (50,617 online)
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads

Tagged as

Stats

466.8K views
9.1K downloads
263 bookmarked
Posted

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
testing
Indexes
Storage
Views
Properties
UnitTests
Properties
UnitTests.nunit
_svn
all-wcprops
dir-prop-base
entries
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
UnitTests.nunit
DataTypes
UnitTests release.nunit
UnitTests.nunit
UnitTests.nunit
using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace RaptorDB
{
    public class StorageFile
    {
        System.IO.Stream _fromfile;
        private int _maxKeyLen;

        public static byte[] _fileheader = { (byte)'M', (byte)'G', (byte)'D', (byte)'B',
                                              0, // -- [flags] = [shutdownOK:1],
                                              0  // -- [maxkeylen] 
                                           };

        public static byte[] _rowheader = { (byte)'M', (byte)'G', (byte)'R' ,
                                           0,               // 4     [keylen]
                                           0,0,0,0,0,0,0,0, // 5-12  [datetime] 8 bytes = insert time
                                           0,0,0,0,         // 13-16 [data length] 4 bytes
                                           0,               // 17 -- [flags] = isCommited:1 
                                                            //                 isRollback:1
                                                            //                 isCompressed:1
                                                            //                 isDeleted:1
                                                            //                 isVersioned:1 
                                           0                // 18 -- [crc] = header crc check
                                       };
        private enum HDR_POS
        {
            KeyLen = 3,
            DateTime = 4,
            DataLength = 12,
            Flags = 16,
            CRC = 17
        }

        public bool SkipDateTime = false;

        public StorageFile(System.IO.Stream file, int maxkeylen)
        {
            _fromfile = file;
            _maxKeyLen = maxkeylen;
            if (_fromfile.Length == 0)
            {
                // new file
                byte b = (byte)maxkeylen;
                _fileheader[5] = b;
                _fromfile.Write(_fileheader, 0, _fileheader.Length);
                _fromfile.Flush();
            }
            else
            {
                // TODO : check file header exists
                // TODO : check file flags ok
            }
            bw = new BinaryWriter(ms, Encoding.UTF8);
        }

        public long WriteData(byte[] key, byte[] data)
        {
            byte[] k = key;
            int kl = k.Length;

            // seek end of file
            long offset = _fromfile.Seek(0L, System.IO.SeekOrigin.End);
            byte[] hdr = CreateRowHeader(kl, data.Length);
            // write header info
            _fromfile.Write(hdr, 0, hdr.Length);
            // write key
            _fromfile.Write(k, 0, kl);
            // write data block
            _fromfile.Write(data, 0, data.Length);
            _fromfile.Flush();
            // return starting offset
            return offset;
        }

        MemoryStream ms = new MemoryStream();
        BinaryWriter bw;
        private byte[] CreateRowHeader(int keylen, int datalen)
        {
            ms.Seek(0L, SeekOrigin.Begin);
            bw.Write(_rowheader, 0, 3);
            bw.Write((byte)keylen);
            if (SkipDateTime == false)
                bw.Write(FastDateTime.Now.Ticks);
            else
                bw.Write(0L);
            bw.Write(datalen);
            bw.Write((byte)0);
            bw.Write((byte)0);
            bw.Flush();
            return ms.ToArray();
        }

        public byte[] ReadData(long offset)
        {
            // seek offset in file
            byte[] hdr = new byte[_rowheader.Length];
            _fromfile.Seek(offset, System.IO.SeekOrigin.Begin);
            // read header
            _fromfile.Read(hdr, 0, _rowheader.Length);
            // check header
            if (CheckHeader(hdr))
            {
                // skip key bytes
                _fromfile.Seek(hdr[(int)HDR_POS.KeyLen], System.IO.SeekOrigin.Current);
                int dl = Helper.ToInt32(hdr, (int)HDR_POS.DataLength);
                byte[] data = new byte[dl];
                // read data block
                _fromfile.Read(data, 0, dl);
                return data;
            }
            else
                throw new Exception("data header error");
        }

        private bool CheckHeader(byte[] hdr)
        {
            if (hdr[0] != (byte)'M' && hdr[1] != (byte)'G' && hdr[2] != (byte)'R' && hdr[(int)HDR_POS.CRC] != 0)
                return false;
            return true;
        }

        public void Shutdown()
        {
            this._fromfile.Flush();
            this._fromfile.Close();
        }
    }
}

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 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)

You may also be interested in...

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