CatapultServer
v0.5.0.1 (Elephant)
|
Go to the documentation of this file.
24 namespace catapult {
namespace tree {
27 template<
typename TEncoder,
typename TDataSource>
30 using KeyType =
typename TEncoder::KeyType;
50 auto encodedValue = TEncoder::EncodeValue(value);
70 if (leafNode.path() == newPair.Path)
87 const auto& leafPath = leafNode.
path();
89 auto sharedPath = leafPath.subpath(0, differenceIndex);
92 auto node1 =
LeafTreeNode(leafPath.subpath(differenceIndex + 1), leafNode.
value());
93 auto node2 =
LeafTreeNode(newPair.Path.subpath(differenceIndex + 1), newPair.Value);
97 setLink(branchNode, node1, leafPath.nibbleAt(differenceIndex));
98 setLink(branchNode, node2, newPair.Path.nibbleAt(differenceIndex));
104 const auto& branchPath = branchNode.
path();
109 if (!branchPath.empty() && differenceIndex == branchPath.size()) {
110 auto newPairPathContinuation = newPair.Path.subpath(differenceIndex);
120 size_t differenceIndex,
121 const PathValuePairRef& newPair) {
122 auto isBranchPathConsumed = branchPath.
empty();
126 auto pNextNode = isBranchPathConsumed ?
getLinkedNode(branchNode, newPair.Path.nibbleAt(0)) :
nullptr;
128 pNextNode = std::make_unique<TreeNode>();
131 auto updatedNextNode =
set(*pNextNode, { newPair.Path.subpath(differenceIndex + 1), newPair.Value });
134 if (isBranchPathConsumed) {
135 setLink(branchNode, updatedNextNode, newPair.Path.nibbleAt(0));
141 setLink(newBranchNode, updatedNextNode, newPair.Path.nibbleAt(differenceIndex));
144 auto branchLinkIndex = branchPath.
nibbleAt(differenceIndex);
148 setLink(newBranchNode, branchNode, branchLinkIndex);
149 return newBranchNode;
160 auto canMerge =
true;
171 if (differenceIndex == keyPath.
size()) {
183 auto nodeLinkIndex = keyPath.
nibbleAt(differenceIndex);
187 if (!pNextNode || !
unset(*pNextNode, keyPath.
subpath(differenceIndex + 1), updatedNextNode, canMerge))
192 updatedNode = canMerge
202 branchNode.clearLink(linkIndex);
203 if (1 != branchNode.numLinks())
207 auto lastLinkIndex = branchNode.highestLinkIndex();
208 auto referencedNode =
getLinkedNode(branchNode, lastLinkIndex)->copy();
210 auto mergedPath =
TreeNodePath::Join(branchNode.path(), lastLinkIndex, referencedNode.path());
211 referencedNode.setPath(mergedPath);
212 return std::move(referencedNode);
216 setLink(branchNode, linkedNode, linkIndex);
226 std::pair<Hash256, bool>
lookup(
const KeyType& key, std::vector<TreeNode>& nodePath)
const {
237 nodePath.push_back(node.
copy());
244 auto nodeLinkIndex = keyPath.
nibbleAt(differenceIndex);
249 return lookup(*pNextNode, keyPath.
subpath(differenceIndex + 1), nodePath);
253 return std::make_pair(
Hash256(),
false);
315 branchNode.compactLinks();
326 auto pLinkedNode = branchNode.
linkedNode(index);
327 return pLinkedNode ? std::move(pLinkedNode) :
m_dataSource.get(branchNode.
link(index));
331 branchNode.
setLink(node, index);
334 template<
typename TNode>
const Hash256 & Value
Definition: PatriciaTree.h:57
Hash256 root() const
Gets the root hash that uniquely identifies this tree.
Definition: PatriciaTree.h:40
TreeNode copy() const
Creates a copy of this node.
Definition: TreeNode.cpp:234
void save(const TreeNode &node)
Definition: PatriciaTree.h:292
TDataSource & m_dataSource
Definition: PatriciaTree.h:342
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:90
PatriciaTree(TDataSource &dataSource)
Creates a tree around a dataSource.
Definition: PatriciaTree.h:35
size_t FindFirstDifferenceIndex(const TreeNodePath &lhs, const TreeNodePath &rhs)
Compares two paths (lhs and rhs) and returns the index of the first non-equal nibble.
Definition: TreeNodePath.cpp:142
std::unique_ptr< const TreeNode > getLinkedNode(const BranchTreeNode &branchNode, size_t index) const
Definition: PatriciaTree.h:324
bool empty() const
Returns true if this path is empty.
Definition: TreeNodePath.cpp:54
Represents a compact patricia tree.
Definition: PatriciaTree.h:28
void saveAll()
Saves all tree nodes to the underlying data source.
Definition: PatriciaTree.h:286
utils::ByteArray< Hash256_Size, Hash256_tag > Hash256
Definition: src/catapult/types.h:47
TreeNodePath subpath(size_t offset) const
Creates a subpath starting at nibble offset.
Definition: TreeNodePath.cpp:78
void setLink(BranchTreeNode &branchNode, const TNode &node, size_t index)
Definition: PatriciaTree.h:335
uint8_t nibbleAt(size_t index) const
Gets the nibble at index.
Definition: TreeNodePath.cpp:62
typename TEncoder::KeyType KeyType
Definition: PatriciaTree.h:30
void setLink(BranchTreeNode &branchNode, const TreeNode &node, size_t index)
Definition: PatriciaTree.h:330
void saveAll(const TreeNode &node)
Definition: PatriciaTree.h:299
const Hash256 & hash() const
Gets the hash representation of this node.
Definition: TreeNode.cpp:202
bool empty() const
Returns true if this node represents an empty node.
Definition: TreeNode.cpp:181
typename TEncoder::ValueType ValueType
Definition: PatriciaTree.h:31
bool unset(const TreeNode &node, const TreeNodePath &keyPath, TreeNode &updatedNode, bool &canMerge)
Definition: PatriciaTree.h:165
TreeNode unsetBranchLink(BranchTreeNode &&branchNode, size_t linkIndex)
Definition: PatriciaTree.h:200
const Hash256 & value() const
Gets the node value.
Definition: TreeNode.cpp:72
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:68
bool tryLoad(const Hash256 &rootHash)
Loads the node with hash rootHash and sets it as the root node.
Definition: PatriciaTree.h:262
bool isLeaf() const
Returns true if this node represents a leaf node.
Definition: TreeNode.cpp:189
static constexpr size_t Max_Links
Maximum number of branch links.
Definition: TreeNode.h:63
LeafTreeNode createLeaf(const PathValuePairRef &pair)
Definition: PatriciaTree.h:81
bool unset(const KeyType &key)
Removes the value associated with key from the tree.
Definition: PatriciaTree.h:158
const Hash256 & link(size_t index) const
Gets the branch link at index.
Definition: TreeNode.cpp:102
const BranchTreeNode & asBranchNode() const
Gets a branch node interface to this node.
Definition: TreeNode.cpp:227
static std::pair< Hash256, bool > LookupNotFoundResult()
Definition: PatriciaTree.h:252
Definition: PatriciaTree.h:55
TreeNode updateBranchLink(BranchTreeNode &&branchNode, size_t linkIndex, const TreeNode &linkedNode)
Definition: PatriciaTree.h:215
void setPath(const TreeNodePath &path)
Sets the branch node path.
Definition: TreeNode.cpp:130
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: PatriciaTree.h:226
BranchTreeNode branchLeafNode(const LeafTreeNode &leafNode, const PathValuePairRef &newPair)
Definition: PatriciaTree.h:86
TreeNode set(const TreeNode &node, const PathValuePairRef &newPair)
Definition: PatriciaTree.h:61
void set(const KeyType &key, const ValueType &value)
Sets key to value in the tree.
Definition: PatriciaTree.h:48
Represents a leaf tree node.
Definition: TreeNode.h:34
std::pair< Hash256, bool > lookup(const TreeNode &node, const TreeNodePath &keyPath, std::vector< TreeNode > &nodePath) const
Definition: PatriciaTree.h:232
const TreeNodePath & Path
Definition: PatriciaTree.h:56
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:193
void setRoot(const TreeNode &rootNode)
Sets the root to rootNode.
Definition: PatriciaTree.h:271
void clear()
Clears the tree.
Definition: PatriciaTree.h:276
BranchTreeNode insertNewPairIntoBranch(BranchTreeNode &branchNode, const TreeNodePath &branchPath, size_t differenceIndex, const PathValuePairRef &newPair)
Definition: PatriciaTree.h:117
void setLink(const Hash256 &link, size_t index)
Sets the branch link at index.
Definition: TreeNode.cpp:135
const LeafTreeNode & asLeafNode() const
Gets a leaf node interface to this node.
Definition: TreeNode.cpp:220
Definition: AddressExtractionExtension.cpp:28
size_t size() const
Gets the number of nibbles in this path.
Definition: TreeNodePath.cpp:58
static TreeNodePath Join(const TreeNodePath &lhs, const TreeNodePath &rhs)
Joins lhs and rhs into a new path.
Definition: TreeNodePath.cpp:116
Represents a tree node.
Definition: TreeNode.h:124
bool isBranch() const
Returns true if this node represents a branch node.
Definition: TreeNode.cpp:185
BranchTreeNode updateBranchLink(BranchTreeNode &&branchNode, const PathValuePairRef &newPair)
Definition: PatriciaTree.h:103
Represents a path in a tree.
Definition: TreeNodePath.h:31
TreeNode m_rootNode
Definition: PatriciaTree.h:343
std::unique_ptr< const TreeNode > linkedNode(size_t index) const
Gets a copy of the linked node at index or nullptr if no linked node is present.
Definition: TreeNode.cpp:107
Represents a branch tree node.
Definition: TreeNode.h:60