libcryfs/implementations/onblocks/datatreestore/DataTree.cpp

322 lines
12 KiB
C++
Raw Normal View History

2015-01-23 04:39:36 +01:00
#include "DataTree.h"
2015-02-17 00:40:34 +01:00
#include "../datanodestore/DataNodeStore.h"
#include "../datanodestore/DataInnerNode.h"
#include "../datanodestore/DataLeafNode.h"
#include "../utils/Math.h"
2015-01-23 04:39:36 +01:00
#include "impl/algorithms.h"
2015-06-21 17:43:45 +02:00
#include "messmer/cpp-utils/pointer/cast.h"
#include "messmer/cpp-utils/pointer/optional_ownership_ptr.h"
#include <cmath>
2015-01-23 04:39:36 +01:00
using blockstore::Key;
using blobstore::onblocks::datanodestore::DataNodeStore;
using blobstore::onblocks::datanodestore::DataNode;
using blobstore::onblocks::datanodestore::DataInnerNode;
using blobstore::onblocks::datanodestore::DataLeafNode;
using blobstore::onblocks::datanodestore::DataNodeLayout;
2015-01-23 04:39:36 +01:00
using std::dynamic_pointer_cast;
using std::function;
2015-04-09 16:10:57 +02:00
using boost::shared_mutex;
using boost::shared_lock;
using boost::unique_lock;
2015-06-26 15:59:18 +02:00
using boost::none;
using std::vector;
2015-01-23 04:39:36 +01:00
2015-02-17 00:40:34 +01:00
using cpputils::dynamic_pointer_move;
using cpputils::optional_ownership_ptr;
using cpputils::WithOwnership;
using cpputils::WithoutOwnership;
2015-06-26 15:59:18 +02:00
using cpputils::unique_ref;
2015-01-23 04:39:36 +01:00
namespace blobstore {
namespace onblocks {
namespace datatreestore {
2015-06-26 15:59:18 +02:00
DataTree::DataTree(DataNodeStore *nodeStore, unique_ref<DataNode> rootNode)
2015-04-09 16:10:57 +02:00
: _mutex(), _nodeStore(nodeStore), _rootNode(std::move(rootNode)) {
2015-01-23 04:39:36 +01:00
}
DataTree::~DataTree() {
}
void DataTree::removeLastDataLeaf() {
auto deletePosOrNull = algorithms::GetLowestRightBorderNodeWithMoreThanOneChildOrNull(_nodeStore, _rootNode.get());
assert(deletePosOrNull.get() != nullptr); //TODO Correct exception (tree has only one leaf, can't shrink it)
deleteLastChildSubtree(deletePosOrNull.get());
ifRootHasOnlyOneChildReplaceRootWithItsChild();
}
void DataTree::ifRootHasOnlyOneChildReplaceRootWithItsChild() {
DataInnerNode *rootNode = dynamic_cast<DataInnerNode*>(_rootNode.get());
assert(rootNode != nullptr);
if (rootNode->numChildren() == 1) {
auto child = _nodeStore->load(rootNode->getChild(0)->key());
2015-06-26 15:59:18 +02:00
assert(child != none);
_rootNode = _nodeStore->overwriteNodeWith(std::move(_rootNode), **child);
_nodeStore->remove(std::move(*child));
}
}
void DataTree::deleteLastChildSubtree(DataInnerNode *node) {
2015-02-24 23:11:20 +01:00
auto lastChild = _nodeStore->load(node->LastChild()->key());
2015-06-26 15:59:18 +02:00
assert(lastChild != none);
_nodeStore->removeSubtree(std::move(*lastChild));
node->removeLastChild();
}
2015-06-26 15:59:18 +02:00
unique_ref<DataLeafNode> DataTree::addDataLeaf() {
auto insertPosOrNull = algorithms::GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNull(_nodeStore, _rootNode.get());
2015-01-23 04:39:36 +01:00
if (insertPosOrNull) {
return addDataLeafAt(insertPosOrNull.get());
2015-01-23 04:39:36 +01:00
} else {
return addDataLeafToFullTree();
2015-01-23 04:39:36 +01:00
}
}
2015-06-26 15:59:18 +02:00
unique_ref<DataLeafNode> DataTree::addDataLeafAt(DataInnerNode *insertPos) {
2015-01-23 04:39:36 +01:00
auto new_leaf = _nodeStore->createNewLeafNode();
auto chain = createChainOfInnerNodes(insertPos->depth()-1, new_leaf.get());
insertPos->addChild(*chain);
2015-01-23 04:39:36 +01:00
return new_leaf;
}
optional_ownership_ptr<DataNode> DataTree::createChainOfInnerNodes(unsigned int num, DataNode *child) {
2015-06-26 15:59:18 +02:00
//TODO This function is implemented twice, once with optional_ownership_ptr, once with unique_ref. Redundancy!
optional_ownership_ptr<DataNode> chain = cpputils::WithoutOwnership<DataNode>(child);
for(unsigned int i=0; i<num; ++i) {
auto newnode = _nodeStore->createNewInnerNode(*chain);
chain = cpputils::WithOwnership<DataNode>(std::move(newnode));
2015-01-23 04:39:36 +01:00
}
return chain;
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataTree::createChainOfInnerNodes(unsigned int num, unique_ref<DataNode> child) {
unique_ref<DataNode> chain = std::move(child);
for(unsigned int i=0; i<num; ++i) {
chain = _nodeStore->createNewInnerNode(*chain);
}
return chain;
}
DataInnerNode* DataTree::increaseTreeDepth(unsigned int levels) {
assert(levels >= 1);
auto copyOfOldRoot = _nodeStore->createNewNodeAsCopyFrom(*_rootNode);
auto chain = createChainOfInnerNodes(levels-1, copyOfOldRoot.get());
auto newRootNode = DataNode::convertToNewInnerNode(std::move(_rootNode), *chain);
DataInnerNode *result = newRootNode.get();
_rootNode = std::move(newRootNode);
return result;
}
2015-06-26 15:59:18 +02:00
unique_ref<DataLeafNode> DataTree::addDataLeafToFullTree() {
DataInnerNode *rootNode = increaseTreeDepth(1);
auto newLeaf = addDataLeafAt(rootNode);
return newLeaf;
2015-01-23 04:39:36 +01:00
}
const Key &DataTree::key() const {
return _rootNode->key();
}
2015-01-23 04:39:36 +01:00
void DataTree::flush() const {
_rootNode->flush();
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataTree::releaseRootNode() {
return std::move(_rootNode);
}
//TODO Test numLeaves(), for example also two configurations with same number of bytes but different number of leaves (last leaf has 0 bytes)
uint32_t DataTree::numLeaves() const {
return _numLeaves(*_rootNode);
}
uint32_t DataTree::_numLeaves(const DataNode &node) const {
const DataLeafNode *leaf = dynamic_cast<const DataLeafNode*>(&node);
if (leaf != nullptr) {
return 1;
}
const DataInnerNode &inner = dynamic_cast<const DataInnerNode&>(node);
uint64_t numLeavesInLeftChildren = (inner.numChildren()-1) * leavesPerFullChild(inner);
auto lastChild = _nodeStore->load(inner.LastChild()->key());
2015-06-26 15:59:18 +02:00
assert(lastChild != none);
uint64_t numLeavesInRightChild = _numLeaves(**lastChild);
return numLeavesInLeftChildren + numLeavesInRightChild;
}
void DataTree::traverseLeaves(uint32_t beginIndex, uint32_t endIndex, function<void (DataLeafNode*, uint32_t)> func) {
unique_lock<shared_mutex> lock(_mutex); //TODO Only lock when resizing
assert(beginIndex <= endIndex);
uint8_t neededTreeDepth = utils::ceilLog(_nodeStore->layout().maxChildrenPerInnerNode(), endIndex);
uint32_t numLeaves = this->numLeaves();
if (_rootNode->depth() < neededTreeDepth) {
//TODO Test cases that actually increase it here by 0 level / 1 level / more than 1 level
increaseTreeDepth(neededTreeDepth - _rootNode->depth());
}
if (numLeaves <= beginIndex) {
//TODO Test cases with numLeaves < / >= beginIndex
// There is a gap between the current size and the begin of the traversal
return _traverseLeaves(_rootNode.get(), 0, numLeaves-1, endIndex, [beginIndex, numLeaves, &func, this](DataLeafNode* node, uint32_t index) {
if (index >= beginIndex) {
func(node, index);
} else if (index == numLeaves - 1) {
// It is the old last leaf - resize it to maximum
node->resize(_nodeStore->layout().maxBytesPerLeaf());
}
});
} else if (numLeaves < endIndex) {
// We are starting traversal in the valid region, but traverse until after it (we grow new leaves)
return _traverseLeaves(_rootNode.get(), 0, beginIndex, endIndex, [numLeaves, &func, this] (DataLeafNode *node, uint32_t index) {
if (index == numLeaves - 1) {
// It is the old last leaf - resize it to maximum
node->resize(_nodeStore->layout().maxBytesPerLeaf());
}
func(node, index);
});
} else {
//We are traversing entierly inside the valid region
_traverseLeaves(_rootNode.get(), 0, beginIndex, endIndex, func);
}
}
void DataTree::_traverseLeaves(DataNode *root, uint32_t leafOffset, uint32_t beginIndex, uint32_t endIndex, function<void (DataLeafNode*, uint32_t)> func) {
DataLeafNode *leaf = dynamic_cast<DataLeafNode*>(root);
if (leaf != nullptr) {
assert(beginIndex <= 1 && endIndex <= 1);
if (beginIndex == 0 && endIndex == 1) {
func(leaf, leafOffset);
}
return;
}
DataInnerNode *inner = dynamic_cast<DataInnerNode*>(root);
2015-02-25 23:08:16 +01:00
uint32_t leavesPerChild = leavesPerFullChild(*inner);
uint32_t beginChild = beginIndex/leavesPerChild;
uint32_t endChild = utils::ceilDivision(endIndex, leavesPerChild);
2015-06-26 15:59:18 +02:00
vector<unique_ref<DataNode>> children = getOrCreateChildren(inner, beginChild, endChild);
for (uint32_t childIndex = beginChild; childIndex < endChild; ++childIndex) {
uint32_t childOffset = childIndex * leavesPerChild;
uint32_t localBeginIndex = utils::maxZeroSubtraction(beginIndex, childOffset);
uint32_t localEndIndex = std::min(leavesPerChild, endIndex - childOffset);
auto child = std::move(children[childIndex-beginChild]);
_traverseLeaves(child.get(), leafOffset + childOffset, localBeginIndex, localEndIndex, func);
}
}
2015-06-26 15:59:18 +02:00
vector<unique_ref<DataNode>> DataTree::getOrCreateChildren(DataInnerNode *node, uint32_t begin, uint32_t end) {
vector<unique_ref<DataNode>> children;
children.reserve(end-begin);
for (uint32_t childIndex = begin; childIndex < std::min(node->numChildren(), end); ++childIndex) {
2015-06-26 15:59:18 +02:00
auto child = _nodeStore->load(node->getChild(childIndex)->key());
assert(child != none);
children.emplace_back(std::move(*child));
}
for (uint32_t childIndex = node->numChildren(); childIndex < end; ++childIndex) {
children.emplace_back(addChildTo(node));
}
assert(children.size() == end-begin);
return children;
}
2015-06-26 15:59:18 +02:00
unique_ref<DataNode> DataTree::addChildTo(DataInnerNode *node) {
auto new_leaf = _nodeStore->createNewLeafNode();
new_leaf->resize(_nodeStore->layout().maxBytesPerLeaf());
auto chain = createChainOfInnerNodes(node->depth()-1, std::move(new_leaf));
node->addChild(*chain);
return std::move(chain);
}
2015-01-23 04:39:36 +01:00
2015-02-25 23:08:16 +01:00
uint32_t DataTree::leavesPerFullChild(const DataInnerNode &root) const {
return utils::intPow(_nodeStore->layout().maxChildrenPerInnerNode(), root.depth()-1);
2015-02-25 23:08:16 +01:00
}
uint64_t DataTree::numStoredBytes() const {
2015-04-09 16:30:36 +02:00
shared_lock<shared_mutex> lock(_mutex);
return _numStoredBytes();
2015-02-25 23:08:16 +01:00
}
2015-04-09 16:30:36 +02:00
uint64_t DataTree::_numStoredBytes() const {
return _numStoredBytes(*_rootNode);
}
uint64_t DataTree::_numStoredBytes(const DataNode &root) const {
2015-02-25 23:08:16 +01:00
const DataLeafNode *leaf = dynamic_cast<const DataLeafNode*>(&root);
if (leaf != nullptr) {
return leaf->numBytes();
}
const DataInnerNode &inner = dynamic_cast<const DataInnerNode&>(root);
uint64_t numBytesInLeftChildren = (inner.numChildren()-1) * leavesPerFullChild(inner) * _nodeStore->layout().maxBytesPerLeaf();
2015-02-25 23:08:16 +01:00
auto lastChild = _nodeStore->load(inner.LastChild()->key());
2015-06-26 15:59:18 +02:00
assert(lastChild != none);
uint64_t numBytesInRightChild = _numStoredBytes(**lastChild);
2015-02-25 23:08:16 +01:00
return numBytesInLeftChildren + numBytesInRightChild;
}
void DataTree::resizeNumBytes(uint64_t newNumBytes) {
2015-04-09 16:30:36 +02:00
boost::upgrade_lock<shared_mutex> lock(_mutex);
{
boost::upgrade_to_unique_lock<shared_mutex> exclusiveLock(lock);
//TODO Faster implementation possible (no addDataLeaf()/removeLastDataLeaf() in a loop, but directly resizing)
LastLeaf(_rootNode.get())->resize(_nodeStore->layout().maxBytesPerLeaf());
uint64_t currentNumBytes = _numStoredBytes();
assert(currentNumBytes % _nodeStore->layout().maxBytesPerLeaf() == 0);
uint32_t currentNumLeaves = currentNumBytes / _nodeStore->layout().maxBytesPerLeaf();
uint32_t newNumLeaves = std::max(1u, utils::ceilDivision(newNumBytes, _nodeStore->layout().maxBytesPerLeaf()));
for(uint32_t i = currentNumLeaves; i < newNumLeaves; ++i) {
addDataLeaf()->resize(_nodeStore->layout().maxBytesPerLeaf());
}
for(uint32_t i = currentNumLeaves; i > newNumLeaves; --i) {
removeLastDataLeaf();
}
uint32_t newLastLeafSize = newNumBytes - (newNumLeaves-1)*_nodeStore->layout().maxBytesPerLeaf();
LastLeaf(_rootNode.get())->resize(newLastLeafSize);
}
2015-02-26 18:53:24 +01:00
assert(newNumBytes == numStoredBytes());
}
optional_ownership_ptr<DataLeafNode> DataTree::LastLeaf(DataNode *root) {
DataLeafNode *leaf = dynamic_cast<DataLeafNode*>(root);
if (leaf != nullptr) {
return WithoutOwnership(leaf);
}
DataInnerNode *inner = dynamic_cast<DataInnerNode*>(root);
2015-06-26 15:59:18 +02:00
auto lastChild = _nodeStore->load(inner->LastChild()->key());
assert(lastChild != none);
return WithOwnership(LastLeaf(std::move(*lastChild)));
}
2015-06-26 15:59:18 +02:00
unique_ref<DataLeafNode> DataTree::LastLeaf(unique_ref<DataNode> root) {
auto leaf = dynamic_pointer_move<DataLeafNode>(root);
2015-06-26 15:59:18 +02:00
if (leaf != none) {
return std::move(*leaf);
}
auto inner = dynamic_pointer_move<DataInnerNode>(root);
2015-06-26 15:59:18 +02:00
assert(inner != none);
auto child = _nodeStore->load((*inner)->LastChild()->key());
assert(child != none);
return LastLeaf(std::move(*child));
}
uint32_t DataTree::maxBytesPerLeaf() const {
return _nodeStore->layout().maxBytesPerLeaf();
}
2015-01-23 04:39:36 +01:00
}
}
}