diff --git a/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp b/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp index c8c43c05..058fea50 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp +++ b/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp @@ -65,6 +65,9 @@ unique_ptr DataTree::addDataLeafToFullTree() { return newLeaf; } +const Key &DataTree::key() const { + return _rootNode->key(); +} } diff --git a/src/blobstore/implementations/onblocks/datatreestore/DataTree.h b/src/blobstore/implementations/onblocks/datatreestore/DataTree.h index 99cc653b..5f35121c 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/DataTree.h +++ b/src/blobstore/implementations/onblocks/datatreestore/DataTree.h @@ -6,6 +6,9 @@ #include "fspp/utils/macros.h" #include "impl/GetLowestRightBorderNodeWithLessThanKChildrenOrNull.h" +namespace blockstore { +class Key; +} namespace blobstore { namespace onblocks { namespace datanodestore { @@ -22,6 +25,8 @@ public: virtual ~DataTree(); std::unique_ptr addDataLeaf(); + + const blockstore::Key &key() const; private: datanodestore::DataNodeStore *_nodeStore; std::unique_ptr _rootNode; diff --git a/src/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest.cpp b/src/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest.cpp index b70194df..a7114d49 100644 --- a/src/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest.cpp +++ b/src/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest.cpp @@ -4,14 +4,22 @@ #include "blobstore/implementations/onblocks/datanodestore/DataNodeStore.h" #include "blobstore/implementations/onblocks/datatreestore/DataTree.h" #include "blobstore/implementations/onblocks/datanodestore/DataLeafNode.h" +#include "blobstore/implementations/onblocks/datanodestore/DataInnerNode.h" + +#include "fspp/utils/pointer.h" using ::testing::Test; using std::unique_ptr; using std::make_unique; +using fspp::dynamic_pointer_move; using blobstore::onblocks::datanodestore::DataNodeStore; using blobstore::onblocks::datanodestore::DataNode; +using blobstore::onblocks::datanodestore::DataInnerNode; +using blobstore::onblocks::datanodestore::DataLeafNode; using blockstore::testfake::FakeBlockStore; +using blockstore::BlockStore; +using blockstore::Key; namespace blobstore { namespace onblocks { @@ -28,9 +36,76 @@ public: return make_unique(&nodeStore, std::move(leafnode)); } + Key CreateTreeAddOneLeafReturnRootKey() { + auto tree = CreateLeafOnlyTree(); + auto key = tree->key(); + tree->addDataLeaf(); + return key; + } + + Key CreateTreeAddTwoLeavesReturnRootKey() { + auto tree = CreateLeafOnlyTree(); + auto key = tree->key(); + tree->addDataLeaf(); + tree->addDataLeaf(); + return key; + } + + unique_ptr LoadInnerNode(const Key &key) { + auto node = nodeStore.load(key); + return dynamic_pointer_move(node); + } + + unique_ptr LoadLeafNode(const Key &key) { + auto node = nodeStore.load(key); + return dynamic_pointer_move(node); + } + + void EXPECT_IS_LEAF_NODE(const Key &key) { + auto node = LoadLeafNode(key); + EXPECT_NE(nullptr, node.get()); + } + + void EXPECT_IS_INNER_NODE(const Key &key) { + auto node = LoadInnerNode(key); + EXPECT_NE(nullptr, node.get()); + } + DataNodeStore nodeStore; }; +TEST_F(DataTreeTest, GrowAOneNodeTree_KeyDoesntChange) { + auto tree = CreateLeafOnlyTree(); + auto key = tree->key(); + tree->addDataLeaf(); + EXPECT_EQ(key, tree->key()); +} + +TEST_F(DataTreeTest, GrowAOneNodeTree_Structure) { + auto key = CreateTreeAddOneLeafReturnRootKey(); + + EXPECT_IS_INNER_NODE(key); + auto root = LoadInnerNode(key); + + EXPECT_EQ(2u, root->numChildren()); + EXPECT_IS_LEAF_NODE(root->getChild(0)->key()); + EXPECT_IS_LEAF_NODE(root->getChild(1)->key()); +} + +TEST_F(DataTreeTest, GrowATwoNodeTree_Structure) { + auto key = CreateTreeAddTwoLeavesReturnRootKey(); + + EXPECT_IS_INNER_NODE(key); + auto root = LoadInnerNode(key); + + EXPECT_EQ(3u, root->numChildren()); + EXPECT_IS_LEAF_NODE(root->getChild(0)->key()); + EXPECT_IS_LEAF_NODE(root->getChild(1)->key()); + EXPECT_IS_LEAF_NODE(root->getChild(2)->key()); +} + +//TODO Test that when growing, the original leaf retains its data + } } }