Skip to content

Commit

Permalink
fix: sort datacenter names & compare names lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
akarras committed Oct 8, 2023
1 parent 2650a13 commit 4a2a917
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
38 changes: 20 additions & 18 deletions ultros-db/src/regions_and_datacenters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,29 @@ impl UltrosDb {
let new_datacenters: Vec<DataCenterView> = 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)
Expand Down
9 changes: 5 additions & 4 deletions ultros-db/src/world_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -284,8 +284,9 @@ impl WorldCache {
}

pub fn lookup_value_by_name(&self, name: &str) -> Result<AnyResult, WorldCacheError> {
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(|| {
Expand Down

0 comments on commit 4a2a917

Please sign in to comment.