コード例 #1
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;
 }
コード例 #2
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);
            }
        }
コード例 #3
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);
        }
コード例 #4
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);
                }
            }
        }
コード例 #5
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));
                }
            }
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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);
                }
            }
        }