From dd80ab8d4dfe5a2686b4073e01d9b05ceacfad1c Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 11 Mar 2015 01:04:48 +0100 Subject: [PATCH] Add Blob::tryRead() --- implementations/onblocks/BlobOnBlocks.cpp | 14 +++++++++++--- implementations/onblocks/BlobOnBlocks.h | 1 + interface/Blob.h | 3 +++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/implementations/onblocks/BlobOnBlocks.cpp b/implementations/onblocks/BlobOnBlocks.cpp index 074dc3c3..94e1fc64 100644 --- a/implementations/onblocks/BlobOnBlocks.cpp +++ b/implementations/onblocks/BlobOnBlocks.cpp @@ -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 { - assert(offset <= _datatree->numStoredBytes() && offset + size <= _datatree->numStoredBytes()); - traverseLeaves(offset, size, [target, offset] (uint64_t indexOfFirstLeafByte, const DataLeafNode *leaf, uint32_t leafDataOffset, uint32_t leafDataSize) { +void BlobOnBlocks::read(void *target, uint64_t offset, uint64_t count) const { + assert(offset <= _datatree->numStoredBytes() && offset + count <= size()); + 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 leaf->read((uint8_t*)target + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize); }); + return realCount; } void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t size) { diff --git a/implementations/onblocks/BlobOnBlocks.h b/implementations/onblocks/BlobOnBlocks.h index 67f008e5..ed5f3f44 100644 --- a/implementations/onblocks/BlobOnBlocks.h +++ b/implementations/onblocks/BlobOnBlocks.h @@ -26,6 +26,7 @@ public: void resize(uint64_t numBytes) 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; std::unique_ptr releaseTree(); diff --git a/interface/Blob.h b/interface/Blob.h index ea74b9b3..d400148d 100644 --- a/interface/Blob.h +++ b/interface/Blob.h @@ -22,7 +22,10 @@ public: virtual void resize(uint64_t numBytes) = 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; + + //TODO Test tryRead }; }