CatapultServer  v0.5.0.1 (Elephant)
PatriciaTreeUtils.h
Go to the documentation of this file.
1 
21 #pragma once
24 #include "catapult/exceptions.h"
25 
26 namespace catapult { namespace cache {
27 
28  // region IsActiveAdapter
29 
30  namespace detail {
32  public:
33  template<typename T>
34  static bool IsActive(const T& value, Height height) {
35  return IsActive(value, height, IsActiveAccessor<T>());
36  }
37 
38  private:
40  using UnsupportedIsActiveFlag = std::integral_constant<IsActiveType, IsActiveType::Unsupported>;
41  using SupportedIsActiveFlag = std::integral_constant<IsActiveType, IsActiveType::Supported>;
42 
43  template<typename T, typename = void>
45 
46  template<typename T>
48  T,
49  utils::traits::is_type_expression_t<decltype(reinterpret_cast<const T*>(0)->isActive(Height()))>>
50  : public SupportedIsActiveFlag
51  {};
52 
53  private:
54  template<typename T>
55  static bool IsActive(const T&, Height, UnsupportedIsActiveFlag) {
56  return true;
57  }
58 
59  template<typename T>
60  static bool IsActive(const T& value, Height height, SupportedIsActiveFlag) {
61  return value.isActive(height);
62  }
63  };
64  }
65 
66  // endregion
67 
70  template<typename TTree, typename TSet>
71  void ApplyDeltasToTree(TTree& tree, const TSet& set, uint32_t minGenerationId, Height height) {
72  auto needsApplication = [&set, minGenerationId, maxGenerationId = set.generationId()](const auto& key) {
73  auto generationId = set.generationId(key);
74  return minGenerationId <= generationId && generationId <= maxGenerationId;
75  };
76 
77  auto handleModification = [&tree, height](const auto& pair) {
78  if (detail::IsActiveAdapter::IsActive(pair.second, height))
79  tree.set(pair.first, pair.second);
80  else
81  tree.unset(pair.first);
82  };
83 
84  auto deltas = set.deltas();
85  for (const auto& pair : deltas.Added) {
86  if (needsApplication(pair.first)) {
87  // a value can be added and deactivated during the processing of a single chain part
88  handleModification(pair);
89  }
90  }
91 
92  for (const auto& pair : deltas.Copied) {
93  if (needsApplication(pair.first))
94  handleModification(pair);
95  }
96 
97  for (const auto& pair : deltas.Removed) {
98  if (needsApplication(pair.first))
99  tree.unset(pair.first);
100  }
101  }
102 }}
exceptions.h
catapult::cache::detail::IsActiveAdapter::IsActive
static bool IsActive(const T &value, Height height)
Definition: PatriciaTreeUtils.h:34
catapult::cache::detail::IsActiveAdapter
Definition: PatriciaTreeUtils.h:31
catapult::cache::detail::IsActiveAdapter::IsActive
static bool IsActive(const T &value, Height height, SupportedIsActiveFlag)
Definition: PatriciaTreeUtils.h:60
catapult::cache::detail::IsActiveAdapter::IsActive
static bool IsActive(const T &, Height, UnsupportedIsActiveFlag)
Definition: PatriciaTreeUtils.h:55
catapult::utils::BaseValue< uint64_t, Height_tag >
PatriciaTree.h
catapult::cache::detail::IsActiveAdapter::UnsupportedIsActiveFlag
std::integral_constant< IsActiveType, IsActiveType::Unsupported > UnsupportedIsActiveFlag
Definition: PatriciaTreeUtils.h:40
catapult::cache::detail::IsActiveAdapter::IsActiveType::Unsupported
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
DeltaElements.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::detail::IsActiveAdapter::SupportedIsActiveFlag
std::integral_constant< IsActiveType, IsActiveType::Supported > SupportedIsActiveFlag
Definition: PatriciaTreeUtils.h:41
catapult::cache::detail::IsActiveAdapter::IsActiveAccessor
Definition: PatriciaTreeUtils.h:44
catapult::cache::detail::IsActiveAdapter::IsActiveType::Supported
catapult::cache::ApplyDeltasToTree
void ApplyDeltasToTree(TTree &tree, const TSet &set, uint32_t minGenerationId, Height height)
Definition: PatriciaTreeUtils.h:71
catapult::cache::detail::IsActiveAdapter::IsActiveType
IsActiveType
Definition: PatriciaTreeUtils.h:39