Skip to content

Commit

Permalink
Loader: use ValueTuple as hash key (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 authored Sep 24, 2023
1 parent 36e2f14 commit 17c943d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
46 changes: 17 additions & 29 deletions src/query/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,20 @@ where

let data = stmt.all(db).await?;

let hashmap: HashMap<String, <R as EntityTrait>::Model> = data.into_iter().fold(
HashMap::<String, <R as EntityTrait>::Model>::new(),
|mut acc: HashMap<String, <R as EntityTrait>::Model>,
value: <R as EntityTrait>::Model| {
let hashmap: HashMap<ValueTuple, <R as EntityTrait>::Model> = data.into_iter().fold(
HashMap::new(),
|mut acc, value: <R as EntityTrait>::Model| {
{
let key = extract_key(&rel_def.to_col, &value);

acc.insert(format!("{key:?}"), value);
acc.insert(key, value);
}

acc
},
);

let result: Vec<Option<<R as EntityTrait>::Model>> = keys
.iter()
.map(|key| hashmap.get(&format!("{key:?}")).cloned())
.collect();
let result: Vec<Option<<R as EntityTrait>::Model>> =
keys.iter().map(|key| hashmap.get(key).cloned()).collect();

Ok(result)
}
Expand Down Expand Up @@ -213,11 +209,10 @@ where

let data = stmt.all(db).await?;

let mut hashmap: HashMap<String, Vec<<R as EntityTrait>::Model>> =
let mut hashmap: HashMap<ValueTuple, Vec<<R as EntityTrait>::Model>> =
keys.iter()
.fold(HashMap::new(), |mut acc, key: &ValueTuple| {
acc.insert(format!("{key:?}"), Vec::new());

acc.insert(key.clone(), Vec::new());
acc
});

Expand All @@ -226,20 +221,15 @@ 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);
});

let result: Vec<Vec<R::Model>> = 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)
Expand Down Expand Up @@ -287,14 +277,14 @@ where
.collect();

// Map of M::PK -> Vec<R::PK>
let mut keymap: HashMap<String, Vec<ValueTuple>> = Default::default();
let mut keymap: HashMap<ValueTuple, Vec<ValueTuple>> = Default::default();

let keys: Vec<ValueTuple> = {
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);
Expand All @@ -309,26 +299,24 @@ where
let stmt = <Select<R> as QueryFilter>::filter(stmt.select(), condition);

let data = stmt.all(db).await?;

// Map of R::PK -> R::Model
let data: HashMap<String, <R as EntityTrait>::Model> = data
let data: HashMap<ValueTuple, <R as EntityTrait>::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();

let result: Vec<Vec<R::Model>> = 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
Expand Down

0 comments on commit 17c943d

Please sign in to comment.