heliwatch: don't Arc unique data

This commit is contained in:
Astro 2024-04-14 22:16:57 +02:00
parent e1d6c0ecb2
commit 9172dc5abd
1 changed files with 15 additions and 12 deletions

View File

@ -7,7 +7,7 @@ use serde::Deserialize;
#[derive(Debug)]
pub struct Aircraft {
pub owner: Option<Arc<String>>,
pub registration: Option<Arc<String>>,
pub registration: Option<String>,
pub model: Option<Arc<String>>,
}
@ -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,