Exemple #1
0
        public void TestFindExistingKey()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 5, store);

            btree.Insert(3, null);
            btree.Insert(10, null);
            btree.Insert(2, null);
            btree.Insert(7, null);
            btree.Insert(5, null);
            btree.Insert(4, null);
            btree.Insert(1, null);
            btree.Insert(12, null);
            btree.Insert(19, null);
            btree.Insert(11, null);

            Assert.AreEqual(10, InOrderTraversal(btree).Count);

            foreach (var key in InOrderTraversal(btree))
            {
                Node <IntData> keyNode = btree.Lookup(key);
                Assert.IsNotNull(keyNode);
                Assert.IsTrue(keyNode.Keys.Contains(key));
            }
        }
Exemple #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);
        }
Exemple #3
0
        public void TestExceptionWhenInsertingDuplicateKey()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(2, 3, store);

            btree.Insert(1, null);
            btree.Insert(1, null);
        }
Exemple #4
0
        public void TestSecondInsert()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(2, 3, store);

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

            Assert.IsNotNull(btree.Root);
            Assert.AreEqual(2, btree.Root.Keys.Count);
            Assert.AreEqual(1ul, btree.Root.Keys[0].Key);
            Assert.AreEqual(2ul, btree.Root.Keys[1].Key);
        }
Exemple #5
0
        public void TestDeleteInLeaf()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

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

            btree.Delete(2);

            Assert.IsNull(btree.Lookup(2));
            AssertOrderInList(InOrderTraversal(btree));
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
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);
        }
Exemple #9
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);
        }
Exemple #10
0
        public void TestEntryLookup()
        {
            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);

            Entry <IntData> entry = null;
            Node <IntData>  node  = null;

            btree.LookupEntry(3, out entry, out node);

            Assert.IsNotNull(entry);
            Assert.IsNotNull(node);
        }
Exemple #11
0
        public void TestMillionIds()
        {
            var start = DateTime.UtcNow;
            var store = new Store();
            var btree = new PersistentBTree <IntData>(2, 51, store);

            for (ulong i = 0; i < 500000; i++)
            {
                btree.Insert(i, null);
            }
            var end = DateTime.UtcNow;

            Console.WriteLine(end.Subtract(start).TotalMilliseconds);
        }
Exemple #12
0
        public void TestFindNonExistantKey()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

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

            var keyNode = btree.Lookup(100);

            Assert.IsNull(keyNode);
        }
Exemple #13
0
        public void TestNonSequentialInsert()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

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

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

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

            AssertOrderInList(inorderKeys);
        }
Exemple #14
0
        public void TestOutOfOrderInsert()
        {
            var store = new Store();
            var btree = new PersistentBTree <IntData>(store.GetNextObjectId(), 3, store);

            btree.Insert(6, null);
            btree.Insert(2, null);
            btree.Insert(3, null);
            btree.Insert(1, null);
            btree.Insert(4, null);
            btree.Insert(18, null);
            btree.Insert(5, null);
            btree.Insert(7, null);
            btree.Insert(8, null);
            btree.Insert(9, null);
            btree.Insert(10, null);
            btree.Insert(11, null);
            btree.Insert(14, null);
            btree.Insert(15, null);
            btree.Insert(16, null);
            btree.Insert(17, null);
            btree.Insert(19, null);
            btree.Insert(12, null);
            btree.Insert(13, null);
            btree.Insert(20, null);

            AssertOrderInList(InOrderTraversal(btree));
            Assert.AreEqual(20, InOrderTraversal(btree).Count);
        }