forked from nanocurrency/nano-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
vote_uniquer
(nanocurrency#4339)
* Introduce generic `uniquer` class * Convert `block_uniquer` to use generic implementation * Convert `vote_uniquer` to use generic implementation * Adjust `uniquer` class container info reporting * Use nano specific lock primitives
- Loading branch information
1 parent
083f243
commit bba89c3
Showing
11 changed files
with
98 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/interval.hpp> | ||
#include <nano/lib/locks.hpp> | ||
#include <nano/lib/utility.hpp> | ||
|
||
#include <memory> | ||
|
||
namespace nano | ||
{ | ||
template <typename Key, typename Value> | ||
class uniquer final | ||
{ | ||
public: | ||
using key_type = Key; | ||
using value_type = Value; | ||
|
||
std::shared_ptr<Value> unique (std::shared_ptr<Value> const & value) | ||
{ | ||
if (value == nullptr) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
// Types used as value need to provide full_hash() | ||
Key hash = value->full_hash (); | ||
|
||
nano::lock_guard<nano::mutex> guard{ mutex }; | ||
|
||
if (cleanup_interval.elapsed ()) | ||
{ | ||
cleanup (); | ||
} | ||
|
||
auto & existing = values[hash]; | ||
if (auto result = existing.lock ()) | ||
{ | ||
return result; | ||
} | ||
else | ||
{ | ||
existing = value; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
std::size_t size () const | ||
{ | ||
nano::lock_guard<nano::mutex> guard{ mutex }; | ||
return values.size (); | ||
} | ||
|
||
std::unique_ptr<container_info_component> collect_container_info (std::string const & name) const | ||
{ | ||
nano::lock_guard<nano::mutex> guard{ mutex }; | ||
|
||
auto composite = std::make_unique<container_info_composite> (name); | ||
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "cache", values.size (), sizeof (Value) })); | ||
return composite; | ||
} | ||
|
||
static std::chrono::milliseconds constexpr cleanup_cutoff{ 500 }; | ||
|
||
private: | ||
void cleanup () | ||
{ | ||
debug_assert (!mutex.try_lock ()); | ||
|
||
std::erase_if (values, [] (auto const & item) { | ||
return item.second.expired (); | ||
}); | ||
} | ||
|
||
private: | ||
mutable nano::mutex mutex; | ||
std::unordered_map<Key, std::weak_ptr<Value>> values; | ||
nano::interval cleanup_interval{ cleanup_cutoff }; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters