Example #1
0
        static void TestExpressionDelete()
        {
            string dbPath = "./testdbfile.minidb";

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

            Expression expression = GetAndsExpression();

            for (int i = 1; i < 30; i++)
            {
                DBRecord record = GetTestRecord_expression(i, "str", (float)3.3);
                DBRecord key    = GetTestKey_expression(i);
                if (i == 17 || i == 19)
                {
                    record = GetTestRecord_expression(i, "str", (float)10.5);
                }
                else if (i == 20 || i == 23)
                {
                    record = GetTestRecord_expression(i, "www", (float)1.3);
                }
                root = controller.InsertCell(root, key, record);
            }
            List <AttributeDeclaration> attributeNames = new List <AttributeDeclaration>();

            AttributeDeclaration attribute_1 = new AttributeDeclaration();

            attribute_1.AttributeName = "a";
            attribute_1.IsUnique      = true;
            attributeNames.Add(attribute_1);

            AttributeDeclaration attribute_2 = new AttributeDeclaration();

            attribute_2.AttributeName = "b";
            attribute_2.IsUnique      = false;
            attributeNames.Add(attribute_2);

            AttributeDeclaration attribute_3 = new AttributeDeclaration();

            attribute_3.AttributeName = "c";
            attribute_3.IsUnique      = false;
            attributeNames.Add(attribute_3);

            root = controller.DeleteCells(root, expression, "a", attributeNames);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            pager.Close();
        }
Example #2
0
        public (IDatabaseController, Pager) UseDatabase(string databaseName)
        {
            // init
            string              dbPath          = $"./{databaseName}.minidb";
            Pager               pager           = new Pager(dbPath, 1024 * 8, 400);
            FreeList            freeList        = new FreeList(pager);
            IIndexManager       bTreeController = new BTreeController(pager, freeList, 40);
            IInterpreter        interpreter     = new Parsing();
            ICatalogManager     catalogManager  = new Catalog(databaseName);
            IRecordManager      recordManager   = new RecordContext(pager, bTreeController);
            IDatabaseController database        = new DatabaseController(interpreter, catalogManager, recordManager);

            return(database, pager);
        }
Example #3
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();
        }
Example #4
0
        static void TestMaxHeightBTree()
        {
            string dbPath = "./testdbfile.minidb";

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

            root = controller.OccupyNewTableNode();

            // init record
            DBRecord keyRecord = null;
            DBRecord record    = null;

            // insert
            keyRecord = GetTestBRecord(1);
            record    = GetTestBRecord(175.1, 1, "Person1", "000001", 18);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            keyRecord = GetTestBRecord(2);
            record    = GetTestBRecord(165.1, 2, "Person2", "000002", 19);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            keyRecord = GetTestBRecord(3);
            record    = GetTestBRecord(165.3, 3, "Person3", "000003", 20);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            keyRecord = GetTestBRecord(4);
            record    = GetTestBRecord(175.9, 4, "Person4", "000004", 21);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            keyRecord = GetTestBRecord(5);
            record    = GetTestBRecord(175.0, 5, "Person5", "000005", 22);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            keyRecord = GetTestBRecord(6);
            record    = GetTestBRecord(172.1, 6, "Person6", "000006", 23);
            // insert to tree
            root = controller.InsertCell(root, keyRecord, record);
            // visualize
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            Console.WriteLine();

            pager.Close();
        }
Example #5
0
        static void TestBTreeInsert()
        {
            LeafTableCell result = null;
            // init record
            DBRecord record_0 = GetTestBRecord(100);
            DBRecord record_1 = GetTestBRecord(101);
            DBRecord record_2 = GetTestBRecord(102);
            DBRecord record_3 = GetTestBRecord(103);
            DBRecord record_4 = GetTestBRecord(104);
            DBRecord record_5 = GetTestBRecord(105);
            DBRecord record_6 = GetTestBRecord(106);
            DBRecord record_7 = GetTestBRecord(107);

            DBRecord keyRecord_0 = GetTestBRecord(1);
            DBRecord keyRecord_1 = GetTestBRecord(2);
            DBRecord keyRecord_2 = GetTestBRecord(3);
            DBRecord keyRecord_3 = GetTestBRecord(4);
            DBRecord keyRecord_4 = GetTestBRecord(5);
            DBRecord keyRecord_5 = GetTestBRecord(6);
            DBRecord keyRecord_6 = GetTestBRecord(7);


            // init key
            string dbPath = "./testdbfile.minidb";

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

            BTreeNode root = null;

            root   = controller.InsertCell(root, keyRecord_0, record_0);
            result = (LeafTableCell)controller.FindCell(keyRecord_0, root);
            Assert.NotNull(result);
            Assert.Equal(1, result.Key.GetValues()[0].IntegerValue);
            //1
            root   = controller.InsertCell(root, keyRecord_1, record_1);
            result = (LeafTableCell)controller.FindCell(keyRecord_1, root);
            Assert.NotNull(result);
            Assert.Equal(2, result.Key.GetValues()[0].IntegerValue);
            //2
            root   = controller.InsertCell(root, keyRecord_2, record_2);
            result = (LeafTableCell)controller.FindCell(keyRecord_2, root);
            Assert.NotNull(result);
            Assert.Equal(3, result.Key.GetValues()[0].IntegerValue);
            //3
            root   = controller.InsertCell(root, keyRecord_3, record_3);
            result = (LeafTableCell)controller.FindCell(keyRecord_3, root);
            Assert.NotNull(result);
            Assert.Equal(4, result.Key.GetValues()[0].IntegerValue);
            //4
            root   = controller.InsertCell(root, keyRecord_4, record_4);
            result = (LeafTableCell)controller.FindCell(keyRecord_4, root);
            Assert.NotNull(result);
            Assert.Equal(5, result.Key.GetValues()[0].IntegerValue);
            //5
            root   = controller.InsertCell(root, keyRecord_5, record_5);
            result = (LeafTableCell)controller.FindCell(keyRecord_5, root);
            Assert.NotNull(result);
            Assert.Equal(6, result.Key.GetValues()[0].IntegerValue);

            //6
            root   = controller.InsertCell(root, keyRecord_6, record_6);
            result = (LeafTableCell)controller.FindCell(keyRecord_6, root);
            Assert.NotNull(result);
            Assert.Equal(7, result.Key.GetValues()[0].IntegerValue);
            //Find

            result = (LeafTableCell)controller.FindCell(keyRecord_0, root);
            Assert.NotNull(result);
            Assert.Equal(1, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_1, root);
            Assert.NotNull(result);
            Assert.Equal(2, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_2, root);
            Assert.NotNull(result);
            Assert.Equal(3, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_3, root);
            Assert.NotNull(result);
            Assert.Equal(4, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_4, root);
            Assert.NotNull(result);
            Assert.Equal(5, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_5, root);
            Assert.NotNull(result);
            Assert.Equal(6, result.Key.GetValues()[0].IntegerValue);

            result = (LeafTableCell)controller.FindCell(keyRecord_6, root);
            Assert.NotNull(result);
            Assert.Equal(7, result.Key.GetValues()[0].IntegerValue);

            pager.Close();
        }
Example #6
0
        static void HardTestForBTree()
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager           pager      = new Pager(dbPath);
            FreeList        freeList   = new FreeList(pager);
            BTreeController controller = new BTreeController(pager, freeList, 4);
            BTreeNode       root       = null;
            LeafTableCell   result     = null;

            //Construct BTree
            for (int i = 1; i < 20; i++)
            {
                DBRecord record    = GetTestBRecord(i + 100);
                DBRecord keyRecord = GetTestBRecord(i);
                root = controller.InsertCell(root, keyRecord, record);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.NotNull(result);
                Assert.Equal(i, result.Key.GetValues()[0].IntegerValue);
            }
            // test inserting records with repeated primary keys
            DBRecord record_D    = GetTestBRecord(103);
            DBRecord keyRecord_D = GetTestBRecord(3);
            bool     isError     = false;

            try
            {
                root = controller.InsertCell(root, keyRecord_D, record_D);
            }
            catch (RepeatedKeyException)
            {
                isError = true;
            }
            Assert.True(isError);

            isError     = false;
            record_D    = GetTestBRecord(105);
            keyRecord_D = GetTestBRecord(5);
            try
            {
                root = controller.InsertCell(root, keyRecord_D, record_D);
            }
            catch (RepeatedKeyException)
            {
                isError = true;
            }

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            //find all
            for (int i = 1; i < 20; i++)
            {
                DBRecord keyRecord = GetTestBRecord(i);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.NotNull(result);
                Assert.Equal(i, result.Key.GetValues()[0].IntegerValue);
            }

            //delete
            for (int i = 10; i < 20; i++)
            {
                DBRecord keyRecord = GetTestBRecord(i);
                root = controller.Delete(keyRecord, root);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.Null(result);

                for (int m = 1; m < 10; m++)
                {
                    DBRecord keyRecord_check = GetTestBRecord(m);

                    result = (LeafTableCell)controller.FindCell(keyRecord_check, root);
                    Assert.NotNull(result);
                    Assert.Equal(m, result.Key.GetValues()[0].IntegerValue);
                }
            }
            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            //find others
            for (int i = 1; i < 10; i++)
            {
                DBRecord keyRecord = GetTestBRecord(i);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.NotNull(result);
                Assert.Equal(i, result.Key.GetValues()[0].IntegerValue);
            }


            //insert after delete
            for (int i = 10; i < 20; i++)
            {
                DBRecord record    = GetTestBRecord(i + 100);
                DBRecord keyRecord = GetTestBRecord(i);
                root = controller.InsertCell(root, keyRecord, record);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.NotNull(result);
                Assert.Equal(i, result.Key.GetValues()[0].IntegerValue);
            }

            //find all
            for (int i = 1; i < 20; i++)
            {
                DBRecord keyRecord = GetTestBRecord(i);

                result = (LeafTableCell)controller.FindCell(keyRecord, root);
                Assert.NotNull(result);
                Assert.Equal(i, result.Key.GetValues()[0].IntegerValue);
            }

            pager.Close();
        }
Example #7
0
        static void TestExpressionFind()
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager            pager      = new Pager(dbPath);
            FreeList         freeList   = new FreeList(pager);
            BTreeController  controller = new BTreeController(pager, freeList);
            BTreeNode        root       = null;
            List <BTreeCell> result     = null;

            Expression expression = GetAndsExpression();

            for (int i = 1; i < 30; i++)
            {
                DBRecord record = GetTestRecord_expression(i, "str", (float)3.3);
                DBRecord key    = GetTestKey_expression(i);
                if (i == 17 || i == 19)
                {
                    record = GetTestRecord_expression(i, "str", (float)10.5);
                }
                else if (i == 20 || i == 23)
                {
                    record = GetTestRecord_expression(i, "www", (float)1.3);
                }
                root = controller.InsertCell(root, key, record);
            }
            List <AttributeDeclaration> attributeNames = new List <AttributeDeclaration>();

            AttributeDeclaration attribute_1 = new AttributeDeclaration();

            attribute_1.AttributeName = "a";
            attribute_1.IsUnique      = true;
            attributeNames.Add(attribute_1);

            AttributeDeclaration attribute_2 = new AttributeDeclaration();

            attribute_2.AttributeName = "b";
            attribute_2.IsUnique      = false;
            attributeNames.Add(attribute_2);

            AttributeDeclaration attribute_3 = new AttributeDeclaration();

            attribute_3.AttributeName = "c";
            attribute_3.IsUnique      = false;
            attributeNames.Add(attribute_3);

            result = controller.FindCells(root, expression, "a", attributeNames);

            foreach (var cell in result)
            {
                LeafTableCell leafTableCell = (LeafTableCell)cell;
                Console.Write(leafTableCell.DBRecord.GetValues()[0].IntegerValue);
                Console.Write("|");
                Console.Write(leafTableCell.DBRecord.GetValues()[1].StringValue);
                Console.Write("|");
                Console.Write(leafTableCell.DBRecord.GetValues()[2].FloatValue);
                Console.WriteLine();
            }

            pager.Close();
        }
Example #8
0
        static void BugTest3()
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager           pager      = new Pager(dbPath);
            FreeList        freeList   = new FreeList(pager);
            BTreeController controller = new BTreeController(pager, freeList);
            BTreeNode       root       = null;
            LeafTableCell   result     = null;
            // 1
            // DBRecord record = GetTestBRecord(74396264);
            // DBRecord keyRecord = GetTestBRecord(74396264);
            DBRecord record    = GetTestBRecord(1);
            DBRecord keyRecord = GetTestBRecord(1);

            root = controller.InsertCell(root, keyRecord, record);
            // 2
            // record = GetTestBRecord(1766307441);
            // keyRecord = GetTestBRecord(1766307441);
            record    = GetTestBRecord(7);
            keyRecord = GetTestBRecord(7);
            root      = controller.InsertCell(root, keyRecord, record);
            // 3
            // record = GetTestBRecord(2025306881);
            // keyRecord = GetTestBRecord(2025306881);
            record    = GetTestBRecord(8);
            keyRecord = GetTestBRecord(8);
            root      = controller.InsertCell(root, keyRecord, record);
            // 4
            // record = GetTestBRecord(147488698);
            // keyRecord = GetTestBRecord(147488698);
            record    = GetTestBRecord(2);
            keyRecord = GetTestBRecord(2);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);
            // 5
            // record = GetTestBRecord(1109110087);
            // keyRecord = GetTestBRecord(1109110087);
            record    = GetTestBRecord(4);
            keyRecord = GetTestBRecord(4);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);
            // test 5
            // keyRecord = GetTestBRecord(1109110087);
            keyRecord = GetTestBRecord(4);
            result    = (LeafTableCell)controller.FindCell(keyRecord, root);
            Assert.NotNull(result);
            // Assert.Equal(==, result.Key.GetValues()[0].IntegerValue 1109110087);
            Assert.Equal(4, result.Key.GetValues()[0].IntegerValue);

            // 6
            // record = GetTestBRecord(1163206015);
            // keyRecord = GetTestBRecord(1163206015);
            record    = GetTestBRecord(5);
            keyRecord = GetTestBRecord(5);
            // ISSUE HERE
            root = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            // 7
            // record = GetTestBRecord(1485715653);
            // keyRecord = GetTestBRecord(1485715653);
            record    = GetTestBRecord(6);
            keyRecord = GetTestBRecord(6);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            // 8
            // record = GetTestBRecord(1087082570);
            // keyRecord = GetTestBRecord(1087082570);
            record    = GetTestBRecord(3);
            keyRecord = GetTestBRecord(3);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            pager.Close();
        }
Example #9
0
        static void Bugtest2()
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager           pager      = new Pager(dbPath);
            FreeList        freeList   = new FreeList(pager);
            BTreeController controller = new BTreeController(pager, freeList);
            BTreeNode       root       = null;
            LeafTableCell   result     = null;

            DBRecord record    = GetTestBRecord(660132168);
            DBRecord keyRecord = GetTestBRecord(660132168);

            root = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(2007593075);
            keyRecord = GetTestBRecord(2007593075);
            root      = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(356456016);
            keyRecord = GetTestBRecord(356456016);
            root      = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(32731844);
            keyRecord = GetTestBRecord(32731844);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(159431057);
            keyRecord = GetTestBRecord(159431057);
            root      = controller.InsertCell(root, keyRecord, record);

            keyRecord = GetTestBRecord(660132168);
            result    = (LeafTableCell)controller.FindCell(keyRecord, root);
            Assert.NotNull(result);
            Assert.Equal(660132168, result.Key.GetValues()[0].IntegerValue);


            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(991596943);
            keyRecord = GetTestBRecord(991596943);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(794643883);
            keyRecord = GetTestBRecord(794643883);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(1158712065);
            keyRecord = GetTestBRecord(1158712065);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            pager.Close();
        }
Example #10
0
        static void BugTest1()
        {
            string dbPath = "./testdbfile.minidb";

            File.Delete(dbPath);
            Pager           pager      = new Pager(dbPath);
            FreeList        freeList   = new FreeList(pager);
            BTreeController controller = new BTreeController(pager, freeList);
            BTreeNode       root       = null;
            LeafTableCell   result     = null;

            DBRecord record    = GetTestBRecord(76767785);
            DBRecord keyRecord = GetTestBRecord(76767785);

            root = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(1922063022);
            keyRecord = GetTestBRecord(1922063022);
            root      = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(514874720);
            keyRecord = GetTestBRecord(514874720);
            root      = controller.InsertCell(root, keyRecord, record);

            record    = GetTestBRecord(724803552);
            keyRecord = GetTestBRecord(724803552);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(1219882375);
            keyRecord = GetTestBRecord(1219882375);
            root      = controller.InsertCell(root, keyRecord, record);

            keyRecord = GetTestBRecord(724803552);
            result    = (LeafTableCell)controller.FindCell(keyRecord, root);
            Assert.NotNull(result);
            Assert.Equal(724803552, result.Key.GetValues()[0].IntegerValue);


            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(681446986);
            keyRecord = GetTestBRecord(681446986);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(1427789753);
            keyRecord = GetTestBRecord(1427789753);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            record    = GetTestBRecord(1066176166);
            keyRecord = GetTestBRecord(1066176166);
            root      = controller.InsertCell(root, keyRecord, record);

            BTreeNodeHelper.VisualizeIntegerTree(pager, root);

            pager.Close();
        }