heliwatch: don't Arc unique data
This commit is contained in:
parent
e1d6c0ecb2
commit
9172dc5abd
|
@ -7,7 +7,7 @@ use serde::Deserialize;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Aircraft {
|
pub struct Aircraft {
|
||||||
pub owner: Option<Arc<String>>,
|
pub owner: Option<Arc<String>>,
|
||||||
pub registration: Option<Arc<String>>,
|
pub registration: Option<String>,
|
||||||
pub model: Option<Arc<String>>,
|
pub model: Option<Arc<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,35 +26,38 @@ pub struct Aircrafts {
|
||||||
|
|
||||||
impl Aircrafts {
|
impl Aircrafts {
|
||||||
pub fn load(file: &str) -> Self {
|
pub fn load(file: &str) -> Self {
|
||||||
let mut strings = HashMap::new();
|
let nullable = |s: String| {
|
||||||
let mut nullable = move |s: String| {
|
if s == "" {
|
||||||
if s.len() > 0 {
|
|
||||||
let e = strings.entry(s.clone())
|
|
||||||
.or_insert_with(|| Arc::new(s));
|
|
||||||
Some(e.clone())
|
|
||||||
} else {
|
|
||||||
None
|
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 data = HashMap::new();
|
||||||
let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap()));
|
let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap()));
|
||||||
for result in rdr.deserialize() {
|
for result in rdr.deserialize() {
|
||||||
let rec: Record = result.unwrap();
|
let rec: Record = result.unwrap();
|
||||||
if rec.registration != "" {
|
if rec.registration != "" {
|
||||||
data.insert(rec.icao24, Aircraft {
|
data.insert(rec.icao24, Aircraft {
|
||||||
owner: nullable(rec.owner),
|
owner: nullable(rec.owner).map(&mut stringtabled),
|
||||||
registration: nullable(rec.registration),
|
registration: nullable(rec.registration),
|
||||||
model: if rec.manufacturername == "" && rec.model == "" {
|
model: if rec.manufacturername == "" && rec.model == "" {
|
||||||
None
|
None
|
||||||
} else if rec.model.starts_with(&rec.manufacturername) {
|
} else if rec.model.starts_with(&rec.manufacturername) {
|
||||||
nullable(rec.model)
|
nullable(rec.model).map(&mut stringtabled)
|
||||||
} else {
|
} 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 {
|
Aircrafts {
|
||||||
data,
|
data,
|
||||||
|
|
Loading…
Reference in New Issue