xrpld
Loading...
Searching...
No Matches
BookDirs.cpp
1#include <xrpl/ledger/BookDirs.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/ledger/helpers/DirectoryHelpers.h>
7#include <xrpl/protocol/Book.h>
8#include <xrpl/protocol/Indexes.h>
9
10#include <optional>
11
12namespace xrpl {
13
14BookDirs::BookDirs(ReadView const& view, Book const& book)
15 : view_(&view)
16 , root_(keylet::page(getBookBase(book)).key)
18 , key_(view_->succ(root_, nextQuality_).value_or(beast::kZero))
19{
20 XRPL_ASSERT(root_ != beast::kZero, "xrpl::BookDirs::BookDirs : nonzero root");
21 if (key_ != beast::kZero)
22 {
24 {
25 // LCOV_EXCL_START
26 UNREACHABLE("xrpl::BookDirs::BookDirs : directory is empty");
27 // LCOV_EXCL_STOP
28 }
29 }
30}
31
32auto
34{
36 if (key_ != beast::kZero)
37 {
38 it.nextQuality_ = nextQuality_;
39 it.sle_ = sle_;
40 it.entry_ = entry_;
41 it.index_ = index_;
42 }
43 return it;
44}
45
46auto
51
52bool
54{
55 if (view_ == nullptr || other.view_ == nullptr)
56 return false;
57
58 XRPL_ASSERT(
59 view_ == other.view_ && root_ == other.root_,
60 "xrpl::BookDirs::const_iterator::operator== : views and roots are "
61 "matching");
62 return entry_ == other.entry_ && curKey_ == other.curKey_ && index_ == other.index_;
63}
64
67{
68 XRPL_ASSERT(
69 index_ != beast::kZero, "xrpl::BookDirs::const_iterator::operator* : nonzero index");
70 if (!cache_)
72 return *cache_;
73}
74
77{
78 using beast::kZero;
79
80 XRPL_ASSERT(index_ != kZero, "xrpl::BookDirs::const_iterator::operator++ : nonzero index");
82 {
83 if (index_ == 0)
84 curKey_ = view_->succ(++curKey_, nextQuality_).value_or(kZero);
85
86 if (index_ != 0 || curKey_ == kZero)
87 {
88 curKey_ = key_;
89 entry_ = 0;
90 index_ = kZero;
91 }
92 else if (!cdirFirst(*view_, curKey_, sle_, entry_, index_))
93 {
94 // LCOV_EXCL_START
95 UNREACHABLE("xrpl::BookDirs::const_iterator::operator++ : directory is empty");
96 // LCOV_EXCL_STOP
97 }
98 }
99
100 cache_ = std::nullopt;
101 return *this;
102}
103
106{
107 XRPL_ASSERT(
108 index_ != beast::kZero, "xrpl::BookDirs::const_iterator::operator++(int) : nonzero index");
109 const_iterator tmp(*this);
110 ++(*this);
111 return tmp;
112}
113
114} // namespace xrpl
std::optional< value_type > cache_
Definition BookDirs.h:82
SLE::const_pointer sle_
Definition BookDirs.h:79
SLE::const_pointer sle_
Definition BookDirs.h:14
uint256 index_
Definition BookDirs.h:16
uint256 const key_
Definition BookDirs.h:13
BookDirs(ReadView const &, Book const &)
Definition BookDirs.cpp:14
uint256 const nextQuality_
Definition BookDirs.h:12
unsigned int entry_
Definition BookDirs.h:15
const_iterator end() const
Definition BookDirs.cpp:47
const_iterator begin() const
Definition BookDirs.cpp:33
ReadView const * view_
Definition BookDirs.h:10
uint256 const root_
Definition BookDirs.h:11
Specifies an order book.
Definition Book.h:16
ConstIterator & operator++()
Definition Dir.cpp:71
value_type const & reference
Definition Dir.h:46
reference operator*() const
Definition Dir.cpp:62
bool operator==(ConstIterator const &other) const
Definition Dir.cpp:50
A view into a ledger.
Definition ReadView.h:31
Keylet computation functions.
Definition Indexes.h:34
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition Indexes.cpp:264
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:144
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:102
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.
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.