CatapultServer  v0.5.0.1 (Elephant)
NamespaceCacheMixins.h
Go to the documentation of this file.
1 
21 #pragma once
25 #include <numeric>
26 
27 namespace catapult { namespace cache {
28 
30  struct NamespaceSizes {
32  size_t Active;
33 
35  size_t Deep;
36  };
37 
39  template<typename TSet>
41  public:
43  explicit NamespaceDeepSizeMixin(const NamespaceSizes& sizes) : m_sizes(sizes)
44  {}
45 
46  public:
48  size_t activeSize() const {
49  return m_sizes.Active;
50  }
51 
53  size_t deepSize() const {
54  return m_sizes.Deep;
55  }
56 
57  protected:
59  void incrementActiveSize(size_t delta = 1) {
60  m_sizes.Active += delta;
61  }
62 
64  void decrementActiveSize(size_t delta = 1) {
65  m_sizes.Active -= delta;
66  }
67 
69  void incrementDeepSize(size_t delta = 1) {
70  m_sizes.Deep += delta;
71  }
72 
74  void decrementDeepSize(size_t delta = 1) {
75  m_sizes.Deep -= delta;
76  }
77 
78  private:
80  };
81 
84  template<typename TPrimarySet, typename TFlatMap>
86  public:
89  public:
92  {}
93 
96  : m_isValid(false)
97  , m_id(id)
98  {}
99 
101  const_iterator(typename TFlatMap::FindConstIterator&& namespaceIter, typename TPrimarySet::FindConstIterator&& rootIter)
102  : m_isValid(true)
103  , m_namespaceIter(std::move(namespaceIter))
104  , m_rootIter(std::move(rootIter))
105  , m_entry(*m_namespaceIter.get(), m_rootIter.get()->back())
106  {}
107 
108  public:
111  const state::NamespaceEntry& get() const {
112  if (!m_isValid)
113  CATAPULT_THROW_INVALID_ARGUMENT_1("unknown namespace", m_id);
114 
115  return *tryGet();
116  }
117 
119  const state::NamespaceEntry* tryGet() const {
120  return m_isValid ? &m_entry : nullptr;
121  }
122 
125  return m_rootIter.get();
126  }
127 
128  private:
129  bool m_isValid;
131  typename TFlatMap::FindConstIterator m_namespaceIter;
132  typename TPrimarySet::FindConstIterator m_rootIter;
134  };
135 
136  public:
138  explicit NamespaceLookupMixin(const TPrimarySet& set, const TFlatMap& flatMap)
139  : m_set(set)
140  , m_flatMap(flatMap)
141  {}
142 
143  public:
145  bool isActive(NamespaceId id, Height height) const {
146  auto namespaceIter = m_flatMap.find(id);
147  if (!namespaceIter.get())
148  return false;
149 
150  auto historyIter = m_set.find(namespaceIter.get()->rootId());
151  if (!historyIter.get())
152  CATAPULT_THROW_RUNTIME_ERROR_1("no history for root namespace found", namespaceIter.get()->rootId());
153 
154  return historyIter.get()->isActiveAndUnlocked(height);
155  }
156 
159  auto namespaceIter = m_flatMap.find(id);
160  if (!namespaceIter.get())
161  return const_iterator(id);
162 
163  auto rootIter = m_set.find(namespaceIter.get()->rootId());
164  return const_iterator(std::move(namespaceIter), std::move(rootIter));
165  }
166 
167  private:
168  const TPrimarySet& m_set;
169  const TFlatMap& m_flatMap;
170  };
171 }}
RootNamespaceHistory.h
catapult::cache::NamespaceDeepSizeMixin::incrementDeepSize
void incrementDeepSize(size_t delta=1)
Increments the deep size by delta.
Definition: NamespaceCacheMixins.h:69
catapult::cache::NamespaceLookupMixin::const_iterator::m_id
NamespaceId m_id
Definition: NamespaceCacheMixins.h:130
catapult::cache::NamespaceLookupMixin::m_flatMap
const TFlatMap & m_flatMap
Definition: NamespaceCacheMixins.h:169
catapult::cache::NamespaceSizes::Active
size_t Active
Number of unique active namespaces.
Definition: NamespaceCacheMixins.h:32
catapult::cache::NamespaceLookupMixin::const_iterator
An iterator that is returned by namespace cache find functions.
Definition: NamespaceCacheMixins.h:88
catapult::cache::NamespaceLookupMixin::const_iterator::const_iterator
const_iterator(NamespaceId id)
Creates an uninitialized iterator around id.
Definition: NamespaceCacheMixins.h:95
catapult::cache::NamespaceLookupMixin::isActive
bool isActive(NamespaceId id, Height height) const
Returns true if the value specified by identifier id is active at height.
Definition: NamespaceCacheMixins.h:145
catapult::cache::NamespaceDeepSizeMixin::incrementActiveSize
void incrementActiveSize(size_t delta=1)
Increments the active size by delta.
Definition: NamespaceCacheMixins.h:59
catapult::cache::NamespaceDeepSizeMixin
A mixin for calculating the deep size of namespaces.
Definition: NamespaceCacheMixins.h:40
catapult::state::NamespaceEntry
A pair composed of a namespace and its root.
Definition: NamespaceEntry.h:28
catapult::state::RootNamespaceHistory
A root namespace history.
Definition: RootNamespaceHistory.h:31
catapult::cache::NamespaceLookupMixin
Definition: NamespaceCacheMixins.h:85
catapult::cache::NamespaceLookupMixin::const_iterator::m_entry
state::NamespaceEntry m_entry
Definition: NamespaceCacheMixins.h:133
catapult::utils::BaseValue< uint64_t, NamespaceId_tag >
catapult::cache::NamespaceLookupMixin::const_iterator::get
const state::NamespaceEntry & get() const
Definition: NamespaceCacheMixins.h:111
catapult::cache::NamespaceLookupMixin::const_iterator::m_namespaceIter
TFlatMap::FindConstIterator m_namespaceIter
Definition: NamespaceCacheMixins.h:131
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
catapult::cache::NamespaceLookupMixin::const_iterator::const_iterator
const_iterator()
Creates an uninitialized iterator.
Definition: NamespaceCacheMixins.h:91
catapult::cache::NamespaceLookupMixin::const_iterator::tryGet
const state::NamespaceEntry * tryGet() const
Tries to get a const value.
Definition: NamespaceCacheMixins.h:119
catapult::cache::NamespaceLookupMixin::NamespaceLookupMixin
NamespaceLookupMixin(const TPrimarySet &set, const TFlatMap &flatMap)
Creates a mixin around (history by id) set and flatMap.
Definition: NamespaceCacheMixins.h:138
catapult::cache::NamespaceDeepSizeMixin::deepSize
size_t deepSize() const
Gets the total number of namespaces in the cache (including versions).
Definition: NamespaceCacheMixins.h:53
catapult::cache::NamespaceLookupMixin::find
const_iterator find(NamespaceId id) const
Finds the cache value identified by id.
Definition: NamespaceCacheMixins.h:158
catapult::cache::NamespaceDeepSizeMixin::NamespaceDeepSizeMixin
NamespaceDeepSizeMixin(const NamespaceSizes &sizes)
Creates a mixin around sizes.
Definition: NamespaceCacheMixins.h:43
catapult::cache::NamespaceDeepSizeMixin::activeSize
size_t activeSize() const
Gets the number of unique active namespaces in the cache.
Definition: NamespaceCacheMixins.h:48
CATAPULT_THROW_INVALID_ARGUMENT_1
#define CATAPULT_THROW_INVALID_ARGUMENT_1(MESSAGE, PARAM1)
Macro used to throw a catapult invalid argument with a single parameter.
Definition: exceptions.h:183
catapult::cache::NamespaceLookupMixin::const_iterator::m_isValid
bool m_isValid
Definition: NamespaceCacheMixins.h:129
catapult::cache::NamespaceDeepSizeMixin::m_sizes
NamespaceSizes m_sizes
Definition: NamespaceCacheMixins.h:79
catapult::cache::NamespaceDeepSizeMixin::decrementDeepSize
void decrementDeepSize(size_t delta=1)
Decrements the deep size by delta.
Definition: NamespaceCacheMixins.h:74
catapult::cache::NamespaceLookupMixin::const_iterator::tryGetUnadapted
const state::RootNamespaceHistory * tryGetUnadapted() const
Tries to get a const (unadapted) value.
Definition: NamespaceCacheMixins.h:124
catapult::cache::NamespaceSizes::Deep
size_t Deep
Total number of namespaces (including all versions).
Definition: NamespaceCacheMixins.h:35
catapult::cache::NamespaceLookupMixin::const_iterator::const_iterator
const_iterator(typename TFlatMap::FindConstIterator &&namespaceIter, typename TPrimarySet::FindConstIterator &&rootIter)
Creates an iterator around a namespace iterator (namespaceIter) and a root iterator (rootIter).
Definition: NamespaceCacheMixins.h:101
catapult::cache::NamespaceDeepSizeMixin::decrementActiveSize
void decrementActiveSize(size_t delta=1)
Decrements the active size by delta.
Definition: NamespaceCacheMixins.h:64
catapult
Definition: AddressExtractionExtension.cpp:28
NamespaceEntry.h
catapult::cache::NamespaceSizes
Extended namespace sizes.
Definition: NamespaceCacheMixins.h:30
BaseSetDelta.h
catapult::cache::NamespaceLookupMixin::m_set
const TPrimarySet & m_set
Definition: NamespaceCacheMixins.h:168
catapult::cache::NamespaceLookupMixin::const_iterator::m_rootIter
TPrimarySet::FindConstIterator m_rootIter
Definition: NamespaceCacheMixins.h:132