20 #ifndef RIPPLE_PEERFINDER_LIVECACHE_H_INCLUDED
21 #define RIPPLE_PEERFINDER_LIVECACHE_H_INCLUDED
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/random.h>
25 #include <ripple/beast/container/aged_map.h>
26 #include <ripple/beast/utility/maybe_const.h>
27 #include <ripple/peerfinder/PeerfinderManager.h>
28 #include <ripple/peerfinder/impl/Tuning.h>
29 #include <ripple/peerfinder/impl/iosformat.h>
30 #include <boost/intrusive/list.hpp>
31 #include <boost/iterator/transform_iterator.hpp>
36 namespace PeerFinder {
49 struct Element : boost::intrusive::list_base_hook<>
59 make_list<Element, boost::intrusive::constant_time_size<false>>::type;
66 template <
bool IsConst>
72 #ifdef _LIBCPP_VERSION
76 #ifndef _LIBCPP_VERSION
92 transform_iterator<Transform, typename list_type::const_iterator>;
98 typename list_type::const_reverse_iterator>;
154 auto& e(
const_cast<Element&
>(*pos.base()));
175 template <
bool IsConst>
199 template <
class Allocator = std::allocator<
char>>
200 class Livecache :
protected detail::LivecacheBase
220 Allocator alloc = Allocator());
240 template <
bool IsConst>
242 #ifdef _LIBCPP_VERSION
244 unary_function<typename lists_type::value_type, Hop<IsConst>>
247 #ifndef _LIBCPP_VERSION
257 typename lists_type::value_type>::type& list)
const
259 return make_hop<IsConst>(list);
265 transform_iterator<Transform<false>,
typename lists_type::iterator>;
269 typename lists_type::const_iterator>;
273 typename lists_type::reverse_iterator>;
277 typename lists_type::const_reverse_iterator>;
359 explicit hops_t(Allocator
const& alloc);
405 template <
class Allocator>
410 : m_journal(journal), m_cache(clock, alloc), hops(alloc)
414 template <
class Allocator>
424 Element& e(iter->second);
426 iter = m_cache.
erase(iter);
432 << ((n > 1) ?
" entries" :
" entry");
436 template <
class Allocator>
449 Element& e(result.
first->second);
457 else if (!result.
second && (ep.
hops > e.endpoint.hops))
462 << ep.
address <<
" at hops +" << excess;
469 if (ep.
hops < e.endpoint.hops)
471 hops.reinsert(e, ep.
hops);
482 template <
class Allocator>
488 map[
"size"] = size();
489 map[
"hist"] = hops.histogram();
491 for (
auto iter(m_cache.
cbegin()); iter != m_cache.
cend(); ++iter)
493 auto const& e(iter->second);
495 item[
"hops"] = e.endpoint.hops;
496 item[
"address"] = e.endpoint.address.to_string();
498 ss << (iter.when() -
expired).count();
499 item[
"expires"] = ss.
str();
505 template <
class Allocator>
521 template <
class Allocator>
526 for (
typename decltype(m_hist)::size_type i(0); i < m_hist.size(); ++i)
533 template <
class Allocator>
536 std::fill(m_hist.begin(), m_hist.end(), 0);
539 template <
class Allocator>
543 assert(e.endpoint.hops >= 0 && e.endpoint.hops <=
Tuning::maxHops + 1);
545 m_lists[e.endpoint.hops].push_front(e);
546 ++m_hist[e.endpoint.hops];
549 template <
class Allocator>
554 list_type& list(m_lists[e.endpoint.hops]);
555 list.erase(list.iterator_to(e));
556 --m_hist[e.endpoint.hops];
558 e.endpoint.hops = numHops;
562 template <
class Allocator>
566 --m_hist[e.endpoint.hops];
567 list_type& list(m_lists[e.endpoint.hops]);
568 list.erase(list.iterator_to(e));
boost::transform_iterator< Transform, typename list_type::const_reverse_iterator > reverse_iterator
beast::detail::aged_container_iterator< false, Iterator, Base > erase(beast::detail::aged_container_iterator< is_const, Iterator, Base > pos)
bool empty() const noexcept
std::string histogram() const
static const std::chrono::seconds liveCacheSecondsToLive(30)
const_reverse_iterator rbegin() const
Stream trace() const
Severity stream access functions.
boost::transform_iterator< Transform< false >, typename lists_type::reverse_iterator > reverse_iterator
time_point const & when() const
T back_inserter(T... args)
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
boost::transform_iterator< Transform< false >, typename lists_type::iterator > iterator
A list of Endpoint at the same hops This is a lightweight wrapper around a reference to the underlyin...
boost::intrusive::make_list< Element, boost::intrusive::constant_time_size< false > >::type list_type
@ expired
List is expired, but has the largest non-pending sequence seen so far.
typename clock_type::time_point time_point
cache_type::size_type size() const
Returns the number of entries in the cache.
virtual time_point now() const =0
Returns the current time.
Element(Endpoint const &endpoint_)
void insert(Endpoint const &ep)
Creates or updates an existing Element based on a new message.
boost::transform_iterator< Transform, typename list_type::const_iterator > iterator
const_iterator cend() const
reverse_iterator rend() const
const_reverse_iterator crend() const
reverse_iterator rbegin() const
const_reverse_iterator rend() const
const_reverse_iterator crbegin() const
auto emplace(Args &&... args) -> typename std::enable_if<!maybe_multi, std::pair< iterator, bool >>::type
Livecache(clock_type &clock, beast::Journal journal, Allocator alloc=Allocator())
Create the cache.
void shuffle()
Shuffle each hop list.
reverse_iterator rbegin()
void move_back(const_iterator pos)
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
boost::transform_iterator< Transform< true >, typename lists_type::const_iterator > const_iterator
std::reference_wrapper< typename beast::maybe_const< IsConst, list_type >::type > m_list
beast::xor_shift_engine & default_prng()
Return the default random engine.
void reinsert(Element &e, int hops)
The Livecache holds the short-lived relayed Endpoint messages.
A generic endpoint for log messages.
const_iterator end() const
reverse_iterator crend() const
class ripple::PeerFinder::Livecache::hops_t hops
Makes T const or non const depending on a bool.
Left justifies a field at the specified width.
const_iterator begin() const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
size_type size() const noexcept
hops_t(Allocator const &alloc)
const_iterator cbegin() const
Associative container where each element is also indexed by time.
typename std::conditional< IsConst, typename std::remove_const< T >::type const, typename std::remove_const< T >::type >::type type
class beast::detail::aged_ordered_container::chronological_t chronological
Hop(typename beast::maybe_const< IsConst, list_type >::type &list)
beast::aged_map< beast::IP::Endpoint, Element, std::chrono::steady_clock, std::less< beast::IP::Endpoint >, Allocator > cache_type
void expire()
Erase entries whose time has expired.
const_iterator cend() const
bool empty() const
Returns true if the cache is empty.
A version-independent IP address and port combination.
boost::transform_iterator< Transform< true >, typename lists_type::const_reverse_iterator > const_reverse_iterator
const_iterator cbegin() const
Describes a connectible peer address along with some metadata.
reverse_iterator const_reverse_iterator
beast::IP::Endpoint address
static Hop< IsConst > make_hop(typename beast::maybe_const< IsConst, list_type >::type &list)
reverse_iterator crbegin() const
void touch(beast::detail::aged_container_iterator< is_const, Iterator, Base > pos)