| 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 <lmdb.h> |
| 31 | #include <memory> |
| 32 | |
| 33 | #include "lmdb/error.h" |
| 34 | |
| 35 | //! Uses C++ type system to differentiate between cursors |
| 36 | #define MONERO_CURSOR(name) \ |
| 37 | struct close_ ## name : ::lmdb::close_cursor {}; \ |
| 38 | using name = std::unique_ptr< MDB_cursor, close_ ## name >; |
| 39 | |
| 40 | namespace lmdb |
| 41 | { |
| 42 | struct abort_txn |
| 43 | { |
| 44 | void operator()(MDB_txn* ptr) const noexcept |
| 45 | { |
| 46 | if (ptr) |
| 47 | mdb_txn_abort(ptr); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | /*! |
| 52 | Only valid if used via `create_read_txn()`. Decrements active count in |
| 53 | associated `context`, and aborts a LMDB transaction (`mdb_txn_abort`). |
| 54 | */ |
| 55 | struct release_read_txn |
| 56 | { |
| 57 | void operator()(MDB_txn* ptr) const noexcept; |
| 58 | // implementation in database.cpp |
| 59 | }; |
| 60 | |
| 61 | /*! |
| 62 | Only valid if used via `create_write_txn()`. Decrements active count in |
| 63 | associated `context`, and aborts a LMDB transaction (`mdb_txn_abort`). |
| 64 | */ |
| 65 | struct abort_write_txn |
| 66 | { |
| 67 | void operator()(MDB_txn* ptr) const noexcept |
| 68 | { |
| 69 | release_read_txn{}(ptr); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct close_cursor |
| 74 | { |
| 75 | void operator()(MDB_cursor* ptr) const noexcept |
| 76 | { |
| 77 | if (ptr) |
| 78 | mdb_cursor_close(ptr); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | template<typename D> |
| 83 | inline expect<std::unique_ptr<MDB_cursor, D>> |
| 84 | open_cursor(MDB_txn& txn, MDB_dbi tbl) noexcept |
| 85 | { |
| 86 | MDB_cursor* cur = nullptr; |
| 87 | MONERO_LMDB_CHECK(mdb_cursor_open(&txn, tbl, &cur)); |
| 88 | return std::unique_ptr<MDB_cursor, D>{cur}; |
| 89 | } |
| 90 | |
| 91 | // The below use the C++ type system to designate `MDB_txn` status. |
| 92 | |
| 93 | using suspended_txn = std::unique_ptr<MDB_txn, abort_txn>; |
| 94 | using read_txn = std::unique_ptr<MDB_txn, release_read_txn>; |
| 95 | using write_txn = std::unique_ptr<MDB_txn, abort_write_txn>; |
| 96 | } // lmdb |