rippled
Loading...
Searching...
No Matches
Dir.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 <xrpl/ledger/Dir.h>
21
22namespace ripple {
23
25
26Dir::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
33auto
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
51auto
53{
55}
56
57bool
59{
60 if (view_ == nullptr || other.view_ == nullptr)
61 return false;
62
63 XRPL_ASSERT(
64 view_ == other.view_ && root_.key == other.root_.key,
65 "ripple::const_iterator::operator== : views and roots are matching");
66 return page_.key == other.page_.key && index_ == other.index_;
67}
68
71{
72 XRPL_ASSERT(
73 index_ != beast::zero,
74 "ripple::const_iterator::operator* : nonzero index");
75 if (!cache_)
77 return *cache_;
78}
79
82{
83 XRPL_ASSERT(
84 index_ != beast::zero,
85 "ripple::const_iterator::operator++ : nonzero index");
86 if (++it_ != std::end(*indexes_))
87 {
88 index_ = *it_;
90 return *this;
91 }
92
93 return next_page();
94}
95
98{
99 XRPL_ASSERT(
100 index_ != beast::zero,
101 "ripple::const_iterator::operator++(int) : nonzero index");
102 const_iterator tmp(*this);
103 ++(*this);
104 return tmp;
105}
106
109{
110 auto const next = sle_->getFieldU64(sfIndexNext);
111 if (next == 0)
112 {
113 page_.key = root_.key;
114 index_ = beast::zero;
115 }
116 else
117 {
118 page_ = keylet::page(root_, next);
119 sle_ = view_->read(page_);
120 XRPL_ASSERT(sle_, "ripple::const_iterator::next_page : non-null SLE");
121 indexes_ = &sle_->getFieldV256(sfIndexes);
122 if (indexes_->empty())
123 {
124 index_ = beast::zero;
125 }
126 else
127 {
129 index_ = *it_;
130 }
131 }
133 return *this;
134}
135
138{
139 return indexes_->size();
140}
141
142} // namespace ripple
T begin(T... args)
bool operator==(const_iterator const &other) const
Definition Dir.cpp:58
const_iterator & next_page()
Definition Dir.cpp:108
std::vector< uint256 >::const_iterator it_
Definition Dir.h:127
reference operator*() const
Definition Dir.cpp:70
std::size_t page_size()
Definition Dir.cpp:137
ReadView const * view_
Definition Dir.h:120
value_type const & reference
Definition Dir.h:66
std::optional< value_type > cache_
Definition Dir.h:124
STVector256 const * indexes_
Definition Dir.h:126
const_iterator & operator++()
Definition Dir.cpp:81
std::shared_ptr< SLE const > sle_
Definition Dir.h:125
std::shared_ptr< SLE const > sle_
Definition Dir.h:45
const_iterator end() const
Definition Dir.cpp:52
ReadView const * view_
Definition Dir.h:43
STVector256 const * indexes_
Definition Dir.h:46
Dir(ReadView const &, Keylet const &)
Definition Dir.cpp:26
Keylet root_
Definition Dir.h:44
const_iterator begin() const
Definition Dir.cpp:34
A view into a ledger.
Definition ReadView.h:51
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
std::size_t size() const
bool empty() const
T end(T... args)
T is_same_v
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:190
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:380
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:39
uint256 key
Definition Keylet.h:40