| 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 <atomic> |
| 31 | #include <cstddef> |
| 32 | #include <lmdb.h> |
| 33 | #include <memory> |
| 34 | #include <type_traits> |
| 35 | |
| 36 | #include "common/expect.h" |
| 37 | #include "lmdb/error.h" |
| 38 | #include "lmdb/transaction.h" |
| 39 | |
| 40 | namespace lmdb |
| 41 | { |
| 42 | //! Closes LMDB environment handle. |
| 43 | struct close_env |
| 44 | { |
| 45 | void operator()(MDB_env* ptr) const noexcept |
| 46 | { |
| 47 | if (ptr) |
| 48 | mdb_env_close(ptr); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | using environment = std::unique_ptr<MDB_env, close_env>; |
| 53 | |
| 54 | //! \return LMDB environment at `path` with a max of `max_dbs` tables. |
| 55 | expect<environment> open_environment(const char* path, MDB_dbi max_dbs) noexcept; |
| 56 | |
| 57 | //! Context given to LMDB. |
| 58 | struct context |
| 59 | { |
| 60 | std::atomic<std::size_t> active; |
| 61 | std::atomic_flag lock; |
| 62 | }; |
| 63 | |
| 64 | //! Manages a LMDB environment for safe memory-map resizing. Thread-safe. |
| 65 | class database |
| 66 | { |
| 67 | environment env; |
| 68 | context ctx; |
| 69 | |
| 70 | //! \return The LMDB environment associated with the object. |
| 71 | MDB_env* handle() const noexcept { return env.get(); } |
| 72 | |
| 73 | expect<write_txn> do_create_txn(unsigned int flags) noexcept; |
| 74 | |
| 75 | public: |
| 76 | database(environment env); |
| 77 | |
| 78 | database(database&&) = delete; |
| 79 | database(database const&) = delete; |
| 80 | |
| 81 | virtual ~database() noexcept; |
| 82 | |
| 83 | database& operator=(database&&) = delete; |
| 84 | database& operator=(database const&) = delete; |
| 85 | |
| 86 | /*! |
| 87 | Resize the memory map for the LMDB environment. Will block until |
| 88 | all reads/writes on the environment complete. |
| 89 | */ |
| 90 | expect<void> resize() noexcept; |
| 91 | |
| 92 | //! \return A read only LMDB transaction, reusing `txn` if provided. |
| 93 | expect<read_txn> create_read_txn(suspended_txn txn = nullptr) noexcept; |
| 94 | |
| 95 | //! \return `txn` after releasing context. |
| 96 | expect<suspended_txn> reset_txn(read_txn txn) noexcept; |
| 97 | |
| 98 | //! \return A read-write LMDB transaction. |
| 99 | expect<write_txn> create_write_txn() noexcept; |
| 100 | |
| 101 | //! Commit the read-write transaction. |
| 102 | expect<void> commit(write_txn txn) noexcept; |
| 103 | |
| 104 | /*! |
| 105 | Create a write transaction, pass it to `f`, then try to commit |
| 106 | the write if `f` succeeds. |
| 107 | |
| 108 | \tparam F must be callable with signature `expect<T>(MDB_txn&)`. |
| 109 | \param f must be re-startable if `lmdb::error(MDB_MAP_FULL)`. |
| 110 | |
| 111 | \return The result of calling `f`. |
| 112 | */ |
| 113 | template<typename F> |
| 114 | typename std::result_of<F(MDB_txn&)>::type try_write(F f, unsigned attempts = 3) |
| 115 | { |
| 116 | for (unsigned i = 0; i < attempts; ++i) |
| 117 | { |
| 118 | expect<write_txn> txn = create_write_txn(); |
| 119 | if (!txn) |
| 120 | return txn.error(); |
| 121 | |
| 122 | MONERO_PRECOND(*txn != nullptr); |
| 123 | const auto wrote = f(*(*txn)); |
| 124 | if (wrote) |
| 125 | { |
| 126 | MONERO_CHECK(commit(std::move(*txn))); |
| 127 | return wrote; |
| 128 | } |
| 129 | if (wrote != lmdb::error(MDB_MAP_FULL)) |
| 130 | return wrote; |
| 131 | |
| 132 | txn->reset(); |
| 133 | MONERO_CHECK(this->resize()); |
| 134 | } |
| 135 | return {lmdb::error(MDB_MAP_FULL)}; |
| 136 | } |
| 137 | }; |
| 138 | } // lmdb |
| 139 |