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