Skip to content

Commit

Permalink
Reduce cloning in nested read
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn authored and Julius de Bruijn committed Mar 19, 2020
1 parent 6aa23a6 commit 92d6f6f
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions query-engine/core/src/interpreter/query_interpreters/nested_read.rs
@@ -1,7 +1,11 @@
use crate::interpreter::query_interpreters::nested_pagination::NestedPagination;
use crate::{interpreter::InterpretationResult, query_ast::*};
use connector::{self, filter::Filter, ConnectionLike, IdFilter, QueryArguments, ReadOperations, ScalarCompare};
use prisma_models::{ManyRecords, RecordProjection, RelationFieldRef, Result as DomainResult, SelectedFields};
use connector::{
self, filter::Filter, ConnectionLike, IdFilter, QueryArguments, ReadOperations, ScalarCompare,
};
use prisma_models::{
ManyRecords, RecordProjection, RelationFieldRef, Result as DomainResult, SelectedFields,
};
use prisma_value::PrismaValue;
use std::collections::HashMap;

Expand All @@ -25,7 +29,9 @@ pub async fn m2m<'a, 'b>(
}
};

let ids = tx.get_related_m2m_record_ids(&query.parent_field, &parent_ids).await?;
let ids = tx
.get_related_m2m_record_ids(&query.parent_field, &parent_ids)
.await?;
let child_model_id = query.parent_field.related_model().primary_identifier();

let child_ids: Vec<Vec<PrismaValue>> = ids
Expand Down Expand Up @@ -150,8 +156,8 @@ pub async fn one2m<'a, 'b>(
.into_iter()
.map(|id_values: &Vec<PrismaValue>| {
Ok(child_link_id
.from_unchecked(id_values.iter().map(|v: &PrismaValue| v.clone()).collect())
.filter())
.from_unchecked(id_values.iter().map(|v: &PrismaValue| v.clone()).collect())
.filter())
})
.collect::<DomainResult<_>>()?;

Expand All @@ -171,22 +177,22 @@ pub async fn one2m<'a, 'b>(
.get_many_records(&parent_field.related_model(), args, selected_fields)
.await?;

let child_field_names = scalars.field_names.clone();

// Inlining is done on the parent, this means that we need to write the primary parent ID
// into the child records that we retrieved. The matching is done based on the parent link values.
if parent_field.is_inlined_on_enclosing_model() {
let mut additional_records = vec![];

for mut record in scalars.records.iter_mut() {
let child_link: RecordProjection = record.projection(&child_field_names, &child_link_id)?;
let child_link_values: Vec<PrismaValue> = child_link.pairs.iter().map(|(_, v)| v.clone()).collect();
let child_link: RecordProjection =
record.projection(&scalars.field_names, &child_link_id)?;
let child_link_values: Vec<PrismaValue> =
child_link.pairs.into_iter().map(|(_, v)| v).collect();

if let Some(parent_ids) = link_mapping.get_mut(&child_link_values) {
parent_ids.reverse();

let parent_id = parent_ids.pop().unwrap();
record.parent_id = Some(parent_id.clone());
record.parent_id = Some(parent_id);

for parent_id in parent_ids {
let mut record = record.clone();
Expand All @@ -203,13 +209,13 @@ pub async fn one2m<'a, 'b>(
let child_link_fields = parent_field.related_field().linking_fields();

for record in scalars.records.iter_mut() {
let child_link: RecordProjection = record.projection(&child_field_names, &child_link_fields)?;
let child_link_values: Vec<PrismaValue> = child_link.pairs.iter().map(|(_, v)| v.clone()).collect();

if let Some(parent_ids) = link_mapping.get_mut(&child_link_values) {
parent_ids.reverse();
let child_link: RecordProjection =
record.projection(&scalars.field_names, &child_link_fields)?;
let child_link_values: Vec<PrismaValue> =
child_link.pairs.into_iter().map(|(_, v)| v).collect();

let parent_id = parent_ids.first().unwrap();
if let Some(parent_ids) = link_mapping.get(&child_link_values) {
let parent_id = parent_ids.last().unwrap();
record.parent_id = Some(parent_id.clone());
}
}
Expand Down

0 comments on commit 92d6f6f

Please sign in to comment.