Skip to content

Commit

Permalink
Add multi-index utility function for value retrieval
Browse files Browse the repository at this point in the history
- Introduced `get_value_multi_index` in the utility class to facilitate value retrieval from Boost multi-index containers.
- Refactored `active_transactions.cpp` to utilise the new utility function, streamlining code readability.

This change aids in centralising the logic for multi-index value retrieval
  • Loading branch information
gr0vity-dev committed Oct 1, 2023
1 parent 847f54c commit f73eb1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
11 changes: 11 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ auto get_value (const MapType & map, const KeyType & key) -> std::optional<typen
return std::nullopt;
}

template <typename MultiIndex, typename KeyType>
auto get_value_multi_index (const MultiIndex & mi, const KeyType & key) -> std::optional<typename MultiIndex::value_type>
{
auto it = mi.find (key);
if (it != mi.end ())
{
return *it;
}
return std::nullopt;
}

/**
* Erase elements from container when predicate returns true
* TODO: Use `std::erase_if` in c++20
Expand Down
24 changes: 12 additions & 12 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un
if (!stopped)
{
auto root (block_a->qualified_root ());
auto existing (roots.get<tag_root> ().find (root));
if (existing == roots.get<tag_root> ().end ())
auto existing_entry = get_value_multi_index (roots.get<tag_root> (), root);
if (!existing_entry)
{
if (!recently_confirmed.exists (root))
{
Expand Down Expand Up @@ -427,7 +427,7 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un
}
else
{
result.election = existing->election;
result.election = existing_entry->election;
}

if (lock_a.owns_lock ())
Expand Down Expand Up @@ -532,10 +532,10 @@ std::shared_ptr<nano::election> nano::active_transactions::election (nano::quali
{
std::shared_ptr<nano::election> result;
nano::lock_guard<nano::mutex> lock{ mutex };
auto existing = roots.get<tag_root> ().find (root_a);
if (existing != roots.get<tag_root> ().end ())
auto existing_entry = get_value_multi_index (roots.get<tag_root> (), root_a);
if (existing_entry)
{
result = existing->election;
result = existing_entry->election;
}
return result;
}
Expand All @@ -561,10 +561,10 @@ void nano::active_transactions::erase (nano::block const & block_a)
void nano::active_transactions::erase (nano::qualified_root const & root_a)
{
nano::unique_lock<nano::mutex> lock{ mutex };
auto root_it (roots.get<tag_root> ().find (root_a));
if (root_it != roots.get<tag_root> ().end ())
auto existing_entry = get_value_multi_index (roots.get<tag_root> (), root_a);
if (existing_entry)
{
cleanup_election (lock, root_it->election);
cleanup_election (lock, existing_entry->election);
}
}

Expand Down Expand Up @@ -600,11 +600,11 @@ std::size_t nano::active_transactions::size () const
bool nano::active_transactions::publish (std::shared_ptr<nano::block> const & block_a)
{
nano::unique_lock<nano::mutex> lock{ mutex };
auto existing (roots.get<tag_root> ().find (block_a->qualified_root ()));
auto result (true);
if (existing != roots.get<tag_root> ().end ())
auto existing_entry = get_value_multi_index (roots.get<tag_root> (), block_a->qualified_root ());
if (existing_entry)
{
auto election (existing->election);
auto election = existing_entry->election;
lock.unlock ();
result = election->publish (block_a);
if (!result)
Expand Down

0 comments on commit f73eb1c

Please sign in to comment.