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-07-22 13:42:07 +02:00
|
|
|
#include <messmer/cpp-utils/assert/assert.h>
|
2015-04-16 13:59:52 +02:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
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-07-20 18:57:48 +02:00
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
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-07-20 18:57:48 +02:00
|
|
|
optional<unique_ref<Block>> ParallelAccessBlockStore::tryCreate(const Key &key, cpputils::Data data) {
|
2015-10-14 14:40:53 +02:00
|
|
|
ASSERT(!_parallelAccessStore.isOpened(key), ("Key "+key.ToString()+"already exists").c_str());
|
2015-07-20 18:57:48 +02:00
|
|
|
auto block = _baseBlockStore->tryCreate(key, std::move(data));
|
2015-06-26 13:45:03 +02:00
|
|
|
if (block == none) {
|
2015-04-18 14:47:12 +02:00
|
|
|
//TODO Test this code branch
|
2015-07-20 18:57:48 +02:00
|
|
|
return none;
|
2015-04-18 14:47:12 +02:00
|
|
|
}
|
2015-07-20 18:57:48 +02:00
|
|
|
return unique_ref<Block>(_parallelAccessStore.add(key, std::move(*block)));
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
optional<unique_ref<Block>> ParallelAccessBlockStore::load(const Key &key) {
|
2015-06-26 13:45:03 +02:00
|
|
|
auto block = _parallelAccessStore.load(key);
|
|
|
|
if (block == none) {
|
2015-07-21 14:50:52 +02:00
|
|
|
return none;
|
2015-06-26 13:45:03 +02:00
|
|
|
}
|
2015-07-21 14:50:52 +02:00
|
|
|
return unique_ref<Block>(std::move(*block));
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
void ParallelAccessBlockStore::remove(unique_ref<Block> block) {
|
2015-04-16 13:59:52 +02:00
|
|
|
Key key = block->key();
|
2015-07-21 14:50:52 +02:00
|
|
|
auto block_ref = dynamic_pointer_move<BlockRef>(block);
|
2015-07-22 13:42:07 +02:00
|
|
|
ASSERT(block_ref != none, "Block is not a BlockRef");
|
2015-07-21 14:50:52 +02:00
|
|
|
return _parallelAccessStore.remove(key, std::move(*block_ref));
|
2015-04-16 13:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ParallelAccessBlockStore::numBlocks() const {
|
|
|
|
return _baseBlockStore->numBlocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|