CatapultServer  v0.5.0.1 (Elephant)
BatchRangeDispatcher.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "ConsumerDispatcher.h"
23 #include "catapult/utils/Casting.h"
24 #include "catapult/utils/Hashers.h"
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace catapult { namespace disruptor {
30 
32  template<typename TAnnotatedEntityRange>
34  private:
35  using EntityRange = decltype(TAnnotatedEntityRange::Range);
36 
37  struct RangeGroupKey {
38  public:
41 
42  public:
43  friend bool operator==(const RangeGroupKey& lhs, const RangeGroupKey& rhs) {
44  return lhs.SourcePublicKey == rhs.SourcePublicKey && lhs.Source == rhs.Source;
45  }
46  };
47 
49  size_t operator()(const RangeGroupKey& key) const {
51  }
52  };
53 
54  using GroupedRangesMap = std::unordered_map<RangeGroupKey, std::vector<EntityRange>, RangeGroupKeyHasher>;
55 
56  public:
58  explicit BatchRangeDispatcher(ConsumerDispatcher& dispatcher) : m_dispatcher(dispatcher)
59  {}
60 
61  public:
63  void queue(TAnnotatedEntityRange&& range, InputSource source) {
65  m_rangesMap[{ range.SourcePublicKey, source }].push_back(std::move(range.Range));
66  }
67 
69  void dispatch() {
70  GroupedRangesMap rangesMap;
71 
72  {
74  rangesMap = std::move(m_rangesMap);
75  }
76 
77  for (auto& pair : rangesMap) {
78  auto mergedRange = EntityRange::MergeRanges(std::move(pair.second));
79  m_dispatcher.processElement(ConsumerInput({ std::move(mergedRange), pair.first.SourcePublicKey }, pair.first.Source));
80  }
81  }
82 
83  public:
85  bool empty() const {
87  return m_rangesMap.empty();
88  }
89 
90  private:
94  };
95 }}
catapult::disruptor::ConsumerInput
Consumer input composed of a range of entities augmented with metadata.
Definition: ConsumerInput.h:30
SpinLock.h
catapult::disruptor::BatchRangeDispatcher::m_lock
utils::SpinLock m_lock
Definition: BatchRangeDispatcher.h:93
ConsumerDispatcher.h
catapult::disruptor::InputSource
InputSource
Possible consumer input sources.
Definition: InputSource.h:42
catapult::disruptor::BatchRangeDispatcher::GroupedRangesMap
std::unordered_map< RangeGroupKey, std::vector< EntityRange >, RangeGroupKeyHasher > GroupedRangesMap
Definition: BatchRangeDispatcher.h:54
catapult::disruptor::BatchRangeDispatcher::m_rangesMap
GroupedRangesMap m_rangesMap
Definition: BatchRangeDispatcher.h:92
catapult::disruptor::ConsumerDispatcher
Dispatcher for disruptor consumers.
Definition: ConsumerDispatcher.h:35
catapult::disruptor::BatchRangeDispatcher::RangeGroupKey
Definition: BatchRangeDispatcher.h:37
catapult::disruptor::BatchRangeDispatcher
Batches entity ranges for processing by a ConsumerDispatcher.
Definition: BatchRangeDispatcher.h:33
catapult::utils::SpinLock
Definition: SpinLock.h:31
catapult::disruptor::BatchRangeDispatcher::RangeGroupKeyHasher::operator()
size_t operator()(const RangeGroupKey &key) const
Definition: BatchRangeDispatcher.h:49
catapult::disruptor::BatchRangeDispatcher::m_dispatcher
ConsumerDispatcher & m_dispatcher
Definition: BatchRangeDispatcher.h:91
catapult::disruptor::BatchRangeDispatcher::queue
void queue(TAnnotatedEntityRange &&range, InputSource source)
Queues processing of range from source.
Definition: BatchRangeDispatcher.h:63
catapult::disruptor::BatchRangeDispatcher::EntityRange
decltype(TAnnotatedEntityRange::Range) EntityRange
Definition: BatchRangeDispatcher.h:35
catapult::disruptor::ConsumerDispatcher::processElement
DisruptorElementId processElement(ConsumerInput &&input, const ProcessingCompleteFunc &processingComplete)
Definition: ConsumerDispatcher.cpp:163
catapult::disruptor::BatchRangeDispatcher::RangeGroupKey::SourcePublicKey
Key SourcePublicKey
Definition: BatchRangeDispatcher.h:39
catapult::disruptor::BatchRangeDispatcher::dispatch
void dispatch()
Dispatches all queued elements to the underlying dispatcher.
Definition: BatchRangeDispatcher.h:69
Casting.h
catapult
Definition: AddressExtractionExtension.cpp:28
Hashers.h
catapult::disruptor::BatchRangeDispatcher::RangeGroupKey::Source
InputSource Source
Definition: BatchRangeDispatcher.h:40
catapult::disruptor::BatchRangeDispatcher::empty
bool empty() const
Returns true if no ranges are currently queued.
Definition: BatchRangeDispatcher.h:85
catapult::utils::ByteArray< Key_Size, Key_tag >
catapult::utils::SpinLockGuard
std::lock_guard< SpinLock > SpinLockGuard
A spin lock guard.
Definition: SpinLock.h:60
catapult::utils::ArrayHasher
Definition: Hashers.h:32
catapult::disruptor::BatchRangeDispatcher::RangeGroupKey::operator==
friend bool operator==(const RangeGroupKey &lhs, const RangeGroupKey &rhs)
Definition: BatchRangeDispatcher.h:43
catapult::disruptor::BatchRangeDispatcher::BatchRangeDispatcher
BatchRangeDispatcher(ConsumerDispatcher &dispatcher)
Creates a batch range dispatcher around dispatcher.
Definition: BatchRangeDispatcher.h:58
catapult::disruptor::BatchRangeDispatcher::RangeGroupKeyHasher
Definition: BatchRangeDispatcher.h:48