From ff962bf68ab859f9f2b8bcd8cfff6887f2591999 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 9 Sep 2024 19:33:47 +0100 Subject: [PATCH] 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;