Replace unique_ptr with unique_ref in ParallelAccessBlockStore

This commit is contained in:
Sebastian Messmer 2015-06-26 13:45:03 +02:00
parent c755717898
commit de37cddcce
2 changed files with 21 additions and 10 deletions

View File

@ -12,12 +12,14 @@ using std::mutex;
using std::lock_guard; using std::lock_guard;
using std::promise; using std::promise;
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
using cpputils::make_unique_ref;
using boost::none;
namespace blockstore { namespace blockstore {
namespace parallelaccess { namespace parallelaccess {
ParallelAccessBlockStore::ParallelAccessBlockStore(unique_ptr<BlockStore> baseBlockStore) ParallelAccessBlockStore::ParallelAccessBlockStore(unique_ptr<BlockStore> baseBlockStore)
: _baseBlockStore(std::move(baseBlockStore)), _parallelAccessStore(make_unique<ParallelAccessBlockStoreAdapter>(_baseBlockStore.get())) { : _baseBlockStore(std::move(baseBlockStore)), _parallelAccessStore(make_unique_ref<ParallelAccessBlockStoreAdapter>(_baseBlockStore.get())) {
} }
Key ParallelAccessBlockStore::createKey() { Key ParallelAccessBlockStore::createKey() {
@ -25,22 +27,29 @@ Key ParallelAccessBlockStore::createKey() {
} }
unique_ptr<Block> ParallelAccessBlockStore::tryCreate(const Key &key, cpputils::Data data) { unique_ptr<Block> ParallelAccessBlockStore::tryCreate(const Key &key, cpputils::Data data) {
auto block = _baseBlockStore->tryCreate(key, std::move(data)); //TODO Don't use nullcheck/to_unique_ptr but make blockstore use unique_ref
if (block.get() == nullptr) { auto block = cpputils::nullcheck(_baseBlockStore->tryCreate(key, std::move(data)));
if (block == none) {
//TODO Test this code branch //TODO Test this code branch
return nullptr; return nullptr;
} }
return _parallelAccessStore.add(key, std::move(block)); return cpputils::to_unique_ptr(_parallelAccessStore.add(key, std::move(*block)));
} }
unique_ptr<Block> ParallelAccessBlockStore::load(const Key &key) { unique_ptr<Block> ParallelAccessBlockStore::load(const Key &key) {
return _parallelAccessStore.load(key); auto block = _parallelAccessStore.load(key);
if (block == none) {
return nullptr;
}
//TODO Don't use to_unique_ptr but make blockstore use unique_ref
return cpputils::to_unique_ptr(std::move(*block));
} }
void ParallelAccessBlockStore::remove(unique_ptr<Block> block) { void ParallelAccessBlockStore::remove(unique_ptr<Block> block) {
Key key = block->key(); Key key = block->key();
return _parallelAccessStore.remove(key, dynamic_pointer_move<BlockRef>(block)); //TODO Don't use nullcheck but make blockstore use unique_ref
return _parallelAccessStore.remove(key, std::move(dynamic_pointer_move<BlockRef>(cpputils::nullcheck(std::move(block)).get()).get()));
} }
uint64_t ParallelAccessBlockStore::numBlocks() const { uint64_t ParallelAccessBlockStore::numBlocks() const {

View File

@ -14,12 +14,14 @@ public:
:_baseBlockStore(std::move(baseBlockStore)) { :_baseBlockStore(std::move(baseBlockStore)) {
} }
std::unique_ptr<Block> loadFromBaseStore(const Key &key) override { boost::optional<cpputils::unique_ref<Block>> loadFromBaseStore(const Key &key) override {
return _baseBlockStore->load(key); //TODO Don't use nullcheck but make blockstore use unique_ref
return cpputils::nullcheck(_baseBlockStore->load(key));
} }
void removeFromBaseStore(std::unique_ptr<Block> block) override { void removeFromBaseStore(cpputils::unique_ref<Block> block) override {
return _baseBlockStore->remove(std::move(block)); //TODO Don't use to_unique_ptr but make blockstore use unique_ref
return _baseBlockStore->remove(cpputils::to_unique_ptr(std::move(block)));
} }
private: private: