BlockStore only writes changed data back if it actually was changed
This commit is contained in:
parent
d42bda0ca6
commit
2cb0f4e5de
@ -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> OnDiskBlock::LoadFromDisk(const bf::path &rootdir, const
|
||||
}
|
||||
|
||||
unique_ptr<OnDiskBlock> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -15,7 +15,7 @@ namespace blockstore {
|
||||
namespace testfake {
|
||||
|
||||
FakeBlock::FakeBlock(FakeBlockStore *store, const Key &key, shared_ptr<Data> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ public:
|
||||
private:
|
||||
FakeBlockStore *_store;
|
||||
std::shared_ptr<Data> _data;
|
||||
bool _dataChanged;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FakeBlock);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user