diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 8b89673..d05e8e9 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -44,6 +44,12 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bimap" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50ae17cabbc8a38a1e3e4c1a6a664e9a09672dc14d0896fa8d865d3a5a446b07" + [[package]] name = "bincode" version = "1.3.3" @@ -157,6 +163,7 @@ dependencies = [ name = "common" version = "0.1.0" dependencies = [ + "bimap", "bincode", "bytes", "fnv", @@ -1187,6 +1194,7 @@ name = "telemetry" version = "0.1.0" dependencies = [ "anyhow", + "bimap", "bincode", "common", "futures", diff --git a/backend/common/Cargo.toml b/backend/common/Cargo.toml index e873306..391cbbd 100644 --- a/backend/common/Cargo.toml +++ b/backend/common/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Parity Technologies Ltd. "] edition = "2018" [dependencies] +bimap = "0.6.1" bytes = "1.0.1" fnv = "1.0.7" hex = "0.4.3" diff --git a/backend/common/src/assign_id.rs b/backend/common/src/assign_id.rs index a85db06..9f4e0a7 100644 --- a/backend/common/src/assign_id.rs +++ b/backend/common/src/assign_id.rs @@ -1,5 +1,6 @@ -use std::{collections::HashMap, hash::Hash}; +use std::hash::Hash; use serde::{Serialize,Deserialize}; +use bimap::BiMap; #[derive(Clone,Copy,Debug,Hash,PartialEq,Eq,Serialize,Deserialize)] pub struct Id(usize); @@ -9,6 +10,11 @@ impl std::convert::From for usize { id.0 } } +impl std::convert::From for Id { + fn from(n: usize) -> Id { + Id(n) + } +} /// A struct that allows you to assign ID to an arbitrary set of /// details (so long as they are Eq+Hash+Clone), and then access @@ -17,60 +23,45 @@ impl std::convert::From for usize { #[derive(Debug)] pub struct AssignId
{ current_id: Id, - from_details: HashMap, - from_id: HashMap + mapping: BiMap } -impl
AssignId
where Details: Eq + Hash + Clone { +impl
AssignId
where Details: Eq + Hash { pub fn new() -> Self { Self { current_id: Id(0), - from_details: HashMap::new(), - from_id: HashMap::new() + mapping: BiMap::new() } } pub fn assign_id(&mut self, details: Details) -> Id { let this_id = self.current_id; self.current_id.0 += 1; - - self.from_details.insert(details.clone(), this_id); - self.from_id.insert(this_id, details); - + self.mapping.insert(this_id, details); this_id } pub fn get_details(&mut self, id: Id) -> Option<&Details> { - self.from_id.get(&id) + self.mapping.get_by_left(&id) } pub fn get_id(&mut self, details: &Details) -> Option { - self.from_details.get(details).map(|id| *id) + self.mapping.get_by_right(details).map(|id| *id) } pub fn remove_by_id(&mut self, id: Id) -> Option
{ - if let Some(details) = self.from_id.remove(&id) { - self.from_details.remove(&details); - Some(details) - } else { - None - } + self.mapping.remove_by_left(&id).map(|(_,details)| details) } pub fn remove_by_details(&mut self, details: &Details) -> Option { - if let Some(id) = self.from_details.remove(&details) { - self.from_id.remove(&id); - Some(id) - } else { - None - } + self.mapping.remove_by_right(&details).map(|(id,_)| id) } pub fn clear(&mut self) { - *self = AssignId::new() + *self = AssignId::new(); } pub fn iter(&self) -> impl Iterator { - self.from_id.iter().map(|(id, details)| (*id, details)) + self.mapping.iter().map(|(id, details)| (*id, details)) } } \ No newline at end of file diff --git a/backend/telemetry/Cargo.toml b/backend/telemetry/Cargo.toml index 0f29d50..bfe352a 100644 --- a/backend/telemetry/Cargo.toml +++ b/backend/telemetry/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] anyhow = "1.0.41" +bimap = "0.6.1" bincode = "1.3.3" common = { path = "../common" } futures = "0.3.15" diff --git a/backend/telemetry/src/aggregator.rs b/backend/telemetry/src/aggregator.rs index a9b1828..8b1e227 100644 --- a/backend/telemetry/src/aggregator.rs +++ b/backend/telemetry/src/aggregator.rs @@ -1,9 +1,9 @@ use common::{ internal_messages::{GlobalId, LocalId}, node, - assign_id::AssignId, util::now }; +use bimap::BiMap; use std::{str::FromStr, sync::Arc}; use std::sync::atomic::AtomicU64; use futures::channel::{ mpsc, oneshot }; @@ -155,7 +155,7 @@ impl Aggregator { // Maintain mappings from the shard connection ID and local ID of messages to a global ID // that uniquely identifies nodes in our node state. - let mut to_global_node_id = AssignId::new(); + let mut global_ids: BiMap = BiMap::new(); // Keep track of channels to communicate with feeds and shards: let mut feed_channels = HashMap::new(); @@ -294,13 +294,13 @@ impl Aggregator { // TODO: node_state.add_node. Every feed should know about node count changes. }, ToAggregator::FromShardWebsocket(shard_conn_id, FromShardWebsocket::Remove { local_id }) => { - if let Some(id) = to_global_node_id.remove_by_details(&(shard_conn_id, local_id)) { + if let Some(id) = global_ids.remove_by_right(&(shard_conn_id, local_id)) { // TODO: node_state.remove_node, Every feed should know about node count changes. } }, ToAggregator::FromShardWebsocket(shard_conn_id, FromShardWebsocket::Update { local_id, payload }) => { // TODO: Fill this all in... - let global_node_id = match to_global_node_id.get_id(&(shard_conn_id, local_id)) { + let global_node_id = match global_ids.get_by_right(&(shard_conn_id, local_id)) { Some(id) => id, None => continue };