示例#1
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();
        }
示例#2
0
        static void TestBTreeDelete()
        {
            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 record_8 = GetTestBRecord(108);

            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);
            DBRecord keyRecord_7 = GetTestBRecord(8);
            DBRecord keyRecord_8 = GetTestBRecord(9);


            // 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;

            //0
            root = controller.InsertCell(root, keyRecord_0, record_0);
            //1
            root = controller.InsertCell(root, keyRecord_1, record_1);
            //2
            root = controller.InsertCell(root, keyRecord_2, record_2);
            //3
            root = controller.InsertCell(root, keyRecord_3, record_3);
            //4
            root = controller.InsertCell(root, keyRecord_4, record_4);
            //5
            root = controller.InsertCell(root, keyRecord_5, record_5);
            //6
            root = controller.InsertCell(root, keyRecord_6, record_6);

            //Delete test;

            //6
            root   = controller.Delete(keyRecord_6, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_6, root);
            Assert.Null(result);

            //5
            root   = controller.Delete(keyRecord_5, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_5, root);
            Assert.Null(result);

            //4
            root   = controller.Delete(keyRecord_4, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_4, root);
            Assert.Null(result);


            //3
            root   = controller.Delete(keyRecord_3, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_3, root);
            Assert.Null(result);

            //2
            root   = controller.Delete(keyRecord_2, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_2, root);
            Assert.Null(result);

            //1
            root   = controller.Delete(keyRecord_1, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_1, root);
            Assert.Null(result);


            //0
            root   = controller.Delete(keyRecord_0, root);
            result = (LeafTableCell)controller.FindCell(keyRecord_0, root);
            Assert.Null(result);


            //insert after delete
            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);

            root   = controller.InsertCell(root, keyRecord_7, record_7);
            result = (LeafTableCell)controller.FindCell(keyRecord_7, root);
            Assert.NotNull(result);
            Assert.Equal(8, result.Key.GetValues()[0].IntegerValue);


            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);

            pager.Close();
        }