Esempio n. 1
0
        public void TestPropagatingSplit()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(1, null);
            btree.Insert(2, null);
            btree.Insert(3, null);
            btree.Insert(4, null);
            btree.Insert(5, null);
            btree.Insert(6, null);
            btree.Insert(7, null);

            AssertOrderInList(InOrderTraversal(btree));

            Assert.IsNotNull(btree.Root);
            Assert.AreEqual(1, btree.Root.Keys.Count);
            Assert.AreEqual(4ul, btree.Root.Keys[0].Key);

            // root has two child nodes
            Assert.AreEqual(2, btree.Root.ChildNodes.Count);

            // 1st root child has 1 key
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[0]).Keys.Count);
            Assert.AreEqual(2ul, btree.LoadNode(btree.Root.ChildNodes[0]).Keys[0].Key);

            // 2nd root child has 1 keys
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[1]).Keys.Count);
            Assert.AreEqual(6ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[0].Key);
        }
Esempio n. 2
0
        public void TestInsertAfterRootSplit()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(1, null);
            btree.Insert(2, null);
            btree.Insert(3, null);
            btree.Insert(4, null);

            Assert.IsNotNull(btree.Root);
            Assert.AreEqual(1, btree.Root.Keys.Count);
            Assert.AreEqual(2ul, btree.Root.Keys[0].Key);

            // root has two child nodes
            Assert.AreEqual(2, btree.Root.ChildNodes.Count);

            // left child has 1 key
            Assert.AreEqual(1ul, btree.LoadNode(btree.Root.ChildNodes[0]).Keys[0].Key);
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[0]).Keys.Count);

            // right child has 2 keys
            Assert.AreEqual(2, btree.LoadNode(btree.Root.ChildNodes[1]).Keys.Count);
            Assert.AreEqual(3ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[0].Key);
            Assert.AreEqual(4ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[1].Key);
        }
Esempio n. 3
0
        private static void TraverseNode <T>(PersistentBTree <T> tree, Node <T> node, List <Entry <T> > keys) where T : class, IStorable
        {
            if (node.ChildNodes.Count == 0)
            {
                keys.AddRange(node.Keys);
                return;
            }

            for (int i = 0; i < node.Keys.Count; i++)
            {
                TraverseNode(tree, tree.LoadNode(node.ChildNodes[i]), keys);
                keys.Add(node.Keys[i]);
            }

            TraverseNode(tree, tree.LoadNode(node.ChildNodes.Last()), keys);
        }
Esempio n. 4
0
        public void TestRootSplit()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(1, null);
            btree.Insert(2, null);
            btree.Insert(3, null);

            AssertOrderInList(InOrderTraversal <IntData>(btree));

            Assert.IsNotNull(btree.Root);
            Assert.AreEqual(1, btree.Root.Keys.Count);
            Assert.AreEqual(2ul, btree.Root.Keys[0].Key);

            Assert.AreEqual(2, btree.Root.ChildNodes.Count);
            Assert.AreEqual(1ul, btree.LoadNode(btree.Root.ChildNodes[0]).Keys[0].Key);
            Assert.AreEqual(3ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[0].Key);
        }
Esempio n. 5
0
        public void TestDeleteInLeafWithRightSiblingBorrow()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(1, null);
            btree.Insert(2, null);
            btree.Insert(3, null);
            btree.Insert(4, null);

            Assert.IsNotNull(btree.Lookup(1));

            btree.Delete(1);

            Assert.IsNull(btree.Lookup(1));
            AssertOrderInList(InOrderTraversal(btree));

            Assert.AreEqual(3ul, btree.Root.Keys[0].Key);
            Assert.AreEqual(2ul, btree.LoadNode(btree.Root.ChildNodes[0]).Keys[0].Key);
            Assert.AreEqual(4ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[0].Key);
        }
Esempio n. 6
0
        public void TestNonRootSplit()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(1, null);
            btree.Insert(2, null);
            btree.Insert(3, null);
            btree.Insert(4, null);
            btree.Insert(5, null);

            AssertOrderInList(InOrderTraversal(btree));

            Assert.IsNotNull(btree.Root);
            Assert.AreEqual(2, btree.Root.Keys.Count);
            Assert.AreEqual(2ul, btree.Root.Keys[0].Key);
            Assert.AreEqual(4ul, btree.Root.Keys[1].Key);

            // root has three child nodes
            Assert.AreEqual(3, btree.Root.ChildNodes.Count);

            // root has 2 keys
            Assert.AreEqual(2ul, btree.Root.Keys[0].Key);
            Assert.AreEqual(4ul, btree.Root.Keys[1].Key);

            // left child has 1 key
            Assert.AreEqual(1ul, btree.LoadNode(btree.Root.ChildNodes[0]).Keys[0].Key);
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[0]).Keys.Count);

            // middle child has 1 keys
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[1]).Keys.Count);
            Assert.AreEqual(3ul, btree.LoadNode(btree.Root.ChildNodes[1]).Keys[0].Key);

            // right child has 1 keys
            Assert.AreEqual(1, btree.LoadNode(btree.Root.ChildNodes[2]).Keys.Count);
            Assert.AreEqual(5ul, btree.LoadNode(btree.Root.ChildNodes[2]).Keys[0].Key);

            // check in order traversal
            var inorderKeys = InOrderTraversal(btree);

            Assert.AreEqual(1ul, inorderKeys.First().Key);
            Assert.AreEqual(5ul, inorderKeys.Last().Key);
        }