Exemple #1
0
        internal T GetKey(int recnum, out bool deleted)
        {
            lock (_lock)
            {
                deleted = false;
                long off = recnum * 8L;
                var  b   = new byte[8];

                _recfileread.Seek(off, SeekOrigin.Begin);
                _recfileread.Read(b, 0, 8);
                off = Helper.ToInt64(b, 0);

                // seek offset in file
                var hdr = new byte[_rowheader.Length];
                _dataread.Seek(off, System.IO.SeekOrigin.Begin);
                // read header
                _dataread.Read(hdr, 0, _rowheader.Length);

                if (CheckHeader(hdr))
                {
                    deleted = IsDeleted(hdr);
                    byte kl    = hdr[3];
                    var  kbyte = new byte[kl];

                    _dataread.Read(kbyte, 0, kl);
                    return(_T.GetObject(kbyte, 0, kl));
                }

                return(default(T));
            }
        }
Exemple #2
0
        public IEnumerable <KeyValuePair <T, byte[]> > Traverse()
        {
            long offset = 0;

            offset = _fileheader.Length;

            while (offset < _writefile.Length)
            {
                long   pointer = offset;
                byte[] key;
                bool   deleted = false;
                offset = NextOffset(offset, out key, out deleted);
                KeyValuePair <T, byte[]> kv = new KeyValuePair <T, byte[]>(_T.GetObject(key, 0, key.Length), internalReadData(pointer));
                if (deleted == false)
                {
                    yield return(kv);
                }
            }
        }
Exemple #3
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);
            }
        }
        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);
            }
        }