CatapultServer  v0.5.0.1 (Elephant)
ObserverUtils.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "ObserverContext.h"
23 #include "ObserverTypes.h"
24 
25 namespace catapult { namespace observers {
26 
28  constexpr bool ShouldPrune(const ObserverContext& context, size_t pruneInterval) {
29  return NotifyMode::Commit == context.Mode && 0 == context.Height.unwrap() % pruneInterval;
30  }
31 
33  template<typename TAction>
34  constexpr bool ShouldLink(TAction action, NotifyMode notifyMode) {
35  return NotifyMode::Commit == notifyMode ? TAction::Link == action : TAction::Unlink == action;
36  }
37 
40  template<typename TCache>
42  const std::string& name,
43  size_t interval,
44  BlockDuration gracePeriod) {
46  return std::make_unique<ObserverType>(name + "PruningObserver", [interval, gracePeriod](const auto&, auto& context) {
47  if (!ShouldPrune(context, interval))
48  return;
49 
50  if (context.Height.unwrap() <= gracePeriod.unwrap())
51  return;
52 
53  auto pruneHeight = Height(context.Height.unwrap() - gracePeriod.unwrap());
54  auto& cache = context.Cache.template sub<TCache>();
55  cache.prune(pruneHeight);
56  });
57  }
58 
60  template<typename TCache>
63  return std::make_unique<ObserverType>(name + "PruningObserver", [interval](const auto& notification, const auto& context) {
64  if (!ShouldPrune(context, interval))
65  return;
66 
67  auto& cache = context.Cache.template sub<TCache>();
68  cache.prune(notification.Timestamp);
69  });
70  }
71 
74  template<typename TCache>
76  const std::string& name,
77  model::ReceiptType receiptType) {
79  return std::make_unique<ObserverType>(name + "TouchObserver", [receiptType](const auto&, auto& context) {
80  auto& cache = context.Cache.template sub<TCache>();
81  auto expiryIds = cache.touch(context.Height);
82 
83  if (NotifyMode::Rollback == context.Mode)
84  return;
85 
86  // sort expiry ids because receipts must be generated deterministically
87  std::set<typename decltype(expiryIds)::value_type> orderedExpiryIds(expiryIds.cbegin(), expiryIds.cend());
88  for (auto id : orderedExpiryIds)
89  context.StatementBuilder().addReceipt(model::ArtifactExpiryReceipt<decltype(id)>(receiptType, id));
90  });
91  }
92 }}
catapult::observers::ObserverContext
Context passed to all the observers.
Definition: ObserverContext.h:78
catapult::Height
utils::BaseValue< uint64_t, Height_tag > Height
Definition: src/catapult/types.h:85
ObserverTypes.h
catapult::observers::NotifyMode
NotifyMode
Enumeration of possible notification modes.
Definition: ObserverContext.h:41
catapult::observers::FunctionalNotificationObserverT
A notification observer implementation that wraps a function.
Definition: FunctionalNotificationObserver.h:29
catapult::utils::BasicBaseValue::unwrap
constexpr ValueType unwrap() const
Unwraps this value and returns the underlying raw value.
Definition: BaseValue.h:53
catapult::observers::CreateCacheBlockPruningObserver
NotificationObserverPointerT< model::BlockNotification > CreateCacheBlockPruningObserver(const std::string &name, size_t interval, BlockDuration gracePeriod)
Definition: ObserverUtils.h:41
catapult::observers::CreateCacheBlockTouchObserver
NotificationObserverPointerT< model::BlockNotification > CreateCacheBlockTouchObserver(const std::string &name, model::ReceiptType receiptType)
Definition: ObserverUtils.h:75
catapult::utils::BaseValue< uint64_t, BlockDuration_tag >
catapult::model::ReceiptType
ReceiptType
Enumeration of receipt types.
Definition: ReceiptType.h:58
catapult::observers::CreateCacheTimePruningObserver
NotificationObserverPointerT< model::BlockNotification > CreateCacheTimePruningObserver(const std::string &name, size_t interval)
Creates a time-based cache pruning observer with name that runs every interval blocks.
Definition: ObserverUtils.h:61
ObserverContext.h
catapult::action
std::function< void()> action
An action function.
Definition: functions.h:27
catapult::observers::ObserverContext::Mode
const NotifyMode Mode
Notification mode.
Definition: ObserverContext.h:95
catapult::observers::NotificationObserverPointerT
std::unique_ptr< const NotificationObserverT< TNotification > > NotificationObserverPointerT
A notification observer (unique) pointer.
Definition: ObserverTypes.h:34
catapult::model::ArtifactExpiryReceipt
Binary layout for an artifact expiry receipt.
Definition: Receipt.h:118
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::observers::ObserverContext::Height
const catapult::Height Height
Current height.
Definition: ObserverContext.h:92
catapult::observers::ShouldPrune
constexpr bool ShouldPrune(const ObserverContext &context, size_t pruneInterval)
Returns true if context and pruneInterval indicate that pruning should be done.
Definition: ObserverUtils.h:28
catapult::observers::ShouldLink
constexpr bool ShouldLink(TAction action, NotifyMode notifyMode)
Returns true if action and notifyMode indicate that a link should be made.
Definition: ObserverUtils.h:34