From dc22d4e40e249e6f0e7354625dc5e2a39e3515d5 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 30 Jan 2022 23:32:00 +0000 Subject: [PATCH] Process array values concurrently --- src/sql/value/del.rs | 10 ++++++---- src/sql/value/get.rs | 12 ++++++------ src/sql/value/set.rs | 11 +++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/sql/value/del.rs b/src/sql/value/del.rs index 24ec9890..abbab3aa 100644 --- a/src/sql/value/del.rs +++ b/src/sql/value/del.rs @@ -7,6 +7,7 @@ use crate::sql::idiom::Idiom; use crate::sql::part::Part; use crate::sql::value::Value; use async_recursion::async_recursion; +use futures::future::try_join_all; use std::collections::HashMap; impl Value { @@ -43,9 +44,9 @@ impl Value { Ok(()) } _ => { - for v in &mut v.value { - v.del(ctx, opt, exe, &path.next()).await?; - } + let pth = path.next(); + let fut = v.value.iter_mut().map(|v| v.del(&ctx, opt, exe, &pth)); + try_join_all(fut).await?; Ok(()) } }, @@ -99,9 +100,10 @@ impl Value { Ok(()) } _ => { + let pth = path.next(); for v in &mut v.value { if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { - v.del(ctx, opt, exe, &path.next()).await?; + v.del(ctx, opt, exe, &pth).await?; } } Ok(()) diff --git a/src/sql/value/get.rs b/src/sql/value/get.rs index 107fc05e..158fa7d7 100644 --- a/src/sql/value/get.rs +++ b/src/sql/value/get.rs @@ -8,6 +8,7 @@ use crate::sql::part::Part; use crate::sql::statements::select::SelectStatement; use crate::sql::value::{Value, Values}; use async_recursion::async_recursion; +use futures::future::try_join_all; impl Value { #[async_recursion] @@ -32,11 +33,9 @@ impl Value { // Current path part is an array Value::Array(v) => match p { Part::All => { - let mut a = Vec::new(); - for v in &v.value { - a.push(v.get(ctx, opt, exe, &path.next()).await?) - } - Ok(a.into()) + let pth = path.next(); + let fut = v.value.iter().map(|v| v.get(&ctx, opt, exe, &pth)); + try_join_all(fut).await.map(|v| v.into()) } Part::First => match v.value.first() { Some(v) => v.get(ctx, opt, exe, &path.next()).await, @@ -51,10 +50,11 @@ impl Value { None => Ok(Value::None), }, Part::Where(w) => { + let pth = path.next(); let mut a = Vec::new(); for v in &v.value { if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { - a.push(v.get(ctx, opt, exe, &path.next()).await?) + a.push(v.get(ctx, opt, exe, &pth).await?) } } Ok(a.into()) diff --git a/src/sql/value/set.rs b/src/sql/value/set.rs index 36109323..1676653e 100644 --- a/src/sql/value/set.rs +++ b/src/sql/value/set.rs @@ -7,6 +7,7 @@ use crate::sql::object::Object; use crate::sql::part::Part; use crate::sql::value::Value; use async_recursion::async_recursion; +use futures::future::try_join_all; impl Value { #[async_recursion] @@ -37,9 +38,10 @@ impl Value { // Current path part is an array Value::Array(v) => match p { Part::All => { - for v in &mut v.value { - v.set(ctx, opt, exe, &path.next(), val.clone()).await?; - } + let pth = path.next(); + let fut = + v.value.iter_mut().map(|v| v.set(ctx, opt, exe, &pth, val.clone())); + try_join_all(fut).await?; Ok(()) } Part::First => match v.value.first_mut() { @@ -55,9 +57,10 @@ impl Value { None => Ok(()), }, Part::Where(w) => { + let pth = path.next(); for v in &mut v.value { if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { - v.set(ctx, opt, exe, &path.next(), val.clone()).await?; + v.set(ctx, opt, exe, &pth, val.clone()).await?; } } Ok(())