rippled
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 <ripple/ledger/BookDirs.h>
22 #include <ripple/ledger/View.h>
23 #include <ripple/protocol/Indexes.h>
24 
25 namespace ripple {
26 
27 BookDirs::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  assert(root_ != beast::zero);
34  if (key_ != beast::zero)
35  {
36  if (!cdirFirst(*view_, key_, sle_, entry_, index_))
37  {
38  assert(false);
39  }
40  }
41 }
42 
43 auto
45 {
47  if (key_ != beast::zero)
48  {
49  it.next_quality_ = next_quality_;
50  it.sle_ = sle_;
51  it.entry_ = entry_;
52  it.index_ = index_;
53  }
54  return it;
55 }
56 
57 auto
59 {
61 }
62 
65 
66 bool
68  BookDirs::const_iterator const& other) const
69 {
70  if (view_ == nullptr || other.view_ == nullptr)
71  return false;
72 
73  assert(view_ == other.view_ && root_ == other.root_);
74  return entry_ == other.entry_ && cur_key_ == other.cur_key_ &&
75  index_ == other.index_;
76 }
77 
80 {
81  assert(index_ != beast::zero);
82  if (!cache_)
83  cache_ = view_->read(keylet::offer(index_));
84  return *cache_;
85 }
86 
89 {
90  using beast::zero;
91 
92  assert(index_ != zero);
93  if (!cdirNext(*view_, cur_key_, sle_, entry_, index_))
94  {
95  if (index_ != 0 ||
96  (cur_key_ =
97  view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero)
98  {
99  cur_key_ = key_;
100  entry_ = 0;
101  index_ = zero;
102  }
103  else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_))
104  {
105  assert(false);
106  }
107  }
108 
109  cache_ = std::nullopt;
110  return *this;
111 }
112 
115 {
116  assert(index_ != beast::zero);
117  const_iterator tmp(*this);
118  ++(*this);
119  return tmp;
120 }
121 
122 } // namespace ripple
ripple::cdirNext
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:145
ripple::BookDirs::const_iterator::operator*
reference operator*() const
Definition: BookDirs.cpp:79
ripple::BookDirs::entry_
unsigned int entry_
Definition: BookDirs.h:35
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:82
ripple::BookDirs::root_
const uint256 root_
Definition: BookDirs.h:31
ripple::keylet::offer
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition: Indexes.cpp:222
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:100
ripple::BookDirs::key_
const uint256 key_
Definition: BookDirs.h:33
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
ripple::BookDirs::const_iterator::operator++
const_iterator & operator++()
Definition: BookDirs.cpp:88
ripple::BookDirs::const_iterator::view_
ReadView const * view_
Definition: BookDirs.h:97
ripple::BookDirs::index_
uint256 index_
Definition: BookDirs.h:36
ripple::BookDirs
Definition: BookDirs.h:27
ripple::BookDirs::BookDirs
BookDirs(ReadView const &, Book const &)
Definition: BookDirs.cpp:27
ripple::BookDirs::end
const_iterator end() const
Definition: BookDirs.cpp:58
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::BookDirs::const_iterator::operator==
bool operator==(const_iterator const &other) const
Definition: BookDirs.cpp:67
ripple::ReadView::succ
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.
ripple::BookDirs::const_iterator::entry_
unsigned int entry_
Definition: BookDirs.h:103
ripple::BookDirs::view_
ReadView const * view_
Definition: BookDirs.h:30
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::BookDirs::const_iterator
Definition: BookDirs.h:51
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::BookDirs::const_iterator::root_
uint256 root_
Definition: BookDirs.h:98
ripple::BookDirs::const_iterator::cur_key_
uint256 cur_key_
Definition: BookDirs.h:101
ripple::BookDirs::const_iterator::index_
uint256 index_
Definition: BookDirs.h:104
ripple::Book
Specifies an order book.
Definition: Book.h:33
ripple::BookDirs::const_iterator::j_
static beast::Journal j_
Definition: BookDirs.h:107
ripple::Dir::const_iterator::reference
value_type const & reference
Definition: Directory.h:54
ripple::cdirFirst
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:134
ripple::BookDirs::sle_
std::shared_ptr< SLE const > sle_
Definition: BookDirs.h:34
ripple::BookDirs::next_quality_
const uint256 next_quality_
Definition: BookDirs.h:32
beast
Definition: base_uint.h:641
ripple::BookDirs::begin
const_iterator begin() const
Definition: BookDirs.cpp:44