xrpld
Loading...
Searching...
No Matches
DirectoryHelpers.h
1#pragma once
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/Keylet.h>
10#include <xrpl/protocol/SField.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12
13#include <cstdint>
14#include <functional>
15#include <memory>
16#include <type_traits>
17
18namespace xrpl {
19
20namespace detail {
21
22template <class V, class N>
23bool
25 V& view,
26 uint256 const& root,
28 unsigned int& index,
29 uint256& entry)
31{
32 auto const& svIndexes = page->getFieldV256(sfIndexes);
33 XRPL_ASSERT(index <= svIndexes.size(), "xrpl::detail::internalDirNext : index inside range");
34
35 if (index >= svIndexes.size())
36 {
37 auto const next = page->getFieldU64(sfIndexNext);
38
39 if (!next)
40 {
41 entry.zero();
42 return false;
43 }
44
45 if constexpr (std::is_const_v<N>)
46 {
47 page = view.read(keylet::page(root, next));
48 }
49 else
50 {
51 page = view.peek(keylet::page(root, next));
52 }
53
54 XRPL_ASSERT(page, "xrpl::detail::internalDirNext : non-null root");
55
56 if (!page)
57 return false;
58
59 index = 0;
60
61 return internalDirNext(view, root, page, index, entry);
62 }
63
64 entry = svIndexes[index++];
65 return true;
66}
67
68template <class V, class N>
69bool
71 V& view,
72 uint256 const& root,
74 unsigned int& index,
75 uint256& entry)
77{
78 if constexpr (std::is_const_v<N>)
79 {
80 page = view.read(keylet::page(root));
81 }
82 else
83 {
84 page = view.peek(keylet::page(root));
85 }
86
87 if (!page)
88 return false;
89
90 index = 0;
91
92 return internalDirNext(view, root, page, index, entry);
93}
94
95} // namespace detail
96
113bool
115 ReadView const& view,
116 uint256 const& root,
117 SLE::const_pointer& page,
118 unsigned int& index,
119 uint256& entry);
120
121bool
123 ApplyView& view,
124 uint256 const& root,
125 SLE::pointer& page,
126 unsigned int& index,
127 uint256& entry);
129
146bool
148 ReadView const& view,
149 uint256 const& root,
150 SLE::const_pointer& page,
151 unsigned int& index,
152 uint256& entry);
153
154bool
155dirNext(
156 ApplyView& view,
157 uint256 const& root,
158 SLE::pointer& page,
159 unsigned int& index,
160 uint256& entry);
162
166void
167forEachItem(ReadView const& view, Keylet const& root, std::function<void(SLE::const_ref)> const& f);
168
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(SLE::const_ref)> const& f);
184
188inline void
189forEachItem(ReadView const& view, AccountID const& id, std::function<void(SLE::const_ref)> const& f)
190{
191 forEachItem(view, keylet::ownerDir(id), f);
192}
193
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(SLE::const_ref)> const& f)
209{
210 return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
211}
212
217[[nodiscard]] bool
218dirIsEmpty(ReadView const& view, Keylet const& k);
219
223[[nodiscard]] std::function<void(SLE::ref)>
224describeOwnerDir(AccountID const& account);
225
226} // namespace xrpl
A view into a ledger.
Definition ReadView.h:41
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
std::shared_ptr< STLedgerEntry const > const_pointer
T is_base_of_v
T is_const_v
T is_same_v
bool internalDirNext(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
bool internalDirFirst(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:373
Keylet page(uint256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Definition Indexes.cpp:379
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Number root(Number f, unsigned d)
bool dirNext(ApplyView &view, uint256 const &root, SLE::pointer &page, unsigned int &index, uint256 &entry)
STLedgerEntry SLE
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:572
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool cdirNext(ReadView const &view, uint256 const &root, SLE::const_pointer &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(SLE::const_ref)> const &f)
Iterate all items in the given directory.
bool cdirFirst(ReadView const &view, uint256 const &root, SLE::const_pointer &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
BaseUInt< 256 > uint256
Definition base_uint.h:580
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::const_ref)> const &f)
Iterate all items after an item in the given directory.
bool dirFirst(ApplyView &view, uint256 const &root, SLE::pointer &page, unsigned int &index, uint256 &entry)