2015-03-12 14:27:51 +01:00
|
|
|
#include "FakeBlock.h"
|
|
|
|
#include "FakeBlockStore.h"
|
2014-12-11 01:31:21 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::shared_ptr;
|
|
|
|
using std::istream;
|
|
|
|
using std::ostream;
|
|
|
|
using std::ifstream;
|
|
|
|
using std::ofstream;
|
|
|
|
using std::ios;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace testfake {
|
|
|
|
|
2015-01-24 22:08:41 +01:00
|
|
|
FakeBlock::FakeBlock(FakeBlockStore *store, const Key &key, shared_ptr<Data> data)
|
2015-03-05 22:16:57 +01:00
|
|
|
: Block(key), _store(store), _data(data), _dataChanged(false) {
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FakeBlock::~FakeBlock() {
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:47:02 +01:00
|
|
|
const void *FakeBlock::data() const {
|
2014-12-11 01:31:21 +01:00
|
|
|
return _data->data();
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:47:02 +01:00
|
|
|
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);
|
2015-03-05 22:16:57 +01:00
|
|
|
_dataChanged = true;
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t FakeBlock::size() const {
|
|
|
|
return _data->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FakeBlock::flush() {
|
2015-03-05 22:16:57 +01:00
|
|
|
if(_dataChanged) {
|
|
|
|
_store->updateData(key(), *_data);
|
|
|
|
_dataChanged = false;
|
|
|
|
}
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|