CatapultServer  v0.5.0.1 (Elephant)
BasePatriciaTree.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BasePatriciaTreeDelta.h"
24 #include "catapult/exceptions.h"
25 
26 namespace catapult { namespace tree {
27 
29  template<typename TEncoder, typename TDataSource, typename THasher = std::hash<typename TEncoder::KeyType>>
31  public:
32  using KeyType = typename TEncoder::KeyType;
33  using ValueType = typename TEncoder::ValueType;
35 
36  public:
38  explicit BasePatriciaTree(TDataSource& dataSource)
39  : m_dataSource(dataSource)
41  {}
42 
44  BasePatriciaTree(TDataSource& dataSource, const Hash256& rootHash) : BasePatriciaTree(dataSource) {
45  if (!m_tree.tryLoad(rootHash))
46  CATAPULT_THROW_RUNTIME_ERROR_1("unable to load tree with root hash", rootHash);
47  }
48 
49  public:
51  Hash256 root() const {
52  return m_tree.root();
53  }
54 
56  std::pair<Hash256, bool> lookup(const KeyType& key, std::vector<TreeNode>& nodePath) const {
57  return m_tree.lookup(key, nodePath);
58  }
59 
60  public:
62  std::shared_ptr<DeltaType> rebase() {
63  if (m_pWeakDelta.lock())
64  CATAPULT_THROW_RUNTIME_ERROR("only a single attached delta is allowed at a time");
65 
66  auto pDelta = std::make_shared<DeltaType>(m_dataSource, root());
67  m_pWeakDelta = pDelta;
68  return pDelta;
69  }
70 
73  std::shared_ptr<DeltaType> rebaseDetached() const {
74  return std::make_shared<DeltaType>(m_dataSource, root());
75  }
76 
77  public:
79  void commit() {
80  auto pDelta = m_pWeakDelta.lock();
81  if (!pDelta)
82  CATAPULT_THROW_RUNTIME_ERROR("attempting to commit changes to a tree without any outstanding attached deltas");
83 
84  // copy all pending changes directly into the data source, update the root hash and reset the delta
85  pDelta->setCheckpoint(); // commit should always create a checkpoint
86  pDelta->copyPendingChangesTo(m_dataSource);
87  pDelta->copyRootTo(m_tree); // cannot lookup in m_dataSource directly because of delayed write data sources
88  pDelta->reset(pDelta->root());
89  }
90 
91  private:
92  TDataSource& m_dataSource;
94  std::weak_ptr<DeltaType> m_pWeakDelta;
95  };
96 }}
catapult::tree::BasePatriciaTree::BasePatriciaTree
BasePatriciaTree(TDataSource &dataSource, const Hash256 &rootHash)
Creates a tree around a dataSource with specified root hash (rootHash).
Definition: BasePatriciaTree.h:44
exceptions.h
catapult::tree::PatriciaTree
Represents a compact patricia tree.
Definition: PatriciaTree.h:28
catapult::tree::BasePatriciaTree::m_pWeakDelta
std::weak_ptr< DeltaType > m_pWeakDelta
Definition: BasePatriciaTree.h:94
catapult::tree::BasePatriciaTree
A base patricia tree.
Definition: BasePatriciaTree.h:30
catapult::tree::BasePatriciaTree::rebase
std::shared_ptr< DeltaType > rebase()
Returns a delta based on the same data source as this tree.
Definition: BasePatriciaTree.h:62
catapult::tree::BasePatriciaTree::ValueType
typename TEncoder::ValueType ValueType
Definition: BasePatriciaTree.h:33
catapult::tree::BasePatriciaTreeDelta
A delta on top of a base patricia tree that offers methods to set/unset nodes.
Definition: BasePatriciaTreeDelta.h:33
BasePatriciaTreeDelta.h
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
HexFormatter.h
catapult::tree::BasePatriciaTree::lookup
std::pair< Hash256, bool > lookup(const KeyType &key, std::vector< TreeNode > &nodePath) const
Tries to find the value associated with key in the tree and stores proof of existence or not in nodeP...
Definition: BasePatriciaTree.h:56
CATAPULT_THROW_RUNTIME_ERROR
#define CATAPULT_THROW_RUNTIME_ERROR(MESSAGE)
Macro used to throw a catapult runtime error.
Definition: exceptions.h:167
catapult::tree::BasePatriciaTree::BasePatriciaTree
BasePatriciaTree(TDataSource &dataSource)
Creates a tree around a dataSource.
Definition: BasePatriciaTree.h:38
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::tree::BasePatriciaTree::rebaseDetached
std::shared_ptr< DeltaType > rebaseDetached() const
Definition: BasePatriciaTree.h:73
catapult::tree::BasePatriciaTree::m_dataSource
TDataSource & m_dataSource
Definition: BasePatriciaTree.h:92
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::tree::BasePatriciaTree::commit
void commit()
Commits all changes in the rebased tree.
Definition: BasePatriciaTree.h:79
catapult::tree::BasePatriciaTree::KeyType
typename TEncoder::KeyType KeyType
Definition: BasePatriciaTree.h:32
catapult::tree::BasePatriciaTree::m_tree
PatriciaTree< TEncoder, TDataSource > m_tree
Definition: BasePatriciaTree.h:93
catapult::tree::BasePatriciaTree::root
Hash256 root() const
Gets the root hash that uniquely identifies this tree.
Definition: BasePatriciaTree.h:51