CatapultServer  v0.5.0.1 (Elephant)
CacheChangesStorageAdapter.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "CacheChangesSerializer.h"
23 #include "CacheChangesStorage.h"
24 
25 namespace catapult { namespace cache {
26 
28  template<typename TCache, typename TStorageTraits>
30  public:
32  explicit CacheChangesStorageAdapter(TCache& cache) : m_cache(cache)
33  {}
34 
35  public:
36  void saveAll(const CacheChanges& changes, io::OutputStream& output) const override {
37  WriteCacheChanges<TStorageTraits>(changes.sub<TCache>(), output);
38  }
39 
40  std::unique_ptr<const MemoryCacheChanges> loadAll(io::InputStream& input) const override {
41  auto pMemoryCacheChanges = std::make_unique<MemoryCacheChangesT<typename TCache::CacheValueType>>();
42  ReadCacheChanges<TStorageTraits>(input, *pMemoryCacheChanges);
43  return std::move(pMemoryCacheChanges);
44  }
45 
46  void apply(const CacheChanges& changes) const override {
47  auto delta = m_cache.createDelta();
48 
49  auto subCacheChanges = changes.sub<TCache>();
50  for (const auto* pAdded : subCacheChanges.addedElements()) {
51  TStorageTraits::Purge(*pAdded, *delta);
52  TStorageTraits::LoadInto(*pAdded, *delta);
53  }
54 
55  for (const auto* pModified : subCacheChanges.modifiedElements()) {
56  TStorageTraits::Purge(*pModified, *delta);
57  TStorageTraits::LoadInto(*pModified, *delta);
58  }
59 
60  for (const auto* pRemoved : subCacheChanges.removedElements())
61  TStorageTraits::Purge(*pRemoved, *delta);
62 
63  m_cache.commit();
64  }
65 
66  private:
67  TCache& m_cache;
68  };
69 }}
catapult::cache::CacheChangesStorage
Interface for loading and saving cache changes.
Definition: CacheChangesStorage.h:34
catapult::cache::CacheChangesStorageAdapter::apply
void apply(const CacheChanges &changes) const override
Applies cache changes to the underlying cache.
Definition: CacheChangesStorageAdapter.h:46
catapult::cache::CacheChanges::sub
auto sub() const
Gets a specific sub cache changes view.
Definition: CacheChanges.h:130
CacheChangesSerializer.h
CacheChangesStorage.h
catapult::cache::CacheChanges
Provides common view of aggregate cache changes.
Definition: CacheChanges.h:111
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
catapult::cache::CacheChangesStorageAdapter::CacheChangesStorageAdapter
CacheChangesStorageAdapter(TCache &cache)
Creates an adapter around cache.
Definition: CacheChangesStorageAdapter.h:32
catapult::cache::CacheChangesStorageAdapter::loadAll
std::unique_ptr< const MemoryCacheChanges > loadAll(io::InputStream &input) const override
Loads cache changes from input.
Definition: CacheChangesStorageAdapter.h:40
catapult::cache::CacheChangesStorageAdapter::saveAll
void saveAll(const CacheChanges &changes, io::OutputStream &output) const override
Saves cache changes to output.
Definition: CacheChangesStorageAdapter.h:36
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::CacheChangesStorageAdapter
CacheChangesStorage implementation that wraps a cache and associated storage traits.
Definition: CacheChangesStorageAdapter.h:29
catapult::cache::CacheChangesStorageAdapter::m_cache
TCache & m_cache
Definition: CacheChangesStorageAdapter.h:67
catapult::io::OutputStream
Writer interface.
Definition: Stream.h:41