From 9172dc5abd036707d5b5a21bcff5c61f6e55fde1 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 14 Apr 2024 22:16:57 +0200 Subject: [PATCH] heliwatch: don't Arc unique data --- heliwatch/src/aircrafts.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/heliwatch/src/aircrafts.rs b/heliwatch/src/aircrafts.rs index e8e2011..4ebb1e2 100644 --- a/heliwatch/src/aircrafts.rs +++ b/heliwatch/src/aircrafts.rs @@ -7,7 +7,7 @@ use serde::Deserialize; #[derive(Debug)] pub struct Aircraft { pub owner: Option>, - pub registration: Option>, + pub registration: Option, pub model: Option>, } @@ -26,35 +26,38 @@ pub struct Aircrafts { impl Aircrafts { pub fn load(file: &str) -> Self { - let mut strings = HashMap::new(); - let mut nullable = move |s: String| { - if s.len() > 0 { - let e = strings.entry(s.clone()) - .or_insert_with(|| Arc::new(s)); - Some(e.clone()) - } else { + let nullable = |s: String| { + if s == "" { None + } else { + Some(s) } }; + let mut stringtable = HashMap::new(); + let mut stringtabled = |s: String| { + let e = stringtable.entry(s.clone()) + .or_insert_with(|| Arc::new(s)); + e.clone() + }; let mut data = HashMap::new(); let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap())); for result in rdr.deserialize() { let rec: Record = result.unwrap(); if rec.registration != "" { data.insert(rec.icao24, Aircraft { - owner: nullable(rec.owner), + owner: nullable(rec.owner).map(&mut stringtabled), registration: nullable(rec.registration), model: if rec.manufacturername == "" && rec.model == "" { None } else if rec.model.starts_with(&rec.manufacturername) { - nullable(rec.model) + nullable(rec.model).map(&mut stringtabled) } else { - nullable(format!("{} {}", rec.manufacturername, rec.model)) + Some(stringtabled(format!("{} {}", rec.manufacturername, rec.model))) }, }); } } - println!("Loaded aircraft database with {} entries", data.len()); + println!("Loaded {} aircrafts ({} entries, {} strings)", data.len(), data.len(), stringtable.len()); Aircrafts { data,