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#include <xrpl/protocol/Indexes.h>
24
25namespace ripple {
26
27BookDirs::BookDirs(ReadView const& view, Book const& book)
28 : view_(&view)
29 , root_(keylet::page(getBookBase(book)).key)
30 , next_quality_(getQualityNext(root_))
31 , key_(view_->succ(root_, next_quality_).value_or(beast::zero))
32{
33 XRPL_ASSERT(
34 root_ != beast::zero, "ripple::BookDirs::BookDirs : nonzero root");
35 if (key_ != beast::zero)
36 {
38 {
39 UNREACHABLE("ripple::BookDirs::BookDirs : directory is empty");
40 }
41 }
42}
43
44auto
46{
48 if (key_ != beast::zero)
49 {
50 it.next_quality_ = next_quality_;
51 it.sle_ = sle_;
52 it.entry_ = entry_;
53 it.index_ = index_;
54 }
55 return it;
56}
57
58auto
60{
62}
63
64beast::Journal BookDirs::const_iterator::j_ =
66
67bool
69 BookDirs::const_iterator const& other) const
70{
71 if (view_ == nullptr || other.view_ == nullptr)
72 return false;
73
74 XRPL_ASSERT(
75 view_ == other.view_ && root_ == other.root_,
76 "ripple::BookDirs::const_iterator::operator== : views and roots are "
77 "matching");
78 return entry_ == other.entry_ && cur_key_ == other.cur_key_ &&
79 index_ == other.index_;
80}
81
84{
85 XRPL_ASSERT(
86 index_ != beast::zero,
87 "ripple::BookDirs::const_iterator::operator* : nonzero index");
88 if (!cache_)
89 cache_ = view_->read(keylet::offer(index_));
90 return *cache_;
91}
92
95{
96 using beast::zero;
97
98 XRPL_ASSERT(
99 index_ != zero,
100 "ripple::BookDirs::const_iterator::operator++ : nonzero index");
101 if (!cdirNext(*view_, cur_key_, sle_, entry_, index_))
102 {
103 if (index_ != 0 ||
104 (cur_key_ =
105 view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero)
106 {
107 cur_key_ = key_;
108 entry_ = 0;
109 index_ = zero;
110 }
111 else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_))
112 {
113 UNREACHABLE(
114 "ripple::BookDirs::const_iterator::operator++ : directory is "
115 "empty");
116 }
117 }
118
119 cache_ = std::nullopt;
120 return *this;
121}
122
125{
126 XRPL_ASSERT(
127 index_ != beast::zero,
128 "ripple::BookDirs::const_iterator::operator++(int) : nonzero index");
129 const_iterator tmp(*this);
130 ++(*this);
131 return tmp;
132}
133
134} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
static Sink & getNullSink()
Returns a Sink which does nothing.
uint256 const next_quality_
Definition: BookDirs.h:32
BookDirs(ReadView const &, Book const &)
Definition: BookDirs.cpp:27
unsigned int entry_
Definition: BookDirs.h:35
ReadView const * view_
Definition: BookDirs.h:30
uint256 const root_
Definition: BookDirs.h:31
std::shared_ptr< SLE const > sle_
Definition: BookDirs.h:34
const_iterator begin() const
Definition: BookDirs.cpp:45
const_iterator end() const
Definition: BookDirs.cpp:59
uint256 index_
Definition: BookDirs.h:36
uint256 const key_
Definition: BookDirs.h:33
Specifies an order book.
Definition: Book.h:34
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:66
const_iterator & operator++()
Definition: Dir.cpp:81
A view into a ledger.
Definition: ReadView.h:55
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.
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition: Indexes.cpp:250
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:136
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:147
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:117
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:98