CatapultServer  v0.5.0.1 (Elephant)
CachePatriciaTree.h
Go to the documentation of this file.
1 
21 #pragma once
24 #include <memory>
25 
26 namespace catapult { namespace cache {
27 
29  template<typename TTree>
31  public:
33  CachePatriciaTree(bool enable, CacheDatabase& database, size_t columnId)
34  : m_pImpl(enable ? std::make_unique<Impl>(database, columnId) : nullptr)
35  {}
36 
37  public:
39  const auto* get() const {
40  return m_pImpl ? &m_pImpl->tree() : nullptr;
41  }
42 
43  public:
45  auto rebase() {
46  return m_pImpl ? m_pImpl->tree().rebase() : nullptr;
47  }
48 
51  auto rebaseDetached() const {
52  return m_pImpl ? m_pImpl->tree().rebaseDetached() : nullptr;
53  }
54 
56  void commit() {
57  if (m_pImpl)
58  m_pImpl->commit();
59  }
60 
61  private:
62  class Impl {
63  public:
64  Impl(CacheDatabase& database, size_t columnId)
65  : m_container(database, columnId)
67  , m_pTree(std::make_unique<TTree>(m_dataSource)) {
68  Hash256 rootHash;
69  if (!m_container.prop("root", rootHash))
70  return;
71 
72  if (Hash256() == rootHash)
73  return;
74 
75  m_pTree = std::make_unique<TTree>(m_dataSource, rootHash);
76  }
77 
78  public:
79  const auto& tree() const {
80  return *m_pTree;
81  }
82 
83  auto& tree() {
84  return *m_pTree;
85  }
86 
87  public:
88  void commit() {
89  m_pTree->commit();
90 
91  // skip setProp if hash did not change
92  Hash256 rootHash;
93  if (m_container.prop("root", rootHash) && rootHash == m_pTree->root())
94  return;
95 
96  m_container.setProp("root", m_pTree->root());
97  }
98 
99  private:
102  std::unique_ptr<TTree> m_pTree;
103  };
104 
105  std::unique_ptr<Impl> m_pImpl;
106  };
107 }}
catapult::cache::CachePatriciaTree::CachePatriciaTree
CachePatriciaTree(bool enable, CacheDatabase &database, size_t columnId)
Creates a tree around database and columnId if enable is true.
Definition: CachePatriciaTree.h:33
catapult::cache::RdbColumnContainer::prop
bool prop(const std::string &propertyName, TValue &value) const
Definition: RdbColumnContainer.h:51
catapult::cache::CachePatriciaTree::Impl::m_dataSource
PatriciaTreeRdbDataSource m_dataSource
Definition: CachePatriciaTree.h:101
catapult::Hash256
utils::ByteArray< Hash256_Size, Hash256_tag > Hash256
Definition: src/catapult/types.h:47
catapult::cache::CachePatriciaTree::Impl::tree
const auto & tree() const
Definition: CachePatriciaTree.h:79
catapult::cache::CachePatriciaTree::Impl::m_container
PatriciaTreeContainer m_container
Definition: CachePatriciaTree.h:100
catapult::cache::PatriciaTreeRdbDataSource
Patricia tree rocksdb-based data source.
Definition: PatriciaTreeRdbDataSource.h:28
catapult::cache::CachePatriciaTree::Impl::tree
auto & tree()
Definition: CachePatriciaTree.h:83
catapult::cache::RdbColumnContainer::setProp
void setProp(const std::string &propertyName, const TValue &value)
Sets property value of a column (propertyName) to value.
Definition: RdbColumnContainer.h:66
catapult::cache::CachePatriciaTree::get
const auto * get() const
Gets a pointer to the underlying tree if enabled.
Definition: CachePatriciaTree.h:39
CacheDatabase.h
catapult::cache::CachePatriciaTree::commit
void commit()
Commits all changes in the rebased tree.
Definition: CachePatriciaTree.h:56
catapult::cache::CachePatriciaTree::rebaseDetached
auto rebaseDetached() const
Definition: CachePatriciaTree.h:51
catapult::cache::CachePatriciaTree::Impl::m_pTree
std::unique_ptr< TTree > m_pTree
Definition: CachePatriciaTree.h:102
catapult::cache::CachePatriciaTree::Impl
Definition: CachePatriciaTree.h:62
catapult::cache::RocksDatabase
RocksDb-backed database.
Definition: RocksDatabase.h:116
catapult::cache::CachePatriciaTree::rebase
auto rebase()
Returns a delta based on the same data source as this tree.
Definition: CachePatriciaTree.h:45
catapult::cache::CachePatriciaTree::Impl::commit
void commit()
Definition: CachePatriciaTree.h:88
PatriciaTreeRdbDataSource.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::cache::CachePatriciaTree::Impl::Impl
Impl(CacheDatabase &database, size_t columnId)
Definition: CachePatriciaTree.h:64
catapult::cache::RdbTypedColumnContainer< PatriciaTreeColumnDescriptor >
catapult::cache::CachePatriciaTree
A wrapper around a patricia tree used by caches.
Definition: CachePatriciaTree.h:30
catapult::cache::CachePatriciaTree::m_pImpl
std::unique_ptr< Impl > m_pImpl
Definition: CachePatriciaTree.h:105