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

View File

@ -8,7 +8,7 @@ pub struct Bucket {
}
pub(super) type EntriesIterMut<'a> =
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 {
match state {
@ -21,7 +21,7 @@ fn state_ordering(state: BucketEntryState) -> usize {
impl Bucket {
pub fn new(routing_table: RoutingTable) -> Self {
Self {
routing_table: routing_table,
routing_table,
entries: BTreeMap::new(),
newest_entry: None,
}
@ -61,9 +61,9 @@ impl Bucket {
self.entries.get_mut(key)
}
pub(super) fn entries(&self) -> EntriesIter {
self.entries.iter()
}
// pub(super) fn entries(&self) -> EntriesIter {
// self.entries.iter()
// }
pub(super) fn entries_mut(&mut self) -> EntriesIterMut {
self.entries.iter_mut()
@ -103,28 +103,28 @@ impl Bucket {
);
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 extra_entries == 0 {
// The first 'live' entry we find is our newest entry
if self.newest_entry.is_none() {
self.newest_entry = Some(sorted_entries[i].0.clone());
self.newest_entry = Some(*entry.0);
}
break;
}
extra_entries -= 1;
// 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
if self.newest_entry.is_none() {
self.newest_entry = Some(sorted_entries[i].0.clone());
self.newest_entry = Some(*entry.0);
}
continue;
}
// 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
@ -133,7 +133,7 @@ impl Bucket {
self.remove_entry(id);
}
if dead_node_ids.len() > 0 {
if !dead_node_ids.is_empty() {
Some(dead_node_ids)
} else {
None

View File

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

View File

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

View File

@ -5,6 +5,8 @@ use crate::intf::*;
use crate::xx::*;
use crate::*;
pub type FilterType = Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>;
impl RoutingTable {
// 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
@ -109,7 +111,7 @@ impl RoutingTable {
let mut nodes =
Vec::<(&DHTKey, Option<&mut BucketEntry>)>::with_capacity(inner.bucket_entry_count + 1);
// 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);
if filter(&selfkv) {
nodes.push(selfkv);
@ -134,19 +136,15 @@ impl RoutingTable {
// return transformed vector for filtered+sorted nodes
let cnt = usize::min(node_count, nodes.len());
let mut out = Vec::<O>::with_capacity(cnt);
for i in 0..cnt {
let val = transform(&mut nodes[i]);
for mut node in nodes {
let val = transform(&mut node);
out.push(val);
}
out
}
pub fn find_fastest_nodes<T, O>(
&self,
filter: Option<Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>>,
transform: T,
) -> Vec<O>
pub fn find_fastest_nodes<T, O>(&self, filter: Option<FilterType>, transform: T) -> Vec<O>
where
T: Fn(&mut (&DHTKey, Option<&mut BucketEntry>)) -> O,
{
@ -162,12 +160,12 @@ impl RoutingTable {
|kv| {
if kv.1.is_none() {
// filter out self peer, as it is irrelevant to the 'fastest nodes' search
false
} else if filter.is_some() && !filter.as_ref().unwrap()(kv) {
false
} else {
true
return false;
}
if filter.is_some() && !filter.as_ref().unwrap()(kv) {
return false;
}
true
},
// sort
|(a_key, a_entry), (b_key, b_entry)| {
@ -224,7 +222,7 @@ impl RoutingTable {
pub fn find_closest_nodes<T, O>(
&self,
node_id: DHTKey,
filter: Option<Box<dyn Fn(&(&DHTKey, Option<&mut BucketEntry>)) -> bool>>,
filter: Option<FilterType>,
transform: T,
) -> Vec<O>
where
@ -242,12 +240,12 @@ impl RoutingTable {
|kv| {
if kv.1.is_none() {
// include self peer, as it is relevant to the 'closest nodes' search
true
} else if filter.is_some() && !filter.as_ref().unwrap()(kv) {
false
} else {
true
return true;
}
if filter.is_some() && !filter.as_ref().unwrap()(kv) {
return false;
}
true
},
// sort
|(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 {
entry.ref_count += 1;
Self {
routing_table: routing_table,
routing_table,
node_id: key,
protocol_address_type: None,
}
@ -25,7 +25,7 @@ impl NodeRef {
) -> Self {
entry.ref_count += 1;
Self {
routing_table: routing_table,
routing_table,
node_id: key,
protocol_address_type: Some(protocol_address_type),
}
@ -86,7 +86,7 @@ impl Clone for NodeRef {
});
Self {
routing_table: self.routing_table.clone(),
node_id: self.node_id.clone(),
node_id: self.node_id,
protocol_address_type: self.protocol_address_type,
}
}