diff --git a/CachingBaseStore.h b/CachingBaseStore.h new file mode 100644 index 00000000..3a7d1ed3 --- /dev/null +++ b/CachingBaseStore.h @@ -0,0 +1,18 @@ +#ifndef MESSMER_CACHINGSTORE_CACHINGBASESTORE_H_ +#define MESSMER_CACHINGSTORE_CACHINGBASESTORE_H_ + +#include + +namespace cachingstore { + +template +class CachingBaseStore { +public: + virtual ~CachingBaseStore() {} + virtual std::unique_ptr loadFromBaseStore(const Key &key) = 0; + virtual void removeFromBaseStore(std::unique_ptr block) = 0; +}; + +} + +#endif diff --git a/CachingStore.h b/CachingStore.h index 0fa01921..0e19159a 100644 --- a/CachingStore.h +++ b/CachingStore.h @@ -8,22 +8,33 @@ #include #include -//TODO Test CachingStore +#include "CachingBaseStore.h" + +//TODO Refactor +//TODO Test cases + +namespace cachingstore { template class CachingStore { public: - CachingStore(): _mutex(), _openResources(), _resourcesToRemove() {} + CachingStore(std::unique_ptr> 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::CreateRandomKey()) {} - void init(CachingStore *cachingStore, const Key &key) { - _cachingStore = cachingStore; - _key = key; - } + //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); } @@ -37,10 +48,6 @@ public: std::unique_ptr load(const Key &key); void remove(const Key &key, std::unique_ptr block); -protected: - virtual std::unique_ptr loadFromBaseStore(const Key &key) = 0; - virtual void removeFromBaseStore(std::unique_ptr resource) = 0; - private: class OpenResource { public: @@ -68,6 +75,8 @@ private: }; std::mutex _mutex; + std::unique_ptr> _baseStore; + std::map _openResources; std::map>> _resourcesToRemove; @@ -105,7 +114,7 @@ std::unique_ptr CachingStore::load(c std::lock_guard lock(_mutex); auto found = _openResources.find(key); if (found == _openResources.end()) { - auto resource = loadFromBaseStore(key); + auto resource = _baseStore->loadFromBaseStore(key); if (resource.get() == nullptr) { return nullptr; } @@ -124,7 +133,7 @@ void CachingStore::remove(const Key &key, std: //Wait for last resource user to release it auto resourceToRemove = insertResult.first->second.get_future().get(); - removeFromBaseStore(std::move(resourceToRemove)); + _baseStore->removeFromBaseStore(std::move(resourceToRemove)); } template @@ -142,5 +151,6 @@ void CachingStore::release(const Key &key) { } } +} #endif diff --git a/test/CachingBaseStoreTest.cpp b/test/CachingBaseStoreTest.cpp new file mode 100644 index 00000000..9e05e76b --- /dev/null +++ b/test/CachingBaseStoreTest.cpp @@ -0,0 +1,3 @@ +#include "../CachingBaseStore.h" + +// Test that CachingBaseStore.h can be included without errors