Added SynchronizedBlockStore. In this first version, it only synchronizes the direct blockstore functions, but still allows opening the same block twice. This will be forbidden in future commits

This commit is contained in:
Sebastian Meßmer 2015-03-19 11:16:20 +01:00
parent 6d61b896af
commit f6669c86c1
5 changed files with 99 additions and 4 deletions

View File

@ -4,8 +4,6 @@
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::string; using std::string;
using std::mutex;
using std::lock_guard;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;

View File

@ -7,8 +7,6 @@
#include "messmer/cpp-utils/macros.h" #include "messmer/cpp-utils/macros.h"
#include <mutex>
namespace blockstore { namespace blockstore {
namespace ondisk { namespace ondisk {

View File

@ -0,0 +1,41 @@
#include "SynchronizedBlockStore.h"
using std::unique_ptr;
using std::make_unique;
using std::string;
using std::mutex;
using std::lock_guard;
namespace bf = boost::filesystem;
namespace blockstore {
namespace synchronized {
SynchronizedBlockStore::SynchronizedBlockStore(unique_ptr<BlockStore> baseBlockStore)
: _baseBlockStore(std::move(baseBlockStore)), _mutex() {}
unique_ptr<Block> SynchronizedBlockStore::create(size_t size) {
//TODO Does this need to be locked?
lock_guard<mutex> lock(_mutex);
return _baseBlockStore->create(size);
}
unique_ptr<Block> SynchronizedBlockStore::load(const Key &key) {
//TODO Only load each block once and lock until old block not used anymore
lock_guard<mutex> lock(_mutex);
return _baseBlockStore->load(key);
}
void SynchronizedBlockStore::remove(unique_ptr<Block> block) {
lock_guard<mutex> lock(_mutex);
return _baseBlockStore->remove(std::move(block));
}
uint64_t SynchronizedBlockStore::numBlocks() const {
//TODO Does this need to be locked?
lock_guard<mutex> lock(_mutex);
return _baseBlockStore->numBlocks();
}
}
}

View File

@ -0,0 +1,35 @@
#pragma once
#ifndef BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_
#define BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_
#include <boost/filesystem.hpp>
#include "../../interface/BlockStore.h"
#include "messmer/cpp-utils/macros.h"
#include <mutex>
#include <memory>
namespace blockstore {
namespace synchronized {
class SynchronizedBlockStore: public BlockStore {
public:
SynchronizedBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
std::unique_ptr<Block> create(size_t size) override;
std::unique_ptr<Block> load(const Key &key) override;
void remove(std::unique_ptr<Block> block) override;
uint64_t numBlocks() const override;
private:
std::unique_ptr<BlockStore> _baseBlockStore;
mutable std::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(SynchronizedBlockStore);
};
}
}
#endif

View File

@ -0,0 +1,23 @@
#include "../../../implementations/synchronized/SynchronizedBlockStore.h"
#include "../../../implementations/testfake/FakeBlockStore.h"
#include "../../testutils/BlockStoreTest.h"
#include "google/gtest/gtest.h"
using blockstore::BlockStore;
using blockstore::synchronized::SynchronizedBlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
class SynchronizedBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<SynchronizedBlockStore>(make_unique<FakeBlockStore>());
}
};
INSTANTIATE_TYPED_TEST_CASE_P(Synchronized, BlockStoreTest, SynchronizedBlockStoreTestFixture);
//TODO Add specific tests ensuring that the access to the underlying blockstore is synchronized