xrpld
Loading...
Searching...
No Matches
Dir.cpp
1#include <xrpl/ledger/Dir.h>
2
3#include <xrpl/beast/utility/Zero.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/Keylet.h>
8#include <xrpl/protocol/SField.h>
9
10#include <cstddef>
11#include <iterator>
12#include <optional>
13
14namespace xrpl {
15
17
18Dir::Dir(ReadView const& view, Keylet const& key)
19 : view_(&view), root_(key), sle_(view_->read(root_))
20{
21 if (sle_ != nullptr)
22 indexes_ = &sle_->getFieldV256(sfIndexes);
23}
24
25auto
27{
28 auto it = ConstIterator(*view_, root_, root_);
29 if (sle_ != nullptr)
30 {
31 it.sle_ = sle_;
32 if (!indexes_->empty())
33 {
34 it.indexes_ = indexes_;
35 it.it_ = std::begin(*indexes_);
36 it.index_ = *it.it_;
37 }
38 }
39
40 return it;
41}
42
43auto
45{
46 return ConstIterator(*view_, root_, root_);
47}
48
49bool
51{
52 if (view_ == nullptr || other.view_ == nullptr)
53 return false;
54
55 XRPL_ASSERT(
56 view_ == other.view_ && root_.key == other.root_.key,
57 "xrpl::Dir::ConstIterator::operator== : views and roots are matching");
58 return page_.key == other.page_.key && index_ == other.index_;
59}
60
63{
64 XRPL_ASSERT(index_ != beast::kZero, "xrpl::Dir::ConstIterator::operator* : nonzero index");
65 if (!cache_)
67 return *cache_;
68}
69
72{
73 XRPL_ASSERT(index_ != beast::kZero, "xrpl::Dir::ConstIterator::operator++ : nonzero index");
74 if (++it_ != std::end(*indexes_))
75 {
76 index_ = *it_;
77 cache_ = std::nullopt;
78 return *this;
79 }
80
81 return nextPage();
82}
83
86{
87 XRPL_ASSERT(
88 index_ != beast::kZero, "xrpl::Dir::ConstIterator::operator++(int) : nonzero index");
89 ConstIterator tmp(*this);
90 ++(*this);
91 return tmp;
92}
93
96{
97 auto const next = sle_->getFieldU64(sfIndexNext);
98 if (next == 0)
99 {
100 page_.key = root_.key;
101 index_ = beast::kZero;
102 }
103 else
104 {
105 page_ = keylet::page(root_, next);
106 sle_ = view_->read(page_);
107 XRPL_ASSERT(sle_, "xrpl::Dir::ConstIterator::nextPage : non-null SLE");
108 indexes_ = &sle_->getFieldV256(sfIndexes);
109 if (indexes_->empty())
110 {
111 index_ = beast::kZero;
112 }
113 else
114 {
116 index_ = *it_;
117 }
118 }
119 cache_ = std::nullopt;
120 return *this;
121}
122
125{
126 return indexes_->size();
127}
128
129} // namespace xrpl
T begin(T... args)
std::size_t pageSize()
Definition Dir.cpp:124
ConstIterator & nextPage()
Definition Dir.cpp:95
ConstIterator & operator++()
Definition Dir.cpp:71
SLE::const_pointer sle_
Definition Dir.h:105
ReadView const * view_
Definition Dir.h:100
value_type const & reference
Definition Dir.h:46
ConstIterator(ReadView const &view, Keylet const &root, Keylet const &page)
Definition Dir.h:95
reference operator*() const
Definition Dir.cpp:62
bool operator==(ConstIterator const &other) const
Definition Dir.cpp:50
std::vector< uint256 >::const_iterator it_
Definition Dir.h:107
STVector256 const * indexes_
Definition Dir.h:106
std::optional< value_type > cache_
Definition Dir.h:104
ReadView const * view_
Definition Dir.h:23
ConstIterator begin() const
Definition Dir.cpp:26
ConstIterator end() const
Definition Dir.cpp:44
SLE::const_pointer sle_
Definition Dir.h:25
STVector256 const * indexes_
Definition Dir.h:26
Keylet root_
Definition Dir.h:24
Dir(ReadView const &, Keylet const &)
Definition Dir.cpp:18
A view into a ledger.
Definition ReadView.h:31
T end(T... args)
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:192
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:363
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Dir::ConstIterator const_iterator
Definition Dir.cpp:16
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20