Monero
Loading...
Searching...
No Matches
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
40namespace 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 static_assert(std::is_trivially_copyable<K>(), "key is not memcpy safe");
137 assert(!is_end());
138 K out;
139 std::memcpy(std::addressof(out), key.data(), sizeof(out));
140 return out;
141 }
142
156 template<typename T = V, typename F = T, std::size_t offset = 0>
158 {
159 static_assert(std::is_same<T, V>(), "bad MONERO_FIELD usage?");
160 return {cur};
161 }
162
174 template<typename T = V, typename F = T, std::size_t offset = 0>
175 boost::iterator_range<value_iterator<T, F, offset>> make_value_range() const
176 {
178 }
179 };
180
189 template<typename K, typename V, typename D>
191 {
192 std::unique_ptr<MDB_cursor, D> cur;
193 public:
194
196 explicit key_stream(std::unique_ptr<MDB_cursor, D> cur)
197 : cur(std::move(cur))
198 {}
199
200 key_stream(key_stream&&) = default;
201 key_stream(key_stream const&) = delete;
202 ~key_stream() = default;
204 key_stream& operator=(key_stream const&) = delete;
205
212 std::unique_ptr<MDB_cursor, D> give_cursor() noexcept
213 {
214 return {std::move(cur)};
215 }
216
225 void reset()
226 {
227 if (cur)
229 }
230
237 {
238 return {cur.get()};
239 }
240
246 boost::iterator_range<key_iterator<K, V>> make_range() const
247 {
248 return {make_iterator(), key_iterator<K, V>{}};
249 }
250 };
251
252 template<typename K, typename V>
253 inline
254 bool operator==(key_iterator<K, V> const& lhs, key_iterator<K, V> const& rhs) noexcept
255 {
256 return lhs.equal(rhs);
257 }
258
259 template<typename K, typename V>
260 inline
261 bool operator!=(key_iterator<K, V> const& lhs, key_iterator<K, V> const& rhs) noexcept
262 {
263 return !lhs.equal(rhs);
264 }
265} // lmdb
266
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
Definition key_stream.h:59
void pointer
Definition key_stream.h:73
key_iterator & operator++()
Definition key_stream.h:109
value_iterator< T, F, offset > make_value_iterator() const
Definition key_stream.h:157
boost::iterator_range< value_iterator< T, F, offset > > make_value_range() const
Definition key_stream.h:175
void increment()
Definition key_stream.h:63
key_iterator() noexcept
Construct an "end" iterator.
Definition key_stream.h:78
std::pair< K, boost::iterator_range< value_iterator< V > > > value_type
Definition key_stream.h:71
bool is_end() const noexcept
Definition key_stream.h:95
K get_key() const noexcept
Definition key_stream.h:134
std::input_iterator_tag iterator_category
Definition key_stream.h:75
key_iterator(MDB_cursor *cur)
Definition key_stream.h:87
key_iterator operator++(int)
Definition key_stream.h:120
epee::span< const std::uint8_t > key
Definition key_stream.h:61
bool equal(key_iterator const &rhs) const noexcept
Definition key_stream.h:98
value_type reference
Definition key_stream.h:72
std::size_t difference_type
Definition key_stream.h:74
MDB_cursor * cur
Definition key_stream.h:60
value_type operator*() const
Definition key_stream.h:128
std::unique_ptr< MDB_cursor, D > give_cursor() noexcept
Definition key_stream.h:212
key_stream(key_stream const &)=delete
key_stream(std::unique_ptr< MDB_cursor, D > cur)
Take ownership of cur without changing position. nullptr valid.
Definition key_stream.h:196
key_stream & operator=(key_stream &&)=default
void reset()
Definition key_stream.h:225
boost::iterator_range< key_iterator< K, V > > make_range() const
Definition key_stream.h:246
key_iterator< K, V > make_iterator() const
Definition key_stream.h:236
std::unique_ptr< MDB_cursor, D > cur
Definition key_stream.h:192
~key_stream()=default
key_stream & operator=(key_stream const &)=delete
key_stream(key_stream &&)=default
Definition value_stream.h:84
@ MDB_GET_CURRENT
Definition lmdb.h:404
@ MDB_FIRST
Definition lmdb.h:399
@ MDB_NEXT_NODUP
Definition lmdb.h:417
#define const
Definition ipfrdr.c:80
Lightning memory-mapped database library.
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
Definition database.cpp:46
bool operator==(key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
Definition key_stream.h:254
bool operator!=(key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
Definition key_stream.h:261
Definition enums.h:68
Definition mdb.c:1372