public void TestMergeRight() { using (var pageStore = TestUtils.CreateEmptyPageStore("TestMergeRight.data")) { var tree = new BPlusTree(pageStore); var testBytes = new byte[] { 1, 2, 3, 4 }; var buff = new byte[_config.ValueSize]; var txnId = 0ul; for (int i = 0; i < _config.LeafLoadFactor * _config.InternalBranchFactor; i++) { try { tree.Insert(txnId, (ulong)i, testBytes); } catch (Exception ex) { Assert.Fail("Insert failed for key {0} with exception {1}", i, ex); } } var rootNode = tree.GetNode(tree.RootId, null); var childId = (rootNode as InternalNode).GetChildNodeId(rootNode.RightmostKey); var child = tree.GetNode(childId, null) as InternalNode; var childLeftmostKey = BitConverter.ToUInt64(child.LeftmostKey, 0); var grandchild = tree.GetNode(child.GetChildNodeId(BitConverter.GetBytes(childLeftmostKey - 1)), null) as LeafNode; var deleteFrom = BitConverter.ToUInt64(grandchild.LeftmostKey, 0); var findChildId = (rootNode as InternalNode).GetChildNodeId(BitConverter.GetBytes(deleteFrom)); Assert.AreEqual(child.PageId, findChildId, "Incorrect node id returned for key {0}", deleteFrom); for (ulong i = 0; i < 4; i++) { var deleteKey = deleteFrom + i; tree.Delete(txnId, deleteKey, null); // Should be enough to force a right merge } for (ulong i = 0; i < (ulong)(_config.LeafLoadFactor * _config.InternalBranchFactor); i++) { Assert.IsTrue(tree.Search(i, buff, null) ^ (i - deleteFrom >= 0 && i - deleteFrom < 4), "Could not find key {0}. deleteFrom={1}", i, deleteFrom); } deleteFrom = (ulong)(_config.LeafLoadFactor * _config.InternalBranchFactor) - 5; for (ulong i = 0; i < 4; i++) { var deleteKey = deleteFrom + i; tree.Delete(txnId, deleteKey, null); } } }
public void TestCoalesceLeafLoadPlusOne() { const string storeName = "Coalesce.LeafLoadPlusOne.data"; ulong srcRootId, targetRootId; var txnId = 0ul; using (var store = TestUtils.CreateEmptyPageStore(storeName)) { var sourceTree = new BPlusTree(0, store); var config = sourceTree.Configuration; for (int i = 0; i < config.LeafLoadFactor + 1; i++) { sourceTree.Insert(txnId, (ulong)i, BitConverter.GetBytes((ulong)i)); } sourceTree.DumpStructure(); srcRootId = sourceTree.Save(txnId, null); store.Commit(txnId, null); } using (var store = TestUtils.OpenPageStore(storeName, false)) { var sourceTree = new BPlusTree(store, srcRootId); var config = sourceTree.Configuration; var builder = new BPlusTreeBuilder(store, config); targetRootId = builder.Build(1, sourceTree.Scan(null)); var targetTree = new BPlusTree(store, targetRootId); targetTree.DumpStructure(); byte[] valueBuff = new byte[64]; for (int i = 0; i < config.LeafLoadFactor + 1; i++) { Assert.IsTrue(targetTree.Search((ulong)i, valueBuff, null)); Assert.AreEqual((ulong)i, BitConverter.ToUInt64(valueBuff, 0)); } store.Commit(1, null); } using (var store = TestUtils.OpenPageStore(storeName, true)) { var targetTree = new BPlusTree(store, targetRootId); var config = targetTree.Configuration; targetTree.DumpStructure(); byte[] valueBuff = new byte[64]; for (int i = 0; i < config.LeafLoadFactor + 1; i++) { Assert.IsTrue(targetTree.Search((ulong)i, valueBuff, null)); Assert.AreEqual((ulong)i, BitConverter.ToUInt64(valueBuff, 0)); } var root = targetTree.GetNode(targetTree.RootId, null) as IInternalNode; Assert.IsNotNull(root); Assert.AreEqual(1, root.KeyCount); var leftChild = targetTree.GetNode(root.GetChildPointer(0), null) as ILeafNode; var rightChild = targetTree.GetNode(root.GetChildPointer(1), null) as ILeafNode; Assert.IsNotNull(leftChild); Assert.IsNotNull(rightChild); Assert.AreEqual(config.LeafLoadFactor + 1, leftChild.KeyCount + rightChild.KeyCount); // Key count in each node should be >= split index Assert.IsTrue(leftChild.KeyCount > config.LeafSplitIndex, "Left child has too few keys"); Assert.IsTrue(rightChild.KeyCount > config.LeafSplitIndex, "Right child has too few keys"); // And neither } }