rippled
Loading...
Searching...
No Matches
BookDirs.cpp
1#include <xrpl/ledger/BookDirs.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/ledger/helpers/DirectoryHelpers.h>
4#include <xrpl/protocol/Indexes.h>
5
6namespace xrpl {
7
8BookDirs::BookDirs(ReadView const& view, Book const& book)
9 : view_(&view)
10 , root_(keylet::page(getBookBase(book)).key)
11 , next_quality_(getQualityNext(root_))
12 , key_(view_->succ(root_, next_quality_).value_or(beast::zero))
13{
14 XRPL_ASSERT(root_ != beast::zero, "xrpl::BookDirs::BookDirs : nonzero root");
15 if (key_ != beast::zero)
16 {
18 {
19 // LCOV_EXCL_START
20 UNREACHABLE("xrpl::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_ = beast::Journal{beast::Journal::getNullSink()};
47
48bool
50{
51 if (view_ == nullptr || other.view_ == nullptr)
52 return false;
53
54 XRPL_ASSERT(
55 view_ == other.view_ && root_ == other.root_,
56 "xrpl::BookDirs::const_iterator::operator== : views and roots are "
57 "matching");
58 return entry_ == other.entry_ && cur_key_ == other.cur_key_ && index_ == other.index_;
59}
60
63{
64 XRPL_ASSERT(index_ != beast::zero, "xrpl::BookDirs::const_iterator::operator* : nonzero index");
65 if (!cache_)
66 cache_ = view_->read(keylet::offer(index_));
67 return *cache_;
68}
69
72{
73 using beast::zero;
74
75 XRPL_ASSERT(index_ != zero, "xrpl::BookDirs::const_iterator::operator++ : nonzero index");
76 if (!cdirNext(*view_, cur_key_, sle_, entry_, index_))
77 {
78 if (index_ == 0)
79 cur_key_ = view_->succ(++cur_key_, next_quality_).value_or(zero);
80
81 if (index_ != 0 || cur_key_ == zero)
82 {
83 cur_key_ = key_;
84 entry_ = 0;
85 index_ = zero;
86 }
87 else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_))
88 {
89 // LCOV_EXCL_START
90 UNREACHABLE("xrpl::BookDirs::const_iterator::operator++ : directory is empty");
91 // LCOV_EXCL_STOP
92 }
93 }
94
95 cache_ = std::nullopt;
96 return *this;
97}
98
101{
102 XRPL_ASSERT(
103 index_ != beast::zero, "xrpl::BookDirs::const_iterator::operator++(int) : nonzero index");
104 const_iterator tmp(*this);
105 ++(*this);
106 return tmp;
107}
108
109} // 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:8
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:41
const_iterator begin() const
Definition BookDirs.cpp:27
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:39
const_iterator & operator++()
Definition Dir.cpp:60
reference operator*() const
Definition Dir.cpp:51
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:243
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.
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.
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:123
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:98