Struct libp2p::kad::kbucket::KBucketsTable
source · pub struct KBucketsTable<TKey, TVal> { /* private fields */ }
Expand description
A KBucketsTable
represents a Kademlia routing table.
Implementations§
source§impl<TKey, TVal> KBucketsTable<TKey, TVal>where
TKey: Clone + AsRef<KeyBytes>,
TVal: Clone,
impl<TKey, TVal> KBucketsTable<TKey, TVal>where TKey: Clone + AsRef<KeyBytes>, TVal: Clone,
sourcepub fn new(
local_key: TKey,
pending_timeout: Duration
) -> KBucketsTable<TKey, TVal>
pub fn new( local_key: TKey, pending_timeout: Duration ) -> KBucketsTable<TKey, TVal>
Creates a new, empty Kademlia routing table with entries partitioned into buckets as per the Kademlia protocol.
The given pending_timeout
specifies the duration after creation of
a PendingEntry
after which it becomes eligible for insertion into
a full bucket, replacing the least-recently (dis)connected node.
sourcepub fn entry<'a>(&'a mut self, key: &'a TKey) -> Entry<'a, TKey, TVal>
pub fn entry<'a>(&'a mut self, key: &'a TKey) -> Entry<'a, TKey, TVal>
Returns an Entry
for the given key, representing the state of the entry
in the routing table.
sourcepub fn iter(&mut self) -> impl Iterator<Item = KBucketRef<'_, TKey, TVal>>
pub fn iter(&mut self) -> impl Iterator<Item = KBucketRef<'_, TKey, TVal>>
Returns an iterator over all buckets.
The buckets are ordered by proximity to the local_key
, i.e. the first
bucket is the closest bucket (containing at most one key).
sourcepub fn bucket<K>(&mut self, key: &K) -> Option<KBucketRef<'_, TKey, TVal>>where
K: AsRef<KeyBytes>,
pub fn bucket<K>(&mut self, key: &K) -> Option<KBucketRef<'_, TKey, TVal>>where K: AsRef<KeyBytes>,
Returns the bucket for the distance to the given key.
Returns None
if the given key refers to the local key.
sourcepub fn take_applied_pending(&mut self) -> Option<AppliedPending<TKey, TVal>>
pub fn take_applied_pending(&mut self) -> Option<AppliedPending<TKey, TVal>>
Consumes the next applied pending entry, if any.
When an entry is attempted to be inserted and the respective bucket is full,
it may be recorded as pending insertion after a timeout, see InsertResult::Pending
.
If the oldest currently disconnected entry in the respective bucket does not change
its status until the timeout of pending entry expires, it is evicted and
the pending entry inserted instead. These insertions of pending entries
happens lazily, whenever the KBucketsTable
is accessed, and the corresponding
buckets are updated accordingly. The fact that a pending entry was applied is
recorded in the KBucketsTable
in the form of AppliedPending
results, which must be
consumed by calling this function.
sourcepub fn closest_keys<T, 'a>(
&'a mut self,
target: &'a T
) -> impl Iterator<Item = TKey> + 'awhere
T: AsRef<KeyBytes>,
pub fn closest_keys<T, 'a>( &'a mut self, target: &'a T ) -> impl Iterator<Item = TKey> + 'awhere T: AsRef<KeyBytes>,
Returns an iterator over the keys closest to target
, ordered by
increasing distance.
sourcepub fn closest<T, 'a>(
&'a mut self,
target: &'a T
) -> impl Iterator<Item = EntryView<TKey, TVal>> + 'awhere
T: Clone + AsRef<KeyBytes>,
TVal: Clone,
pub fn closest<T, 'a>( &'a mut self, target: &'a T ) -> impl Iterator<Item = EntryView<TKey, TVal>> + 'awhere T: Clone + AsRef<KeyBytes>, TVal: Clone,
Returns an iterator over the nodes closest to the target
key, ordered by
increasing distance.
sourcepub fn count_nodes_between<T>(&mut self, target: &T) -> usizewhere
T: AsRef<KeyBytes>,
pub fn count_nodes_between<T>(&mut self, target: &T) -> usizewhere T: AsRef<KeyBytes>,
Counts the number of nodes between the local node and the node
closest to target
.
The number of nodes between the local node and the target are calculated by backtracking from the target towards the local key.