CatapultServer  v0.5.0.1 (Elephant)
SubCachePluginAdapter.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include "CacheStorageAdapter.h"
24 #include "SubCachePlugin.h"
25 #include <memory>
26 #include <sstream>
27 
28 namespace catapult { namespace cache {
29 
31  template<typename TCache, typename TStorageTraits>
33  public:
35  explicit SubCachePluginAdapter(std::unique_ptr<TCache>&& pCache) : m_pCache(std::move(pCache)) {
36  std::ostringstream out;
37  out << TCache::Name << " (id = " << TCache::Id << ")";
38  m_name = out.str();
39  }
40 
41  public:
42  const std::string& name() const override {
43  return m_name;
44  }
45 
46  size_t id() const override {
47  return TCache::Id;
48  }
49 
50  public:
51  std::unique_ptr<const SubCacheView> createView() const override {
52  return std::make_unique<const SubCacheViewAdapter<decltype(m_pCache->createView())>>(
53  m_pCache->createView(),
55  }
56 
57  std::unique_ptr<SubCacheView> createDelta() override {
58  return std::make_unique<SubCacheViewAdapter<decltype(m_pCache->createDelta())>>(
59  m_pCache->createDelta(),
61  }
62 
63  std::unique_ptr<DetachedSubCacheView> createDetachedDelta() const override {
64  return std::make_unique<DetachedSubCacheViewAdapter<decltype(m_pCache->createDetachedDelta())>>(
65  m_pCache->createDetachedDelta(),
67  }
68 
69  void commit() override {
70  m_pCache->commit();
71  }
72 
73  public:
74  const void* get() const override {
75  return m_pCache.get();
76  }
77 
78  public:
79  std::unique_ptr<CacheStorage> createStorage() override {
81  ? std::make_unique<CacheStorageAdapter<TCache, TStorageTraits>>(*m_pCache)
82  : nullptr;
83  }
84 
85  std::unique_ptr<CacheChangesStorage> createChangesStorage() const override {
86  return std::make_unique<CacheChangesStorageAdapter<TCache, TStorageTraits>>(*m_pCache);
87  }
88 
89  public:
91  TCache& cache() {
92  return *m_pCache;
93  }
94 
95  private:
97  SubCacheViewIdentifier viewIdentifier{};
98  std::memcpy(viewIdentifier.CacheName.data(), name().data(), viewIdentifier.CacheName.size());
99  viewIdentifier.CacheId = id();
100  viewIdentifier.ViewType = viewType;
101  return viewIdentifier;
102  }
103 
104  private:
105  static bool IsCacheStorageSupported(const TCache& cache) {
106  return !!cache.createView()->tryMakeIterableView();
107  }
108 
109  private:
110  // region SubCacheViewAdapter
111 
112  template<typename TView>
114  public:
115  explicit SubCacheViewAdapter(TView&& view, const SubCacheViewIdentifier& id)
116  : m_view(std::move(view))
117  , m_id(id)
118  {}
119 
120  private:
121  auto merkleRootAccessor() const {
122  // need to dereference to get underlying view type from LockedCacheView
123  using UnderlyingViewType = std::remove_reference_t<decltype(*m_view)>;
125  }
126 
128  // need to dereference to get underlying view type from LockedCacheView
129  using UnderlyingViewType = std::remove_reference_t<decltype(*m_view)>;
131  }
132 
133  public:
134  const SubCacheViewIdentifier& id() const override {
135  return m_id;
136  }
137 
138  const void* get() const override {
139  return &*m_view;
140  }
141 
142  void* get() override {
143  return &*m_view;
144  }
145 
146  bool supportsMerkleRoot() const override {
148  }
149 
150  bool tryGetMerkleRoot(Hash256& merkleRoot) const override {
151  return TryGetMerkleRoot(m_view, merkleRoot, merkleRootAccessor());
152  }
153 
154  bool trySetMerkleRoot(const Hash256& merkleRoot) override {
155  return TrySetMerkleRoot(m_view, merkleRoot, merkleRootMutator());
156  }
157 
158  void updateMerkleRoot(Height height) override {
160  }
161 
162  const void* asReadOnly() const override {
163  return &m_view->asReadOnly();
164  }
165 
166  private:
168  using UnsupportedMerkleRootFlag = std::integral_constant<MerkleRootType, MerkleRootType::Unsupported>;
169  using SupportedMerkleRootFlag = std::integral_constant<MerkleRootType, MerkleRootType::Supported>;
170 
171  template<typename T, typename = void>
173 
174  template<typename T>
176  T,
177  utils::traits::is_type_expression_t<decltype(reinterpret_cast<const T*>(0)->tryGetMerkleRoot())>>
178  : public SupportedMerkleRootFlag
179  {};
180 
181  template<typename T, typename = void>
183 
184  template<typename T>
186  T,
187  utils::traits::is_type_expression_t<decltype(reinterpret_cast<T*>(0)->setMerkleRoot(Hash256()))>>
188  : public SupportedMerkleRootFlag
189  {};
190 
191  static bool SupportsMerkleRoot(const TView&, UnsupportedMerkleRootFlag) {
192  return false;
193  }
194 
195  static bool SupportsMerkleRoot(const TView& view, SupportedMerkleRootFlag) {
196  return view->supportsMerkleRoot();
197  }
198 
199  static bool TryGetMerkleRoot(const TView&, Hash256&, UnsupportedMerkleRootFlag) {
200  return false;
201  }
202 
203  static bool TryGetMerkleRoot(const TView& view, Hash256& merkleRoot, SupportedMerkleRootFlag) {
204  auto result = view->tryGetMerkleRoot();
205  merkleRoot = result.first;
206  return result.second;
207  }
208 
209  static bool TrySetMerkleRoot(TView&, const Hash256&, UnsupportedMerkleRootFlag) {
210  return false;
211  }
212 
213  static bool TrySetMerkleRoot(TView& view, const Hash256& merkleRoot, SupportedMerkleRootFlag) {
214  if (!view->supportsMerkleRoot())
215  return false;
216 
217  view->setMerkleRoot(merkleRoot);
218  return true;
219  }
220 
222  {}
223 
224  static void UpdateMerkleRoot(TView& view, Height height, SupportedMerkleRootFlag) {
225  view->updateMerkleRoot(height);
226  }
227 
228  private:
229  TView m_view;
231  };
232 
233  // endregion
234 
235  // region DetachedSubCacheViewAdapter
236 
237  template<typename TLockableCacheDelta>
239  public:
240  explicit DetachedSubCacheViewAdapter(TLockableCacheDelta&& lockableCacheDelta, const SubCacheViewIdentifier& id)
241  : m_lockableCacheDelta(std::move(lockableCacheDelta))
242  , m_id(id)
243  {}
244 
245  public:
246  std::unique_ptr<SubCacheView> tryLock() {
247  auto delta = m_lockableCacheDelta.tryLock();
248  return delta ? std::make_unique<SubCacheViewAdapter<decltype(delta)>>(std::move(delta), m_id) : nullptr;
249  }
250 
251  private:
252  TLockableCacheDelta m_lockableCacheDelta;
254  };
255 
256  // endregion
257 
258  private:
259  std::unique_ptr<TCache> m_pCache;
260  std::string m_name;
261  };
262 }}
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::MerkleRootAccessor
Definition: SubCachePluginAdapter.h:172
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::TrySetMerkleRoot
static bool TrySetMerkleRoot(TView &view, const Hash256 &merkleRoot, SupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:213
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::m_view
TView m_view
Definition: SubCachePluginAdapter.h:229
catapult::cache::SubCachePluginAdapter::id
size_t id() const override
Gets the cache id.
Definition: SubCachePluginAdapter.h:46
catapult::cache::SubCacheViewType
SubCacheViewType
Sub cache view types.
Definition: SubCachePlugin.h:39
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::UpdateMerkleRoot
static void UpdateMerkleRoot(TView &, Height, UnsupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:221
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::TryGetMerkleRoot
static bool TryGetMerkleRoot(const TView &, Hash256 &, UnsupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:199
catapult::cache::SubCachePluginAdapter::createStorage
std::unique_ptr< CacheStorage > createStorage() override
Returns a cache storage based on this cache.
Definition: SubCachePluginAdapter.h:79
CacheStorageAdapter.h
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::supportsMerkleRoot
bool supportsMerkleRoot() const override
Returns true if cache supports merkle root.
Definition: SubCachePluginAdapter.h:146
catapult::cache::SubCacheViewIdentifier
Sub cache view identifier.
Definition: SubCachePlugin.h:51
catapult::cache::SubCachePluginAdapter::createChangesStorage
std::unique_ptr< CacheChangesStorage > createChangesStorage() const override
Returns a cache changes storage based on this cache.
Definition: SubCachePluginAdapter.h:85
catapult::cache::SubCachePluginAdapter::DetachedSubCacheViewAdapter::m_lockableCacheDelta
TLockableCacheDelta m_lockableCacheDelta
Definition: SubCachePluginAdapter.h:252
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::merkleRootMutator
auto merkleRootMutator()
Definition: SubCachePluginAdapter.h:127
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::UnsupportedMerkleRootFlag
std::integral_constant< MerkleRootType, MerkleRootType::Unsupported > UnsupportedMerkleRootFlag
Definition: SubCachePluginAdapter.h:168
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::SupportsMerkleRoot
static bool SupportsMerkleRoot(const TView &, UnsupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:191
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter
Definition: SubCachePluginAdapter.h:113
CacheChangesStorageAdapter.h
catapult::cache::SubCachePluginAdapter::DetachedSubCacheViewAdapter::tryLock
std::unique_ptr< SubCacheView > tryLock()
Definition: SubCachePluginAdapter.h:246
catapult::cache::SubCachePluginAdapter::IsCacheStorageSupported
static bool IsCacheStorageSupported(const TCache &cache)
Definition: SubCachePluginAdapter.h:105
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::id
const SubCacheViewIdentifier & id() const override
Gets view identifier.
Definition: SubCachePluginAdapter.h:134
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::get
void * get() override
Returns a pointer to the underlying view.
Definition: SubCachePluginAdapter.h:142
catapult::cache::SubCachePluginAdapter::DetachedSubCacheViewAdapter::m_id
SubCacheViewIdentifier m_id
Definition: SubCachePluginAdapter.h:253
catapult::cache::DetachedSubCacheView
Detached sub cache view.
Definition: SubCachePlugin.h:99
Id
NamespaceId Id
Definition: RootNamespaceHistorySerializer.cpp:100
catapult::cache::SubCachePluginAdapter::cache
TCache & cache()
Gets a typed reference to the underlying cache.
Definition: SubCachePluginAdapter.h:91
catapult::utils::BaseValue< uint64_t, Height_tag >
catapult::cache::SubCachePluginAdapter::get
const void * get() const override
Returns a const pointer to the underlying cache.
Definition: SubCachePluginAdapter.h:74
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::SubCacheViewAdapter
SubCacheViewAdapter(TView &&view, const SubCacheViewIdentifier &id)
Definition: SubCachePluginAdapter.h:115
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::get
const void * get() const override
Returns a const pointer to the underlying view.
Definition: SubCachePluginAdapter.h:138
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::MerkleRootType::Supported
catapult::cache::SubCachePluginAdapter::makeSubCacheViewIdentifier
auto makeSubCacheViewIdentifier(SubCacheViewType viewType) const
Definition: SubCachePluginAdapter.h:96
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::SupportsMerkleRoot
static bool SupportsMerkleRoot(const TView &view, SupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:195
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::trySetMerkleRoot
bool trySetMerkleRoot(const Hash256 &merkleRoot) override
Sets the cache merkle root (merkleRoot) if supported.
Definition: SubCachePluginAdapter.h:154
catapult::cache::SubCacheViewIdentifier::CacheId
size_t CacheId
Cache id.
Definition: SubCachePlugin.h:57
catapult::cache::SubCacheViewType::DetachedDelta
Cache detached delta.
catapult::cache::SubCachePluginAdapter::DetachedSubCacheViewAdapter
Definition: SubCachePluginAdapter.h:238
catapult::cache::SubCachePluginAdapter::name
const std::string & name() const override
Gets the cache name.
Definition: SubCachePluginAdapter.h:42
catapult::cache::SubCachePluginAdapter::m_name
std::string m_name
Definition: SubCachePluginAdapter.h:260
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::merkleRootAccessor
auto merkleRootAccessor() const
Definition: SubCachePluginAdapter.h:121
catapult::cache::SubCacheViewType::View
Cache view.
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::asReadOnly
const void * asReadOnly() const override
Returns a read-only view of this view.
Definition: SubCachePluginAdapter.h:162
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::m_id
SubCacheViewIdentifier m_id
Definition: SubCachePluginAdapter.h:230
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::tryGetMerkleRoot
bool tryGetMerkleRoot(Hash256 &merkleRoot) const override
Gets the cache merkle root (merkleRoot) if supported.
Definition: SubCachePluginAdapter.h:150
catapult::cache::SubCachePluginAdapter::commit
void commit() override
Commits all pending changes to the underlying storage.
Definition: SubCachePluginAdapter.h:69
catapult::cache::SubCacheView
Sub cache view.
Definition: SubCachePlugin.h:68
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
catapult::cache::SubCachePlugin
Sub cache plugin that can be added to the main catapult cache.
Definition: SubCachePlugin.h:114
Name
static constexpr auto Name
Definition: PtSynchronizer.cpp:31
catapult::cache::SubCachePluginAdapter::SubCachePluginAdapter
SubCachePluginAdapter(std::unique_ptr< TCache > &&pCache)
Creates an adapter around pCache.
Definition: SubCachePluginAdapter.h:35
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::TryGetMerkleRoot
static bool TryGetMerkleRoot(const TView &view, Hash256 &merkleRoot, SupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:203
catapult::cache::SubCachePluginAdapter::createDetachedDelta
std::unique_ptr< DetachedSubCacheView > createDetachedDelta() const override
Definition: SubCachePluginAdapter.h:63
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::UpdateMerkleRoot
static void UpdateMerkleRoot(TView &view, Height height, SupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:224
catapult::cache::SubCachePluginAdapter::createView
std::unique_ptr< const SubCacheView > createView() const override
Returns a locked cache view based on this cache.
Definition: SubCachePluginAdapter.h:51
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::TrySetMerkleRoot
static bool TrySetMerkleRoot(TView &, const Hash256 &, UnsupportedMerkleRootFlag)
Definition: SubCachePluginAdapter.h:209
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::MerkleRootMutator
Definition: SubCachePluginAdapter.h:182
catapult::cache::SubCachePluginAdapter::createDelta
std::unique_ptr< SubCacheView > createDelta() override
Definition: SubCachePluginAdapter.h:57
SubCachePlugin.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::SubCachePluginAdapter::DetachedSubCacheViewAdapter::DetachedSubCacheViewAdapter
DetachedSubCacheViewAdapter(TLockableCacheDelta &&lockableCacheDelta, const SubCacheViewIdentifier &id)
Definition: SubCachePluginAdapter.h:240
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::MerkleRootType::Unsupported
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::updateMerkleRoot
void updateMerkleRoot(Height height) override
Recalculates the merkle root given the specified chain height if supported.
Definition: SubCachePluginAdapter.h:158
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::cache::SubCacheViewType::Delta
Cache delta.
catapult::cache::SubCachePluginAdapter::m_pCache
std::unique_ptr< TCache > m_pCache
Definition: SubCachePluginAdapter.h:259
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::MerkleRootType
MerkleRootType
Definition: SubCachePluginAdapter.h:167
catapult::cache::SubCachePluginAdapter
A SubCachePlugin implementation that wraps a SynchronizedCache.
Definition: SubCachePluginAdapter.h:32
catapult::cache::SubCachePluginAdapter::SubCacheViewAdapter::SupportedMerkleRootFlag
std::integral_constant< MerkleRootType, MerkleRootType::Supported > SupportedMerkleRootFlag
Definition: SubCachePluginAdapter.h:169