libcryfs/implementations/inmemory/InMemoryBlockStore.cpp
Sebastian Meßmer 417a701636 - BlockStore::create() gets the data of the new block as a parameter
- Fixed numBlocks() in OnDiskBlockStore, FakeBlockStore, CachingBlockStore, ...
- CachingBlockStore caches created blocks and doesn't directly create them in the underlying blockstore
2015-04-18 14:47:12 +02:00

52 lines
1.2 KiB
C++

#include "InMemoryBlock.h"
#include "InMemoryBlockStore.h"
#include <memory>
using std::unique_ptr;
using std::make_unique;
using std::string;
using std::mutex;
using std::lock_guard;
using std::piecewise_construct;
using std::make_tuple;
namespace blockstore {
namespace inmemory {
InMemoryBlockStore::InMemoryBlockStore()
: _blocks() {}
unique_ptr<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)));
if (!insert_result.second) {
return nullptr;
}
//Return a pointer to the stored InMemoryBlock
return make_unique<InMemoryBlock>(insert_result.first->second);
}
unique_ptr<Block> InMemoryBlockStore::load(const Key &key) {
//Return a pointer to the stored InMemoryBlock
try {
return make_unique<InMemoryBlock>(_blocks.at(key.ToString()));
} catch (const std::out_of_range &e) {
return nullptr;
}
}
void InMemoryBlockStore::remove(unique_ptr<Block> block) {
Key key = block->key();
block.reset();
int numRemoved = _blocks.erase(key.ToString());
assert(1==numRemoved);
}
uint64_t InMemoryBlockStore::numBlocks() const {
return _blocks.size();
}
}
}