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"
|
2015-07-22 13:42:44 +02:00
|
|
|
#include <messmer/cpp-utils/assert/assert.h>
|
2014-12-13 17:43:02 +01:00
|
|
|
|
|
|
|
using blockstore::BlockStore;
|
|
|
|
using blockstore::Block;
|
|
|
|
using blockstore::Key;
|
2015-04-25 02:55:34 +02:00
|
|
|
using cpputils::Data;
|
2015-06-26 15:59:18 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2014-12-13 17:43:02 +01:00
|
|
|
using std::runtime_error;
|
2015-06-26 15:59:18 +02:00
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2014-12-13 17:43:02 +01:00
|
|
|
|
|
|
|
namespace blobstore {
|
|
|
|
namespace onblocks {
|
2014-12-13 19:17:08 +01:00
|
|
|
namespace datanodestore {
|
2014-12-13 17:43:02 +01:00
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
DataNodeStore::DataNodeStore(unique_ref<BlockStore> blockstore, uint32_t blocksizeBytes)
|
2015-02-25 22:30:48 +01:00
|
|
|
: _blockstore(std::move(blockstore)), _layout(blocksizeBytes) {
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataNodeStore::~DataNodeStore() {
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:00:57 +02:00
|
|
|
unique_ref<DataNode> DataNodeStore::load(unique_ref<Block> block) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(block->size() == _layout.blocksizeBytes(), "Loading block of wrong size");
|
2015-07-21 15:00:57 +02:00
|
|
|
DataNodeView node(std::move(block));
|
2014-12-13 17:43:02 +01:00
|
|
|
|
2015-03-04 20:58:39 +01:00
|
|
|
if (node.Depth() == 0) {
|
2015-06-26 15:59:18 +02:00
|
|
|
return make_unique_ref<DataLeafNode>(std::move(node));
|
2015-03-04 20:58:39 +01:00
|
|
|
} else if (node.Depth() <= MAX_DEPTH) {
|
2015-06-26 15:59:18 +02:00
|
|
|
return make_unique_ref<DataInnerNode>(std::move(node));
|
2014-12-13 17:43:02 +01:00
|
|
|
} else {
|
|
|
|
throw runtime_error("Tree is to deep. Data corruption?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataInnerNode> DataNodeStore::createNewInnerNode(const DataNode &first_child) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(first_child.node().layout().blocksizeBytes() == _layout.blocksizeBytes(), "Source node has wrong layout. Is it from the same DataNodeStore?");
|
2015-04-18 14:52:38 +02:00
|
|
|
//TODO Initialize block and then create it in the blockstore - this is more efficient than creating it and then writing to it
|
2015-04-25 00:31:05 +02:00
|
|
|
auto block = _blockstore->create(Data(_layout.blocksizeBytes()).FillWithZeroes());
|
2015-02-20 19:46:52 +01:00
|
|
|
return DataInnerNode::InitializeNewNode(std::move(block), first_child);
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataLeafNode> DataNodeStore::createNewLeafNode() {
|
2015-04-18 14:52:38 +02:00
|
|
|
//TODO Initialize block and then create it in the blockstore - this is more efficient than creating it and then writing to it
|
2015-04-25 00:31:05 +02:00
|
|
|
auto block = _blockstore->create(Data(_layout.blocksizeBytes()).FillWithZeroes());
|
2015-02-20 19:46:52 +01:00
|
|
|
return DataLeafNode::InitializeNewNode(std::move(block));
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
optional<unique_ref<DataNode>> DataNodeStore::load(const Key &key) {
|
2015-02-23 16:21:02 +01:00
|
|
|
auto block = _blockstore->load(key);
|
2015-07-21 15:00:57 +02:00
|
|
|
if (block == none) {
|
2015-06-26 15:59:18 +02:00
|
|
|
return none;
|
2015-07-21 15:00:57 +02:00
|
|
|
} else {
|
|
|
|
return load(std::move(*block));
|
2015-02-23 16:21:02 +01:00
|
|
|
}
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataNode> DataNodeStore::createNewNodeAsCopyFrom(const DataNode &source) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(source.node().layout().blocksizeBytes() == _layout.blocksizeBytes(), "Source node has wrong layout. Is it from the same DataNodeStore?");
|
2015-01-24 01:59:42 +01:00
|
|
|
auto newBlock = blockstore::utils::copyToNewBlock(_blockstore.get(), source.node().block());
|
2015-07-21 15:00:57 +02:00
|
|
|
return load(std::move(newBlock));
|
2015-01-24 01:59:42 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
unique_ref<DataNode> DataNodeStore::overwriteNodeWith(unique_ref<DataNode> target, const DataNode &source) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(target->node().layout().blocksizeBytes() == _layout.blocksizeBytes(), "Target node has wrong layout. Is it from the same DataNodeStore?");
|
|
|
|
ASSERT(source.node().layout().blocksizeBytes() == _layout.blocksizeBytes(), "Source node has wrong layout. Is it from the same DataNodeStore?");
|
2015-02-22 19:30:42 +01:00
|
|
|
Key key = target->key();
|
|
|
|
{
|
|
|
|
auto targetBlock = target->node().releaseBlock();
|
2015-07-21 15:24:49 +02:00
|
|
|
cpputils::destruct(std::move(target)); // Call destructor
|
2015-02-22 19:30:42 +01:00
|
|
|
blockstore::utils::copyTo(targetBlock.get(), source.node().block());
|
|
|
|
}
|
2015-06-26 15:59:18 +02:00
|
|
|
auto loaded = load(key);
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(loaded != none, "Couldn't load the target node after overwriting it");
|
2015-06-26 15:59:18 +02:00
|
|
|
return std::move(*loaded);
|
2015-02-22 19:30:42 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
void DataNodeStore::remove(unique_ref<DataNode> node) {
|
2015-02-22 19:30:42 +01:00
|
|
|
auto block = node->node().releaseBlock();
|
2015-07-21 15:24:49 +02:00
|
|
|
cpputils::destruct(std::move(node)); // Call destructor
|
2015-07-21 15:00:57 +02:00
|
|
|
_blockstore->remove(std::move(block));
|
2015-02-22 19:30:42 +01:00
|
|
|
}
|
|
|
|
|
2015-02-23 21:06:45 +01:00
|
|
|
uint64_t DataNodeStore::numNodes() const {
|
|
|
|
return _blockstore->numBlocks();
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 +02:00
|
|
|
void DataNodeStore::removeSubtree(unique_ref<DataNode> node) {
|
2015-02-25 22:30:48 +01:00
|
|
|
DataInnerNode *inner = dynamic_cast<DataInnerNode*>(node.get());
|
|
|
|
if (inner != nullptr) {
|
2015-03-16 18:32:37 +01:00
|
|
|
for (uint32_t i = 0; i < inner->numChildren(); ++i) {
|
2015-02-25 22:30:48 +01:00
|
|
|
auto child = load(inner->getChild(i)->key());
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(child != none, "Couldn't load child node");
|
2015-06-26 15:59:18 +02:00
|
|
|
removeSubtree(std::move(*child));
|
2015-02-25 22:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
remove(std::move(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
DataNodeLayout DataNodeStore::layout() const {
|
|
|
|
return _layout;
|
|
|
|
}
|
2015-02-24 22:44:10 +01:00
|
|
|
|
2014-12-13 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 19:17:08 +01:00
|
|
|
}
|