From b6a36c5b1d453dbb45f34a47f002042888342fd0 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 21:08:19 +0100 Subject: [PATCH 01/18] Include IWYU --- nano/store/transaction.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/nano/store/transaction.hpp b/nano/store/transaction.hpp index 23459a9258..c875babafe 100644 --- a/nano/store/transaction.hpp +++ b/nano/store/transaction.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace nano::store From b76d827a7c034741adf4e60fd3332dc026ff293b Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Tue, 10 Sep 2024 12:58:55 +0100 Subject: [PATCH 02/18] Add /Zc:__cplusplus flag for windows so __cplusplus macro is correctly versioned. --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0614d03be0..01d1d1322f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,9 @@ if(COVERAGE) endif() if(MSVC) - add_definitions(/MP) + add_compile_options(/MP) + add_compile_options(/Zc:__cplusplus) # Tell MSVC to emit correct version + # number in the __cplusplus macro endif() set(CPACK_PACKAGE_VENDOR "Nano Currency") From 498ea2c60336618512556c0fcddfaf4c60d195f3 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 17:17:20 +0100 Subject: [PATCH 03/18] Removing unused MDB_dbi handles. --- nano/store/lmdb/account.hpp | 12 ------------ nano/store/lmdb/lmdb.cpp | 6 ++---- nano/store/lmdb/pending.hpp | 12 ------------ 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/nano/store/lmdb/account.hpp b/nano/store/lmdb/account.hpp index 6faaf3cfe8..53870f85f6 100644 --- a/nano/store/lmdb/account.hpp +++ b/nano/store/lmdb/account.hpp @@ -28,18 +28,6 @@ class account : public nano::store::account store::iterator end () const override; void for_each_par (std::function, store::iterator)> const & action_a) const override; - /** - * Maps account v1 to account information, head, rep, open, balance, timestamp and block count. (Removed) - * nano::account -> nano::block_hash, nano::block_hash, nano::block_hash, nano::amount, uint64_t, uint64_t - */ - MDB_dbi accounts_v0_handle{ 0 }; - - /** - * Maps account v0 to account information, head, rep, open, balance, timestamp and block count. (Removed) - * nano::account -> nano::block_hash, nano::block_hash, nano::block_hash, nano::amount, uint64_t, uint64_t - */ - MDB_dbi accounts_v1_handle{ 0 }; - /** * Maps account v0 to account information, head, rep, open, balance, timestamp, block count and epoch * nano::account -> nano::block_hash, nano::block_hash, nano::block_hash, nano::amount, uint64_t, uint64_t, nano::epoch diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index a2fbb618ed..4f5c63bcb6 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -200,10 +200,8 @@ void nano::store::lmdb::component::open_databases (bool & error_a, store::transa error_a |= mdb_dbi_open (env.tx (transaction_a), "peers", flags, &peer_store.peers_handle) != 0; error_a |= mdb_dbi_open (env.tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle) != 0; error_a |= mdb_dbi_open (env.tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts", flags, &account_store.accounts_v0_handle) != 0; - account_store.accounts_handle = account_store.accounts_v0_handle; - error_a |= mdb_dbi_open (env.tx (transaction_a), "pending", flags, &pending_store.pending_v0_handle) != 0; - pending_store.pending_handle = pending_store.pending_v0_handle; + error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts", flags, &account_store.accounts_handle) != 0; + error_a |= mdb_dbi_open (env.tx (transaction_a), "pending", flags, &pending_store.pending_handle) != 0; error_a |= mdb_dbi_open (env.tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle) != 0; error_a |= mdb_dbi_open (env.tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle) != 0; error_a |= mdb_dbi_open (env.tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle) != 0; diff --git a/nano/store/lmdb/pending.hpp b/nano/store/lmdb/pending.hpp index 13cd9172d2..fd8a5d13a7 100644 --- a/nano/store/lmdb/pending.hpp +++ b/nano/store/lmdb/pending.hpp @@ -27,18 +27,6 @@ class pending : public nano::store::pending store::iterator end () const override; void for_each_par (std::function, store::iterator)> const & action_a) const override; - /** - * Maps min_version 0 (destination account, pending block) to (source account, amount). (Removed) - * nano::account, nano::block_hash -> nano::account, nano::amount - */ - MDB_dbi pending_v0_handle{ 0 }; - - /** - * Maps min_version 1 (destination account, pending block) to (source account, amount). (Removed) - * nano::account, nano::block_hash -> nano::account, nano::amount - */ - MDB_dbi pending_v1_handle{ 0 }; - /** * Maps (destination account, pending block) to (source account, amount, version). (Removed) * nano::account, nano::block_hash -> nano::account, nano::amount, nano::epoch From ba72e18827bb54d4de817714d70c6303b9e15b0c Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 16:27:34 +0100 Subject: [PATCH 04/18] Clear up lmdb::drop and lmdb::clear which weren't acting as their function name described. --- nano/store/lmdb/confirmation_height.cpp | 2 +- nano/store/lmdb/final_vote.cpp | 2 +- nano/store/lmdb/lmdb.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nano/store/lmdb/confirmation_height.cpp b/nano/store/lmdb/confirmation_height.cpp index 1ce01edab4..814fca4fea 100644 --- a/nano/store/lmdb/confirmation_height.cpp +++ b/nano/store/lmdb/confirmation_height.cpp @@ -56,7 +56,7 @@ void nano::store::lmdb::confirmation_height::clear (store::write_transaction con void nano::store::lmdb::confirmation_height::clear (store::write_transaction const & transaction_a) { - store.drop (transaction_a, nano::tables::confirmation_height); + store.clear (transaction_a, store.table_to_dbi (nano::tables::confirmation_height)); } nano::store::iterator nano::store::lmdb::confirmation_height::begin (store::transaction const & transaction, nano::account const & account) const diff --git a/nano/store/lmdb/final_vote.cpp b/nano/store/lmdb/final_vote.cpp index 2ae93bd5a9..5615bce90b 100644 --- a/nano/store/lmdb/final_vote.cpp +++ b/nano/store/lmdb/final_vote.cpp @@ -61,7 +61,7 @@ void nano::store::lmdb::final_vote::clear (store::write_transaction const & tran void nano::store::lmdb::final_vote::clear (store::write_transaction const & transaction_a) { - store.drop (transaction_a, nano::tables::final_votes); + store.clear (transaction_a, store.table_to_dbi (nano::tables::final_votes)); } nano::store::iterator nano::store::lmdb::final_vote::begin (store::transaction const & transaction, nano::qualified_root const & root) const diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 4f5c63bcb6..37b96b60ed 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -254,7 +254,7 @@ void nano::store::lmdb::component::upgrade_v22_to_v23 (store::write_transaction { logger.info (nano::log::type::lmdb, "Upgrading database from v22 to v23..."); - drop (transaction, tables::rep_weights); + clear (transaction, table_to_dbi (tables::rep_weights)); transaction.refresh (); release_assert (rep_weight.begin (tx_begin_read ()) == rep_weight.end (), "rep weights table must be empty before upgrading to v23"); @@ -371,7 +371,7 @@ int nano::store::lmdb::component::del (store::write_transaction const & transact int nano::store::lmdb::component::drop (store::write_transaction const & transaction_a, tables table_a) { - return clear (transaction_a, table_to_dbi (table_a)); + return mdb_drop (env.tx (transaction_a), table_to_dbi (table_a), 1); } int nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, MDB_dbi handle_a) From 548b5e6d7c4639eca53333f1afb0717dcea39dde Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 8 Sep 2024 18:15:10 +0100 Subject: [PATCH 05/18] Rename init to error --- nano/core_test/wallet.cpp | 152 +++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 951e9d0878..4fad8b9ae7 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -16,13 +16,13 @@ unsigned constexpr nano::wallet_store::version_current; TEST (wallet, no_special_keys_accounts) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::keypair key1; ASSERT_FALSE (wallet.exists (transaction, key1.pub)); wallet.insert_adhoc (transaction, key1.prv); @@ -37,13 +37,13 @@ TEST (wallet, no_special_keys_accounts) TEST (wallet, no_key) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::keypair key1; nano::raw_key prv1; ASSERT_TRUE (wallet.fetch (transaction, key1.pub, prv1)); @@ -52,12 +52,12 @@ TEST (wallet, no_key) TEST (wallet, fetch_locked) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_TRUE (wallet.valid_password (transaction)); nano::keypair key1; ASSERT_EQ (key1.pub, wallet.insert_adhoc (transaction, key1.prv)); @@ -74,13 +74,13 @@ TEST (wallet, fetch_locked) TEST (wallet, retrieval) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::keypair key1; ASSERT_TRUE (wallet.valid_password (transaction)); wallet.insert_adhoc (transaction, key1.prv); @@ -96,13 +96,13 @@ TEST (wallet, retrieval) TEST (wallet, empty_iteration) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); auto i (wallet.begin (transaction)); auto j (wallet.end ()); ASSERT_EQ (i, j); @@ -110,13 +110,13 @@ TEST (wallet, empty_iteration) TEST (wallet, one_item_iteration) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::keypair key1; wallet.insert_adhoc (transaction, key1.prv); for (auto i (wallet.begin (transaction)), j (wallet.end ()); i != j; ++i) @@ -132,9 +132,9 @@ TEST (wallet, one_item_iteration) TEST (wallet, two_item_iteration) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); nano::keypair key1; nano::keypair key2; ASSERT_NE (key1.pub, key2.pub); @@ -143,8 +143,8 @@ TEST (wallet, two_item_iteration) nano::kdf kdf{ nano::dev::network_params.kdf_work }; { auto transaction (env.tx_begin_write ()); - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); wallet.insert_adhoc (transaction, key1.prv); wallet.insert_adhoc (transaction, key2.prv); for (auto i (wallet.begin (transaction)), j (wallet.end ()); i != j; ++i) @@ -258,26 +258,26 @@ TEST (wallet, spend_no_previous) TEST (wallet, find_none) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::account account (1000); ASSERT_EQ (wallet.end (), wallet.find (transaction, account)); } TEST (wallet, find_existing) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::keypair key1; ASSERT_FALSE (wallet.exists (transaction, key1.pub)); wallet.insert_adhoc (transaction, key1.prv); @@ -290,17 +290,17 @@ TEST (wallet, find_existing) TEST (wallet, rekey) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::raw_key password; wallet.password.value (password); ASSERT_TRUE (password.is_zero ()); - ASSERT_FALSE (init); + ASSERT_FALSE (error); nano::keypair key1; wallet.insert_adhoc (transaction, key1.prv); nano::raw_key prv1; @@ -362,13 +362,13 @@ TEST (account, encode_fail) TEST (wallet, hash_password) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); nano::raw_key hash1; wallet.derive_key (hash1, transaction, ""); nano::raw_key hash2; @@ -411,32 +411,32 @@ TEST (fan, change) TEST (wallet, reopen_default_password) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); - ASSERT_FALSE (init); nano::kdf kdf{ nano::dev::network_params.kdf_work }; { - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); ASSERT_TRUE (wallet.valid_password (transaction)); } { - bool init; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + bool error = false; + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); ASSERT_TRUE (wallet.valid_password (transaction)); } { - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); wallet.rekey (transaction, ""); ASSERT_TRUE (wallet.valid_password (transaction)); } { - bool init; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); - ASSERT_FALSE (init); + bool error = false; + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + ASSERT_FALSE (error); ASSERT_FALSE (wallet.valid_password (transaction)); wallet.attempt_password (transaction, " "); ASSERT_FALSE (wallet.valid_password (transaction)); @@ -712,12 +712,12 @@ TEST (wallet, insert_locked) TEST (wallet, deterministic_keys) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); auto key1 = wallet.deterministic_key (transaction, 0); auto key2 = wallet.deterministic_key (transaction, 0); ASSERT_EQ (key1, key2); @@ -755,12 +755,12 @@ TEST (wallet, deterministic_keys) TEST (wallet, reseed) { - bool init; - nano::store::lmdb::env env (init, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (init); + bool error = false; + nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); + ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); nano::raw_key seed1; seed1 = 1; nano::raw_key seed2; From cc39da9cfb80c6448eecff5ad3c0ef287524cc08 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 8 Sep 2024 09:20:50 +0100 Subject: [PATCH 06/18] Adding lmdbxx submodule. --- .gitmodules | 3 +++ submodules/lmdbxx | 1 + 2 files changed, 4 insertions(+) create mode 160000 submodules/lmdbxx diff --git a/.gitmodules b/.gitmodules index c1d9a0bf26..b9de47e792 100644 --- a/.gitmodules +++ b/.gitmodules @@ -37,3 +37,6 @@ [submodule "submodules/fmt"] path = submodules/fmt url = https://github.com/fmtlib/fmt.git +[submodule "submodules/lmdbxx"] + path = submodules/lmdbxx + url = https://github.com/hoytech/lmdbxx.git diff --git a/submodules/lmdbxx b/submodules/lmdbxx new file mode 160000 index 0000000000..6f497d1d8e --- /dev/null +++ b/submodules/lmdbxx @@ -0,0 +1 @@ +Subproject commit 6f497d1d8e1a6e0afa8ae83891d13b3fac68b62c From 3e543c7ceb672108cab2a04d50c452024d8e1974 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 8 Sep 2024 15:12:52 +0100 Subject: [PATCH 07/18] Moving mdb::env::tx off of mdb:env and in to nano::store::lmdb namespace as a separation of concerns. --- nano/core_test/block_store.cpp | 8 ++--- nano/node/wallet.cpp | 34 +++++++++---------- nano/store/CMakeLists.txt | 2 ++ nano/store/lmdb/iterator.hpp | 3 +- nano/store/lmdb/lmdb.cpp | 62 +++++++++++++++++----------------- nano/store/lmdb/lmdb_env.cpp | 6 ---- nano/store/lmdb/lmdb_env.hpp | 1 - nano/store/lmdb/utility.cpp | 7 ++++ nano/store/lmdb/utility.hpp | 10 ++++++ 9 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 nano/store/lmdb/utility.cpp create mode 100644 nano/store/lmdb/utility.hpp diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 82cc5b35c0..a6a7e50b08 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1376,7 +1376,7 @@ TEST (mdb_block_store, upgrade_v21_v22) auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); MDB_dbi unchecked_handle{ 0 }; - ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store.env.tx (transaction), "unchecked", 0, &unchecked_handle)); + ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store::lmdb::tx (transaction), "unchecked", 0, &unchecked_handle)); }; // Testing current version doesn't contain the unchecked table @@ -1388,7 +1388,7 @@ TEST (mdb_block_store, upgrade_v21_v22) auto transaction (store.tx_begin_write ()); store.version.put (transaction, 21); MDB_dbi unchecked_handle{ 0 }; - ASSERT_FALSE (mdb_dbi_open (store.env.tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); + ASSERT_FALSE (mdb_dbi_open (store::lmdb::tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); ASSERT_EQ (store.version.get (transaction), 21); } @@ -1412,7 +1412,7 @@ TEST (mdb_block_store, upgrade_v23_v24) auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); MDB_dbi frontiers_handle{ 0 }; - ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store.env.tx (transaction), "frontiers", 0, &frontiers_handle)); + ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store::lmdb::tx (transaction), "frontiers", 0, &frontiers_handle)); }; // Testing current version doesn't contain the frontiers table @@ -1424,7 +1424,7 @@ TEST (mdb_block_store, upgrade_v23_v24) auto transaction (store.tx_begin_write ()); store.version.put (transaction, 23); MDB_dbi frontiers_handle{ 0 }; - ASSERT_FALSE (mdb_dbi_open (store.env.tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); + ASSERT_FALSE (mdb_dbi_open (store::lmdb::tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); ASSERT_EQ (store.version.get (transaction), 23); } diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 764e3fea15..e41e454c4e 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -270,7 +270,7 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans if (!init_a) { MDB_val junk; - debug_assert (mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) == MDB_NOTFOUND); + debug_assert (mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) == MDB_NOTFOUND); boost::property_tree::ptree wallet_l; std::stringstream istream (json_a); try @@ -303,11 +303,11 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans init_a = true; } } - init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) != 0; - init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (wallet_key_special), &junk) != 0; - init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (salt_special), &junk) != 0; - init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (check_special), &junk) != 0; - init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (representative_special), &junk) != 0; + init_a |= mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) != 0; + init_a |= mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (wallet_key_special), &junk) != 0; + init_a |= mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (salt_special), &junk) != 0; + init_a |= mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (check_special), &junk) != 0; + init_a |= mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (representative_special), &junk) != 0; nano::raw_key key; key.clear (); password.value_set (key); @@ -328,7 +328,7 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans { int version_status; MDB_val version_value; - version_status = mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &version_value); + version_status = mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &version_value); if (version_status == MDB_NOTFOUND) { version_put (transaction_a, version_current); @@ -383,7 +383,7 @@ void nano::wallet_store::initialize (store::transaction const & transaction_a, b debug_assert (strlen (path_a.c_str ()) == path_a.size ()); auto error (0); MDB_dbi handle_l; - error |= mdb_dbi_open (env.tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); + error |= mdb_dbi_open (store::lmdb::tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); handle = handle_l; init_a = error != 0; } @@ -430,7 +430,7 @@ bool nano::wallet_store::insert_watch (store::transaction const & transaction_a, void nano::wallet_store::erase (store::transaction const & transaction_a, nano::account const & pub) { - auto status (mdb_del (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub), nullptr)); + auto status (mdb_del (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (pub), nullptr)); (void)status; debug_assert (status == 0); } @@ -439,7 +439,7 @@ nano::wallet_value nano::wallet_store::entry_get_raw (store::transaction const & { nano::wallet_value result; nano::store::lmdb::db_val value; - auto status (mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), value)); + auto status (mdb_get (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), value)); if (status == 0) { result = nano::wallet_value (value); @@ -454,7 +454,7 @@ nano::wallet_value nano::wallet_store::entry_get_raw (store::transaction const & void nano::wallet_store::entry_put_raw (store::transaction const & transaction_a, nano::account const & pub_a, nano::wallet_value const & entry_a) { - auto status (mdb_put (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), nano::store::lmdb::db_val (sizeof (entry_a), const_cast (&entry_a)), 0)); + auto status (mdb_put (store::lmdb::tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), nano::store::lmdb::db_val (sizeof (entry_a), const_cast (&entry_a)), 0)); (void)status; debug_assert (status == 0); } @@ -830,7 +830,7 @@ void nano::wallet::serialize (std::string & json_a) void nano::wallet_store::destroy (store::transaction const & transaction_a) { - auto status (mdb_drop (env.tx (transaction_a), handle, 1)); + auto status (mdb_drop (store::lmdb::tx (transaction_a), handle, 1)); (void)status; debug_assert (status == 0); handle = 0; @@ -956,7 +956,7 @@ std::shared_ptr nano::wallet::send_action (nano::account const & so if (id_mdb_val) { nano::store::lmdb::db_val result; - auto status (mdb_get (wallets.env.tx (transaction), wallets.node.wallets.send_action_ids, *id_mdb_val, result)); + auto status (mdb_get (store::lmdb::tx (transaction), wallets.node.wallets.send_action_ids, *id_mdb_val, result)); if (status == 0) { nano::block_hash hash (result); @@ -996,7 +996,7 @@ std::shared_ptr nano::wallet::send_action (nano::account const & so details.epoch = info->epoch (); if (id_mdb_val && block != nullptr) { - auto status (mdb_put (wallets.env.tx (transaction), wallets.node.wallets.send_action_ids, *id_mdb_val, nano::store::lmdb::db_val (block->hash ()), 0)); + auto status (mdb_put (store::lmdb::tx (transaction), wallets.node.wallets.send_action_ids, *id_mdb_val, nano::store::lmdb::db_val (block->hash ()), 0)); if (status != 0) { block = nullptr; @@ -1355,8 +1355,8 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) : if (!error_a) { auto transaction (tx_begin_write ()); - auto status (mdb_dbi_open (env.tx (transaction), nullptr, MDB_CREATE, &handle)); - status |= mdb_dbi_open (env.tx (transaction), "send_action_ids", MDB_CREATE, &send_action_ids); + auto status (mdb_dbi_open (store::lmdb::tx (transaction), nullptr, MDB_CREATE, &handle)); + status |= mdb_dbi_open (store::lmdb::tx (transaction), "send_action_ids", MDB_CREATE, &send_action_ids); release_assert (status == 0); std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); @@ -1634,7 +1634,7 @@ nano::store::read_transaction nano::wallets::tx_begin_read () void nano::wallets::clear_send_ids (store::transaction const & transaction_a) { - auto status (mdb_drop (env.tx (transaction_a), send_action_ids, 0)); + auto status (mdb_drop (store::lmdb::tx (transaction_a), send_action_ids, 0)); (void)status; debug_assert (status == 0); } diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index 68cde103a1..3bab8b87c3 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -25,6 +25,7 @@ add_library( lmdb/pruned.hpp lmdb/rep_weight.hpp lmdb/transaction_impl.hpp + lmdb/utility.hpp lmdb/version.hpp lmdb/wallet_value.hpp online_weight.hpp @@ -71,6 +72,7 @@ add_library( lmdb/pending.cpp lmdb/pruned.cpp lmdb/rep_weight.cpp + lmdb/utility.cpp lmdb/version.cpp lmdb/wallet_value.cpp online_weight.cpp diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index 3c2fe197c7..ebf6c19f41 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -17,7 +18,7 @@ class iterator : public iterator_impl iterator (store::transaction const & transaction_a, env const & env_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : nano::store::iterator_impl (transaction_a) { - auto status (mdb_cursor_open (env_a.tx (transaction_a), db_a, &cursor)); + auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor)); release_assert (status == 0); auto operation (MDB_SET_RANGE); if (val_a.mv_size != 0) diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 37b96b60ed..f959aaaf76 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -53,7 +53,7 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste auto is_fresh_db (false); { auto transaction (tx_begin_read ()); - auto err = mdb_dbi_open (env.tx (transaction), "meta", 0, &version_store.meta_handle); + auto err = mdb_dbi_open (tx (transaction), "meta", 0, &version_store.meta_handle); is_fresh_db = err != MDB_SUCCESS; if (err == MDB_SUCCESS) { @@ -195,16 +195,16 @@ nano::store::lmdb::txn_callbacks nano::store::lmdb::component::create_txn_callba void nano::store::lmdb::component::open_databases (bool & error_a, store::transaction const & transaction_a, unsigned flags) { - error_a |= mdb_dbi_open (env.tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "meta", flags, &version_store.meta_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "peers", flags, &peer_store.peers_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts", flags, &account_store.accounts_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "pending", flags, &pending_store.pending_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle) != 0; - error_a |= mdb_dbi_open (env.tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "meta", flags, &version_store.meta_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "peers", flags, &peer_store.peers_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "accounts", flags, &account_store.accounts_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "pending", flags, &pending_store.pending_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle) != 0; + error_a |= mdb_dbi_open (tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle) != 0; } bool nano::store::lmdb::component::do_upgrades (store::write_transaction & transaction, nano::ledger_constants & constants, bool & needs_vacuuming) @@ -242,8 +242,8 @@ void nano::store::lmdb::component::upgrade_v21_to_v22 (store::write_transaction logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22..."); MDB_dbi unchecked_handle{ 0 }; - release_assert (!mdb_dbi_open (env.tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); - release_assert (!mdb_drop (env.tx (transaction), unchecked_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. + release_assert (!mdb_dbi_open (tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); + release_assert (!mdb_drop (tx (transaction), unchecked_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. version.put (transaction, 22); logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22 completed"); @@ -313,8 +313,8 @@ void nano::store::lmdb::component::upgrade_v23_to_v24 (store::write_transaction logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24..."); MDB_dbi frontiers_handle{ 0 }; - release_assert (!mdb_dbi_open (env.tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); - release_assert (!mdb_drop (env.tx (transaction), frontiers_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. + release_assert (!mdb_dbi_open (tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); + release_assert (!mdb_drop (tx (transaction), frontiers_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. version.put (transaction, 24); logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24 completed"); } @@ -356,27 +356,27 @@ bool nano::store::lmdb::component::exists (store::transaction const & transactio int nano::store::lmdb::component::get (store::transaction const & transaction_a, tables table_a, nano::store::lmdb::db_val const & key_a, nano::store::lmdb::db_val & value_a) const { - return mdb_get (env.tx (transaction_a), table_to_dbi (table_a), key_a, value_a); + return mdb_get (tx (transaction_a), table_to_dbi (table_a), key_a, value_a); } int nano::store::lmdb::component::put (store::write_transaction const & transaction_a, tables table_a, nano::store::lmdb::db_val const & key_a, nano::store::lmdb::db_val const & value_a) const { - return (mdb_put (env.tx (transaction_a), table_to_dbi (table_a), key_a, value_a, 0)); + return (mdb_put (tx (transaction_a), table_to_dbi (table_a), key_a, value_a, 0)); } int nano::store::lmdb::component::del (store::write_transaction const & transaction_a, tables table_a, nano::store::lmdb::db_val const & key_a) const { - return (mdb_del (env.tx (transaction_a), table_to_dbi (table_a), key_a, nullptr)); + return (mdb_del (tx (transaction_a), table_to_dbi (table_a), key_a, nullptr)); } int nano::store::lmdb::component::drop (store::write_transaction const & transaction_a, tables table_a) { - return mdb_drop (env.tx (transaction_a), table_to_dbi (table_a), 1); + return mdb_drop (tx (transaction_a), table_to_dbi (table_a), 1); } int nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, MDB_dbi handle_a) { - return mdb_drop (env.tx (transaction_a), handle_a, 0); + return mdb_drop (tx (transaction_a), handle_a, 0); } uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, tables table_a) const @@ -387,7 +387,7 @@ uint64_t nano::store::lmdb::component::count (store::transaction const & transac uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, MDB_dbi db_a) const { MDB_stat stats; - auto status (mdb_stat (env.tx (transaction_a), db_a, &stats)); + auto status (mdb_stat (tx (transaction_a), db_a, &stats)); release_assert_success (status); return (stats.ms_entries); } @@ -454,46 +454,46 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & for (auto const & table : tables) { MDB_dbi temp; - mdb_dbi_open (env.tx (transaction_a), "temp_table", MDB_CREATE, &temp); + mdb_dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, table))), n (store::iterator (nullptr)); i != n; ++i) { - auto s = mdb_put (env.tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); + auto s = mdb_put (tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); release_assert_success (s); } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Clear existing table - mdb_drop (env.tx (transaction_a), table, 0); + mdb_drop (tx (transaction_a), table, 0); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { - auto s = mdb_put (env.tx (transaction_a), table, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); + auto s = mdb_put (tx (transaction_a), table, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); release_assert_success (s); } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Remove temporary table - mdb_drop (env.tx (transaction_a), temp, 1); + mdb_drop (tx (transaction_a), temp, 1); } // Pending table { MDB_dbi temp; - mdb_dbi_open (env.tx (transaction_a), "temp_table", MDB_CREATE, &temp); + mdb_dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, pending_store.pending_handle))), n (store::iterator (nullptr)); i != n; ++i) { - auto s = mdb_put (env.tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); + auto s = mdb_put (tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - mdb_drop (env.tx (transaction_a), pending_store.pending_handle, 0); + mdb_drop (tx (transaction_a), pending_store.pending_handle, 0); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { - auto s = mdb_put (env.tx (transaction_a), pending_store.pending_handle, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); + auto s = mdb_put (tx (transaction_a), pending_store.pending_handle, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - mdb_drop (env.tx (transaction_a), temp, 1); + mdb_drop (tx (transaction_a), temp, 1); } } diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index cd1897c80a..05560a8e04 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -100,9 +100,3 @@ nano::store::write_transaction nano::store::lmdb::env::tx_begin_write (store::lm { return store::write_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; } - -MDB_txn * nano::store::lmdb::env::tx (store::transaction const & transaction_a) const -{ - debug_assert (transaction_a.store_id () == store_id); - return static_cast (transaction_a.get_handle ()); -} diff --git a/nano/store/lmdb/lmdb_env.hpp b/nano/store/lmdb/lmdb_env.hpp index e912653250..f4ea347b22 100644 --- a/nano/store/lmdb/lmdb_env.hpp +++ b/nano/store/lmdb/lmdb_env.hpp @@ -61,7 +61,6 @@ class env final operator MDB_env * () const; store::read_transaction tx_begin_read (txn_callbacks callbacks = txn_callbacks{}) const; store::write_transaction tx_begin_write (txn_callbacks callbacks = txn_callbacks{}) const; - MDB_txn * tx (store::transaction const & transaction_a) const; MDB_env * environment; nano::id_t const store_id{ nano::next_id () }; }; diff --git a/nano/store/lmdb/utility.cpp b/nano/store/lmdb/utility.cpp new file mode 100644 index 0000000000..132bd05f02 --- /dev/null +++ b/nano/store/lmdb/utility.cpp @@ -0,0 +1,7 @@ +#include +#include + +MDB_txn * nano::store::lmdb::tx (store::transaction const & transaction_a) +{ + return static_cast (transaction_a.get_handle ()); +} diff --git a/nano/store/lmdb/utility.hpp b/nano/store/lmdb/utility.hpp new file mode 100644 index 0000000000..952e38995a --- /dev/null +++ b/nano/store/lmdb/utility.hpp @@ -0,0 +1,10 @@ +#include + +namespace nano::store +{ +class transaction; +} +namespace nano::store::lmdb +{ +MDB_txn * tx (store::transaction const & transaction_a); +} From fda250f233fbea8bcb70a57ac302c238d2826ccc Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 8 Sep 2024 17:25:48 +0100 Subject: [PATCH 08/18] Move transaction creation off of nano::store::lmdb::env --- nano/core_test/wallet.cpp | 38 ++++++++++++++-------------- nano/node/wallet.cpp | 4 +-- nano/node/wallet.hpp | 2 ++ nano/store/lmdb/lmdb.cpp | 4 +-- nano/store/lmdb/lmdb.hpp | 2 ++ nano/store/lmdb/lmdb_env.cpp | 10 -------- nano/store/lmdb/lmdb_env.hpp | 3 --- nano/store/lmdb/transaction.cpp | 2 -- nano/store/lmdb/transaction_impl.hpp | 4 +-- nano/store/rocksdb/rocksdb.cpp | 4 +-- nano/store/rocksdb/rocksdb.hpp | 2 ++ nano/store/transaction.cpp | 35 +++++++++++++------------ nano/store/transaction.hpp | 18 ++++++------- 13 files changed, 58 insertions(+), 70 deletions(-) diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 4fad8b9ae7..b4096f58f5 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -19,7 +19,7 @@ TEST (wallet, no_special_keys_accounts) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -40,7 +40,7 @@ TEST (wallet, no_key) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -55,7 +55,7 @@ TEST (wallet, fetch_locked) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_TRUE (wallet.valid_password (transaction)); @@ -77,7 +77,7 @@ TEST (wallet, retrieval) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -99,7 +99,7 @@ TEST (wallet, empty_iteration) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -113,7 +113,7 @@ TEST (wallet, one_item_iteration) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -142,7 +142,7 @@ TEST (wallet, two_item_iteration) std::unordered_set prvs; nano::kdf kdf{ nano::dev::network_params.kdf_work }; { - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); wallet.insert_adhoc (transaction, key1.prv); @@ -261,7 +261,7 @@ TEST (wallet, find_none) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -274,7 +274,7 @@ TEST (wallet, find_existing) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -293,7 +293,7 @@ TEST (wallet, rekey) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -365,7 +365,7 @@ TEST (wallet, hash_password) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -414,7 +414,7 @@ TEST (wallet, reopen_default_password) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; { nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -450,7 +450,7 @@ TEST (wallet, representative) auto error (false); nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -471,7 +471,7 @@ TEST (wallet, serialize_json_empty) auto error (false); nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -496,7 +496,7 @@ TEST (wallet, serialize_json_one) auto error (false); nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -525,7 +525,7 @@ TEST (wallet, serialize_json_password) auto error (false); nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -558,7 +558,7 @@ TEST (wallet_store, move) auto error (false); nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); @@ -715,7 +715,7 @@ TEST (wallet, deterministic_keys) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); auto key1 = wallet.deterministic_key (transaction, 0); @@ -758,7 +758,7 @@ TEST (wallet, reseed) bool error = false; nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); ASSERT_FALSE (error); - auto transaction (env.tx_begin_write ()); + nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); nano::raw_key seed1; diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index e41e454c4e..d198f9cce5 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1624,12 +1624,12 @@ void nano::wallets::start () nano::store::write_transaction nano::wallets::tx_begin_write () { - return env.tx_begin_write (); + return store::write_transaction{ std::make_unique (env), store_id }; } nano::store::read_transaction nano::wallets::tx_begin_read () { - return env.tx_begin_read (); + return store::read_transaction{ std::make_unique (env), store_id }; } void nano::wallets::clear_send_ids (store::transaction const & transaction_a) diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index 7366ec4702..98e60be93e 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -248,6 +248,8 @@ class wallets final private: mutable nano::mutex reps_cache_mutex; nano::wallet_representatives representatives; + + nano::id_t const store_id{ nano::next_id () }; }; std::unique_ptr collect_container_info (wallets & wallets, std::string const & name); diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index f959aaaf76..884700b955 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -165,12 +165,12 @@ void nano::store::lmdb::component::serialize_memory_stats (boost::property_tree: nano::store::write_transaction nano::store::lmdb::component::tx_begin_write (std::vector const &, std::vector const &) { - return env.tx_begin_write (create_txn_callbacks ()); + return store::write_transaction{ std::make_unique (env, create_txn_callbacks ()), store_id }; } nano::store::read_transaction nano::store::lmdb::component::tx_begin_read () const { - return env.tx_begin_read (create_txn_callbacks ()); + return store::read_transaction{ std::make_unique (env, create_txn_callbacks ()), store_id }; } std::string nano::store::lmdb::component::vendor_get () const diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index 290b19f813..5e7700d6dd 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -157,5 +157,7 @@ class component : public nano::store::component friend class mdb_block_store_supported_version_upgrades_Test; friend class mdb_block_store_upgrade_v21_v22_Test; friend class block_store_DISABLED_change_dupsort_Test; + + nano::id_t const store_id{ nano::next_id () }; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index 05560a8e04..106f9dea72 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -90,13 +90,3 @@ nano::store::lmdb::env::operator MDB_env * () const { return environment; } - -nano::store::read_transaction nano::store::lmdb::env::tx_begin_read (store::lmdb::txn_callbacks mdb_txn_callbacks) const -{ - return store::read_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; -} - -nano::store::write_transaction nano::store::lmdb::env::tx_begin_write (store::lmdb::txn_callbacks mdb_txn_callbacks) const -{ - return store::write_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; -} diff --git a/nano/store/lmdb/lmdb_env.hpp b/nano/store/lmdb/lmdb_env.hpp index f4ea347b22..ca470e8ec2 100644 --- a/nano/store/lmdb/lmdb_env.hpp +++ b/nano/store/lmdb/lmdb_env.hpp @@ -59,9 +59,6 @@ class env final void init (bool &, std::filesystem::path const &, env::options options_a = env::options::make ()); ~env (); operator MDB_env * () const; - store::read_transaction tx_begin_read (txn_callbacks callbacks = txn_callbacks{}) const; - store::write_transaction tx_begin_write (txn_callbacks callbacks = txn_callbacks{}) const; MDB_env * environment; - nano::id_t const store_id{ nano::next_id () }; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/transaction.cpp b/nano/store/lmdb/transaction.cpp index 2460539cfb..b1bd92c097 100644 --- a/nano/store/lmdb/transaction.cpp +++ b/nano/store/lmdb/transaction.cpp @@ -36,7 +36,6 @@ class matches_txn final } nano::store::lmdb::read_transaction_impl::read_transaction_impl (nano::store::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : - store::read_transaction_impl (environment_a.store_id), txn_callbacks (txn_callbacks_a) { auto status (mdb_txn_begin (environment_a, nullptr, MDB_RDONLY, &handle)); @@ -71,7 +70,6 @@ void * nano::store::lmdb::read_transaction_impl::get_handle () const } nano::store::lmdb::write_transaction_impl::write_transaction_impl (nano::store::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : - store::write_transaction_impl (environment_a.store_id), env (environment_a), txn_callbacks (txn_callbacks_a) { diff --git a/nano/store/lmdb/transaction_impl.hpp b/nano/store/lmdb/transaction_impl.hpp index 06a4e1d414..81102515b0 100644 --- a/nano/store/lmdb/transaction_impl.hpp +++ b/nano/store/lmdb/transaction_impl.hpp @@ -29,7 +29,7 @@ class txn_callbacks class read_transaction_impl final : public store::read_transaction_impl { public: - read_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks); + read_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks = {}); ~read_transaction_impl (); void reset () override; void renew () override; @@ -41,7 +41,7 @@ class read_transaction_impl final : public store::read_transaction_impl class write_transaction_impl final : public store::write_transaction_impl { public: - write_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks); + write_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks = {}); ~write_transaction_impl (); void commit () override; void renew () override; diff --git a/nano/store/rocksdb/rocksdb.cpp b/nano/store/rocksdb/rocksdb.cpp index 60f7e95cee..2a2d716c05 100644 --- a/nano/store/rocksdb/rocksdb.cpp +++ b/nano/store/rocksdb/rocksdb.cpp @@ -545,12 +545,12 @@ nano::store::write_transaction nano::store::rocksdb::component::tx_begin_write ( // Tables must be kept in alphabetical order. These can be used for mutex locking, so order is important to prevent deadlocking debug_assert (std::is_sorted (tables_requiring_locks_a.begin (), tables_requiring_locks_a.end ())); - return store::write_transaction{ std::move (txn) }; + return store::write_transaction{ std::move (txn), store_id }; } nano::store::read_transaction nano::store::rocksdb::component::tx_begin_read () const { - return store::read_transaction{ std::make_unique (db.get ()) }; + return store::read_transaction{ std::make_unique (db.get ()), store_id }; } std::string nano::store::rocksdb::component::vendor_get () const diff --git a/nano/store/rocksdb/rocksdb.hpp b/nano/store/rocksdb/rocksdb.hpp index 5d8b22bba7..1e89b00a0e 100644 --- a/nano/store/rocksdb/rocksdb.hpp +++ b/nano/store/rocksdb/rocksdb.hpp @@ -177,5 +177,7 @@ class component : public nano::store::component friend class nano::rocksdb_block_store_tombstone_count_Test; friend class rocksdb_block_store_upgrade_v21_v22_Test; + + nano::id_t const store_id{ nano::next_id () }; }; } // namespace nano::store::rocksdb diff --git a/nano/store/transaction.cpp b/nano/store/transaction.cpp index dcfc3d7e8d..23fd380e63 100644 --- a/nano/store/transaction.cpp +++ b/nano/store/transaction.cpp @@ -6,8 +6,7 @@ * transaction_impl */ -nano::store::transaction_impl::transaction_impl (nano::id_dispenser::id_t const store_id_a) : - store_id{ store_id_a } +nano::store::transaction_impl::transaction_impl () { } @@ -15,8 +14,7 @@ nano::store::transaction_impl::transaction_impl (nano::id_dispenser::id_t const * read_transaction_impl */ -nano::store::read_transaction_impl::read_transaction_impl (nano::id_dispenser::id_t const store_id_a) : - transaction_impl (store_id_a) +nano::store::read_transaction_impl::read_transaction_impl () { } @@ -24,8 +22,7 @@ nano::store::read_transaction_impl::read_transaction_impl (nano::id_dispenser::i * write_transaction_impl */ -nano::store::write_transaction_impl::write_transaction_impl (nano::id_dispenser::id_t const store_id_a) : - transaction_impl (store_id_a) +nano::store::write_transaction_impl::write_transaction_impl () { } @@ -33,6 +30,16 @@ nano::store::write_transaction_impl::write_transaction_impl (nano::id_dispenser: * transaction */ +nano::store::transaction::transaction (nano::id_dispenser::id_t store_id) : + store_id_m{ store_id } +{ +} + +nano::id_dispenser::id_t nano::store::transaction::store_id () const +{ + return store_id_m; +} + auto nano::store::transaction::epoch () const -> epoch_t { return current_epoch; @@ -47,7 +54,8 @@ std::chrono::steady_clock::time_point nano::store::transaction::timestamp () con * read_transaction */ -nano::store::read_transaction::read_transaction (std::unique_ptr read_transaction_impl) : +nano::store::read_transaction::read_transaction (std::unique_ptr read_transaction_impl, nano::id_dispenser::id_t store_id) : + transaction{ store_id }, impl (std::move (read_transaction_impl)) { start = std::chrono::steady_clock::now (); @@ -58,11 +66,6 @@ void * nano::store::read_transaction::get_handle () const return impl->get_handle (); } -nano::id_dispenser::id_t nano::store::read_transaction::store_id () const -{ - return impl->store_id; -} - void nano::store::read_transaction::reset () { ++current_epoch; @@ -95,7 +98,8 @@ void nano::store::read_transaction::refresh_if_needed (std::chrono::milliseconds * write_transaction */ -nano::store::write_transaction::write_transaction (std::unique_ptr write_transaction_impl) : +nano::store::write_transaction::write_transaction (std::unique_ptr write_transaction_impl, nano::id_dispenser::id_t store_id) : + transaction{ store_id }, impl (std::move (write_transaction_impl)) { /* @@ -111,11 +115,6 @@ void * nano::store::write_transaction::get_handle () const return impl->get_handle (); } -nano::id_dispenser::id_t nano::store::write_transaction::store_id () const -{ - return impl->store_id; -} - void nano::store::write_transaction::commit () { ++current_epoch; diff --git a/nano/store/transaction.hpp b/nano/store/transaction.hpp index c875babafe..a166aad1b7 100644 --- a/nano/store/transaction.hpp +++ b/nano/store/transaction.hpp @@ -11,17 +11,15 @@ namespace nano::store class transaction_impl { public: - transaction_impl (nano::id_dispenser::id_t const store_id); + transaction_impl (); virtual ~transaction_impl () = default; virtual void * get_handle () const = 0; - - nano::id_dispenser::id_t const store_id; }; class read_transaction_impl : public transaction_impl { public: - explicit read_transaction_impl (nano::id_dispenser::id_t const store_id = 0); + explicit read_transaction_impl (); virtual void reset () = 0; virtual void renew () = 0; }; @@ -29,7 +27,7 @@ class read_transaction_impl : public transaction_impl class write_transaction_impl : public transaction_impl { public: - explicit write_transaction_impl (nano::id_dispenser::id_t const store_id = 0); + explicit write_transaction_impl (); virtual void commit () = 0; virtual void renew () = 0; virtual bool contains (nano::tables table_a) const = 0; @@ -41,14 +39,16 @@ class transaction using epoch_t = size_t; public: + transaction (nano::id_dispenser::id_t store_id); virtual ~transaction () = default; virtual void * get_handle () const = 0; - virtual nano::id_dispenser::id_t store_id () const = 0; + virtual nano::id_dispenser::id_t store_id () const; epoch_t epoch () const; std::chrono::steady_clock::time_point timestamp () const; protected: + nano::id_dispenser::id_t store_id_m; epoch_t current_epoch{ 0 }; std::chrono::steady_clock::time_point start{}; }; @@ -60,9 +60,8 @@ class transaction class read_transaction final : public transaction { public: - explicit read_transaction (std::unique_ptr read_transaction_impl); + explicit read_transaction (std::unique_ptr read_transaction_impl, nano::id_dispenser::id_t store_id); void * get_handle () const override; - nano::id_dispenser::id_t store_id () const override; void reset (); void renew (); @@ -80,9 +79,8 @@ class read_transaction final : public transaction class write_transaction final : public transaction { public: - explicit write_transaction (std::unique_ptr write_transaction_impl); + explicit write_transaction (std::unique_ptr write_transaction_impl, nano::id_dispenser::id_t store_id); void * get_handle () const override; - nano::id_dispenser::id_t store_id () const override; void commit (); void renew (); From e06bc1bb97ec90fd97d85d8992fd6ca61576be1b Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 8 Sep 2024 21:22:51 +0100 Subject: [PATCH 09/18] Extracting options from lmdb::env --- nano/node/wallet.cpp | 2 +- nano/store/CMakeLists.txt | 2 ++ nano/store/lmdb/lmdb.cpp | 4 +-- nano/store/lmdb/lmdb_env.cpp | 4 +-- nano/store/lmdb/lmdb_env.hpp | 47 +++--------------------------------- nano/store/lmdb/options.cpp | 25 +++++++++++++++++++ nano/store/lmdb/options.hpp | 30 +++++++++++++++++++++++ 7 files changed, 65 insertions(+), 49 deletions(-) create mode 100644 nano/store/lmdb/options.cpp create mode 100644 nano/store/lmdb/options.hpp diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index d198f9cce5..1fcec3e9c3 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1792,7 +1792,7 @@ nano::store::iterator nano::wallet_store::end return store::iterator (nullptr); } nano::mdb_wallets_store::mdb_wallets_store (std::filesystem::path const & path_a, nano::lmdb_config const & lmdb_config_a) : - environment (error, path_a, nano::store::lmdb::env::options::make ().set_config (lmdb_config_a).override_config_sync (nano::lmdb_config::sync_strategy::always).override_config_map_size (1ULL * 1024 * 1024 * 1024)) + environment (error, path_a, nano::store::lmdb::options::make ().set_config (lmdb_config_a).override_config_sync (nano::lmdb_config::sync_strategy::always).override_config_map_size (1ULL * 1024 * 1024 * 1024)) { } diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index 3bab8b87c3..d1bb470db5 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -20,6 +20,7 @@ add_library( lmdb/iterator.hpp lmdb/transaction_impl.hpp lmdb/online_weight.hpp + lmdb/options.hpp lmdb/peer.hpp lmdb/pending.hpp lmdb/pruned.hpp @@ -68,6 +69,7 @@ add_library( lmdb/lmdb_env.cpp lmdb/transaction.cpp lmdb/online_weight.cpp + lmdb/options.cpp lmdb/peer.cpp lmdb/pending.cpp lmdb/pruned.cpp diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 884700b955..0e378e4eac 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -41,7 +41,7 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste version_store{ *this }, rep_weight_store{ *this }, logger{ logger_a }, - env (error, path_a, nano::store::lmdb::env::options::make ().set_config (lmdb_config_a).set_use_no_mem_init (true)), + env (error, path_a, nano::store::lmdb::options::make ().set_config (lmdb_config_a).set_use_no_mem_init (true)), mdb_txn_tracker (logger_a, txn_tracking_config_a, block_processor_batch_max_time_a), txn_tracking_enabled (txn_tracking_config_a.enable) { @@ -127,7 +127,7 @@ bool nano::store::lmdb::component::vacuum_after_upgrade (std::filesystem::path c std::filesystem::rename (vacuum_path, path_a); // Set up the environment again - auto options = nano::store::lmdb::env::options::make () + auto options = nano::store::lmdb::options::make () .set_config (lmdb_config_a) .set_use_no_mem_init (true); env.init (error, path_a, options); diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index 106f9dea72..80004096fa 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -3,12 +3,12 @@ #include -nano::store::lmdb::env::env (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::env::options options_a) +nano::store::lmdb::env::env (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::options options_a) { init (error_a, path_a, options_a); } -void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::env::options options_a) +void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::options options_a) { debug_assert (path_a.extension () == ".ldb", "invalid filename extension for lmdb database file"); diff --git a/nano/store/lmdb/lmdb_env.hpp b/nano/store/lmdb/lmdb_env.hpp index ca470e8ec2..b536b0302e 100644 --- a/nano/store/lmdb/lmdb_env.hpp +++ b/nano/store/lmdb/lmdb_env.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace nano::store::lmdb @@ -13,50 +14,8 @@ namespace nano::store::lmdb class env final { public: - /** Environment options, most of which originates from the config file. */ - class options final - { - friend class env; - - public: - static options make () - { - return options (); - } - - options & set_config (nano::lmdb_config config_a) - { - config = config_a; - return *this; - } - - options & set_use_no_mem_init (int use_no_mem_init_a) - { - use_no_mem_init = use_no_mem_init_a; - return *this; - } - - /** Used by the wallet to override the config map size */ - options & override_config_map_size (std::size_t map_size_a) - { - config.map_size = map_size_a; - return *this; - } - - /** Used by the wallet to override the sync strategy */ - options & override_config_sync (nano::lmdb_config::sync_strategy sync_a) - { - config.sync = sync_a; - return *this; - } - - private: - bool use_no_mem_init{ false }; - nano::lmdb_config config; - }; - - env (bool &, std::filesystem::path const &, env::options options_a = env::options::make ()); - void init (bool &, std::filesystem::path const &, env::options options_a = env::options::make ()); + env (bool &, std::filesystem::path const &, options options_a = options::make ()); + void init (bool &, std::filesystem::path const &, options options_a = options::make ()); ~env (); operator MDB_env * () const; MDB_env * environment; diff --git a/nano/store/lmdb/options.cpp b/nano/store/lmdb/options.cpp new file mode 100644 index 0000000000..79bce75b52 --- /dev/null +++ b/nano/store/lmdb/options.cpp @@ -0,0 +1,25 @@ +#include + +auto nano::store::lmdb::options::set_config (nano::lmdb_config config_a) -> options & +{ + config = config_a; + return *this; +} + +auto nano::store::lmdb::options::set_use_no_mem_init (int use_no_mem_init_a) -> options & +{ + use_no_mem_init = use_no_mem_init_a; + return *this; +} + +auto nano::store::lmdb::options::override_config_map_size (std::size_t map_size_a) -> options & +{ + config.map_size = map_size_a; + return *this; +} + +auto nano::store::lmdb::options::override_config_sync (nano::lmdb_config::sync_strategy sync_a) -> options & +{ + config.sync = sync_a; + return *this; +} diff --git a/nano/store/lmdb/options.hpp b/nano/store/lmdb/options.hpp new file mode 100644 index 0000000000..e6bcaf3ab8 --- /dev/null +++ b/nano/store/lmdb/options.hpp @@ -0,0 +1,30 @@ +#include + +namespace nano::store::lmdb +{ +/** Environment options, most of which originates from the config file. */ +class options final +{ + friend class env; + +public: + static options make () + { + return options (); + } + + options & set_config (nano::lmdb_config config_a); + + options & set_use_no_mem_init (int use_no_mem_init_a); + + /** Used by the wallet to override the config map size */ + options & override_config_map_size (std::size_t map_size_a); + + /** Used by the wallet to override the sync strategy */ + options & override_config_sync (nano::lmdb_config::sync_strategy sync_a); + +private: + bool use_no_mem_init{ false }; + nano::lmdb_config config; +}; +} From 53301a9e744a58e20acfc363003cd6e5f108912a Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 11:40:57 +0100 Subject: [PATCH 10/18] Moving flag and option applying outside of lmdb_env. --- nano/store/lmdb/lmdb_env.cpp | 36 ++--------------------------- nano/store/lmdb/options.cpp | 45 ++++++++++++++++++++++++++++++++++++ nano/store/lmdb/options.hpp | 15 +++++++++--- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index 80004096fa..c014c3ba59 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -21,40 +21,8 @@ void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const & { auto status1 (mdb_env_create (&environment)); release_assert (status1 == 0); - auto status2 (mdb_env_set_maxdbs (environment, options_a.config.max_databases)); - release_assert (status2 == 0); - auto map_size = options_a.config.map_size; - auto max_instrumented_map_size = 16 * 1024 * 1024; - if (memory_intensive_instrumentation () && map_size > max_instrumented_map_size) - { - // In order to run LMDB with some types of memory instrumentation, the maximum map size must be smaller than what is normally used when non-instrumented - map_size = max_instrumented_map_size; - } - auto status3 (mdb_env_set_mapsize (environment, map_size)); - release_assert (status3 == 0); - // It seems if there's ever more threads than mdb_env_set_maxreaders has read slots available, we get failures on transaction creation unless MDB_NOTLS is specified - // This can happen if something like 256 io_threads are specified in the node config - // MDB_NORDAHEAD will allow platforms that support it to load the DB in memory as needed. - // MDB_NOMEMINIT prevents zeroing malloc'ed pages. Can provide improvement for non-sensitive data but may make memory checkers noisy (e.g valgrind). - auto environment_flags = MDB_NOSUBDIR | MDB_NOTLS | MDB_NORDAHEAD; - if (options_a.config.sync == nano::lmdb_config::sync_strategy::nosync_safe) - { - environment_flags |= MDB_NOMETASYNC; - } - else if (options_a.config.sync == nano::lmdb_config::sync_strategy::nosync_unsafe) - { - environment_flags |= MDB_NOSYNC; - } - else if (options_a.config.sync == nano::lmdb_config::sync_strategy::nosync_unsafe_large_memory) - { - environment_flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC; - } - - if (!memory_intensive_instrumentation () && options_a.use_no_mem_init) - { - environment_flags |= MDB_NOMEMINIT; - } - auto status4 (mdb_env_open (environment, path_a.string ().c_str (), environment_flags, 00600)); + options_a.apply (*this); + auto status4 (mdb_env_open (environment, path_a.string ().c_str (), options_a.flags (), 00600)); if (status4 != 0) { std::string message = "Could not open lmdb environment(" + std::to_string (status4) + "): " + mdb_strerror (status4); diff --git a/nano/store/lmdb/options.cpp b/nano/store/lmdb/options.cpp index 79bce75b52..f70f1aa567 100644 --- a/nano/store/lmdb/options.cpp +++ b/nano/store/lmdb/options.cpp @@ -1,5 +1,8 @@ +#include #include +#include + auto nano::store::lmdb::options::set_config (nano::lmdb_config config_a) -> options & { config = config_a; @@ -23,3 +26,45 @@ auto nano::store::lmdb::options::override_config_sync (nano::lmdb_config::sync_s config.sync = sync_a; return *this; } + +auto nano::store::lmdb::options::apply (nano::store::lmdb::env & env) -> options & +{ + auto status = mdb_env_set_maxdbs (env.environment, config.max_databases); + release_assert (status == MDB_SUCCESS); + auto map_size = config.map_size; + auto max_instrumented_map_size = 16 * 1024 * 1024; + if (memory_intensive_instrumentation () && map_size > max_instrumented_map_size) + { + // In order to run LMDB with some types of memory instrumentation, the maximum map size must be smaller than what is normally used when non-instrumented + map_size = max_instrumented_map_size; + } + status = mdb_env_set_mapsize (env.environment, map_size); + release_assert (status == MDB_SUCCESS); + return *this; +} + +auto nano::store::lmdb::options::flags () const -> unsigned int +{ + // It seems if there's ever more threads than mdb_env_set_maxreaders has read slots available, we get failures on transaction creation unless MDB_NOTLS is specified + // This can happen if something like 256 io_threads are specified in the node config + // MDB_NORDAHEAD will allow platforms that support it to load the DB in memory as needed. + // MDB_NOMEMINIT prevents zeroing malloc'ed pages. Can provide improvement for non-sensitive data but may make memory checkers noisy (e.g valgrind). + unsigned int environment_flags = MDB_NOSUBDIR | MDB_NOTLS | MDB_NORDAHEAD; + if (config.sync == nano::lmdb_config::sync_strategy::nosync_safe) + { + environment_flags |= MDB_NOMETASYNC; + } + else if (config.sync == nano::lmdb_config::sync_strategy::nosync_unsafe) + { + environment_flags |= MDB_NOSYNC; + } + else if (config.sync == nano::lmdb_config::sync_strategy::nosync_unsafe_large_memory) + { + environment_flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC; + } + if (!memory_intensive_instrumentation () && use_no_mem_init) + { + environment_flags |= MDB_NOMEMINIT; + } + return environment_flags; +} diff --git a/nano/store/lmdb/options.hpp b/nano/store/lmdb/options.hpp index e6bcaf3ab8..9b463ba59a 100644 --- a/nano/store/lmdb/options.hpp +++ b/nano/store/lmdb/options.hpp @@ -1,19 +1,25 @@ +#pragma once + #include +namespace nano::store::lmdb +{ +class env; +} + namespace nano::store::lmdb { /** Environment options, most of which originates from the config file. */ class options final { - friend class env; - public: static options make () { return options (); } - options & set_config (nano::lmdb_config config_a); + options & + set_config (nano::lmdb_config config_a); options & set_use_no_mem_init (int use_no_mem_init_a); @@ -23,6 +29,9 @@ class options final /** Used by the wallet to override the sync strategy */ options & override_config_sync (nano::lmdb_config::sync_strategy sync_a); + options & apply (nano::store::lmdb::env & env); + unsigned int flags () const; + private: bool use_no_mem_init{ false }; nano::lmdb_config config; From 64bf712c464f9298f82e2e1104a0e5853365f5b7 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 15:08:36 +0100 Subject: [PATCH 11/18] Replace nano::store::lmdb::lmdb_env with lmdb++ env --- CMakeLists.txt | 3 ++ nano/core_test/wallet.cpp | 81 +++++++++++++--------------- nano/node/wallet.cpp | 11 ++-- nano/node/wallet.hpp | 10 ++-- nano/store/CMakeLists.txt | 2 - nano/store/lmdb/iterator.hpp | 5 +- nano/store/lmdb/lmdb.cpp | 31 +++++------ nano/store/lmdb/lmdb.hpp | 7 ++- nano/store/lmdb/lmdb_env.cpp | 60 --------------------- nano/store/lmdb/lmdb_env.hpp | 23 -------- nano/store/lmdb/options.cpp | 10 ++-- nano/store/lmdb/options.hpp | 7 +-- nano/store/lmdb/transaction.cpp | 5 +- nano/store/lmdb/transaction_impl.hpp | 13 ++--- 14 files changed, 85 insertions(+), 183 deletions(-) delete mode 100644 nano/store/lmdb/lmdb_env.cpp delete mode 100644 nano/store/lmdb/lmdb_env.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 01d1d1322f..8c72ba997d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -676,6 +676,9 @@ add_library( submodules/lmdb/libraries/liblmdb/mdb.c submodules/lmdb/libraries/liblmdb/midl.c) +include_directories(submodules/lmdb/libraries/liblmdb) +include_directories(submodules/lmdbxx/include) + if(WIN32) target_link_libraries(lmdb ntdll) endif() diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index b4096f58f5..834f9e9869 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -14,13 +15,22 @@ using namespace std::chrono_literals; unsigned constexpr nano::wallet_store::version_current; +auto create_wallet_env () -> ::lmdb::env +{ + auto env = ::lmdb::env::create (); + nano::store::lmdb::options::make ().apply (env); + auto path = nano::unique_path () / "wallet.ldb"; + std::filesystem::create_directories (path); + env.open (path.string ().c_str ()); + return env; +} + TEST (wallet, no_special_keys_accounts) { - bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; + bool error = false; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); ASSERT_FALSE (error); nano::keypair key1; @@ -37,9 +47,8 @@ TEST (wallet, no_special_keys_accounts) TEST (wallet, no_key) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -52,9 +61,8 @@ TEST (wallet, no_key) TEST (wallet, fetch_locked) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -74,9 +82,8 @@ TEST (wallet, fetch_locked) TEST (wallet, retrieval) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -96,9 +103,8 @@ TEST (wallet, retrieval) TEST (wallet, empty_iteration) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -110,9 +116,8 @@ TEST (wallet, empty_iteration) TEST (wallet, one_item_iteration) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -132,9 +137,8 @@ TEST (wallet, one_item_iteration) TEST (wallet, two_item_iteration) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::keypair key1; nano::keypair key2; ASSERT_NE (key1.pub, key2.pub); @@ -258,9 +262,8 @@ TEST (wallet, spend_no_previous) TEST (wallet, find_none) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -271,9 +274,8 @@ TEST (wallet, find_none) TEST (wallet, find_existing) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -290,9 +292,8 @@ TEST (wallet, find_existing) TEST (wallet, rekey) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -300,7 +301,6 @@ TEST (wallet, rekey) nano::raw_key password; wallet.password.value (password); ASSERT_TRUE (password.is_zero ()); - ASSERT_FALSE (error); nano::keypair key1; wallet.insert_adhoc (transaction, key1.prv); nano::raw_key prv1; @@ -362,9 +362,8 @@ TEST (account, encode_fail) TEST (wallet, hash_password) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -411,9 +410,8 @@ TEST (fan, change) TEST (wallet, reopen_default_password) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; { @@ -447,9 +445,8 @@ TEST (wallet, reopen_default_password) TEST (wallet, representative) { - auto error (false); - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); + bool error = false; nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -468,9 +465,8 @@ TEST (wallet, representative) TEST (wallet, serialize_json_empty) { - auto error (false); - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); + bool error = false; nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -493,9 +489,8 @@ TEST (wallet, serialize_json_empty) TEST (wallet, serialize_json_one) { - auto error (false); - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); + bool error = false; nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -522,9 +517,8 @@ TEST (wallet, serialize_json_one) TEST (wallet, serialize_json_password) { - auto error (false); - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); + bool error = false; nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -555,9 +549,8 @@ TEST (wallet, serialize_json_password) TEST (wallet_store, move) { - auto error (false); - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); + auto env = create_wallet_env (); + bool error = false; nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -712,9 +705,8 @@ TEST (wallet, insert_locked) TEST (wallet, deterministic_keys) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); @@ -755,9 +747,8 @@ TEST (wallet, deterministic_keys) TEST (wallet, reseed) { + auto env = create_wallet_env (); bool error = false; - nano::store::lmdb::env env (error, nano::unique_path () / "wallet.ldb"); - ASSERT_FALSE (error); nano::store::write_transaction transaction{ std::make_unique (env), 0 }; nano::kdf kdf{ nano::dev::network_params.kdf_work }; nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis_key.pub, 1, "0"); diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 1fcec3e9c3..638c9859dd 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -259,7 +260,7 @@ int const nano::wallet_store::special_count (7); std::size_t const nano::wallet_store::check_iv_index (0); std::size_t const nano::wallet_store::seed_iv_index (1); -nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, store::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a, std::string const & json_a) : +nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, ::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a, std::string const & json_a) : password (0, fanout_a), wallet_key_mem (0, fanout_a), kdf (kdf_a), @@ -316,7 +317,7 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans } } -nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, store::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a) : +nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, ::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a) : password (0, fanout_a), wallet_key_mem (0, fanout_a), kdf (kdf_a), @@ -1791,9 +1792,13 @@ nano::store::iterator nano::wallet_store::end { return store::iterator (nullptr); } + nano::mdb_wallets_store::mdb_wallets_store (std::filesystem::path const & path_a, nano::lmdb_config const & lmdb_config_a) : - environment (error, path_a, nano::store::lmdb::options::make ().set_config (lmdb_config_a).override_config_sync (nano::lmdb_config::sync_strategy::always).override_config_map_size (1ULL * 1024 * 1024 * 1024)) + environment (::lmdb::env::create ()) { + auto options = nano::store::lmdb::options::make ().set_config (lmdb_config_a).override_config_sync (nano::lmdb_config::sync_strategy::always).override_config_map_size (1ULL * 1024 * 1024 * 1024); + options.apply (environment); + environment.open (path_a.string ().c_str (), options.flags ()); } bool nano::mdb_wallets_store::init_error () const diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index 98e60be93e..f9c4d9e28c 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -58,8 +58,8 @@ enum class key_type class wallet_store final { public: - wallet_store (bool &, nano::kdf &, store::transaction &, store::lmdb::env &, nano::account, unsigned, std::string const &); - wallet_store (bool &, nano::kdf &, store::transaction &, store::lmdb::env &, nano::account, unsigned, std::string const &, std::string const &); + wallet_store (bool &, nano::kdf &, store::transaction &, ::lmdb::env &, nano::account, unsigned, std::string const &); + wallet_store (bool &, nano::kdf &, store::transaction &, ::lmdb::env &, nano::account, unsigned, std::string const &, std::string const &); std::vector accounts (store::transaction const &); void initialize (store::transaction const &, bool &, std::string const &); nano::uint256_union check (store::transaction const &); @@ -124,7 +124,7 @@ class wallet_store final std::recursive_mutex mutex; private: - nano::store::lmdb::env & env; + ::lmdb::env & env; }; // A wallet is a set of account keys encrypted by a common encryption key @@ -234,7 +234,7 @@ class wallets final MDB_dbi handle; MDB_dbi send_action_ids; nano::node & node; - nano::store::lmdb::env & env; + ::lmdb::env & env; std::atomic stopped; std::thread thread; static nano::uint128_t const generate_priority; @@ -265,7 +265,7 @@ class mdb_wallets_store final : public wallets_store { public: mdb_wallets_store (std::filesystem::path const &, nano::lmdb_config const & lmdb_config_a = nano::lmdb_config{}); - nano::store::lmdb::env environment; + ::lmdb::env environment; bool init_error () const override; bool error{ false }; }; diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index d1bb470db5..b55a7fe966 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -16,7 +16,6 @@ add_library( lmdb/final_vote.hpp lmdb/iterator.hpp lmdb/lmdb.hpp - lmdb/lmdb_env.hpp lmdb/iterator.hpp lmdb/transaction_impl.hpp lmdb/online_weight.hpp @@ -66,7 +65,6 @@ add_library( lmdb/db_val.cpp lmdb/final_vote.cpp lmdb/lmdb.cpp - lmdb/lmdb_env.cpp lmdb/transaction.cpp lmdb/online_weight.cpp lmdb/options.cpp diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index ebf6c19f41..ce6751ad9d 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -3,11 +3,10 @@ #include #include #include -#include #include #include -#include +#include namespace nano::store::lmdb { @@ -15,7 +14,7 @@ template class iterator : public iterator_impl { public: - iterator (store::transaction const & transaction_a, env const & env_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : + iterator (store::transaction const & transaction_a, ::lmdb::env const & env_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : nano::store::iterator_impl (transaction_a) { auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor)); diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 0e378e4eac..df9153a6ce 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -41,14 +42,16 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste version_store{ *this }, rep_weight_store{ *this }, logger{ logger_a }, - env (error, path_a, nano::store::lmdb::options::make ().set_config (lmdb_config_a).set_use_no_mem_init (true)), + env (::lmdb::env::create ()), mdb_txn_tracker (logger_a, txn_tracking_config_a, block_processor_batch_max_time_a), txn_tracking_enabled (txn_tracking_config_a.enable) { + auto options = nano::store::lmdb::options::make ().set_config (lmdb_config_a).set_use_no_mem_init (true); + options.apply (env); + debug_assert (path_a.filename () == "data.ldb"); + env.open (path_a.string ().c_str (), options.flags ()); if (!error) { - debug_assert (path_a.filename () == "data.ldb"); - auto is_fully_upgraded (false); auto is_fresh_db (false); { @@ -119,9 +122,8 @@ bool nano::store::lmdb::component::vacuum_after_upgrade (std::filesystem::path c if (vacuum_success) { // Need to close the database to release the file handle - mdb_env_sync (env.environment, true); - mdb_env_close (env.environment); - env.environment = nullptr; + env.sync (true); + env.close (); // Replace the ledger file with the vacuumed one std::filesystem::rename (vacuum_path, path_a); @@ -130,12 +132,11 @@ bool nano::store::lmdb::component::vacuum_after_upgrade (std::filesystem::path c auto options = nano::store::lmdb::options::make () .set_config (lmdb_config_a) .set_use_no_mem_init (true); - env.init (error, path_a, options); - if (!error) - { - auto transaction (tx_begin_read ()); - open_databases (error, transaction, 0); - } + env = ::lmdb::env::create (); + options.apply (env); + env.open (path_a.string ().c_str (), options.flags ()); + auto transaction = tx_begin_read (); + open_databases (error, transaction, 0); } else { @@ -153,7 +154,7 @@ void nano::store::lmdb::component::serialize_mdb_tracker (boost::property_tree:: void nano::store::lmdb::component::serialize_memory_stats (boost::property_tree::ptree & json) { MDB_stat stats; - auto status (mdb_env_stat (env.environment, &stats)); + auto status = mdb_env_stat (env.handle (), &stats); release_assert (status == 0); json.put ("branch_pages", stats.ms_branch_pages); json.put ("depth", stats.ms_depth); @@ -320,7 +321,7 @@ void nano::store::lmdb::component::upgrade_v23_to_v24 (store::write_transaction } /** Takes a filepath, appends '_backup_' to the end (but before any extension) and saves that file in the same directory */ -void nano::store::lmdb::component::create_backup_file (nano::store::lmdb::env & env_a, std::filesystem::path const & filepath_a, nano::logger & logger) +void nano::store::lmdb::component::create_backup_file (::lmdb::env & env_a, std::filesystem::path const & filepath_a, nano::logger & logger) { auto extension = filepath_a.extension (); auto filename_without_extension = filepath_a.filename ().replace_extension (""); @@ -444,7 +445,7 @@ std::string nano::store::lmdb::component::error_string (int status) const bool nano::store::lmdb::component::copy_db (std::filesystem::path const & destination_file) { - return !mdb_env_copy2 (env.environment, destination_file.string ().c_str (), MDB_CP_COMPACT); + return !mdb_env_copy2 (env.handle (), destination_file.string ().c_str (), MDB_CP_COMPACT); } void nano::store::lmdb::component::rebuild_db (store::write_transaction const & transaction_a) diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index 5e7700d6dd..b19664f77f 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -24,7 +23,7 @@ #include -#include +#include namespace nano { @@ -71,7 +70,7 @@ class component : public nano::store::component void serialize_mdb_tracker (boost::property_tree::ptree &, std::chrono::milliseconds, std::chrono::milliseconds) override; - static void create_backup_file (nano::store::lmdb::env &, std::filesystem::path const &, nano::logger &); + static void create_backup_file (::lmdb::env &, std::filesystem::path const &, nano::logger &); void serialize_memory_stats (boost::property_tree::ptree &) override; @@ -82,7 +81,7 @@ class component : public nano::store::component bool error{ false }; public: - nano::store::lmdb::env env; + ::lmdb::env env; bool exists (store::transaction const & transaction_a, tables table_a, nano::store::lmdb::db_val const & key_a) const; diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp deleted file mode 100644 index c014c3ba59..0000000000 --- a/nano/store/lmdb/lmdb_env.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -#include - -nano::store::lmdb::env::env (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::options options_a) -{ - init (error_a, path_a, options_a); -} - -void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const & path_a, nano::store::lmdb::options options_a) -{ - debug_assert (path_a.extension () == ".ldb", "invalid filename extension for lmdb database file"); - - boost::system::error_code error_mkdir, error_chmod; - if (path_a.has_parent_path ()) - { - std::filesystem::create_directories (path_a.parent_path (), error_mkdir); - nano::set_secure_perm_directory (path_a.parent_path (), error_chmod); - if (!error_mkdir) - { - auto status1 (mdb_env_create (&environment)); - release_assert (status1 == 0); - options_a.apply (*this); - auto status4 (mdb_env_open (environment, path_a.string ().c_str (), options_a.flags (), 00600)); - if (status4 != 0) - { - std::string message = "Could not open lmdb environment(" + std::to_string (status4) + "): " + mdb_strerror (status4); - throw std::runtime_error (message); - } - release_assert (status4 == 0); - error_a = status4 != 0; - } - else - { - error_a = true; - environment = nullptr; - } - } - else - { - error_a = true; - environment = nullptr; - } -} - -nano::store::lmdb::env::~env () -{ - if (environment != nullptr) - { - // Make sure the commits are flushed. This is a no-op unless MDB_NOSYNC is used. - mdb_env_sync (environment, true); - mdb_env_close (environment); - } -} - -nano::store::lmdb::env::operator MDB_env * () const -{ - return environment; -} diff --git a/nano/store/lmdb/lmdb_env.hpp b/nano/store/lmdb/lmdb_env.hpp deleted file mode 100644 index b536b0302e..0000000000 --- a/nano/store/lmdb/lmdb_env.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace nano::store::lmdb -{ -/** - * RAII wrapper for MDB_env - */ -class env final -{ -public: - env (bool &, std::filesystem::path const &, options options_a = options::make ()); - void init (bool &, std::filesystem::path const &, options options_a = options::make ()); - ~env (); - operator MDB_env * () const; - MDB_env * environment; -}; -} // namespace nano::store::lmdb diff --git a/nano/store/lmdb/options.cpp b/nano/store/lmdb/options.cpp index f70f1aa567..335bce6ef0 100644 --- a/nano/store/lmdb/options.cpp +++ b/nano/store/lmdb/options.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -27,10 +27,9 @@ auto nano::store::lmdb::options::override_config_sync (nano::lmdb_config::sync_s return *this; } -auto nano::store::lmdb::options::apply (nano::store::lmdb::env & env) -> options & +auto nano::store::lmdb::options::apply (::lmdb::env & env) -> options & { - auto status = mdb_env_set_maxdbs (env.environment, config.max_databases); - release_assert (status == MDB_SUCCESS); + env.set_max_dbs (config.max_databases); auto map_size = config.map_size; auto max_instrumented_map_size = 16 * 1024 * 1024; if (memory_intensive_instrumentation () && map_size > max_instrumented_map_size) @@ -38,8 +37,7 @@ auto nano::store::lmdb::options::apply (nano::store::lmdb::env & env) -> options // In order to run LMDB with some types of memory instrumentation, the maximum map size must be smaller than what is normally used when non-instrumented map_size = max_instrumented_map_size; } - status = mdb_env_set_mapsize (env.environment, map_size); - release_assert (status == MDB_SUCCESS); + env.set_mapsize (map_size); return *this; } diff --git a/nano/store/lmdb/options.hpp b/nano/store/lmdb/options.hpp index 9b463ba59a..fe8ae28d42 100644 --- a/nano/store/lmdb/options.hpp +++ b/nano/store/lmdb/options.hpp @@ -2,10 +2,7 @@ #include -namespace nano::store::lmdb -{ -class env; -} +#include namespace nano::store::lmdb { @@ -29,7 +26,7 @@ class options final /** Used by the wallet to override the sync strategy */ options & override_config_sync (nano::lmdb_config::sync_strategy sync_a); - options & apply (nano::store::lmdb::env & env); + options & apply (::lmdb::env & env); unsigned int flags () const; private: diff --git a/nano/store/lmdb/transaction.cpp b/nano/store/lmdb/transaction.cpp index b1bd92c097..874f460aa5 100644 --- a/nano/store/lmdb/transaction.cpp +++ b/nano/store/lmdb/transaction.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -35,7 +34,7 @@ class matches_txn final }; } -nano::store::lmdb::read_transaction_impl::read_transaction_impl (nano::store::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : +nano::store::lmdb::read_transaction_impl::read_transaction_impl (::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : txn_callbacks (txn_callbacks_a) { auto status (mdb_txn_begin (environment_a, nullptr, MDB_RDONLY, &handle)); @@ -69,7 +68,7 @@ void * nano::store::lmdb::read_transaction_impl::get_handle () const return handle; } -nano::store::lmdb::write_transaction_impl::write_transaction_impl (nano::store::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : +nano::store::lmdb::write_transaction_impl::write_transaction_impl (::lmdb::env const & environment_a, nano::store::lmdb::txn_callbacks txn_callbacks_a) : env (environment_a), txn_callbacks (txn_callbacks_a) { diff --git a/nano/store/lmdb/transaction_impl.hpp b/nano/store/lmdb/transaction_impl.hpp index 81102515b0..1e8b35044c 100644 --- a/nano/store/lmdb/transaction_impl.hpp +++ b/nano/store/lmdb/transaction_impl.hpp @@ -10,12 +10,7 @@ #include #include -#include - -namespace nano::store::lmdb -{ -class env; -} +#include namespace nano::store::lmdb { @@ -29,7 +24,7 @@ class txn_callbacks class read_transaction_impl final : public store::read_transaction_impl { public: - read_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks = {}); + read_transaction_impl (::lmdb::env const & env, txn_callbacks mdb_txn_callbacks = {}); ~read_transaction_impl (); void reset () override; void renew () override; @@ -41,14 +36,14 @@ class read_transaction_impl final : public store::read_transaction_impl class write_transaction_impl final : public store::write_transaction_impl { public: - write_transaction_impl (nano::store::lmdb::env const &, txn_callbacks mdb_txn_callbacks = {}); + write_transaction_impl (::lmdb::env const & env, txn_callbacks mdb_txn_callbacks = {}); ~write_transaction_impl (); void commit () override; void renew () override; void * get_handle () const override; bool contains (nano::tables table_a) const override; MDB_txn * handle; - nano::store::lmdb::env const & env; + ::lmdb::env const & env; lmdb::txn_callbacks txn_callbacks; bool active{ true }; }; From 9b4c0ebf0223822dae5fd66e7ab977b83e77a84c Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 15:43:58 +0100 Subject: [PATCH 12/18] Replace direct usage of mdb_dbi_open with ::lmdb::dbi_open --- nano/core_test/block_store.cpp | 8 ++--- nano/node/wallet.cpp | 22 ++++++++----- nano/node/wallet.hpp | 2 +- nano/store/lmdb/lmdb.cpp | 60 +++++++++++++++++----------------- nano/store/lmdb/lmdb.hpp | 2 +- 5 files changed, 49 insertions(+), 45 deletions(-) diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index a6a7e50b08..aa05e722b9 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1376,7 +1376,7 @@ TEST (mdb_block_store, upgrade_v21_v22) auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); MDB_dbi unchecked_handle{ 0 }; - ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store::lmdb::tx (transaction), "unchecked", 0, &unchecked_handle)); + ASSERT_THROW (::lmdb::dbi_open (store::lmdb::tx (transaction), "unchecked", 0, &unchecked_handle), ::lmdb::not_found_error); }; // Testing current version doesn't contain the unchecked table @@ -1388,7 +1388,7 @@ TEST (mdb_block_store, upgrade_v21_v22) auto transaction (store.tx_begin_write ()); store.version.put (transaction, 21); MDB_dbi unchecked_handle{ 0 }; - ASSERT_FALSE (mdb_dbi_open (store::lmdb::tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); + ::lmdb::dbi_open (store::lmdb::tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle); ASSERT_EQ (store.version.get (transaction), 21); } @@ -1412,7 +1412,7 @@ TEST (mdb_block_store, upgrade_v23_v24) auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); MDB_dbi frontiers_handle{ 0 }; - ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store::lmdb::tx (transaction), "frontiers", 0, &frontiers_handle)); + ASSERT_THROW (::lmdb::dbi_open (store::lmdb::tx (transaction), "frontiers", 0, &frontiers_handle), ::lmdb::not_found_error); }; // Testing current version doesn't contain the frontiers table @@ -1424,7 +1424,7 @@ TEST (mdb_block_store, upgrade_v23_v24) auto transaction (store.tx_begin_write ()); store.version.put (transaction, 23); MDB_dbi frontiers_handle{ 0 }; - ASSERT_FALSE (mdb_dbi_open (store::lmdb::tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); + ::lmdb::dbi_open (store::lmdb::tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle); ASSERT_EQ (store.version.get (transaction), 23); } diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 638c9859dd..2ae66271de 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -267,7 +267,7 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans env{ env_a } { init_a = false; - initialize (transaction_a, init_a, wallet_a); + initialize (transaction_a, wallet_a); if (!init_a) { MDB_val junk; @@ -324,7 +324,14 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans env{ env_a } { init_a = false; - initialize (transaction_a, init_a, wallet_a); + try + { + initialize (transaction_a, wallet_a); + } + catch (::lmdb::runtime_error const & e) + { + init_a = true; + } if (!init_a) { int version_status; @@ -379,14 +386,12 @@ std::vector nano::wallet_store::accounts (store::transaction cons return result; } -void nano::wallet_store::initialize (store::transaction const & transaction_a, bool & init_a, std::string const & path_a) +void nano::wallet_store::initialize (store::transaction const & transaction_a, std::string const & path_a) { debug_assert (strlen (path_a.c_str ()) == path_a.size ()); - auto error (0); MDB_dbi handle_l; - error |= mdb_dbi_open (store::lmdb::tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); + ::lmdb::dbi_open (store::lmdb::tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); handle = handle_l; - init_a = error != 0; } bool nano::wallet_store::is_representative (store::transaction const & transaction_a) @@ -1356,9 +1361,8 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) : if (!error_a) { auto transaction (tx_begin_write ()); - auto status (mdb_dbi_open (store::lmdb::tx (transaction), nullptr, MDB_CREATE, &handle)); - status |= mdb_dbi_open (store::lmdb::tx (transaction), "send_action_ids", MDB_CREATE, &send_action_ids); - release_assert (status == 0); + ::lmdb::dbi_open (store::lmdb::tx (transaction), nullptr, MDB_CREATE, &handle); + ::lmdb::dbi_open (store::lmdb::tx (transaction), "send_action_ids", MDB_CREATE, &send_action_ids); std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index f9c4d9e28c..52ad26b391 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -61,7 +61,7 @@ class wallet_store final wallet_store (bool &, nano::kdf &, store::transaction &, ::lmdb::env &, nano::account, unsigned, std::string const &); wallet_store (bool &, nano::kdf &, store::transaction &, ::lmdb::env &, nano::account, unsigned, std::string const &, std::string const &); std::vector accounts (store::transaction const &); - void initialize (store::transaction const &, bool &, std::string const &); + void initialize (store::transaction const &, std::string const &); nano::uint256_union check (store::transaction const &); bool rekey (store::transaction const &, std::string const &); bool valid_password (store::transaction const &); diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index df9153a6ce..9febfe1973 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -52,17 +52,20 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste env.open (path_a.string ().c_str (), options.flags ()); if (!error) { - auto is_fully_upgraded (false); - auto is_fresh_db (false); + auto is_fully_upgraded = false; + auto is_fresh_db = false; { - auto transaction (tx_begin_read ()); - auto err = mdb_dbi_open (tx (transaction), "meta", 0, &version_store.meta_handle); - is_fresh_db = err != MDB_SUCCESS; - if (err == MDB_SUCCESS) + auto transaction = tx_begin_read (); + try { + ::lmdb::dbi_open (tx (transaction), "meta", 0, &version_store.meta_handle); is_fully_upgraded = (version.get (transaction) == version_current); mdb_dbi_close (env, version_store.meta_handle); } + catch (::lmdb::not_found_error const &) + { + is_fresh_db = true; + } } // Only open a write lock when upgrades are needed. This is because CLI commands @@ -81,12 +84,9 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste } auto needs_vacuuming = false; { - auto transaction (tx_begin_write ()); - open_databases (error, transaction, MDB_CREATE); - if (!error) - { - error |= do_upgrades (transaction, constants, needs_vacuuming); - } + auto transaction = tx_begin_write (); + open_databases (transaction, MDB_CREATE); + error |= do_upgrades (transaction, constants, needs_vacuuming); } if (needs_vacuuming) @@ -107,8 +107,8 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste } else { - auto transaction (tx_begin_read ()); - open_databases (error, transaction, 0); + auto transaction = tx_begin_read (); + open_databases (transaction, 0); } } } @@ -136,7 +136,7 @@ bool nano::store::lmdb::component::vacuum_after_upgrade (std::filesystem::path c options.apply (env); env.open (path_a.string ().c_str (), options.flags ()); auto transaction = tx_begin_read (); - open_databases (error, transaction, 0); + open_databases (transaction, 0); } else { @@ -194,18 +194,18 @@ nano::store::lmdb::txn_callbacks nano::store::lmdb::component::create_txn_callba return mdb_txn_callbacks; } -void nano::store::lmdb::component::open_databases (bool & error_a, store::transaction const & transaction_a, unsigned flags) +void nano::store::lmdb::component::open_databases (store::transaction const & transaction_a, unsigned flags) { - error_a |= mdb_dbi_open (tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "meta", flags, &version_store.meta_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "peers", flags, &peer_store.peers_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "accounts", flags, &account_store.accounts_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "pending", flags, &pending_store.pending_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle) != 0; - error_a |= mdb_dbi_open (tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle) != 0; + ::lmdb::dbi_open (tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle); + ::lmdb::dbi_open (tx (transaction_a), "meta", flags, &version_store.meta_handle); + ::lmdb::dbi_open (tx (transaction_a), "peers", flags, &peer_store.peers_handle); + ::lmdb::dbi_open (tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle); + ::lmdb::dbi_open (tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle); + ::lmdb::dbi_open (tx (transaction_a), "accounts", flags, &account_store.accounts_handle); + ::lmdb::dbi_open (tx (transaction_a), "pending", flags, &pending_store.pending_handle); + ::lmdb::dbi_open (tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle); + ::lmdb::dbi_open (tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle); + ::lmdb::dbi_open (tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle); } bool nano::store::lmdb::component::do_upgrades (store::write_transaction & transaction, nano::ledger_constants & constants, bool & needs_vacuuming) @@ -243,7 +243,7 @@ void nano::store::lmdb::component::upgrade_v21_to_v22 (store::write_transaction logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22..."); MDB_dbi unchecked_handle{ 0 }; - release_assert (!mdb_dbi_open (tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle)); + ::lmdb::dbi_open (tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle); release_assert (!mdb_drop (tx (transaction), unchecked_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. version.put (transaction, 22); @@ -314,7 +314,7 @@ void nano::store::lmdb::component::upgrade_v23_to_v24 (store::write_transaction logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24..."); MDB_dbi frontiers_handle{ 0 }; - release_assert (!mdb_dbi_open (tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle)); + ::lmdb::dbi_open (tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle); release_assert (!mdb_drop (tx (transaction), frontiers_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. version.put (transaction, 24); logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24 completed"); @@ -455,7 +455,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & for (auto const & table : tables) { MDB_dbi temp; - mdb_dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); + ::lmdb::dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, table))), n (store::iterator (nullptr)); i != n; ++i) { @@ -478,7 +478,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & // Pending table { MDB_dbi temp; - mdb_dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); + ::lmdb::dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, pending_store.pending_handle))), n (store::iterator (nullptr)); i != n; ++i) { diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index b19664f77f..97667a2461 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -115,7 +115,7 @@ class component : public nano::store::component void upgrade_v22_to_v23 (store::write_transaction &); void upgrade_v23_to_v24 (store::write_transaction &); - void open_databases (bool &, store::transaction const &, unsigned); + void open_databases (store::transaction const &, unsigned); int drop (store::write_transaction const & transaction_a, tables table_a) override; int clear (store::write_transaction const & transaction_a, MDB_dbi handle_a); From 2eab8efef25db26f2281f6735929c532982f57b2 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 16:35:04 +0100 Subject: [PATCH 13/18] Replace usages of mdb_drop with ::lmdb::dbi_drop --- nano/node/wallet.cpp | 8 ++------ nano/store/lmdb/lmdb.cpp | 26 +++++++++++++++++--------- nano/store/lmdb/lmdb.hpp | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 2ae66271de..445d1de54f 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -836,9 +836,7 @@ void nano::wallet::serialize (std::string & json_a) void nano::wallet_store::destroy (store::transaction const & transaction_a) { - auto status (mdb_drop (store::lmdb::tx (transaction_a), handle, 1)); - (void)status; - debug_assert (status == 0); + ::lmdb::dbi_drop (store::lmdb::tx (transaction_a), handle, true); handle = 0; } @@ -1639,9 +1637,7 @@ nano::store::read_transaction nano::wallets::tx_begin_read () void nano::wallets::clear_send_ids (store::transaction const & transaction_a) { - auto status (mdb_drop (store::lmdb::tx (transaction_a), send_action_ids, 0)); - (void)status; - debug_assert (status == 0); + ::lmdb::dbi_drop (store::lmdb::tx (transaction_a), send_action_ids, false); } nano::wallet_representatives nano::wallets::reps () const diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 9febfe1973..42e0744177 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -244,7 +244,7 @@ void nano::store::lmdb::component::upgrade_v21_to_v22 (store::write_transaction MDB_dbi unchecked_handle{ 0 }; ::lmdb::dbi_open (tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle); - release_assert (!mdb_drop (tx (transaction), unchecked_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. + ::lmdb::dbi_drop (tx (transaction), unchecked_handle, true); version.put (transaction, 22); logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22 completed"); @@ -315,7 +315,7 @@ void nano::store::lmdb::component::upgrade_v23_to_v24 (store::write_transaction MDB_dbi frontiers_handle{ 0 }; ::lmdb::dbi_open (tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle); - release_assert (!mdb_drop (tx (transaction), frontiers_handle, 1)); // del = 1, to delete it from the environment and close the DB handle. + ::lmdb::dbi_drop (tx (transaction), frontiers_handle, true); version.put (transaction, 24); logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24 completed"); } @@ -372,12 +372,20 @@ int nano::store::lmdb::component::del (store::write_transaction const & transact int nano::store::lmdb::component::drop (store::write_transaction const & transaction_a, tables table_a) { - return mdb_drop (tx (transaction_a), table_to_dbi (table_a), 1); + try + { + ::lmdb::dbi_drop (tx (transaction_a), table_to_dbi (table_a), true); + return 0; + } + catch (::lmdb::runtime_error const &) + { + return -1; + } } -int nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, MDB_dbi handle_a) +void nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, MDB_dbi handle_a) { - return mdb_drop (tx (transaction_a), handle_a, 0); + ::lmdb::dbi_drop (tx (transaction_a), handle_a, false); } uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, tables table_a) const @@ -464,7 +472,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Clear existing table - mdb_drop (tx (transaction_a), table, 0); + ::lmdb::dbi_drop (tx (transaction_a), table, false); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { @@ -473,7 +481,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Remove temporary table - mdb_drop (tx (transaction_a), temp, 1); + ::lmdb::dbi_drop (tx (transaction_a), temp, true); } // Pending table { @@ -486,7 +494,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - mdb_drop (tx (transaction_a), pending_store.pending_handle, 0); + ::lmdb::dbi_drop (tx (transaction_a), pending_store.pending_handle, false); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { @@ -494,7 +502,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - mdb_drop (tx (transaction_a), temp, 1); + ::lmdb::dbi_drop (tx (transaction_a), temp, true); } } diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index 97667a2461..d2ae1ab6ef 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -118,7 +118,7 @@ class component : public nano::store::component void open_databases (store::transaction const &, unsigned); int drop (store::write_transaction const & transaction_a, tables table_a) override; - int clear (store::write_transaction const & transaction_a, MDB_dbi handle_a); + void clear (store::write_transaction const & transaction_a, MDB_dbi handle_a); bool not_found (int status) const override; bool success (int status) const override; From 5aa80b7e8bd602700ca2900c7df18e6bb2e2ce3d Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 16:36:38 +0100 Subject: [PATCH 14/18] Replace mdb_close with ::lmdb::dbi_close --- nano/store/lmdb/lmdb.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 42e0744177..845e839a87 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -60,7 +60,7 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste { ::lmdb::dbi_open (tx (transaction), "meta", 0, &version_store.meta_handle); is_fully_upgraded = (version.get (transaction) == version_current); - mdb_dbi_close (env, version_store.meta_handle); + ::lmdb::dbi_close (env, version_store.meta_handle); } catch (::lmdb::not_found_error const &) { From ead93d6845c694ed8384dca6713fb767ee228b1f Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 16:38:36 +0100 Subject: [PATCH 15/18] Replacing env and dbi stats with ::lmdb equivalents. --- nano/store/lmdb/lmdb.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 845e839a87..5e3756b1c8 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -154,8 +154,7 @@ void nano::store::lmdb::component::serialize_mdb_tracker (boost::property_tree:: void nano::store::lmdb::component::serialize_memory_stats (boost::property_tree::ptree & json) { MDB_stat stats; - auto status = mdb_env_stat (env.handle (), &stats); - release_assert (status == 0); + ::lmdb::env_stat (env.handle (), &stats); json.put ("branch_pages", stats.ms_branch_pages); json.put ("depth", stats.ms_depth); json.put ("entries", stats.ms_entries); @@ -396,8 +395,7 @@ uint64_t nano::store::lmdb::component::count (store::transaction const & transac uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, MDB_dbi db_a) const { MDB_stat stats; - auto status (mdb_stat (tx (transaction_a), db_a, &stats)); - release_assert_success (status); + ::lmdb::dbi_stat (tx (transaction_a), db_a, &stats); return (stats.ms_entries); } From 6ce753ca69f192a0f96a5738dc82d23efa5b40b4 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 17:26:32 +0100 Subject: [PATCH 16/18] Convert MDB_dbi to ::lmdb::dbi --- nano/core_test/block_store.cpp | 12 +++---- nano/node/wallet.cpp | 8 ++--- nano/node/wallet.hpp | 6 ++-- nano/store/lmdb/account.hpp | 5 +-- nano/store/lmdb/block.hpp | 3 +- nano/store/lmdb/confirmation_height.hpp | 3 +- nano/store/lmdb/final_vote.hpp | 3 +- nano/store/lmdb/lmdb.cpp | 42 +++++++++++-------------- nano/store/lmdb/online_weight.hpp | 2 +- nano/store/lmdb/options.cpp | 2 +- nano/store/lmdb/peer.hpp | 2 +- nano/store/lmdb/pending.hpp | 3 +- nano/store/lmdb/pruned.hpp | 3 +- nano/store/lmdb/rep_weight.hpp | 3 +- nano/store/lmdb/version.hpp | 3 +- 15 files changed, 49 insertions(+), 51 deletions(-) diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index aa05e722b9..8d317e4345 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1375,8 +1375,7 @@ TEST (mdb_block_store, upgrade_v21_v22) nano::store::lmdb::component store (logger, path, nano::dev::constants); auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); - MDB_dbi unchecked_handle{ 0 }; - ASSERT_THROW (::lmdb::dbi_open (store::lmdb::tx (transaction), "unchecked", 0, &unchecked_handle), ::lmdb::not_found_error); + ASSERT_THROW (::lmdb::dbi::open (store::lmdb::tx (transaction), "unchecked", 0), ::lmdb::not_found_error); }; // Testing current version doesn't contain the unchecked table @@ -1387,8 +1386,7 @@ TEST (mdb_block_store, upgrade_v21_v22) nano::store::lmdb::component store (logger, path, nano::dev::constants); auto transaction (store.tx_begin_write ()); store.version.put (transaction, 21); - MDB_dbi unchecked_handle{ 0 }; - ::lmdb::dbi_open (store::lmdb::tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle); + auto unchecked_handle = ::lmdb::dbi::open (store::lmdb::tx (transaction), "unchecked", MDB_CREATE); ASSERT_EQ (store.version.get (transaction), 21); } @@ -1411,8 +1409,7 @@ TEST (mdb_block_store, upgrade_v23_v24) nano::store::lmdb::component store (logger, path, nano::dev::constants); auto transaction (store.tx_begin_write ()); ASSERT_EQ (store.version.get (transaction), store.version_current); - MDB_dbi frontiers_handle{ 0 }; - ASSERT_THROW (::lmdb::dbi_open (store::lmdb::tx (transaction), "frontiers", 0, &frontiers_handle), ::lmdb::not_found_error); + ASSERT_THROW (::lmdb::dbi::open (store::lmdb::tx (transaction), "frontiers", 0), ::lmdb::not_found_error); }; // Testing current version doesn't contain the frontiers table @@ -1423,8 +1420,7 @@ TEST (mdb_block_store, upgrade_v23_v24) nano::store::lmdb::component store (logger, path, nano::dev::constants); auto transaction (store.tx_begin_write ()); store.version.put (transaction, 23); - MDB_dbi frontiers_handle{ 0 }; - ::lmdb::dbi_open (store::lmdb::tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle); + auto frontiers_handle = ::lmdb::dbi::open (store::lmdb::tx (transaction), "frontiers", MDB_CREATE); ASSERT_EQ (store.version.get (transaction), 23); } diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 445d1de54f..7ad999ab13 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -389,9 +389,7 @@ std::vector nano::wallet_store::accounts (store::transaction cons void nano::wallet_store::initialize (store::transaction const & transaction_a, std::string const & path_a) { debug_assert (strlen (path_a.c_str ()) == path_a.size ()); - MDB_dbi handle_l; - ::lmdb::dbi_open (store::lmdb::tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); - handle = handle_l; + handle = ::lmdb::dbi::open (store::lmdb::tx (transaction_a), path_a.c_str (), MDB_CREATE); } bool nano::wallet_store::is_representative (store::transaction const & transaction_a) @@ -1359,8 +1357,8 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) : if (!error_a) { auto transaction (tx_begin_write ()); - ::lmdb::dbi_open (store::lmdb::tx (transaction), nullptr, MDB_CREATE, &handle); - ::lmdb::dbi_open (store::lmdb::tx (transaction), "send_action_ids", MDB_CREATE, &send_action_ids); + handle = ::lmdb::dbi::open (store::lmdb::tx (transaction), nullptr, MDB_CREATE); + send_action_ids = ::lmdb::dbi::open (store::lmdb::tx (transaction), "send_action_ids", MDB_CREATE); std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index 52ad26b391..5ec4ae9d07 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -120,7 +120,7 @@ class wallet_store final static std::size_t const seed_iv_index; static int const special_count; nano::kdf & kdf; - std::atomic handle{ 0 }; + ::lmdb::dbi handle; std::recursive_mutex mutex; private: @@ -231,8 +231,8 @@ class wallets final nano::mutex action_mutex; nano::condition_variable condition; nano::kdf kdf; - MDB_dbi handle; - MDB_dbi send_action_ids; + ::lmdb::dbi handle; + ::lmdb::dbi send_action_ids; nano::node & node; ::lmdb::env & env; std::atomic stopped; diff --git a/nano/store/lmdb/account.hpp b/nano/store/lmdb/account.hpp index 53870f85f6..dc1f969e10 100644 --- a/nano/store/lmdb/account.hpp +++ b/nano/store/lmdb/account.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -32,12 +33,12 @@ class account : public nano::store::account * Maps account v0 to account information, head, rep, open, balance, timestamp, block count and epoch * nano::account -> nano::block_hash, nano::block_hash, nano::block_hash, nano::amount, uint64_t, uint64_t, nano::epoch */ - MDB_dbi accounts_handle{ 0 }; + ::lmdb::dbi accounts_handle; /** * Representative weights. (Removed) * nano::account -> nano::uint128_t */ - MDB_dbi representation_handle{ 0 }; + ::lmdb::dbi representation_handle; }; } // amespace nano::store::lmdb diff --git a/nano/store/lmdb/block.hpp b/nano/store/lmdb/block.hpp index 575803d831..ad95e29f90 100644 --- a/nano/store/lmdb/block.hpp +++ b/nano/store/lmdb/block.hpp @@ -4,6 +4,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -40,7 +41,7 @@ class block : public nano::store::block * Contains block_sideband and block for all block types (legacy send/change/open/receive & state blocks) * nano::block_hash -> nano::block_sideband, nano::block */ - MDB_dbi blocks_handle{ 0 }; + ::lmdb::dbi blocks_handle; protected: void block_raw_get (store::transaction const & transaction_a, nano::block_hash const & hash_a, db_val & value) const; diff --git a/nano/store/lmdb/confirmation_height.hpp b/nano/store/lmdb/confirmation_height.hpp index 4674f81137..dd0ba9c628 100644 --- a/nano/store/lmdb/confirmation_height.hpp +++ b/nano/store/lmdb/confirmation_height.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -32,6 +33,6 @@ class confirmation_height : public nano::store::confirmation_height * Confirmation height of an account, and the hash for the block at that height * nano::account -> uint64_t, nano::block_hash */ - MDB_dbi confirmation_height_handle{ 0 }; + ::lmdb::dbi confirmation_height_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/final_vote.hpp b/nano/store/lmdb/final_vote.hpp index 92fc5253fd..1496df8486 100644 --- a/nano/store/lmdb/final_vote.hpp +++ b/nano/store/lmdb/final_vote.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -32,6 +33,6 @@ class final_vote : public nano::store::final_vote * Maps root to block hash for generated final votes. * nano::qualified_root -> nano::block_hash */ - MDB_dbi final_votes_handle{ 0 }; + ::lmdb::dbi final_votes_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 5e3756b1c8..e9ed1bbc98 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -58,7 +58,7 @@ nano::store::lmdb::component::component (nano::logger & logger_a, std::filesyste auto transaction = tx_begin_read (); try { - ::lmdb::dbi_open (tx (transaction), "meta", 0, &version_store.meta_handle); + version_store.meta_handle = ::lmdb::dbi::open (tx (transaction), "meta", 0); is_fully_upgraded = (version.get (transaction) == version_current); ::lmdb::dbi_close (env, version_store.meta_handle); } @@ -195,16 +195,16 @@ nano::store::lmdb::txn_callbacks nano::store::lmdb::component::create_txn_callba void nano::store::lmdb::component::open_databases (store::transaction const & transaction_a, unsigned flags) { - ::lmdb::dbi_open (tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle); - ::lmdb::dbi_open (tx (transaction_a), "meta", flags, &version_store.meta_handle); - ::lmdb::dbi_open (tx (transaction_a), "peers", flags, &peer_store.peers_handle); - ::lmdb::dbi_open (tx (transaction_a), "pruned", flags, &pruned_store.pruned_handle); - ::lmdb::dbi_open (tx (transaction_a), "confirmation_height", flags, &confirmation_height_store.confirmation_height_handle); - ::lmdb::dbi_open (tx (transaction_a), "accounts", flags, &account_store.accounts_handle); - ::lmdb::dbi_open (tx (transaction_a), "pending", flags, &pending_store.pending_handle); - ::lmdb::dbi_open (tx (transaction_a), "final_votes", flags, &final_vote_store.final_votes_handle); - ::lmdb::dbi_open (tx (transaction_a), "blocks", MDB_CREATE, &block_store.blocks_handle); - ::lmdb::dbi_open (tx (transaction_a), "rep_weights", flags, &rep_weight_store.rep_weights_handle); + online_weight_store.online_weight_handle = ::lmdb::dbi::open (tx (transaction_a), "online_weight", flags); + version_store.meta_handle = ::lmdb::dbi::open (tx (transaction_a), "meta", flags); + peer_store.peers_handle = ::lmdb::dbi::open (tx (transaction_a), "peers", flags); + pruned_store.pruned_handle = ::lmdb::dbi::open (tx (transaction_a), "pruned", flags); + confirmation_height_store.confirmation_height_handle = ::lmdb::dbi::open (tx (transaction_a), "confirmation_height", flags); + account_store.accounts_handle = ::lmdb::dbi::open (tx (transaction_a), "accounts", flags); + pending_store.pending_handle = ::lmdb::dbi::open (tx (transaction_a), "pending", flags); + final_vote_store.final_votes_handle = ::lmdb::dbi::open (tx (transaction_a), "final_votes", flags); + block_store.blocks_handle = ::lmdb::dbi::open (tx (transaction_a), "blocks", MDB_CREATE); + rep_weight_store.rep_weights_handle = ::lmdb::dbi::open (tx (transaction_a), "rep_weights", flags); } bool nano::store::lmdb::component::do_upgrades (store::write_transaction & transaction, nano::ledger_constants & constants, bool & needs_vacuuming) @@ -241,9 +241,8 @@ void nano::store::lmdb::component::upgrade_v21_to_v22 (store::write_transaction { logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22..."); - MDB_dbi unchecked_handle{ 0 }; - ::lmdb::dbi_open (tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle); - ::lmdb::dbi_drop (tx (transaction), unchecked_handle, true); + auto unchecked_handle = ::lmdb::dbi::open (tx (transaction), "unchecked", MDB_CREATE); + unchecked_handle.drop (tx (transaction), true); version.put (transaction, 22); logger.info (nano::log::type::lmdb, "Upgrading database from v21 to v22 completed"); @@ -312,9 +311,8 @@ void nano::store::lmdb::component::upgrade_v23_to_v24 (store::write_transaction { logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24..."); - MDB_dbi frontiers_handle{ 0 }; - ::lmdb::dbi_open (tx (transaction), "frontiers", MDB_CREATE, &frontiers_handle); - ::lmdb::dbi_drop (tx (transaction), frontiers_handle, true); + auto frontiers_handle = ::lmdb::dbi::open (tx (transaction), "frontiers", MDB_CREATE); + frontiers_handle.drop (tx (transaction), true); version.put (transaction, 24); logger.info (nano::log::type::lmdb, "Upgrading database from v23 to v24 completed"); } @@ -460,8 +458,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & std::vector tables = { account_store.accounts_handle, block_store.blocks_handle, pruned_store.pruned_handle, confirmation_height_store.confirmation_height_handle }; for (auto const & table : tables) { - MDB_dbi temp; - ::lmdb::dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); + auto temp = ::lmdb::dbi::open (tx (transaction_a), "temp_table", MDB_CREATE); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, table))), n (store::iterator (nullptr)); i != n; ++i) { @@ -479,12 +476,11 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Remove temporary table - ::lmdb::dbi_drop (tx (transaction_a), temp, true); + temp.drop (tx (transaction_a), true); } // Pending table { - MDB_dbi temp; - ::lmdb::dbi_open (tx (transaction_a), "temp_table", MDB_CREATE, &temp); + auto temp = ::lmdb::dbi::open (tx (transaction_a), "temp_table", MDB_CREATE); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, pending_store.pending_handle))), n (store::iterator (nullptr)); i != n; ++i) { @@ -500,7 +496,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - ::lmdb::dbi_drop (tx (transaction_a), temp, true); + temp.drop (tx (transaction_a), true); } } diff --git a/nano/store/lmdb/online_weight.hpp b/nano/store/lmdb/online_weight.hpp index 0567c63d9b..d38876a048 100644 --- a/nano/store/lmdb/online_weight.hpp +++ b/nano/store/lmdb/online_weight.hpp @@ -25,6 +25,6 @@ class online_weight : public nano::store::online_weight * Samples of online vote weight * uint64_t -> nano::amount */ - MDB_dbi online_weight_handle{ 0 }; + ::lmdb::dbi online_weight_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/options.cpp b/nano/store/lmdb/options.cpp index 335bce6ef0..8d41dc67d8 100644 --- a/nano/store/lmdb/options.cpp +++ b/nano/store/lmdb/options.cpp @@ -31,7 +31,7 @@ auto nano::store::lmdb::options::apply (::lmdb::env & env) -> options & { env.set_max_dbs (config.max_databases); auto map_size = config.map_size; - auto max_instrumented_map_size = 16 * 1024 * 1024; + auto max_instrumented_map_size = 16ull * 1024 * 1024 * 1024; if (memory_intensive_instrumentation () && map_size > max_instrumented_map_size) { // In order to run LMDB with some types of memory instrumentation, the maximum map size must be smaller than what is normally used when non-instrumented diff --git a/nano/store/lmdb/peer.hpp b/nano/store/lmdb/peer.hpp index 8197c00bee..170bacb4a3 100644 --- a/nano/store/lmdb/peer.hpp +++ b/nano/store/lmdb/peer.hpp @@ -26,6 +26,6 @@ class peer : public nano::store::peer * Endpoints for peers * nano::endpoint_key -> no_value */ - MDB_dbi peers_handle{ 0 }; + ::lmdb::dbi peers_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/pending.hpp b/nano/store/lmdb/pending.hpp index fd8a5d13a7..e095bc31cd 100644 --- a/nano/store/lmdb/pending.hpp +++ b/nano/store/lmdb/pending.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -31,6 +32,6 @@ class pending : public nano::store::pending * Maps (destination account, pending block) to (source account, amount, version). (Removed) * nano::account, nano::block_hash -> nano::account, nano::amount, nano::epoch */ - MDB_dbi pending_handle{ 0 }; + ::lmdb::dbi pending_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/pruned.hpp b/nano/store/lmdb/pruned.hpp index 0aa2d7fc20..1272cb3e1b 100644 --- a/nano/store/lmdb/pruned.hpp +++ b/nano/store/lmdb/pruned.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -28,6 +29,6 @@ class pruned : public nano::store::pruned * Pruned blocks hashes * nano::block_hash -> none */ - MDB_dbi pruned_handle{ 0 }; + ::lmdb::dbi pruned_handle; }; } // namespace nano::store::lmdb diff --git a/nano/store/lmdb/rep_weight.hpp b/nano/store/lmdb/rep_weight.hpp index 2a6ef53c5c..5b5a86dc37 100644 --- a/nano/store/lmdb/rep_weight.hpp +++ b/nano/store/lmdb/rep_weight.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -29,6 +30,6 @@ class rep_weight : public nano::store::rep_weight * Representative weights * nano::account -> uint128_t */ - MDB_dbi rep_weights_handle{ 0 }; + ::lmdb::dbi rep_weights_handle; }; } diff --git a/nano/store/lmdb/version.hpp b/nano/store/lmdb/version.hpp index 7ec10b8183..af653d31a1 100644 --- a/nano/store/lmdb/version.hpp +++ b/nano/store/lmdb/version.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace nano::store::lmdb { @@ -20,6 +21,6 @@ class version : public nano::store::version * Meta information about block store, such as versions. * nano::uint256_union (arbitrary key) -> blob */ - MDB_dbi meta_handle{ 0 }; + ::lmdb::dbi meta_handle; }; } // namespace nano::store::lmdb From ff962bf68ab859f9f2b8bcd8cfff6887f2591999 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 19:33:47 +0100 Subject: [PATCH 17/18] Replace remaining usages of MDB_dbi with ::lmdb::dbi --- nano/store/lmdb/iterator.hpp | 6 +++--- nano/store/lmdb/lmdb.cpp | 24 +++++++++++++++--------- nano/store/lmdb/lmdb.hpp | 7 ++++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index ce6751ad9d..b7a9b48085 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -14,7 +14,7 @@ template class iterator : public iterator_impl { public: - iterator (store::transaction const & transaction_a, ::lmdb::env const & env_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : + iterator (store::transaction const & transaction_a, ::lmdb::env const & env_a, ::lmdb::dbi const & db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : nano::store::iterator_impl (transaction_a) { auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor)); @@ -176,7 +176,7 @@ template class merge_iterator : public iterator_impl { public: - merge_iterator (store::transaction const & transaction_a, MDB_dbi db1_a, MDB_dbi db2_a) : + merge_iterator (store::transaction const & transaction_a, ::lmdb::dbi const & db1_a, ::lmdb::dbi const & db2_a) : impl1 (std::make_unique> (transaction_a, db1_a)), impl2 (std::make_unique> (transaction_a, db2_a)) { @@ -188,7 +188,7 @@ class merge_iterator : public iterator_impl { } - merge_iterator (store::transaction const & transaction_a, MDB_dbi db1_a, MDB_dbi db2_a, MDB_val const & val_a) : + merge_iterator (store::transaction const & transaction_a, ::lmdb::dbi const & db1_a, ::lmdb::dbi const & db2_a, MDB_val const & val_a) : impl1 (std::make_unique> (transaction_a, db1_a, val_a)), impl2 (std::make_unique> (transaction_a, db2_a, val_a)) { diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index e9ed1bbc98..3512739128 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -371,7 +371,7 @@ int nano::store::lmdb::component::drop (store::write_transaction const & transac { try { - ::lmdb::dbi_drop (tx (transaction_a), table_to_dbi (table_a), true); + table_to_dbi (table_a).drop (tx (transaction_a), true); return 0; } catch (::lmdb::runtime_error const &) @@ -380,9 +380,9 @@ int nano::store::lmdb::component::drop (store::write_transaction const & transac } } -void nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, MDB_dbi handle_a) +void nano::store::lmdb::component::clear (store::write_transaction const & transaction_a, ::lmdb::dbi & handle_a) { - ::lmdb::dbi_drop (tx (transaction_a), handle_a, false); + handle_a.drop (tx (transaction_a), false); } uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, tables table_a) const @@ -390,14 +390,14 @@ uint64_t nano::store::lmdb::component::count (store::transaction const & transac return count (transaction_a, table_to_dbi (table_a)); } -uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, MDB_dbi db_a) const +uint64_t nano::store::lmdb::component::count (store::transaction const & transaction_a, ::lmdb::dbi const & db_a) const { MDB_stat stats; ::lmdb::dbi_stat (tx (transaction_a), db_a, &stats); return (stats.ms_entries); } -MDB_dbi nano::store::lmdb::component::table_to_dbi (tables table_a) const +auto nano::store::lmdb::component::table_to_dbi (tables table_a) -> ::lmdb::dbi & { switch (table_a) { @@ -427,6 +427,11 @@ MDB_dbi nano::store::lmdb::component::table_to_dbi (tables table_a) const } } +auto nano::store::lmdb::component::table_to_dbi (tables table_a) const -> ::lmdb::dbi const & +{ + return const_cast (this)->table_to_dbi (table_a); +} + bool nano::store::lmdb::component::not_found (int status) const { return (status_code_not_found () == status); @@ -455,9 +460,10 @@ bool nano::store::lmdb::component::copy_db (std::filesystem::path const & destin void nano::store::lmdb::component::rebuild_db (store::write_transaction const & transaction_a) { // Tables with uint256_union key - std::vector tables = { account_store.accounts_handle, block_store.blocks_handle, pruned_store.pruned_handle, confirmation_height_store.confirmation_height_handle }; - for (auto const & table : tables) + std::vector<::lmdb::dbi *> tables = { &account_store.accounts_handle, &block_store.blocks_handle, &pruned_store.pruned_handle, &confirmation_height_store.confirmation_height_handle }; + for (auto & table_p : tables) { + auto & table = *table_p; auto temp = ::lmdb::dbi::open (tx (transaction_a), "temp_table", MDB_CREATE); // Copy all values to temporary table for (auto i (store::iterator (std::make_unique> (transaction_a, env, table))), n (store::iterator (nullptr)); i != n; ++i) @@ -467,7 +473,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & } release_assert (count (transaction_a, table) == count (transaction_a, temp)); // Clear existing table - ::lmdb::dbi_drop (tx (transaction_a), table, false); + table.drop (tx (transaction_a), false); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { @@ -488,7 +494,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & release_assert_success (s); } release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); - ::lmdb::dbi_drop (tx (transaction_a), pending_store.pending_handle, false); + pending_store.pending_handle.drop (tx (transaction_a), false); // Put values from copy for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index d2ae1ab6ef..a92da50007 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -106,7 +106,7 @@ class component : public nano::store::component bool init_error () const override; - uint64_t count (store::transaction const &, MDB_dbi) const; + uint64_t count (store::transaction const &, ::lmdb::dbi const &) const; std::string error_string (int status) const override; private: @@ -118,7 +118,7 @@ class component : public nano::store::component void open_databases (store::transaction const &, unsigned); int drop (store::write_transaction const & transaction_a, tables table_a) override; - void clear (store::write_transaction const & transaction_a, MDB_dbi handle_a); + void clear (store::write_transaction const & transaction_a, ::lmdb::dbi & handle_a); bool not_found (int status) const override; bool success (int status) const override; @@ -131,7 +131,8 @@ class component : public nano::store::component } int status_code_not_found () const override; - MDB_dbi table_to_dbi (tables table_a) const; + auto table_to_dbi (tables table_a) -> ::lmdb::dbi &; + auto table_to_dbi (tables table_a) const -> ::lmdb::dbi const &; mutable nano::mdb_txn_tracker mdb_txn_tracker; nano::store::lmdb::txn_callbacks create_txn_callbacks () const; From 519f57e4c7291d40c9ea57b224044846c3cdca57 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 20:06:18 +0100 Subject: [PATCH 18/18] Convert MDB_cursor and operations to ::lmdb variants. --- nano/store/lmdb/iterator.hpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index b7a9b48085..83fde7de80 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -15,11 +15,10 @@ class iterator : public iterator_impl { public: iterator (store::transaction const & transaction_a, ::lmdb::env const & env_a, ::lmdb::dbi const & db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) : - nano::store::iterator_impl (transaction_a) + nano::store::iterator_impl (transaction_a), + cursor{ ::lmdb::cursor::open (tx (transaction_a), db_a) } { - auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor)); - release_assert (status == 0); - auto operation (MDB_SET_RANGE); + auto operation = MDB_SET_RANGE; if (val_a.mv_size != 0) { current.first = val_a; @@ -28,12 +27,9 @@ class iterator : public iterator_impl { operation = direction_asc ? MDB_FIRST : MDB_LAST; } - auto status2 (mdb_cursor_get (cursor, ¤t.first.value, ¤t.second.value, operation)); - release_assert (status2 == 0 || status2 == MDB_NOTFOUND); - if (status2 != MDB_NOTFOUND) + if (::lmdb::cursor_get (cursor, ¤t.first.value, ¤t.second.value, operation)) { - auto status3 (mdb_cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_GET_CURRENT)); - release_assert (status3 == 0 || status3 == MDB_NOTFOUND); + ::lmdb::cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_GET_CURRENT); if (current.first.size () != sizeof (T)) { clear (); @@ -60,16 +56,14 @@ class iterator : public iterator_impl { if (cursor != nullptr) { - mdb_cursor_close (cursor); + cursor.close (); } } store::iterator_impl & operator++ () override { debug_assert (cursor != nullptr); - auto status (mdb_cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_NEXT)); - release_assert (status == 0 || status == MDB_NOTFOUND); - if (status == MDB_NOTFOUND) + if (!::lmdb::cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_NEXT)) { clear (); } @@ -83,9 +77,7 @@ class iterator : public iterator_impl store::iterator_impl & operator-- () override { debug_assert (cursor != nullptr); - auto status (mdb_cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_PREV)); - release_assert (status == 0 || status == MDB_NOTFOUND); - if (status == MDB_NOTFOUND) + if (!::lmdb::cursor_get (cursor, ¤t.first.value, ¤t.second.value, MDB_PREV)) { clear (); } @@ -155,7 +147,7 @@ class iterator : public iterator_impl { if (cursor != nullptr) { - mdb_cursor_close (cursor); + cursor.close (); } cursor = other_a.cursor; other_a.cursor = nullptr; @@ -165,7 +157,7 @@ class iterator : public iterator_impl } store::iterator_impl & operator= (store::iterator_impl const &) = delete; - MDB_cursor * cursor{ nullptr }; + ::lmdb::cursor cursor; std::pair, store::db_val> current; }; @@ -282,7 +274,7 @@ class merge_iterator : public iterator_impl } else { - auto key_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.first, impl2->current.first)); + auto key_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.first, impl2->current.first)); if (key_cmp < 0) { @@ -296,7 +288,7 @@ class merge_iterator : public iterator_impl } else { - auto val_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.second, impl2->current.second)); + auto val_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.second, impl2->current.second)); result = val_cmp < 0 ? impl1.get () : impl2.get (); from_first_database = (result == impl1.get ()); }