rippled
Loading...
Searching...
No Matches
BookDirs.cpp
1//------------
2//------------------------------------------------------------------
3/*
4 This file is part of rippled: https://github.com/ripple/rippled
5 Copyright (c) 2012, 2015 Ripple Labs Inc.
6
7 Permission to use, copy, modify, and/or distribute this software for any
8 purpose with or without fee is hereby granted, provided that the above
9 copyright notice and this permission notice appear in all copies.
10
11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*/
19//==============================================================================
20
21#include <xrpld/ledger/BookDirs.h>
22#include <xrpld/ledger/View.h>
23
24#include <xrpl/protocol/Indexes.h>
25
26namespace ripple {
27
28BookDirs::BookDirs(ReadView const& view, Book const& book)
29 : view_(&view)
30 , root_(keylet::page(getBookBase(book)).key)
31 , next_quality_(getQualityNext(root_))
32 , key_(view_->succ(root_, next_quality_).value_or(beast::zero))
33{
34 XRPL_ASSERT(
35 root_ != beast::zero, "ripple::BookDirs::BookDirs : nonzero root");
36 if (key_ != beast::zero)
37 {
39 {
40 UNREACHABLE("ripple::BookDirs::BookDirs : directory is empty");
41 }
42 }
43}
44
45auto
47{
49 if (key_ != beast::zero)
50 {
51 it.next_quality_ = next_quality_;
52 it.sle_ = sle_;
53 it.entry_ = entry_;
54 it.index_ = index_;
55 }
56 return it;
57}
58
59auto
64
65beast::Journal BookDirs::const_iterator::j_ =
67
68bool
70 BookDirs::const_iterator const& other) const
71{
72 if (view_ == nullptr || other.view_ == nullptr)
73 return false;
74
75 XRPL_ASSERT(
76 view_ == other.view_ && root_ == other.root_,
77 "ripple::BookDirs::const_iterator::operator== : views and roots are "
78 "matching");
79 return entry_ == other.entry_ && cur_key_ == other.cur_key_ &&
80 index_ == other.index_;
81}
82
85{
86 XRPL_ASSERT(
87 index_ != beast::zero,
88 "ripple::BookDirs::const_iterator::operator* : nonzero index");
89 if (!cache_)
90 cache_ = view_->read(keylet::offer(index_));
91 return *cache_;
92}
93
96{
97 using beast::zero;
98
99 XRPL_ASSERT(
100 index_ != zero,
101 "ripple::BookDirs::const_iterator::operator++ : nonzero index");
102 if (!cdirNext(*view_, cur_key_, sle_, entry_, index_))
103 {
104 if (index_ != 0 ||
105 (cur_key_ =
106 view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero)
107 {
108 cur_key_ = key_;
109 entry_ = 0;
110 index_ = zero;
111 }
112 else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_))
113 {
114 UNREACHABLE(
115 "ripple::BookDirs::const_iterator::operator++ : directory is "
116 "empty");
117 }
118 }
119
120 cache_ = std::nullopt;
121 return *this;
122}
123
126{
127 XRPL_ASSERT(
128 index_ != beast::zero,
129 "ripple::BookDirs::const_iterator::operator++(int) : nonzero index");
130 const_iterator tmp(*this);
131 ++(*this);
132 return tmp;
133}
134
135} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
static Sink & getNullSink()
Returns a Sink which does nothing.
uint256 const next_quality_
Definition BookDirs.h:34
BookDirs(ReadView const &, Book const &)
Definition BookDirs.cpp:28
unsigned int entry_
Definition BookDirs.h:37
ReadView const * view_
Definition BookDirs.h:32
uint256 const root_
Definition BookDirs.h:33
std::shared_ptr< SLE const > sle_
Definition BookDirs.h:36
const_iterator begin() const
Definition BookDirs.cpp:46
const_iterator end() const
Definition BookDirs.cpp:60
uint256 index_
Definition BookDirs.h:38
uint256 const key_
Definition BookDirs.h:35
Specifies an order book.
Definition Book.h:36
bool operator==(const_iterator const &other) const
Definition Dir.cpp:58
reference operator*() const
Definition Dir.cpp:70
value_type const & reference
Definition Dir.h:67
const_iterator & operator++()
Definition Dir.cpp:81
A view into a ledger.
Definition ReadView.h:52
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:274
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:146
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:157
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:141
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:115