rippled
Directory.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/Directory.h>
21 
22 namespace ripple {
23 
25 
26 Dir::Dir(ReadView const& view, Keylet const& key)
27  : view_(&view), root_(key), sle_(view_->read(root_))
28 {
29  if (sle_ != nullptr)
30  indexes_ = &sle_->getFieldV256(sfIndexes);
31 }
32 
33 auto
35 {
36  auto it = const_iterator(*view_, root_, root_);
37  if (sle_ != nullptr)
38  {
39  it.sle_ = sle_;
40  if (!indexes_->empty())
41  {
42  it.indexes_ = indexes_;
43  it.it_ = std::begin(*indexes_);
44  it.index_ = *it.it_;
45  }
46  }
47 
48  return it;
49 }
50 
51 auto
53 {
54  return const_iterator(*view_, root_, root_);
55 }
56 
57 bool
59 {
60  if (view_ == nullptr || other.view_ == nullptr)
61  return false;
62 
63  assert(view_ == other.view_ && root_.key == other.root_.key);
64  return page_.key == other.page_.key && index_ == other.index_;
65 }
66 
69 {
70  assert(index_ != beast::zero);
71  if (!cache_)
73  return *cache_;
74 }
75 
78 {
79  assert(index_ != beast::zero);
80  if (++it_ != std::end(*indexes_))
81  {
82  index_ = *it_;
83  cache_ = std::nullopt;
84  return *this;
85  }
86 
87  return next_page();
88 }
89 
92 {
93  assert(index_ != beast::zero);
94  const_iterator tmp(*this);
95  ++(*this);
96  return tmp;
97 }
98 
101 {
102  auto const next = sle_->getFieldU64(sfIndexNext);
103  if (next == 0)
104  {
105  page_.key = root_.key;
106  index_ = beast::zero;
107  }
108  else
109  {
110  page_ = keylet::page(root_, next);
111  sle_ = view_->read(page_);
112  assert(sle_);
113  indexes_ = &sle_->getFieldV256(sfIndexes);
114  if (indexes_->empty())
115  {
116  index_ = beast::zero;
117  }
118  else
119  {
120  it_ = std::begin(*indexes_);
121  index_ = *it_;
122  }
123  }
124  cache_ = std::nullopt;
125  return *this;
126 }
127 
130 {
131  return indexes_->size();
132 }
133 
134 } // namespace ripple
ripple::sfIndexNext
const SF_UINT64 sfIndexNext
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Dir::const_iterator
Definition: Directory.h:49
ripple::Dir::begin
const_iterator begin() const
Definition: Directory.cpp:34
ripple::Dir::root_
Keylet root_
Definition: Directory.h:32
ripple::Dir::const_iterator::sle_
std::shared_ptr< SLE const > sle_
Definition: Directory.h:113
ripple::Dir::const_iterator::cache_
std::optional< value_type > cache_
Definition: Directory.h:112
ripple::keylet::child
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition: Indexes.cpp:139
ripple::Dir::const_iterator::view_
ReadView const * view_
Definition: Directory.h:108
ripple::STVector256::empty
bool empty() const
Definition: STVector256.h:178
ripple::Dir::const_iterator::index_
uint256 index_
Definition: Directory.h:111
ripple::const_iterator
Dir::const_iterator const_iterator
Definition: Directory.cpp:24
ripple::sfIndexes
const SF_VECTOR256 sfIndexes
ripple::Keylet::key
uint256 key
Definition: Keylet.h:40
ripple::Dir::const_iterator::page_
Keylet page_
Definition: Directory.h:110
ripple::Dir::view_
ReadView const * view_
Definition: Directory.h:31
ripple::keylet::page
Keylet page(uint256 const &key, std::uint64_t index) noexcept
A page in a directory.
Definition: Indexes.cpp:309
ripple::Dir::indexes_
STVector256 const * indexes_
Definition: Directory.h:34
ripple::STVector256::size
std::size_t size() const
Definition: STVector256.h:166
ripple::Dir::const_iterator::root_
Keylet root_
Definition: Directory.h:109
ripple::Dir::sle_
std::shared_ptr< SLE const > sle_
Definition: Directory.h:33
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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::Dir::const_iterator::operator++
const_iterator & operator++()
Definition: Directory.cpp:77
ripple::Dir::const_iterator::indexes_
STVector256 const * indexes_
Definition: Directory.h:114
std::begin
T begin(T... args)
ripple::Dir::const_iterator::page_size
std::size_t page_size()
Definition: Directory.cpp:129
ripple::Dir::Dir
Dir(ReadView const &, Keylet const &)
Definition: Directory.cpp:26
std::size_t
ripple::Dir::const_iterator::next_page
const_iterator & next_page()
Definition: Directory.cpp:100
std::end
T end(T... args)
ripple::Dir::end
const_iterator end() const
Definition: Directory.cpp:52
ripple::Dir::const_iterator::reference
value_type const & reference
Definition: Directory.h:54
ripple::Dir::const_iterator::operator==
bool operator==(const_iterator const &other) const
Definition: Directory.cpp:58
ripple::Dir::const_iterator::operator*
reference operator*() const
Definition: Directory.cpp:68
ripple::Dir::const_iterator::it_
std::vector< uint256 >::const_iterator it_
Definition: Directory.h:115