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 <xrpld/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_;
89 cache_ = std::nullopt;
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 }
132 cache_ = std::nullopt;
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:128
reference operator*() const
Definition: Dir.cpp:70
std::size_t page_size()
Definition: Dir.cpp:137
ReadView const * view_
Definition: Dir.h:121
value_type const & reference
Definition: Dir.h:67
std::optional< value_type > cache_
Definition: Dir.h:125
STVector256 const * indexes_
Definition: Dir.h:127
const_iterator & operator++()
Definition: Dir.cpp:81
std::shared_ptr< SLE const > sle_
Definition: Dir.h:126
std::shared_ptr< SLE const > sle_
Definition: Dir.h:46
const_iterator end() const
Definition: Dir.cpp:52
ReadView const * view_
Definition: Dir.h:44
STVector256 const * indexes_
Definition: Dir.h:47
Dir(ReadView const &, Keylet const &)
Definition: Dir.cpp:26
Keylet root_
Definition: Dir.h:45
const_iterator begin() const
Definition: Dir.cpp:34
A view into a ledger.
Definition: ReadView.h:52
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
Definition: STVector256.h:168
bool empty() const
Definition: STVector256.h:180
T end(T... args)
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition: Indexes.cpp:182
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition: Indexes.cpp:372
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
uint256 key
Definition: Keylet.h:40