This commit is contained in:
John Smith 2021-11-26 10:39:43 -05:00
parent a80178da54
commit 303a7aec29
6 changed files with 40 additions and 49 deletions

View File

@ -1,7 +1,6 @@
use crate::intf::*; use crate::intf::*;
use crate::xx::*; use crate::xx::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_cbor;
cfg_if! { cfg_if! {
if #[cfg(target_arch = "wasm32")] { if #[cfg(target_arch = "wasm32")] {
@ -32,20 +31,17 @@ impl TableDB {
pub fn new(table: String, table_store: TableStore, database: Database) -> Self { pub fn new(table: String, table_store: TableStore, database: Database) -> Self {
Self { Self {
inner: Arc::new(Mutex::new(TableDBInner { inner: Arc::new(Mutex::new(TableDBInner {
table: table, table,
table_store: table_store.clone(), table_store: table_store.clone(),
database: database, database,
})), })),
} }
} }
pub fn try_new_from_weak_inner(weak_inner: Weak<Mutex<TableDBInner>>) -> Option<Self> { pub fn try_new_from_weak_inner(weak_inner: Weak<Mutex<TableDBInner>>) -> Option<Self> {
match weak_inner.upgrade() { weak_inner.upgrade().map(|table_db_inner| Self {
Some(table_db_inner) => Some(Self {
inner: table_db_inner, inner: table_db_inner,
}), })
None => None,
}
} }
pub fn weak_inner(&self) -> Weak<Mutex<TableDBInner>> { pub fn weak_inner(&self) -> Weak<Mutex<TableDBInner>> {

View File

@ -8,7 +8,7 @@ pub struct Bucket {
} }
pub(super) type EntriesIterMut<'a> = pub(super) type EntriesIterMut<'a> =
alloc::collections::btree_map::IterMut<'a, DHTKey, BucketEntry>; alloc::collections::btree_map::IterMut<'a, DHTKey, BucketEntry>;
pub(super) type EntriesIter<'a> = alloc::collections::btree_map::Iter<'a, DHTKey, BucketEntry>; //pub(super) type EntriesIter<'a> = alloc::collections::btree_map::Iter<'a, DHTKey, BucketEntry>;
fn state_ordering(state: BucketEntryState) -> usize { fn state_ordering(state: BucketEntryState) -> usize {
match state { match state {
@ -21,7 +21,7 @@ fn state_ordering(state: BucketEntryState) -> usize {
impl Bucket { impl Bucket {
pub fn new(routing_table: RoutingTable) -> Self { pub fn new(routing_table: RoutingTable) -> Self {
Self { Self {
routing_table: routing_table, routing_table,
entries: BTreeMap::new(), entries: BTreeMap::new(),
newest_entry: None, newest_entry: None,
} }
@ -61,9 +61,9 @@ impl Bucket {
self.entries.get_mut(key) self.entries.get_mut(key)
} }
pub(super) fn entries(&self) -> EntriesIter { // pub(super) fn entries(&self) -> EntriesIter {
self.entries.iter() // self.entries.iter()
} // }
pub(super) fn entries_mut(&mut self) -> EntriesIterMut { pub(super) fn entries_mut(&mut self) -> EntriesIterMut {
self.entries.iter_mut() self.entries.iter_mut()
@ -103,28 +103,28 @@ impl Bucket {
); );
self.newest_entry = None; self.newest_entry = None;
for i in 0..sorted_entries.len() { for entry in sorted_entries {
// If we're not evicting more entries, exit, noting this may be the newest entry // If we're not evicting more entries, exit, noting this may be the newest entry
if extra_entries == 0 { if extra_entries == 0 {
// The first 'live' entry we find is our newest entry // The first 'live' entry we find is our newest entry
if self.newest_entry.is_none() { if self.newest_entry.is_none() {
self.newest_entry = Some(sorted_entries[i].0.clone()); self.newest_entry = Some(*entry.0);
} }
break; break;
} }
extra_entries -= 1; extra_entries -= 1;
// if this entry has references we can't drop it yet // if this entry has references we can't drop it yet
if sorted_entries[i].1.ref_count > 0 { if entry.1.ref_count > 0 {
// The first 'live' entry we fine is our newest entry // The first 'live' entry we fine is our newest entry
if self.newest_entry.is_none() { if self.newest_entry.is_none() {
self.newest_entry = Some(sorted_entries[i].0.clone()); self.newest_entry = Some(*entry.0);
} }
continue; continue;
} }
// if no references, lets evict it // if no references, lets evict it
dead_node_ids.insert(sorted_entries[i].0.clone()); dead_node_ids.insert(*entry.0);
} }
// Now purge the dead node ids // Now purge the dead node ids
@ -133,7 +133,7 @@ impl Bucket {
self.remove_entry(id); self.remove_entry(id);
} }
if dead_node_ids.len() > 0 { if !dead_node_ids.is_empty() {
Some(dead_node_ids) Some(dead_node_ids)
} else { } else {
None None

View File

@ -186,8 +186,6 @@ impl BucketEntry {
die.dial_info(), die.dial_info(),
&self.last_connection.as_ref().unwrap().0.remote, &self.last_connection.as_ref().unwrap().0.remote,
) { ) {
drop(die);
// push the most recent dialinfo to the front // push the most recent dialinfo to the front
let dies = &mut self.dial_info_entries; let dies = &mut self.dial_info_entries;
let die = dies.remove(i).unwrap(); let die = dies.remove(i).unwrap();

View File

@ -8,12 +8,11 @@ pub struct DialInfoEntry {
impl DialInfoEntry { impl DialInfoEntry {
pub fn try_new(dial_info: DialInfo) -> Result<Self, String> { pub fn try_new(dial_info: DialInfo) -> Result<Self, String> {
let addr = match dial_info.resolve() { let addr = dial_info
Ok(a) => a, .resolve()
Err(_) => return Err("failed to resolve address".to_owned()), .map_err(|e| format!("failed to resolve address: {:?}", e))?;
};
Ok(Self { Ok(Self {
dial_info: dial_info, dial_info,
resolved_address: addr, resolved_address: addr,
}) })
} }

View File

@ -5,6 +5,8 @@ use crate::intf::*;
use crate::xx::*; use crate::xx::*;
use crate::*; use crate::*;
pub type FilterType = Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>;
impl RoutingTable { impl RoutingTable {
// Retrieve the fastest nodes in the routing table with a particular kind of protocol address type // Retrieve the fastest nodes in the routing table with a particular kind of protocol address type
// Returns noderefs are are scoped to that address type only // Returns noderefs are are scoped to that address type only
@ -109,7 +111,7 @@ impl RoutingTable {
let mut nodes = let mut nodes =
Vec::<(&DHTKey, Option<&mut BucketEntry>)>::with_capacity(inner.bucket_entry_count + 1); Vec::<(&DHTKey, Option<&mut BucketEntry>)>::with_capacity(inner.bucket_entry_count + 1);
// add our own node (only one of there with the None entry) // add our own node (only one of there with the None entry)
let self_node_id = inner.node_id.clone(); let self_node_id = inner.node_id;
let selfkv = (&self_node_id, None); let selfkv = (&self_node_id, None);
if filter(&selfkv) { if filter(&selfkv) {
nodes.push(selfkv); nodes.push(selfkv);
@ -134,19 +136,15 @@ impl RoutingTable {
// return transformed vector for filtered+sorted nodes // return transformed vector for filtered+sorted nodes
let cnt = usize::min(node_count, nodes.len()); let cnt = usize::min(node_count, nodes.len());
let mut out = Vec::<O>::with_capacity(cnt); let mut out = Vec::<O>::with_capacity(cnt);
for i in 0..cnt { for mut node in nodes {
let val = transform(&mut nodes[i]); let val = transform(&mut node);
out.push(val); out.push(val);
} }
out out
} }
pub fn find_fastest_nodes<T, O>( pub fn find_fastest_nodes<T, O>(&self, filter: Option<FilterType>, transform: T) -> Vec<O>
&self,
filter: Option<Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>>,
transform: T,
) -> Vec<O>
where where
T: Fn(&mut (&DHTKey, Option<&mut BucketEntry>)) -> O, T: Fn(&mut (&DHTKey, Option<&mut BucketEntry>)) -> O,
{ {
@ -162,12 +160,12 @@ impl RoutingTable {
|kv| { |kv| {
if kv.1.is_none() { if kv.1.is_none() {
// filter out self peer, as it is irrelevant to the 'fastest nodes' search // filter out self peer, as it is irrelevant to the 'fastest nodes' search
false return false;
} else if filter.is_some() && !filter.as_ref().unwrap()(kv) {
false
} else {
true
} }
if filter.is_some() && !filter.as_ref().unwrap()(kv) {
return false;
}
true
}, },
// sort // sort
|(a_key, a_entry), (b_key, b_entry)| { |(a_key, a_entry), (b_key, b_entry)| {
@ -224,7 +222,7 @@ impl RoutingTable {
pub fn find_closest_nodes<T, O>( pub fn find_closest_nodes<T, O>(
&self, &self,
node_id: DHTKey, node_id: DHTKey,
filter: Option<Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>>, filter: Option<FilterType>,
transform: T, transform: T,
) -> Vec<O> ) -> Vec<O>
where where
@ -242,12 +240,12 @@ impl RoutingTable {
|kv| { |kv| {
if kv.1.is_none() { if kv.1.is_none() {
// include self peer, as it is relevant to the 'closest nodes' search // include self peer, as it is relevant to the 'closest nodes' search
true return true;
} else if filter.is_some() && !filter.as_ref().unwrap()(kv) {
false
} else {
true
} }
if filter.is_some() && !filter.as_ref().unwrap()(kv) {
return false;
}
true
}, },
// sort // sort
|(a_key, a_entry), (b_key, b_entry)| { |(a_key, a_entry), (b_key, b_entry)| {

View File

@ -12,7 +12,7 @@ impl NodeRef {
pub fn new(routing_table: RoutingTable, key: DHTKey, entry: &mut BucketEntry) -> Self { pub fn new(routing_table: RoutingTable, key: DHTKey, entry: &mut BucketEntry) -> Self {
entry.ref_count += 1; entry.ref_count += 1;
Self { Self {
routing_table: routing_table, routing_table,
node_id: key, node_id: key,
protocol_address_type: None, protocol_address_type: None,
} }
@ -25,7 +25,7 @@ impl NodeRef {
) -> Self { ) -> Self {
entry.ref_count += 1; entry.ref_count += 1;
Self { Self {
routing_table: routing_table, routing_table,
node_id: key, node_id: key,
protocol_address_type: Some(protocol_address_type), protocol_address_type: Some(protocol_address_type),
} }
@ -86,7 +86,7 @@ impl Clone for NodeRef {
}); });
Self { Self {
routing_table: self.routing_table.clone(), routing_table: self.routing_table.clone(),
node_id: self.node_id.clone(), node_id: self.node_id,
protocol_address_type: self.protocol_address_type, protocol_address_type: self.protocol_address_type,
} }
} }