Ejemplo n.º 1
0
        // return null if not found
        public List <AtomValue> SelectRecord(AtomValue key, int rootPage)
        {
            List <AtomValue> wrapper = new List <AtomValue> {
                key
            };
            DBRecord         keyDBRecord = new DBRecord(wrapper);
            BTreeNode        node        = BTreeNodeHelper.GetBTreeNode(_pager, rootPage);
            BTreeCell        cell        = _bTree.FindCell(keyDBRecord, node);
            List <AtomValue> result      = null;

            result = ((LeafTableCell)cell)?.DBRecord.GetValues();
            return(result);
        }
Ejemplo n.º 2
0
        private BTreeNode Delete_leaf_redistri(BTreeNode NodetobeDeleted, DBRecord key, BTreeNode Root)
        {
            BTreeCell cell;
            UInt16    offset;
            int       indexInOffsetArray;

            MemoryPage parentPage = _pager.ReadPage((int)NodetobeDeleted.ParentPage);
            BTreeNode  parentNode = new BTreeNode(parentPage);

            (cell, offset, indexInOffsetArray) = parentNode.FindBTreeCell(key);
            //the deleted node is not on the right page of parentNode
            if (cell != null)
            {
                MemoryPage brotherPage = _pager.ReadPage((int)NodetobeDeleted.RightPage);
                BTreeNode  brotherNode = new BTreeNode(brotherPage);
                //If there is a node on the left of the deleted node,it need to be connected to the brother node.
                if (indexInOffsetArray >= 1)
                {
                    InternalTableCell leftNodeCell = (InternalTableCell)parentNode.GetBTreeCell(parentNode.CellOffsetArray[indexInOffsetArray - 1]);
                    MemoryPage        leftPage     = _pager.ReadPage((int)leftNodeCell.ChildPage);
                    BTreeNode         leftNode     = new BTreeNode(leftPage);
                    leftNode.RightPage = (uint)brotherNode.RawPage.PageNumber;
                }

                if (brotherNode.NumCells + NodetobeDeleted.NumCells <= MaxCell)  //merge
                {
                    //After the merge,one cell in the parentNode will be deleted
                    parentNode.DeleteBTreeCell(cell);
                    //merge two node
                    for (int i = 0; i < NodetobeDeleted.NumCells; i++)
                    {
                        brotherNode.InsertBTreeCell(NodetobeDeleted.GetBTreeCell(NodetobeDeleted.CellOffsetArray[i]));
                    }
                    DeleteNode(NodetobeDeleted);
                }
                else    //redistribute
                {
                    BTreeCell movedCell = brotherNode.GetBTreeCell(brotherNode.CellOffsetArray[0]);
                    DBRecord  newKey    = brotherNode.GetBTreeCell(brotherNode.CellOffsetArray[1]).Key;

                    NodetobeDeleted.InsertBTreeCell(movedCell);
                    brotherNode.DeleteBTreeCell(movedCell);

                    BTreeCell parent_Cell;
                    UInt16    parent_offset;
                    int       parent_indexInOffsetArray;
                    (parent_Cell, parent_offset, parent_indexInOffsetArray) = parentNode.FindBTreeCell(key);

                    InternalTableCell newCell = new InternalTableCell(newKey, (uint)NodetobeDeleted.RawPage.PageNumber);
                    parentNode.DeleteBTreeCell(parent_Cell);
                    parentNode.InsertBTreeCell(newCell);
                }
            }
            //The node to be deleted is on the rightpage,the brother node is on the left
            else
            {
                InternalTableCell brotherCell = (InternalTableCell)parentNode.GetBTreeCell(parentNode.CellOffsetArray[parentNode.NumCells - 1]);
                MemoryPage        brotherPage = _pager.ReadPage((int)brotherCell.ChildPage);
                BTreeNode         brotherNode = new BTreeNode(brotherPage);

                //The right page of brother node should be 0

                brotherNode.RightPage = NodetobeDeleted.RightPage;

                if (brotherNode.NumCells + NodetobeDeleted.NumCells <= MaxCell)  //merge
                {
                    //After the merge,one cell in the parentNode will be deleted
                    parentNode.DeleteBTreeCell(brotherCell);
                    //merge two node
                    for (int i = 0; i < NodetobeDeleted.NumCells; i++)
                    {
                        brotherNode.InsertBTreeCell(NodetobeDeleted.GetBTreeCell(NodetobeDeleted.CellOffsetArray[i]));
                    }
                    DeleteNode(NodetobeDeleted);
                    parentNode.RightPage = (uint)brotherNode.RawPage.PageNumber;
                }
                else    //redistribute
                {
                    BTreeCell movedCell = brotherNode.GetBTreeCell(brotherNode.CellOffsetArray[brotherNode.NumCells - 1]);
                    DBRecord  newKey    = movedCell.Key;

                    NodetobeDeleted.InsertBTreeCell(movedCell);
                    brotherNode.DeleteBTreeCell(movedCell);

                    BTreeCell parent_Cell;
                    UInt16    parent_offset;
                    int       parent_indexInOffsetArray;
                    (parent_Cell, parent_offset, parent_indexInOffsetArray) = parentNode.FindBTreeCell(key);

                    InternalTableCell newCell = new InternalTableCell(newKey, (uint)NodetobeDeleted.RawPage.PageNumber);
                    parentNode.DeleteBTreeCell(parent_Cell);
                    parentNode.InsertBTreeCell(newCell);
                }
            }


            NodetobeDeleted = parentNode;
            //deal with parent Nodes
            return(Delete_internal_redistribute(NodetobeDeleted, key, Root));
        }
Ejemplo n.º 3
0
        public BTreeNode Delete(BTreeCell keyCell, BTreeNode Root)
        {
            DBRecord key = keyCell.Key;

            return(Delete(key, Root));
        }
Ejemplo n.º 4
0
 static void AssertCell(BTreeCell c1, BTreeCell c2)
 {
     Assert.Equal(c1.Types, c2.Types);
     AssertDBRecords(c1.Key, c2.Key);
 }
Ejemplo n.º 5
0
 public static void AssertCell(BTreeCell c1, BTreeCell c2)
 {
     Debug.Assert(c1.Types == c2.Types);
     AssertDBRecords(c1.Key, c2.Key);
 }