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); }
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); }
public static void TestLeafTableNode() { string dbPath = "./testdbfile.minidb"; File.Delete(dbPath); Pager pager = new Pager(dbPath); MemoryPage page = pager.GetNewPage(); BTreeNode node = new BTreeNode(page, PageTypes.LeafTablePage); // init record DBRecord keyRecord = null; DBRecord record = null; LeafTableCell leafTableCell = null; keyRecord = GetTestBRecord(1); record = GetTestBRecord(175.1, 1, "Person1", "000001", 18); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); keyRecord = GetTestBRecord(2); record = GetTestBRecord(165.1, 2, "Person2", "000002", 19); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); keyRecord = GetTestBRecord(3); record = GetTestBRecord(165.3, 3, "Person3", "000003", 20); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); keyRecord = GetTestBRecord(4); record = GetTestBRecord(175.9, 4, "Person4", "000004", 21); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); keyRecord = GetTestBRecord(5); record = GetTestBRecord(175.0, 5, "Person5", "000005", 22); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); // visualize BTreeNodeHelper.VisualizeIntegerTree(pager, node); keyRecord = GetTestBRecord(6); record = GetTestBRecord(172.1, 6, "Person6", "000006", 23); // build cell + insert to node leafTableCell = new LeafTableCell(keyRecord, record); node.InsertBTreeCell(leafTableCell); // visualize BTreeNodeHelper.VisualizeIntegerTree(pager, node); pager.Close(); }
// 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); }
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(); }
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); }
// 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); }
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); } }
static DBRecord GetTestBRecord(int key = 222) { // init record List <AtomValue> values = new List <AtomValue>(); AtomValue value1 = new AtomValue() { Type = AttributeTypes.Int, IntegerValue = key }; // AtomValue value2 = new AtomValue() { Type = AttributeTypes.Null }; // AtomValue value3 = new AtomValue() { Type = AttributeTypes.Char, CharLimit = 5, StringValue = "222" }; values.Add(value1); // values.Add(value2); // values.Add(value3); DBRecord record = new DBRecord(values); DBRecord cloneRecord = new DBRecord(record.Pack(), 0); BTreeNodeHelper.AssertDBRecords(record, cloneRecord); return(record); }
static DBRecord GetTestBRecord(double height, int pid, string name, string identity, int age) { // init record List <AtomValue> values = new List <AtomValue>(); AtomValue value1 = new AtomValue() { Type = AttributeTypes.Float, FloatValue = height }; AtomValue value2 = new AtomValue() { Type = AttributeTypes.Int, IntegerValue = pid }; AtomValue value3 = new AtomValue() { Type = AttributeTypes.Char, CharLimit = 32, StringValue = name }; AtomValue value4 = new AtomValue() { Type = AttributeTypes.Char, CharLimit = 128, StringValue = identity }; AtomValue value5 = new AtomValue() { Type = AttributeTypes.Int, IntegerValue = age }; values.Add(value1); values.Add(value2); values.Add(value3); values.Add(value4); values.Add(value5); DBRecord record = new DBRecord(values); DBRecord cloneRecord = new DBRecord(record.Pack(), 0); BTreeNodeHelper.AssertDBRecords(record, cloneRecord); return(record); }
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(); }
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(); }
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(); }
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(); }
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(); }