xrpld
Loading...
Searching...
No Matches
SHAMapInnerNode.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/SHAMapHash.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/protocol/Serializer.h>
7#include <xrpl/shamap/SHAMapNodeID.h>
8#include <xrpl/shamap/SHAMapTreeNode.h>
9#include <xrpl/shamap/detail/TaggedPointer.h>
10
11#include <atomic>
12#include <cstdint>
13#include <optional>
14#include <string>
15
16namespace xrpl {
17
18class SHAMapInnerNode final : public SHAMapTreeNode, public CountedObject<SHAMapInnerNode>
19{
20public:
24 static constexpr unsigned int kBranchFactor = 16;
25
26private:
33
36
41
53 void
55
66 getChildIndex(int i) const;
67
75 template <class F>
76 void
77 iterChildren(F&& f) const;
78
87 template <class F>
88 void
89 iterNonEmptyChildIndexes(F&& f) const;
90
91public:
92 explicit SHAMapInnerNode(std::uint32_t cowid, std::uint8_t numAllocatedChildren = 2);
93
96 operator=(SHAMapInnerNode const&) = delete;
97 ~SHAMapInnerNode() override;
98
99 // Needed to support intrusive weak pointers
100 void
101 partialDestructor() override;
102
104 clone(std::uint32_t cowid) const override;
105
107 getType() const override
108 {
110 }
111
112 bool
113 isLeaf() const override
114 {
115 return false;
116 }
117
118 bool
119 isInner() const override
120 {
121 return true;
122 }
123
124 bool
125 isEmpty() const;
126
127 bool
128 isEmptyBranch(int m) const;
129
130 int
131 getBranchCount() const;
132
133 SHAMapHash const&
134 getChildHash(int m) const;
135
136 void
137 setChild(int m, SHAMapTreeNodePtr child);
138
139 void
140 shareChild(int m, SHAMapTreeNodePtr const& child);
141
143 getChildPointer(int branch);
144
146 getChild(int branch);
147
149 canonicalizeChild(int branch, SHAMapTreeNodePtr node);
150
151 // sync functions
152 bool
153 isFullBelow(std::uint32_t generation) const;
154
155 void
157
158 void
159 updateHash() override;
160
164 void
166
167 void
168 serializeForWire(Serializer&) const override;
169
170 void
171 serializeWithPrefix(Serializer&) const override;
172
174 getString(SHAMapNodeID const&) const override;
175
176 void
177 invariants(bool isRoot = false) const override;
178
179 static SHAMapTreeNodePtr
180 makeFullInner(Slice data, SHAMapHash const& hash, bool hashValid);
181
182 static SHAMapTreeNodePtr
184};
185
186inline bool
188{
189 return isBranch_ == 0;
190}
191
192inline bool
194{
195 return (isBranch_ & (1 << m)) == 0;
196}
197
198inline int
203
204inline bool
206{
207 return fullBelowGen_ == generation;
208}
209
210inline void
215
216} // 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:28
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)