We don't need CachingStore::getKey()

This commit is contained in:
Sebastian Meßmer 2015-04-02 07:21:52 -04:00
parent c18c028d7f
commit f2129f6dc2
3 changed files with 16 additions and 21 deletions

View File

@ -18,7 +18,8 @@ CachingBlockStore::CachingBlockStore(unique_ptr<BlockStore> baseBlockStore)
unique_ptr<Block> CachingBlockStore::create(size_t size) {
auto block = _baseBlockStore->create(size);
return CachingStore::add(std::move(block));
Key key = block->key();
return CachingStore::add(key, std::move(block));
}
unique_ptr<Block> CachingBlockStore::load(const Key &key) {
@ -27,11 +28,8 @@ unique_ptr<Block> CachingBlockStore::load(const Key &key) {
void CachingBlockStore::remove(unique_ptr<Block> block) {
return CachingStore::remove(std::move(block));
}
const Key &CachingBlockStore::getKey(const Block &block) const {
return block.key();
Key key = block->key();
return CachingStore::remove(key, std::move(block));
}
unique_ptr<Block> CachingBlockStore::loadFromBaseStore(const Key &key) {

View File

@ -20,7 +20,6 @@ public:
uint64_t numBlocks() const override;
protected:
const Key &getKey(const Block &block) const override;
std::unique_ptr<Block> loadFromBaseStore(const Key &key) override;
void removeFromBaseStore(std::unique_ptr<Block> block) override;

View File

@ -31,13 +31,11 @@ public:
Key _key;
};
std::unique_ptr<Resource> add(std::unique_ptr<Resource> resource);
std::unique_ptr<Resource> add(const Key &key, std::unique_ptr<Resource> resource);
std::unique_ptr<Resource> load(const Key &key);
void remove(std::unique_ptr<Resource> block);
void remove(const Key &key, std::unique_ptr<Resource> block);
protected:
//TODO Template instead of virtual for getKey?
virtual const Key &getKey(const Resource &resource) const = 0;
virtual std::unique_ptr<Resource> loadFromBaseStore(const Key &key) = 0;
virtual void removeFromBaseStore(std::unique_ptr<Resource> resource) = 0;
@ -71,7 +69,7 @@ private:
std::map<Key, OpenResource> _openResources;
std::map<Key, std::promise<std::unique_ptr<Resource>>> _resourcesToRemove;
std::unique_ptr<Resource> _add(std::unique_ptr<Resource> resource);
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);
@ -81,22 +79,22 @@ private:
};
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::add(std::unique_ptr<Resource> resource) {
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(std::move(resource));
return _add(key, std::move(resource));
}
template<class Resource, class CachedResourceRef, class Key>
std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::_add(std::unique_ptr<Resource> resource) {
auto insertResult = _openResources.emplace(getKey(*resource), std::move(resource));
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(), getKey(*resource));
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, getKey(*resource));
resourceRef->init(this, key);
return std::move(resourceRef);
}
@ -109,15 +107,15 @@ std::unique_ptr<Resource> CachingStore<Resource, CachedResourceRef, Key>::load(c
if (resource.get() == nullptr) {
return nullptr;
}
return _add(std::move(resource));
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(std::unique_ptr<Resource> resource) {
auto insertResult = _resourcesToRemove.emplace(getKey(*resource), std::promise<std::unique_ptr<Resource>>());
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();