CatapultServer  v0.5.0.1 (Elephant)
ReadOnlyArtifactCache.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "ReadOnlySimpleCache.h"
23 #include "catapult/types.h"
24 
25 namespace catapult { namespace cache {
26 
28  template<typename TCache, typename TCacheDelta, typename TKey, typename TValue>
29  class ReadOnlyArtifactCache : public ReadOnlySimpleCache<TCache, TCacheDelta, TKey> {
30  public:
32  template<typename TCacheIterator, typename TCacheDeltaIterator>
34  public:
37  {}
38 
40  explicit ReadOnlyFindIterator(TCacheIterator&& cacheIter)
41  : m_hasCacheIter(true)
42  , m_cacheIter(std::move(cacheIter))
43  {}
44 
46  explicit ReadOnlyFindIterator(TCacheDeltaIterator&& cacheDeltaIter)
47  : m_hasCacheIter(false)
48  , m_cacheDeltaIter(std::move(cacheDeltaIter))
49  {}
50 
51  public:
53  const TValue& get() const {
54  return m_hasCacheIter ? m_cacheIter.get() : m_cacheDeltaIter.get();
55  }
56 
58  const TValue* tryGet() const {
59  return m_hasCacheIter ? m_cacheIter.tryGet() : m_cacheDeltaIter.tryGet();
60  }
61 
62  private:
64  TCacheIterator m_cacheIter;
65  TCacheDeltaIterator m_cacheDeltaIter;
66  };
67 
68  public:
70  explicit ReadOnlyArtifactCache(const TCache& cache)
71  : ReadOnlySimpleCache<TCache, TCacheDelta, TKey>(cache)
72  , m_pCache(&cache)
73  , m_pCacheDelta(nullptr)
74  {}
75 
77  explicit ReadOnlyArtifactCache(const TCacheDelta& cache)
78  : ReadOnlySimpleCache<TCache, TCacheDelta, TKey>(cache)
79  , m_pCache(nullptr)
80  , m_pCacheDelta(&cache)
81  {}
82 
83  public:
85  auto find(TKey id) const {
86  // note: having alias within function instead of at class scope allows forward declaration of caches
87  using FindIterator = ReadOnlyFindIterator<decltype(m_pCache->find(id)), decltype(m_pCacheDelta->find(id))>;
88  return m_pCache ? FindIterator(m_pCache->find(id)) : FindIterator(m_pCacheDelta->find(id));
89  }
90 
92  bool isActive(TKey id, Height height) const {
93  return m_pCache ? m_pCache->isActive(id, height) : m_pCacheDelta->isActive(id, height);
94  }
95 
96  private:
97  const TCache* m_pCache;
98  const TCacheDelta* m_pCacheDelta;
99  };
100 }}
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::ReadOnlyFindIterator
ReadOnlyFindIterator()
Creates an uninitialized iterator.
Definition: ReadOnlyArtifactCache.h:36
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::m_cacheDeltaIter
TCacheDeltaIterator m_cacheDeltaIter
Definition: ReadOnlyArtifactCache.h:65
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::ReadOnlyFindIterator
ReadOnlyFindIterator(TCacheIterator &&cacheIter)
Creates a find iterator around cacheIter.
Definition: ReadOnlyArtifactCache.h:40
catapult::cache::ReadOnlyArtifactCache::ReadOnlyArtifactCache
ReadOnlyArtifactCache(const TCache &cache)
Creates a read-only overlay on top of cache.
Definition: ReadOnlyArtifactCache.h:70
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator
Find iterator returned by ReadOnlyArtifactCache::find.
Definition: ReadOnlyArtifactCache.h:33
catapult::cache::ReadOnlyArtifactCache::find
auto find(TKey id) const
Finds the cache value identified by key.
Definition: ReadOnlyArtifactCache.h:85
catapult::cache::ReadOnlyArtifactCache::m_pCacheDelta
const TCacheDelta * m_pCacheDelta
Definition: ReadOnlyArtifactCache.h:98
catapult::utils::BaseValue< uint64_t, Height_tag >
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::m_cacheIter
TCacheIterator m_cacheIter
Definition: ReadOnlyArtifactCache.h:64
catapult::cache::ReadOnlySimpleCache
A read-only overlay on top of a cache that provides support for contains.
Definition: HashCacheTypes.h:37
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::get
const TValue & get() const
Gets a const value.
Definition: ReadOnlyArtifactCache.h:53
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::tryGet
const TValue * tryGet() const
Tries to get a const value.
Definition: ReadOnlyArtifactCache.h:58
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::m_hasCacheIter
bool m_hasCacheIter
Definition: ReadOnlyArtifactCache.h:63
catapult::cache::ReadOnlyArtifactCache::ReadOnlyArtifactCache
ReadOnlyArtifactCache(const TCacheDelta &cache)
Creates a read-only overlay on top of cache.
Definition: ReadOnlyArtifactCache.h:77
types.h
ReadOnlySimpleCache.h
catapult::cache::ReadOnlyArtifactCache::ReadOnlyFindIterator::ReadOnlyFindIterator
ReadOnlyFindIterator(TCacheDeltaIterator &&cacheDeltaIter)
Creates a find iterator around cacheDeltaIter.
Definition: ReadOnlyArtifactCache.h:46
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::ReadOnlyArtifactCache::isActive
bool isActive(TKey id, Height height) const
Gets a value indicating whether or not an artifact with id is active at height.
Definition: ReadOnlyArtifactCache.h:92
catapult::cache::ReadOnlyArtifactCache::m_pCache
const TCache * m_pCache
Definition: ReadOnlyArtifactCache.h:97