2015-02-17 00:23:33 +01:00
|
|
|
#include <messmer/blockstore/implementations/inmemory/InMemoryBlock.h>
|
|
|
|
#include <messmer/blockstore/implementations/inmemory/InMemoryBlockStore.h>
|
|
|
|
#include <memory>
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
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;
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace inmemory {
|
|
|
|
|
|
|
|
InMemoryBlockStore::InMemoryBlockStore()
|
|
|
|
: _blocks() {}
|
|
|
|
|
2014-12-09 20:36:32 +01:00
|
|
|
unique_ptr<Block> InMemoryBlockStore::create(const Key &key, size_t size) {
|
2015-01-24 22:08:41 +01:00
|
|
|
auto insert_result = _blocks.emplace(piecewise_construct, make_tuple(key.ToString()), make_tuple(key, size));
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
if (!insert_result.second) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:31:21 +01:00
|
|
|
//Return a pointer to the stored InMemoryBlock
|
2014-12-09 20:36:32 +01:00
|
|
|
return make_unique<InMemoryBlock>(insert_result.first->second);
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 20:36:32 +01:00
|
|
|
unique_ptr<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 {
|
2014-12-13 11:59:48 +01:00
|
|
|
return make_unique<InMemoryBlock>(_blocks.at(key.ToString()));
|
2014-12-09 17:19:59 +01:00
|
|
|
} catch (const std::out_of_range &e) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 00:29:21 +01:00
|
|
|
void InMemoryBlockStore::remove(const Key &key) {
|
|
|
|
int numRemoved = _blocks.erase(key.ToString());
|
|
|
|
assert(1==numRemoved);
|
|
|
|
}
|
|
|
|
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
}
|