| 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 "span.h" |
| 38 | |
| 39 | namespace lmdb |
| 40 | { |
| 41 | namespace stream |
| 42 | { |
| 43 | /* |
| 44 | \throw std::system_error if unexpected LMDB error. |
| 45 | \return 0 if `cur == nullptr`, otherwise count of values at current key. |
| 46 | */ |
| 47 | mdb_size_t count(MDB_cursor* cur); |
| 48 | |
| 49 | /*! |
| 50 | Calls `mdb_cursor_get` and does some error checking. |
| 51 | |
| 52 | \param cur is given to `mdb_cursor_get` without modification. |
| 53 | \param op is passed to `mdb_cursor_get` without modification. |
| 54 | \param key expected key size or 0 to skip key size check. |
| 55 | \param value expected value size or 0 to skip value size check. |
| 56 | |
| 57 | \throw std::system_error if `key != 0` and `key_.mv_size != key`. |
| 58 | \throw std::system_error if `value != 0` and `value_.mv_size != value`. |
| 59 | \throw std::system_error if `mdb_cursor_get` returns any error |
| 60 | other than `MDB_NOTFOUND`. |
| 61 | |
| 62 | \return {key bytes, value bytes} or two empty spans if `MDB_NOTFOUND`. |
| 63 | */ |
| 64 | std::pair<epee::span<const std::uint8_t>, epee::span<const std::uint8_t>> |
| 65 | get(MDB_cursor& cur, MDB_cursor_op op, std::size_t key, std::size_t value); |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | An InputIterator for a fixed-sized LMDB value at a specific key. |
| 70 | |
| 71 | \tparam T The value type at the specific key. |
| 72 | \tparam F The value type being returned when dereferenced. |
| 73 | \tparam offset to `F` within `T`. |
| 74 | |
| 75 | \note This meets requirements for an InputIterator only. The iterator |
| 76 | can only be incremented and dereferenced. All copies of an iterator |
| 77 | share the same LMDB cursor, and therefore incrementing any copy will |
| 78 | change the cursor state for all (incrementing an iterator will |
| 79 | invalidate all prior copies of the iterator). Usage is identical |
| 80 | to `std::istream_iterator`. |
| 81 | */ |
| 82 | template<typename T, typename F = T, std::size_t offset = 0> |
| 83 | class value_iterator |
| 84 | { |
| 85 | MDB_cursor* cur; |
| 86 | epee::span<const std::uint8_t> values; |
| 87 | |
| 88 | void increment() |
| 89 | { |
| 90 | values.remove_prefix(sizeof(T)); |
| 91 | if (values.empty() && cur) |
| 92 | values = lmdb::stream::get(*cur, MDB_NEXT_DUP, 0, sizeof(T)).second; |
| 93 | } |
| 94 | |
| 95 | public: |
| 96 | using value_type = F; |
| 97 | using reference = value_type; |
| 98 | using pointer = void; |
| 99 | using difference_type = std::size_t; |
| 100 | using iterator_category = std::input_iterator_tag; |
| 101 | |
| 102 | //! Construct an "end" iterator. |
| 103 | value_iterator() noexcept |
| 104 | : cur(nullptr), values() |
| 105 | {} |
| 106 | |
| 107 | /*! |
| 108 | \param cur Iterate over values starting at this cursor position. |
| 109 | \throw std::system_error if unexpected LMDB error. This can happen |
| 110 | if `cur` is invalid. |
| 111 | */ |
| 112 | value_iterator(MDB_cursor* cur) |
| 113 | : cur(cur), values() |
| 114 | { |
| 115 | if (cur) |
| 116 | values = lmdb::stream::get(*cur, MDB_GET_CURRENT, 0, sizeof(T)).second; |
| 117 | } |
| 118 | |
| 119 | value_iterator(value_iterator const&) = default; |
| 120 | ~value_iterator() = default; |
| 121 | value_iterator& operator=(value_iterator const&) = default; |
| 122 | |
| 123 | //! \return True if `this` is one-past the last value. |
| 124 | bool is_end() const noexcept { return values.empty(); } |
| 125 | |
| 126 | //! \return True iff `rhs` is referencing `this` value. |
| 127 | bool equal(value_iterator const& rhs) const noexcept |
| 128 | { |
| 129 | return |
| 130 | (values.empty() && rhs.values.empty()) || |
| 131 | values.data() == rhs.values.data(); |
| 132 | } |
| 133 | |
| 134 | //! Invalidates all prior copies of the iterator. |
| 135 | value_iterator& operator++() |
| 136 | { |
| 137 | increment(); |
| 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | //! \return A copy that is already invalidated, ignore |
| 142 | value_iterator operator++(int) |
| 143 | { |
| 144 | value_iterator out{*this}; |
| 145 | increment(); |
| 146 | return out; |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | Get a specific field within `F`. Default behavior is to return |
| 151 | the entirety of `U`, despite the filtering logic of `operator*`. |
| 152 | |
| 153 | \pre `!is_end()` |
| 154 | |
| 155 | \tparam U must match `T`, used for `MONERO_FIELD` sanity checking. |
| 156 | \tparam G field type to extract from the value |
| 157 | \tparam uoffset to `G` type, or `0` when `std::is_same<U, G>()`. |
| 158 | |
| 159 | \return The field `G`, at `uoffset` within `U`. |
| 160 | */ |
| 161 | template<typename U, typename G = U, std::size_t uoffset = 0> |
| 162 | G get_value() const noexcept |
| 163 | { |
| 164 | static_assert(std::is_same<U, T>(), "bad MONERO_FIELD usage?"); |
| 165 | static_assert(std::is_trivially_copyable<U>(), "value type must be memcpy safe"); |
| 166 | static_assert(std::is_trivially_copyable<G>(), "field type must be memcpy safe"); |
| 167 | static_assert(sizeof(G) + uoffset <= sizeof(U), "bad field and/or offset"); |
| 168 | assert(sizeof(G) + uoffset <= values.size()); |
| 169 | assert(!is_end()); |
| 170 | |
| 171 | G value; |
| 172 | std::memcpy(std::addressof(value), values.data() + uoffset, sizeof(value)); |
| 173 | return value; |
| 174 | } |
| 175 | |
| 176 | //! \pre `!is_end()` \return The field `F`, at `offset`, within `T`. |
| 177 | value_type operator*() const noexcept { return get_value<T, F, offset>(); } |
| 178 | }; |
| 179 | |
| 180 | /*! |
| 181 | C++ wrapper for a LMDB read-only cursor on a fixed-sized value `T`. |
| 182 | |
| 183 | \tparam T value type being stored by each record. |
| 184 | \tparam D cleanup functor for the cursor; usually unique per db/table. |
| 185 | */ |
| 186 | template<typename T, typename D> |
| 187 | class value_stream |
| 188 | { |
| 189 | std::unique_ptr<MDB_cursor, D> cur; |
| 190 | public: |
| 191 | |
| 192 | //! Take ownership of `cur` without changing position. `nullptr` valid. |
| 193 | explicit value_stream(std::unique_ptr<MDB_cursor, D> cur) |
| 194 | : cur(std::move(cur)) |
| 195 | {} |
| 196 | |
| 197 | value_stream(value_stream&&) = default; |
| 198 | value_stream(value_stream const&) = delete; |
| 199 | ~value_stream() = default; |
| 200 | value_stream& operator=(value_stream&&) = default; |
| 201 | value_stream& operator=(value_stream const&) = delete; |
| 202 | |
| 203 | /*! |
| 204 | Give up ownership of the cursor. `count()`, `make_iterator()` and |
| 205 | `make_range()` can still be invoked, but return the empty set. |
| 206 | |
| 207 | \return Currently owned LMDB cursor. |
| 208 | */ |
| 209 | std::unique_ptr<MDB_cursor, D> give_cursor() noexcept |
| 210 | { |
| 211 | return {std::move(cur)}; |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | Place the stream back at the first value. Newly created iterators |
| 216 | will start at the first value again. |
| 217 | |
| 218 | \note Invalidates all current iterators from `this`, including |
| 219 | those created with `make_iterator` or `make_range`. |
| 220 | */ |
| 221 | void reset() |
| 222 | { |
| 223 | if (cur) |
| 224 | lmdb::stream::get(*cur, MDB_FIRST_DUP, 0, 0); |
| 225 | } |
| 226 | |
| 227 | /*! |
| 228 | \throw std::system_error if LMDB has unexpected errors. |
| 229 | \return Number of values at this key. |
| 230 | */ |
| 231 | std::size_t count() const |
| 232 | { |
| 233 | return lmdb::stream::count(cur.get()); |
| 234 | } |
| 235 | |
| 236 | /*! |
| 237 | Return a C++ iterator over database values from current cursor |
| 238 | position that will reach `.is_end()` after the last duplicate key |
| 239 | record. Calling `make_iterator()` will return an iterator whose |
| 240 | `operator*` will return entire value (`T`). |
| 241 | `make_iterator<MONERO_FIELD(account, id)>()` will return an |
| 242 | iterator whose `operator*` will return a `decltype(account.id)` |
| 243 | object - the other fields in the struct `account` are never copied |
| 244 | from the database. |
| 245 | |
| 246 | \throw std::system_error if LMDB has unexpected errors. |
| 247 | \return C++ iterator starting at current cursor position. |
| 248 | */ |
| 249 | template<typename U = T, typename F = U, std::size_t offset = 0> |
| 250 | value_iterator<U, F, offset> make_iterator() const |
| 251 | { |
| 252 | static_assert(std::is_same<U, T>(), "was MONERO_FIELD used with wrong type?"); |
| 253 | return {cur.get()}; |
| 254 | } |
| 255 | |
| 256 | /*! |
| 257 | Return a range from current cursor position until last duplicate |
| 258 | key record. Useful in for-each range loops or in templated code |
| 259 | expecting a range of elements. Calling `make_range()` will return |
| 260 | a range of `T` objects. `make_range<MONERO_FIELD(account, id)>()` |
| 261 | will return a range of `decltype(account.id)` objects - the other |
| 262 | fields in the struct `account` are never copied from the database. |
| 263 | |
| 264 | \throw std::system_error if LMDB has unexpected errors. |
| 265 | \return An InputIterator range over values at cursor position. |
| 266 | */ |
| 267 | template<typename U = T, typename F = U, std::size_t offset = 0> |
| 268 | boost::iterator_range<value_iterator<U, F, offset>> make_range() const |
| 269 | { |
| 270 | return {make_iterator<U, F, offset>(), value_iterator<U, F, offset>{}}; |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | template<typename T, typename F, std::size_t offset> |
| 275 | inline |
| 276 | bool operator==(value_iterator<T, F, offset> const& lhs, value_iterator<T, F, offset> const& rhs) noexcept |
| 277 | { |
| 278 | return lhs.equal(rhs); |
| 279 | } |
| 280 | |
| 281 | template<typename T, typename F, std::size_t offset> |
| 282 | inline |
| 283 | bool operator!=(value_iterator<T, F, offset> const& lhs, value_iterator<T, F, offset> const& rhs) noexcept |
| 284 | { |
| 285 | return !lhs.equal(rhs); |
| 286 | } |
| 287 | } // lmdb |
| 288 |