| 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 | |
| 43 | /*! |
| 44 | An InputIterator for a fixed-sized LMDB key and value. `operator++` |
| 45 | iterates over keys. |
| 46 | |
| 47 | \tparam K Key type in database records. |
| 48 | \tparam V Value type in database records. |
| 49 | |
| 50 | \note This meets requirements for an InputIterator only. The iterator |
| 51 | can only be incremented and dereferenced. All copies of an iterator |
| 52 | share the same LMDB cursor, and therefore incrementing any copy will |
| 53 | change the cursor state for all (incrementing an iterator will |
| 54 | invalidate all prior copies of the iterator). Usage is identical |
| 55 | to `std::istream_iterator`. |
| 56 | */ |
| 57 | template<typename K, typename V> |
| 58 | class key_iterator |
| 59 | { |
| 60 | MDB_cursor* cur; |
| 61 | epee::span<const std::uint8_t> key; |
| 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>>>; |
| 72 | using reference = value_type; |
| 73 | using pointer = void; |
| 74 | using difference_type = std::size_t; |
| 75 | using iterator_category = std::input_iterator_tag; |
| 76 | |
| 77 | //! Construct an "end" iterator. |
| 78 | key_iterator() noexcept |
| 79 | : cur(nullptr), key() |
| 80 | {} |
| 81 | |
| 82 | /*! |
| 83 | \param cur Iterate over keys starting at this cursor position. |
| 84 | \throw std::system_error if unexpected LMDB error. This can happen |
| 85 | if `cur` is invalid. |
| 86 | */ |
| 87 | key_iterator(MDB_cursor* cur) |
| 88 | : cur(cur), key() |
| 89 | { |
| 90 | if (cur) |
| 91 | key = lmdb::stream::get(*cur, MDB_GET_CURRENT, sizeof(K), sizeof(V)).first; |
| 92 | } |
| 93 | |
| 94 | //! \return True if `this` is one-past the last key. |
| 95 | bool is_end() const noexcept { return key.empty(); } |
| 96 | |
| 97 | //! \return True iff `rhs` is referencing `this` key. |
| 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 | |
| 105 | /*! |
| 106 | Moves iterator to next key or end. Invalidates all prior copies of |
| 107 | the iterator. |
| 108 | */ |
| 109 | key_iterator& operator++() |
| 110 | { |
| 111 | increment(); |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | Moves iterator to next key or end. |
| 117 | |
| 118 | \return A copy that is already invalidated, ignore |
| 119 | */ |
| 120 | key_iterator operator++(int) |
| 121 | { |
| 122 | key_iterator out{*this}; |
| 123 | increment(); |
| 124 | return out; |
| 125 | } |
| 126 | |
| 127 | //! \pre `!is_end()` \return {current key, current value range} |
| 128 | value_type operator*() const |
| 129 | { |
| 130 | return {get_key(), make_value_range()}; |
| 131 | } |
| 132 | |
| 133 | //! \pre `!is_end()` \return Current key |
| 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 | |
| 143 | /*! |
| 144 | Return a C++ iterator over database values from current cursor |
| 145 | position that will reach `.is_end()` after the last duplicate key |
| 146 | record. Calling `make_iterator()` will return an iterator whose |
| 147 | `operator*` will return an entire value (`V`). |
| 148 | `make_iterator<MONERO_FIELD(account, id)>()` will return an |
| 149 | iterator whose `operator*` will return a `decltype(account.id)` |
| 150 | object - the other fields in the struct `account` are never copied |
| 151 | from the database. |
| 152 | |
| 153 | \throw std::system_error if LMDB has unexpected errors. |
| 154 | \return C++ iterator starting at current cursor position. |
| 155 | */ |
| 156 | template<typename T = V, typename F = T, std::size_t offset = 0> |
| 157 | value_iterator<T, F, offset> make_value_iterator() const |
| 158 | { |
| 159 | static_assert(std::is_same<T, V>(), "bad MONERO_FIELD usage?"); |
| 160 | return {cur}; |
| 161 | } |
| 162 | |
| 163 | /*! |
| 164 | Return a range from current cursor position until last duplicate |
| 165 | key record. Useful in for-each range loops or in templated code |
| 166 | expecting a range of elements. Calling `make_range()` will return |
| 167 | a range of `T` objects. `make_range<MONERO_FIELD(account, id)>()` |
| 168 | will return a range of `decltype(account.id)` objects - the other |
| 169 | fields in the struct `account` are never copied from the database. |
| 170 | |
| 171 | \throw std::system_error if LMDB has unexpected errors. |
| 172 | \return An InputIterator range over values at cursor position. |
| 173 | */ |
| 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 | { |
| 177 | return {make_value_iterator<T, F, offset>(), value_iterator<T, F, offset>{}}; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | /*! |
| 182 | C++ wrapper for a LMDB read-only cursor on a fixed-sized key `K` and |
| 183 | value `V`. |
| 184 | |
| 185 | \tparam K key type being stored by each record. |
| 186 | \tparam V value type being stored by each record. |
| 187 | \tparam D cleanup functor for the cursor; usually unique per db/table. |
| 188 | */ |
| 189 | template<typename K, typename V, typename D> |
| 190 | class key_stream |
| 191 | { |
| 192 | std::unique_ptr<MDB_cursor, D> cur; |
| 193 | public: |
| 194 | |
| 195 | //! Take ownership of `cur` without changing position. `nullptr` valid. |
| 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; |
| 203 | key_stream& operator=(key_stream&&) = default; |
| 204 | key_stream& operator=(key_stream const&) = delete; |
| 205 | |
| 206 | /*! |
| 207 | Give up ownership of the cursor. `make_iterator()` and |
| 208 | `make_range()` can still be invoked, but return the empty set. |
| 209 | |
| 210 | \return Currently owned LMDB cursor. |
| 211 | */ |
| 212 | std::unique_ptr<MDB_cursor, D> give_cursor() noexcept |
| 213 | { |
| 214 | return {std::move(cur)}; |
| 215 | } |
| 216 | |
| 217 | /*! |
| 218 | Place the stream back at the first key/value. Newly created |
| 219 | iterators will start at the first value again. |
| 220 | |
| 221 | \note Invalidates all current iterators, including those created |
| 222 | with `make_iterator` or `make_range`. Also invalidates all |
| 223 | `value_iterator`s created with `key_iterator`. |
| 224 | */ |
| 225 | void reset() |
| 226 | { |
| 227 | if (cur) |
| 228 | lmdb::stream::get(*cur, MDB_FIRST, 0, 0); |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | \throw std::system_error if LMDB has unexpected errors. |
| 233 | \return C++ iterator over database keys from current cursor |
| 234 | position that will reach `.is_end()` after the last key. |
| 235 | */ |
| 236 | key_iterator<K, V> make_iterator() const |
| 237 | { |
| 238 | return {cur.get()}; |
| 239 | } |
| 240 | |
| 241 | /*! |
| 242 | \throw std::system_error if LMDB has unexpected errors. |
| 243 | \return Range from current cursor position until last key record. |
| 244 | Useful in for-each range loops or in templated code |
| 245 | */ |
| 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 |