xrpld
Loading...
Searching...
No Matches
SHAMapInnerNode.h
1#pragma once
2
3#include <xrpl/basics/IntrusivePointer.h>
4#include <xrpl/shamap/SHAMapNodeID.h>
5#include <xrpl/shamap/detail/TaggedPointer.h>
6
7#include <atomic>
8#include <cstdint>
9#include <optional>
10#include <string>
11
12namespace xrpl {
13
14class SHAMapInnerNode final : public SHAMapTreeNode, public CountedObject<SHAMapInnerNode>
15{
16public:
18 static constexpr unsigned int kBranchFactor = 16;
19
20private:
26
29
32
43 void
45
55 getChildIndex(int i) const;
56
63 template <class F>
64 void
65 iterChildren(F&& f) const;
66
74 template <class F>
75 void
76 iterNonEmptyChildIndexes(F&& f) const;
77
78public:
79 explicit SHAMapInnerNode(std::uint32_t cowid, std::uint8_t numAllocatedChildren = 2);
80
83 operator=(SHAMapInnerNode const&) = delete;
84 ~SHAMapInnerNode() override;
85
86 // Needed to support intrusive weak pointers
87 void
88 partialDestructor() override;
89
91 clone(std::uint32_t cowid) const override;
92
94 getType() const override
95 {
97 }
98
99 bool
100 isLeaf() const override
101 {
102 return false;
103 }
104
105 bool
106 isInner() const override
107 {
108 return true;
109 }
110
111 bool
112 isEmpty() const;
113
114 bool
115 isEmptyBranch(int m) const;
116
117 int
118 getBranchCount() const;
119
120 SHAMapHash const&
121 getChildHash(int m) const;
122
123 void
124 setChild(int m, SHAMapTreeNodePtr child);
125
126 void
127 shareChild(int m, SHAMapTreeNodePtr const& child);
128
130 getChildPointer(int branch);
131
133 getChild(int branch);
134
136 canonicalizeChild(int branch, SHAMapTreeNodePtr node);
137
138 // sync functions
139 bool
140 isFullBelow(std::uint32_t generation) const;
141
142 void
144
145 void
146 updateHash() override;
147
149 void
151
152 void
153 serializeForWire(Serializer&) const override;
154
155 void
156 serializeWithPrefix(Serializer&) const override;
157
159 getString(SHAMapNodeID const&) const override;
160
161 void
162 invariants(bool isRoot = false) const override;
163
164 static SHAMapTreeNodePtr
165 makeFullInner(Slice data, SHAMapHash const& hash, bool hashValid);
166
167 static SHAMapTreeNodePtr
169};
170
171inline bool
173{
174 return isBranch_ == 0;
175}
176
177inline bool
179{
180 return (isBranch_ & (1 << m)) == 0;
181}
182
183inline int
188
189inline bool
191{
192 return fullBelowGen_ == generation;
193}
194
195inline void
200
201} // namespace xrpl
void serializeWithPrefix(Serializer &) const override
Serialize the node in a format appropriate for hashing.
void updateHash() override
Recalculate the hash of this node.
bool isFullBelow(std::uint32_t generation) const
TaggedPointer hashesAndChildren_
Opaque type that contains the hashes array (array of type SHAMapHash) and the children array (array o...
static constexpr unsigned int kBranchFactor
Each inner node has 16 children (the 'radix tree' part of the map).
SHAMapHash const & getChildHash(int m) const
~SHAMapInnerNode() override
void updateHashDeep()
Recalculate the hash of all children and this node.
void shareChild(int m, SHAMapTreeNodePtr const &child)
void iterNonEmptyChildIndexes(F &&f) const
Call the f callback for all non-empty branches.
SHAMapNodeType getType() const override
Determines the type of node.
void setFullBelowGen(std::uint32_t gen)
SHAMapTreeNodePtr clone(std::uint32_t cowid) const override
Make a copy of this node, setting the owner.
SHAMapTreeNodePtr getChild(int branch)
SHAMapTreeNodePtr canonicalizeChild(int branch, SHAMapTreeNodePtr node)
SHAMapTreeNode * getChildPointer(int branch)
static SHAMapTreeNodePtr makeCompressedInner(Slice data)
std::string getString(SHAMapNodeID const &) const override
SHAMapInnerNode(std::uint32_t cowid, std::uint8_t numAllocatedChildren=2)
void setChild(int m, SHAMapTreeNodePtr child)
void iterChildren(F &&f) const
Call the f callback for all 16 (branchFactor) branches - even if the branch is empty.
void resizeChildArrays(std::uint8_t toAllocate)
Convert arrays stored in hashesAndChildren_ so they can store the requested number of children.
void invariants(bool isRoot=false) const override
bool isInner() const override
Determines if this is an inner node.
std::uint32_t fullBelowGen_
void partialDestructor() override
std::atomic< std::uint16_t > lock_
A bitlock for the children of this node, with one bit per child.
bool isLeaf() const override
Determines if this is a leaf node.
void serializeForWire(Serializer &) const override
Serialize the node in a format appropriate for sending over the wire.
std::optional< int > getChildIndex(int i) const
Get the child's index inside the hashes or children array (stored in hashesAndChildren_.
static SHAMapTreeNodePtr makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
SHAMapInnerNode & operator=(SHAMapInnerNode const &)=delete
SHAMapInnerNode(SHAMapInnerNode const &)=delete
bool isEmptyBranch(int m) const
Identifies a node inside a SHAMap.
SHAMapTreeNode(std::uint32_t cowid) noexcept
Construct a node.
An immutable linear range of bytes.
Definition Slice.h:26
TaggedPointer is a combination of a pointer and a mask stored in the lowest two bits.
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
intr_ptr::SharedPtr< SHAMapTreeNode > SHAMapTreeNodePtr
int popcnt16(std::uint16_t a)