Start with DataTreeStore

This commit is contained in:
Sebastian Messmer 2015-01-23 04:39:36 +01:00
parent dabe6e2567
commit 753c5aab27
9 changed files with 218 additions and 7 deletions

View File

@ -38,11 +38,11 @@ public:
void addChild(const DataNode &child_key);
private:
ChildEntry *LastChild();
const ChildEntry *LastChild() const;
private:
ChildEntry *ChildrenBegin();
ChildEntry *ChildrenEnd();
const ChildEntry *ChildrenBegin() const;

View File

@ -53,10 +53,6 @@ unique_ptr<DataNode> DataNodeStore::load(const Key &key) {
return load(_blockstore->load(key), key);
}
unique_ptr<const DataNode> DataNodeStore::load(const Key &key) const {
return const_cast<DataNodeStore*>(this)->load(key);
}
}
}
}

View File

@ -26,7 +26,6 @@ public:
static constexpr uint8_t MAX_DEPTH = 10;
std::unique_ptr<DataNode> load(const blockstore::Key &key);
std::unique_ptr<const DataNode> load(const blockstore::Key &key) const;
std::unique_ptr<DataLeafNode> createNewLeafNode();
std::unique_ptr<DataInnerNode> createNewInnerNode(const DataNode &first_child);

View File

@ -0,0 +1,3 @@
add_library(blobstore_onblocks_datatreestore DataTree.cpp DataTreeStore.cpp)
target_link_libraries(blobstore_onblocks_datatreestore blobstore_onblocks_datanodestore)

View File

@ -0,0 +1,98 @@
#include "DataTree.h"
#include "blobstore/implementations/onblocks/datanodestore/DataNodeStore.h"
#include "blobstore/implementations/onblocks/datanodestore/DataInnerNode.h"
#include "blobstore/implementations/onblocks/datanodestore/DataLeafNode.h"
#include "fspp/utils/pointer.h"
using blockstore::Key;
using blobstore::onblocks::datanodestore::DataNodeStore;
using blobstore::onblocks::datanodestore::DataNode;
using blobstore::onblocks::datanodestore::DataInnerNode;
using blobstore::onblocks::datanodestore::DataLeafNode;
using std::unique_ptr;
using fspp::dynamic_pointer_move;
namespace blobstore {
namespace onblocks {
namespace datatreestore {
DataTree::DataTree(DataNodeStore *nodeStore, unique_ptr<DataNode> rootNode)
: _nodeStore(nodeStore), _rootNode(std::move(rootNode)) {
}
DataTree::~DataTree() {
}
void DataTree::addDataLeaf() {
auto insertPosOrNull = LowestRightBorderNodeWithLessThanKChildrenOrNull();
if (insertPosOrNull) {
addDataLeafAt(insertPosOrNull.get());
} else {
addDataLeafToFullTree();
}
}
unique_ptr<DataInnerNode> DataTree::LowestRightBorderNodeWithLessThanKChildrenOrNull() {
const DataInnerNode *root_inner_node = dynamic_cast<const DataInnerNode*>(_rootNode.get());
if (nullptr == root_inner_node) {
//Root is not an inner node but a leaf
return nullptr;
}
unique_ptr<DataNode> currentNode = _nodeStore->load(root_inner_node->LastChild()->key());
unique_ptr<DataInnerNode> result(nullptr);
while(auto currentInnerNode = dynamic_pointer_move<DataInnerNode>(currentNode)) {
Key rightmostChildKey = currentInnerNode->LastChild()->key();
if (currentInnerNode->numChildren() < DataInnerNode::MAX_STORED_CHILDREN) {
result = std::move(currentInnerNode);
}
currentNode = _nodeStore->load(rightmostChildKey);
}
return result;
}
unique_ptr<DataLeafNode> DataTree::addDataLeafAt(DataInnerNode *insertPos) {
auto new_leaf = _nodeStore->createNewLeafNode();
if (insertPos->depth() == 1) {
insertPos->addChild(*new_leaf);
} else {
auto chain = createChainOfInnerNodes(insertPos->depth()-1, *new_leaf);
insertPos->addChild(*chain);
}
return new_leaf;
}
unique_ptr<DataInnerNode> DataTree::createChainOfInnerNodes(unsigned int num, const DataLeafNode &leaf) {
assert(num > 0);
unique_ptr<DataInnerNode> chain = _nodeStore->createNewInnerNode(leaf);
for(unsigned int i=1; i<num; ++i) {
chain = _nodeStore->createNewInnerNode(*chain);
}
return chain;
}
unique_ptr<DataLeafNode> DataTree::addDataLeafToFullTree() {
//TODO
//auto copyOfOldRoot = copyNode(*_rootNode);
//_rootNode->InitializeNewInnerNode(*copyOfOldRoot);
//addDataLeafAt(_rootNode.get());
assert(false);
return nullptr;
}
unique_ptr<DataNode> DataTree::copyNode(const DataNode &source) {
//TODO
assert(false);
return nullptr;
}
}
}
}

View File

@ -0,0 +1,41 @@
#pragma once
#ifndef BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATATREE_H_
#define BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATATREE_H_
#include <memory>
#include "fspp/utils/macros.h"
namespace blobstore {
namespace onblocks {
namespace datanodestore {
class DataNodeStore;
class DataInnerNode;
class DataLeafNode;
class DataNode;
}
namespace datatreestore {
class DataTree {
public:
DataTree(datanodestore::DataNodeStore *nodeStore, std::unique_ptr<datanodestore::DataNode> rootNode);
virtual ~DataTree();
void addDataLeaf();
private:
datanodestore::DataNodeStore *_nodeStore;
std::unique_ptr<datanodestore::DataNode> _rootNode;
std::unique_ptr<datanodestore::DataInnerNode> LowestRightBorderNodeWithLessThanKChildrenOrNull();
std::unique_ptr<datanodestore::DataLeafNode> addDataLeafAt(datanodestore::DataInnerNode *insertPos);
std::unique_ptr<datanodestore::DataInnerNode> createChainOfInnerNodes(unsigned int num, const datanodestore::DataLeafNode &leaf);
std::unique_ptr<datanodestore::DataLeafNode> addDataLeafToFullTree();
std::unique_ptr<datanodestore::DataNode> copyNode(const datanodestore::DataNode &source);
DISALLOW_COPY_AND_ASSIGN(DataTree);
};
}
}
}
#endif

View File

@ -0,0 +1,34 @@
#include "DataTreeStore.h"
#include "blobstore/implementations/onblocks/datanodestore/DataNodeStore.h"
#include "blobstore/implementations/onblocks/datanodestore/DataLeafNode.h"
#include "DataTree.h"
using std::unique_ptr;
using std::make_unique;
using blobstore::onblocks::datanodestore::DataNodeStore;
using blobstore::onblocks::datanodestore::DataNode;
namespace blobstore {
namespace onblocks {
namespace datatreestore {
DataTreeStore::DataTreeStore(unique_ptr<DataNodeStore> nodeStore)
: _nodeStore(std::move(nodeStore)) {
}
DataTreeStore::~DataTreeStore() {
}
unique_ptr<DataTree> DataTreeStore::load(const blockstore::Key &key) {
return make_unique<DataTree>(_nodeStore.get(), _nodeStore->load(key));
}
unique_ptr<DataTree> DataTreeStore::createNewTree() {
unique_ptr<DataNode> newleaf = _nodeStore->createNewLeafNode();
return make_unique<DataTree>(_nodeStore.get(), std::move(newleaf));
}
}
}
}

View File

@ -0,0 +1,38 @@
#pragma once
#ifndef BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATATREESTORE_DATATREESTORE_H_
#define BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATATREESTORE_DATATREESTORE_H_
#include <memory>
#include "fspp/utils/macros.h"
namespace blockstore{
class Key;
}
namespace blobstore {
namespace onblocks {
namespace datanodestore {
class DataNodeStore;
}
namespace datatreestore {
class DataTree;
class DataTreeStore {
public:
DataTreeStore(std::unique_ptr<datanodestore::DataNodeStore> nodeStore);
virtual ~DataTreeStore();
std::unique_ptr<DataTree> load(const blockstore::Key &key);
std::unique_ptr<DataTree> createNewTree();
private:
std::unique_ptr<datanodestore::DataNodeStore> _nodeStore;
DISALLOW_COPY_AND_ASSIGN(DataTreeStore);
};
}
}
}
#endif

View File

@ -138,3 +138,5 @@ TEST_F(DataInnerNodeTest, BuildingAThreeLevelTreeAndReload) {
EXPECT_EQ(node2->key(), parent->getChild(1)->key());
}
//TODO TestCase for LastChild