Skip to content

Commit

Permalink
Loader: use ValueTuple as hash key (still requires update on SeaQuery)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 22, 2023
1 parent 5a6acd6 commit c6941f3
Showing 1 changed file with 17 additions and 29 deletions.
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 c6941f3

Please sign in to comment.