2015-03-12 14:27:51 +01:00
|
|
|
#include "InMemoryBlock.h"
|
|
|
|
#include "InMemoryBlockStore.h"
|
2015-02-17 00:23:33 +01:00
|
|
|
#include <memory>
|
2015-07-22 13:42:07 +02:00
|
|
|
#include <messmer/cpp-utils/assert/assert.h>
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using std::make_unique;
|
|
|
|
using std::string;
|
|
|
|
using std::mutex;
|
|
|
|
using std::lock_guard;
|
2015-01-24 22:08:41 +01:00
|
|
|
using std::piecewise_construct;
|
|
|
|
using std::make_tuple;
|
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-09 17:19:59 +01:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace inmemory {
|
|
|
|
|
|
|
|
InMemoryBlockStore::InMemoryBlockStore()
|
|
|
|
: _blocks() {}
|
|
|
|
|
2015-07-20 18:57:48 +02:00
|
|
|
optional<unique_ref<Block>> InMemoryBlockStore::tryCreate(const Key &key, Data data) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto insert_result = _blocks.emplace(piecewise_construct, make_tuple(key.ToString()), make_tuple(key, std::move(data)));
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
if (!insert_result.second) {
|
2015-07-20 18:57:48 +02:00
|
|
|
return none;
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 01:31:21 +01:00
|
|
|
//Return a pointer to the stored InMemoryBlock
|
2015-07-20 18:57:48 +02:00
|
|
|
return optional<unique_ref<Block>>(make_unique_ref<InMemoryBlock>(insert_result.first->second));
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
optional<unique_ref<Block>> InMemoryBlockStore::load(const Key &key) {
|
2014-12-11 01:31:21 +01:00
|
|
|
//Return a pointer to the stored InMemoryBlock
|
2014-12-09 17:19:59 +01:00
|
|
|
try {
|
2015-07-21 14:50:52 +02:00
|
|
|
return optional<unique_ref<Block>>(make_unique_ref<InMemoryBlock>(_blocks.at(key.ToString())));
|
2014-12-09 17:19:59 +01:00
|
|
|
} catch (const std::out_of_range &e) {
|
2015-07-21 14:50:52 +02:00
|
|
|
return none;
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
void InMemoryBlockStore::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-02-22 00:29:21 +01:00
|
|
|
int numRemoved = _blocks.erase(key.ToString());
|
2015-07-22 13:42:07 +02:00
|
|
|
ASSERT(1==numRemoved, "Didn't find block to remove");
|
2015-02-22 00:29:21 +01:00
|
|
|
}
|
|
|
|
|
2015-02-23 21:07:07 +01:00
|
|
|
uint64_t InMemoryBlockStore::numBlocks() const {
|
|
|
|
return _blocks.size();
|
|
|
|
}
|
|
|
|
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
}
|