| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without modification, are |
| 5 | // permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 8 | // conditions and the following disclaimer. |
| 9 | // |
| 10 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 11 | // of conditions and the following disclaimer in the documentation and/or other |
| 12 | // materials provided with the distribution. |
| 13 | // |
| 14 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 15 | // used to endorse or promote products derived from this software without specific |
| 16 | // prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 19 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 25 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 26 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | #include "database.h" |
| 28 | #include "lmdb/error.h" |
| 29 | #include "lmdb/util.h" |
| 30 | |
| 31 | #ifdef _WIN32 |
| 32 | namespace |
| 33 | { |
| 34 | constexpr const mdb_mode_t open_flags = 0; |
| 35 | } |
| 36 | #else |
| 37 | #include <sys/stat.h> |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | constexpr const mdb_mode_t open_flags = (S_IRUSR | S_IWUSR); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | namespace lmdb |
| 46 | { |
| 47 | namespace |
| 48 | { |
| 49 | constexpr const mdb_size_t max_resize = 1 * 1024 * 1024 * 1024; // 1 GB |
| 50 | void acquire_context(context& ctx) noexcept |
| 51 | { |
| 52 | while (ctx.lock.test_and_set()); |
| 53 | ++(ctx.active); |
| 54 | ctx.lock.clear(); |
| 55 | } |
| 56 | |
| 57 | void release_context(context& ctx) noexcept |
| 58 | { |
| 59 | --(ctx.active); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void release_read_txn::operator()(MDB_txn* ptr) const noexcept |
| 64 | { |
| 65 | if (ptr) |
| 66 | { |
| 67 | MDB_env* const env = mdb_txn_env(ptr); |
| 68 | abort_txn{}(ptr); |
| 69 | if (env) |
| 70 | { |
| 71 | context* ctx = reinterpret_cast<context*>(mdb_env_get_userctx(env)); |
| 72 | if (ctx) |
| 73 | release_context(*ctx); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | expect<environment> open_environment(const char* path, MDB_dbi max_dbs) noexcept |
| 79 | { |
| 80 | MONERO_PRECOND(path != nullptr); |
| 81 | |
| 82 | MDB_env* obj = nullptr; |
| 83 | MONERO_LMDB_CHECK(mdb_env_create(std::addressof(obj))); |
| 84 | environment out{obj}; |
| 85 | |
| 86 | MONERO_LMDB_CHECK(mdb_env_set_maxdbs(out.get(), max_dbs)); |
| 87 | MONERO_LMDB_CHECK(mdb_env_open(out.get(), path, 0, open_flags)); |
| 88 | return {std::move(out)}; |
| 89 | } |
| 90 | |
| 91 | expect<write_txn> database::do_create_txn(unsigned int flags) noexcept |
| 92 | { |
| 93 | MONERO_PRECOND(handle() != nullptr); |
| 94 | |
| 95 | for (unsigned attempts = 0; attempts < 3; ++attempts) |
| 96 | { |
| 97 | acquire_context(ctx); |
| 98 | |
| 99 | MDB_txn* txn = nullptr; |
| 100 | const int err = |
| 101 | mdb_txn_begin(handle(), nullptr, flags, &txn); |
| 102 | if (!err && txn != nullptr) |
| 103 | return write_txn{txn}; |
| 104 | |
| 105 | release_context(ctx); |
| 106 | if (err != MDB_MAP_RESIZED) |
| 107 | return {lmdb::error(err)}; |
| 108 | MONERO_CHECK(this->resize()); |
| 109 | } |
| 110 | return {lmdb::error(MDB_MAP_RESIZED)}; |
| 111 | } |
| 112 | |
| 113 | database::database(environment env) |
| 114 | : env(std::move(env)), ctx{{}, ATOMIC_FLAG_INIT} |
| 115 | { |
| 116 | if (handle()) |
| 117 | { |
| 118 | const int err = mdb_env_set_userctx(handle(), std::addressof(ctx)); |
| 119 | if (err) |
| 120 | MONERO_THROW(lmdb::error(err), "Failed to set user context"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | database::~database() noexcept |
| 125 | { |
| 126 | while (ctx.active); |
| 127 | } |
| 128 | |
| 129 | expect<void> database::resize() noexcept |
| 130 | { |
| 131 | MONERO_PRECOND(handle() != nullptr); |
| 132 | |
| 133 | while (ctx.lock.test_and_set()); |
| 134 | while (ctx.active); |
| 135 | |
| 136 | MDB_envinfo info{}; |
| 137 | MONERO_LMDB_CHECK(mdb_env_info(handle(), &info)); |
| 138 | |
| 139 | const mdb_size_t resize = std::min(info.me_mapsize, max_resize); |
| 140 | const int err = mdb_env_set_mapsize(handle(), info.me_mapsize + resize); |
| 141 | ctx.lock.clear(); |
| 142 | if (err) |
| 143 | return {lmdb::error(err)}; |
| 144 | return success(); |
| 145 | } |
| 146 | |
| 147 | expect<read_txn> database::create_read_txn(suspended_txn txn) noexcept |
| 148 | { |
| 149 | if (txn) |
| 150 | { |
| 151 | acquire_context(ctx); |
| 152 | const int err = mdb_txn_renew(txn.get()); |
| 153 | if (err) |
| 154 | { |
| 155 | release_context(ctx); |
| 156 | return {lmdb::error(err)}; |
| 157 | } |
| 158 | return read_txn{txn.release()}; |
| 159 | } |
| 160 | auto new_txn = do_create_txn(MDB_RDONLY); |
| 161 | if (new_txn) |
| 162 | return read_txn{new_txn->release()}; |
| 163 | return new_txn.error(); |
| 164 | } |
| 165 | |
| 166 | expect<suspended_txn> database::reset_txn(read_txn txn) noexcept |
| 167 | { |
| 168 | MONERO_PRECOND(txn != nullptr); |
| 169 | mdb_txn_reset(txn.get()); |
| 170 | release_context(ctx); |
| 171 | return suspended_txn{txn.release()}; |
| 172 | } |
| 173 | |
| 174 | expect<write_txn> database::create_write_txn() noexcept |
| 175 | { |
| 176 | return do_create_txn(0); |
| 177 | } |
| 178 | |
| 179 | expect<void> database::commit(write_txn txn) noexcept |
| 180 | { |
| 181 | MONERO_PRECOND(txn != nullptr); |
| 182 | MONERO_LMDB_CHECK(mdb_txn_commit(txn.get())); |
| 183 | txn.release(); |
| 184 | release_context(ctx); |
| 185 | return success(); |
| 186 | } |
| 187 | } // lmdb |