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