rippled
BookDirs.cpp
1 //------------ ------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2015 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/ledger/BookDirs.h>
21 #include <ripple/ledger/View.h>
22 #include <ripple/protocol/Indexes.h>
23 
24 namespace ripple {
25 
26 BookDirs::BookDirs(ReadView const& view, Book const& book)
27  : view_(&view)
28  , root_(keylet::page(getBookBase(book)).key)
29  , next_quality_(getQualityNext(root_))
30  , key_(view_->succ(root_, next_quality_).value_or(beast::zero))
31 {
32  assert(root_ != beast::zero);
33  if (key_ != beast::zero)
34  {
35  if (! cdirFirst(*view_, key_, sle_, entry_, index_,
37  {
38  assert(false);
39  }
40  }
41 }
42 
43 auto
44 BookDirs::begin() const ->
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 
58 auto
59 BookDirs::end() const ->
61 {
63 }
64 
65 
68 
69 bool
70 BookDirs::const_iterator::operator==
71  (BookDirs::const_iterator const& other) const
72 {
73  if (view_ == nullptr || other.view_ == nullptr)
74  return false;
75 
76  assert(view_ == other.view_ && root_ == other.root_);
77  return entry_ == other.entry_ &&
78  cur_key_ == other.cur_key_ &&
79  index_ == other.index_;
80 }
81 
84 {
85  assert(index_ != beast::zero);
86  if (! cache_)
88  return *cache_;
89 }
90 
93 {
94  using beast::zero;
95 
96  assert(index_ != zero);
97  if (! cdirNext(*view_, cur_key_, sle_, entry_, index_, j_))
98  {
99  if (index_ != 0 ||
100  (cur_key_ = view_->succ(++cur_key_,
101  next_quality_).value_or(zero)) == zero)
102  {
103  cur_key_ = key_;
104  entry_ = 0;
105  index_ = zero;
106  }
107  else if (! cdirFirst(*view_, cur_key_,
108  sle_, entry_, index_, j_))
109  {
110  assert(false);
111  }
112  }
113 
114  cache_ = boost::none;
115  return *this;
116 }
117 
120 {
121  assert(index_ != beast::zero);
122  const_iterator tmp(*this);
123  ++(*this);
124  return tmp;
125 }
126 
127 } // ripple
ripple::BookDirs::const_iterator::operator*
reference operator*() const
Definition: BookDirs.cpp:83
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:472
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:67
ripple::BookDirs::root_
const uint256 root_
Definition: BookDirs.h:31
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:127
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:67
ripple::BookDirs::const_iterator::operator++
const_iterator & operator++()
Definition: BookDirs.cpp:92
ripple::BookDirs::const_iterator::view_
ReadView const * view_
Definition: BookDirs.h:103
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:26
ripple::BookDirs::end
const_iterator end() const
Definition: BookDirs.cpp:59
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:186
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::BookDirs::const_iterator::cache_
boost::optional< value_type > cache_
Definition: BookDirs.h:111
ripple::BookDirs::const_iterator::index_
uint256 index_
Definition: BookDirs.h:110
ripple::Book
Specifies an order book.
Definition: Book.h:32
ripple::BookDirs::const_iterator::j_
static beast::Journal j_
Definition: BookDirs.h:113
ripple::Dir::const_iterator::reference
value_type const & reference
Definition: Directory.h:57
ripple::keylet::offer
static const offer_t offer
Definition: Indexes.h:191
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:458
beast
Definition: base_uint.h:582
ripple::BookDirs::begin
const_iterator begin() const
Definition: BookDirs.cpp:44