Exemple #1
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);
                    }
                }
            }
        }
Exemple #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);
        }
Exemple #3
0
 public override void Close()
 {
     _cache.RemoveAll();
     _cache    = null;
     _freeNode = null;
     _count    = 0;
     _reader.Close();
     _writer.Close();
     _stream.Close();
 }
Exemple #4
0
        private BTreePage PreparePage(BTreeNode foundNode)
        {
            BTreePage page = (BTreePage)_cache.TryKey(foundNode.PageOffset);

            if (page == null)
            {
                page = NewPage(foundNode);
                page.Read();
                _cache.CacheObject(foundNode.PageOffset, page);
            }
            return(page);
        }
Exemple #5
0
        public override void GetAllKeys(ArrayList keys_offsets)
        {
            RBNodeBase rbNode = _rbTree.GetMinimumNode();

            while (rbNode != null)
            {
                BTreePage page = PreparePage((BTreeNode)rbNode.Key);
                if (page.GetAllKeys(keys_offsets) == 0)
                {
                    break;
                }
                rbNode = _rbTree.GetNext(rbNode);
            }
        }
Exemple #6
0
        private BTreePage NewPage(BTreeNode treeNode)
        {
            BTreePage result = _freeNode;

            if (result == null)
            {
                result = new BTreePage(treeNode, _maxCount, _stream, _factoryKey);
            }
            else
            {
                result.SetPageData(treeNode, _maxCount, _factoryKey);
                _freeNode = null;
            }
            return(result);
        }
Exemple #7
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;
        }
Exemple #8
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);
            }
        }
Exemple #9
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);
                }
            }
        }
Exemple #10
0
 private void _cache_ObjectRemoved(object sender, ObjectCacheEventArgs e)
 {
     _freeNode = (BTreePage)e.Object;
     _freeNode.Write();
 }