2015-03-12 14:27:51 +01:00
|
|
|
#include "FakeBlock.h"
|
|
|
|
#include "FakeBlockStore.h"
|
2015-07-22 13:42:07 +02:00
|
|
|
#include <messmer/cpp-utils/assert/assert.h>
|
2014-12-11 01:31:21 +01:00
|
|
|
|
|
|
|
using std::make_shared;
|
|
|
|
using std::string;
|
|
|
|
using std::mutex;
|
|
|
|
using std::lock_guard;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::Data;
|
2015-07-20 18:57:48 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2014-12-11 01:31:21 +01:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace testfake {
|
|
|
|
|
|
|
|
FakeBlockStore::FakeBlockStore()
|
2015-10-17 21:10:26 +02:00
|
|
|
: _blocks(), _used_dataregions_for_blocks(), _mutex() {}
|
2014-12-11 01:31:21 +01:00
|
|
|
|
2015-07-20 18:57:48 +02:00
|
|
|
optional<unique_ref<Block>> FakeBlockStore::tryCreate(const Key &key, Data data) {
|
2015-10-15 04:49:31 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2015-04-18 14:47:12 +02:00
|
|
|
auto insert_result = _blocks.emplace(key.ToString(), std::move(data));
|
|
|
|
|
|
|
|
if (!insert_result.second) {
|
2015-07-20 18:57:48 +02:00
|
|
|
return none;
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
2015-04-18 14:47:12 +02:00
|
|
|
|
|
|
|
//Return a copy of the stored data
|
2015-10-15 04:49:31 +02:00
|
|
|
return _load(key);
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
optional<unique_ref<Block>> FakeBlockStore::load(const Key &key) {
|
2015-10-15 04:49:31 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
return _load(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<unique_ref<Block>> FakeBlockStore::_load(const Key &key) {
|
2014-12-11 01:31:21 +01:00
|
|
|
//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-04-15 14:51:41 +02:00
|
|
|
return makeFakeBlockFromData(key, _blocks.at(key_string), false);
|
2014-12-11 01:31:21 +01:00
|
|
|
} catch (const std::out_of_range &e) {
|
2015-07-21 14:50:52 +02:00
|
|
|
return none;
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
void FakeBlockStore::remove(unique_ref<Block> block) {
|
2015-02-22 16:53:49 +01:00
|
|
|
Key key = block->key();
|
2015-07-21 18:19:34 +02:00
|
|
|
cpputils::destruct(std::move(block));
|
2015-10-15 04:49:31 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2015-02-22 00:29:21 +01:00
|
|
|
int numRemoved = _blocks.erase(key.ToString());
|
2015-07-22 13:42:07 +02:00
|
|
|
ASSERT(numRemoved == 1, "Block not found");
|
2015-02-22 00:29:21 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
unique_ref<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Data &data, bool dirty) {
|
2014-12-11 01:41:08 +01:00
|
|
|
auto newdata = make_shared<Data>(data.copy());
|
|
|
|
_used_dataregions_for_blocks.push_back(newdata);
|
2015-07-21 14:50:52 +02:00
|
|
|
return make_unique_ref<FakeBlock>(this, key, newdata, dirty);
|
2014-12-11 01:41:08 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 22:08:41 +01:00
|
|
|
void FakeBlockStore::updateData(const Key &key, const Data &data) {
|
2015-10-15 04:49:31 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2015-04-15 14:51:41 +02:00
|
|
|
auto found = _blocks.find(key.ToString());
|
|
|
|
if (found == _blocks.end()) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto insertResult = _blocks.emplace(key.ToString(), data.copy());
|
2015-07-22 13:42:07 +02:00
|
|
|
ASSERT(true == insertResult.second, "Inserting didn't work");
|
2015-04-15 14:51:41 +02:00
|
|
|
found = insertResult.first;
|
|
|
|
}
|
|
|
|
Data &stored_data = found->second;
|
2015-07-22 13:42:07 +02:00
|
|
|
ASSERT(data.size() == stored_data.size(), "Wrong data size in block");
|
2014-12-11 01:31:21 +01:00
|
|
|
std::memcpy(stored_data.data(), data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:07:07 +01:00
|
|
|
uint64_t FakeBlockStore::numBlocks() const {
|
2015-10-15 04:49:31 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2015-02-23 21:07:07 +01:00
|
|
|
return _blocks.size();
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
|
|
|
}
|