rippled
Loading...
Searching...
No Matches
DirectoryHelpers.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/STLedgerEntry.h>
8#include <xrpl/protocol/TER.h>
9
10#include <functional>
11#include <memory>
12#include <type_traits>
13
14namespace xrpl {
15
16namespace detail {
17
18template <
19 class V,
20 class N,
21 class = std::enable_if_t<
23bool
25 V& view,
26 uint256 const& root,
28 unsigned int& index,
29 uint256& entry)
30{
31 auto const& svIndexes = page->getFieldV256(sfIndexes);
32 XRPL_ASSERT(index <= svIndexes.size(), "xrpl::detail::internalDirNext : index inside range");
33
34 if (index >= svIndexes.size())
35 {
36 auto const next = page->getFieldU64(sfIndexNext);
37
38 if (!next)
39 {
40 entry.zero();
41 return false;
42 }
43
44 if constexpr (std::is_const_v<N>)
45 {
46 page = view.read(keylet::page(root, next));
47 }
48 else
49 {
50 page = view.peek(keylet::page(root, next));
51 }
52
53 XRPL_ASSERT(page, "xrpl::detail::internalDirNext : non-null root");
54
55 if (!page)
56 return false;
57
58 index = 0;
59
60 return internalDirNext(view, root, page, index, entry);
61 }
62
63 entry = svIndexes[index++];
64 return true;
65}
66
67template <
68 class V,
69 class N,
70 class = std::enable_if_t<
72bool
74 V& view,
75 uint256 const& root,
77 unsigned int& index,
78 uint256& entry)
79{
80 if constexpr (std::is_const_v<N>)
81 {
82 page = view.read(keylet::page(root));
83 }
84 else
85 {
86 page = view.peek(keylet::page(root));
87 }
88
89 if (!page)
90 return false;
91
92 index = 0;
93
94 return internalDirNext(view, root, page, index, entry);
95}
96
97} // namespace detail
98
114bool
116 ReadView const& view,
117 uint256 const& root,
119 unsigned int& index,
120 uint256& entry);
121
122bool
124 ApplyView& view,
125 uint256 const& root,
127 unsigned int& index,
128 uint256& entry);
146bool
148 ReadView const& view,
149 uint256 const& root,
151 unsigned int& index,
152 uint256& entry);
153
154bool
155dirNext(
156 ApplyView& view,
157 uint256 const& root,
159 unsigned int& index,
160 uint256& entry);
164void
166 ReadView const& view,
167 Keylet const& root,
168 std::function<void(std::shared_ptr<SLE const> const&)> const& f);
169
176bool
178 ReadView const& view,
179 Keylet const& root,
180 uint256 const& after,
181 std::uint64_t const hint,
182 unsigned int limit,
183 std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
184
186inline void
188 ReadView const& view,
189 AccountID const& id,
190 std::function<void(std::shared_ptr<SLE const> const&)> const& f)
191{
192 return forEachItem(view, keylet::ownerDir(id), f);
193}
194
201inline bool
203 ReadView const& view,
204 AccountID const& id,
205 uint256 const& after,
206 std::uint64_t const hint,
207 unsigned int limit,
208 std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
209{
210 return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
211}
212
216[[nodiscard]] bool
217dirIsEmpty(ReadView const& view, Keylet const& k);
218
220[[nodiscard]] std::function<void(SLE::ref)>
221describeOwnerDir(AccountID const& account);
222
223} // namespace xrpl
A view into a ledger.
Definition ReadView.h:31
std::shared_ptr< STLedgerEntry > const & ref
T is_same_v
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 ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
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.
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
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.
base_uint< 256 > uint256
Definition base_uint.h:531
STLedgerEntry SLE
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)