rippled
Loading...
Searching...
No Matches
BookDirs.cpp
1#include <xrpl/ledger/BookDirs.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/protocol/Indexes.h>
4
5namespace ripple {
6
7BookDirs::BookDirs(ReadView const& view, Book const& book)
8 : view_(&view)
9 , root_(keylet::page(getBookBase(book)).key)
10 , next_quality_(getQualityNext(root_))
11 , key_(view_->succ(root_, next_quality_).value_or(beast::zero))
12{
13 XRPL_ASSERT(
14 root_ != beast::zero, "ripple::BookDirs::BookDirs : nonzero root");
15 if (key_ != beast::zero)
16 {
18 {
19 // LCOV_EXCL_START
20 UNREACHABLE("ripple::BookDirs::BookDirs : directory is empty");
21 // LCOV_EXCL_STOP
22 }
23 }
24}
25
26auto
28{
30 if (key_ != beast::zero)
31 {
32 it.next_quality_ = next_quality_;
33 it.sle_ = sle_;
34 it.entry_ = entry_;
35 it.index_ = index_;
36 }
37 return it;
38}
39
40auto
45
46beast::Journal BookDirs::const_iterator::j_ =
48
49bool
51 BookDirs::const_iterator const& other) const
52{
53 if (view_ == nullptr || other.view_ == nullptr)
54 return false;
55
56 XRPL_ASSERT(
57 view_ == other.view_ && root_ == other.root_,
58 "ripple::BookDirs::const_iterator::operator== : views and roots are "
59 "matching");
60 return entry_ == other.entry_ && cur_key_ == other.cur_key_ &&
61 index_ == other.index_;
62}
63
66{
67 XRPL_ASSERT(
68 index_ != beast::zero,
69 "ripple::BookDirs::const_iterator::operator* : nonzero index");
70 if (!cache_)
71 cache_ = view_->read(keylet::offer(index_));
72 return *cache_;
73}
74
77{
78 using beast::zero;
79
80 XRPL_ASSERT(
81 index_ != zero,
82 "ripple::BookDirs::const_iterator::operator++ : nonzero index");
83 if (!cdirNext(*view_, cur_key_, sle_, entry_, index_))
84 {
85 if (index_ != 0 ||
86 (cur_key_ =
87 view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero)
88 {
89 cur_key_ = key_;
90 entry_ = 0;
91 index_ = zero;
92 }
93 else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_))
94 {
95 // LCOV_EXCL_START
96 UNREACHABLE(
97 "ripple::BookDirs::const_iterator::operator++ : directory is "
98 "empty");
99 // LCOV_EXCL_STOP
100 }
101 }
102
103 cache_ = std::nullopt;
104 return *this;
105}
106
109{
110 XRPL_ASSERT(
111 index_ != beast::zero,
112 "ripple::BookDirs::const_iterator::operator++(int) : nonzero index");
113 const_iterator tmp(*this);
114 ++(*this);
115 return tmp;
116}
117
118} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
static Sink & getNullSink()
Returns a Sink which does nothing.
uint256 const next_quality_
Definition BookDirs.h:14
BookDirs(ReadView const &, Book const &)
Definition BookDirs.cpp:7
unsigned int entry_
Definition BookDirs.h:17
ReadView const * view_
Definition BookDirs.h:12
uint256 const root_
Definition BookDirs.h:13
std::shared_ptr< SLE const > sle_
Definition BookDirs.h:16
const_iterator begin() const
Definition BookDirs.cpp:27
const_iterator end() const
Definition BookDirs.cpp:41
uint256 index_
Definition BookDirs.h:18
uint256 const key_
Definition BookDirs.h:15
Specifies an order book.
Definition Book.h:17
bool operator==(const_iterator const &other) const
Definition Dir.cpp:39
reference operator*() const
Definition Dir.cpp:51
value_type const & reference
Definition Dir.h:47
const_iterator & operator++()
Definition Dir.cpp:62
A view into a ledger.
Definition ReadView.h:32
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
T is_same_v
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition Indexes.cpp:255
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:122
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.
Definition View.cpp:126
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.
Definition View.cpp:137
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:96