diff --git a/implementations/ondisk/OnDiskBlockStore.cpp b/implementations/ondisk/OnDiskBlockStore.cpp index 6dbbdbd2..11459bee 100644 --- a/implementations/ondisk/OnDiskBlockStore.cpp +++ b/implementations/ondisk/OnDiskBlockStore.cpp @@ -4,8 +4,6 @@ using std::unique_ptr; using std::make_unique; using std::string; -using std::mutex; -using std::lock_guard; namespace bf = boost::filesystem; diff --git a/implementations/ondisk/OnDiskBlockStore.h b/implementations/ondisk/OnDiskBlockStore.h index 0b774ffa..4836e64f 100644 --- a/implementations/ondisk/OnDiskBlockStore.h +++ b/implementations/ondisk/OnDiskBlockStore.h @@ -7,8 +7,6 @@ #include "messmer/cpp-utils/macros.h" -#include - namespace blockstore { namespace ondisk { diff --git a/implementations/synchronized/SynchronizedBlockStore.cpp b/implementations/synchronized/SynchronizedBlockStore.cpp new file mode 100644 index 00000000..6ee1cb02 --- /dev/null +++ b/implementations/synchronized/SynchronizedBlockStore.cpp @@ -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 baseBlockStore) + : _baseBlockStore(std::move(baseBlockStore)), _mutex() {} + +unique_ptr SynchronizedBlockStore::create(size_t size) { + //TODO Does this need to be locked? + lock_guard lock(_mutex); + return _baseBlockStore->create(size); +} + +unique_ptr SynchronizedBlockStore::load(const Key &key) { + //TODO Only load each block once and lock until old block not used anymore + lock_guard lock(_mutex); + return _baseBlockStore->load(key); +} + +void SynchronizedBlockStore::remove(unique_ptr block) { + lock_guard lock(_mutex); + return _baseBlockStore->remove(std::move(block)); +} + +uint64_t SynchronizedBlockStore::numBlocks() const { + //TODO Does this need to be locked? + lock_guard lock(_mutex); + return _baseBlockStore->numBlocks(); +} + +} +} diff --git a/implementations/synchronized/SynchronizedBlockStore.h b/implementations/synchronized/SynchronizedBlockStore.h new file mode 100644 index 00000000..456d65ce --- /dev/null +++ b/implementations/synchronized/SynchronizedBlockStore.h @@ -0,0 +1,35 @@ +#pragma once +#ifndef BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_ +#define BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_ + +#include +#include "../../interface/BlockStore.h" + +#include "messmer/cpp-utils/macros.h" + +#include +#include + +namespace blockstore { +namespace synchronized { + +class SynchronizedBlockStore: public BlockStore { +public: + SynchronizedBlockStore(std::unique_ptr baseBlockStore); + + std::unique_ptr create(size_t size) override; + std::unique_ptr load(const Key &key) override; + void remove(std::unique_ptr block) override; + uint64_t numBlocks() const override; + +private: + std::unique_ptr _baseBlockStore; + mutable std::mutex _mutex; + + DISALLOW_COPY_AND_ASSIGN(SynchronizedBlockStore); +}; + +} +} + +#endif diff --git a/test/implementations/synchronized/SynchronizedBlockStoreTest.cpp b/test/implementations/synchronized/SynchronizedBlockStoreTest.cpp new file mode 100644 index 00000000..c6334cc7 --- /dev/null +++ b/test/implementations/synchronized/SynchronizedBlockStoreTest.cpp @@ -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 createBlockStore() override { + return make_unique(make_unique()); + } +}; + +INSTANTIATE_TYPED_TEST_CASE_P(Synchronized, BlockStoreTest, SynchronizedBlockStoreTestFixture); + +//TODO Add specific tests ensuring that the access to the underlying blockstore is synchronized