2015-01-22 23:37:03 +01:00
|
|
|
#include "DataInnerNode.h"
|
|
|
|
#include "DataLeafNode.h"
|
|
|
|
#include "DataNodeStore.h"
|
2015-02-17 00:40:34 +01:00
|
|
|
#include "messmer/blockstore/interface/BlockStore.h"
|
|
|
|
#include "messmer/blockstore/interface/Block.h"
|
|
|
|
#include "messmer/blockstore/utils/BlockStoreUtils.h"
|
2014-12-13 17:43:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
using blockstore::BlockStore;
|
|
|
|
using blockstore::Block;
|
|
|
|
using blockstore::Key;
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::runtime_error;
|
|
|
|
|
|
|
|
namespace blobstore {
|
|
|
|
namespace onblocks {
|
2014-12-13 19:17:08 +01:00
|
|
|
namespace datanodestore {
|
2014-12-13 17:43:02 +01:00
|
|
|
|
|
|
|
DataNodeStore::DataNodeStore(unique_ptr<BlockStore> blockstore)
|
|
|
|
: _blockstore(std::move(blockstore)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DataNodeStore::~DataNodeStore() {
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:27:14 +01:00
|
|
|
unique_ptr<DataNode> DataNodeStore::load(unique_ptr<Block> block) {
|
2014-12-13 17:43:02 +01:00
|
|
|
DataNodeView node(std::move(block));
|
|
|
|
|
|
|
|
if (*node.Depth() == 0) {
|
2015-01-24 22:27:14 +01:00
|
|
|
return unique_ptr<DataLeafNode>(new DataLeafNode(std::move(node)));
|
2014-12-13 17:43:02 +01:00
|
|
|
} else if (*node.Depth() <= MAX_DEPTH) {
|
2015-01-24 22:27:14 +01:00
|
|
|
return unique_ptr<DataInnerNode>(new DataInnerNode(std::move(node)));
|
2014-12-13 17:43:02 +01:00
|
|
|
} else {
|
|
|
|
throw runtime_error("Tree is to deep. Data corruption?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 17:58:11 +01:00
|
|
|
unique_ptr<DataInnerNode> DataNodeStore::createNewInnerNode(const DataNode &first_child) {
|
2014-12-13 17:43:02 +01:00
|
|
|
auto block = _blockstore->create(DataNodeView::BLOCKSIZE_BYTES);
|
2015-02-20 19:46:52 +01:00
|
|
|
return DataInnerNode::InitializeNewNode(std::move(block), first_child);
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-13 17:58:11 +01:00
|
|
|
unique_ptr<DataLeafNode> DataNodeStore::createNewLeafNode() {
|
2014-12-13 17:43:02 +01:00
|
|
|
auto block = _blockstore->create(DataNodeView::BLOCKSIZE_BYTES);
|
2015-02-20 19:46:52 +01:00
|
|
|
return DataLeafNode::InitializeNewNode(std::move(block));
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<DataNode> DataNodeStore::load(const Key &key) {
|
2015-02-23 16:21:02 +01:00
|
|
|
auto block = _blockstore->load(key);
|
|
|
|
if (block == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return load(std::move(block));
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:59:42 +01:00
|
|
|
unique_ptr<DataNode> DataNodeStore::createNewNodeAsCopyFrom(const DataNode &source) {
|
|
|
|
auto newBlock = blockstore::utils::copyToNewBlock(_blockstore.get(), source.node().block());
|
2015-01-24 22:27:14 +01:00
|
|
|
return load(std::move(newBlock));
|
2015-01-24 01:59:42 +01:00
|
|
|
}
|
|
|
|
|
2015-02-22 19:30:42 +01:00
|
|
|
unique_ptr<DataNode> DataNodeStore::overwriteNodeWith(unique_ptr<DataNode> target, const DataNode &source) {
|
|
|
|
Key key = target->key();
|
|
|
|
{
|
|
|
|
auto targetBlock = target->node().releaseBlock();
|
|
|
|
target.reset();
|
|
|
|
blockstore::utils::copyTo(targetBlock.get(), source.node().block());
|
|
|
|
}
|
|
|
|
return load(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataNodeStore::remove(unique_ptr<DataNode> node) {
|
|
|
|
auto block = node->node().releaseBlock();
|
|
|
|
node.reset();
|
|
|
|
_blockstore->remove(std::move(block));
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:06:45 +01:00
|
|
|
uint64_t DataNodeStore::numNodes() const {
|
|
|
|
return _blockstore->numBlocks();
|
|
|
|
}
|
|
|
|
|
2015-02-24 22:44:10 +01:00
|
|
|
void DataNodeStore::removeSubtree(unique_ptr<DataNode> node) {
|
|
|
|
DataInnerNode *inner = dynamic_cast<DataInnerNode*>(node.get());
|
|
|
|
if (inner != nullptr) {
|
|
|
|
for (int i = 0; i < inner->numChildren(); ++i) {
|
|
|
|
auto child = load(inner->getChild(i)->key());
|
|
|
|
removeSubtree(std::move(child));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remove(std::move(node));
|
|
|
|
}
|
|
|
|
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 19:17:08 +01:00
|
|
|
}
|