libcryfs/implementations/inmemory/InMemoryBlockStore.cpp

57 lines
1.4 KiB
C++
Raw Normal View History

#include "InMemoryBlock.h"
#include "InMemoryBlockStore.h"
2015-02-17 00:23:33 +01:00
#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;
using cpputils::Data;
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() {}
optional<unique_ref<Block>> InMemoryBlockStore::tryCreate(const Key &key, Data data) {
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) {
return none;
2014-12-09 17:19:59 +01:00
}
//Return a pointer to the stored InMemoryBlock
return optional<unique_ref<Block>>(make_unique_ref<InMemoryBlock>(insert_result.first->second));
2014-12-09 17:19:59 +01:00
}
unique_ptr<Block> InMemoryBlockStore::load(const Key &key) {
//Return a pointer to the stored InMemoryBlock
2014-12-09 17:19:59 +01:00
try {
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;
}
}
void InMemoryBlockStore::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(1==numRemoved);
}
2015-02-23 21:07:07 +01:00
uint64_t InMemoryBlockStore::numBlocks() const {
return _blocks.size();
}
2014-12-09 17:19:59 +01:00
}
}