20#include <xrpld/shamap/SHAMap.h>
21#include <xrpl/basics/contract.h>
40 boost::intrusive_ptr<SHAMapItem const>
const& otherMapItem,
50 bool emptyBranch = !otherMapItem;
52 while (!nodeStack.
empty())
54 node = nodeStack.
top();
61 for (
int i = 0; i < 16; ++i)
62 if (!inner->isEmptyBranch(i))
70 if (emptyBranch || (item->key() != otherMapItem->key()))
83 else if (item->slice() != otherMapItem->slice())
88 item->key(),
DeltaRef(item, otherMapItem)));
91 item->key(),
DeltaRef(otherMapItem, item)));
111 otherMapItem->key(), DeltaRef(
nullptr, otherMapItem)));
114 otherMapItem->key(), DeltaRef(otherMapItem,
nullptr)));
124SHAMap::compare(
SHAMap const& otherMap,
Delta& differences,
int maxCount)
const
132 isValid() && otherMap.
isValid(),
133 "ripple::SHAMap::compare : valid state and valid input");
135 if (getHash() == otherMap.
getHash())
142 nodeStack.
push({root_.get(), otherMap.
root_.get()});
143 while (!nodeStack.
empty())
145 auto [ourNode, otherNode] = nodeStack.
top();
148 if (!ourNode || !otherNode)
150 UNREACHABLE(
"ripple::SHAMap::compare : missing a node");
151 Throw<SHAMapMissingNode>(type_,
uint256());
154 if (ourNode->isLeaf() && otherNode->isLeaf())
159 if (ours->peekItem()->key() == other->peekItem()->key())
161 if (ours->peekItem()->slice() != other->peekItem()->slice())
164 ours->peekItem()->key(),
165 DeltaRef(ours->peekItem(), other->peekItem())));
173 ours->peekItem()->key(),
174 DeltaRef(ours->peekItem(),
nullptr)));
179 other->peekItem()->key(),
180 DeltaRef(
nullptr, other->peekItem())));
185 else if (ourNode->isInner() && otherNode->isLeaf())
190 ours, other->peekItem(),
true, differences, maxCount))
193 else if (ourNode->isLeaf() && otherNode->isInner())
198 other, ours->peekItem(),
false, differences, maxCount))
201 else if (ourNode->isInner() && otherNode->isInner())
205 for (
int i = 0; i < 16; ++i)
206 if (ours->getChildHash(i) != other->getChildHash(i))
208 if (other->isEmptyBranch(i))
213 iNode,
nullptr,
true, differences, maxCount))
216 else if (ours->isEmptyBranch(i))
221 iNode,
nullptr,
false, differences, maxCount))
226 {descendThrow(ours, i),
231 UNREACHABLE(
"ripple::SHAMap::compare : invalid node");
241 if (!root_->isInner())
247 nodeStack.
push(std::static_pointer_cast<SHAMapInnerNode>(root_));
249 while (!nodeStack.
empty())
254 for (
int i = 0; i < 16; ++i)
256 if (!node->isEmptyBranch(i))
259 descendNoStore(node, i);
263 if (nextNode->isInner())
265 std::static_pointer_cast<SHAMapInnerNode>(
270 missingNodes.
emplace_back(type_, node->getChildHash(i));
271 if (--maxMissing <= 0)
280SHAMap::walkMapParallel(
282 int maxMissing)
const
284 if (!root_->isInner())
290 auto const& innerRoot =
291 std::static_pointer_cast<SHAMapInnerNode>(root_);
292 for (
int i = 0; i < 16; ++i)
294 if (!innerRoot->isEmptyBranch(i))
295 topChildren[i] = descendNoStore(innerRoot, i);
309 for (
int rootChildIndex = 0; rootChildIndex < 16; ++rootChildIndex)
311 auto const& child = topChildren[rootChildIndex];
312 if (!child || !child->isInner())
315 nodeStacks[rootChildIndex].push(
316 std::static_pointer_cast<SHAMapInnerNode>(child));
318 JLOG(journal_.debug()) <<
"starting worker " << rootChildIndex;
320 [&m, &missingNodes, &maxMissing, &exceptions,
this](
324 while (!nodeStack.empty())
326 std::shared_ptr<SHAMapInnerNode> node =
327 std::move(nodeStack.top());
330 "ripple::SHAMap::walkMapParallel : non-null node");
333 for (int i = 0; i < 16; ++i)
335 if (node->isEmptyBranch(i))
337 std::shared_ptr<SHAMapTreeNode> nextNode =
338 descendNoStore(node, i);
342 if (nextNode->isInner())
343 nodeStack.push(std::static_pointer_cast<
344 SHAMapInnerNode>(nextNode));
348 std::lock_guard l{m};
349 missingNodes.emplace_back(
350 type_, node->getChildHash(i));
351 if (--maxMissing <= 0)
363 std::move(nodeStacks[rootChildIndex])));
370 if (exceptions.empty())
373 ss <<
"Exception(s) in ledger load: ";
374 for (
auto const& e : exceptions)
375 ss << e.what() <<
", ";
376 JLOG(journal_.error()) << ss.
str();
virtual bool isInner() const =0
Determines if this is an inner node.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
std::pair< boost::intrusive_ptr< SHAMapItem const >, boost::intrusive_ptr< SHAMapItem const > > DeltaRef
std::shared_ptr< SHAMapTreeNode > root_
SHAMapHash getHash() const
bool walkBranch(SHAMapTreeNode *node, boost::intrusive_ptr< SHAMapItem const > const &otherMapItem, bool isFirstMap, Delta &differences, int &maxCount) const
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.