CatapultServer  v0.5.0.1 (Elephant)
CacheMixins.h
Go to the documentation of this file.
1 
21 #pragma once
26 #include "catapult/utils/Casting.h"
29 #include "catapult/functions.h"
30 #include "catapult/types.h"
31 
32 namespace catapult { namespace cache {
33 
35  template<typename TSet>
36  class SizeMixin {
37  public:
39  explicit SizeMixin(const TSet& set) : m_set(set)
40  {}
41 
42  public:
44  size_t size() const {
45  return m_set.size();
46  }
47 
48  private:
49  const TSet& m_set;
50  };
51 
53  template<typename TSet, typename TCacheDescriptor>
54  class ContainsMixin {
55  private:
56  using KeyType = typename TCacheDescriptor::KeyType;
57 
58  public:
60  explicit ContainsMixin(const TSet& set) : m_set(set)
61  {}
62 
63  public:
65  bool contains(const KeyType& key) const {
66  return m_set.contains(key);
67  }
68 
69  private:
70  const TSet& m_set;
71  };
72 
74  template<typename TSet>
76  public:
78  struct IterableView {
79  public:
81  explicit IterableView(const TSet& set) : m_set(set)
82  {}
83 
84  public:
86  auto begin() const {
87  return MakeIterableView(m_set).begin();
88  }
89 
91  auto end() const {
92  return MakeIterableView(m_set).end();
93  }
94 
95  private:
96  const TSet& m_set;
97  };
98 
99  public:
101  explicit IterationMixin(const TSet& set) : m_set(set)
102  {}
103 
104  public:
107  auto tryMakeIterableView() const {
108  // use argument dependent lookup to resolve IsBaseSetIterable
109  return IsBaseSetIterable(m_set) ? std::make_unique<IterableView>(m_set) : nullptr;
110  }
111 
112  private:
113  const TSet& m_set;
114  };
115 
116  namespace detail {
117  template<typename TCacheDescriptor, typename T>
118  [[noreturn]]
119  void ThrowInvalidKeyError(const char* keyState, const T& key) {
120  std::ostringstream out;
121  out << "value with key '" << key << "' is " << keyState << " in cache (" << TCacheDescriptor::Name << ")";
122  CATAPULT_THROW_INVALID_ARGUMENT(out.str().c_str());
123  }
124 
125  template<typename TCacheDescriptor, size_t N>
126  [[noreturn]]
127  void ThrowInvalidKeyError(const char* keyState, const std::array<uint8_t, N>& key) {
128  ThrowInvalidKeyError<TCacheDescriptor>(keyState, utils::HexFormat(key));
129  }
130 
131  template<typename TValue>
132  struct NoOpAdapter {
133  using AdaptedValueType = TValue;
134 
135  static TValue& Adapt(TValue& value) {
136  return value;
137  }
138  };
139 
141  template<typename TCacheDescriptor, typename TValueAdapter, typename TBaseSetIterator, typename TValue>
143  private:
144  using KeyType = typename TCacheDescriptor::KeyType;
145 
146  public:
148  CacheFindIterator() = default;
149 
151  CacheFindIterator(TBaseSetIterator&& iter, const KeyType& key)
152  : m_iter(std::move(iter))
153  , m_key(key)
154  {}
155 
156  public:
159  TValue& get() const {
160  auto* pValue = tryGet();
161  if (!pValue)
162  detail::ThrowInvalidKeyError<TCacheDescriptor>("not", m_key);
163 
164  return *pValue;
165  }
166 
168  TValue* tryGet() const {
169  auto pValue = m_iter.get(); // can be raw or shared_ptr
170  return pValue ? &TValueAdapter::Adapt(*pValue) : nullptr;
171  }
172 
174  const auto* tryGetUnadapted() const {
175  auto pValue = m_iter.get(); // can be raw or shared_ptr
176  return &*pValue;
177  }
178 
179  private:
180  TBaseSetIterator m_iter;
182  };
183  }
184 
186  template<
187  typename TSet,
188  typename TCacheDescriptor,
191  private:
192  using KeyType = typename TCacheDescriptor::KeyType;
193  using ValueType = typename TValueAdapter::AdaptedValueType;
194  using SetIteratorType = typename TSet::FindConstIterator;
195 
196  public:
199 
200  public:
202  explicit ConstAccessorMixin(const TSet& set) : m_set(set)
203  {}
204 
205  public:
207  const_iterator find(const KeyType& key) const {
208  auto setIter = m_set.find(key);
209  return const_iterator(std::move(setIter), key);
210  }
211 
212  private:
213  const TSet& m_set;
214  };
215 
218  template<
219  typename TSet,
220  typename TCacheDescriptor,
223  private:
224  using KeyType = typename TCacheDescriptor::KeyType;
225  using ValueType = typename TValueAdapter::AdaptedValueType;
226  using SetIteratorType = typename TSet::FindIterator;
227 
228  public:
231 
232  public:
234  explicit MutableAccessorMixin(TSet& set) : m_set(set)
235  {}
236 
237  public:
239  iterator find(const KeyType& key) {
240  auto setIter = m_set.find(key);
241  return iterator(std::move(setIter), key);
242  }
243 
244  private:
245  TSet& m_set;
246  };
247 
249  template<typename TSet, typename TCacheDescriptor>
251  private:
252  using KeyType = typename TCacheDescriptor::KeyType;
253 
254  public:
256  explicit ActivePredicateMixin(const TSet& set) : m_set(set)
257  {}
258 
259  public:
261  bool isActive(const KeyType& key, Height height) const {
262  auto iter = m_set.find(key);
263  const auto* pValue = iter.get();
264  return pValue && pValue->isActive(height);
265  }
266 
267  private:
268  const TSet& m_set;
269  };
270 
272  template<typename TSet, typename TCacheDescriptor>
274  private:
275  using KeyType = typename TCacheDescriptor::KeyType;
276  using ValueType = typename TCacheDescriptor::ValueType;
277 
278  public:
280  explicit BasicInsertRemoveMixin(TSet& set) : m_set(set)
281  {}
282 
283  public:
285  void insert(const ValueType& value) {
286  auto result = m_set.insert(value);
288  return;
289 
290  CATAPULT_LOG(error) << "insert failed with " << utils::to_underlying_type(result);
291  detail::ThrowInvalidKeyError<TCacheDescriptor>("already", TCacheDescriptor::GetKeyFromValue(value));
292  }
293 
295  void remove(const KeyType& key) {
296  auto result = m_set.remove(key);
298  return;
299 
300  CATAPULT_LOG(error) << "remove failed with " << utils::to_underlying_type(result);
301  detail::ThrowInvalidKeyError<TCacheDescriptor>("not", key);
302  }
303 
304  private:
305  TSet& m_set;
306  };
307 
309  template<typename TSet, typename THeightGroupedSet>
311  public:
313  HeightBasedTouchMixin(TSet& set, THeightGroupedSet& heightGroupedSet)
314  : m_set(set)
315  , m_heightGroupedSet(heightGroupedSet)
316  {}
317 
318  public:
320  typename THeightGroupedSet::ElementType::Identifiers touch(Height height) {
321  // 1. using non-const set, touch all elements at height
322  ForEachIdentifierWithGroup(m_set, m_heightGroupedSet, height, [](const auto&) {});
323 
324  // 2. using const set, find identifiers of all deactivating elements
326  }
327 
328  private:
329  TSet& m_set;
330  THeightGroupedSet& m_heightGroupedSet;
331  };
332 
334  template<typename TSet, typename THeightGroupedSet>
336  public:
338  HeightBasedPruningMixin(TSet& set, THeightGroupedSet& heightGroupedSet)
339  : m_set(set)
340  , m_heightGroupedSet(heightGroupedSet)
341  {}
342 
343  public:
345  void prune(Height height) {
347  }
348 
349  private:
350  TSet& m_set;
351  THeightGroupedSet& m_heightGroupedSet;
352  };
353 }}
catapult::cache::ContainsMixin::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:56
catapult::cache::detail::CacheFindIterator::CacheFindIterator
CacheFindIterator()=default
Creates an uninitialized iterator.
catapult::cache::FindDeactivatingIdentifiersAtHeight
TIdentifiers FindDeactivatingIdentifiersAtHeight(const TSet &set, const TGroupedSet &groupedSet, Height height)
Finds identifiers of all values in set (with grouped view groupedSet) that are deactivating at height...
Definition: IdentifierGroupCacheUtils.h:84
catapult::cache::detail::NoOpAdapter::Adapt
static TValue & Adapt(TValue &value)
Definition: CacheMixins.h:135
catapult::cache::BasicInsertRemoveMixin::remove
void remove(const KeyType &key)
Removes the value identified by key from the cache.
Definition: CacheMixins.h:295
catapult::cache::IterationMixin
A mixin for adding iteration support to a cache.
Definition: CacheMixins.h:75
catapult::cache::ConstAccessorMixin::ConstAccessorMixin
ConstAccessorMixin(const TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:202
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::deltaset::InsertResult::Redundant
Insert failed because the element already exists (immutable elements only).
catapult::cache::ConstAccessorMixin::ValueType
typename TValueAdapter::AdaptedValueType ValueType
Definition: CacheMixins.h:193
catapult::cache::detail::NoOpAdapter
Definition: CacheMixins.h:132
catapult::cache::BasicInsertRemoveMixin::m_set
TSet & m_set
Definition: CacheMixins.h:305
catapult::cache::BasicInsertRemoveMixin::ValueType
typename TCacheDescriptor::ValueType ValueType
Definition: CacheMixins.h:276
catapult::cache::ActivePredicateMixin
A mixin for adding active querying support to a cache.
Definition: CacheMixins.h:250
catapult::utils::HexFormat
constexpr void HexFormat(const ByteArray< N, TTag > &)
catapult::cache::BasicInsertRemoveMixin::insert
void insert(const ValueType &value)
Inserts value into the cache.
Definition: CacheMixins.h:285
IdentifierGroup.h
catapult::cache::detail::ThrowInvalidKeyError
void ThrowInvalidKeyError(const char *keyState, const T &key)
Definition: CacheMixins.h:119
catapult::cache::ContainsMixin::m_set
const TSet & m_set
Definition: CacheMixins.h:70
catapult::cache::RemoveAllIdentifiersWithGroup
void RemoveAllIdentifiersWithGroup(TSet &set, TGroupedSet &groupedSet, const TGroupingKey &key)
Removes all values in set with grouping key according to groupedSet.
Definition: IdentifierGroupCacheUtils.h:70
catapult::cache::HeightBasedTouchMixin::m_heightGroupedSet
THeightGroupedSet & m_heightGroupedSet
Definition: CacheMixins.h:330
catapult::cache::BasicInsertRemoveMixin::BasicInsertRemoveMixin
BasicInsertRemoveMixin(TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:280
catapult::cache::detail::CacheFindIterator::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:144
catapult::cache::HeightBasedPruningMixin::prune
void prune(Height height)
Prunes the cache at height.
Definition: CacheMixins.h:345
catapult::cache::MutableAccessorMixin::SetIteratorType
typename TSet::FindIterator SetIteratorType
Definition: CacheMixins.h:226
catapult::cache::IterationMixin::tryMakeIterableView
auto tryMakeIterableView() const
Definition: CacheMixins.h:107
catapult::cache::ConstAccessorMixin::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:192
catapult::deltaset::IsBaseSetIterable
bool IsBaseSetIterable(const BaseSet< TElementTraits, TSetTraits, TCommitPolicy > &set)
Returns true if set is iterable.
Definition: BaseSetIterationView.h:73
BaseSetIterationView.h
catapult::cache::HeightBasedPruningMixin::m_set
TSet & m_set
Definition: CacheMixins.h:350
catapult::cache::BasicInsertRemoveMixin
A mixin for adding basic insert and remove support to a cache.
Definition: CacheMixins.h:273
catapult::cache::ActivePredicateMixin::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:252
catapult::cache::IterationMixin::IterableView::IterableView
IterableView(const TSet &set)
Creates a view around set.
Definition: CacheMixins.h:81
functions.h
catapult::utils::to_underlying_type
constexpr std::underlying_type_t< TEnum > to_underlying_type(TEnum value)
Converts a strongly typed enumeration value to its underlying integral value.
Definition: Casting.h:37
catapult::cache::ForEachIdentifierWithGroup
void ForEachIdentifierWithGroup(TSet &set, const TGroupedSet &groupedSet, const TGroupingKey &key, TAction action)
Calls action for each value in set with grouping key according to groupedSet.
Definition: IdentifierGroupCacheUtils.h:41
catapult::cache::IterationMixin::IterableView
An iterable view of the cache.
Definition: CacheMixins.h:78
catapult::cache::ContainsMixin::ContainsMixin
ContainsMixin(const TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:60
catapult::cache::SizeMixin::SizeMixin
SizeMixin(const TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:39
catapult::cache::detail::CacheFindIterator::m_key
KeyType m_key
Definition: CacheMixins.h:181
catapult::utils::BaseValue< uint64_t, Height_tag >
catapult::cache::ConstAccessorMixin
A mixin for adding const access support to a cache.
Definition: CacheMixins.h:190
catapult::cache::BasicInsertRemoveMixin::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:275
catapult::cache::HeightBasedTouchMixin::HeightBasedTouchMixin
HeightBasedTouchMixin(TSet &set, THeightGroupedSet &heightGroupedSet)
Creates a mixin around set and heightGroupedSet.
Definition: CacheMixins.h:313
catapult::cache::detail::NoOpAdapter::AdaptedValueType
TValue AdaptedValueType
Definition: CacheMixins.h:133
catapult::cache::detail::CacheFindIterator
An iterator that is returned by cache find functions.
Definition: CacheMixins.h:142
catapult::cache::SizeMixin::size
size_t size() const
Gets the number of elements in the cache.
Definition: CacheMixins.h:44
BaseSetDeltaIterationView.h
catapult::cache::detail::CacheFindIterator::CacheFindIterator
CacheFindIterator(TBaseSetIterator &&iter, const KeyType &key)
Creates an iterator around a set iterator (iter) for the specified key.
Definition: CacheMixins.h:151
catapult::cache::ActivePredicateMixin::isActive
bool isActive(const KeyType &key, Height height) const
Returns true if the value specified by identifier key is active at height.
Definition: CacheMixins.h:261
HexFormatter.h
catapult::cache::ContainsMixin
A mixin for adding contains support to a cache.
Definition: CacheMixins.h:54
catapult::cache::IterationMixin::m_set
const TSet & m_set
Definition: CacheMixins.h:113
catapult::cache::HeightBasedTouchMixin::m_set
TSet & m_set
Definition: CacheMixins.h:329
catapult::cache::ActivePredicateMixin::ActivePredicateMixin
ActivePredicateMixin(const TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:256
catapult::cache::MutableAccessorMixin::m_set
TSet & m_set
Definition: CacheMixins.h:245
catapult::cache::IterationMixin::IterationMixin
IterationMixin(const TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:101
catapult::cache::ConstAccessorMixin::find
const_iterator find(const KeyType &key) const
Finds the cache value identified by key.
Definition: CacheMixins.h:207
catapult::cache::ConstAccessorMixin::SetIteratorType
typename TSet::FindConstIterator SetIteratorType
Definition: CacheMixins.h:194
catapult::cache::MutableAccessorMixin::find
iterator find(const KeyType &key)
Finds the cache value identified by key.
Definition: CacheMixins.h:239
catapult::cache::MutableAccessorMixin::ValueType
typename TValueAdapter::AdaptedValueType ValueType
Definition: CacheMixins.h:225
catapult::cache::SizeMixin::m_set
const TSet & m_set
Definition: CacheMixins.h:49
Name
static constexpr auto Name
Definition: PtSynchronizer.cpp:31
catapult::cache::HeightBasedPruningMixin::HeightBasedPruningMixin
HeightBasedPruningMixin(TSet &set, THeightGroupedSet &heightGroupedSet)
Creates a mixin around set and heightGroupedSet.
Definition: CacheMixins.h:338
catapult::cache::detail::CacheFindIterator::m_iter
TBaseSetIterator m_iter
Definition: CacheMixins.h:180
catapult::cache::IterationMixin::IterableView::m_set
const TSet & m_set
Definition: CacheMixins.h:96
catapult::deltaset::InsertResult::Unremoved
An element pending removal was reverted.
catapult::cache::detail::CacheFindIterator::tryGet
TValue * tryGet() const
Tries to get a const value.
Definition: CacheMixins.h:168
catapult::cache::ConstAccessorMixin::m_set
const TSet & m_set
Definition: CacheMixins.h:213
catapult::cache::HeightBasedTouchMixin::touch
THeightGroupedSet::ElementType::Identifiers touch(Height height)
Touches the cache at height and returns identifiers of all deactivating elements.
Definition: CacheMixins.h:320
catapult::deltaset::RemoveResult::None
No matching element was found.
catapult::cache::MutableAccessorMixin::MutableAccessorMixin
MutableAccessorMixin(TSet &set)
Creates a mixin around set.
Definition: CacheMixins.h:234
catapult::cache::MutableAccessorMixin::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheMixins.h:224
catapult::cache::ContainsMixin::contains
bool contains(const KeyType &key) const
Gets a value indicating whether or not the cache contains an element with key.
Definition: CacheMixins.h:65
catapult::cache::HeightBasedTouchMixin
A mixin for height-based touching.
Definition: CacheMixins.h:310
types.h
Casting.h
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
IdentifierGroupCacheUtils.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::SizeMixin
A mixin for adding size support to a cache.
Definition: CacheMixins.h:36
catapult::cache::ConstAccessorMixin::const_iterator
detail::CacheFindIterator< TCacheDescriptor, TValueAdapter, SetIteratorType, const ValueType > const_iterator
Find (const) iterator.
Definition: CacheMixins.h:198
catapult::deltaset::InsertResult::Inserted
A new element was inserted.
catapult::cache::IterationMixin::IterableView::end
auto end() const
Returns a const iterator to the element following the last element of the underlying set.
Definition: CacheMixins.h:91
catapult::deltaset::MakeIterableView
BaseSetDeltaIterationView< TSetTraits > MakeIterableView(const BaseSetDelta< TElementTraits, TSetTraits > &delta)
Definition: BaseSetDeltaIterationView.h:200
catapult::cache::detail::CacheFindIterator::tryGetUnadapted
const auto * tryGetUnadapted() const
Tries to get a const (unadapted) value.
Definition: CacheMixins.h:174
catapult::cache::detail::CacheFindIterator::get
TValue & get() const
Definition: CacheMixins.h:159
catapult::cache::MutableAccessorMixin::iterator
detail::CacheFindIterator< TCacheDescriptor, TValueAdapter, SetIteratorType, ValueType > iterator
Find (mutable) iterator.
Definition: CacheMixins.h:230
BaseSetDelta.h
catapult::cache::HeightBasedPruningMixin::m_heightGroupedSet
THeightGroupedSet & m_heightGroupedSet
Definition: CacheMixins.h:351
catapult::cache::MutableAccessorMixin
Definition: CacheMixins.h:222
catapult::cache::IterationMixin::IterableView::begin
auto begin() const
Returns a const iterator to the first element of the underlying set.
Definition: CacheMixins.h:86
catapult::cache::ActivePredicateMixin::m_set
const TSet & m_set
Definition: CacheMixins.h:268
catapult::cache::HeightBasedPruningMixin
A mixin for height-based pruning.
Definition: CacheMixins.h:335