From 9125e7a3cedfdadf989970978ab0e39baafc672c Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 6 Dec 2014 22:28:57 +0100 Subject: [PATCH] Factor out BlobStoreWithRandomKeys --- src/blobstore/CMakeLists.txt | 1 + .../implementations/inmemory/CMakeLists.txt | 2 +- .../inmemory/InMemoryBlobStore.cpp | 20 +++-------- .../inmemory/InMemoryBlobStore.h | 13 ++++---- .../implementations/ondisk/CMakeLists.txt | 2 +- .../ondisk/OnDiskBlobStore.cpp | 23 ++++--------- .../implementations/ondisk/OnDiskBlobStore.h | 15 ++++----- src/blobstore/interface/BlobStore.h | 9 ++--- src/blobstore/interface/CMakeLists.txt | 1 + .../helpers/BlobStoreWithRandomKeys.cpp | 32 ++++++++++++++++++ .../helpers/BlobStoreWithRandomKeys.h | 33 +++++++++++++++++++ src/blobstore/utils/BlobWithKey.h | 20 +++++++++++ 12 files changed, 114 insertions(+), 57 deletions(-) create mode 100644 src/blobstore/interface/CMakeLists.txt create mode 100644 src/blobstore/interface/helpers/BlobStoreWithRandomKeys.cpp create mode 100644 src/blobstore/interface/helpers/BlobStoreWithRandomKeys.h create mode 100644 src/blobstore/utils/BlobWithKey.h diff --git a/src/blobstore/CMakeLists.txt b/src/blobstore/CMakeLists.txt index dbb22041..d4f05ddd 100644 --- a/src/blobstore/CMakeLists.txt +++ b/src/blobstore/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory(interface) add_subdirectory(utils) add_subdirectory(implementations) diff --git a/src/blobstore/implementations/inmemory/CMakeLists.txt b/src/blobstore/implementations/inmemory/CMakeLists.txt index 061874bf..b3b202de 100644 --- a/src/blobstore/implementations/inmemory/CMakeLists.txt +++ b/src/blobstore/implementations/inmemory/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(blobstore_inmemory InMemoryBlob.cpp InMemoryBlobStore.cpp) -target_link_libraries(blobstore_inmemory blobstore_utils) +target_link_libraries(blobstore_inmemory blobstore_interface blobstore_utils) diff --git a/src/blobstore/implementations/inmemory/InMemoryBlobStore.cpp b/src/blobstore/implementations/inmemory/InMemoryBlobStore.cpp index f962f80b..736d727d 100644 --- a/src/blobstore/implementations/inmemory/InMemoryBlobStore.cpp +++ b/src/blobstore/implementations/inmemory/InMemoryBlobStore.cpp @@ -12,10 +12,9 @@ namespace blobstore { namespace inmemory { InMemoryBlobStore::InMemoryBlobStore() - : _blobs(), _generate_key_mutex() {} + : _blobs() {} -BlobStore::BlobWithKey InMemoryBlobStore::create(size_t size) { - std::string key = _generateKey(); +BlobWithKey InMemoryBlobStore::create(const std::string &key, size_t size) { InMemoryBlob blob(size); _blobs.insert(make_pair(key, blob)); @@ -23,19 +22,8 @@ BlobStore::BlobWithKey InMemoryBlobStore::create(size_t size) { return BlobWithKey(key, make_unique(blob)); } -string InMemoryBlobStore::_generateKey() { - lock_guard lock(_generate_key_mutex); - - string key; - do { - key = _generateRandomKey(); - } while (_blobs.count(key) > 0); - - return key; -} - -string InMemoryBlobStore::_generateRandomKey() { - return RandomKeyGenerator::singleton().create(); +bool InMemoryBlobStore::exists(const std::string &key) { + return _blobs.count(key) > 0; } unique_ptr InMemoryBlobStore::load(const string &key) { diff --git a/src/blobstore/implementations/inmemory/InMemoryBlobStore.h b/src/blobstore/implementations/inmemory/InMemoryBlobStore.h index 803a82b0..e446de04 100644 --- a/src/blobstore/implementations/inmemory/InMemoryBlobStore.h +++ b/src/blobstore/implementations/inmemory/InMemoryBlobStore.h @@ -2,7 +2,7 @@ #ifndef BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOBSTORE_H_ #define BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOBSTORE_H_ -#include "blobstore/interface/BlobStore.h" +#include "blobstore/interface/helpers/BlobStoreWithRandomKeys.h" #include "fspp/utils/macros.h" @@ -13,19 +13,18 @@ namespace blobstore { namespace inmemory { class InMemoryBlob; -class InMemoryBlobStore: public BlobStore { +class InMemoryBlobStore: public BlobStoreWithRandomKeys { public: InMemoryBlobStore(); - BlobWithKey create(size_t size) override; + bool exists(const std::string &key) override; std::unique_ptr load(const std::string &key) override; -private: - std::string _generateKey(); - std::string _generateRandomKey(); +protected: + BlobWithKey create(const std::string &key, size_t size) override; +private: std::map _blobs; - std::mutex _generate_key_mutex; DISALLOW_COPY_AND_ASSIGN(InMemoryBlobStore); }; diff --git a/src/blobstore/implementations/ondisk/CMakeLists.txt b/src/blobstore/implementations/ondisk/CMakeLists.txt index 4267ad54..94c108ef 100644 --- a/src/blobstore/implementations/ondisk/CMakeLists.txt +++ b/src/blobstore/implementations/ondisk/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(blobstore_ondisk OnDiskBlob.cpp OnDiskBlobStore.cpp FileAlreadyExistsException.cpp) -target_link_libraries(blobstore_ondisk blobstore_utils boost_filesystem boost_system) +target_link_libraries(blobstore_ondisk blobstore_interface blobstore_utils boost_filesystem boost_system) diff --git a/src/blobstore/implementations/ondisk/OnDiskBlobStore.cpp b/src/blobstore/implementations/ondisk/OnDiskBlobStore.cpp index fc67a755..5766330b 100644 --- a/src/blobstore/implementations/ondisk/OnDiskBlobStore.cpp +++ b/src/blobstore/implementations/ondisk/OnDiskBlobStore.cpp @@ -14,29 +14,18 @@ namespace blobstore { namespace ondisk { OnDiskBlobStore::OnDiskBlobStore(const boost::filesystem::path &rootdir) - : _rootdir(rootdir), _generate_key_mutex() {} + : _rootdir(rootdir) {} -BlobStore::BlobWithKey OnDiskBlobStore::create(size_t size) { - std::string key = _generateKey(); +BlobWithKey OnDiskBlobStore::create(const std::string &key, size_t size) { auto file_path = _rootdir / key; auto blob = OnDiskBlob::CreateOnDisk(file_path, size); - return BlobStore::BlobWithKey(key, std::move(blob)); + return BlobWithKey(key, std::move(blob)); } -string OnDiskBlobStore::_generateKey() { - lock_guard lock(_generate_key_mutex); - - string key; - do { - key = _generateRandomKey(); - } while (bf::exists(_rootdir / key)); - - return key; -} - -string OnDiskBlobStore::_generateRandomKey() { - return RandomKeyGenerator::singleton().create(); +bool OnDiskBlobStore::exists(const std::string &key) { + auto file_path = _rootdir / key; + return bf::exists(file_path); } unique_ptr OnDiskBlobStore::load(const string &key) { diff --git a/src/blobstore/implementations/ondisk/OnDiskBlobStore.h b/src/blobstore/implementations/ondisk/OnDiskBlobStore.h index f8ed027c..0db04f12 100644 --- a/src/blobstore/implementations/ondisk/OnDiskBlobStore.h +++ b/src/blobstore/implementations/ondisk/OnDiskBlobStore.h @@ -2,7 +2,7 @@ #ifndef BLOBSTORE_IMPLEMENTATIONS_ONDISK_ONDISKBLOBSTORE_H_ #define BLOBSTORE_IMPLEMENTATIONS_ONDISK_ONDISKBLOBSTORE_H_ -#include "blobstore/interface/BlobStore.h" +#include "blobstore/interface/helpers/BlobStoreWithRandomKeys.h" #include @@ -13,19 +13,18 @@ namespace blobstore { namespace ondisk { -class OnDiskBlobStore: public BlobStore { +class OnDiskBlobStore: public BlobStoreWithRandomKeys { public: OnDiskBlobStore(const boost::filesystem::path &rootdir); - BlobWithKey create(size_t size) override; + bool exists(const std::string &key) override; std::unique_ptr load(const std::string &key) override; -private: - std::string _generateKey(); - std::string _generateRandomKey(); - const boost::filesystem::path _rootdir; +protected: + BlobWithKey create(const std::string &key, size_t size) override; - std::mutex _generate_key_mutex; +private: + const boost::filesystem::path _rootdir; DISALLOW_COPY_AND_ASSIGN(OnDiskBlobStore); }; diff --git a/src/blobstore/interface/BlobStore.h b/src/blobstore/interface/BlobStore.h index c5af9d61..182607c0 100644 --- a/src/blobstore/interface/BlobStore.h +++ b/src/blobstore/interface/BlobStore.h @@ -6,6 +6,7 @@ #include #include "Blob.h" +#include "blobstore/utils/BlobWithKey.h" namespace blobstore { @@ -15,15 +16,9 @@ class BlobStore { public: virtual ~BlobStore() {} - struct BlobWithKey { - BlobWithKey(const std::string &key_, std::unique_ptr &&blob_): key(key_), blob(std::move(blob_)) {} - - std::string key; - std::unique_ptr blob; - }; - virtual BlobWithKey create(size_t size) = 0; virtual std::unique_ptr load(const std::string &key) = 0; + virtual bool exists(const std::string &key) = 0; //TODO Needed for performance? Or is deleting loaded blobs enough? //virtual void remove(const std::string &key) = 0; }; diff --git a/src/blobstore/interface/CMakeLists.txt b/src/blobstore/interface/CMakeLists.txt new file mode 100644 index 00000000..85539c23 --- /dev/null +++ b/src/blobstore/interface/CMakeLists.txt @@ -0,0 +1 @@ +add_library(blobstore_interface helpers/BlobStoreWithRandomKeys.cpp) diff --git a/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.cpp b/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.cpp new file mode 100644 index 00000000..8200ce1c --- /dev/null +++ b/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.cpp @@ -0,0 +1,32 @@ +#include "BlobStoreWithRandomKeys.h" + +#include "blobstore/utils/RandomKeyGenerator.h" + +using namespace blobstore; + +using std::string; +using std::lock_guard; +using std::mutex; + +BlobStoreWithRandomKeys::BlobStoreWithRandomKeys() + :_generate_key_mutex() { +} + +BlobWithKey BlobStoreWithRandomKeys::create(size_t size) { + return create(_generateKey(), size); +} + +string BlobStoreWithRandomKeys::_generateKey() { + lock_guard lock(_generate_key_mutex); + + string key; + do { + key = _generateRandomKey(); + } while (exists(key)); + + return key; +} + +string BlobStoreWithRandomKeys::_generateRandomKey() { + return RandomKeyGenerator::singleton().create(); +} diff --git a/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.h b/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.h new file mode 100644 index 00000000..5746ba47 --- /dev/null +++ b/src/blobstore/interface/helpers/BlobStoreWithRandomKeys.h @@ -0,0 +1,33 @@ +#pragma once +#ifndef FSPP_BLOBSTORE_BLOBSTOREWITHRANDOMKEYS_H_ +#define FSPP_BLOBSTORE_BLOBSTOREWITHRANDOMKEYS_H_ + +#include + +#include "blobstore/interface/Blob.h" +#include "blobstore/interface/BlobStore.h" + +namespace blobstore { + +class BlobStoreWithRandomKeys: public BlobStore { +public: + BlobStoreWithRandomKeys(); + virtual ~BlobStoreWithRandomKeys() {} + + BlobWithKey create(size_t size) override; + + virtual std::unique_ptr load(const std::string &key) = 0; + +protected: + virtual BlobWithKey create(const std::string &key, size_t size) = 0; + +private: + std::string _generateKey(); + std::string _generateRandomKey(); + + std::mutex _generate_key_mutex; +}; + +} + +#endif diff --git a/src/blobstore/utils/BlobWithKey.h b/src/blobstore/utils/BlobWithKey.h new file mode 100644 index 00000000..3f4f7c38 --- /dev/null +++ b/src/blobstore/utils/BlobWithKey.h @@ -0,0 +1,20 @@ +#pragma once +#ifndef BLOBSTORE_INTERFACE_BLOBWITHKEY_H_ +#define BLOBSTORE_INTERFACE_BLOBWITHKEY_H_ + +#include "blobstore/interface/Blob.h" + +#include + +namespace blobstore { + +struct BlobWithKey { + BlobWithKey(const std::string &key_, std::unique_ptr &&blob_): key(key_), blob(std::move(blob_)) {} + + std::string key; + std::unique_ptr blob; +}; + +} + +#endif