static void TestInternalIndexCell() { // init record DBRecord record = GetTestBRecord(); // init key List <AtomValue> keyValues = new List <AtomValue>(); AtomValue key = new AtomValue() { Type = AttributeTypes.Char, CharLimit = 8, StringValue = "114514" }; keyValues.Add(key); DBRecord keyRecord = new DBRecord(keyValues); // make raw bytes List <byte> rawNode = new List <byte>(); rawNode.AddRange(new byte[30]); // build cell InternalIndexCell internalIndexCell = new InternalIndexCell(record, 114514, keyRecord); byte[] raw = internalIndexCell.Pack(); rawNode.AddRange(raw); // clone InternalIndexCell clone = new InternalIndexCell(rawNode.ToArray(), 30); // assert AssertCell(internalIndexCell, clone); Assert.Equal(internalIndexCell.ChildPage, clone.ChildPage); AssertDBRecords(internalIndexCell.PrimaryKey, clone.PrimaryKey); }
static void TestInsertIntoAndDeletionInsideBTreeNode() { string dbPath = "./testdbfile.minidb"; File.Delete(dbPath); Pager pager = new Pager(dbPath); pager.ExtendNumberOfPages(); pager.ExtendNumberOfPages(); MemoryPage page = pager.ReadPage(1); BTreeNode node = new BTreeNode(page, PageTypes.InternalIndexPage); // `keys` := (1, 6, 2, 5, 3, 4) List <DBRecord> keys = new List <DBRecord>(); keys.Add(GetTestBRecord(1)); keys.Add(GetTestBRecord(6)); keys.Add(GetTestBRecord(2)); keys.Add(GetTestBRecord(5)); keys.Add(GetTestBRecord(3)); keys.Add(GetTestBRecord(4)); // `cells` := ((1, 0), (6, 1), (2, 2), (5, 3), (3, 4), (4, 5)) List <InternalIndexCell> cells = new List <InternalIndexCell>(); cells.Add(new InternalIndexCell(keys[0], 114, GetTestBRecord(0))); cells.Add(new InternalIndexCell(keys[1], 114, GetTestBRecord(1))); cells.Add(new InternalIndexCell(keys[2], 114, GetTestBRecord(2))); cells.Add(new InternalIndexCell(keys[3], 114, GetTestBRecord(3))); cells.Add(new InternalIndexCell(keys[4], 114, GetTestBRecord(4))); cells.Add(new InternalIndexCell(keys[5], 114, GetTestBRecord(5))); byte[] raw = cells[0].Pack(); InternalIndexCell cellClone = new InternalIndexCell(raw, 0); node.InsertBTreeCell(cells[0]); node.InsertBTreeCell(cells[1]); node.InsertBTreeCell(cells[2]); node.InsertBTreeCell(cells[3]); node.InsertBTreeCell(cells[4]); node.InsertBTreeCell(cells[5]); List <ushort> offsets = node.CellOffsetArray; List <InternalIndexCell> clonecells = new List <InternalIndexCell>(); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[0])); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[1])); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[2])); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[3])); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[4])); clonecells.Add((InternalIndexCell)node.GetBTreeCell(offsets[5])); List <AtomValue> cloneCellKey = clonecells[0].Key.GetValues(); // all cells (not the list `cells`) inside `node`: // ((1, 0), (2, 2), (3, 4), (4, 5), (5, 3), (6, 1)) // they are all pointing to page #114 // check if the keys are stored in ascending order Assert.Equal(1, node.GetBTreeCell(offsets[0]).Key.GetValues()[0].IntegerValue); Assert.Equal(2, node.GetBTreeCell(offsets[1]).Key.GetValues()[0].IntegerValue); Assert.Equal(3, node.GetBTreeCell(offsets[2]).Key.GetValues()[0].IntegerValue); Assert.Equal(4, node.GetBTreeCell(offsets[3]).Key.GetValues()[0].IntegerValue); Assert.Equal(5, node.GetBTreeCell(offsets[4]).Key.GetValues()[0].IntegerValue); Assert.Equal(6, node.GetBTreeCell(offsets[5]).Key.GetValues()[0].IntegerValue); List <AtomValue> tmpAtomList = null; // check if `node` could be iterated by "foreach" statement int i = 1; foreach (var iteratedCell in node) { tmpAtomList = iteratedCell.Key.GetValues(); Assert.Equal(tmpAtomList[0].IntegerValue, i); i++; } // check node indexing AssertCell(node[0], cells[0]); AssertCell(node[1], cells[2]); BTreeCell cell; ushort offset; int indexInOffsetArray; // find by the keys below and check if it returns currect cells // key 6: value 1 (cell, offset, indexInOffsetArray) = node.FindBTreeCell(keys[1]); tmpAtomList = ((InternalIndexCell)cell).Key.GetValues(); Assert.Equal(6, tmpAtomList[0].IntegerValue); tmpAtomList = ((InternalIndexCell)cell).PrimaryKey.GetValues(); Assert.Equal(1, tmpAtomList[0].IntegerValue); // key 5: value 3 (cell, offset, indexInOffsetArray) = node.FindBTreeCell(keys[3]); tmpAtomList = ((InternalIndexCell)cell).Key.GetValues(); Assert.Equal(5, tmpAtomList[0].IntegerValue); tmpAtomList = ((InternalIndexCell)cell).PrimaryKey.GetValues(); Assert.Equal(3, tmpAtomList[0].IntegerValue); // key 2: value 2 (cell, offset, indexInOffsetArray) = node.FindBTreeCell(keys[2]); tmpAtomList = cell.Key.GetValues(); Assert.Equal(2, tmpAtomList[0].IntegerValue); tmpAtomList = ((InternalIndexCell)cell).PrimaryKey.GetValues(); Assert.Equal(2, tmpAtomList[0].IntegerValue); // delete cell with key == 2 node.DeleteBTreeCell(offset); // check deletion offsets = node.CellOffsetArray; tmpAtomList = node.GetBTreeCell(offsets[0]).Key.GetValues(); Assert.Equal(1, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[1]).Key.GetValues(); Assert.Equal(3, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[2]).Key.GetValues(); Assert.Equal(4, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[3]).Key.GetValues(); Assert.Equal(5, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[4]).Key.GetValues(); Assert.Equal(6, tmpAtomList[0].IntegerValue); // delete cell with key == 4 node.DeleteBTreeCell(offsets[2]); // check deletion offsets = node.CellOffsetArray; tmpAtomList = node.GetBTreeCell(offsets[0]).Key.GetValues(); Assert.Equal(1, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[1]).Key.GetValues(); Assert.Equal(3, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[2]).Key.GetValues(); Assert.Equal(5, tmpAtomList[0].IntegerValue); tmpAtomList = node.GetBTreeCell(offsets[3]).Key.GetValues(); Assert.Equal(6, tmpAtomList[0].IntegerValue); // delete by index 0 (cell with key == 1) node.DeleteBTreeCell(node[0]); tmpAtomList = node[0].Key.GetValues(); Assert.Equal(3, tmpAtomList[0].IntegerValue); tmpAtomList = node[1].Key.GetValues(); Assert.Equal(5, tmpAtomList[0].IntegerValue); tmpAtomList = node[2].Key.GetValues(); Assert.Equal(6, tmpAtomList[0].IntegerValue); // delete remaining cells node.DeleteBTreeCell(offsets[0]); offsets = node.CellOffsetArray; node.DeleteBTreeCell(offsets[0]); offsets = node.CellOffsetArray; node.DeleteBTreeCell(offsets[0]); offsets = node.CellOffsetArray; pager.Close(); }