From 4a2a9172453861a5a14067fc68bc99ae44add319 Mon Sep 17 00:00:00 2001 From: akarras Date: Sun, 8 Oct 2023 11:03:47 -0600 Subject: [PATCH] fix: sort datacenter names & compare names lowercase --- ultros-db/src/regions_and_datacenters.rs | 38 +++++++++++++----------- ultros-db/src/world_cache.rs | 9 +++--- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/ultros-db/src/regions_and_datacenters.rs b/ultros-db/src/regions_and_datacenters.rs index 61ba6a2e..231cc116 100644 --- a/ultros-db/src/regions_and_datacenters.rs +++ b/ultros-db/src/regions_and_datacenters.rs @@ -90,27 +90,29 @@ impl UltrosDb { let new_datacenters: Vec = datacenter .0 .iter() - .sorted_by(|a, b| a.name.cmp(&b.name)) + .sorted_by_key(|d| &d.name) .cloned() .collect(); - let new_datacenters: Vec<_> = - PartialDiffIterator::from((new_datacenters.iter(), existing_datacenters.iter())) - .flat_map(|m| match m { - crate::partial_diff_iterator::DiffItem::Same(_, _) => None, - crate::partial_diff_iterator::DiffItem::Left(datacenter) => { - Some(datacenter::ActiveModel { - id: ActiveValue::default(), - name: Set(datacenter.name.0.clone()), - region_id: Set(regions - .iter() - .find(|r| r.name == datacenter.region.0) - .map(|m| m.id) - .expect("We should have all regions stored at this point.")), - }) - } - crate::partial_diff_iterator::DiffItem::Right(_) => None, + let new_datacenters: Vec<_> = PartialDiffIterator::from(( + new_datacenters.iter(), + existing_datacenters.iter().sorted_by_key(|d| &d.name), + )) + .flat_map(|m| match m { + crate::partial_diff_iterator::DiffItem::Same(_, _) => None, + crate::partial_diff_iterator::DiffItem::Left(datacenter) => { + Some(datacenter::ActiveModel { + id: ActiveValue::default(), + name: Set(datacenter.name.0.clone()), + region_id: Set(regions + .iter() + .find(|r| r.name == datacenter.region.0) + .map(|m| m.id) + .expect("We should have all regions stored at this point.")), }) - .collect(); + } + crate::partial_diff_iterator::DiffItem::Right(_) => None, + }) + .collect(); if !new_datacenters.is_empty() { info!("new datacenters {new_datacenters:?}"); if let Err(e) = datacenter::Entity::insert_many(new_datacenters) diff --git a/ultros-db/src/world_cache.rs b/ultros-db/src/world_cache.rs index 83a8138d..733f862e 100644 --- a/ultros-db/src/world_cache.rs +++ b/ultros-db/src/world_cache.rs @@ -206,16 +206,16 @@ impl WorldCache { .expect("World query shouldn't ever fail"); let name_map: HashMap<_, _> = worlds .iter() - .map(|i| (i.name.clone(), AnySelector::World(i.id))) + .map(|i| (i.name.to_lowercase(), AnySelector::World(i.id))) .chain( datacenters .iter() - .map(|i| (i.name.clone(), AnySelector::Datacenter(i.id))), + .map(|i| (i.name.to_lowercase(), AnySelector::Datacenter(i.id))), ) .chain( regions .iter() - .map(|i| (i.name.clone(), AnySelector::Region(i.id))), + .map(|i| (i.name.to_lowercase(), AnySelector::Region(i.id))), ) .collect(); @@ -284,8 +284,9 @@ impl WorldCache { } pub fn lookup_value_by_name(&self, name: &str) -> Result { + let name = name.to_lowercase(); self.name_map - .get(name) + .get(&name) .and_then(|selector| self.lookup_selector(selector).ok()) // if there's a world id that we could match with, try using that .or_else(|| {