2015-02-17 00:40:34 +01:00
|
|
|
#include <google/gtest/gtest.h>
|
2015-01-23 02:41:55 +01:00
|
|
|
|
2015-02-17 00:40:34 +01:00
|
|
|
#include "../../../../implementations/onblocks/datanodestore/DataInnerNode.h"
|
|
|
|
#include "../../../../implementations/onblocks/datanodestore/DataLeafNode.h"
|
|
|
|
#include "../../../../implementations/onblocks/datanodestore/DataNodeStore.h"
|
2015-01-23 02:41:55 +01:00
|
|
|
|
2015-02-17 00:40:34 +01:00
|
|
|
#include "messmer/blockstore/implementations/testfake/FakeBlockStore.h"
|
|
|
|
#include "messmer/blockstore/implementations/testfake/FakeBlock.h"
|
2015-01-23 02:41:55 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2015-02-17 00:40:34 +01:00
|
|
|
#include "messmer/cpp-utils/pointer.h"
|
2015-01-23 02:41:55 +01:00
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
|
2015-02-17 00:40:34 +01:00
|
|
|
using cpputils::dynamic_pointer_move;
|
2015-01-23 03:05:55 +01:00
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
using blockstore::Key;
|
|
|
|
using blockstore::testfake::FakeBlockStore;
|
|
|
|
using blockstore::BlockStore;
|
2015-01-24 00:54:27 +01:00
|
|
|
using blockstore::Data;
|
2015-01-23 02:41:55 +01:00
|
|
|
using namespace blobstore;
|
|
|
|
using namespace blobstore::onblocks;
|
|
|
|
using namespace blobstore::onblocks::datanodestore;
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
|
|
|
|
class DataInnerNodeTest: public Test {
|
|
|
|
public:
|
2015-02-25 22:30:48 +01:00
|
|
|
static constexpr uint32_t BLOCKSIZE_BYTES = 1024;
|
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
DataInnerNodeTest() :
|
|
|
|
_blockStore(make_unique<FakeBlockStore>()),
|
|
|
|
blockStore(_blockStore.get()),
|
2015-02-25 22:30:48 +01:00
|
|
|
nodeStore(make_unique<DataNodeStore>(std::move(_blockStore), BLOCKSIZE_BYTES)),
|
|
|
|
ZEROES(nodeStore->layout().maxBytesPerLeaf()),
|
2015-01-23 02:41:55 +01:00
|
|
|
leaf(nodeStore->createNewLeafNode()),
|
|
|
|
node(nodeStore->createNewInnerNode(*leaf)) {
|
2015-01-24 00:54:27 +01:00
|
|
|
|
|
|
|
ZEROES.FillWithZeroes();
|
2015-01-23 02:41:55 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 03:05:55 +01:00
|
|
|
unique_ptr<DataInnerNode> LoadInnerNode(const Key &key) {
|
|
|
|
auto node = nodeStore->load(key);
|
|
|
|
return dynamic_pointer_move<DataInnerNode>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
Key CreateNewInnerNodeReturnKey(const DataNode &firstChild) {
|
|
|
|
return nodeStore->createNewInnerNode(firstChild)->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<DataInnerNode> CreateNewInnerNode() {
|
|
|
|
auto new_leaf = nodeStore->createNewLeafNode();
|
|
|
|
return nodeStore->createNewInnerNode(*new_leaf);
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<DataInnerNode> CreateAndLoadNewInnerNode(const DataNode &firstChild) {
|
|
|
|
auto key = CreateNewInnerNodeReturnKey(firstChild);
|
|
|
|
return LoadInnerNode(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<DataInnerNode> CreateNewInnerNode(const DataNode &firstChild, const DataNode &secondChild) {
|
|
|
|
auto node = nodeStore->createNewInnerNode(firstChild);
|
|
|
|
node->addChild(secondChild);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key CreateNewInnerNodeReturnKey(const DataNode &firstChild, const DataNode &secondChild) {
|
|
|
|
return CreateNewInnerNode(firstChild, secondChild)->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<DataInnerNode> CreateAndLoadNewInnerNode(const DataNode &firstChild, const DataNode &secondChild) {
|
|
|
|
auto key = CreateNewInnerNodeReturnKey(firstChild, secondChild);
|
|
|
|
return LoadInnerNode(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Key AddALeafTo(DataInnerNode *node) {
|
|
|
|
auto leaf2 = nodeStore->createNewLeafNode();
|
|
|
|
node->addChild(*leaf2);
|
|
|
|
return leaf2->key();
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:54:27 +01:00
|
|
|
Key CreateNodeWithDataConvertItToInnerNodeAndReturnKey() {
|
|
|
|
auto node = CreateNewInnerNode();
|
|
|
|
AddALeafTo(node.get());
|
|
|
|
AddALeafTo(node.get());
|
|
|
|
auto child = nodeStore->createNewLeafNode();
|
|
|
|
unique_ptr<DataInnerNode> converted = DataNode::convertToNewInnerNode(std::move(node), *child);
|
|
|
|
return converted->key();
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:59:42 +01:00
|
|
|
unique_ptr<DataInnerNode> CopyInnerNode(const DataInnerNode &node) {
|
|
|
|
auto copied = nodeStore->createNewNodeAsCopyFrom(node);
|
|
|
|
return dynamic_pointer_move<DataInnerNode>(copied);
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:46:52 +01:00
|
|
|
Key InitializeInnerNodeAddLeafReturnKey() {
|
2015-02-25 22:30:48 +01:00
|
|
|
auto node = DataInnerNode::InitializeNewNode(blockStore->create(BLOCKSIZE_BYTES), *leaf);
|
2015-02-20 19:46:52 +01:00
|
|
|
AddALeafTo(node.get());
|
|
|
|
return node->key();
|
|
|
|
}
|
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
unique_ptr<BlockStore> _blockStore;
|
|
|
|
BlockStore *blockStore;
|
|
|
|
unique_ptr<DataNodeStore> nodeStore;
|
2015-02-25 22:30:48 +01:00
|
|
|
Data ZEROES;
|
2015-01-23 02:41:55 +01:00
|
|
|
unique_ptr<DataLeafNode> leaf;
|
|
|
|
unique_ptr<DataInnerNode> node;
|
|
|
|
};
|
|
|
|
|
2015-02-25 22:30:48 +01:00
|
|
|
constexpr uint32_t DataInnerNodeTest::BLOCKSIZE_BYTES;
|
|
|
|
|
2015-02-27 14:32:28 +01:00
|
|
|
TEST_F(DataInnerNodeTest, CorrectKeyReturnedAfterInitialization) {
|
|
|
|
auto block = blockStore->create(BLOCKSIZE_BYTES);
|
|
|
|
Key key = block->key();
|
|
|
|
auto node = DataInnerNode::InitializeNewNode(std::move(block), *leaf);
|
|
|
|
EXPECT_EQ(key, node->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, CorrectKeyReturnedAfterLoading) {
|
|
|
|
auto block = blockStore->create(BLOCKSIZE_BYTES);
|
|
|
|
Key key = block->key();
|
|
|
|
DataInnerNode::InitializeNewNode(std::move(block), *leaf);
|
|
|
|
|
|
|
|
auto loaded = nodeStore->load(key);
|
|
|
|
EXPECT_EQ(key, loaded->key());
|
|
|
|
}
|
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
TEST_F(DataInnerNodeTest, InitializesCorrectly) {
|
2015-02-25 22:30:48 +01:00
|
|
|
auto node = DataInnerNode::InitializeNewNode(blockStore->create(BLOCKSIZE_BYTES), *leaf);
|
2015-01-23 03:05:55 +01:00
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
EXPECT_EQ(1u, node->numChildren());
|
|
|
|
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, ReinitializesCorrectly) {
|
2015-02-20 19:46:52 +01:00
|
|
|
auto key = InitializeInnerNodeAddLeafReturnKey();
|
|
|
|
auto node = DataInnerNode::InitializeNewNode(blockStore->load(key), *leaf);
|
2015-01-23 02:41:55 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(1u, node->numChildren());
|
|
|
|
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
|
|
|
|
}
|
|
|
|
|
2015-01-23 03:05:55 +01:00
|
|
|
TEST_F(DataInnerNodeTest, IsCorrectlyInitializedAfterLoading) {
|
|
|
|
auto loaded = CreateAndLoadNewInnerNode(*leaf);
|
|
|
|
|
|
|
|
EXPECT_EQ(1u, loaded->numChildren());
|
|
|
|
EXPECT_EQ(leaf->key(), loaded->getChild(0)->key());
|
|
|
|
}
|
|
|
|
|
2015-01-23 02:41:55 +01:00
|
|
|
TEST_F(DataInnerNodeTest, AddingASecondLeaf) {
|
2015-01-23 03:05:55 +01:00
|
|
|
Key leaf2_key = AddALeafTo(node.get());
|
2015-01-23 02:41:55 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(2u, node->numChildren());
|
|
|
|
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
|
2015-01-23 03:05:55 +01:00
|
|
|
EXPECT_EQ(leaf2_key, node->getChild(1)->key());
|
2015-01-23 02:41:55 +01:00
|
|
|
}
|
2015-01-23 03:05:55 +01:00
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, AddingASecondLeafAndReload) {
|
|
|
|
auto leaf2 = nodeStore->createNewLeafNode();
|
|
|
|
auto loaded = CreateAndLoadNewInnerNode(*leaf, *leaf2);
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, loaded->numChildren());
|
|
|
|
EXPECT_EQ(leaf->key(), loaded->getChild(0)->key());
|
|
|
|
EXPECT_EQ(leaf2->key(), loaded->getChild(1)->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, BuildingAThreeLevelTree) {
|
|
|
|
auto node2 = CreateNewInnerNode();
|
|
|
|
auto parent = CreateNewInnerNode(*node, *node2);
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, parent->numChildren());
|
|
|
|
EXPECT_EQ(node->key(), parent->getChild(0)->key());
|
|
|
|
EXPECT_EQ(node2->key(), parent->getChild(1)->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, BuildingAThreeLevelTreeAndReload) {
|
|
|
|
auto node2 = CreateNewInnerNode();
|
|
|
|
auto parent = CreateAndLoadNewInnerNode(*node, *node2);
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, parent->numChildren());
|
|
|
|
EXPECT_EQ(node->key(), parent->getChild(0)->key());
|
|
|
|
EXPECT_EQ(node2->key(), parent->getChild(1)->key());
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:54:27 +01:00
|
|
|
TEST_F(DataInnerNodeTest, ConvertToInternalNode) {
|
|
|
|
auto child = nodeStore->createNewLeafNode();
|
|
|
|
Key node_key = node->key();
|
|
|
|
unique_ptr<DataInnerNode> converted = DataNode::convertToNewInnerNode(std::move(node), *child);
|
|
|
|
|
|
|
|
EXPECT_EQ(1u, converted->numChildren());
|
|
|
|
EXPECT_EQ(child->key(), converted->getChild(0)->key());
|
|
|
|
EXPECT_EQ(node_key, converted->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, ConvertToInternalNodeZeroesOutChildrenRegion) {
|
|
|
|
Key key = CreateNodeWithDataConvertItToInnerNodeAndReturnKey();
|
|
|
|
|
|
|
|
auto block = blockStore->load(key);
|
2015-02-25 22:30:48 +01:00
|
|
|
EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry)));
|
2015-01-24 00:54:27 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:59:42 +01:00
|
|
|
TEST_F(DataInnerNodeTest, CopyingCreatesNewNode) {
|
|
|
|
auto copied = CopyInnerNode(*node);
|
|
|
|
EXPECT_NE(node->key(), copied->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, CopyInnerNodeWithOneChild) {
|
|
|
|
auto copied = CopyInnerNode(*node);
|
|
|
|
|
|
|
|
EXPECT_EQ(node->numChildren(), copied->numChildren());
|
|
|
|
EXPECT_EQ(node->getChild(0)->key(), copied->getChild(0)->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, CopyInnerNodeWithTwoChildren) {
|
|
|
|
AddALeafTo(node.get());
|
|
|
|
auto copied = CopyInnerNode(*node);
|
|
|
|
|
|
|
|
EXPECT_EQ(node->numChildren(), copied->numChildren());
|
|
|
|
EXPECT_EQ(node->getChild(0)->key(), copied->getChild(0)->key());
|
|
|
|
EXPECT_EQ(node->getChild(1)->key(), copied->getChild(1)->key());
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:54:23 +01:00
|
|
|
TEST_F(DataInnerNodeTest, LastChildWhenOneChild) {
|
|
|
|
EXPECT_EQ(leaf->key(), node->LastChild()->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DataInnerNodeTest, LastChildWhenTwoChildren) {
|
|
|
|
Key key = AddALeafTo(node.get());
|
|
|
|
EXPECT_EQ(key, node->LastChild()->key());
|
|
|
|
}
|
2015-01-23 04:39:36 +01:00
|
|
|
|
2015-01-25 23:54:23 +01:00
|
|
|
TEST_F(DataInnerNodeTest, LastChildWhenThreeChildren) {
|
|
|
|
AddALeafTo(node.get());
|
|
|
|
Key key = AddALeafTo(node.get());
|
|
|
|
EXPECT_EQ(key, node->LastChild()->key());
|
|
|
|
}
|