Exemple #1
0
        public int DeleteRecords(Expression condition, string primaryKeyName, List <AttributeDeclaration> attributeDeclarations, int rootPage)
        {
            BTreeNode node    = BTreeNodeHelper.GetBTreeNode(_pager, rootPage);
            BTreeNode newRoot = _bTree.DeleteCells(node, condition, primaryKeyName, attributeDeclarations);

            return(newRoot.RawPage.PageNumber);
        }
Exemple #2
0
        public int CreateIndex(int tableRootPage, string indexedColumnName, List <AttributeDeclaration> attributeDeclarations)
        {
            BTreeNode indexRoot = _bTree.OccupyNewTableNode();
            BTreeNode tableRoot = BTreeNodeHelper.GetBTreeNode(_pager, tableRootPage);

            int indexedColumnIndex = attributeDeclarations.FindIndex(x => x.AttributeName == indexedColumnName);

            foreach (BTreeCell tableCell in _bTree.LinearSearch(tableRoot))
            {
                AtomValue indexedValue = ((LeafTableCell)tableCell).DBRecord.GetValues()[indexedColumnIndex];
                AtomValue primaryKey   = ((LeafTableCell)tableCell).Key.GetValues()[0];
                // List<AtomValue> indexPrimaryKeyPair = new List<AtomValue>() { indexedValue, primaryKey };
                List <AtomValue> wrappedPrimaryKey = new List <AtomValue>()
                {
                    primaryKey
                };
                DBRecord wrappedKey = new DBRecord(new List <AtomValue>()
                {
                    indexedValue
                });
                // DBRecord wrappedValues = new DBRecord(indexPrimaryKeyPair);
                DBRecord wrappedValues = new DBRecord(wrappedPrimaryKey);
                indexRoot = _bTree.InsertCell(indexRoot, wrappedKey, wrappedValues);
            }

            return(indexRoot.RawPage.PageNumber);
        }
Exemple #3
0
        // return new root page number
        public int InsertRecord(List <AtomValue> values, AtomValue key, int rootPage)
        {
            BTreeNode node       = BTreeNodeHelper.GetBTreeNode(_pager, rootPage);
            DBRecord  wrappedKey = new DBRecord(new List <AtomValue>()
            {
                key
            });
            DBRecord  wrappedValues = new DBRecord(values);
            BTreeNode newRoot       = _bTree.InsertCell(node, wrappedKey, wrappedValues);

            return(newRoot.RawPage.PageNumber);
        }
Exemple #4
0
        public List <List <AtomValue> > SelectRecords(SelectStatement selectStatement, string primaryKeyName, List <AttributeDeclaration> attributeDeclarations, int rootPage)
        {
            BTreeNode                node  = BTreeNodeHelper.GetBTreeNode(_pager, rootPage);
            List <BTreeCell>         cells = _bTree.FindCells(node, selectStatement.Condition, primaryKeyName, attributeDeclarations);
            List <List <AtomValue> > rows  = new List <List <AtomValue> >();

            foreach (BTreeCell cell in cells)
            {
                List <AtomValue> row = ((LeafTableCell)cell).DBRecord.GetValues();
                rows.Add(row);
            }
            return(rows);
        }
Exemple #5
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);
        }
Exemple #6
0
        private static void TestInsertRandomRecord(int maxCell)
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager           pager      = new Pager(dbPath);
            FreeList        freeList   = new FreeList(pager);
            BTreeController controller = new BTreeController(pager, freeList);

            RecordContext recordManager = new RecordContext(pager, controller);

            // create new table
            CreateStatement createStatement = GetCreateStatement();
            int             newRoot         = recordManager.CreateTable();

            // insert
            BTreeNode       node;
            InsertStatement insertStatement;
            int             key;
            int             newRootAfterInsert = newRoot;
            int             i;

            for (i = 0; i < maxCell; i++)
            {
                (insertStatement, key) = GetInsertStatement(1);
                AtomValue atomValue = GetAtomValue(key);

                newRootAfterInsert = recordManager.InsertRecord(insertStatement.Values, atomValue, newRootAfterInsert);
                Console.WriteLine(key);
                Debug.Assert(newRoot == newRootAfterInsert);
            }
            node = BTreeNodeHelper.GetBTreeNode(pager, newRootAfterInsert);
            BTreeNodeHelper.VisualizeIntegerTree(pager, node);
            Console.WriteLine();

            for (i = 0; i < maxCell; i++)
            {
                (insertStatement, key) = GetInsertStatement(1);
                AtomValue atomValue = GetAtomValue(key);
                newRootAfterInsert = recordManager.InsertRecord(insertStatement.Values, atomValue, newRootAfterInsert);
                Console.WriteLine(key);
            }
            node = BTreeNodeHelper.GetBTreeNode(pager, newRootAfterInsert);
            BTreeNodeHelper.VisualizeIntegerTree(pager, node);

            pager.Close();
        }
        /// <summary>
        /// Search the leaf nodes linearly.
        /// </summary>
        /// <param name="root">the root node of the B+ tree</param>
        /// <returns></returns>
        public IEnumerable <BTreeCell> LinearSearch(BTreeNode root)
        {
            BTreeNode startNode = FindMin(root);

            while (true)
            {
                foreach (var cell in startNode)
                {
                    yield return(cell);
                }
                if (startNode.RightPage == 0)
                {
                    break;
                }
                startNode = BTreeNodeHelper.GetBTreeNode(_pager, (int)startNode.RightPage);
            }
        }