rippled
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 <ripple/basics/TaggedCache.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/shamap/SHAMapItem.h>
26 #include <ripple/shamap/SHAMapNodeID.h>
27 #include <ripple/shamap/SHAMapTreeNode.h>
28 #include <ripple/shamap/impl/TaggedPointer.h>
29 
30 #include <bitset>
31 #include <cstdint>
32 #include <memory>
33 #include <mutex>
34 #include <optional>
35 #include <string>
36 
37 namespace ripple {
38 
39 class SHAMapInnerNode final : public SHAMapTreeNode,
40  public CountedObject<SHAMapInnerNode>
41 {
42 public:
44  static inline constexpr unsigned int branchFactor = 16;
45 
46 private:
52 
55 
57 
68  void
69  resizeChildArrays(std::uint8_t toAllocate);
70 
80  getChildIndex(int i) const;
81 
88  template <class F>
89  void
90  iterChildren(F&& f) const;
91 
99  template <class F>
100  void
101  iterNonEmptyChildIndexes(F&& f) const;
102 
103 public:
104  explicit SHAMapInnerNode(
106  std::uint8_t numAllocatedChildren = branchFactor);
107 
108  SHAMapInnerNode(SHAMapInnerNode const&) = delete;
110  operator=(SHAMapInnerNode const&) = delete;
111 
113  clone(std::uint32_t cowid) const override;
114 
116  getType() const override
117  {
119  }
120 
121  bool
122  isLeaf() const override
123  {
124  return false;
125  }
126 
127  bool
128  isInner() const override
129  {
130  return true;
131  }
132 
133  bool
134  isEmpty() const;
135 
136  bool
137  isEmptyBranch(int m) const;
138 
139  int
140  getBranchCount() const;
141 
142  SHAMapHash const&
143  getChildHash(int m) const;
144 
145  void
146  setChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);
147 
148  void
149  shareChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);
150 
152  getChildPointer(int branch);
153 
155  getChild(int branch);
156 
159 
160  // sync functions
161  bool
162  isFullBelow(std::uint32_t generation) const;
163 
164  void
166 
167  void
168  updateHash() override;
169 
171  void
172  updateHashDeep();
173 
174  void
175  serializeForWire(Serializer&) const override;
176 
177  void
178  serializeWithPrefix(Serializer&) const override;
179 
181  getString(SHAMapNodeID const&) const override;
182 
183  void
184  invariants(bool is_root = false) const override;
185 
187  makeFullInner(Slice data, SHAMapHash const& hash, bool hashValid);
188 
191 };
192 
193 inline bool
195 {
196  return (isBranch_ & (1 << m)) == 0;
197 }
198 
199 inline bool
201 {
202  return fullBelowGen_ == generation;
203 }
204 
205 inline void
207 {
208  fullBelowGen_ = gen;
209 }
210 
211 } // namespace ripple
212 #endif
ripple::SHAMapTreeNode::cowid
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Definition: SHAMapTreeNode.h:196
ripple::SHAMapInnerNode::serializeWithPrefix
void serializeWithPrefix(Serializer &) const override
Serialize the node in a format appropriate for hashing.
Definition: SHAMapInnerNode.cpp:246
ripple::SHAMapInnerNode::isInner
bool isInner() const override
Determines if this is an inner node.
Definition: SHAMapInnerNode.h:128
bitset
ripple::SHAMapInnerNode::setChild
void setChild(int m, std::shared_ptr< SHAMapTreeNode > const &child)
Definition: SHAMapInnerNode.cpp:283
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::SHAMapInnerNode::clone
std::shared_ptr< SHAMapTreeNode > clone(std::uint32_t cowid) const override
Make a copy of this node, setting the owner.
Definition: SHAMapInnerNode.cpp:80
ripple::SHAMapInnerNode::SHAMapInnerNode
SHAMapInnerNode(std::uint32_t cowid, std::uint8_t numAllocatedChildren=branchFactor)
Definition: SHAMapInnerNode.cpp:45
std::string
STL class.
std::shared_ptr
STL class.
ripple::SHAMapNodeType::tnINNER
@ tnINNER
ripple::SHAMapInnerNode::hashesAndChildren_
TaggedPointer hashesAndChildren_
Opaque type that contains the hashes array (array of type SHAMapHash) and the children array (array o...
Definition: SHAMapInnerNode.h:51
ripple::SHAMapInnerNode::getChild
std::shared_ptr< SHAMapTreeNode > getChild(int branch)
Definition: SHAMapInnerNode.cpp:341
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::SHAMapInnerNode::makeFullInner
static std::shared_ptr< SHAMapTreeNode > makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapInnerNode.cpp:128
ripple::SHAMapInnerNode::canonicalizeChild
virtual std::shared_ptr< SHAMapTreeNode > canonicalizeChild(int branch, std::shared_ptr< SHAMapTreeNode > node)
Definition: SHAMapInnerNode.cpp:361
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:126
ripple::SHAMapInnerNode::updateHash
void updateHash() override
Recalculate the hash of this node.
Definition: SHAMapInnerNode.cpp:192
ripple::SHAMapInnerNode::branchFactor
static constexpr unsigned int branchFactor
Each inner node has 16 children (the 'radix tree' part of the map)
Definition: SHAMapInnerNode.h:44
ripple::SHAMapInnerNode::shareChild
void shareChild(int m, std::shared_ptr< SHAMapTreeNode > const &child)
Definition: SHAMapInnerNode.cpp:319
ripple::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:33
ripple::SHAMapInnerNode::iterChildren
void iterChildren(F &&f) const
Call the f callback for all 16 (branchFactor) branches - even if the branch is empty.
Definition: SHAMapInnerNode.cpp:54
ripple::TaggedPointer
TaggedPointer is a combination of a pointer and a mask stored in the lowest two bits.
Definition: TaggedPointer.h:57
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:47
ripple::SHAMapInnerNode::isEmptyBranch
bool isEmptyBranch(int m) const
Definition: SHAMapInnerNode.h:194
ripple::SHAMapInnerNode::iterNonEmptyChildIndexes
void iterNonEmptyChildIndexes(F &&f) const
Call the f callback for all non-empty branches.
Definition: SHAMapInnerNode.cpp:61
ripple::SHAMapInnerNode::getString
std::string getString(SHAMapNodeID const &) const override
Definition: SHAMapInnerNode.cpp:268
ripple::SHAMapInnerNode::operator=
SHAMapInnerNode & operator=(SHAMapInnerNode const &)=delete
ripple::SHAMapInnerNode::getChildHash
SHAMapHash const & getChildHash(int m) const
Definition: SHAMapInnerNode.cpp:351
ripple::SHAMapInnerNode
Definition: SHAMapInnerNode.h:39
ripple::SHAMapInnerNode::resizeChildArrays
void resizeChildArrays(std::uint8_t toAllocate)
Convert arrays stored in hashesAndChildren_ so they can store the requested number of children.
Definition: SHAMapInnerNode.cpp:67
ripple::SHAMapInnerNode::isFullBelow
bool isFullBelow(std::uint32_t generation) const
Definition: SHAMapInnerNode.h:200
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:133
ripple::SHAMapInnerNode::getChildIndex
std::optional< int > getChildIndex(int i) const
Get the child's index inside the hashes or children array (stored in hashesAndChildren_.
Definition: SHAMapInnerNode.cpp:74
ripple::SHAMapInnerNode::updateHashDeep
void updateHashDeep()
Recalculate the hash of all children and this node.
Definition: SHAMapInnerNode.cpp:207
cstdint
ripple::SHAMapInnerNode::isBranch_
std::uint16_t isBranch_
Definition: SHAMapInnerNode.h:54
ripple::SHAMapInnerNode::getBranchCount
int getBranchCount() const
Definition: SHAMapInnerNode.cpp:262
std::uint32_t
memory
ripple::SHAMapInnerNode::getType
SHAMapNodeType getType() const override
Determines the type of node.
Definition: SHAMapInnerNode.h:116
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapInnerNode::serializeForWire
void serializeForWire(Serializer &) const override
Serialize the node in a format appropriate for sending over the wire.
Definition: SHAMapInnerNode.cpp:222
ripple::SHAMapInnerNode::childLock
static std::mutex childLock
Definition: SHAMapInnerNode.h:56
optional
mutex
ripple::SHAMapInnerNode::isLeaf
bool isLeaf() const override
Determines if this is a leaf node.
Definition: SHAMapInnerNode.h:122
ripple::SHAMapInnerNode::isEmpty
bool isEmpty() const
Definition: SHAMapInnerNode.cpp:256
ripple::SHAMapInnerNode::makeCompressedInner
static std::shared_ptr< SHAMapTreeNode > makeCompressedInner(Slice data)
Definition: SHAMapInnerNode.cpp:159
ripple::SHAMapInnerNode::getChildPointer
SHAMapTreeNode * getChildPointer(int branch)
Definition: SHAMapInnerNode.cpp:331
ripple::SHAMapInnerNode::setFullBelowGen
void setFullBelowGen(std::uint32_t gen)
Definition: SHAMapInnerNode.h:206
ripple::SHAMapInnerNode::invariants
void invariants(bool is_root=false) const override
Definition: SHAMapInnerNode.cpp:387
ripple::SHAMapInnerNode::fullBelowGen_
std::uint32_t fullBelowGen_
Definition: SHAMapInnerNode.h:53
string