Add Blob::tryRead()

This commit is contained in:
Sebastian Messmer 2015-03-11 01:04:48 +01:00
parent 11930a8363
commit dd80ab8d4d
3 changed files with 15 additions and 3 deletions

View File

@ -44,12 +44,20 @@ void BlobOnBlocks::traverseLeaves(uint64_t beginByte, uint64_t sizeBytes, functi
}); });
} }
void BlobOnBlocks::read(void *target, uint64_t offset, uint64_t size) const { void BlobOnBlocks::read(void *target, uint64_t offset, uint64_t count) const {
assert(offset <= _datatree->numStoredBytes() && offset + size <= _datatree->numStoredBytes()); assert(offset <= _datatree->numStoredBytes() && offset + count <= size());
traverseLeaves(offset, size, [target, offset] (uint64_t indexOfFirstLeafByte, const DataLeafNode *leaf, uint32_t leafDataOffset, uint32_t leafDataSize) { uint64_t read = tryRead(target, offset, count);
assert(read == count);
}
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
uint64_t realCount = std::max(0uL, std::min(count, size()-offset));
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 //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);
}); });
return realCount;
} }
void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t size) { void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t size) {

View File

@ -26,6 +26,7 @@ public:
void resize(uint64_t numBytes) override; void resize(uint64_t numBytes) override;
void read(void *target, uint64_t offset, uint64_t size) const override; void read(void *target, uint64_t offset, uint64_t size) const override;
uint64_t tryRead(void *target, uint64_t offset, uint64_t size) const override;
void write(const void *source, uint64_t offset, uint64_t size) override; void write(const void *source, uint64_t offset, uint64_t size) override;
std::unique_ptr<datatreestore::DataTree> releaseTree(); std::unique_ptr<datatreestore::DataTree> releaseTree();

View File

@ -22,7 +22,10 @@ public:
virtual void resize(uint64_t numBytes) = 0; virtual void resize(uint64_t numBytes) = 0;
virtual void read(void *target, uint64_t offset, uint64_t size) const = 0; virtual void read(void *target, uint64_t offset, uint64_t size) const = 0;
virtual uint64_t tryRead(void *target, uint64_t offset, uint64_t size) const = 0;
virtual void write(const void *source, uint64_t offset, uint64_t size) = 0; virtual void write(const void *source, uint64_t offset, uint64_t size) = 0;
//TODO Test tryRead
}; };
} }