2015-02-20 18:31:18 +01:00
|
|
|
#include "DataTreeTest.h"
|
|
|
|
|
2016-02-11 15:19:58 +01:00
|
|
|
#include <blockstore/implementations/testfake/FakeBlockStore.h>
|
|
|
|
#include <cpp-utils/pointer/cast.h>
|
|
|
|
#include <cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
|
2015-02-20 18:31:18 +01:00
|
|
|
|
|
|
|
using blobstore::onblocks::datanodestore::DataNodeStore;
|
2015-02-23 17:03:45 +01:00
|
|
|
using blobstore::onblocks::datanodestore::DataNode;
|
2015-02-20 18:31:18 +01:00
|
|
|
using blobstore::onblocks::datanodestore::DataInnerNode;
|
2015-02-22 19:30:42 +01:00
|
|
|
using blobstore::onblocks::datanodestore::DataLeafNode;
|
2015-02-20 18:31:18 +01:00
|
|
|
using blobstore::onblocks::datatreestore::DataTree;
|
2016-07-15 10:23:09 +02:00
|
|
|
using blockstore::mock::MockBlockStore;
|
2015-02-20 18:31:18 +01:00
|
|
|
using blockstore::Key;
|
2015-06-26 15:59:18 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2015-02-23 17:03:45 +01:00
|
|
|
using std::initializer_list;
|
2015-02-23 17:30:44 +01:00
|
|
|
using std::vector;
|
2015-06-26 15:59:18 +02:00
|
|
|
using boost::none;
|
2015-02-22 19:30:42 +01:00
|
|
|
using cpputils::dynamic_pointer_move;
|
2016-07-15 12:48:20 +02:00
|
|
|
using cpputils::Data;
|
2015-02-20 18:31:18 +01:00
|
|
|
|
2015-02-25 22:30:48 +01:00
|
|
|
constexpr uint32_t DataTreeTest::BLOCKSIZE_BYTES;
|
|
|
|
|
2015-02-20 18:31:18 +01:00
|
|
|
DataTreeTest::DataTreeTest()
|
2016-07-14 23:38:17 +02:00
|
|
|
:_blockStore(make_unique_ref<MockBlockStore>()),
|
|
|
|
blockStore(_blockStore.get()),
|
|
|
|
_nodeStore(make_unique_ref<DataNodeStore>(std::move(_blockStore), BLOCKSIZE_BYTES)),
|
2015-02-24 22:44:10 +01:00
|
|
|
nodeStore(_nodeStore.get()),
|
|
|
|
treeStore(std::move(_nodeStore)) {
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataLeafNode> DataTreeTest::CreateLeaf() {
|
2016-07-15 12:48:20 +02:00
|
|
|
return nodeStore->createNewLeafNode(Data(nodeStore->layout().maxBytesPerLeaf()));
|
2015-02-23 17:03:45 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateInner(initializer_list<unique_ref<DataNode>> children) {
|
2015-02-23 17:30:44 +01:00
|
|
|
vector<const DataNode*> childrenVector(children.size());
|
2015-06-26 15:59:18 +02:00
|
|
|
std::transform(children.begin(), children.end(), childrenVector.begin(), [](const unique_ref<DataNode> &ptr) {return ptr.get();});
|
2015-02-23 17:30:44 +01:00
|
|
|
return CreateInner(childrenVector);
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateInner(initializer_list<const DataNode*> children) {
|
2015-02-23 17:30:44 +01:00
|
|
|
return CreateInner(vector<const DataNode*>(children));
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateInner(vector<const DataNode*> children) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(children.size() >= 1, "An inner node must have at least one child");
|
2016-07-15 12:48:20 +02:00
|
|
|
vector<Key> childrenKeys;
|
|
|
|
childrenKeys.reserve(children.size());
|
|
|
|
for (const DataNode *child : children) {
|
|
|
|
ASSERT(child->depth() == (*children.begin())->depth(), "Children with different depth");
|
|
|
|
childrenKeys.push_back(child->key());
|
2015-02-23 17:03:45 +01:00
|
|
|
}
|
2016-07-15 12:48:20 +02:00
|
|
|
auto node = nodeStore->createNewInnerNode((*children.begin())->depth()+1, childrenKeys);
|
2015-02-23 17:03:45 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataTree> DataTreeTest::CreateLeafOnlyTree() {
|
2015-02-24 22:44:10 +01:00
|
|
|
auto key = CreateLeaf()->key();
|
2015-06-28 16:59:13 +02:00
|
|
|
return treeStore.load(key).value();
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::FillNode(DataInnerNode *node) {
|
2015-02-25 22:30:48 +01:00
|
|
|
for(unsigned int i=node->numChildren(); i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) {
|
2015-02-23 17:03:45 +01:00
|
|
|
node->addChild(*CreateLeaf());
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::FillNodeTwoLevel(DataInnerNode *node) {
|
2015-02-25 22:30:48 +01:00
|
|
|
for(unsigned int i=node->numChildren(); i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) {
|
2015-02-23 17:03:45 +01:00
|
|
|
node->addChild(*CreateFullTwoLevel());
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFullTwoLevel() {
|
2015-02-23 17:03:45 +01:00
|
|
|
auto root = CreateInner({CreateLeaf().get()});
|
2015-02-20 18:31:18 +01:00
|
|
|
FillNode(root.get());
|
2015-02-23 17:03:45 +01:00
|
|
|
return root;
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateThreeLevelMinData() {
|
2015-02-24 22:46:44 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateFullTwoLevel(),
|
|
|
|
CreateInner({CreateLeaf()})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFourLevelMinData() {
|
2015-02-25 16:52:17 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateFullThreeLevel(),
|
|
|
|
CreateInner({CreateInner({CreateLeaf()})})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFullThreeLevel() {
|
2015-02-23 17:03:45 +01:00
|
|
|
auto root = CreateInner({CreateFullTwoLevel().get()});
|
2015-02-20 18:31:18 +01:00
|
|
|
FillNodeTwoLevel(root.get());
|
2015-02-23 17:03:45 +01:00
|
|
|
return root;
|
2015-02-20 18:31:18 +01:00
|
|
|
}
|
2015-02-22 19:30:42 +01:00
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::LoadInnerNode(const Key &key) {
|
2015-06-28 16:59:13 +02:00
|
|
|
auto node = nodeStore->load(key).value();
|
2015-02-22 19:30:42 +01:00
|
|
|
auto casted = dynamic_pointer_move<DataInnerNode>(node);
|
2015-06-26 15:59:18 +02:00
|
|
|
EXPECT_NE(none, casted) << "Is not an inner node";
|
|
|
|
return std::move(*casted);
|
2015-02-22 19:30:42 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataLeafNode> DataTreeTest::LoadLeafNode(const Key &key) {
|
2015-06-28 16:59:13 +02:00
|
|
|
auto node = nodeStore->load(key).value();
|
2015-02-22 19:30:42 +01:00
|
|
|
auto casted = dynamic_pointer_move<DataLeafNode>(node);
|
2015-06-26 15:59:18 +02:00
|
|
|
EXPECT_NE(none, casted) << "Is not a leaf node";
|
|
|
|
return std::move(*casted);
|
2015-02-22 19:30:42 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateTwoLeaf() {
|
2015-02-23 17:03:45 +01:00
|
|
|
return CreateInner({CreateLeaf().get(), CreateLeaf().get()});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataTree> DataTreeTest::CreateTwoLeafTree() {
|
2015-02-24 22:44:10 +01:00
|
|
|
auto key = CreateTwoLeaf()->key();
|
2015-06-28 16:59:13 +02:00
|
|
|
return treeStore.load(key).value();
|
2015-02-23 16:21:02 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataLeafNode> DataTreeTest::CreateLeafWithSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
auto leaf = CreateLeaf();
|
|
|
|
leaf->resize(size);
|
|
|
|
return leaf;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateTwoLeafWithSecondLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateLeafWithSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateLeafWithSize(size)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFullTwoLevelWithLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
auto root = CreateFullTwoLevel();
|
2015-03-16 18:32:37 +01:00
|
|
|
for (uint32_t i = 0; i < root->numChildren()-1; ++i) {
|
2015-03-04 02:05:03 +01:00
|
|
|
LoadLeafNode(root->getChild(i)->key())->resize(nodeStore->layout().maxBytesPerLeaf());
|
|
|
|
}
|
|
|
|
LoadLeafNode(root->LastChild()->key())->resize(size);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateThreeLevelWithOneChildAndLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateInner({
|
|
|
|
CreateLeafWithSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateLeafWithSize(size)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateThreeLevelWithTwoChildrenAndLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateFullTwoLevelWithLastLeafSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateInner({
|
|
|
|
CreateLeafWithSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateLeafWithSize(size)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateThreeLevelWithThreeChildrenAndLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateFullTwoLevelWithLastLeafSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateFullTwoLevelWithLastLeafSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateInner({
|
|
|
|
CreateLeafWithSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateLeafWithSize(size)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFullThreeLevelWithLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
auto root = CreateFullThreeLevel();
|
2015-03-16 18:32:37 +01:00
|
|
|
for (uint32_t i = 0; i < root->numChildren(); ++i) {
|
2015-03-04 02:05:03 +01:00
|
|
|
auto node = LoadInnerNode(root->getChild(i)->key());
|
2015-03-16 18:32:37 +01:00
|
|
|
for (uint32_t j = 0; j < node->numChildren(); ++j) {
|
2015-03-04 02:05:03 +01:00
|
|
|
LoadLeafNode(node->getChild(j)->key())->resize(nodeStore->layout().maxBytesPerLeaf());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LoadLeafNode(LoadInnerNode(root->LastChild()->key())->LastChild()->key())->resize(size);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataTreeTest::CreateFourLevelMinDataWithLastLeafSize(uint32_t size) {
|
2015-03-04 02:05:03 +01:00
|
|
|
return CreateInner({
|
|
|
|
CreateFullThreeLevelWithLastLeafSize(nodeStore->layout().maxBytesPerLeaf()),
|
|
|
|
CreateInner({CreateInner({CreateLeafWithSize(size)})})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-22 19:30:42 +01:00
|
|
|
void DataTreeTest::EXPECT_IS_LEAF_NODE(const Key &key) {
|
|
|
|
auto node = LoadLeafNode(key);
|
|
|
|
EXPECT_NE(nullptr, node.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::EXPECT_IS_INNER_NODE(const Key &key) {
|
|
|
|
auto node = LoadInnerNode(key);
|
|
|
|
EXPECT_NE(nullptr, node.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::EXPECT_IS_TWONODE_CHAIN(const Key &key) {
|
|
|
|
auto node = LoadInnerNode(key);
|
|
|
|
EXPECT_EQ(1u, node->numChildren());
|
|
|
|
EXPECT_IS_LEAF_NODE(node->getChild(0)->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::EXPECT_IS_FULL_TWOLEVEL_TREE(const Key &key) {
|
|
|
|
auto node = LoadInnerNode(key);
|
2015-02-25 22:30:48 +01:00
|
|
|
EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), node->numChildren());
|
2015-02-22 19:30:42 +01:00
|
|
|
for (unsigned int i = 0; i < node->numChildren(); ++i) {
|
|
|
|
EXPECT_IS_LEAF_NODE(node->getChild(i)->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataTreeTest::EXPECT_IS_FULL_THREELEVEL_TREE(const Key &key) {
|
|
|
|
auto root = LoadInnerNode(key);
|
2015-02-25 22:30:48 +01:00
|
|
|
EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), root->numChildren());
|
2015-02-22 19:30:42 +01:00
|
|
|
for (unsigned int i = 0; i < root->numChildren(); ++i) {
|
|
|
|
auto node = LoadInnerNode(root->getChild(i)->key());
|
2015-02-25 22:30:48 +01:00
|
|
|
EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), node->numChildren());
|
2015-02-22 19:30:42 +01:00
|
|
|
for (unsigned int j = 0; j < node->numChildren(); ++j) {
|
|
|
|
EXPECT_IS_LEAF_NODE(node->getChild(j)->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 16:42:13 +01:00
|
|
|
|
|
|
|
void DataTreeTest::CHECK_DEPTH(int depth, const Key &key) {
|
|
|
|
if (depth == 0) {
|
|
|
|
EXPECT_IS_LEAF_NODE(key);
|
|
|
|
} else {
|
|
|
|
auto node = LoadInnerNode(key);
|
|
|
|
EXPECT_EQ(depth, node->depth());
|
2015-03-16 18:32:37 +01:00
|
|
|
for (uint32_t i = 0; i < node->numChildren(); ++i) {
|
2015-02-24 16:46:05 +01:00
|
|
|
CHECK_DEPTH(depth-1, node->getChild(i)->key());
|
2015-02-24 16:42:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|