libcryfs/implementations/onblocks/datanodestore/DataNodeStore.cpp

114 lines
3.9 KiB
C++
Raw Normal View History

#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"
using blockstore::BlockStore;
using blockstore::Block;
using blockstore::Key;
using cpputils::Data;
2015-06-26 15:59:18 +02:00
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using std::unique_ptr;
using std::runtime_error;
2015-06-26 15:59:18 +02:00
using boost::optional;
using boost::none;
namespace blobstore {
namespace onblocks {
namespace datanodestore {
2015-06-26 15:59:18 +02:00
DataNodeStore::DataNodeStore(unique_ref<BlockStore> blockstore, uint32_t blocksizeBytes)
: _blockstore(std::move(blockstore)), _layout(blocksizeBytes) {
}
DataNodeStore::~DataNodeStore() {
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataNodeStore::load(unique_ptr<Block> block) {
assert(block->size() == _layout.blocksizeBytes());
DataNodeView node(std::move(block));
if (node.Depth() == 0) {
2015-06-26 15:59:18 +02:00
return make_unique_ref<DataLeafNode>(std::move(node));
} else if (node.Depth() <= MAX_DEPTH) {
2015-06-26 15:59:18 +02:00
return make_unique_ref<DataInnerNode>(std::move(node));
} 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) {
assert(first_child.node().layout().blocksizeBytes() == _layout.blocksizeBytes()); // This might be violated if source is from a different 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());
return DataInnerNode::InitializeNewNode(std::move(block), first_child);
}
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());
return DataLeafNode::InitializeNewNode(std::move(block));
}
2015-06-26 15:59:18 +02:00
optional<unique_ref<DataNode>> DataNodeStore::load(const Key &key) {
auto block = _blockstore->load(key);
if (block == nullptr) {
2015-06-26 15:59:18 +02:00
return none;
}
return load(std::move(block));
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataNodeStore::createNewNodeAsCopyFrom(const DataNode &source) {
assert(source.node().layout().blocksizeBytes() == _layout.blocksizeBytes()); // This might be violated if source is from a different DataNodeStore
auto newBlock = blockstore::utils::copyToNewBlock(_blockstore.get(), source.node().block());
return load(std::move(newBlock));
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataNodeStore::overwriteNodeWith(unique_ref<DataNode> target, const DataNode &source) {
assert(target->node().layout().blocksizeBytes() == _layout.blocksizeBytes());
assert(source.node().layout().blocksizeBytes() == _layout.blocksizeBytes());
Key key = target->key();
{
auto targetBlock = target->node().releaseBlock();
2015-06-26 15:59:18 +02:00
cpputils::to_unique_ptr(std::move(target)).reset(); // Call destructor
blockstore::utils::copyTo(targetBlock.get(), source.node().block());
}
2015-06-26 15:59:18 +02:00
auto loaded = load(key);
assert(loaded != none);
return std::move(*loaded);
}
2015-06-26 15:59:18 +02:00
void DataNodeStore::remove(unique_ref<DataNode> node) {
auto block = node->node().releaseBlock();
2015-06-26 15:59:18 +02:00
cpputils::to_unique_ptr(std::move(node)).reset(); // Call destructor
_blockstore->remove(std::move(block));
}
uint64_t DataNodeStore::numNodes() const {
return _blockstore->numBlocks();
}
2015-06-26 15:59:18 +02:00
void DataNodeStore::removeSubtree(unique_ref<DataNode> node) {
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) {
auto child = load(inner->getChild(i)->key());
2015-06-26 15:59:18 +02:00
assert(child != none);
removeSubtree(std::move(*child));
}
}
remove(std::move(node));
}
DataNodeLayout DataNodeStore::layout() const {
return _layout;
}
}
}
}