Allow writing out of the bounds of a blob, resize blob in this case

This commit is contained in:
Sebastian Messmer 2015-03-04 03:29:22 +01:00
parent 5fb235a40c
commit b43464d669
3 changed files with 12 additions and 2 deletions

View File

@ -51,11 +51,19 @@ void BlobOnBlocks::read(void *target, uint64_t offset, uint64_t size) const {
}
void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t size) {
resizeIfSmallerThan(offset + size);
traverseLeaves(offset, size, [source] (uint64_t indexOfFirstLeafByte, void *leafDataBegin, uint32_t leafDataSize) {
std::memcpy(leafDataBegin, source, leafDataSize);
});
}
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);
}
}
Key BlobOnBlocks::key() const {
return _datatree->key();
}

View File

@ -29,7 +29,9 @@ public:
void write(const void *source, uint64_t offset, uint64_t size) override;
private:
void traverseLeaves(uint64_t offsetBytes, uint64_t sizeBytes, std::function<void (uint64_t, void *, uint32_t)>) const;
void traverseLeaves(uint64_t offsetBytes, uint64_t sizeBytes, std::function<void (uint64_t, void *, uint32_t)>) const;
void resizeIfSmallerThan(uint64_t neededSize);
std::unique_ptr<datatreestore::DataTree> _datatree;
};

View File

@ -169,7 +169,7 @@ uint64_t DataTree::numStoredBytes(const DataNode &root) const {
}
void DataTree::resizeNumBytes(uint64_t newNumBytes) {
//TODO Faster implementation possible
//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);