Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility to get values for maps and multi_index containers #4303

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ void transform_if (InputIt first, InputIt last, OutputIt dest, Pred pred, Func t
}
}

template <typename MapType, typename KeyType>
auto get_value (const MapType & map, const KeyType & key) -> std::optional<typename MapType::mapped_type>
{
auto it = map.find (key);
if (it != map.end ())
{
return it->second;
}
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
43 changes: 21 additions & 22 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ void nano::active_transactions::block_cemented_callback (std::shared_ptr<nano::b
{
auto hash (block_a->hash ());
nano::unique_lock<nano::mutex> election_winners_lk{ election_winner_details_mutex };
auto existing (election_winner_details.find (hash));
if (existing != election_winner_details.end ())
auto existing_election = get_value (election_winner_details, hash);
if (existing_election)
{
auto election = existing->second;
auto election = *existing_election;
election_winner_details.erase (hash);
election_winners_lk.unlock ();
if (election->confirmed () && election->winner ()->hash () == hash)
Expand Down 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 @@ -544,12 +544,11 @@ std::shared_ptr<nano::block> nano::active_transactions::winner (nano::block_hash
{
std::shared_ptr<nano::block> result;
nano::unique_lock<nano::mutex> lock{ mutex };
auto existing = blocks.find (hash_a);
if (existing != blocks.end ())
auto existing_election = get_value (blocks, hash_a);
if (existing_election)
{
auto election = existing->second;
lock.unlock ();
result = election->winner ();
result = (*existing_election)->winner ();
}
return result;
}
Expand All @@ -562,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 @@ -601,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 All @@ -630,10 +629,10 @@ boost::optional<nano::election_status_type> nano::active_transactions::confirm_b
std::shared_ptr<nano::election> election = nullptr;
{
nano::lock_guard<nano::mutex> guard{ mutex };
auto existing = blocks.find (hash);
if (existing != blocks.end ())
auto existing_election = get_value (blocks, hash);
if (existing_election)
{
election = existing->second;
election = *existing_election;
}
}

Expand Down
Loading