pub struct KBucketsTable<TNodeId, TVal: Eq> { /* private fields */ }
Expand description
A KBucketsTable
represents a Kademlia routing table.
Implementations§
Source§impl<TNodeId, TVal> KBucketsTable<TNodeId, TVal>
impl<TNodeId, TVal> KBucketsTable<TNodeId, TVal>
Sourcepub fn new(
local_key: Key<TNodeId>,
pending_timeout: Duration,
max_incoming_per_bucket: usize,
table_filter: Option<Box<dyn Filter<TVal>>>,
bucket_filter: Option<Box<dyn Filter<TVal>>>,
) -> Self
pub fn new( local_key: Key<TNodeId>, pending_timeout: Duration, max_incoming_per_bucket: usize, table_filter: Option<Box<dyn Filter<TVal>>>, bucket_filter: Option<Box<dyn Filter<TVal>>>, ) -> Self
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.
A filter can be applied that limits entries into a bucket based on the buckets contents. Entries that fail the filter, will not be inserted.
pub fn update_node_status( &mut self, key: &Key<TNodeId>, state: ConnectionState, direction: Option<ConnectionDirection>, ) -> UpdateResult
Sourcepub fn update_node(
&mut self,
key: &Key<TNodeId>,
value: TVal,
state: Option<ConnectionState>,
) -> UpdateResult
pub fn update_node( &mut self, key: &Key<TNodeId>, value: TVal, state: Option<ConnectionState>, ) -> UpdateResult
Updates a node’s value if it exists in the table.
Optionally the connection state can be modified.
pub fn insert_or_update( &mut self, key: &Key<TNodeId>, value: TVal, status: NodeStatus, ) -> InsertResult<TNodeId>
Sourcepub fn remove(&mut self, key: &Key<TNodeId>) -> bool
pub fn remove(&mut self, key: &Key<TNodeId>) -> bool
Removes a node from the routing table. Returns true
of the node existed.
Sourcepub fn entry<'a>(
&'a mut self,
key: &'a Key<TNodeId>,
) -> Entry<'a, TNodeId, TVal>
pub fn entry<'a>( &'a mut self, key: &'a Key<TNodeId>, ) -> Entry<'a, TNodeId, TVal>
Returns an Entry
for the given key, representing the state of the entry
in the routing table.
NOTE: This must be used with caution. Modifying values manually can bypass the internal
table filters and ingoing/outgoing limits.
Sourcepub fn iter(&mut self) -> impl Iterator<Item = EntryRefView<'_, TNodeId, TVal>>
pub fn iter(&mut self) -> impl Iterator<Item = EntryRefView<'_, TNodeId, TVal>>
Returns an iterator over all the entries in the routing table.
Sourcepub fn buckets_iter(&self) -> impl Iterator<Item = &KBucket<TNodeId, TVal>>
pub fn buckets_iter(&self) -> impl Iterator<Item = &KBucket<TNodeId, TVal>>
Returns an iterator over all the buckets in the routing table
Sourcepub fn iter_ref(&self) -> impl Iterator<Item = EntryRefView<'_, TNodeId, TVal>>
pub fn iter_ref(&self) -> impl Iterator<Item = EntryRefView<'_, TNodeId, TVal>>
Returns an iterator over all the entries in the routing table. Does not add pending node to kbucket to get an iterator which takes a mutable reference instead of a reference.
Sourcepub fn take_applied_pending(&mut self) -> Option<AppliedPending<TNodeId, TVal>>
pub fn take_applied_pending(&mut self) -> Option<AppliedPending<TNodeId, 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 nodes_by_distances(
&mut self,
log2_distances: &[u64],
max_nodes: usize,
) -> Vec<EntryRefView<'_, TNodeId, TVal>>
pub fn nodes_by_distances( &mut self, log2_distances: &[u64], max_nodes: usize, ) -> Vec<EntryRefView<'_, TNodeId, TVal>>
Returns an iterator over the keys that are contained in a kbucket, specified by a log2 distance.
Sourcepub fn closest_keys<'a, T>(
&'a mut self,
target: &'a Key<T>,
) -> impl Iterator<Item = Key<TNodeId>> + 'awhere
T: Clone,
pub fn closest_keys<'a, T>(
&'a mut self,
target: &'a Key<T>,
) -> impl Iterator<Item = Key<TNodeId>> + 'awhere
T: Clone,
Returns an iterator over the keys closest to target
, ordered by
increasing distance.
Sourcepub fn closest_values<'a, T>(
&'a mut self,
target: &'a Key<T>,
) -> impl Iterator<Item = ClosestValue<TNodeId, TVal>> + 'a
pub fn closest_values<'a, T>( &'a mut self, target: &'a Key<T>, ) -> impl Iterator<Item = ClosestValue<TNodeId, TVal>> + 'a
Returns an iterator over the keys closest to target
, ordered by
increasing distance.
Sourcepub fn closest_values_predicate<'a, T, F>(
&'a mut self,
target: &'a Key<T>,
predicate: F,
) -> impl Iterator<Item = PredicateValue<TNodeId, TVal>> + 'a
pub fn closest_values_predicate<'a, T, F>( &'a mut self, target: &'a Key<T>, predicate: F, ) -> impl Iterator<Item = PredicateValue<TNodeId, TVal>> + 'a
Returns an iterator over the keys closest to target
, ordered by
increasing distance specifying which keys agree with a value predicate.
Sourcepub fn get_bucket(&self, key: &Key<TNodeId>) -> Option<&KBucket<TNodeId, TVal>>
pub fn get_bucket(&self, key: &Key<TNodeId>) -> Option<&KBucket<TNodeId, TVal>>
Returns a reference to a bucket given the key. Returns None if bucket does not exist.