Lots more refactoring, finish add node (and almost the location updating)

This commit is contained in:
James Wilson
2021-06-23 18:03:11 +01:00
parent 2db2677217
commit 47c12ce210
16 changed files with 1297 additions and 459 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ impl std::convert::From<usize> for Id {
}
}
/// A struct that allows you to assign ID to an arbitrary set of
/// A struct that allows you to assign an ID to an arbitrary set of
/// details (so long as they are Eq+Hash+Clone), and then access
/// the assigned ID given those details or access the details given
/// the ID.
+9 -4
View File
@@ -9,9 +9,6 @@ use serde::{Deserialize, Serialize};
/// might send data on behalf of more than one chain.
pub type LocalId = Id;
/// A global ID assigned to messages from each different pair of ConnId+LocalId.
pub type GlobalId = usize;
/// Message sent from the shard to the backend core
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum FromShardAggregator {
@@ -37,6 +34,14 @@ pub enum FromShardAggregator {
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum FromTelemetryCore {
Mute {
local_id: LocalId
local_id: LocalId,
reason: MuteReason
}
}
/// Why is the thing being muted?
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum MuteReason {
Overquota,
ChainNotAllowed
}
+2 -1
View File
@@ -4,4 +4,5 @@ pub mod types;
pub mod util;
pub mod json;
pub mod log_level;
pub mod assign_id;
pub mod assign_id;
pub mod most_seen;
+109
View File
@@ -0,0 +1,109 @@
use std::collections::HashMap;
use std::hash::Hash;
/// Add items to this, and it will keep track of what the item
/// seen the most is.
#[derive(Debug)]
pub struct MostSeen<T> {
current_best: T,
current_count: usize,
others: HashMap<T, usize>
}
impl <T: Hash + Eq> MostSeen<T> {
pub fn new(item: T) -> Self {
Self {
current_best: item,
current_count: 1,
others: HashMap::new()
}
}
pub fn best(&self) -> &T {
&self.current_best
}
}
impl <T: Hash + Eq + Clone> MostSeen<T> {
pub fn insert(&mut self, item: &T) -> ChangeResult {
if &self.current_best == item {
// Item already the best one; bump count.
self.current_count += 1;
return ChangeResult::NoChange;
}
// Item not the best; increment count in map
let item_count = self.others.entry(item.clone()).or_default();
*item_count += 1;
// Is item now the best?
if *item_count > self.current_count {
let (item, count) = self.others
.remove_entry(item)
.expect("item added above");
self.current_best = item;
self.current_count = count;
ChangeResult::NewMostSeenItem
} else {
ChangeResult::NoChange
}
}
pub fn remove(&mut self, item: &T) -> ChangeResult {
if &self.current_best == item {
// Item already the best one; reduce count
self.current_count -= 1;
// Is there a new best?
let other_best = self.others
.iter()
.max_by_key(|f| f.1);
let (other_item, &other_count) = match other_best {
Some(item) => item,
None => { return ChangeResult::NoChange }
};
if other_count > self.current_count {
// Clone item to unborrow self.others so that we can remove
// the item from it. We could pre-emptively remove and reinsert
// instead, but most of the time there is no change, so I'm
// aiming to keep that path cheaper.
let other_item = other_item.clone();
let (other_item, other_count) = self.others
.remove_entry(&other_item)
.expect("item returned above, so def exists");
self.current_best = other_item;
self.current_count = other_count;
return ChangeResult::NewMostSeenItem;
} else {
return ChangeResult::NoChange;
}
}
// Item is in the map; not the best anyway. decrement count.
if let Some(count) = self.others.get_mut(item) {
*count += 1;
}
ChangeResult::NoChange
}
}
/// Record the result of adding/removing an entry
#[derive(Clone,Copy)]
pub enum ChangeResult {
/// The best item has remained the same.
NoChange,
/// There is a new best item now.
NewMostSeenItem
}
impl ChangeResult {
pub fn has_changed(self) -> bool {
match self {
ChangeResult::NewMostSeenItem => true,
ChangeResult::NoChange => false
}
}
}