Use new CachingStore

This commit is contained in:
Sebastian Meßmer 2015-04-02 12:19:05 -04:00
parent f2129f6dc2
commit ff35d72e34
8 changed files with 67 additions and 207 deletions

View File

@ -4,6 +4,7 @@
cryptopp/cryptopp: 8 cryptopp/cryptopp: 8
google/gmock: 2 google/gmock: 2
google/gtest: 10 google/gtest: 10
messmer/cachingstore: -1
messmer/cmake: 3 messmer/cmake: 3
messmer/cpp-utils: 2 messmer/cpp-utils: 2
messmer/tempfile: 4 messmer/tempfile: 4

View File

@ -1,37 +1 @@
#include <messmer/blockstore/implementations/caching/CachedBlockRef.h> #include "CachedBlockRef.h"
#include <messmer/blockstore/implementations/caching/CachingBlockStore.h>
using std::shared_ptr;
using std::make_unique;
using std::function;
namespace blockstore {
namespace caching {
CachedBlockRef::CachedBlockRef(Block *baseBlock)
:Block(baseBlock->key()),
_baseBlock(baseBlock) {
}
CachedBlockRef::~CachedBlockRef() {
_baseBlock->flush();
}
const void *CachedBlockRef::data() const {
return _baseBlock->data();
}
void CachedBlockRef::write(const void *source, uint64_t offset, uint64_t size) {
return _baseBlock->write(source, offset, size);
}
size_t CachedBlockRef::size() const {
return _baseBlock->size();
}
void CachedBlockRef::flush() {
return _baseBlock->flush();
}
}
}

View File

@ -3,7 +3,7 @@
#define BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHEDBLOCKREF_H_ #define BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHEDBLOCKREF_H_
#include "../../interface/Block.h" #include "../../interface/Block.h"
#include "CachingStore.h" #include <messmer/cachingstore/CachingStore.h>
#include "messmer/cpp-utils/macros.h" #include "messmer/cpp-utils/macros.h"
#include <memory> #include <memory>
@ -12,17 +12,30 @@ namespace blockstore {
namespace caching { namespace caching {
class CachingBlockStore; class CachingBlockStore;
class CachedBlockRef: public Block, public CachingStore<Block, CachedBlockRef, Key>::CachedResource { class CachedBlockRef: public Block, public cachingstore::CachingStore<Block, CachedBlockRef, Key>::CachedResource {
public: public:
CachedBlockRef(Block *baseBlock); //TODO Unneccessarily storing Key twice here (in parent class and in _baseBlock).
virtual ~CachedBlockRef(); CachedBlockRef(Block *baseBlock): Block(baseBlock->key()), _baseBlock(baseBlock) {}
const void *data() const override; virtual ~CachedBlockRef() {
void write(const void *source, uint64_t offset, uint64_t size) override; _baseBlock->flush();
}
void flush() override; const void *data() const override {
return _baseBlock->data();
}
size_t size() const override; void write(const void *source, uint64_t offset, uint64_t size) override {
return _baseBlock->write(source, offset, size);
}
void flush() override {
return _baseBlock->flush();
}
size_t size() const override {
return _baseBlock->size();
}
private: private:
Block *_baseBlock; Block *_baseBlock;

View File

@ -2,6 +2,8 @@
#include <messmer/blockstore/implementations/caching/CachingBlockStore.h> #include <messmer/blockstore/implementations/caching/CachingBlockStore.h>
#include <cassert> #include <cassert>
#include "CachingBlockStoreAdapter.h"
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::string; using std::string;
@ -13,31 +15,23 @@ namespace blockstore {
namespace caching { namespace caching {
CachingBlockStore::CachingBlockStore(unique_ptr<BlockStore> baseBlockStore) CachingBlockStore::CachingBlockStore(unique_ptr<BlockStore> baseBlockStore)
: _baseBlockStore(std::move(baseBlockStore)) { : _baseBlockStore(std::move(baseBlockStore)), _cachingStore(make_unique<CachingBlockStoreAdapter>(_baseBlockStore.get())) {
} }
unique_ptr<Block> CachingBlockStore::create(size_t size) { unique_ptr<Block> CachingBlockStore::create(size_t size) {
auto block = _baseBlockStore->create(size); auto block = _baseBlockStore->create(size);
Key key = block->key(); Key key = block->key();
return CachingStore::add(key, std::move(block)); return _cachingStore.add(key, std::move(block));
} }
unique_ptr<Block> CachingBlockStore::load(const Key &key) { unique_ptr<Block> CachingBlockStore::load(const Key &key) {
return CachingStore::load(key); return _cachingStore.load(key);
} }
void CachingBlockStore::remove(unique_ptr<Block> block) { void CachingBlockStore::remove(unique_ptr<Block> block) {
Key key = block->key(); Key key = block->key();
return CachingStore::remove(key, std::move(block)); return _cachingStore.remove(key, std::move(block));
}
unique_ptr<Block> CachingBlockStore::loadFromBaseStore(const Key &key) {
return _baseBlockStore->load(key);
}
void CachingBlockStore::removeFromBaseStore(unique_ptr<Block> block) {
return _baseBlockStore->remove(std::move(block));
} }
uint64_t CachingBlockStore::numBlocks() const { uint64_t CachingBlockStore::numBlocks() const {

View File

@ -2,7 +2,7 @@
#ifndef BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGBLOCKSTORE_H_ #ifndef BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGBLOCKSTORE_H_
#define BLOCKSTORE_IMPLEMENTATIONS_CACHIN_CACHINGBLOCKSTORE_H_ #define BLOCKSTORE_IMPLEMENTATIONS_CACHIN_CACHINGBLOCKSTORE_H_
#include "CachingStore.h" #include <messmer/cachingstore/CachingStore.h>
#include "../../interface/BlockStore.h" #include "../../interface/BlockStore.h"
#include "CachedBlockRef.h" #include "CachedBlockRef.h"
@ -10,7 +10,7 @@
namespace blockstore { namespace blockstore {
namespace caching { namespace caching {
class CachingBlockStore: public BlockStore, private CachingStore<Block, CachedBlockRef, Key> { class CachingBlockStore: public BlockStore {
public: public:
CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore); CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
@ -19,12 +19,9 @@ public:
void remove(std::unique_ptr<Block> block) override; void remove(std::unique_ptr<Block> block) override;
uint64_t numBlocks() const override; uint64_t numBlocks() const override;
protected:
std::unique_ptr<Block> loadFromBaseStore(const Key &key) override;
void removeFromBaseStore(std::unique_ptr<Block> block) override;
private: private:
std::unique_ptr<BlockStore> _baseBlockStore; std::unique_ptr<BlockStore> _baseBlockStore;
cachingstore::CachingStore<Block, CachedBlockRef, Key> _cachingStore;
DISALLOW_COPY_AND_ASSIGN(CachingBlockStore); DISALLOW_COPY_AND_ASSIGN(CachingBlockStore);
}; };

View File

@ -0,0 +1 @@
#include "CachingBlockStoreAdapter.h"

View File

@ -0,0 +1,34 @@
#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGBLOCKSTOREADAPTER_H_
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGBLOCKSTOREADAPTER_H_
#include <messmer/cpp-utils/macros.h>
#include <messmer/cachingstore/CachingStore.h>
#include "../../interface/BlockStore.h"
namespace blockstore {
namespace caching {
class CachingBlockStoreAdapter: public cachingstore::CachingBaseStore<Block, Key> {
public:
CachingBlockStoreAdapter(BlockStore *baseBlockStore)
:_baseBlockStore(std::move(baseBlockStore)) {
}
std::unique_ptr<Block> loadFromBaseStore(const Key &key) override {
return _baseBlockStore->load(key);
}
void removeFromBaseStore(std::unique_ptr<Block> block) override {
return _baseBlockStore->remove(std::move(block));
}
private:
BlockStore *_baseBlockStore;
DISALLOW_COPY_AND_ASSIGN(CachingBlockStoreAdapter);
};
}
}
#endif

View File

@ -1,144 +0,0 @@
#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGSTORE_H_
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHINGSTORE_H_
#include <mutex>
#include <memory>
#include <map>
#include <future>
#include <cassert>
#include <messmer/cpp-utils/macros.h>
template<class Resource, class CachedResourceRef, class Key>
class CachingStore {
public:
CachingStore() {} //TODO Init member variables
//TODO Enforce CachedResourceRef inherits from CachedResource
class CachedResource {
public:
//TODO Better way to initialize
CachedResource(): _cachingStore(nullptr), _key(Key::CreateRandomKey()) {}
void init(CachingStore *cachingStore, const Key &key) {
_cachingStore = cachingStore;
_key = key;
}
virtual ~CachedResource() {
_cachingStore->release(_key);
}
private:
CachingStore *_cachingStore;
//TODO We're storing Key twice (here and in the base resource). Rather use getKey() on the base resource if possible somehow.
Key _key;
};
std::unique_ptr<Resource> add(const Key &key, std::unique_ptr<Resource> resource);
std::unique_ptr<Resource> load(const Key &key);
void remove(const Key &key, std::unique_ptr<Resource> block);
protected:
virtual std::unique_ptr<Resource> loadFromBaseStore(const Key &key) = 0;
virtual void removeFromBaseStore(std::unique_ptr<Resource> resource) = 0;
private:
class OpenResource {
public:
OpenResource(std::unique_ptr<Resource> resource): _resource(std::move(resource)), _refCount(0) {}
Resource *getReference() {
++_refCount;
return _resource.get();
}
void releaseReference() {
--_refCount;
}
bool refCountIsZero() const {
return 0 == _refCount;
}
std::unique_ptr<Resource> moveResourceOut() {
return std::move(_resource);
}
private:
std::unique_ptr<Resource> _resource;
uint32_t _refCount;
};
std::mutex _mutex;
std::map<Key, OpenResource> _openResources;
std::map<Key, std::promise<std::unique_ptr<Resource>>> _resourcesToRemove;
std::unique_ptr<Resource> _add(const Key &key, std::unique_ptr<Resource> resource);
std::unique_ptr<Resource> _createCachedResourceRef(Resource *resource, const Key &key);
void release(const Key &key);
friend class CachedResource;
DISALLOW_COPY_AND_ASSIGN(CachingStore);
};
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::add(const Key &key, std::unique_ptr<Resource> resource) {
std::lock_guard<std::mutex> lock(_mutex);
return _add(key, std::move(resource));
}
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::_add(const Key &key, std::unique_ptr<Resource> resource) {
auto insertResult = _openResources.emplace(key, std::move(resource));
assert(true == insertResult.second);
return _createCachedResourceRef(insertResult.first->second.getReference(), key);
}
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::_createCachedResourceRef(Resource *resource, const Key &key) {
auto resourceRef = std::make_unique<CachedResourceRef>(resource);
resourceRef->init(this, key);
return std::move(resourceRef);
}
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::load(const Key &key) {
std::lock_guard<std::mutex> lock(_mutex);
auto found = _openResources.find(key);
if (found == _openResources.end()) {
auto resource = loadFromBaseStore(key);
if (resource.get() == nullptr) {
return nullptr;
}
return _add(key, std::move(resource));
} else {
return _createCachedResourceRef(found->second.getReference(), key);
}
}
template<class Resource, class CachedResourceRef, class Key>
void CachingStore<Resource, CachedResourceRef, Key>::remove(const Key &key, std::unique_ptr<Resource> resource) {
auto insertResult = _resourcesToRemove.emplace(key, std::promise<std::unique_ptr<Resource>>());
assert(true == insertResult.second);
resource.reset();
//Wait for last resource user to release it
auto resourceToRemove = insertResult.first->second.get_future().get();
removeFromBaseStore(std::move(resourceToRemove));
}
template<class Resource, class CachedResourceRef, class Key>
void CachingStore<Resource, CachedResourceRef, Key>::release(const Key &key) {
std::lock_guard<std::mutex> lock(_mutex);
auto found = _openResources.find(key);
assert (found != _openResources.end());
found->second.releaseReference();
if (found->second.refCountIsZero()) {
auto foundToRemove = _resourcesToRemove.find(key);
if (foundToRemove != _resourcesToRemove.end()) {
foundToRemove->second.set_value(found->second.moveResourceOut());
}
_openResources.erase(found);
}
}
#endif