Factor out BlobStoreWithRandomKeys

This commit is contained in:
Sebastian Messmer 2014-12-06 22:28:57 +01:00
parent 2b112c7fdb
commit 9125e7a3ce
12 changed files with 114 additions and 57 deletions

View File

@ -1,2 +1,3 @@
add_subdirectory(interface)
add_subdirectory(utils)
add_subdirectory(implementations)

View File

@ -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)

View File

@ -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<InMemoryBlob>(blob));
}
string InMemoryBlobStore::_generateKey() {
lock_guard<mutex> 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<Blob> InMemoryBlobStore::load(const string &key) {

View File

@ -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<Blob> 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<std::string, InMemoryBlob> _blobs;
std::mutex _generate_key_mutex;
DISALLOW_COPY_AND_ASSIGN(InMemoryBlobStore);
};

View File

@ -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)

View File

@ -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<mutex> 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<Blob> OnDiskBlobStore::load(const string &key) {

View File

@ -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 <boost/filesystem.hpp>
@ -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<Blob> 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);
};

View File

@ -6,6 +6,7 @@
#include <memory>
#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> &&blob_): key(key_), blob(std::move(blob_)) {}
std::string key;
std::unique_ptr<Blob> blob;
};
virtual BlobWithKey create(size_t size) = 0;
virtual std::unique_ptr<Blob> 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;
};

View File

@ -0,0 +1 @@
add_library(blobstore_interface helpers/BlobStoreWithRandomKeys.cpp)

View File

@ -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<mutex> lock(_generate_key_mutex);
string key;
do {
key = _generateRandomKey();
} while (exists(key));
return key;
}
string BlobStoreWithRandomKeys::_generateRandomKey() {
return RandomKeyGenerator::singleton().create();
}

View File

@ -0,0 +1,33 @@
#pragma once
#ifndef FSPP_BLOBSTORE_BLOBSTOREWITHRANDOMKEYS_H_
#define FSPP_BLOBSTORE_BLOBSTOREWITHRANDOMKEYS_H_
#include <mutex>
#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<Blob> 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

View File

@ -0,0 +1,20 @@
#pragma once
#ifndef BLOBSTORE_INTERFACE_BLOBWITHKEY_H_
#define BLOBSTORE_INTERFACE_BLOBWITHKEY_H_
#include "blobstore/interface/Blob.h"
#include <memory>
namespace blobstore {
struct BlobWithKey {
BlobWithKey(const std::string &key_, std::unique_ptr<Blob> &&blob_): key(key_), blob(std::move(blob_)) {}
std::string key;
std::unique_ptr<Blob> blob;
};
}
#endif