Implemented an in-memory blobstore

This commit is contained in:
Sebastian Messmer 2014-12-06 16:14:22 +01:00
parent e0e9129099
commit a116a8330c
20 changed files with 171 additions and 16 deletions

View File

@ -1 +1,2 @@
add_subdirectory(ondisk)
add_subdirectory(inmemory)

View File

@ -0,0 +1,3 @@
add_library(blobstore_inmemory InMemoryBlob.cpp InMemoryBlobStore.cpp)
target_link_libraries(blobstore_inmemory blobstore_utils)

View File

@ -0,0 +1,45 @@
#include "InMemoryBlob.h"
#include "InMemoryBlobStore.h"
#include <cstring>
using std::unique_ptr;
using std::make_unique;
using std::make_shared;
using std::istream;
using std::ostream;
using std::ifstream;
using std::ofstream;
using std::ios;
namespace blobstore {
namespace inmemory {
InMemoryBlob::InMemoryBlob(size_t size)
: _data(make_shared<Data>(size)) {
}
InMemoryBlob::InMemoryBlob(const InMemoryBlob &rhs)
: _data(rhs._data) {
}
InMemoryBlob::~InMemoryBlob() {
}
void *InMemoryBlob::data() {
return _data->data();
}
const void *InMemoryBlob::data() const {
return _data->data();
}
size_t InMemoryBlob::size() const {
return _data->size();
}
void InMemoryBlob::flush() {
}
} /* namespace inmemory */
} /* namespace blobstore */

View File

@ -0,0 +1,32 @@
#pragma once
#ifndef BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOB_H_
#define BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOB_H_
#include "blobstore/interface/Blob.h"
#include "blobstore/utils/Data.h"
namespace blobstore {
namespace inmemory {
class InMemoryBlobStore;
class InMemoryBlob: public Blob {
public:
InMemoryBlob(size_t size);
InMemoryBlob(const InMemoryBlob &rhs);
virtual ~InMemoryBlob();
void *data() override;
const void *data() const override;
void flush() override;
size_t size() const override;
private:
std::shared_ptr<Data> _data;
};
} /* namespace inmemory */
} /* namespace blobstore */
#endif

View File

@ -0,0 +1,47 @@
#include "InMemoryBlobStore.h"
#include "InMemoryBlob.h"
#include "blobstore/utils/RandomKeyGenerator.h"
using std::unique_ptr;
using std::make_unique;
using std::string;
using std::mutex;
using std::lock_guard;
namespace blobstore {
namespace inmemory {
InMemoryBlobStore::InMemoryBlobStore()
: _blobs(), _generate_key_mutex() {}
BlobStore::BlobWithKey InMemoryBlobStore::create(size_t size) {
std::string key = _generateKey();
InMemoryBlob blob(size);
_blobs.insert(make_pair(key, blob));
//Return a copy of the stored InMemoryBlob
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();
}
unique_ptr<Blob> InMemoryBlobStore::load(const string &key) {
//Return a copy of the stored InMemoryBlob
return make_unique<InMemoryBlob>(_blobs.at(key));
}
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -0,0 +1,36 @@
#pragma once
#ifndef BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOBSTORE_H_
#define BLOBSTORE_IMPLEMENTATIONS_INMEMORY_INMEMORYBLOBSTORE_H_
#include "blobstore/interface/BlobStore.h"
#include "fspp/utils/macros.h"
#include <mutex>
#include <map>
namespace blobstore {
namespace inmemory {
class InMemoryBlob;
class InMemoryBlobStore: public BlobStore {
public:
InMemoryBlobStore();
BlobWithKey create(size_t size) override;
std::unique_ptr<Blob> load(const std::string &key) override;
private:
std::string _generateKey();
std::string _generateRandomKey();
std::map<std::string, InMemoryBlob> _blobs;
std::mutex _generate_key_mutex;
DISALLOW_COPY_AND_ASSIGN(InMemoryBlobStore);
};
} /* namespace inmemory */
} /* namespace blobstore */
#endif

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 cryptopp)
target_link_libraries(blobstore_ondisk blobstore_utils boost_filesystem boost_system)

View File

@ -12,7 +12,6 @@
namespace blobstore {
namespace ondisk {
class OnDiskBlob;
class OnDiskBlobStore: public BlobStore {
public:

View File

@ -1,2 +1,3 @@
add_library(blobstore_utils Data.cpp RandomKeyGenerator.cpp FileDoesntExistException.cpp)
target_link_libraries(blobstore_utils cryptopp)

View File

@ -15,7 +15,6 @@ using std::make_unique;
namespace bf = boost::filesystem;
namespace blobstore {
namespace ondisk {
Data::Data(size_t size)
: _size(size), _data(std::malloc(size)) {
@ -89,5 +88,4 @@ void Data::_readFromStream(istream &stream) {
stream.read((char*)_data, _size);
}
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -10,7 +10,6 @@
#include <memory>
namespace blobstore {
namespace ondisk {
class Data {
public:
@ -39,7 +38,6 @@ private:
DISALLOW_COPY_AND_ASSIGN(Data);
};
} /* namespace ondisk */
} /* namespace blobstore */
#endif

View File

@ -6,7 +6,6 @@ using std::runtime_error;
using std::string;
namespace blobstore {
namespace ondisk {
FileDoesntExistException::FileDoesntExistException(const bf::path &filepath)
: runtime_error(string("The file ")+filepath.c_str()+" doesn't exist") {
@ -15,5 +14,4 @@ FileDoesntExistException::FileDoesntExistException(const bf::path &filepath)
FileDoesntExistException::~FileDoesntExistException() {
}
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -7,7 +7,6 @@
#include <stdexcept>
namespace blobstore {
namespace ondisk {
class FileDoesntExistException: public std::runtime_error {
public:
@ -15,7 +14,6 @@ public:
virtual ~FileDoesntExistException();
};
} /* namespace ondisk */
} /* namespace blobstore */
#endif

View File

@ -13,7 +13,6 @@ using CryptoPP::HexEncoder;
using std::make_unique;
namespace blobstore {
namespace ondisk {
constexpr unsigned int RandomKeyGenerator::KEYLENGTH_ENTROPY;
constexpr unsigned int RandomKeyGenerator::KEYLENGTH;
@ -51,5 +50,4 @@ string encodeKeyToHex(const byte *data) {
}
}
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -10,7 +10,6 @@ class AutoSeededRandomPool;
}
namespace blobstore {
namespace ondisk {
// Creates random keys for use as block access handles.
// A key here is NOT a key for encryption, but a key as used in key->value mappings ("access handle for a block").
@ -33,7 +32,6 @@ private:
DISALLOW_COPY_AND_ASSIGN(RandomKeyGenerator);
};
} /* namespace ondisk */
} /* namespace blobstore */
#endif

View File

@ -11,6 +11,7 @@ using ::testing::Values;
using std::unique_ptr;
using namespace blobstore;
using namespace blobstore::ondisk;
namespace bf = boost::filesystem;

View File

@ -12,6 +12,7 @@ using ::testing::Values;
using std::unique_ptr;
using namespace blobstore;
using namespace blobstore::ondisk;
namespace bf = boost::filesystem;

View File

@ -17,6 +17,7 @@ using std::ofstream;
using std::unique_ptr;
using std::ios;
using namespace blobstore;
using namespace blobstore::ondisk;
namespace bf = boost::filesystem;

View File

@ -15,7 +15,7 @@ using ::testing::Values;
using std::ifstream;
using std::ofstream;
using namespace blobstore::ondisk;
using namespace blobstore;
class DataTest: public Test {
public:

View File

@ -6,7 +6,7 @@ using ::testing::Test;
using std::string;
using namespace blobstore::ondisk;
using namespace blobstore;
class RandomKeyGeneratorTest: public Test {};