2015-04-16 13:59:52 +02:00
|
|
|
#include "BlockRef.h"
|
|
|
|
#include "ParallelAccessBlockStore.h"
|
|
|
|
#include "ParallelAccessBlockStoreAdapter.h"
|
|
|
|
#include <cassert>
|
2015-06-21 17:43:31 +02:00
|
|
|
#include <messmer/cpp-utils/pointer/cast.h>
|
2015-04-16 13:59:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::string;
|
|
|
|
using std::mutex;
|
|
|
|
using std::lock_guard;
|
|
|
|
using std::promise;
|
|
|
|
using cpputils::dynamic_pointer_move;
|
2015-06-26 13:45:03 +02:00
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::none;
|
2015-06-26 15:54:19 +02:00
|
|
|
using cpputils::unique_ref;
|
2015-04-16 13:59:52 +02:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace parallelaccess {
|
|
|
|
|
2015-06-26 15:54:19 +02:00
|
|
|
ParallelAccessBlockStore::ParallelAccessBlockStore(unique_ref<BlockStore> baseBlockStore)
|
2015-06-26 13:45:03 +02:00
|
|
|
: _baseBlockStore(std::move(baseBlockStore)), _parallelAccessStore(make_unique_ref<ParallelAccessBlockStoreAdapter>(_baseBlockStore.get())) {
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
Key ParallelAccessBlockStore::createKey() {
|
|
|
|
return _baseBlockStore->createKey();
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
unique_ptr<Block> ParallelAccessBlockStore::tryCreate(const Key &key, cpputils::Data data) {
|
2015-06-26 13:45:03 +02:00
|
|
|
//TODO Don't use nullcheck/to_unique_ptr but make blockstore use unique_ref
|
|
|
|
auto block = cpputils::nullcheck(_baseBlockStore->tryCreate(key, std::move(data)));
|
|
|
|
if (block == none) {
|
2015-04-18 14:47:12 +02:00
|
|
|
//TODO Test this code branch
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-26 13:45:03 +02:00
|
|
|
return cpputils::to_unique_ptr(_parallelAccessStore.add(key, std::move(*block)));
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<Block> ParallelAccessBlockStore::load(const Key &key) {
|
2015-06-26 13:45:03 +02:00
|
|
|
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));
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ParallelAccessBlockStore::remove(unique_ptr<Block> block) {
|
|
|
|
Key key = block->key();
|
2015-06-26 13:45:03 +02:00
|
|
|
//TODO Don't use nullcheck but make blockstore use unique_ref
|
2015-06-28 17:01:50 +02:00
|
|
|
auto block_ref = cpputils::nullcheck(std::move(block)).value();
|
|
|
|
return _parallelAccessStore.remove(key, dynamic_pointer_move<BlockRef>(block_ref).value());
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ParallelAccessBlockStore::numBlocks() const {
|
|
|
|
return _baseBlockStore->numBlocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|