diff --git a/implementations/onblocks/datanodestore/DataInnerNode.cpp b/implementations/onblocks/datanodestore/DataInnerNode.cpp index 6f59a10e..400b2bb7 100644 --- a/implementations/onblocks/datanodestore/DataInnerNode.cpp +++ b/implementations/onblocks/datanodestore/DataInnerNode.cpp @@ -2,6 +2,7 @@ #include "DataNodeStore.h" using std::unique_ptr; +using std::make_unique; using blockstore::Block; using blockstore::Data; using blockstore::Key; @@ -14,15 +15,23 @@ constexpr uint32_t DataInnerNode::MAX_STORED_CHILDREN; DataInnerNode::DataInnerNode(DataNodeView view) : DataNode(std::move(view)) { + assert(depth() > 0); } DataInnerNode::~DataInnerNode() { } -void DataInnerNode::InitializeNewNode(const DataNode &first_child) { - *node().Depth() = first_child.depth() + 1; - *node().Size() = 1; - ChildrenBegin()->setKey(first_child.key()); +unique_ptr DataInnerNode::InitializeNewNode(unique_ptr block, const DataNode &first_child) { + DataNodeView node(std::move(block)); + *node.Depth() = first_child.depth() + 1; + *node.Size() = 1; + auto result = make_unique(std::move(node)); + result->ChildrenBegin()->setKey(first_child.key()); + return result; +} + +uint8_t DataInnerNode::depth() const { + return *node().Depth(); } uint32_t DataInnerNode::numChildren() const { diff --git a/implementations/onblocks/datanodestore/DataInnerNode.h b/implementations/onblocks/datanodestore/DataInnerNode.h index 75b114fa..452f010c 100644 --- a/implementations/onblocks/datanodestore/DataInnerNode.h +++ b/implementations/onblocks/datanodestore/DataInnerNode.h @@ -10,6 +10,8 @@ namespace datanodestore { class DataInnerNode: public DataNode { public: + static std::unique_ptr InitializeNewNode(std::unique_ptr block, const DataNode &first_child_key); + DataInnerNode(DataNodeView block); virtual ~DataInnerNode(); @@ -29,7 +31,7 @@ public: static constexpr uint32_t MAX_STORED_CHILDREN = DataNodeView::DATASIZE_BYTES / sizeof(ChildEntry); - void InitializeNewNode(const DataNode &first_child_key); + uint8_t depth() const; ChildEntry *getChild(unsigned int index); const ChildEntry *getChild(unsigned int index) const; diff --git a/implementations/onblocks/datanodestore/DataLeafNode.cpp b/implementations/onblocks/datanodestore/DataLeafNode.cpp index 0c6f9740..3df4b546 100644 --- a/implementations/onblocks/datanodestore/DataLeafNode.cpp +++ b/implementations/onblocks/datanodestore/DataLeafNode.cpp @@ -13,16 +13,19 @@ namespace datanodestore { DataLeafNode::DataLeafNode(DataNodeView view) : DataNode(std::move(view)) { + assert(*node().Depth() == 0); assert(numBytes() <= MAX_STORED_BYTES); } DataLeafNode::~DataLeafNode() { } -void DataLeafNode::InitializeNewNode() { - *node().Depth() = 0; - *node().Size() = 0; +unique_ptr DataLeafNode::InitializeNewNode(unique_ptr block) { + DataNodeView node(std::move(block)); + *node.Depth() = 0; + *node.Size() = 0; //fillDataWithZeroes(); not needed, because a newly created block will be zeroed out. DataLeafNodeTest.SpaceIsZeroFilledWhenGrowing ensures this. + return make_unique(std::move(node)); } void *DataLeafNode::data() { diff --git a/implementations/onblocks/datanodestore/DataLeafNode.h b/implementations/onblocks/datanodestore/DataLeafNode.h index 7b3d0c83..3f08d8a8 100644 --- a/implementations/onblocks/datanodestore/DataLeafNode.h +++ b/implementations/onblocks/datanodestore/DataLeafNode.h @@ -11,13 +11,13 @@ class DataInnerNode; class DataLeafNode: public DataNode { public: + static std::unique_ptr InitializeNewNode(std::unique_ptr block); + DataLeafNode(DataNodeView block); virtual ~DataLeafNode(); static constexpr uint32_t MAX_STORED_BYTES = DataNodeView::DATASIZE_BYTES; - void InitializeNewNode(); - void *data(); const void *data() const; diff --git a/implementations/onblocks/datanodestore/DataNode.cpp b/implementations/onblocks/datanodestore/DataNode.cpp index 53e7f061..492acc64 100644 --- a/implementations/onblocks/datanodestore/DataNode.cpp +++ b/implementations/onblocks/datanodestore/DataNode.cpp @@ -42,9 +42,7 @@ unique_ptr DataNode::convertToNewInnerNode(unique_ptr n auto block = node->_node.releaseBlock(); std::memset(block->data(), 0, block->size()); - auto innerNode = make_unique(DataNodeView(std::move(block))); - innerNode->InitializeNewNode(first_child); - return innerNode; + return DataInnerNode::InitializeNewNode(std::move(block), first_child); } void DataNode::flush() const { diff --git a/implementations/onblocks/datanodestore/DataNodeStore.cpp b/implementations/onblocks/datanodestore/DataNodeStore.cpp index 6a96696e..ead1e992 100644 --- a/implementations/onblocks/datanodestore/DataNodeStore.cpp +++ b/implementations/onblocks/datanodestore/DataNodeStore.cpp @@ -38,16 +38,12 @@ unique_ptr DataNodeStore::load(unique_ptr block) { unique_ptr DataNodeStore::createNewInnerNode(const DataNode &first_child) { auto block = _blockstore->create(DataNodeView::BLOCKSIZE_BYTES); - auto newNode = make_unique(std::move(block)); - newNode->InitializeNewNode(first_child); - return std::move(newNode); + return DataInnerNode::InitializeNewNode(std::move(block), first_child); } unique_ptr DataNodeStore::createNewLeafNode() { auto block = _blockstore->create(DataNodeView::BLOCKSIZE_BYTES); - auto newNode = make_unique(std::move(block)); - newNode->InitializeNewNode(); - return std::move(newNode); + return DataLeafNode::InitializeNewNode(std::move(block)); } unique_ptr DataNodeStore::load(const Key &key) { diff --git a/test/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp b/test/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp index fabc3c16..18afaeaf 100644 --- a/test/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp +++ b/test/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp @@ -92,6 +92,12 @@ public: return dynamic_pointer_move(copied); } + Key InitializeInnerNodeAddLeafReturnKey() { + auto node = DataInnerNode::InitializeNewNode(blockStore->create(DataNodeView::BLOCKSIZE_BYTES), *leaf); + AddALeafTo(node.get()); + return node->key(); + } + Data ZEROES; unique_ptr _blockStore; BlockStore *blockStore; @@ -101,15 +107,15 @@ public: }; TEST_F(DataInnerNodeTest, InitializesCorrectly) { - node->InitializeNewNode(*leaf); + auto node = DataInnerNode::InitializeNewNode(blockStore->create(DataNodeView::BLOCKSIZE_BYTES), *leaf); EXPECT_EQ(1u, node->numChildren()); EXPECT_EQ(leaf->key(), node->getChild(0)->key()); } TEST_F(DataInnerNodeTest, ReinitializesCorrectly) { - AddALeafTo(node.get()); - node->InitializeNewNode(*leaf); + auto key = InitializeInnerNodeAddLeafReturnKey(); + auto node = DataInnerNode::InitializeNewNode(blockStore->load(key), *leaf); EXPECT_EQ(1u, node->numChildren()); EXPECT_EQ(leaf->key(), node->getChild(0)->key()); diff --git a/test/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp b/test/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp index b376a1ec..84a8e401 100644 --- a/test/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp +++ b/test/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp @@ -88,6 +88,12 @@ public: return dynamic_pointer_move(copied); } + Key InitializeLeafGrowAndReturnKey() { + auto leaf = DataLeafNode::InitializeNewNode(blockStore->create(DataNodeView::BLOCKSIZE_BYTES)); + leaf->resize(5); + return leaf->key(); + } + Data ZEROES; Data randomData; unique_ptr _blockStore; @@ -97,13 +103,13 @@ public: }; TEST_F(DataLeafNodeTest, InitializesCorrectly) { - leaf->InitializeNewNode(); + auto leaf = DataLeafNode::InitializeNewNode(blockStore->create(DataNodeView::BLOCKSIZE_BYTES)); EXPECT_EQ(0u, leaf->numBytes()); } TEST_F(DataLeafNodeTest, ReinitializesCorrectly) { - leaf->resize(5); - leaf->InitializeNewNode(); + auto key = InitializeLeafGrowAndReturnKey(); + auto leaf = DataLeafNode::InitializeNewNode(blockStore->load(key)); EXPECT_EQ(0u, leaf->numBytes()); }