mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-26 16:21:39 +00:00
Merge branch 'weighted_bucket' into weighted_tests
# Conflicts: # protocols/kad/src/kbucket/weighted.rs
This commit is contained in:
@ -14,8 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::K_VALUE;
|
|
||||||
use arrayvec::ArrayVec;
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
enum ChangePosition {
|
enum ChangePosition {
|
||||||
@ -93,15 +91,17 @@ pub struct Position(pub usize);
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SubBucket<Node> {
|
pub struct SubBucket<Node> {
|
||||||
pub nodes: ArrayVec<[Node; K_VALUE.get()]>,
|
pub nodes: Vec<Node>,
|
||||||
pub first_connected_pos: Option<usize>,
|
pub first_connected_pos: Option<usize>,
|
||||||
|
pub capacity: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Node> SubBucket<Node> {
|
impl<Node> SubBucket<Node> {
|
||||||
pub fn new() -> Self {
|
pub fn new(capacity: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
nodes: ArrayVec::new(),
|
nodes: Vec::with_capacity(capacity + 1),
|
||||||
first_connected_pos: None,
|
first_connected_pos: None,
|
||||||
|
capacity,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ impl<Node> SubBucket<Node> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_full(&self) -> bool {
|
pub fn is_full(&self) -> bool {
|
||||||
self.nodes.is_full()
|
self.nodes.len() >= self.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all_nodes_connected(&self) -> bool {
|
pub fn all_nodes_connected(&self) -> bool {
|
||||||
@ -181,7 +181,16 @@ impl<Node> SubBucket<Node> {
|
|||||||
self.change_connected_pos(ChangePosition::RemoveDisconnected)
|
self.change_connected_pos(ChangePosition::RemoveDisconnected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.nodes.pop_at(position.0)
|
if position.0 >= self.nodes.len() {
|
||||||
|
println!(
|
||||||
|
"WARNING: tried to evict node at {} while there's only {} nodes",
|
||||||
|
position.0,
|
||||||
|
self.nodes.len()
|
||||||
|
);
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self.nodes.remove(position.0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop_node(&mut self) -> Option<Node> {
|
pub fn pop_node(&mut self) -> Option<Node> {
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
use crate::kbucket::{
|
use crate::kbucket::{
|
||||||
AppliedPending, InsertResult, KeyBytes, Node, NodeStatus, PendingNode, SubBucket,
|
AppliedPending, InsertResult, KeyBytes, Node, NodeStatus, PendingNode, SubBucket,
|
||||||
};
|
};
|
||||||
|
use crate::K_VALUE;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -33,7 +34,7 @@ where
|
|||||||
{
|
{
|
||||||
pub fn new(pending_timeout: Duration) -> Self {
|
pub fn new(pending_timeout: Duration) -> Self {
|
||||||
Self {
|
Self {
|
||||||
bucket: SubBucket::new(),
|
bucket: SubBucket::new(K_VALUE.get()),
|
||||||
pending: None,
|
pending: None,
|
||||||
pending_timeout,
|
pending_timeout,
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,9 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_bucket_mut(&mut self, weight: u32) -> &mut SubBucket<WeightedNode<TKey, TVal>> {
|
fn get_bucket_mut(&mut self, weight: u32) -> &mut SubBucket<WeightedNode<TKey, TVal>> {
|
||||||
self.map.entry(weight).or_insert(SubBucket::new())
|
self.map
|
||||||
|
.entry(weight)
|
||||||
|
.or_insert(SubBucket::new(W_VALUE.get()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append_connected_node(&mut self, node: WeightedNode<TKey, TVal>) {
|
fn append_connected_node(&mut self, node: WeightedNode<TKey, TVal>) {
|
||||||
|
Reference in New Issue
Block a user