mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-28 06:25:49 +00:00
concurrent peerfinder
This commit is contained in:
@@ -1,32 +1,52 @@
|
||||
#ifndef RIPPLE_PEERFINDER_INMEMORYSTORE_H_INCLUDED
|
||||
#define RIPPLE_PEERFINDER_INMEMORYSTORE_H_INCLUDED
|
||||
|
||||
#include <ripple/beast/net/IPEndpoint.h>
|
||||
#include <ripple/peerfinder/impl/Store.h>
|
||||
#include <vector>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <boost/unordered/concurrent_flat_map.hpp>
|
||||
|
||||
namespace ripple {
|
||||
namespace PeerFinder {
|
||||
|
||||
struct EndpointHasher
|
||||
{
|
||||
std::size_t
|
||||
operator()(beast::IP::Endpoint const& endpoint) const
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
boost::hash_combine(seed, endpoint.address().to_string());
|
||||
boost::hash_combine(seed, endpoint.port());
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
class InMemoryStore : public Store
|
||||
{
|
||||
private:
|
||||
std::vector<Entry> entries;
|
||||
boost::concurrent_flat_map<beast::IP::Endpoint, int, EndpointHasher>
|
||||
entries;
|
||||
|
||||
public:
|
||||
std::size_t
|
||||
load(load_callback const& cb) override
|
||||
{
|
||||
for (auto const& entry : entries)
|
||||
{
|
||||
cb(entry.endpoint, entry.valence);
|
||||
}
|
||||
return entries.size();
|
||||
std::size_t count = 0;
|
||||
entries.visit_all([&](auto const& entry) {
|
||||
cb(entry.first, entry.second);
|
||||
++count;
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
void
|
||||
save(std::vector<Entry> const& v) override
|
||||
{
|
||||
entries = v;
|
||||
entries.clear();
|
||||
for (auto const& entry : v)
|
||||
{
|
||||
entries.emplace(entry.endpoint, entry.valence);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user