CatapultServer  v0.5.0.1 (Elephant)
CacheStorageAdapter.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "CacheStorage.h"
23 #include "CatapultCacheView.h"
24 #include "ChunkedDataLoader.h"
25 #include "catapult/exceptions.h"
26 
27 namespace catapult { namespace cache {
28 
30  template<typename TCache, typename TStorageTraits>
32  public:
34  explicit CacheStorageAdapter(TCache& cache)
35  : m_cache(cache)
36  , m_name(TCache::Name)
37  {}
38 
39  public:
40  const std::string& name() const override {
41  return m_name;
42  }
43 
44  public:
45  void saveAll(const CatapultCacheView& cacheView, io::OutputStream& output) const override {
46  const auto& view = cacheView.sub<TCache>();
47  io::Write64(output, view.size());
48 
49  auto pIterableView = view.tryMakeIterableView();
50  for (const auto& element : *pIterableView)
51  SaveValue(element, output);
52 
53  output.flush();
54  }
55 
56  void saveSummary(const CatapultCacheDelta&, io::OutputStream&) const override {
57  CATAPULT_THROW_INVALID_ARGUMENT("CacheStorageAdapter does not support saveSummary");
58  }
59 
60  void loadAll(io::InputStream& input, size_t batchSize) override {
61  auto delta = m_cache.createDelta();
62 
64  while (loader.hasNext()) {
65  loader.next(batchSize, *delta);
66  m_cache.commit();
67  }
68  }
69 
70  private:
71  // assume pair indicates maps and only forward value to save
72 
73  template<typename T>
74  static void SaveValue(const T& value, io::OutputStream& output) {
75  TStorageTraits::Save(value, output);
76  }
77 
78  template<typename T1, typename T2>
79  static void SaveValue(const std::pair<T1, T2>& pair, io::OutputStream& output) {
80  TStorageTraits::Save(pair.second, output);
81  }
82 
83  private:
84  TCache& m_cache;
85  std::string m_name;
86  };
87 }}
exceptions.h
CacheStorage.h
catapult::io::OutputStream::flush
virtual void flush()=0
catapult::cache::CatapultCacheDelta
Delta on top of a catapult cache.
Definition: CatapultCacheDelta.h:31
catapult::cache::CacheStorageAdapter::m_name
std::string m_name
Definition: CacheStorageAdapter.h:85
catapult::cache::CacheStorageAdapter::m_cache
TCache & m_cache
Definition: CacheStorageAdapter.h:84
catapult::cache::CacheStorageAdapter
A CacheStorage implementation that wraps a cache and associated storage traits.
Definition: CacheStorageAdapter.h:31
ChunkedDataLoader.h
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
catapult::cache::CacheStorage
Interface for loading and saving cache data.
Definition: CacheStorage.h:35
catapult::cache::CacheStorageAdapter::SaveValue
static void SaveValue(const T &value, io::OutputStream &output)
Definition: CacheStorageAdapter.h:74
catapult::cache::CacheStorageAdapter::SaveValue
static void SaveValue(const std::pair< T1, T2 > &pair, io::OutputStream &output)
Definition: CacheStorageAdapter.h:79
catapult::cache::CacheStorageAdapter::loadAll
void loadAll(io::InputStream &input, size_t batchSize) override
Loads cache data from input in batches of batchSize.
Definition: CacheStorageAdapter.h:60
catapult::cache::ChunkedDataLoader::next
void next(uint64_t numRequestedEntries, typename TStorageTraits::DestinationType &destination)
Loads the next data chunk of at most numRequestedEntries into destination.
Definition: ChunkedDataLoader.h:44
CatapultCacheView.h
catapult::cache::CacheStorageAdapter::saveAll
void saveAll(const CatapultCacheView &cacheView, io::OutputStream &output) const override
Saves cache data from cacheView to output.
Definition: CacheStorageAdapter.h:45
Name
static constexpr auto Name
Definition: PtSynchronizer.cpp:31
catapult::io::Write64
void Write64(TIo &output, uint64_t value)
Writes value into output.
Definition: PodIoUtils.h:36
catapult::cache::CatapultCacheView
Locked view on top of the catapult cache.
Definition: CatapultCacheView.h:36
catapult::cache::CatapultCacheView::sub
const TCache::CacheViewType & sub() const
Gets a specific sub cache view.
Definition: CatapultCacheView.h:52
catapult::cache::ChunkedDataLoader
Loads data from an input stream in chunks.
Definition: ChunkedDataLoader.h:30
catapult::cache::ChunkedDataLoader::hasNext
bool hasNext() const
Returns true if there are more entries in the input.
Definition: ChunkedDataLoader.h:39
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::cache::CacheStorageAdapter::saveSummary
void saveSummary(const CatapultCacheDelta &, io::OutputStream &) const override
Saves cache (summary) data from cacheDelta to output.
Definition: CacheStorageAdapter.h:56
catapult::cache::CacheStorageAdapter::name
const std::string & name() const override
Gets the cache name.
Definition: CacheStorageAdapter.h:40
catapult::cache::CacheStorageAdapter::CacheStorageAdapter
CacheStorageAdapter(TCache &cache)
Creates an adapter around cache.
Definition: CacheStorageAdapter.h:34
catapult::io::OutputStream
Writer interface.
Definition: Stream.h:41