Esempio n. 1
0
        private void Split()
        {
            if (_currentPage.SelfIndex == BTreeHeaderPage.RootIndex)                    //root split

            {
                SplitRoot();
                return;
            }

            var pathPart      = _path.Pop();
            var parentPage    = pathPart.ParentPage;
            var childrenIndex = pathPart.ChildrenIndex;

            var newPage = new BTreePage(parentPage.SelfIndex, BTreeFreeSpaceMap.AllocatePage());

            if (!_currentPage.IsLeaf)
            {
                newPage.IsLeaf = false;
            }

            parentPage.Childrens.Insert(childrenIndex, newPage.SelfIndex);

            var recordsPerPage = _currentPage.GetPresentRecords() / 2;

            for (var i = 0; i < recordsPerPage; i++)
            {
                newPage.InsertRecord(_currentPage.PopRecordAt(0, false));
            }

            if (!_currentPage.IsLeaf)
            {
                for (var i = 0; i < recordsPerPage + 1; i++)
                {
                    newPage.Childrens.Add(_currentPage.PopChildrenAt(0));
                }
            }

            var forParent = _currentPage.PopRecordAt(0, false);

            BtreeIO.WritePage(_currentPage.GetBTreePage(), _currentPage.SelfIndex);
            BtreeIO.WritePage(newPage.GetBTreePage(), newPage.SelfIndex);

            try {
                parentPage.InsertRecord(forParent);

                BtreeIO.WritePage(parentPage.GetBTreePage(), parentPage.SelfIndex);
            } catch (PageOverflowException) {
                _currentPage = parentPage;

                if (Compensate())
                {
                    return;
                }

                Split();
            }
        }
Esempio n. 2
0
        private void CompensatePages(BTreePage left, BTreePage right, BTreePage parent, int childrenIndex)
        {
            int parentRecordIndex = 0, recordsToTransfer;

            if (left.GetPresentRecords() < right.GetPresentRecords())
            {
                for (var i = 0; i < parent.Childrens.Count; i++)
                {
                    if (parent.Childrens[i] == left.SelfIndex)
                    {
                        parentRecordIndex = i;
                    }
                }
                //parentRecordIndex = childrenIndex - 1;
                recordsToTransfer = (right.GetPresentRecords() - left.GetPresentRecords()) / 2;

                left.InsertRecord(parent.Records[parentRecordIndex]);
                for (var i = 0; i < recordsToTransfer - 1; i++)
                {
                    left.InsertRecord(right.PopRecordAt(0, false));
                }
                if (!right.IsLeaf)
                {
                    for (var i = 0; i < recordsToTransfer; i++)
                    {
                        left.Childrens.Add(right.PopChildrenAt(0));
                    }
                }

                parent.Records[parentRecordIndex] = right.PopRecordAt(0, false);
            }
            else
            {
                for (var i = 0; i < parent.Childrens.Count; i++)
                {
                    if (parent.Childrens[i] == left.SelfIndex)
                    {
                        parentRecordIndex = i;
                    }
                }
                //parentRecordIndex = childrenIndex;
                recordsToTransfer = (left.GetPresentRecords() - right.GetPresentRecords()) / 2;

                right.InsertRecord(parent.Records[parentRecordIndex]);

                for (var i = 0; i < recordsToTransfer - 1; i++)
                {
                    right.InsertRecord(left.PopRecordAt(left.Records.Count - 1, false));
                }
                if (!left.IsLeaf)
                {
                    for (var i = 0; i < recordsToTransfer; i++)
                    {
                        right.Childrens.Insert(0, left.PopChildrenAt(left.Childrens.Count - 1));
                    }
                }

                parent.Records[parentRecordIndex] = left.PopRecordAt(left.Records.Count - 1, false);
            }
        }
Esempio n. 3
0
        private void Merge()
        {
            var       partPath = _path.Pop();
            var       parentPage = partPath.ParentPage;
            var       childrenIndex = partPath.ChildrenIndex;
            BTreePage leftBrother = null, rightBrother = null;

            if (childrenIndex == 0)                 //zrobic zapisy
            {
                rightBrother = new BTreePage(BtreeIO.ReadPage(parentPage.Childrens[childrenIndex + 1]));

                var recordsToTransfer = rightBrother.GetPresentRecords();
                for (var i = 0; i < recordsToTransfer; i++)
                {
                    _currentPage.InsertRecord(rightBrother.PopRecordAt(0, true));
                }
                if (!_currentPage.IsLeaf)
                {
                    for (var i = 0; i < recordsToTransfer + 1; i++)
                    {
                        _currentPage.Childrens.Add(rightBrother.PopChildrenAt(0));
                    }
                }

                parentPage.PopChildrenAt(childrenIndex + 1);

                _currentPage.InsertRecord(parentPage.Records[childrenIndex]);

                BTreeFreeSpaceMap.FreePage(rightBrother.SelfIndex);
                BtreeIO.WritePage(_currentPage.GetBTreePage(), _currentPage.SelfIndex);
            }
            else
            {
                leftBrother = new BTreePage(BtreeIO.ReadPage(parentPage.Childrens[childrenIndex - 1]));

                var recordsToTransfer = _currentPage.GetPresentRecords();
                for (var i = 0; i < recordsToTransfer; i++)
                {
                    leftBrother.InsertRecord(_currentPage.PopRecordAt(0, true));
                }
                if (!_currentPage.IsLeaf)
                {
                    for (var i = 0; i < recordsToTransfer + 1; i++)
                    {
                        leftBrother.Childrens.Add(_currentPage.PopChildrenAt(0));
                    }
                }

                parentPage.PopChildrenAt(childrenIndex);

                leftBrother.InsertRecord(parentPage.Records[childrenIndex - 1]);

                BTreeFreeSpaceMap.FreePage(_currentPage.SelfIndex);
                BtreeIO.WritePage(leftBrother.GetBTreePage(), leftBrother.SelfIndex);
            }

            try {
                if (childrenIndex == 0)
                {
                    parentPage.PopRecordAt(childrenIndex, false);
                }
                else
                {
                    parentPage.PopRecordAt(childrenIndex - 1, false);
                }
                BtreeIO.WritePage(parentPage.GetBTreePage(), parentPage.SelfIndex);
            } catch (PageDeficitException ex) {
                _currentPage = parentPage;
                if (!Compensate())
                {
                    Merge();
                }
            } catch (RootPageDeficitException ex) {
                BTreeFreeSpaceMap.FreePage(RootPage.SelfIndex);
                if (childrenIndex == 0)
                {
                    RootPage = _currentPage;
                    BTreeHeaderPage.RootIndex = _currentPage.SelfIndex;
                }
                else
                {
                    RootPage = leftBrother;
                    BTreeHeaderPage.RootIndex = leftBrother.SelfIndex;
                }
            }
        }