Renamed to ParallelAccessStore
This commit is contained in:
parent
74381f2cdd
commit
d15a355464
@ -1,18 +0,0 @@
|
||||
#ifndef MESSMER_CACHINGSTORE_CACHINGBASESTORE_H_
|
||||
#define MESSMER_CACHINGSTORE_CACHINGBASESTORE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cachingstore {
|
||||
|
||||
template<class Resource, class Key>
|
||||
class CachingBaseStore {
|
||||
public:
|
||||
virtual ~CachingBaseStore() {}
|
||||
virtual std::unique_ptr<Resource> loadFromBaseStore(const Key &key) = 0;
|
||||
virtual void removeFromBaseStore(std::unique_ptr<Resource> block) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
158
CachingStore.h
158
CachingStore.h
@ -1,158 +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>
|
||||
|
||||
#include "CachingBaseStore.h"
|
||||
|
||||
//TODO Refactor
|
||||
//TODO Test cases
|
||||
|
||||
namespace cachingstore {
|
||||
|
||||
template<class Resource, class CachedResourceRef, class Key>
|
||||
class CachingStore {
|
||||
public:
|
||||
CachingStore(std::unique_ptr<CachingBaseStore<Resource, Key>> baseStore)
|
||||
: _mutex(),
|
||||
_baseStore(std::move(baseStore)),
|
||||
_openResources(),
|
||||
_resourcesToRemove() {
|
||||
}
|
||||
|
||||
//TODO Enforce CachedResourceRef inherits from CachedResource
|
||||
|
||||
class CachedResource {
|
||||
public:
|
||||
//TODO Better way to initialize
|
||||
CachedResource(): _cachingStore(nullptr), _key(Key::CreateRandom()) {}
|
||||
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<CachedResourceRef> add(const Key &key, std::unique_ptr<Resource> resource);
|
||||
std::unique_ptr<CachedResourceRef> load(const Key &key);
|
||||
void remove(const Key &key, std::unique_ptr<CachedResourceRef> block);
|
||||
|
||||
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::unique_ptr<CachingBaseStore<Resource, Key>> _baseStore;
|
||||
|
||||
std::map<Key, OpenResource> _openResources;
|
||||
std::map<Key, std::promise<std::unique_ptr<Resource>>> _resourcesToRemove;
|
||||
|
||||
std::unique_ptr<CachedResourceRef> _add(const Key &key, std::unique_ptr<Resource> resource);
|
||||
std::unique_ptr<CachedResourceRef> _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<CachedResourceRef> 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<CachedResourceRef> 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<CachedResourceRef> 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<CachedResourceRef> CachingStore<Resource, CachedResourceRef, Key>::load(const Key &key) {
|
||||
//TODO This lock doesn't allow loading different blocks in parallel. Can we do something with futures maybe?
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto found = _openResources.find(key);
|
||||
if (found == _openResources.end()) {
|
||||
auto resource = _baseStore->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<CachedResourceRef> 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();
|
||||
_resourcesToRemove.erase(key); //TODO Is this erase causing a race condition?
|
||||
|
||||
_baseStore->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
|
18
ParallelAccessBaseStore.h
Normal file
18
ParallelAccessBaseStore.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef MESSMER_PARALLELACCESSSTORE_PARALLELACCESSBASESTORE_H_
|
||||
#define MESSMER_PARALLELACCESSSTORE_PARALLELACCESSBASESTORE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace parallelaccessstore {
|
||||
|
||||
template<class Resource, class Key>
|
||||
class ParallelAccessBaseStore {
|
||||
public:
|
||||
virtual ~ParallelAccessBaseStore() {}
|
||||
virtual std::unique_ptr<Resource> loadFromBaseStore(const Key &key) = 0;
|
||||
virtual void removeFromBaseStore(std::unique_ptr<Resource> block) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
161
ParallelAccessStore.h
Normal file
161
ParallelAccessStore.h
Normal file
@ -0,0 +1,161 @@
|
||||
#ifndef MESSMER_PARALLELACCESSSTORE_IMPLEMENTATIONS_PARALLELACCESS_PARALLELACCESSSTORE_H_
|
||||
#define MESSMER_PARALLELACCESSSTORE_IMPLEMENTATIONS_PARALLELACCESS_PARALLELACCESSSTORE_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <future>
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
#include <messmer/cpp-utils/macros.h>
|
||||
#include "ParallelAccessBaseStore.h"
|
||||
|
||||
|
||||
//TODO Refactor
|
||||
//TODO Test cases
|
||||
|
||||
namespace parallelaccessstore {
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
class ParallelAccessStore {
|
||||
public:
|
||||
ParallelAccessStore(std::unique_ptr<ParallelAccessBaseStore<Resource, Key>> baseStore);
|
||||
|
||||
class ResourceRefBase {
|
||||
public:
|
||||
//TODO Better way to initialize
|
||||
ResourceRefBase(): _cachingStore(nullptr), _key(Key::CreateRandom()) {}
|
||||
void init(ParallelAccessStore *cachingStore, const Key &key) {
|
||||
_cachingStore = cachingStore;
|
||||
_key = key;
|
||||
}
|
||||
virtual ~ResourceRefBase() {
|
||||
_cachingStore->release(_key);
|
||||
}
|
||||
private:
|
||||
ParallelAccessStore *_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<ResourceRef> add(const Key &key, std::unique_ptr<Resource> resource);
|
||||
std::unique_ptr<ResourceRef> load(const Key &key);
|
||||
void remove(const Key &key, std::unique_ptr<ResourceRef> block);
|
||||
|
||||
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::unique_ptr<ParallelAccessBaseStore<Resource, Key>> _baseStore;
|
||||
|
||||
std::map<Key, OpenResource> _openResources;
|
||||
std::map<Key, std::promise<std::unique_ptr<Resource>>> _resourcesToRemove;
|
||||
|
||||
std::unique_ptr<ResourceRef> _add(const Key &key, std::unique_ptr<Resource> resource);
|
||||
std::unique_ptr<ResourceRef> _createResourceRef(Resource *resource, const Key &key);
|
||||
|
||||
void release(const Key &key);
|
||||
friend class CachedResource;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ParallelAccessStore);
|
||||
};
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
ParallelAccessStore<Resource, ResourceRef, Key>::ParallelAccessStore(std::unique_ptr<ParallelAccessBaseStore<Resource, Key>> baseStore)
|
||||
: _mutex(),
|
||||
_baseStore(std::move(baseStore)),
|
||||
_openResources(),
|
||||
_resourcesToRemove() {
|
||||
static_assert(std::is_base_of<ResourceRefBase, ResourceRef>::value, "ResourceRef must inherit from ResourceRefBase");
|
||||
}
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
std::unique_ptr<ResourceRef> ParallelAccessStore<Resource, ResourceRef, 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 ResourceRef, class Key>
|
||||
std::unique_ptr<ResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::_add(const Key &key, std::unique_ptr<Resource> resource) {
|
||||
auto insertResult = _openResources.emplace(key, std::move(resource));
|
||||
assert(true == insertResult.second);
|
||||
return _createResourceRef(insertResult.first->second.getReference(), key);
|
||||
}
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
std::unique_ptr<ResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::_createResourceRef(Resource *resource, const Key &key) {
|
||||
auto resourceRef = std::make_unique<ResourceRef>(resource);
|
||||
resourceRef->init(this, key);
|
||||
return std::move(resourceRef);
|
||||
}
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
std::unique_ptr<ResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::load(const Key &key) {
|
||||
//TODO This lock doesn't allow loading different blocks in parallel. Can we do something with futures maybe?
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto found = _openResources.find(key);
|
||||
if (found == _openResources.end()) {
|
||||
auto resource = _baseStore->loadFromBaseStore(key);
|
||||
if (resource.get() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return _add(key, std::move(resource));
|
||||
} else {
|
||||
return _createResourceRef(found->second.getReference(), key);
|
||||
}
|
||||
}
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
void ParallelAccessStore<Resource, ResourceRef, Key>::remove(const Key &key, std::unique_ptr<ResourceRef> 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();
|
||||
_resourcesToRemove.erase(key); //TODO Is this erase causing a race condition?
|
||||
|
||||
_baseStore->removeFromBaseStore(std::move(resourceToRemove));
|
||||
}
|
||||
|
||||
template<class Resource, class ResourceRef, class Key>
|
||||
void ParallelAccessStore<Resource, ResourceRef, 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
|
@ -6,7 +6,7 @@
|
||||
messmer/cpp-utils: 2
|
||||
|
||||
[parent]
|
||||
messmer/cachingstore: 0
|
||||
messmer/parallelaccessstore: 0
|
||||
[paths]
|
||||
# Local directories to look for headers (within block)
|
||||
# /
|
||||
|
@ -1,3 +1,3 @@
|
||||
#include "../CachingBaseStore.h"
|
||||
#include "../ParallelAccessBaseStore.h"
|
||||
|
||||
// Test that CachingBaseStore.h can be included without errors
|
||||
|
Loading…
x
Reference in New Issue
Block a user