Simplify code a bit and add assertions
This commit is contained in:
parent
41146282ca
commit
fce81f47ff
@ -37,7 +37,7 @@ void BlobOnBlocks::resize(uint64_t numBytes) {
|
|||||||
_sizeCache = numBytes;
|
_sizeCache = numBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, function<void (uint64_t leafOffset, DataLeafNode *leaf, uint32_t begin, uint32_t count)> onExistingLeaf, function<Data (uint64_t leafOffset, uint32_t begin, uint32_t count)> onCreateLeaf) const {
|
void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, function<void (uint64_t leafOffset, DataLeafNode *leaf, uint32_t begin, uint32_t count)> onExistingLeaf, function<Data (uint64_t beginByte, uint32_t count)> onCreateLeaf) const {
|
||||||
uint64_t endByte = beginByte + sizeBytes;
|
uint64_t endByte = beginByte + sizeBytes;
|
||||||
uint64_t maxBytesPerLeaf = _datatree->maxBytesPerLeaf();
|
uint64_t maxBytesPerLeaf = _datatree->maxBytesPerLeaf();
|
||||||
uint32_t firstLeaf = beginByte / maxBytesPerLeaf;
|
uint32_t firstLeaf = beginByte / maxBytesPerLeaf;
|
||||||
@ -46,6 +46,7 @@ void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, functi
|
|||||||
auto _onExistingLeaf = [&onExistingLeaf, beginByte, endByte, endLeaf, maxBytesPerLeaf, &blobIsGrowingFromThisTraversal] (uint32_t leafIndex, DataLeafNode *leaf) {
|
auto _onExistingLeaf = [&onExistingLeaf, beginByte, endByte, endLeaf, maxBytesPerLeaf, &blobIsGrowingFromThisTraversal] (uint32_t leafIndex, DataLeafNode *leaf) {
|
||||||
uint64_t indexOfFirstLeafByte = leafIndex * maxBytesPerLeaf;
|
uint64_t indexOfFirstLeafByte = leafIndex * maxBytesPerLeaf;
|
||||||
ASSERT(endByte > indexOfFirstLeafByte, "Traversal went too far right");
|
ASSERT(endByte > indexOfFirstLeafByte, "Traversal went too far right");
|
||||||
|
ASSERT(leafIndex == endLeaf-1 || leaf->numBytes() == maxBytesPerLeaf, "All leafes but the rightmost one have to have maximal size");
|
||||||
uint32_t dataBegin = utils::maxZeroSubtraction(beginByte, indexOfFirstLeafByte);
|
uint32_t dataBegin = utils::maxZeroSubtraction(beginByte, indexOfFirstLeafByte);
|
||||||
uint32_t dataEnd = std::min(maxBytesPerLeaf, endByte - indexOfFirstLeafByte);
|
uint32_t dataEnd = std::min(maxBytesPerLeaf, endByte - indexOfFirstLeafByte);
|
||||||
if (leafIndex == endLeaf-1 && leaf->numBytes() < dataEnd) {
|
if (leafIndex == endLeaf-1 && leaf->numBytes() < dataEnd) {
|
||||||
@ -55,13 +56,16 @@ void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, functi
|
|||||||
}
|
}
|
||||||
onExistingLeaf(indexOfFirstLeafByte, leaf, dataBegin, dataEnd-dataBegin);
|
onExistingLeaf(indexOfFirstLeafByte, leaf, dataBegin, dataEnd-dataBegin);
|
||||||
};
|
};
|
||||||
auto _onCreateLeaf = [&onCreateLeaf, maxBytesPerLeaf, beginByte, endByte, &blobIsGrowingFromThisTraversal] (uint32_t leafIndex) -> Data {
|
auto _onCreateLeaf = [&onCreateLeaf, maxBytesPerLeaf, beginByte, firstLeaf, endByte, endLeaf, &blobIsGrowingFromThisTraversal] (uint32_t leafIndex) -> Data {
|
||||||
blobIsGrowingFromThisTraversal = true;
|
blobIsGrowingFromThisTraversal = true;
|
||||||
uint64_t indexOfFirstLeafByte = leafIndex * maxBytesPerLeaf;
|
uint64_t indexOfFirstLeafByte = leafIndex * maxBytesPerLeaf;
|
||||||
ASSERT(endByte > indexOfFirstLeafByte, "Traversal went too far right");
|
ASSERT(endByte > indexOfFirstLeafByte, "Traversal went too far right");
|
||||||
uint32_t dataBegin = utils::maxZeroSubtraction(beginByte, indexOfFirstLeafByte);
|
uint32_t dataBegin = utils::maxZeroSubtraction(beginByte, indexOfFirstLeafByte);
|
||||||
uint32_t dataEnd = std::min(maxBytesPerLeaf, endByte - indexOfFirstLeafByte);
|
uint32_t dataEnd = std::min(maxBytesPerLeaf, endByte - indexOfFirstLeafByte);
|
||||||
Data data = onCreateLeaf(indexOfFirstLeafByte, dataBegin, dataEnd-dataBegin);
|
ASSERT(leafIndex == firstLeaf || dataBegin == 0, "Only the leftmost leaf can have a gap on the left.");
|
||||||
|
ASSERT(leafIndex == endLeaf-1 || dataEnd == maxBytesPerLeaf, "Only the rightmost leaf can have a gap on the right");
|
||||||
|
Data data = onCreateLeaf(indexOfFirstLeafByte + dataBegin, dataEnd-dataBegin);
|
||||||
|
ASSERT(data.size() == dataEnd-dataBegin, "Returned leaf data with wrong size");
|
||||||
// If this leaf is created but only partly in the traversed region (i.e. dataBegin > leafBegin), we have to fill the data before the traversed region with zeroes.
|
// If this leaf is created but only partly in the traversed region (i.e. dataBegin > leafBegin), we have to fill the data before the traversed region with zeroes.
|
||||||
if (dataBegin != 0) {
|
if (dataBegin != 0) {
|
||||||
Data actualData(dataBegin + data.size());
|
Data actualData(dataBegin + data.size());
|
||||||
@ -69,7 +73,6 @@ void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, functi
|
|||||||
std::memcpy(actualData.dataOffset(dataBegin), data.data(), data.size());
|
std::memcpy(actualData.dataOffset(dataBegin), data.data(), data.size());
|
||||||
data = std::move(actualData);
|
data = std::move(actualData);
|
||||||
}
|
}
|
||||||
ASSERT(data.size() == dataEnd, "Returned leaf data with wrong size");
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
_datatree->traverseLeaves(firstLeaf, endLeaf, _onExistingLeaf, _onCreateLeaf);
|
_datatree->traverseLeaves(firstLeaf, endLeaf, _onExistingLeaf, _onCreateLeaf);
|
||||||
@ -106,7 +109,7 @@ void BlobOnBlocks::_read(void *target, uint64_t offset, uint64_t count) const {
|
|||||||
//TODO Simplify formula, make it easier to understand
|
//TODO Simplify formula, make it easier to understand
|
||||||
leaf->read((uint8_t*)target + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
|
leaf->read((uint8_t*)target + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
|
||||||
};
|
};
|
||||||
auto onCreateLeaf = [] (uint64_t /*indexOfFirstLeafByte*/, uint32_t /*begin*/, uint32_t /*count*/) -> Data {
|
auto onCreateLeaf = [] (uint64_t /*beginByte*/, uint32_t /*count*/) -> Data {
|
||||||
ASSERT(false, "Reading shouldn't create new leaves.");
|
ASSERT(false, "Reading shouldn't create new leaves.");
|
||||||
};
|
};
|
||||||
traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf);
|
traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf);
|
||||||
@ -117,10 +120,10 @@ void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) {
|
|||||||
//TODO Simplify formula, make it easier to understand
|
//TODO Simplify formula, make it easier to understand
|
||||||
leaf->write((uint8_t*)source + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
|
leaf->write((uint8_t*)source + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize);
|
||||||
};
|
};
|
||||||
auto onCreateLeaf = [source, offset] (uint64_t indexOfFirstLeafByte, uint32_t leafDataOffset, uint32_t count) -> Data {
|
auto onCreateLeaf = [source, offset] (uint64_t beginByte, uint32_t count) -> Data {
|
||||||
Data result(count);
|
Data result(count);
|
||||||
//TODO Simplify formula, make it easier to understand
|
//TODO Simplify formula, make it easier to understand
|
||||||
std::memcpy(result.data(), (uint8_t*)source + indexOfFirstLeafByte - offset + leafDataOffset, count);
|
std::memcpy(result.data(), (uint8_t*)source + beginByte - offset, count);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf);
|
traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf);
|
||||||
|
@ -38,7 +38,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void _read(void *target, uint64_t offset, uint64_t count) const;
|
void _read(void *target, uint64_t offset, uint64_t count) const;
|
||||||
void traverseLeaves(uint64_t offsetBytes, uint64_t sizeBytes, std::function<void (uint64_t leafOffset, datanodestore::DataLeafNode *leaf, uint32_t begin, uint32_t count)> onExistingLeaf, std::function<cpputils::Data (uint64_t leafOffset, uint32_t begin, uint32_t count)> onCreateLeaf) const;
|
void traverseLeaves(uint64_t offsetBytes, uint64_t sizeBytes, std::function<void (uint64_t leafOffset, datanodestore::DataLeafNode *leaf, uint32_t begin, uint32_t count)> onExistingLeaf, std::function<cpputils::Data (uint64_t beginByte, uint32_t count)> onCreateLeaf) const;
|
||||||
|
|
||||||
cpputils::unique_ref<parallelaccessdatatreestore::DataTreeRef> _datatree;
|
cpputils::unique_ref<parallelaccessdatatreestore::DataTreeRef> _datatree;
|
||||||
mutable boost::optional<uint64_t> _sizeCache;
|
mutable boost::optional<uint64_t> _sizeCache;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user