CatapultServer  v0.5.0.1 (Elephant)
BasicAggregateTransactionsCache.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include <memory>
24 
25 namespace catapult { namespace cache {
26 
29  template<typename TCacheTraits, typename TChangeSubscriberTraits>
30  class BasicAggregateTransactionsCacheModifier : public TCacheTraits::CacheModifierType {
31  private:
32  using ChangeSubscriberType = typename TCacheTraits::ChangeSubscriberType;
33  using CacheModifierProxyType = typename TCacheTraits::CacheModifierProxyType;
34  using TransactionInfoType = typename TChangeSubscriberTraits::TransactionInfoType;
35 
36  public:
37  using TCacheTraits::CacheModifierType::add;
38 
39  public:
42  : m_modifier(std::move(modifier))
44  {}
45 
47  ~BasicAggregateTransactionsCacheModifier() noexcept(false) override {
48  flush();
49  }
50 
51  public:
52  size_t size() const override {
53  return m_modifier.size();
54  }
55 
56  bool add(const TransactionInfoType& transactionInfo) override {
57  if (!m_modifier.add(transactionInfo))
58  return false;
59 
60  m_transactionChangeTracker.add(TChangeSubscriberTraits::ToTransactionInfo(transactionInfo));
61  return true;
62  }
63 
64  TransactionInfoType remove(const Hash256& hash) override {
65  auto transactionInfo = m_modifier.remove(hash);
66  if (transactionInfo)
67  remove(transactionInfo);
68 
69  return transactionInfo;
70  }
71 
72  protected:
75  return m_modifier;
76  }
77 
80  return m_modifier;
81  }
82 
85  return m_subscriber;
86  }
87 
89  void remove(const TransactionInfoType& transactionInfo) {
90  m_transactionChangeTracker.remove(TChangeSubscriberTraits::ToTransactionInfo(transactionInfo));
91  }
92 
93  private:
94  void flush() {
95  const auto& removedTransactionInfos = m_transactionChangeTracker.removedTransactionInfos();
96  if (!removedTransactionInfos.empty())
97  TChangeSubscriberTraits::NotifyRemoves(m_subscriber, m_transactionChangeTracker.removedTransactionInfos());
98 
99  const auto& addedTransactionInfos = m_transactionChangeTracker.addedTransactionInfos();
100  if (!addedTransactionInfos.empty())
101  TChangeSubscriberTraits::NotifyAdds(m_subscriber, addedTransactionInfos);
102 
103  m_subscriber.flush();
105  }
106 
107  private:
111  };
112 
114  template<typename TCacheTraits, typename TAggregateCacheModifier>
115  class BasicAggregateTransactionsCache : public TCacheTraits::CacheType {
116  private:
117  using CacheType = typename TCacheTraits::CacheType;
118  using ChangeSubscriberType = typename TCacheTraits::ChangeSubscriberType;
119  using CacheModifierProxyType = typename TCacheTraits::CacheModifierProxyType;
120 
121  public:
123  BasicAggregateTransactionsCache(CacheType& cache, std::unique_ptr<ChangeSubscriberType>&& pChangeSubscriber)
124  : m_cache(cache)
125  , m_pChangeSubscriber(std::move(pChangeSubscriber))
126  {}
127 
128  public:
130  return CacheModifierProxyType(std::make_unique<TAggregateCacheModifier>(m_cache.modifier(), *m_pChangeSubscriber));
131  }
132 
133  private:
135  std::unique_ptr<ChangeSubscriberType> m_pChangeSubscriber;
136  };
137 }}
catapult::model::TransactionChangeTracker::reset
void reset()
Clears all added and removed transactions.
Definition: TransactionChangeTracker.h:65
catapult::cache::BasicAggregateTransactionsCacheModifier::m_transactionChangeTracker
model::TransactionChangeTracker m_transactionChangeTracker
Definition: BasicAggregateTransactionsCache.h:110
TransactionChangeTracker.h
catapult::model::TransactionChangeTracker
Tracks transaction changes and keeps track of net changes.
Definition: TransactionChangeTracker.h:28
catapult::model::TransactionChangeTracker::addedTransactionInfos
const TransactionInfosSet & addedTransactionInfos() const
Gets the infos of net added transactions.
Definition: TransactionChangeTracker.h:31
catapult::cache::BasicAggregateTransactionsCacheModifier::CacheModifierProxyType
typename TCacheTraits::CacheModifierProxyType CacheModifierProxyType
Definition: BasicAggregateTransactionsCache.h:33
catapult::cache::BasicAggregateTransactionsCache::CacheModifierProxyType
typename TCacheTraits::CacheModifierProxyType CacheModifierProxyType
Definition: BasicAggregateTransactionsCache.h:119
catapult::cache::BasicAggregateTransactionsCacheModifier::flush
void flush()
Definition: BasicAggregateTransactionsCache.h:94
catapult::cache::BasicAggregateTransactionsCache::m_pChangeSubscriber
std::unique_ptr< ChangeSubscriberType > m_pChangeSubscriber
Definition: BasicAggregateTransactionsCache.h:135
catapult::cache::BasicAggregateTransactionsCacheModifier::remove
TransactionInfoType remove(const Hash256 &hash) override
Definition: BasicAggregateTransactionsCache.h:64
catapult::model::TransactionChangeTracker::remove
void remove(const TransactionInfo &transactionInfo)
Marks transaction info (transactionInfo) as removed.
Definition: TransactionChangeTracker.h:53
catapult::cache::BasicAggregateTransactionsCache::m_cache
CacheType & m_cache
Definition: BasicAggregateTransactionsCache.h:134
catapult::cache::BasicAggregateTransactionsCacheModifier::add
bool add(const TransactionInfoType &transactionInfo) override
Definition: BasicAggregateTransactionsCache.h:56
catapult::cache::BasicAggregateTransactionsCache::BasicAggregateTransactionsCache
BasicAggregateTransactionsCache(CacheType &cache, std::unique_ptr< ChangeSubscriberType > &&pChangeSubscriber)
Creates an aggregate transactions cache that delegates to cache and publishes transaction changes to ...
Definition: BasicAggregateTransactionsCache.h:123
catapult::cache::BasicAggregateTransactionsCacheModifier::m_modifier
CacheModifierProxyType m_modifier
Definition: BasicAggregateTransactionsCache.h:108
catapult::cache::BasicAggregateTransactionsCacheModifier::remove
void remove(const TransactionInfoType &transactionInfo)
Removes transactionInfo from the cache.
Definition: BasicAggregateTransactionsCache.h:89
catapult::cache::BasicAggregateTransactionsCacheModifier::ChangeSubscriberType
typename TCacheTraits::ChangeSubscriberType ChangeSubscriberType
Definition: BasicAggregateTransactionsCache.h:32
catapult::cache::BasicAggregateTransactionsCacheModifier::m_subscriber
ChangeSubscriberType & m_subscriber
Definition: BasicAggregateTransactionsCache.h:109
catapult::cache::BasicAggregateTransactionsCacheModifier
Definition: BasicAggregateTransactionsCache.h:30
catapult::cache::BasicAggregateTransactionsCacheModifier::subscriber
ChangeSubscriberType & subscriber()
Gets the subscriber.
Definition: BasicAggregateTransactionsCache.h:84
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::BasicAggregateTransactionsCacheModifier::TransactionInfoType
typename TChangeSubscriberTraits::TransactionInfoType TransactionInfoType
Definition: BasicAggregateTransactionsCache.h:34
catapult::cache::BasicAggregateTransactionsCacheModifier::modifier
const CacheModifierProxyType & modifier() const
Gets the (const) modifier.
Definition: BasicAggregateTransactionsCache.h:79
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::model::TransactionChangeTracker::add
void add(const TransactionInfo &transactionInfo)
Marks transaction info (transactionInfo) as added.
Definition: TransactionChangeTracker.h:42
catapult::cache::BasicAggregateTransactionsCache
A basic aggregate transactions cache that delegates to a wrapped cache and raises notifications on a ...
Definition: BasicAggregateTransactionsCache.h:115
catapult::cache::BasicAggregateTransactionsCache::CacheType
typename TCacheTraits::CacheType CacheType
Definition: BasicAggregateTransactionsCache.h:117
catapult::cache::BasicAggregateTransactionsCacheModifier::~BasicAggregateTransactionsCacheModifier
~BasicAggregateTransactionsCacheModifier() noexcept(false) override
Destroys the modifier and notifies subscribers of changes.
Definition: BasicAggregateTransactionsCache.h:47
catapult::cache::BasicAggregateTransactionsCache::modifier
CacheModifierProxyType modifier() override
Definition: BasicAggregateTransactionsCache.h:129
catapult::cache::BasicAggregateTransactionsCacheModifier::size
size_t size() const override
Definition: BasicAggregateTransactionsCache.h:52
catapult::cache::BasicAggregateTransactionsCacheModifier::modifier
CacheModifierProxyType & modifier()
Gets the modifier.
Definition: BasicAggregateTransactionsCache.h:74
catapult::model::TransactionChangeTracker::removedTransactionInfos
const TransactionInfosSet & removedTransactionInfos() const
Gets the infos of net removed transactions.
Definition: TransactionChangeTracker.h:36
catapult::cache::BasicAggregateTransactionsCache::ChangeSubscriberType
typename TCacheTraits::ChangeSubscriberType ChangeSubscriberType
Definition: BasicAggregateTransactionsCache.h:118
catapult::cache::BasicAggregateTransactionsCacheModifier::BasicAggregateTransactionsCacheModifier
BasicAggregateTransactionsCacheModifier(CacheModifierProxyType &&modifier, ChangeSubscriberType &subscriber)
Creates an aggregate transactions cache modifier around modifier and subscriber.
Definition: BasicAggregateTransactionsCache.h:41