Add locking to blob

This commit is contained in:
Sebastian Messmer 2016-07-14 16:36:30 +02:00
parent 457ca52eb3
commit 6ff0839e90
2 changed files with 9 additions and 5 deletions

View File

@ -7,6 +7,8 @@
#include <cpp-utils/assert/assert.h> #include <cpp-utils/assert/assert.h>
using std::function; using std::function;
using std::unique_lock;
using std::mutex;
using cpputils::unique_ref; using cpputils::unique_ref;
using cpputils::Data; using cpputils::Data;
using blobstore::onblocks::datanodestore::DataLeafNode; using blobstore::onblocks::datanodestore::DataLeafNode;
@ -19,7 +21,7 @@ namespace onblocks {
using parallelaccessdatatreestore::DataTreeRef; using parallelaccessdatatreestore::DataTreeRef;
BlobOnBlocks::BlobOnBlocks(unique_ref<DataTreeRef> datatree) BlobOnBlocks::BlobOnBlocks(unique_ref<DataTreeRef> datatree)
: _datatree(std::move(datatree)), _sizeCache(boost::none) { : _datatree(std::move(datatree)), _sizeCache(boost::none), _mutex() {
} }
BlobOnBlocks::~BlobOnBlocks() { BlobOnBlocks::~BlobOnBlocks() {
@ -37,7 +39,8 @@ 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 beginByte, 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 {
unique_lock<mutex> lock(_mutex); // TODO Multiple traverse calls in parallel?
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;
@ -113,7 +116,7 @@ void BlobOnBlocks::_read(void *target, uint64_t offset, uint64_t count) const {
auto onCreateLeaf = [] (uint64_t /*beginByte*/, 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);
} }
void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) { void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) {
@ -129,7 +132,7 @@ void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) {
std::memcpy(result.data(), (uint8_t*)source + beginByte - offset, numBytes); std::memcpy(result.data(), (uint8_t*)source + beginByte - offset, numBytes);
return result; return result;
}; };
traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf); _traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf);
} }
void BlobOnBlocks::flush() { void BlobOnBlocks::flush() {

View File

@ -38,10 +38,11 @@ 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 beginByte, 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;
mutable std::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(BlobOnBlocks); DISALLOW_COPY_AND_ASSIGN(BlobOnBlocks);
}; };