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(
37  *view_,
38  key_,
39  sle_,
40  entry_,
41  index_,
43  {
44  assert(false);
45  }
46  }
47 }
48 
49 auto
51 {
53  if (key_ != beast::zero)
54  {
55  it.next_quality_ = next_quality_;
56  it.sle_ = sle_;
57  it.entry_ = entry_;
58  it.index_ = index_;
59  }
60  return it;
61 }
62 
63 auto
65 {
67 }
68 
71 
72 bool
74  BookDirs::const_iterator const& other) const
75 {
76  if (view_ == nullptr || other.view_ == nullptr)
77  return false;
78 
79  assert(view_ == other.view_ && root_ == other.root_);
80  return entry_ == other.entry_ && cur_key_ == other.cur_key_ &&
81  index_ == other.index_;
82 }
83 
86 {
87  assert(index_ != beast::zero);
88  if (!cache_)
89  cache_ = view_->read(keylet::offer(index_));
90  return *cache_;
91 }
92 
95 {
96  using beast::zero;
97 
98  assert(index_ != zero);
99  if (!cdirNext(*view_, cur_key_, sle_, entry_, index_, j_))
100  {
101  if (index_ != 0 ||
102  (cur_key_ =
103  view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero)
104  {
105  cur_key_ = key_;
106  entry_ = 0;
107  index_ = zero;
108  }
109  else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_, j_))
110  {
111  assert(false);
112  }
113  }
114 
115  cache_ = boost::none;
116  return *this;
117 }
118 
121 {
122  assert(index_ != beast::zero);
123  const_iterator tmp(*this);
124  ++(*this);
125  return tmp;
126 }
127 
128 } // namespace ripple
ripple::BookDirs::const_iterator::operator*
reference operator*() const
Definition: BookDirs.cpp:85
ripple::BookDirs::entry_
unsigned int entry_
Definition: BookDirs.h:35
ripple::cdirNext
bool cdirNext(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:498
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:79
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:223
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:97
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:94
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:64
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:73
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:192
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:32
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::ReadView::succ
virtual boost::optional< key_type > succ(key_type const &key, boost::optional< key_type > const &last=boost::none) const =0
Return the key of the next state item.
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
ripple::cdirFirst
bool cdirFirst(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:483
beast
Definition: base_uint.h:585
ripple::BookDirs::begin
const_iterator begin() const
Definition: BookDirs.cpp:50