Process array values concurrently

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-30 23:32:00 +00:00
parent 89867e9bfb
commit dc22d4e40e
3 changed files with 19 additions and 14 deletions

View file

@ -7,6 +7,7 @@ use crate::sql::idiom::Idiom;
use crate::sql::part::Part; use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
use async_recursion::async_recursion; use async_recursion::async_recursion;
use futures::future::try_join_all;
use std::collections::HashMap; use std::collections::HashMap;
impl Value { impl Value {
@ -43,9 +44,9 @@ impl Value {
Ok(()) Ok(())
} }
_ => { _ => {
for v in &mut v.value { let pth = path.next();
v.del(ctx, opt, exe, &path.next()).await?; let fut = v.value.iter_mut().map(|v| v.del(&ctx, opt, exe, &pth));
} try_join_all(fut).await?;
Ok(()) Ok(())
} }
}, },
@ -99,9 +100,10 @@ impl Value {
Ok(()) Ok(())
} }
_ => { _ => {
let pth = path.next();
for v in &mut v.value { for v in &mut v.value {
if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { 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(()) Ok(())

View file

@ -8,6 +8,7 @@ use crate::sql::part::Part;
use crate::sql::statements::select::SelectStatement; use crate::sql::statements::select::SelectStatement;
use crate::sql::value::{Value, Values}; use crate::sql::value::{Value, Values};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use futures::future::try_join_all;
impl Value { impl Value {
#[async_recursion] #[async_recursion]
@ -32,11 +33,9 @@ impl Value {
// Current path part is an array // Current path part is an array
Value::Array(v) => match p { Value::Array(v) => match p {
Part::All => { Part::All => {
let mut a = Vec::new(); let pth = path.next();
for v in &v.value { let fut = v.value.iter().map(|v| v.get(&ctx, opt, exe, &pth));
a.push(v.get(ctx, opt, exe, &path.next()).await?) try_join_all(fut).await.map(|v| v.into())
}
Ok(a.into())
} }
Part::First => match v.value.first() { Part::First => match v.value.first() {
Some(v) => v.get(ctx, opt, exe, &path.next()).await, Some(v) => v.get(ctx, opt, exe, &path.next()).await,
@ -51,10 +50,11 @@ impl Value {
None => Ok(Value::None), None => Ok(Value::None),
}, },
Part::Where(w) => { Part::Where(w) => {
let pth = path.next();
let mut a = Vec::new(); let mut a = Vec::new();
for v in &v.value { for v in &v.value {
if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { 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()) Ok(a.into())

View file

@ -7,6 +7,7 @@ use crate::sql::object::Object;
use crate::sql::part::Part; use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
use async_recursion::async_recursion; use async_recursion::async_recursion;
use futures::future::try_join_all;
impl Value { impl Value {
#[async_recursion] #[async_recursion]
@ -37,9 +38,10 @@ impl Value {
// Current path part is an array // Current path part is an array
Value::Array(v) => match p { Value::Array(v) => match p {
Part::All => { Part::All => {
for v in &mut v.value { let pth = path.next();
v.set(ctx, opt, exe, &path.next(), val.clone()).await?; let fut =
} v.value.iter_mut().map(|v| v.set(ctx, opt, exe, &pth, val.clone()));
try_join_all(fut).await?;
Ok(()) Ok(())
} }
Part::First => match v.value.first_mut() { Part::First => match v.value.first_mut() {
@ -55,9 +57,10 @@ impl Value {
None => Ok(()), None => Ok(()),
}, },
Part::Where(w) => { Part::Where(w) => {
let pth = path.next();
for v in &mut v.value { for v in &mut v.value {
if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() { 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(()) Ok(())