CatapultServer  v0.5.0.1 (Elephant)
ConditionalContainer.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BaseSetCommitPolicy.h"
23 #include "DeltaElements.h"
24 #include <memory>
25 
26 namespace catapult { namespace deltaset {
27 
28  namespace detail {
29  // need to expose hashed or sorted container traits as hint to key storage in BaseSetDelta secondary containers
30  template<typename T, typename = void>
32  using key_compare = typename T::key_compare;
33  };
34 
35  template<typename T>
36  struct StlContainerTraits<T, utils::traits::is_type_expression_t<typename T::hasher>> {
37  using hasher = typename T::hasher;
38  using key_equal = typename T::key_equal;
39  };
40  }
41 
45  Storage,
46 
48  Memory
49  };
50 
52  template<typename TKeyTraits, typename TStorageSet, typename TMemorySet>
53  class ConditionalContainer : public detail::StlContainerTraits<TMemorySet> {
54  public:
55  using StorageSetType = TStorageSet;
56  using MemorySetType = TMemorySet;
57 
58  using value_type = typename MemorySetType::value_type;
59 
60  private:
61  // use flags to support similar storage and memory sets with same iterator
62  using StorageFlag = std::integral_constant<ConditionalContainerMode, ConditionalContainerMode::Storage>;
63  using MemoryFlag = std::integral_constant<ConditionalContainerMode, ConditionalContainerMode::Memory>;
64 
65  public:
68  public:
70  ConditionalIterator() = default;
71 
73  explicit ConditionalIterator(typename StorageSetType::const_iterator&& iter, StorageFlag)
74  : m_storageIter(std::move(iter))
76  {}
77 
79  explicit ConditionalIterator(typename MemorySetType::const_iterator&& iter, MemoryFlag)
80  : m_memoryIter(std::move(iter))
82  {}
83 
84  public:
86  bool operator==(const ConditionalIterator& rhs) const {
88  }
89 
91  bool operator!=(const ConditionalIterator& rhs) const {
92  return !(*this == rhs);
93  }
94 
95  public:
97  const auto* operator->() const {
98  return &operator*();
99  }
100 
102  const auto& operator*() const {
104  }
105 
106  private:
107  typename StorageSetType::const_iterator m_storageIter;
108  typename MemorySetType::const_iterator m_memoryIter;
110  };
111 
112  public:
115 
116  public:
119  {}
120 
123  template<typename... TStorageArgs>
124  explicit ConditionalContainer(ConditionalContainerMode mode, TStorageArgs&&... storageArgs) {
126  m_pContainer1 = std::make_unique<StorageSetType>(std::forward<TStorageArgs>(storageArgs)...);
127  else
128  m_pContainer2 = std::make_unique<MemorySetType>();
129  }
130 
131  public:
133  bool empty() const {
134  return m_pContainer1 ? m_pContainer1->empty() : m_pContainer2->empty();
135  }
136 
138  size_t size() const {
139  return m_pContainer1 ? m_pContainer1->size() : m_pContainer2->size();
140  }
141 
142  public:
145  return m_pContainer1
148  }
149 
151  ConditionalIterator find(const typename TKeyTraits::KeyType& key) const {
152  return m_pContainer1
155  }
156 
157  public:
159  void update(const DeltaElements<MemorySetType>& deltas) {
160  if (m_pContainer1)
161  UpdateSet<TKeyTraits>(*m_pContainer1, deltas);
162  else
163  UpdateSet<TKeyTraits>(*m_pContainer2, deltas);
164  }
165 
167  template<typename TPruningBoundary>
168  void prune(const TPruningBoundary& pruningBoundary) {
169  if (m_pContainer1)
170  PruneBaseSet(*m_pContainer1, pruningBoundary);
171  else
172  PruneBaseSet(*m_pContainer2, pruningBoundary);
173  }
174 
175  private:
176  std::unique_ptr<StorageSetType> m_pContainer1;
177  std::unique_ptr<MemorySetType> m_pContainer2;
178 
179  private:
180  template<typename TKeyTraits2, typename TStorageSet2, typename TMemorySet2>
182 
183  template<typename TKeyTraits2, typename TStorageSet2, typename TMemorySet2>
185  };
186 
189  template<typename TKeyTraits, typename TStorageSet, typename TMemorySet>
191  return !!set.m_pContainer2;
192  }
193 
197  template<typename TKeyTraits, typename TStorageSet, typename TMemorySet>
199  if (!IsSetIterable(set))
200  CATAPULT_THROW_INVALID_ARGUMENT("ConditionalContainer is only iterable when it is memory-based");
201 
202  return *set.m_pContainer2;
203  }
204 
207  template<typename TKeyTraits, typename TStorageSet, typename TMemorySet>
209  container.update(deltas);
210  }
211 
214  template<typename TKeyTraits, typename TStorageSet, typename TMemorySet, typename TPruningBoundary>
215  void PruneBaseSet(ConditionalContainer<TKeyTraits, TStorageSet, TMemorySet>& container, const TPruningBoundary& pruningBoundary) {
216  container.prune(pruningBoundary);
217  }
218 }}
catapult::deltaset::ConditionalContainer::value_type
typename MemorySetType::value_type value_type
Definition: ConditionalContainer.h:58
catapult::model::TransactionNotification
Notifies the arrival of a transaction.
Definition: Notifications.h:231
catapult::deltaset::ConditionalContainer::ConditionalIterator::ConditionalIterator
ConditionalIterator()=default
Creates an uninitialized iterator.
catapult::deltaset::ConditionalContainer::cend
ConditionalIterator cend() const
Returns a const iterator to the element following the last element of the underlying set.
Definition: ConditionalContainer.h:144
catapult::deltaset::ConditionalContainer::find
ConditionalIterator find(const typename TKeyTraits::KeyType &key) const
Searches for key in this set.
Definition: ConditionalContainer.h:151
catapult::deltaset::ConditionalContainer::empty
bool empty() const
Gets a value indicating whether or not this set is empty.
Definition: ConditionalContainer.h:133
catapult::deltaset::ConditionalContainerMode
ConditionalContainerMode
Possible conditional container modes.
Definition: ConditionalContainer.h:43
LockStatusAccountBalanceObserver.h
catapult::deltaset::ConditionalContainer::StorageSetType
TStorageSet StorageSetType
Definition: ConditionalContainer.h:55
catapult::deltaset::UpdateSet
void UpdateSet(TStorageSet &elements, const DeltaElements< TMemorySet > &deltas)
Applies all changes in deltas to elements.
Definition: BaseSetCommitPolicy.h:29
catapult::deltaset::IsSetIterable
bool IsSetIterable(const TSet &)
Returns true if set is iterable.
Definition: BaseSetIterationView.h:61
catapult::observers::DEFINE_OBSERVER
DEFINE_OBSERVER(AccountAddress, model::AccountAddressNotification, [](const auto &notification, const auto &context) { DefaultAccountVisitor visitor(context);visitor.visit(notification.Address);})
catapult::deltaset::SelectIterableSet
const TSet & SelectIterableSet(const TSet &set)
Selects the iterable set from set.
Definition: BaseSetIterationView.h:67
catapult::cache::HashLockInfoCache
Synchronized cache composed of hash lock info information.
Definition: HashLockInfoCache.h:33
catapult::deltaset::DeltaElements
Slim wrapper around changed elements.
Definition: DeltaElements.h:27
Receipt_Type
static constexpr auto Receipt_Type
Definition: CompletedAggregateObserver.cpp:36
catapult::deltaset::ConditionalContainer::ConditionalIterator::ConditionalIterator
ConditionalIterator(typename StorageSetType::const_iterator &&iter, StorageFlag)
Creates a conditional iterator around iter for a storage container.
Definition: ConditionalContainer.h:73
catapult::deltaset::ConditionalContainerMode::Storage
Delegate to storage.
catapult::deltaset::ConditionalContainer::m_pContainer1
std::unique_ptr< StorageSetType > m_pContainer1
Definition: ConditionalContainer.h:176
catapult::deltaset::ConditionalContainer::MemoryFlag
std::integral_constant< ConditionalContainerMode, ConditionalContainerMode::Memory > MemoryFlag
Definition: ConditionalContainer.h:63
catapult::observers::Notification
model::TransactionNotification Notification
Definition: CompletedAggregateObserver.cpp:29
BaseSetCommitPolicy.h
catapult::deltaset::detail::StlContainerTraits< T, utils::traits::is_type_expression_t< typename T::hasher > >::hasher
typename T::hasher hasher
Definition: ConditionalContainer.h:37
catapult::deltaset::ConditionalContainer::ConditionalIterator::ConditionalIterator
ConditionalIterator(typename MemorySetType::const_iterator &&iter, MemoryFlag)
Creates a conditional iterator around iter for a memory container.
Definition: ConditionalContainer.h:79
catapult::deltaset::ConditionalContainer::SelectIterableSet
const friend TMemorySet2 & SelectIterableSet(const ConditionalContainer< TKeyTraits2, TStorageSet2, TMemorySet2 > &set)
catapult::deltaset::ConditionalContainer::ConditionalIterator::m_memoryIter
MemorySetType::const_iterator m_memoryIter
Definition: ConditionalContainer.h:108
catapult::deltaset::ConditionalContainer::StorageFlag
std::integral_constant< ConditionalContainerMode, ConditionalContainerMode::Storage > StorageFlag
Definition: ConditionalContainer.h:62
catapult::deltaset::ConditionalContainer::ConditionalIterator::m_mode
ConditionalContainerMode m_mode
Definition: ConditionalContainer.h:109
catapult::deltaset::ConditionalContainer::ConditionalContainer
ConditionalContainer()
Creates a memory conditional container with mode.
Definition: ConditionalContainer.h:118
catapult::deltaset::ConditionalContainer::MemorySetType
TMemorySet MemorySetType
Definition: ConditionalContainer.h:56
catapult::utils::traits::is_type_expression_t
typename is_type_expression< T, Enable >::type is_type_expression_t
true if the expression is valid and evaluates to a type, false otherwise.
Definition: Traits.h:98
HashLockInfoCache.h
catapult::deltaset::ConditionalContainer::ConditionalIterator
A const iterator.
Definition: ConditionalContainer.h:67
catapult::deltaset::ConditionalContainer::size
size_t size() const
Gets the size of this set.
Definition: ConditionalContainer.h:138
catapult::deltaset::ConditionalContainer::ConditionalIterator::operator!=
bool operator!=(const ConditionalIterator &rhs) const
Returns true if this iterator is not equal to rhs.
Definition: ConditionalContainer.h:91
catapult::deltaset::ConditionalContainer::update
void update(const DeltaElements< MemorySetType > &deltas)
Applies all changes in deltas to the underlying container.
Definition: ConditionalContainer.h:159
HashLockReceiptType.h
DeltaElements.h
catapult::deltaset::ConditionalContainer::ConditionalIterator::operator->
const auto * operator->() const
Returns a const pointer to the current element.
Definition: ConditionalContainer.h:97
catapult::deltaset::PruneBaseSet
void PruneBaseSet(ConditionalContainer< TKeyTraits, TStorageSet, TMemorySet > &container, const TPruningBoundary &pruningBoundary)
Definition: ConditionalContainer.h:215
catapult::deltaset::ConditionalContainer::m_pContainer2
std::unique_ptr< MemorySetType > m_pContainer2
Definition: ConditionalContainer.h:177
catapult::deltaset::ConditionalContainer::ConditionalContainer
ConditionalContainer(ConditionalContainerMode mode, TStorageArgs &&... storageArgs)
Definition: ConditionalContainer.h:124
AggregateEntityType.h
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
Observers.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::deltaset::detail::StlContainerTraits
Definition: ConditionalContainer.h:31
catapult::deltaset::ConditionalContainer::IsSetIterable
friend bool IsSetIterable(const ConditionalContainer< TKeyTraits2, TStorageSet2, TMemorySet2 > &set)
catapult::deltaset::ConditionalContainer::prune
void prune(const TPruningBoundary &pruningBoundary)
Optionally prunes underlying container using pruningBoundary.
Definition: ConditionalContainer.h:168
catapult::deltaset::detail::StlContainerTraits< TMemorySet >::key_compare
typename TMemorySet ::key_compare key_compare
Definition: ConditionalContainer.h:32
catapult::deltaset::ConditionalContainerMode::Memory
Delegate to memory.
catapult::deltaset::ConditionalContainer
A conditional container that delegates to either a storage or a memory backed container.
Definition: ConditionalContainer.h:53
catapult::deltaset::ConditionalContainer::ConditionalIterator::m_storageIter
StorageSetType::const_iterator m_storageIter
Definition: ConditionalContainer.h:107
catapult::deltaset::ConditionalContainer::ConditionalIterator::operator==
bool operator==(const ConditionalIterator &rhs) const
Returns true if this iterator is equal to rhs.
Definition: ConditionalContainer.h:86
catapult::deltaset::ConditionalContainer::ConditionalIterator::operator*
const auto & operator*() const
Returns a const reference to the current element.
Definition: ConditionalContainer.h:102
catapult::deltaset::detail::StlContainerTraits< T, utils::traits::is_type_expression_t< typename T::hasher > >::key_equal
typename T::key_equal key_equal
Definition: ConditionalContainer.h:38