From 2cb0f4e5def56effc7f42e377768d61f5a8c5161 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 5 Mar 2015 22:16:57 +0100 Subject: [PATCH] BlockStore only writes changed data back if it actually was changed --- implementations/ondisk/OnDiskBlock.cpp | 13 +++++++++---- implementations/ondisk/OnDiskBlock.h | 1 + implementations/testfake/FakeBlock.cpp | 8 ++++++-- implementations/testfake/FakeBlock.h | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/implementations/ondisk/OnDiskBlock.cpp b/implementations/ondisk/OnDiskBlock.cpp index 44911d3e..2ece694f 100644 --- a/implementations/ondisk/OnDiskBlock.cpp +++ b/implementations/ondisk/OnDiskBlock.cpp @@ -20,15 +20,15 @@ namespace blockstore { namespace ondisk { OnDiskBlock::OnDiskBlock(const Key &key, const bf::path &filepath, size_t size) - : Block(key), _filepath(filepath), _data(size) { + : Block(key), _filepath(filepath), _data(size), _dataChanged(false) { } OnDiskBlock::OnDiskBlock(const Key &key, const bf::path &filepath, Data &&data) - : Block(key), _filepath(filepath), _data(std::move(data)) { + : Block(key), _filepath(filepath), _data(std::move(data)), _dataChanged(false) { } OnDiskBlock::~OnDiskBlock() { - _storeToDisk(); + flush(); } const void *OnDiskBlock::data() const { @@ -38,6 +38,7 @@ const void *OnDiskBlock::data() const { void OnDiskBlock::write(const void *source, uint64_t offset, uint64_t size) { assert(offset <= _data.size() && offset + size <= _data.size()); //Also check offset < _data->size() because of possible overflow in the addition std::memcpy((uint8_t*)_data.data()+offset, source, size); + _dataChanged = true; } size_t OnDiskBlock::size() const { @@ -61,6 +62,7 @@ unique_ptr OnDiskBlock::LoadFromDisk(const bf::path &rootdir, const } unique_ptr OnDiskBlock::CreateOnDisk(const bf::path &rootdir, const Key &key, size_t size) { + //TODO Only writeback, if data was actually changed (Block::write() was called) auto filepath = rootdir / key.ToString(); if (bf::exists(filepath)) { return nullptr; @@ -87,7 +89,10 @@ void OnDiskBlock::_storeToDisk() const { } void OnDiskBlock::flush() { - _storeToDisk(); + if (_dataChanged) { + _storeToDisk(); + _dataChanged = false; + } } } diff --git a/implementations/ondisk/OnDiskBlock.h b/implementations/ondisk/OnDiskBlock.h index db0bf19d..101d0215 100644 --- a/implementations/ondisk/OnDiskBlock.h +++ b/implementations/ondisk/OnDiskBlock.h @@ -31,6 +31,7 @@ public: private: const boost::filesystem::path _filepath; Data _data; + bool _dataChanged; OnDiskBlock(const Key &key, const boost::filesystem::path &filepath, size_t size); OnDiskBlock(const Key &key, const boost::filesystem::path &filepath, Data &&data); diff --git a/implementations/testfake/FakeBlock.cpp b/implementations/testfake/FakeBlock.cpp index 65f48964..6bdc37f3 100644 --- a/implementations/testfake/FakeBlock.cpp +++ b/implementations/testfake/FakeBlock.cpp @@ -15,7 +15,7 @@ namespace blockstore { namespace testfake { FakeBlock::FakeBlock(FakeBlockStore *store, const Key &key, shared_ptr data) - : Block(key), _store(store), _data(data) { + : Block(key), _store(store), _data(data), _dataChanged(false) { } FakeBlock::~FakeBlock() { @@ -29,6 +29,7 @@ const void *FakeBlock::data() const { void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) { assert(offset <= _data->size() && offset + size <= _data->size()); //Also check offset < _data->size() because of possible overflow in the addition std::memcpy((uint8_t*)_data->data()+offset, source, size); + _dataChanged = true; } size_t FakeBlock::size() const { @@ -36,7 +37,10 @@ size_t FakeBlock::size() const { } void FakeBlock::flush() { - _store->updateData(key(), *_data); + if(_dataChanged) { + _store->updateData(key(), *_data); + _dataChanged = false; + } } } diff --git a/implementations/testfake/FakeBlock.h b/implementations/testfake/FakeBlock.h index 8a1ca87d..04842a12 100644 --- a/implementations/testfake/FakeBlock.h +++ b/implementations/testfake/FakeBlock.h @@ -26,6 +26,7 @@ public: private: FakeBlockStore *_store; std::shared_ptr _data; + bool _dataChanged; DISALLOW_COPY_AND_ASSIGN(FakeBlock); };