CatapultServer  v0.5.0.1 (Elephant)
BasePatriciaTreeDelta.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "PatriciaTree.h"
25 #include "catapult/exceptions.h"
26 #include <unordered_map>
27 #include <unordered_set>
28 
29 namespace catapult { namespace tree {
30 
32  template<typename TEncoder, typename TDataSource, typename THasher>
34  private:
35  using KeyType = typename TEncoder::KeyType;
36  using ValueType = typename TEncoder::ValueType;
37 
38  public:
40  explicit BasePatriciaTreeDelta(const TDataSource& dataSource, const Hash256& rootHash)
41  : m_dataSource(dataSource)
42  , m_baseRootHash(rootHash)
43  , m_tree(m_dataSource) {
44  m_tree.tryLoad(rootHash);
45  }
46 
47  public:
49  Hash256 root() const {
50  return m_tree.root();
51  }
52 
54  Hash256 baseRoot() const {
55  return m_baseRootHash;
56  }
57 
58  public:
60  void reset(const Hash256& rootHash) {
61  if (Hash256() == rootHash)
62  m_tree.clear();
63  else if (!m_tree.tryLoad(rootHash))
64  CATAPULT_THROW_RUNTIME_ERROR_1("unable to load root hash", rootHash);
65 
66  m_baseRootHash = rootHash;
67  }
68 
70  void set(const KeyType& key, const ValueType& value) {
71  return m_tree.set(key, value);
72  }
73 
75  bool unset(const KeyType& key) {
76  return m_tree.unset(key);
77  }
78 
79  public:
81  void setCheckpoint() {
82  m_tree.saveAll();
83  }
84 
85  public:
87  template<typename TDestinationDataSource>
88  void copyPendingChangesTo(TDestinationDataSource& dataSource) const {
89  m_dataSource.forEach([&dataSource](const auto& node) {
90  if (node.isLeaf())
91  dataSource.set(node.asLeafNode());
92  else
93  dataSource.set(node.asBranchNode());
94  });
95  }
96 
98  template<typename TTree>
99  void copyRootTo(TTree& tree) const {
100  auto rootHash = root();
101  if (Hash256() == rootHash)
102  tree.clear();
103  else
104  tree.setRoot(*m_dataSource.get(rootHash));
105  }
106 
107  private:
111  };
112 }}
ReadThroughMemoryDataSource.h
catapult::tree::ReadThroughMemoryDataSource< TDataSource >
exceptions.h
catapult::tree::PatriciaTree
Represents a compact patricia tree.
Definition: PatriciaTree.h:28
catapult::Hash256
utils::ByteArray< Hash256_Size, Hash256_tag > Hash256
Definition: src/catapult/types.h:47
catapult::tree::BasePatriciaTreeDelta::setCheckpoint
void setCheckpoint()
Marks all nodes reachable at this point.
Definition: BasePatriciaTreeDelta.h:81
catapult::tree::BasePatriciaTreeDelta::copyRootTo
void copyRootTo(TTree &tree) const
Sets the root of tree to the root of this delta tree.
Definition: BasePatriciaTreeDelta.h:99
catapult::tree::ReadThroughMemoryDataSource::forEach
void forEach(const consumer< const TreeNode & > &consumer) const
Gets all nodes in memory and passes them to consumer.
Definition: ReadThroughMemoryDataSource.h:52
catapult::tree::BasePatriciaTreeDelta::m_dataSource
ReadThroughMemoryDataSource< TDataSource > m_dataSource
Definition: BasePatriciaTreeDelta.h:108
catapult::tree::BasePatriciaTreeDelta::copyPendingChangesTo
void copyPendingChangesTo(TDestinationDataSource &dataSource) const
Copies all pending changes to dataSource.
Definition: BasePatriciaTreeDelta.h:88
catapult::tree::BasePatriciaTreeDelta::m_tree
PatriciaTree< TEncoder, ReadThroughMemoryDataSource< TDataSource > > m_tree
Definition: BasePatriciaTreeDelta.h:110
catapult::tree::BasePatriciaTreeDelta::set
void set(const KeyType &key, const ValueType &value)
Sets key to value in the tree.
Definition: BasePatriciaTreeDelta.h:70
catapult::tree::BasePatriciaTreeDelta::unset
bool unset(const KeyType &key)
Removes the value associated with key from the tree.
Definition: BasePatriciaTreeDelta.h:75
catapult::tree::BasePatriciaTreeDelta::root
Hash256 root() const
Gets the root hash that uniquely identifies this tree.
Definition: BasePatriciaTreeDelta.h:49
catapult::tree::BasePatriciaTreeDelta::ValueType
typename TEncoder::ValueType ValueType
Definition: BasePatriciaTreeDelta.h:36
catapult::tree::BasePatriciaTreeDelta::reset
void reset(const Hash256 &rootHash)
Sets the root hash (rootHash).
Definition: BasePatriciaTreeDelta.h:60
PatriciaTree.h
catapult::tree::BasePatriciaTreeDelta::m_baseRootHash
Hash256 m_baseRootHash
Definition: BasePatriciaTreeDelta.h:109
catapult::tree::ReadThroughMemoryDataSource::get
std::unique_ptr< const TreeNode > get(const Hash256 &hash) const
Gets the tree node associated with hash.
Definition: ReadThroughMemoryDataSource.h:46
catapult::tree::BasePatriciaTreeDelta
A delta on top of a base patricia tree that offers methods to set/unset nodes.
Definition: BasePatriciaTreeDelta.h:33
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::BasePatriciaTreeDelta::baseRoot
Hash256 baseRoot() const
Gets the base root hash that identifies this tree before any changes are applied.
Definition: BasePatriciaTreeDelta.h:54
catapult::tree::BasePatriciaTreeDelta::BasePatriciaTreeDelta
BasePatriciaTreeDelta(const TDataSource &dataSource, const Hash256 &rootHash)
Creates a tree around a dataSource with root rootHash.
Definition: BasePatriciaTreeDelta.h:40
catapult::tree::BasePatriciaTreeDelta::KeyType
typename TEncoder::KeyType KeyType
Definition: BasePatriciaTreeDelta.h:35
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >