Skip to content

Commit

Permalink
Merge branch 'nanocurrency:develop' into qt_state_open
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiySW authored Oct 27, 2024
2 parents c98f769 + 3ca0480 commit 7f4b7d6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
21 changes: 7 additions & 14 deletions nano/store/lmdb/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void iterator::update (int status)
if (status == MDB_SUCCESS)
{
value_type init;
auto status = mdb_cursor_get (cursor, &init.first, &init.second, MDB_GET_CURRENT);
auto status = mdb_cursor_get (cursor.get (), &init.first, &init.second, MDB_GET_CURRENT);
release_assert (status == MDB_SUCCESS);
current = init;
}
Expand All @@ -33,8 +33,10 @@ void iterator::update (int status)

iterator::iterator (MDB_txn * tx, MDB_dbi dbi) noexcept
{
MDB_cursor * cursor;
auto open_status = mdb_cursor_open (tx, dbi, &cursor);
release_assert (open_status == MDB_SUCCESS);
this->cursor.reset (cursor);
this->current = std::monostate{};
}

Expand All @@ -53,7 +55,7 @@ auto iterator::end (MDB_txn * tx, MDB_dbi dbi) -> iterator
auto iterator::lower_bound (MDB_txn * tx, MDB_dbi dbi, MDB_val const & lower_bound) -> iterator
{
iterator result{ tx, dbi };
auto status = mdb_cursor_get (result.cursor, const_cast<MDB_val *> (&lower_bound), nullptr, MDB_SET_RANGE);
auto status = mdb_cursor_get (result.cursor.get (), const_cast<MDB_val *> (&lower_bound), nullptr, MDB_SET_RANGE);
result.update (status);
return std::move (result);
}
Expand All @@ -63,18 +65,9 @@ iterator::iterator (iterator && other) noexcept
*this = std::move (other);
}

iterator::~iterator ()
{
if (cursor)
{
mdb_cursor_close (cursor);
}
}

auto iterator::operator= (iterator && other) noexcept -> iterator &
{
cursor = other.cursor;
other.cursor = nullptr;
cursor = std::move (other.cursor);
current = other.current;
other.current = std::monostate{};
return *this;
Expand All @@ -83,7 +76,7 @@ auto iterator::operator= (iterator && other) noexcept -> iterator &
auto iterator::operator++ () -> iterator &
{
auto operation = is_end () ? MDB_FIRST : MDB_NEXT;
auto status = mdb_cursor_get (cursor, nullptr, nullptr, operation);
auto status = mdb_cursor_get (cursor.get (), nullptr, nullptr, operation);
release_assert (status == MDB_SUCCESS || status == MDB_NOTFOUND);
update (status);
return *this;
Expand All @@ -92,7 +85,7 @@ auto iterator::operator++ () -> iterator &
auto iterator::operator-- () -> iterator &
{
auto operation = is_end () ? MDB_LAST : MDB_PREV;
auto status = mdb_cursor_get (cursor, nullptr, nullptr, operation);
auto status = mdb_cursor_get (cursor.get (), nullptr, nullptr, operation);
release_assert (status == MDB_SUCCESS || status == MDB_NOTFOUND);
update (status);
return *this;
Expand Down
4 changes: 1 addition & 3 deletions nano/store/lmdb/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace nano::store::lmdb
*/
class iterator
{
MDB_cursor * cursor{ nullptr };
std::unique_ptr<MDB_cursor, decltype (&mdb_cursor_close)> cursor{ nullptr, mdb_cursor_close };
std::variant<std::monostate, std::pair<MDB_val, MDB_val>> current;
void update (int status);
iterator (MDB_txn * tx, MDB_dbi dbi) noexcept;
Expand All @@ -39,8 +39,6 @@ class iterator
static auto end (MDB_txn * tx, MDB_dbi dbi) -> iterator;
static auto lower_bound (MDB_txn * tx, MDB_dbi dbi, MDB_val const & lower_bound) -> iterator;

~iterator ();

iterator (iterator const &) = delete;
auto operator= (iterator const &) -> iterator & = delete;

Expand Down
8 changes: 3 additions & 5 deletions nano/store/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ 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;
mdb_env_sync (env, true);

// Replace the ledger file with the vacuumed one
std::filesystem::rename (vacuum_path, path_a);
Expand Down Expand Up @@ -155,7 +153,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, &stats));
release_assert (status == 0);
json.put ("branch_pages", stats.ms_branch_pages);
json.put ("depth", stats.ms_depth);
Expand Down Expand Up @@ -448,7 +446,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, destination_file.string ().c_str (), MDB_CP_COMPACT);
}

void nano::store::lmdb::component::rebuild_db (store::write_transaction const & transaction_a)
Expand Down
9 changes: 4 additions & 5 deletions nano/store/lmdb/lmdb_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const &
nano::set_secure_perm_directory (path_a.parent_path (), error_chmod);
if (!error_mkdir)
{
MDB_env * environment;
auto status1 (mdb_env_create (&environment));
release_assert (status1 == 0);
this->environment.reset (environment);
auto status2 (mdb_env_set_maxdbs (environment, options_a.config.max_databases));
release_assert (status2 == 0);
auto map_size = options_a.config.map_size;
Expand Down Expand Up @@ -66,13 +68,11 @@ void nano::store::lmdb::env::init (bool & error_a, std::filesystem::path const &
else
{
error_a = true;
environment = nullptr;
}
}
else
{
error_a = true;
environment = nullptr;
}
}

Expand All @@ -81,14 +81,13 @@ 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);
mdb_env_sync (environment.get (), true);
}
}

nano::store::lmdb::env::operator MDB_env * () const
{
return environment;
return environment.get ();
}

nano::store::read_transaction nano::store::lmdb::env::tx_begin_read (store::lmdb::txn_callbacks mdb_txn_callbacks) const
Expand Down
2 changes: 1 addition & 1 deletion nano/store/lmdb/lmdb_env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class env final
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;
std::unique_ptr<MDB_env, decltype (&mdb_env_close)> environment{ nullptr, mdb_env_close };
nano::id_t const store_id{ nano::next_id () };
};
} // namespace nano::store::lmdb

0 comments on commit 7f4b7d6

Please sign in to comment.