2015-03-12 14:27:51 +01:00
|
|
|
#include "FakeBlock.h"
|
|
|
|
#include "FakeBlockStore.h"
|
2014-12-11 01:31:21 +01:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::make_shared;
|
|
|
|
using std::string;
|
|
|
|
using std::mutex;
|
|
|
|
using std::lock_guard;
|
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace testfake {
|
|
|
|
|
|
|
|
FakeBlockStore::FakeBlockStore()
|
|
|
|
: _blocks(), _used_dataregions_for_blocks() {}
|
|
|
|
|
|
|
|
unique_ptr<Block> FakeBlockStore::create(const Key &key, size_t size) {
|
2014-12-13 11:59:48 +01:00
|
|
|
auto insert_result = _blocks.emplace(key.ToString(), size);
|
2014-12-11 01:31:21 +01:00
|
|
|
insert_result.first->second.FillWithZeroes();
|
|
|
|
|
|
|
|
if (!insert_result.second) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return a copy of the stored data
|
2014-12-11 01:41:08 +01:00
|
|
|
return load(key);
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<Block> FakeBlockStore::load(const Key &key) {
|
|
|
|
//Return a copy of the stored data
|
2014-12-13 11:59:48 +01:00
|
|
|
string key_string = key.ToString();
|
2014-12-11 01:31:21 +01:00
|
|
|
try {
|
2015-01-24 22:08:41 +01:00
|
|
|
return makeFakeBlockFromData(key, _blocks.at(key_string));
|
2014-12-11 01:31:21 +01:00
|
|
|
} catch (const std::out_of_range &e) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 16:53:49 +01:00
|
|
|
void FakeBlockStore::remove(unique_ptr<Block> block) {
|
|
|
|
Key key = block->key();
|
|
|
|
block.reset();
|
2015-02-22 00:29:21 +01:00
|
|
|
int numRemoved = _blocks.erase(key.ToString());
|
|
|
|
assert(numRemoved == 1);
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:08:41 +01:00
|
|
|
unique_ptr<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Data &data) {
|
2014-12-11 01:41:08 +01:00
|
|
|
auto newdata = make_shared<Data>(data.copy());
|
|
|
|
_used_dataregions_for_blocks.push_back(newdata);
|
|
|
|
return make_unique<FakeBlock>(this, key, newdata);
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:08:41 +01:00
|
|
|
void FakeBlockStore::updateData(const Key &key, const Data &data) {
|
|
|
|
Data &stored_data = _blocks.at(key.ToString());
|
2014-12-11 01:31:21 +01:00
|
|
|
assert(data.size() == stored_data.size());
|
|
|
|
std::memcpy(stored_data.data(), data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:07:07 +01:00
|
|
|
uint64_t FakeBlockStore::numBlocks() const {
|
|
|
|
return _blocks.size();
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
}
|