libcryfs/implementations/onblocks/BlobOnBlocks.cpp

96 lines
3.8 KiB
C++
Raw Normal View History

#include "parallelaccessdatatreestore/DataTreeRef.h"
2015-02-17 00:40:34 +01:00
#include "BlobOnBlocks.h"
#include "datanodestore/DataLeafNode.h"
#include "utils/Math.h"
#include <cmath>
#include <messmer/cpp-utils/assert/assert.h>
using std::function;
2015-06-26 15:59:18 +02:00
using cpputils::unique_ref;
using blobstore::onblocks::datanodestore::DataLeafNode;
using blobstore::onblocks::datanodestore::DataNodeLayout;
2015-02-27 00:18:26 +01:00
using blockstore::Key;
2014-12-09 17:56:48 +01:00
namespace blobstore {
namespace onblocks {
using parallelaccessdatatreestore::DataTreeRef;
2015-06-26 15:59:18 +02:00
BlobOnBlocks::BlobOnBlocks(unique_ref<DataTreeRef> datatree)
: _datatree(std::move(datatree)) {
2014-12-09 17:56:48 +01:00
}
BlobOnBlocks::~BlobOnBlocks() {
2014-12-09 17:56:48 +01:00
}
uint64_t BlobOnBlocks::size() const {
2015-02-25 23:09:48 +01:00
return _datatree->numStoredBytes();
2014-12-09 17:56:48 +01:00
}
void BlobOnBlocks::resize(uint64_t numBytes) {
_datatree->resizeNumBytes(numBytes);
}
void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, function<void (uint64_t, DataLeafNode *leaf, uint32_t, uint32_t)> func) const {
uint64_t endByte = beginByte + sizeBytes;
uint32_t firstLeaf = beginByte / _datatree->maxBytesPerLeaf();
uint32_t endLeaf = utils::ceilDivision(endByte, _datatree->maxBytesPerLeaf());
bool traversingOutOfRange = _datatree->numStoredBytes() < endByte; //TODO numBytes() inefficient
_datatree->traverseLeaves(firstLeaf, endLeaf, [&func, beginByte, endByte, endLeaf, traversingOutOfRange](DataLeafNode *leaf, uint32_t leafIndex) {
uint64_t indexOfFirstLeafByte = leafIndex * leaf->maxStoreableBytes();
uint32_t dataBegin = utils::maxZeroSubtraction(beginByte, indexOfFirstLeafByte);
uint32_t dataEnd = std::min((uint64_t)leaf->maxStoreableBytes(), endByte - indexOfFirstLeafByte);
if (leafIndex == endLeaf-1 && traversingOutOfRange) {
// If we are traversing an area that didn't exist before, then the last leaf was just created with a wrong size. We have to fix it.
leaf->resize(dataEnd);
}
func(indexOfFirstLeafByte, leaf, dataBegin, dataEnd-dataBegin);
});
}
2015-03-11 01:04:48 +01:00
void BlobOnBlocks::read(void *target, uint64_t offset, uint64_t count) const {
ASSERT(offset <= _datatree->numStoredBytes() && offset + count <= size(), "BlobOnBlocks::read() read outside blob. Use BlobOnBlocks::tryRead() if this should be allowed.");
2015-03-11 01:04:48 +01:00
uint64_t read = tryRead(target, offset, count);
ASSERT(read == count, "BlobOnBlocks::read() couldn't read all requested bytes. Use BlobOnBlocks::tryRead() if this should be allowed.");
2015-03-11 01:04:48 +01:00
}
uint64_t BlobOnBlocks::tryRead(void *target, uint64_t offset, uint64_t count) const {
//TODO Quite inefficient to call size() here, because that has to traverse the tree
2015-04-16 16:36:25 +02:00
uint64_t realCount = std::max(UINT64_C(0), std::min(count, size()-offset));
2015-03-11 01:04:48 +01:00
traverseLeaves(offset, realCount, [target, offset] (uint64_t indexOfFirstLeafByte, const DataLeafNode *leaf, uint32_t leafDataOffset, uint32_t leafDataSize) {
//TODO Simplify formula, make it easier to understand
leaf->read((uint8_t*)target + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
});
2015-03-11 01:04:48 +01:00
return realCount;
}
void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t size) {
traverseLeaves(offset, size, [source, offset] (uint64_t indexOfFirstLeafByte, DataLeafNode *leaf, uint32_t leafDataOffset, uint32_t leafDataSize) {
//TODO Simplify formula, make it easier to understand
leaf->write((uint8_t*)source + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
});
}
2015-04-09 23:41:51 +02:00
void BlobOnBlocks::flush() {
_datatree->flush();
}
void BlobOnBlocks::resizeIfSmallerThan(uint64_t neededSize) {
//TODO This is inefficient, because size() and resizeNumBytes() both traverse the tree. Better: _datatree->ensureMinSize(x)
if (neededSize > size()) {
_datatree->resizeNumBytes(neededSize);
}
}
2015-02-27 00:18:26 +01:00
Key BlobOnBlocks::key() const {
return _datatree->key();
}
2015-06-26 15:59:18 +02:00
unique_ref<DataTreeRef> BlobOnBlocks::releaseTree() {
return std::move(_datatree);
}
}
}