CatapultServer  v0.5.0.1 (Elephant)
CacheEntryInfosProducerFactory.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BasicProducer.h"
23 #include "HandlerTypes.h"
27 
28 namespace catapult { namespace handlers {
29 
31  template<typename KeyType>
33 
35  template<typename TCacheDescriptor>
37  private:
38  using KeyType = typename TCacheDescriptor::KeyType;
39  using ValueType = typename TCacheDescriptor::ValueType;
40  using CacheType = typename TCacheDescriptor::CacheType;
41 
42  class Producer : BasicProducer<model::EntityRange<KeyType>> {
43  private:
46 
47  public:
49  : BasicProducer<model::EntityRange<KeyType>>(keys)
50  , m_pView(std::make_shared<ViewType>(std::move(view)))
51  {}
52 
53  public:
54  auto operator()() {
55  return next([&view = **m_pView](const auto& key) {
56  using Serializer = typename TCacheDescriptor::PatriciaTree::Serializer;
57 
58  auto iter = view.find(key);
59  const auto* pEntry = iter.tryGetUnadapted();
60  return pEntry ? MakeInfo(key, Serializer::SerializeValue(*pEntry)) : MakeInfo(key);
61  });
62  }
63 
64  private:
65  static std::shared_ptr<model::CacheEntryInfo<KeyType>> MakeInfo(const KeyType& key) {
66  auto pInfo = std::make_shared<model::CacheEntryInfo<KeyType>>();
67  pInfo->Id = key;
68  pInfo->DataSize = 0;
69  pInfo->Size = static_cast<uint32_t>(model::CacheEntryInfo<KeyType>::CalculateRealSize(*pInfo));
70  return pInfo;
71  }
72 
73  static std::shared_ptr<model::CacheEntryInfo<KeyType>> MakeInfo(const KeyType& key, const std::string& value) {
74  auto size = sizeof(model::CacheEntryInfo<KeyType>) + value.size();
75  auto pInfo = utils::MakeSharedWithSize<model::CacheEntryInfo<KeyType>>(size);
76 
77  pInfo->Id = key;
78 
79  pInfo->DataSize = model::CacheEntryInfo<KeyType>::Max_Data_Size <= value.size()
81  : static_cast<uint32_t>(value.size());
82  pInfo->Size = static_cast<uint32_t>(model::CacheEntryInfo<KeyType>::CalculateRealSize(*pInfo));
83  std::memcpy(pInfo->DataPtr(), value.data(), value.size());
84  return pInfo;
85  }
86 
87  private:
88  std::shared_ptr<ViewType> m_pView;
89  };
90 
91  private:
93  : m_cache(cache)
94  {}
95 
96  public:
99  return CacheEntryInfosProducerFactory(cache);
100  }
101 
102  public:
105  return Producer(m_cache.createView(), ids);
106  }
107 
108  private:
110  };
111 }}
catapult::cache::LockedCacheView
Definition: SynchronizedCache.h:101
catapult::handlers::CacheEntryInfoProducer
supplier< std::shared_ptr< const model::CacheEntryInfo< KeyType > >> CacheEntryInfoProducer
Cache entry info producer.
Definition: CacheEntryInfosProducerFactory.h:32
catapult::handlers::CacheEntryInfosProducerFactory::operator()
CacheEntryInfoProducer< KeyType > operator()(const model::EntityRange< KeyType > &ids) const
Creates a cache entry infos producer for a range of keys (ids).
Definition: CacheEntryInfosProducerFactory.h:104
MemoryUtils.h
catapult::handlers::CacheEntryInfosProducerFactory::Producer::operator()
auto operator()()
Definition: CacheEntryInfosProducerFactory.h:54
catapult::handlers::CacheEntryInfosProducerFactory::Producer::MakeInfo
static std::shared_ptr< model::CacheEntryInfo< KeyType > > MakeInfo(const KeyType &key, const std::string &value)
Definition: CacheEntryInfosProducerFactory.h:73
catapult::model::TrailingVariableDataLayout::Size
uint32_t Size
Data size.
Definition: TrailingVariableDataLayout.h:34
SynchronizedCache.h
catapult::handlers::CacheEntryInfosProducerFactory::CacheEntryInfosProducerFactory
CacheEntryInfosProducerFactory(const CacheType &cache)
Definition: CacheEntryInfosProducerFactory.h:92
catapult::model::EntityRange< KeyType >
BasicProducer.h
catapult::handlers::CacheEntryInfosProducerFactory::ValueType
typename TCacheDescriptor::ValueType ValueType
Definition: CacheEntryInfosProducerFactory.h:39
catapult::supplier
std::function< T()> supplier
A (stateless) supplier function.
Definition: functions.h:39
catapult::handlers::CacheEntryInfosProducerFactory::KeyType
typename TCacheDescriptor::KeyType KeyType
Definition: CacheEntryInfosProducerFactory.h:38
catapult::handlers::CacheEntryInfosProducerFactory
Cache entry infos producer factory.
Definition: CacheEntryInfosProducerFactory.h:36
catapult::handlers::CacheEntryInfosProducerFactory::Create
static CacheEntryInfosProducerFactory Create(const CacheType &cache)
Creates a cache entry infos producer factory around cache.
Definition: CacheEntryInfosProducerFactory.h:98
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::handlers::CacheEntryInfosProducerFactory::m_cache
const CacheType & m_cache
Definition: CacheEntryInfosProducerFactory.h:109
catapult::handlers::CacheEntryInfosProducerFactory::Producer::m_pView
std::shared_ptr< ViewType > m_pView
Definition: CacheEntryInfosProducerFactory.h:88
catapult::handlers::CacheEntryInfosProducerFactory::Producer
Definition: CacheEntryInfosProducerFactory.h:42
catapult
Definition: AddressExtractionExtension.cpp:28
HandlerTypes.h
CacheEntryInfo.h
catapult::handlers::CacheEntryInfosProducerFactory::Producer::MakeInfo
static std::shared_ptr< model::CacheEntryInfo< KeyType > > MakeInfo(const KeyType &key)
Definition: CacheEntryInfosProducerFactory.h:65
catapult::handlers::CacheEntryInfosProducerFactory::CacheType
typename TCacheDescriptor::CacheType CacheType
Definition: CacheEntryInfosProducerFactory.h:40
catapult::handlers::CacheEntryInfosProducerFactory::Producer::Producer
Producer(ViewType &&view, const model::EntityRange< KeyType > &keys)
Definition: CacheEntryInfosProducerFactory.h:48
catapult::handlers::BasicProducer< model::EntityRange< KeyType > >::next
auto next(TConverter convert)
Produces the next entity and calls convert for any required adaptation.
Definition: BasicProducer.h:38
catapult::model::CacheEntryInfo
Cache entry info.
Definition: CacheEntryInfo.h:30
catapult::handlers::BasicProducer
A base class for producers.
Definition: BasicProducer.h:27