Esempio n. 1
0
 public void InsertKey(IFixedLengthKey key, int offset)
 {
     DeferInsertion(key, offset);
     UpdateMinKey(key, offset);
     _modified = true;
     _treeNode.IncrementCount();
 }
Esempio n. 2
0
        public BTreePage Split(IFixedLengthKey key, int offset, long pageOffset, float splitFactor)
        {
            ProcessDeferredUpdates();

            if (splitFactor >= 1 || splitFactor < 0.1)
            {
                throw new Exception("BTreePage.Split: splitFactor = " + splitFactor.ToString() + " is not applicable");
            }

            int             mIndex = (int)(_maxCount * splitFactor);
            int             off;
            IFixedLengthKey minKey       = MemoryRead(mIndex, out off).FactoryMethod();
            BTreePage       splittedPage = new BTreePage(
                new BTreeNode(minKey, off, pageOffset, _maxCount - mIndex), _maxCount, _stream, _factoryKey);

            CopyBytes(_bytes, mIndex, splittedPage._bytes, 0, _maxCount - mIndex);
            _treeNode.SetNewCount(mIndex);
            _rightBound = mIndex;
            _modified   = splittedPage._modified = true;

            if (key.CompareTo(minKey) < 0)
            {
                InsertKey(key, offset);
            }
            else
            {
                splittedPage.InsertKey(key, offset);
            }

            return(splittedPage);
        }
Esempio n. 3
0
        public override void DeleteKey(IFixedLengthKey key, int offset)
        {
            RBNodeBase rbNode;
            BTreeNode  foundNode = SearchBTreeNode(key, offset, out rbNode);

            if (foundNode != null)
            {
                BTreePage page = PreparePage(foundNode);
                _count -= foundNode.Count;
                page.DeleteKey(key, offset);
                int pageCount = foundNode.Count;
                _count += pageCount;
                long pageOffset;
                if (page.Empty())
                {
                    pageOffset = page.BTreeNode.PageOffset;
                    freePages.Push(pageOffset);
                    _rbTree.RB_Delete(page.BTreeNode);
                    _cache.Remove(pageOffset);
                }
                else if (pageCount < (_maxCount >> 2) && (rbNode = _rbTree.GetNext(rbNode)) != null)
                {
                    BTreeNode rightNode = (BTreeNode)rbNode.Key;
                    if (pageCount + rightNode.Count < (_maxCount >> 1))
                    {
                        BTreePage rightPage = PreparePage(rightNode);
                        pageOffset = rightPage.BTreeNode.PageOffset;
                        page.Merge(rightPage);
                        freePages.Push(pageOffset);
                        _rbTree.RB_Delete(rightPage.BTreeNode);
                        _cache.Remove(pageOffset);
                    }
                }
            }
        }
Esempio n. 4
0
        public override IFixedLengthKey FactoryMethod(System.IO.BinaryReader reader)
        {
            IFixedLengthKey key = FactoryMethod();

            key.Read(reader);
            return(key);
        }
Esempio n. 5
0
 public void DeleteKey(IFixedLengthKey key, int offset)
 {
     if (DeferDeletion(key, offset))
     {
         _modified = true;
         _treeNode.DecrementCount();
     }
 }
Esempio n. 6
0
 public void SetPageData(BTreeNode treeNode, int maxCount, IFixedLengthKey factoryKey)
 {
     _treeNode   = treeNode;
     _rightBound = _treeNode.Count;
     _maxCount   = maxCount;
     _factoryKey = factoryKey;
     _curKey     = _factoryKey.FactoryMethod();
     _keySize    = _factoryKey.KeySize + 4;
 }
Esempio n. 7
0
        private void UpdateMinKey(IFixedLengthKey key, int offset)
        {
            int keyCompare = key.CompareTo(_treeNode.MinKey);

            if (keyCompare < 0 || (keyCompare == 0 && offset < _treeNode.MinOffset))
            {
                _treeNode.ChangeMinKey(key.FactoryMethod(), offset);
            }
        }
Esempio n. 8
0
 private BTreeNode SearchBTreeNode(IFixedLengthKey key, int offset, out RBNodeBase rbNode)
 {
     _searchNode.ChangeMinKey(key, offset);
     rbNode = _rbTree.GetEqualOrLess(_searchNode);
     if (rbNode == null)
     {
         rbNode = _rbTree.GetMinimumNode();
     }
     return((rbNode != null) ? (BTreeNode)rbNode.Key : null);
 }
Esempio n. 9
0
 public BTree(string fileName, IFixedLengthKey factoryKey)
 {
     _factoryKey = factoryKey;
     _fileName   = fileName;
     if (_factoryKey.KeySize <= 0)
     {
         throw new KeySizeException();
     }
     _maxCount = (PAGE_SIZE - HEADER_SIZE) / (_factoryKey.KeySize + 4);
 }
Esempio n. 10
0
        public BTreePage(BTreeNode treeNode, int maxCount, FileStream stream, IFixedLengthKey factoryKey)
        {
            SetPageData(treeNode, maxCount, factoryKey);
            _stream = stream;
            _reader = new BinaryReader(stream);
            _writer = new BinaryWriter(stream);

            _memoryStream = new MemoryStream(_bytes, 0, BTree.PAGE_SIZE);
            _memoryReader = new BinaryReader(_memoryStream);
            _memoryWriter = new BinaryWriter(_memoryStream);
        }
Esempio n. 11
0
        public void Merge(BTreePage rightPage)
        {
            int offset;

            ProcessDeferredUpdates();
            rightPage.ProcessDeferredUpdates();
            rightPage.SetPosition(0);
            for (int i = 0; i < rightPage._rightBound; ++i)
            {
                IFixedLengthKey key = rightPage.MemoryReadNext(out offset);
                MemoryWrite(key, offset, _rightBound++);
            }
            _treeNode.SetNewCount(_rightBound);
            rightPage._treeNode.SetNewCount(0);
            _modified = rightPage._modified = true;
        }
Esempio n. 12
0
        public int GetAllKeys(ArrayList keys_offsets)
        {
            int count = _treeNode.Count;

            if (count > 0)
            {
                ProcessDeferredUpdates();
                SetPosition(0);
                for (int index = 0; index < count; index++)
                {
                    int             offset;
                    IFixedLengthKey key = MemoryReadNext(out offset);
                    keys_offsets.Add(new KeyPair(key.FactoryMethod(), offset));
                }
            }
            return(count);
        }
Esempio n. 13
0
        public override void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, ArrayList keys_offsets)
        {
            _searchNode.ChangeMinKey(beginKey, 0);
            RBNodeBase rbNode = _rbTree.GetMaximumLess(_searchNode);

            if (rbNode == null)
            {
                rbNode = _rbTree.GetMinimumNode();
            }

            while (rbNode != null && ((BTreeNode)rbNode.Key).MinKey.CompareTo(endKey) <= 0)
            {
                BTreePage page = PreparePage((BTreeNode)rbNode.Key);
                page.SearchForRange(beginKey, endKey, keys_offsets);
                rbNode = _rbTree.GetNext(rbNode);
            }
        }
Esempio n. 14
0
        public override void InsertKey(IFixedLengthKey key, int offset)
        {
            _count++;

            RBNodeBase rbNode;
            BTreeNode  foundNode = SearchBTreeNode(key, offset, out rbNode);

            if (foundNode == null)
            {
                if (_rbTree.Count == 0)
                {
                    BTreeNode newNode = new BTreeNode(key.FactoryMethod(), offset, GetOffsetForNewPage());
                    _rbTree.RB_Insert(newNode);
                    BTreePage page = NewPage(newNode);
                    page.InsertKey(key, offset);
                    page.Write();
                    _cache.CacheObject(newNode.PageOffset, page);
                    return;
                }
                else
                {
                    RBNodeBase rbMinNode = _rbTree.GetMinimumNode();
                    foundNode = (BTreeNode)rbMinNode.Key;
                }
            }
            else
            {
                BTreePage page = PreparePage(foundNode);
                if (page.Full())
                {
                    float     splitFactor  = (_rbTree.GetNext(rbNode) == null) ? 0.875f : 0.5f;
                    BTreePage splittedPage = page.Split(key, offset, GetOffsetForNewPage(), splitFactor);
                    _rbTree.RB_Insert(splittedPage.BTreeNode);
                    splittedPage.Write();
                    _cache.CacheObject(splittedPage.BTreeNode.PageOffset, splittedPage);
                }
                else
                {
                    page.InsertKey(key, offset);
                }
            }
        }
Esempio n. 15
0
        private void DeferInsertion(IFixedLengthKey key, int offset)
        {
            int index = GetDeferredDeletionIndex(key, offset);

            if (index >= 0)
            {
                _deferredDeletions.RemoveAt(index);
            }
            else
            {
                int insertedCount = _deferredInsertions.Count;
                if (insertedCount == _deferredArrayMaximumSize || _rightBound + insertedCount == _maxCount)
                {
                    ProcessDeferredUpdates();
                }
                index = GetDeferredInsertionIndex(key, offset);
                if (index < 0)
                {
                    _deferredInsertions.Insert(~index, new KeyPair(key.FactoryMethod(), offset));
                }
            }
        }
Esempio n. 16
0
        private IFixedLengthKey SearchR(int l, int r, IFixedLengthKey key, int offset, out int index)
        {
            index = -1;
            if (l > r)
            {
                return(null);
            }

            index = r;

            IFixedLengthKey mKey = null;

            while (l < r)
            {
                int m = (l + r) >> 1;
                int off;
                mKey = MemoryRead(m, out off);
                int compareResult = key.CompareTo(mKey);
                if (compareResult == 0 && offset != -1)
                {
                    compareResult = offset - off;
                }
                if (compareResult >= 0)
                {
                    l     = m + 1;
                    index = l;
                }
                else
                {
                    r     = m;
                    index = m;
                }
            }

            return(mKey);
        }
Esempio n. 17
0
        public override bool Open()
        {
            _stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 64);
            _rbTree = new RedBlackTree();
            _reader = new BinaryReader(_stream);
            _writer = new BinaryWriter(_stream);
            _count  = 0;
            _cache  = new ObjectCache(CACHE_SIZE);
            _cache.ObjectRemoved += new ObjectCacheEventHandler(_cache_ObjectRemoved);

            freePages.Clear();
            int numPages = (int)(_stream.Length / PAGE_SIZE);

            //Tracer._Trace( "####################### " + _fileName );
            for (int i = 0; i < numPages; i++)
            {
                long pageOffset = i * PAGE_SIZE;
                _stream.Position = pageOffset;
                int count = _reader.ReadInt32();
                //Tracer._Trace( "####################### count = " + count.ToString() );

                _stream.Position = pageOffset + HEADER_SIZE;
                IFixedLengthKey key    = _factoryKey.FactoryMethod(_reader);
                int             offset = _reader.ReadInt32();
                if (count != 0)
                {
                    _rbTree.RB_Insert(new BTreeNode(key, offset, pageOffset, count));
                }
                else
                {
                    freePages.Push(pageOffset);
                }
                _count += count;
            }
            return(true);
        }
Esempio n. 18
0
        private bool DeferDeletion(IFixedLengthKey key, int offset)
        {
            int index = GetDeferredInsertionIndex(key, offset);

            if (index >= 0)
            {
                _deferredInsertions.RemoveAt(index);
                return(true);
            }
            else
            {
                if (_deferredDeletions.Count == _deferredArrayMaximumSize)
                {
                    ProcessDeferredUpdates();
                }
                index = GetDeferredDeletionIndex(key, offset);
                if (index < 0)
                {
                    _deferredDeletions.Insert(~index, new KeyPair(key.FactoryMethod(), offset));
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 19
0
 public override IEnumerable SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey)
 {
     throw new NotImplementedException("Use OmniaMeaBTree.");
 }
Esempio n. 20
0
 public abstract void DeleteKey(IFixedLengthKey key, int offset);
Esempio n. 21
0
 public abstract void InsertKey(IFixedLengthKey key, int offset);
Esempio n. 22
0
 public abstract void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, ArrayList keys_offsets);
Esempio n. 23
0
 public abstract IEnumerable SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey);
Esempio n. 24
0
 private void MemoryWrite(IFixedLengthKey key, int offset, int index)
 {
     _memoryWriter.BaseStream.Position = BTree.HEADER_SIZE + _keySize * index;
     key.Write(_memoryWriter);
     _memoryWriter.Write(offset);
 }
Esempio n. 25
0
        public void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, ArrayList keys_offsets)
        {
            if (_treeNode.Count == 0)
            {
                return;
            }

            int             index, offset;
            KeyPair         pair;
            IFixedLengthKey deferredKey;
            int             deferredInsertionsIndex = ~GetDeferredInsertionIndex(beginKey, -1);

            IFixedLengthKey foundKey = SearchL(0, _rightBound, beginKey, -1, out index);

            if (foundKey != null && index != -1 && index < _rightBound)
            {
                int deferredDeletionsIndex = ~GetDeferredDeletionIndex(beginKey, -1);
                SetPosition(index);
                for ( ; index < _rightBound; ++index)
                {
                    IFixedLengthKey aKey = MemoryReadNext(out offset);
                    for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex)
                    {
                        pair        = (KeyPair)_deferredInsertions[deferredInsertionsIndex];
                        deferredKey = pair._key;
                        if (deferredKey.CompareTo(aKey) > 0)
                        {
                            break;
                        }
                        if (beginKey.CompareTo(deferredKey) <= 0)
                        {
                            if (endKey.CompareTo(deferredKey) < 0)
                            {
                                break;
                            }
                            keys_offsets.Add(pair);
                        }
                    }
                    if (endKey.CompareTo(aKey) < 0)
                    {
                        break;
                    }
                    bool deleted = false;
                    while (deferredDeletionsIndex < _deferredDeletions.Count)
                    {
                        pair = (KeyPair)_deferredDeletions[deferredDeletionsIndex];
                        int compareResult = pair._key.CompareTo(aKey);
                        if (compareResult == 0)
                        {
                            compareResult = pair._offset - offset;
                        }
                        if (compareResult > 0)
                        {
                            break;
                        }
                        ++deferredDeletionsIndex;
                        if (compareResult == 0)
                        {
                            deleted = true;
                            break;
                        }
                    }
                    if (!deleted)
                    {
                        keys_offsets.Add(new KeyPair(aKey.FactoryMethod(), offset));
                    }
                }
            }
            for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex)
            {
                pair        = (KeyPair)_deferredInsertions[deferredInsertionsIndex];
                deferredKey = pair._key;
                if (beginKey.CompareTo(deferredKey) <= 0)
                {
                    if (endKey.CompareTo(deferredKey) < 0)
                    {
                        break;
                    }
                    keys_offsets.Add(pair);
                }
            }
        }
Esempio n. 26
0
        private void ProcessDeferredUpdates()
        {
            int count = _treeNode.Count;

            _rightBound = count;
            if (count == 0)
            {
                _deferredDeletions.Clear();
                _deferredInsertions.Clear();
                return;
            }

            int deleted  = _deferredDeletions.Count;
            int inserted = _deferredInsertions.Count;

            if (deleted > 0 || inserted > 0)
            {
                int     off;
                int     rightBound = count + deleted - inserted;
                int     i          = _maxCount;
                bool    isDeleted;
                KeyPair pair;

                while (rightBound > 0)
                {
                    isDeleted = false;
                    IFixedLengthKey key = MemoryRead(--rightBound, out off);
                    _keyPair._key    = key;
                    _keyPair._offset = off;

                    // at first, look through deferred deletions
                    while (deleted > 0)
                    {
                        pair = (KeyPair)_deferredDeletions[deleted - 1];
                        int compareResult = _keyComparer.Compare(_keyPair, pair);
                        if (compareResult >= 0)
                        {
                            if ((isDeleted = compareResult == 0))
                            {
                                --deleted;
                            }
                            break;
                        }
                        --deleted;
                    }

                    // if current key is not deleted, merge it with deferred insertions
                    if (!isDeleted)
                    {
                        while (inserted > 0)
                        {
                            pair = (KeyPair)_deferredInsertions[inserted - 1];
                            if (_keyComparer.Compare(_keyPair, pair) > 0)
                            {
                                MemoryWrite(key, off, --i);
                                break;
                            }
                            else
                            {
                                MemoryWrite(pair._key, pair._offset, --i);
                                --inserted;
                            }
                        }
                        if (inserted == 0)
                        {
                            MemoryWrite(key, off, --i);
                        }
                    }
                }

                // if some insertions remain, write it before all other key pairs
                while (inserted > 0)
                {
                    pair = (KeyPair)_deferredInsertions[--inserted];
                    MemoryWrite(pair._key, pair._offset, --i);
                }

                if (i > 0)
                {
                    CopyBytes(_bytes, i, _bytes, 0, count);
                }

                _deferredDeletions.Clear();
                _deferredInsertions.Clear();
            }
        }
Esempio n. 27
0
 public KeyPair(IFixedLengthKey key, int offset)
 {
     _key    = key;
     _offset = offset;
 }
Esempio n. 28
0
 private int GetDeferredInsertionIndex(IFixedLengthKey key, int offset)
 {
     _keyPair._key    = key;
     _keyPair._offset = offset;
     return(_deferredInsertions.BinarySearch(_keyPair, _keyComparer));
 }