Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loader: use ValueTuple as hash key (still requires update on SeaQuery) #1868

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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