rippled
Loading...
Searching...
No Matches
SHAMapInnerNode.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_SHAMAP_SHAMAPINNERNODE_H_INCLUDED
21#define RIPPLE_SHAMAP_SHAMAPINNERNODE_H_INCLUDED
22
23#include <xrpld/shamap/SHAMapNodeID.h>
24#include <xrpld/shamap/detail/TaggedPointer.h>
25
26#include <xrpl/basics/IntrusivePointer.h>
27
28#include <atomic>
29#include <cstdint>
30#include <optional>
31#include <string>
32
33namespace ripple {
34
35class SHAMapInnerNode final : public SHAMapTreeNode,
36 public CountedObject<SHAMapInnerNode>
37{
38public:
40 static inline constexpr unsigned int branchFactor = 16;
41
42private:
48
51
54
65 void
67
77 getChildIndex(int i) const;
78
85 template <class F>
86 void
87 iterChildren(F&& f) const;
88
96 template <class F>
97 void
98 iterNonEmptyChildIndexes(F&& f) const;
99
100public:
101 explicit SHAMapInnerNode(
103 std::uint8_t numAllocatedChildren = 2);
104
107 operator=(SHAMapInnerNode const&) = delete;
109
110 // Needed to support intrusive weak pointers
111 void
112 partialDestructor() override;
113
115 clone(std::uint32_t cowid) const override;
116
118 getType() const override
119 {
121 }
122
123 bool
124 isLeaf() const override
125 {
126 return false;
127 }
128
129 bool
130 isInner() const override
131 {
132 return true;
133 }
134
135 bool
136 isEmpty() const;
137
138 bool
139 isEmptyBranch(int m) const;
140
141 int
142 getBranchCount() const;
143
144 SHAMapHash const&
145 getChildHash(int m) const;
146
147 void
149
150 void
152
154 getChildPointer(int branch);
155
157 getChild(int branch);
158
161
162 // sync functions
163 bool
164 isFullBelow(std::uint32_t generation) const;
165
166 void
168
169 void
170 updateHash() override;
171
173 void
175
176 void
177 serializeForWire(Serializer&) const override;
178
179 void
180 serializeWithPrefix(Serializer&) const override;
181
183 getString(SHAMapNodeID const&) const override;
184
185 void
186 invariants(bool is_root = false) const override;
187
189 makeFullInner(Slice data, SHAMapHash const& hash, bool hashValid);
190
193};
194
195inline bool
197{
198 return isBranch_ == 0;
199}
200
201inline bool
203{
204 return (isBranch_ & (1 << m)) == 0;
205}
206
207inline int
209{
210 return popcnt16(isBranch_);
211}
212
213inline bool
215{
216 return fullBelowGen_ == generation;
217}
218
219inline void
221{
222 fullBelowGen_ = gen;
223}
224
225} // namespace ripple
226#endif
Tracks the number of instances of an object.
std::uint32_t fullBelowGen_
bool isInner() const override
Determines if this is an inner node.
std::optional< int > getChildIndex(int i) const
Get the child's index inside the hashes or children array (stored in hashesAndChildren_.
void setChild(int m, intr_ptr::SharedPtr< SHAMapTreeNode > child)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeCompressedInner(Slice data)
intr_ptr::SharedPtr< SHAMapTreeNode > clone(std::uint32_t cowid) const override
Make a copy of this node, setting the owner.
static constexpr unsigned int branchFactor
Each inner node has 16 children (the 'radix tree' part of the map)
bool isFullBelow(std::uint32_t generation) const
bool isEmptyBranch(int m) const
void iterNonEmptyChildIndexes(F &&f) const
Call the f callback for all non-empty branches.
void serializeWithPrefix(Serializer &) const override
Serialize the node in a format appropriate for hashing.
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.
bool isLeaf() const override
Determines if this is a leaf node.
void updateHash() override
Recalculate the hash of this node.
intr_ptr::SharedPtr< SHAMapTreeNode > getChild(int branch)
void partialDestructor() override
void shareChild(int m, intr_ptr::SharedPtr< SHAMapTreeNode > const &child)
SHAMapHash const & getChildHash(int m) const
void invariants(bool is_root=false) const override
std::string getString(SHAMapNodeID const &) const override
intr_ptr::SharedPtr< SHAMapTreeNode > canonicalizeChild(int branch, intr_ptr::SharedPtr< SHAMapTreeNode > node)
SHAMapInnerNode & operator=(SHAMapInnerNode const &)=delete
SHAMapInnerNode(SHAMapInnerNode const &)=delete
TaggedPointer hashesAndChildren_
Opaque type that contains the hashes array (array of type SHAMapHash) and the children array (array o...
void setFullBelowGen(std::uint32_t gen)
void serializeForWire(Serializer &) const override
Serialize the node in a format appropriate for sending over the wire.
SHAMapNodeType getType() const override
Determines the type of node.
void updateHashDeep()
Recalculate the hash of all children and this node.
SHAMapTreeNode * getChildPointer(int branch)
std::atomic< std::uint16_t > lock_
A bitlock for the children of this node, with one bit per child.
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:34
A shared intrusive pointer class that supports weak pointers.
An immutable linear range of bytes.
Definition: Slice.h:46
TaggedPointer is a combination of a pointer and a mask stored in the lowest two bits.
Definition: TaggedPointer.h:61
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:25
int popcnt16(std::uint16_t a)