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 {
|
namespace ondisk {
|
||||||
|
|
||||||
OnDiskBlock::OnDiskBlock(const Key &key, const bf::path &filepath, size_t size)
|
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)
|
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() {
|
OnDiskBlock::~OnDiskBlock() {
|
||||||
_storeToDisk();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
const void *OnDiskBlock::data() const {
|
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) {
|
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
|
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);
|
std::memcpy((uint8_t*)_data.data()+offset, source, size);
|
||||||
|
_dataChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t OnDiskBlock::size() const {
|
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) {
|
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();
|
auto filepath = rootdir / key.ToString();
|
||||||
if (bf::exists(filepath)) {
|
if (bf::exists(filepath)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -87,7 +89,10 @@ void OnDiskBlock::_storeToDisk() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OnDiskBlock::flush() {
|
void OnDiskBlock::flush() {
|
||||||
|
if (_dataChanged) {
|
||||||
_storeToDisk();
|
_storeToDisk();
|
||||||
|
_dataChanged = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
const boost::filesystem::path _filepath;
|
const boost::filesystem::path _filepath;
|
||||||
Data _data;
|
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, size_t size);
|
||||||
OnDiskBlock(const Key &key, const boost::filesystem::path &filepath, Data &&data);
|
OnDiskBlock(const Key &key, const boost::filesystem::path &filepath, Data &&data);
|
||||||
|
@ -15,7 +15,7 @@ namespace blockstore {
|
|||||||
namespace testfake {
|
namespace testfake {
|
||||||
|
|
||||||
FakeBlock::FakeBlock(FakeBlockStore *store, const Key &key, shared_ptr<Data> data)
|
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() {
|
FakeBlock::~FakeBlock() {
|
||||||
@ -29,6 +29,7 @@ const void *FakeBlock::data() const {
|
|||||||
void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) {
|
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
|
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);
|
std::memcpy((uint8_t*)_data->data()+offset, source, size);
|
||||||
|
_dataChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FakeBlock::size() const {
|
size_t FakeBlock::size() const {
|
||||||
@ -36,7 +37,10 @@ size_t FakeBlock::size() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FakeBlock::flush() {
|
void FakeBlock::flush() {
|
||||||
|
if(_dataChanged) {
|
||||||
_store->updateData(key(), *_data);
|
_store->updateData(key(), *_data);
|
||||||
|
_dataChanged = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
FakeBlockStore *_store;
|
FakeBlockStore *_store;
|
||||||
std::shared_ptr<Data> _data;
|
std::shared_ptr<Data> _data;
|
||||||
|
bool _dataChanged;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(FakeBlock);
|
DISALLOW_COPY_AND_ASSIGN(FakeBlock);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user