From 17c943d84e85137fa8c2d7d96a94868cc3a3ce0a Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 24 Sep 2023 23:03:14 +0800 Subject: [PATCH] Loader: use ValueTuple as hash key (#1868) --- Cargo.toml | 2 +- src/query/loader.rs | 46 +++++++++++++++++---------------------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c852a1ee..ed33aea24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ tracing = { version = "0.1", default-features = false, features = ["attributes", rust_decimal = { version = "1", default-features = false, optional = true } bigdecimal = { version = "0.3", default-features = false, optional = true } sea-orm-macros = { version = "0.12.3", path = "sea-orm-macros", default-features = false, features = ["strum"] } -sea-query = { version = "0.30.1", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] } +sea-query = { version = "0.30.2", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] } sea-query-binder = { version = "0.5.0", default-features = false, optional = true } strum = { version = "0.25", default-features = false } serde = { version = "1.0", default-features = false } diff --git a/src/query/loader.rs b/src/query/loader.rs index c615b4e46..e4e27aa2d 100644 --- a/src/query/loader.rs +++ b/src/query/loader.rs @@ -158,24 +158,20 @@ where let data = stmt.all(db).await?; - let hashmap: HashMap::Model> = data.into_iter().fold( - HashMap::::Model>::new(), - |mut acc: HashMap::Model>, - value: ::Model| { + let hashmap: HashMap::Model> = data.into_iter().fold( + HashMap::new(), + |mut acc, value: ::Model| { { let key = extract_key(&rel_def.to_col, &value); - - acc.insert(format!("{key:?}"), value); + acc.insert(key, value); } acc }, ); - let result: Vec::Model>> = keys - .iter() - .map(|key| hashmap.get(&format!("{key:?}")).cloned()) - .collect(); + let result: Vec::Model>> = + keys.iter().map(|key| hashmap.get(key).cloned()).collect(); Ok(result) } @@ -213,11 +209,10 @@ where let data = stmt.all(db).await?; - let mut hashmap: HashMap::Model>> = + let mut hashmap: HashMap::Model>> = keys.iter() .fold(HashMap::new(), |mut acc, key: &ValueTuple| { - acc.insert(format!("{key:?}"), Vec::new()); - + acc.insert(key.clone(), Vec::new()); acc }); @@ -226,7 +221,7 @@ where let key = extract_key(&rel_def.to_col, &value); let vec = hashmap - .get_mut(&format!("{key:?}")) + .get_mut(&key) .expect("Failed at finding key on hashmap"); vec.push(value); @@ -234,12 +229,7 @@ where let result: Vec> = keys .iter() - .map(|key: &ValueTuple| { - hashmap - .get(&format!("{key:?}")) - .cloned() - .unwrap_or_default() - }) + .map(|key: &ValueTuple| hashmap.get(key).cloned().unwrap_or_default()) .collect(); Ok(result) @@ -287,14 +277,14 @@ where .collect(); // Map of M::PK -> Vec - let mut keymap: HashMap> = Default::default(); + let mut keymap: HashMap> = Default::default(); let keys: Vec = { let condition = prepare_condition(&via_rel.to_tbl, &via_rel.to_col, &pkeys); let stmt = V::find().filter(condition); let data = stmt.all(db).await?; data.into_iter().for_each(|model| { - let pk = format!("{:?}", extract_key(&via_rel.to_col, &model)); + let pk = extract_key(&via_rel.to_col, &model); let entry = keymap.entry(pk).or_default(); let fk = extract_key(&rel_def.from_col, &model); @@ -309,11 +299,12 @@ where let stmt = as QueryFilter>::filter(stmt.select(), condition); let data = stmt.all(db).await?; + // Map of R::PK -> R::Model - let data: HashMap::Model> = data + let data: HashMap::Model> = data .into_iter() .map(|model| { - let key = format!("{:?}", extract_key(&rel_def.to_col, &model)); + let key = extract_key(&rel_def.to_col, &model); (key, model) }) .collect(); @@ -321,14 +312,11 @@ where let result: Vec> = pkeys .into_iter() .map(|pkey| { - let fkeys = keymap - .get(&format!("{pkey:?}")) - .cloned() - .unwrap_or_default(); + let fkeys = keymap.get(&pkey).cloned().unwrap_or_default(); let models: Vec<_> = fkeys .into_iter() - .filter_map(|fkey| data.get(&format!("{fkey:?}")).cloned()) + .filter_map(|fkey| data.get(&fkey).cloned()) .collect(); models