rippled
Directory.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/Directory.h>
22 
23 namespace ripple {
24 
26 
27 Dir::Dir(ReadView const& view, Keylet const& key)
28  : view_(&view), root_(key), sle_(view_->read(root_))
29 {
30  if (sle_ != nullptr)
31  indexes_ = &sle_->getFieldV256(sfIndexes);
32 }
33 
34 auto
36 {
37  auto it = const_iterator(*view_, root_, root_);
38  if (sle_ != nullptr)
39  {
40  it.sle_ = sle_;
41  if (!indexes_->empty())
42  {
43  it.indexes_ = indexes_;
44  it.it_ = std::begin(*indexes_);
45  it.index_ = *it.it_;
46  }
47  }
48 
49  return it;
50 }
51 
52 auto
54 {
55  return const_iterator(*view_, root_, root_);
56 }
57 
58 bool
60 {
61  if (view_ == nullptr || other.view_ == nullptr)
62  return false;
63 
64  assert(view_ == other.view_ && root_.key == other.root_.key);
65  return page_.key == other.page_.key && index_ == other.index_;
66 }
67 
70 {
71  assert(index_ != beast::zero);
72  if (!cache_)
74  return *cache_;
75 }
76 
79 {
80  assert(index_ != beast::zero);
81  if (++it_ != std::end(*indexes_))
82  {
83  index_ = *it_;
84  }
85  else
86  {
87  auto const next = sle_->getFieldU64(sfIndexNext);
88  if (next == 0)
89  {
90  page_.key = root_.key;
91  index_ = beast::zero;
92  }
93  else
94  {
95  page_ = keylet::page(root_, next);
96  sle_ = view_->read(page_);
97  assert(sle_);
98  indexes_ = &sle_->getFieldV256(sfIndexes);
99  if (indexes_->empty())
100  {
101  index_ = beast::zero;
102  }
103  else
104  {
105  it_ = std::begin(*indexes_);
106  index_ = *it_;
107  }
108  }
109  }
110 
111  cache_ = boost::none;
112  return *this;
113 }
114 
117 {
118  assert(index_ != beast::zero);
119  const_iterator tmp(*this);
120  ++(*this);
121  return tmp;
122 }
123 
124 } // namespace ripple
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::sfIndexNext
const SF_U64 sfIndexNext(access, STI_UINT64, 1, "IndexNext")
Definition: SField.h:395
ripple::Dir::const_iterator
Definition: Directory.h:49
ripple::Dir::begin
const_iterator begin() const
Definition: Directory.cpp:35
ripple::Dir::root_
Keylet root_
Definition: Directory.h:32
ripple::Dir::const_iterator::sle_
std::shared_ptr< SLE const > sle_
Definition: Directory.h:107
ripple::keylet::child
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition: Indexes.cpp:127
ripple::Dir::const_iterator::cache_
boost::optional< value_type > cache_
Definition: Directory.h:106
ripple::Dir::const_iterator::view_
ReadView const * view_
Definition: Directory.h:102
ripple::STVector256::empty
bool empty() const
Definition: STVector256.h:122
ripple::Dir::const_iterator::index_
uint256 index_
Definition: Directory.h:105
ripple::const_iterator
Dir::const_iterator const_iterator
Definition: Directory.cpp:25
ripple::Keylet::key
uint256 key
Definition: Keylet.h:41
ripple::Dir::const_iterator::page_
Keylet page_
Definition: Directory.h:104
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:291
ripple::Dir::indexes_
STVector256 const * indexes_
Definition: Directory.h:34
ripple::Dir::const_iterator::root_
Keylet root_
Definition: Directory.h:103
ripple::Dir::sle_
std::shared_ptr< SLE const > sle_
Definition: Directory.h:33
ripple::sfIndexes
const SF_Vec256 sfIndexes(access, STI_VECTOR256, 1, "Indexes", SField::sMD_Never)
Definition: SField.h:493
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:188
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:78
ripple::Dir::const_iterator::indexes_
STVector256 const * indexes_
Definition: Directory.h:108
std::begin
T begin(T... args)
ripple::Dir::Dir
Dir(ReadView const &, Keylet const &)
Definition: Directory.cpp:27
std::end
T end(T... args)
ripple::Dir::end
const_iterator end() const
Definition: Directory.cpp:53
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:59
ripple::Dir::const_iterator::operator*
reference operator*() const
Definition: Directory.cpp:69
ripple::Dir::const_iterator::it_
std::vector< uint256 >::const_iterator it_
Definition: Directory.h:109