Monero
key_stream.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2022, The Monero Project
2 
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #pragma once
29 
30 #include <boost/range/iterator_range.hpp>
31 #include <cstdint>
32 #include <cstring>
33 #include <iterator>
34 #include <lmdb.h>
35 #include <utility>
36 
37 #include "lmdb/value_stream.h"
38 #include "span.h"
39 
40 namespace lmdb
41 {
42 
57  template<typename K, typename V>
59  {
62 
63  void increment()
64  {
65  // MDB_NEXT_MULTIPLE doesn't work if only one value is stored :/
66  if (cur)
67  key = lmdb::stream::get(*cur, MDB_NEXT_NODUP, sizeof(K), sizeof(V)).first;
68  }
69 
70  public:
71  using value_type = std::pair<K, boost::iterator_range<value_iterator<V>>>;
73  using pointer = void;
74  using difference_type = std::size_t;
75  using iterator_category = std::input_iterator_tag;
76 
78  key_iterator() noexcept
79  : cur(nullptr), key()
80  {}
81 
88  : cur(cur), key()
89  {
90  if (cur)
91  key = lmdb::stream::get(*cur, MDB_GET_CURRENT, sizeof(K), sizeof(V)).first;
92  }
93 
95  bool is_end() const noexcept { return key.empty(); }
96 
98  bool equal(key_iterator const& rhs) const noexcept
99  {
100  return
101  (key.empty() && rhs.key.empty()) ||
102  key.data() == rhs.key.data();
103  }
104 
110  {
111  increment();
112  return *this;
113  }
114 
121  {
122  key_iterator out{*this};
123  increment();
124  return out;
125  }
126 
129  {
130  return {get_key(), make_value_range()};
131  }
132 
134  K get_key() const noexcept
135  {
136  assert(!is_end());
137  K out;
138  std::memcpy(std::addressof(out), key.data(), sizeof(out));
139  return out;
140  }
141 
155  template<typename T = V, typename F = T, std::size_t offset = 0>
157  {
158  static_assert(std::is_same<T, V>(), "bad MONERO_FIELD usage?");
159  return {cur};
160  }
161 
173  template<typename T = V, typename F = T, std::size_t offset = 0>
174  boost::iterator_range<value_iterator<T, F, offset>> make_value_range() const
175  {
176  return {make_value_iterator<T, F, offset>(), value_iterator<T, F, offset>{}};
177  }
178  };
179 
188  template<typename K, typename V, typename D>
190  {
191  std::unique_ptr<MDB_cursor, D> cur;
192  public:
193 
195  explicit key_stream(std::unique_ptr<MDB_cursor, D> cur)
196  : cur(std::move(cur))
197  {}
198 
199  key_stream(key_stream&&) = default;
200  key_stream(key_stream const&) = delete;
201  ~key_stream() = default;
202  key_stream& operator=(key_stream&&) = default;
203  key_stream& operator=(key_stream const&) = delete;
204 
211  std::unique_ptr<MDB_cursor, D> give_cursor() noexcept
212  {
213  return {std::move(cur)};
214  }
215 
224  void reset()
225  {
226  if (cur)
228  }
229 
236  {
237  return {cur.get()};
238  }
239 
245  boost::iterator_range<key_iterator<K, V>> make_range() const
246  {
247  return {make_iterator(), key_iterator<K, V>{}};
248  }
249  };
250 
251  template<typename K, typename V>
252  inline
253  bool operator==(key_iterator<K, V> const& lhs, key_iterator<K, V> const& rhs) noexcept
254  {
255  return lhs.equal(rhs);
256  }
257 
258  template<typename K, typename V>
259  inline
260  bool operator!=(key_iterator<K, V> const& lhs, key_iterator<K, V> const& rhs) noexcept
261  {
262  return !lhs.equal(rhs);
263  }
264 } // lmdb
265 
bool operator!=(key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
Definition: key_stream.h:260
Lightning memory-mapped database library.
bool is_end() const noexcept
Definition: key_stream.h:95
key_iterator(MDB_cursor *cur)
Definition: key_stream.h:87
Definition: key_stream.h:189
epee::span< const std::uint8_t > key
Definition: key_stream.h:61
std::pair< epee::span< const std::uint8_t >, epee::span< const std::uint8_t > > get(MDB_cursor &cur, MDB_cursor_op op, std::size_t key, std::size_t value)
Definition: value_stream.cpp:53
void increment()
Definition: key_stream.h:63
std::unique_ptr< MDB_cursor, D > give_cursor() noexcept
Definition: key_stream.h:211
Definition: value_stream.h:83
Definition: enums.h:67
boost::iterator_range< key_iterator< K, V > > make_range() const
Definition: key_stream.h:245
MDB_cursor * cur
Definition: key_stream.h:60
void pointer
Definition: key_stream.h:73
key_iterator & operator++()
Definition: key_stream.h:109
bool equal(key_iterator const &rhs) const noexcept
Definition: key_stream.h:98
key_stream & operator=(key_stream &&)=default
Definition: lmdb.h:417
Definition: lmdb.h:399
void reset()
Definition: key_stream.h:224
Definition: key_stream.h:58
~key_stream()=default
std::input_iterator_tag iterator_category
Definition: key_stream.h:75
value_iterator< T, F, offset > make_value_iterator() const
Definition: key_stream.h:156
key_iterator() noexcept
Construct an "end" iterator.
Definition: key_stream.h:78
key_iterator operator++(int)
Definition: key_stream.h:120
std::pair< K, boost::iterator_range< value_iterator< V > >> value_type
Definition: key_stream.h:71
const T & move(const T &t)
Definition: gtest-port.h:1317
key_iterator< K, V > make_iterator() const
Definition: key_stream.h:235
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:16
value_type operator*() const
Definition: key_stream.h:128
Definition: database.cpp:45
Definition: lmdb.h:404
K get_key() const noexcept
Definition: key_stream.h:134
bool operator==(key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
Definition: key_stream.h:253
Definition: mdb.c:1372
std::unique_ptr< MDB_cursor, D > cur
Definition: key_stream.h:191
constexpr bool empty() const noexcept
Definition: span.h:107
#define const
Definition: ipfrdr.c:80
key_stream(std::unique_ptr< MDB_cursor, D > cur)
Take ownership of cur without changing position. nullptr valid.
Definition: key_stream.h:195
std::size_t difference_type
Definition: key_stream.h:74
boost::iterator_range< value_iterator< T, F, offset > > make_value_range() const
Definition: key_stream.h:174
Definition: blockchain_usage.cpp:71
constexpr pointer data() const noexcept
Definition: span.h:108