CatapultServer  v0.5.0.1 (Elephant)
CacheChanges.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "CatapultCacheDelta.h"
23 #include <unordered_set>
24 #include <vector>
25 
26 namespace catapult { namespace cache {
27 
28  // region MemoryCacheChanges
29 
33  virtual ~MemoryCacheChanges() = default;
34  };
35 
37  template<typename TValue>
40  std::vector<TValue> Added;
41 
43  std::vector<TValue> Removed;
44 
46  std::vector<TValue> Copied;
47  };
48 
49  // endregion
50 
51  // region SingleCacheChanges
52 
56 
58  template<typename TCacheDelta, typename TValue>
60  private:
61  using PointerContainer = std::unordered_set<const TValue*>;
62 
63  public:
65  explicit SingleCacheChangesT(const TCacheDelta& cacheDelta)
66  : m_pCacheDelta(&cacheDelta)
67  , m_pMemoryCacheChanges(nullptr)
68  {}
69 
71  explicit SingleCacheChangesT(const MemoryCacheChangesT<TValue>& memoryCacheChanges)
72  : m_pCacheDelta(nullptr)
73  , m_pMemoryCacheChanges(&memoryCacheChanges)
74  {}
75 
76  public:
79  return m_pCacheDelta ? m_pCacheDelta->addedElements() : CollectAllPointers(m_pMemoryCacheChanges->Added);
80  }
81 
84  return m_pCacheDelta ? m_pCacheDelta->modifiedElements() : CollectAllPointers(m_pMemoryCacheChanges->Copied);
85  }
86 
89  return m_pCacheDelta ? m_pCacheDelta->removedElements() : CollectAllPointers(m_pMemoryCacheChanges->Removed);
90  }
91 
92  private:
93  static PointerContainer CollectAllPointers(const std::vector<TValue>& values) {
94  PointerContainer pointers;
95  for (const auto& value : values)
96  pointers.insert(&value);
97 
98  return pointers;
99  }
100 
101  private:
102  const TCacheDelta* m_pCacheDelta;
104  };
105 
106  // endregion
107 
108  // region CacheChanges
109 
111  class CacheChanges : public utils::MoveOnly {
112  public:
114  using MemoryCacheChangesContainer = std::vector<std::unique_ptr<const MemoryCacheChanges>>;
115 
116  public:
118  explicit CacheChanges(const CatapultCacheDelta& cacheDelta) : m_pCacheDelta(&cacheDelta)
119  {}
120 
122  explicit CacheChanges(MemoryCacheChangesContainer&& memoryCacheChangesContainer)
123  : m_pCacheDelta(nullptr)
124  , m_memoryCacheChangesContainer(std::move(memoryCacheChangesContainer))
125  {}
126 
127  public:
129  template<typename TCache>
130  auto sub() const {
132  if (m_pCacheDelta)
133  return SubCacheChanges(m_pCacheDelta->sub<TCache>());
134 
135  using TypedMemoryCacheChanges = MemoryCacheChangesT<typename TCache::CacheValueType>;
136  return SubCacheChanges(*static_cast<const TypedMemoryCacheChanges*>(m_memoryCacheChangesContainer[TCache::Id].get()));
137  }
138 
139  private:
142  };
143 
144  // endregion
145 }}
catapult::cache::MemoryCacheChangesT::Added
std::vector< TValue > Added
Added elements.
Definition: CacheChanges.h:40
catapult::cache::MemoryCacheChanges
Definition: CacheChanges.h:32
catapult::cache::CatapultCacheDelta::sub
const TCache::CacheDeltaType & sub() const
Gets a specific sub cache delta view.
Definition: CatapultCacheDelta.h:47
catapult::cache::CacheChanges::CacheChanges
CacheChanges(MemoryCacheChangesContainer &&memoryCacheChangesContainer)
Creates changes around memoryCacheChangesContainer.
Definition: CacheChanges.h:122
catapult::cache::CatapultCacheDelta
Delta on top of a catapult cache.
Definition: CatapultCacheDelta.h:31
catapult::cache::SingleCacheChangesT
Provides common view of single sub cache changes.
Definition: CacheChanges.h:59
catapult::cache::CacheChanges::sub
auto sub() const
Gets a specific sub cache changes view.
Definition: CacheChanges.h:130
Id
NamespaceId Id
Definition: RootNamespaceHistorySerializer.cpp:100
catapult::cache::CacheChanges
Provides common view of aggregate cache changes.
Definition: CacheChanges.h:111
catapult::cache::CacheChanges::CacheChanges
CacheChanges(const CatapultCacheDelta &cacheDelta)
Creates changes around cacheDelta.
Definition: CacheChanges.h:118
catapult::cache::SingleCacheChanges
Definition: CacheChanges.h:55
CatapultCacheDelta.h
catapult::cache::CacheChanges::m_memoryCacheChangesContainer
MemoryCacheChangesContainer m_memoryCacheChangesContainer
Definition: CacheChanges.h:141
catapult::cache::MemoryCacheChangesT::Copied
std::vector< TValue > Copied
Copied elements.
Definition: CacheChanges.h:46
catapult::cache::CacheChanges::MemoryCacheChangesContainer
std::vector< std::unique_ptr< const MemoryCacheChanges > > MemoryCacheChangesContainer
Memory cache changes container.
Definition: CacheChanges.h:114
catapult::cache::SingleCacheChangesT::m_pCacheDelta
const TCacheDelta * m_pCacheDelta
Definition: CacheChanges.h:102
catapult::cache::MemoryCacheChangesT
Deserialized cache changes for a single cache.
Definition: CacheChanges.h:38
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
catapult::cache::SingleCacheChangesT::PointerContainer
std::unordered_set< const TValue * > PointerContainer
Definition: CacheChanges.h:61
catapult::cache::SingleCacheChangesT::CollectAllPointers
static PointerContainer CollectAllPointers(const std::vector< TValue > &values)
Definition: CacheChanges.h:93
catapult::cache::SingleCacheChangesT::SingleCacheChangesT
SingleCacheChangesT(const TCacheDelta &cacheDelta)
Creates changes around cacheDelta.
Definition: CacheChanges.h:65
catapult::cache::SingleCacheChangesT::addedElements
PointerContainer addedElements() const
Gets pointers to all added elements.
Definition: CacheChanges.h:78
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::MemoryCacheChanges::~MemoryCacheChanges
virtual ~MemoryCacheChanges()=default
catapult::cache::MemoryCacheChangesT::Removed
std::vector< TValue > Removed
Removed elements.
Definition: CacheChanges.h:43
catapult::cache::SingleCacheChangesT::m_pMemoryCacheChanges
const MemoryCacheChangesT< TValue > * m_pMemoryCacheChanges
Definition: CacheChanges.h:103
catapult::cache::SingleCacheChangesT::modifiedElements
PointerContainer modifiedElements() const
Gets pointers to all modified elements.
Definition: CacheChanges.h:83
catapult::utils::NonCopyable
A class that can neither be copied nor moved.
Definition: NonCopyable.h:26
catapult::cache::SingleCacheChangesT::SingleCacheChangesT
SingleCacheChangesT(const MemoryCacheChangesT< TValue > &memoryCacheChanges)
Creates changes around memoryCacheChanges.
Definition: CacheChanges.h:71
catapult::cache::CacheChanges::m_pCacheDelta
const CatapultCacheDelta * m_pCacheDelta
Definition: CacheChanges.h:140
catapult::cache::SingleCacheChangesT::removedElements
PointerContainer removedElements() const
Gets pointers to all removed elements.
Definition: CacheChanges.h:88