Ejemplo n.º 1
0
        public Page <T> LoadPageFromPageNumber(int number)
        {
            lock (_fileLock)
            {
                SeekPage(number);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    // create node here
                    Page <T> page = new Page <T>();

                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    page.DiskPageNumber  = number;
                    page.RightPageNumber = Helper.ToInt32(b, 11);
                    int      index = _BlockHeader.Length;
                    object[] keys  = null;

                    for (int i = 0; i < count; i++)
                    {
                        int  idx = index + _rowSize * i;
                        byte ks  = b[idx];
                        T    key = default(T);
                        if (_externalStrings == false)
                        {
                            key = _T.GetObject(b, idx + 1, ks);
                        }
                        else
                        {
                            if (keys == null)
                            {
                                int        blknum  = Helper.ToInt32(b, idx + 1, false);
                                List <int> ablocks = new List <int>();
                                byte[]     bb      = _strings.GetData(blknum, out ablocks);
                                page.allocblocks = ablocks;
                                keys             = (object[])BJSON.ToObject(bb);
                            }
                            key = (T)keys[i];
                        }
                        int offset  = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        int duppage = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        page.tree.Add(key, new KeyInfo(offset, duppage));
                    }
                    return(page);
                }
                else
                {
                    throw new Exception("Page read error header invalid, number = " + number);
                }
            }
        }
Ejemplo n.º 2
0
        private int NodeHeaderCount(int nextpage, ref long c)
        {
            SeekPage(nextpage);
            byte[] b = new byte[_BlockHeader.Length];
            _file.Read(b, 0, _BlockHeader.Length);

            if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
            {
                short count     = Helper.ToInt16(b, 5);
                int   rightpage = Helper.ToInt32(b, 11);
                c += count;
                return(rightpage);
            }
            return(0);
        }
Ejemplo n.º 3
0
        private bool ReadFileHeader()
        {
            _file.Seek(0L, SeekOrigin.Begin);
            byte[] b = new byte[_FileHeader.Length];
            _file.Read(b, 0, _FileHeader.Length);

            if (b[0] == _FileHeader[0] && b[1] == _FileHeader[1] && b[2] == _FileHeader[2])
            {
                byte   maxks = b[3];
                ushort nodes = (ushort)Helper.ToInt16(b, 4);
                //int root = Helper.ToInt32(b, 6);
                _maxKeySize    = maxks;
                _PageNodeCount = nodes;
                _FileHeader    = b;
            }

            return(false);
        }
Ejemplo n.º 4
0
        private int LoadPageListData(int page, SafeSortedList <T, PageInfo> PageList)
        {
            lock (_fileLock)
            {
                // load page list data
                int nextpage = -1;
                SeekPage(page);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    nextpage = Helper.ToInt32(b, 11);
                    int index = _BlockHeader.Length;

                    for (int i = 0; i < count; i++)
                    {
                        int  idx     = index + _rowSize * i;
                        byte ks      = b[idx];
                        T    key     = _T.GetObject(b, idx + 1, ks);
                        int  pagenum = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        // add counts
                        int unique = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        // FEATURE : add dup count
                        PageList.Add(key, new PageInfo(pagenum, unique, 0));
                    }
                }
                else
                {
                    throw new Exception("Page List header is invalid");
                }

                return(nextpage);
            }
        }
Ejemplo n.º 5
0
        public Page <T> LoadPageFromPageNumber(int number)
        {
            lock (_fileLock)
            {
                SeekPage(number);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    // create node here
                    Page <T> page = new Page <T>();

                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    page.DiskPageNumber  = number;
                    page.RightPageNumber = Helper.ToInt32(b, 11);
                    int index = _BlockHeader.Length;

                    for (int i = 0; i < count; i++)
                    {
                        int  idx     = index + _rowSize * i;
                        byte ks      = b[idx];
                        T    key     = _T.GetObject(b, idx + 1, ks);
                        int  offset  = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        int  duppage = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        page.tree.Add(key, new KeyInfo(offset, duppage));
                    }
                    return(page);
                }
                else
                {
                    throw new Exception("Page read error header invalid, number = " + number);
                }
            }
        }
Ejemplo n.º 6
0
        private int LoadPageListData(int page, SafeSortedList <T, PageInfo> PageList)
        {
            lock (_fileLock)
            {
                // load page list data
                int nextpage = -1;
                SeekPage(page);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    nextpage = Helper.ToInt32(b, 11);
                    int      index = _BlockHeader.Length;
                    object[] keys  = null;
                    // TODO : needed??
                    //if (File.Exists(_FileName + ".pagelist"))
                    //{
                    //    var bn = File.ReadAllBytes(_FileName + ".pagelist");
                    //    int blknum = Helper.ToInt32(bn, 0);
                    //    byte[] bb = _strings.GetData(blknum, out _pagelistalllocblock);
                    //    keys = (object[])BJSON.ToObject(bb);
                    //}
                    for (int i = 0; i < count; i++)
                    {
                        int  idx = index + _rowSize * i;
                        byte ks  = b[idx];
                        T    key;
                        if (_externalStrings == false)
                        {
                            key = _T.GetObject(b, idx + 1, ks);
                        }
                        else
                        {
                            if (keys == null)
                            {
                                key = _T.GetObject(b, idx + 1, ks); // do old way until better way
                            }
                            else
                            {
                                key = (T)keys[i];
                            }
                        }
                        int pagenum = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        // add counts
                        int unique = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        // FEATURE : add dup count
                        PageList.Add(key, new PageInfo(pagenum, unique, 0));
                    }
                }
                else
                {
                    throw new Exception("Page List header is invalid");
                }

                return(nextpage);
            }
        }
Ejemplo n.º 7
0
 public short GetObject(byte[] buffer, int offset, int count)
 {
     return(Helper.ToInt16(buffer, offset));
 }