CatapultServer  v0.5.0.1 (Elephant)
RdbTypedColumnContainer.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "KeySerializers.h"
23 #include "RdbColumnContainer.h"
24 #include "RocksDatabase.h"
25 #include "catapult/exceptions.h"
26 #include "catapult/types.h"
27 
28 namespace catapult { namespace cache {
29 
31  template<typename TDescriptor, typename TContainer = RdbColumnContainer>
32  class RdbTypedColumnContainer : public TContainer {
33  public:
34  using KeyType = typename TDescriptor::KeyType;
35  using ValueType = typename TDescriptor::ValueType;
36  using StorageType = typename TDescriptor::StorageType;
37 
38  public:
41  private:
42  using KeyType = typename TDescriptor::KeyType;
43  using ValueType = typename TDescriptor::ValueType;
44  using StorageType = typename TDescriptor::StorageType;
45 
46  public:
48  bool operator==(const const_iterator& rhs) const {
49  return m_iterator == rhs.m_iterator;
50  }
51 
53  bool operator!=(const const_iterator& rhs) const {
54  return !(*this == rhs);
55  }
56 
57  public:
59  const StorageType& operator*() const {
61  CATAPULT_THROW_INVALID_ARGUMENT("dereference on empty iterator");
62 
63  if (!m_pStorage) {
64  auto value = TDescriptor::Serializer::DeserializeValue(m_iterator.buffer());
65  m_pStorage = std::make_unique<StorageType>(TDescriptor::ToStorage(value));
66  }
67 
68  return *m_pStorage;
69  }
70 
72  const StorageType* operator->() const {
73  return &operator*();
74  }
75 
76  public:
78  const RdbDataIterator& dbIterator() const {
79  return m_iterator;
80  }
81 
84  return m_iterator;
85  }
86 
87  private:
89  mutable std::shared_ptr<StorageType> m_pStorage;
90  };
91 
92  public:
94  template<typename TDatabase = RocksDatabase>
95  RdbTypedColumnContainer(TDatabase& database, size_t columnId) : TContainer(database, columnId)
96  {}
97 
98  public:
100  bool empty() const {
101  return 0 == TContainer::size();
102  }
103 
104  public:
105 
106 #if !defined(NDEBUG) && defined(_MSC_VER)
107 #pragma warning(push)
108 #pragma warning(disable:4702) /* "unreachable code" */
109 #endif
110 
112  void insert(const StorageType& element) {
113  TContainer::insert(
114  SerializeKey(TDescriptor::ToKey(element)),
115  TDescriptor::Serializer::SerializeValue(TDescriptor::ToValue(element)));
116  }
117 
118 #if !defined(NDEBUG) && defined(_MSC_VER)
119 #pragma warning(pop)
120 #endif
121 
123  const_iterator find(const KeyType& key) const {
124  const_iterator iter;
125  TContainer::find(SerializeKey(key), iter.dbIterator());
126  return iter;
127  }
128 
130  size_t prune(const KeyType& key) {
131  return TContainer::prune(TDescriptor::Serializer::KeyToBoundary(key));
132  }
133 
135  void remove(const KeyType& key) {
136  TContainer::remove(SerializeKey(key));
137  }
138 
140  const_iterator cend() const {
141  return const_iterator();
142  }
143  };
144 }}
KeySerializers.h
catapult::cache::RdbDataIterator::End
static RdbDataIterator End()
Iterator representing no match.
Definition: RocksDatabase.cpp:52
catapult::cache::RdbTypedColumnContainer::const_iterator::operator!=
bool operator!=(const const_iterator &rhs) const
Returns true if this iterator and rhs are not equal.
Definition: RdbTypedColumnContainer.h:53
exceptions.h
catapult::cache::RdbTypedColumnContainer::const_iterator::KeyType
typename TDescriptor::KeyType KeyType
Definition: RdbTypedColumnContainer.h:42
catapult::cache::RdbTypedColumnContainer::const_iterator::ValueType
typename TDescriptor::ValueType ValueType
Definition: RdbTypedColumnContainer.h:43
catapult::cache::RdbDataIterator::buffer
RawBuffer buffer() const
Returns storage as raw buffer.
Definition: RocksDatabase.cpp:72
catapult::cache::RdbTypedColumnContainer::empty
bool empty() const
Returns true if container is empty.
Definition: RdbTypedColumnContainer.h:100
catapult::cache::RdbTypedColumnContainer::cend
const_iterator cend() const
Returns iterator that represents non-existing element.
Definition: RdbTypedColumnContainer.h:140
catapult::cache::RdbTypedColumnContainer::find
const_iterator find(const KeyType &key) const
Finds element with key. Returns cend() if key has not been found.
Definition: RdbTypedColumnContainer.h:123
catapult::cache::RdbTypedColumnContainer::const_iterator
Typed container iterator that adds descriptor-based deserialization.
Definition: RdbTypedColumnContainer.h:40
catapult::cache::RdbTypedColumnContainer< PatriciaTreeColumnDescriptor >::StorageType
typename PatriciaTreeColumnDescriptor ::StorageType StorageType
Definition: RdbTypedColumnContainer.h:36
catapult::cache::RdbTypedColumnContainer::prune
size_t prune(const KeyType &key)
Prunes elements with keys smaller than key. Returns number of pruned elements.
Definition: RdbTypedColumnContainer.h:130
catapult::cache::RdbDataIterator
Iterator adapter, allowing existence check on keys and data retrieval.
Definition: RocksDatabase.h:40
catapult::cache::RdbTypedColumnContainer::const_iterator::operator*
const StorageType & operator*() const
Returns reference to current element.
Definition: RdbTypedColumnContainer.h:59
RdbColumnContainer.h
catapult::cache::RdbTypedColumnContainer::const_iterator::dbIterator
const RdbDataIterator & dbIterator() const
Returns const reference to underlying db iterator.
Definition: RdbTypedColumnContainer.h:78
catapult::cache::RdbTypedColumnContainer::RdbTypedColumnContainer
RdbTypedColumnContainer(TDatabase &database, size_t columnId)
Creates a container around database and columnId.
Definition: RdbTypedColumnContainer.h:95
catapult::cache::RdbTypedColumnContainer::remove
void remove(const KeyType &key)
Removes element with key.
Definition: RdbTypedColumnContainer.h:135
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::cache::RdbTypedColumnContainer< PatriciaTreeColumnDescriptor >::ValueType
typename PatriciaTreeColumnDescriptor ::ValueType ValueType
Definition: RdbTypedColumnContainer.h:35
catapult::cache::RdbTypedColumnContainer::insert
void insert(const StorageType &element)
Inserts element into container.
Definition: RdbTypedColumnContainer.h:112
catapult::cache::RdbTypedColumnContainer::const_iterator::dbIterator
RdbDataIterator & dbIterator()
Returns reference to underlying db iterator.
Definition: RdbTypedColumnContainer.h:83
catapult::cache::RdbTypedColumnContainer::const_iterator::operator==
bool operator==(const const_iterator &rhs) const
Returns true if this iterator and rhs are equal.
Definition: RdbTypedColumnContainer.h:48
catapult::cache::RdbTypedColumnContainer::const_iterator::m_iterator
RdbDataIterator m_iterator
Definition: RdbTypedColumnContainer.h:88
types.h
catapult::cache::SerializeKey
RawBuffer SerializeKey(const TValue &value)
Serializes value-based key (value).
Definition: KeySerializers.h:31
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
RocksDatabase.h
catapult::cache::RdbTypedColumnContainer< PatriciaTreeColumnDescriptor >::KeyType
typename PatriciaTreeColumnDescriptor ::KeyType KeyType
Definition: RdbTypedColumnContainer.h:34
catapult::cache::RdbTypedColumnContainer::const_iterator::m_pStorage
std::shared_ptr< StorageType > m_pStorage
Definition: RdbTypedColumnContainer.h:89
catapult::cache::RdbTypedColumnContainer::const_iterator::operator->
const StorageType * operator->() const
Returns pointer to current element.
Definition: RdbTypedColumnContainer.h:72
catapult::cache::RdbTypedColumnContainer
Typed container adapter that wraps column.
Definition: RdbTypedColumnContainer.h:32
catapult::cache::RdbTypedColumnContainer::const_iterator::StorageType
typename TDescriptor::StorageType StorageType
Definition: RdbTypedColumnContainer.h:44