rippled
Loading...
Searching...
No Matches
DirectoryHelpers.cpp
1#include <xrpl/ledger/helpers/DirectoryHelpers.h>
2//
3#include <xrpl/protocol/LedgerFormats.h>
4
5namespace xrpl {
6
7bool
9 ApplyView& view,
10 uint256 const& root,
12 unsigned int& index,
13 uint256& entry)
14{
15 return detail::internalDirFirst(view, root, page, index, entry);
16}
17
18bool
20 ApplyView& view,
21 uint256 const& root,
23 unsigned int& index,
24 uint256& entry)
25{
26 return detail::internalDirNext(view, root, page, index, entry);
27}
28
29bool
31 ReadView const& view,
32 uint256 const& root,
34 unsigned int& index,
35 uint256& entry)
36{
37 return detail::internalDirFirst(view, root, page, index, entry);
38}
39
40bool
42 ReadView const& view,
43 uint256 const& root,
45 unsigned int& index,
46 uint256& entry)
47{
48 return detail::internalDirNext(view, root, page, index, entry);
49}
50
51void
53 ReadView const& view,
54 Keylet const& root,
55 std::function<void(std::shared_ptr<SLE const> const&)> const& f)
56{
57 XRPL_ASSERT(root.type == ltDIR_NODE, "xrpl::forEachItem : valid root type");
58
59 if (root.type != ltDIR_NODE)
60 return;
61
62 auto pos = root;
63
64 while (true)
65 {
66 auto sle = view.read(pos);
67 if (!sle)
68 return;
69 for (auto const& key : sle->getFieldV256(sfIndexes))
70 f(view.read(keylet::child(key)));
71 auto const next = sle->getFieldU64(sfIndexNext);
72 if (next == 0u)
73 return;
74 pos = keylet::page(root, next);
75 }
76}
77
78bool
80 ReadView const& view,
81 Keylet const& root,
82 uint256 const& after,
83 std::uint64_t const hint,
84 unsigned int limit,
85 std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
86{
87 XRPL_ASSERT(root.type == ltDIR_NODE, "xrpl::forEachItemAfter : valid root type");
88
89 if (root.type != ltDIR_NODE)
90 return false;
91
92 auto currentIndex = root;
93
94 // If startAfter is not zero try jumping to that page using the hint
95 if (after.isNonZero())
96 {
97 auto const hintIndex = keylet::page(root, hint);
98
99 if (auto hintDir = view.read(hintIndex))
100 {
101 for (auto const& key : hintDir->getFieldV256(sfIndexes))
102 {
103 if (key == after)
104 {
105 // We found the hint, we can start here
106 currentIndex = hintIndex;
107 break;
108 }
109 }
110 }
111
112 bool found = false;
113 for (;;)
114 {
115 auto const ownerDir = view.read(currentIndex);
116 if (!ownerDir)
117 return found;
118 for (auto const& key : ownerDir->getFieldV256(sfIndexes))
119 {
120 if (!found)
121 {
122 if (key == after)
123 found = true;
124 }
125 else if (f(view.read(keylet::child(key))) && limit-- <= 1)
126 {
127 return found;
128 }
129 }
130
131 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
132 if (uNodeNext == 0)
133 return found;
134 currentIndex = keylet::page(root, uNodeNext);
135 }
136 }
137 else
138 {
139 for (;;)
140 {
141 auto const ownerDir = view.read(currentIndex);
142 if (!ownerDir)
143 return true;
144 for (auto const& key : ownerDir->getFieldV256(sfIndexes))
145 {
146 if (f(view.read(keylet::child(key))) && limit-- <= 1)
147 return true;
148 }
149 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
150 if (uNodeNext == 0)
151 return true;
152 currentIndex = keylet::page(root, uNodeNext);
153 }
154 }
155}
156
157bool
158dirIsEmpty(ReadView const& view, Keylet const& k)
159{
160 auto const sleNode = view.read(k);
161 if (!sleNode)
162 return true;
163 if (!sleNode->getFieldV256(sfIndexes).empty())
164 return false;
165 // The first page of a directory may legitimately be empty even if there
166 // are other pages (the first page is the anchor page) so check to see if
167 // there is another page. If there is, the directory isn't empty.
168 return sleNode->getFieldU64(sfIndexNext) == 0;
169}
170
173{
174 return [account](std::shared_ptr<SLE> const& sle) { (*sle)[sfOwner] = account; };
175}
176
177} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:116
A view into a ledger.
Definition ReadView.h:31
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry > const & ref
bool internalDirFirst(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
bool internalDirNext(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:171
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:342
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Number root(Number f, unsigned d)
Definition Number.cpp:958
bool cdirNext(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
bool cdirFirst(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:523
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19