use std::collections::BTreeMap;
use std::ops::Deref;
use crate::cnf::MAX_COMPUTATION_DEPTH;
use crate::ctx::Context;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::exe::try_join_all_buffered;
use crate::fnc::idiom;
use crate::sql::edges::Edges;
use crate::sql::field::{Field, Fields};
use crate::sql::part::{FindRecursionPlan, Next, NextMethod, SplitByRepeatRecurse};
use crate::sql::part::{Part, Skip};
use crate::sql::paths::ID;
use crate::sql::statements::select::SelectStatement;
use crate::sql::value::{Value, Values};
use crate::sql::Function;
use futures::future::try_join_all;
use reblessive::tree::Stk;
use super::idiom_recursion::{compute_idiom_recursion, Recursion};
impl Value {
pub(crate) async fn get(
&self,
stk: &mut Stk,
ctx: &Context,
opt: &Options,
doc: Option<&CursorDoc>,
path: &[Part],
) -> Result<Self, Error> {
if path.len() > (*MAX_COMPUTATION_DEPTH).try_into().unwrap_or(usize::MAX) {
return Err(Error::ComputationDepthExceeded);
}
match path.first() {
Some(Part::Recurse(recurse, inner_path, instruction)) => {
let (path, after) = match inner_path {
Some(p) => (p.0.as_slice(), path.next().to_vec()),
_ => (path.next(), vec![]),
};
let (path, plan, after) = match path.split_by_repeat_recurse() {
Some((path, local_after)) => (path, None, [local_after, &after].concat()),
_ => {
if instruction.is_some() {
match path.find_recursion_plan() {
Some(_) => return Err(Error::RecursionInstructionPlanConflict),
_ => (path, None, after),
}
} else {
match path.find_recursion_plan() {
Some((path, plan, local_after)) => {
(path, Some(plan), [local_after, &after].concat())
}
_ => (path, None, after),
}
}
}
};
let (min, max) = recurse.to_owned().try_into()?;
let rec = Recursion {
min,
max,
iterated: 0,
current: self,
path,
plan: plan.as_ref(),
instruction: instruction.as_ref(),
};
let v = compute_idiom_recursion(stk, ctx, opt, doc, rec).await?;
if !after.is_empty() {
stk.run(|stk| v.get(stk, ctx, opt, doc, after.as_slice())).await
} else {
Ok(v)
}
}
Some(Part::RepeatRecurse) => Err(Error::UnsupportedRepeatRecurse),
Some(Part::Doc) => {
let v = match doc {
Some(doc) => match &doc.rid {
Some(id) => Value::Thing(id.deref().to_owned()),
_ => Value::None,
},
None => Value::None,
};
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Some(p) => match self {
Value::Geometry(v) => match p {
Part::Field(f) if f.is_type() => {
let v = Value::from(v.as_type());
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Field(f) if f.is_coordinates() && v.is_geometry() => {
let v = v.as_coordinates();
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Field(f) if f.is_geometries() && v.is_collection() => {
let v = v.as_coordinates();
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Destructure(_) => {
let obj = Value::Object(v.as_object());
stk.run(|stk| obj.get(stk, ctx, opt, doc, path)).await
}
Part::Method(name, args) => {
let a = stk
.scope(|scope| {
try_join_all(
args.iter()
.map(|v| scope.run(|stk| v.compute(stk, ctx, opt, doc))),
)
})
.await?;
let v = stk
.run(|stk| idiom(stk, ctx, opt, doc, v.clone().into(), name, a))
.await?;
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Optional => {
stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await
}
_ => Ok(Value::None),
},
Value::Future(v) => {
match path.len() {
0 => Ok(Value::Future(v.clone())),
_ => {
let fut = &opt.new_with_futures(true);
let val = v.compute(stk, ctx, fut, doc).await?;
stk.run(|stk| val.get(stk, ctx, opt, doc, path)).await
}
}
}
Value::Object(v) => match p {
Part::Graph(_) => match v.rid() {
Some(v) => {
let v = Value::Thing(v);
stk.run(|stk| v.get(stk, ctx, opt, doc, path)).await
}
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Field(f) => match v.get(f.as_str()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Index(i) => match v.get(&i.to_string()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Value(x) => match stk.run(|stk| x.compute(stk, ctx, opt, doc)).await? {
Value::Strand(f) => match v.get(f.as_str()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None),
},
Value::Thing(t) => match v.get(&t.to_raw()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None),
},
_ => stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await,
},
Part::All => {
let v: Value =
v.values().map(|v| v.to_owned()).collect::<Vec<Value>>().into();
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Destructure(p) => {
let mut obj = BTreeMap::<String, Value>::new();
for p in p.iter() {
let path = p.path();
let v = stk
.run(|stk| self.get(stk, ctx, opt, doc, path.as_slice()))
.await?;
obj.insert(p.field().to_raw(), v);
}
let obj = Value::from(obj);
stk.run(|stk| obj.get(stk, ctx, opt, doc, path.next())).await
}
Part::Method(name, args) => {
let a = stk
.scope(|scope| {
try_join_all(
args.iter()
.map(|v| scope.run(|stk| v.compute(stk, ctx, opt, doc))),
)
})
.await?;
let res = stk
.run(|stk| {
idiom(stk, ctx, opt, doc, v.clone().into(), name, args.clone())
})
.await;
let res = match &res {
Ok(_) => res,
Err(Error::InvalidFunction {
..
}) => match v.get(name) {
Some(v) => {
let fnc = Function::Anonymous(v.clone(), a, true);
match stk.run(|stk| fnc.compute(stk, ctx, opt, doc)).await {
Ok(v) => Ok(v),
Err(Error::InvalidFunction {
..
}) => res,
e => e,
}
}
None => res,
},
_ => res,
}?;
stk.run(|stk| res.get(stk, ctx, opt, doc, path.next())).await
}
Part::Optional => {
stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await
}
_ => Ok(Value::None),
},
Value::Array(v) => match p {
Part::All | Part::Flatten => {
let path = path.next();
stk.scope(|scope| {
let futs =
v.iter().map(|v| scope.run(|stk| v.get(stk, ctx, opt, doc, path)));
try_join_all_buffered(futs)
})
.await
.map(Into::into)
}
Part::First => match v.first() {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Last => match v.last() {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Index(i) => match v.get(i.to_usize()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
},
Part::Where(w) => {
let mut a = Vec::new();
for v in v.iter() {
let cur = v.clone().into();
if stk
.run(|stk| w.compute(stk, ctx, opt, Some(&cur)))
.await?
.is_truthy()
{
a.push(v.clone());
}
}
let v = Value::from(a);
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Value(x) => match stk.run(|stk| x.compute(stk, ctx, opt, doc)).await? {
Value::Number(i) => match v.get(i.to_usize()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None),
},
Value::Range(r) => {
if let Some(range) = r.slice(v.as_slice()) {
let path = path.next();
stk.scope(|scope| {
let futs = range
.iter()
.map(|v| scope.run(|stk| v.get(stk, ctx, opt, doc, path)));
try_join_all_buffered(futs)
})
.await
.map(Into::into)
} else {
Ok(Value::None)
}
}
_ => stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await,
},
Part::Method(name, args) => {
let a = stk
.scope(|scope| {
try_join_all(
args.iter()
.map(|v| scope.run(|stk| v.compute(stk, ctx, opt, doc))),
)
})
.await?;
let v = stk
.run(|stk| idiom(stk, ctx, opt, doc, v.clone().into(), name, a))
.await?;
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Optional => {
stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await
}
_ => {
let len = match path.get(1) {
Some(Part::All) => 2,
_ => 1,
};
let mapped = stk
.scope(|scope| {
let futs = v.iter().map(|v| {
scope.run(|stk| v.get(stk, ctx, opt, doc, &path[0..len]))
});
try_join_all_buffered(futs)
})
.await
.map(Value::from)?;
let mapped = match (path.first(), path.get(1)) {
(Some(Part::Graph(_)), Some(Part::Graph(_))) => mapped.flatten(),
(Some(Part::Graph(_)), Some(Part::Where(_))) => mapped.flatten(),
_ => mapped,
};
stk.run(|stk| mapped.get(stk, ctx, opt, doc, path.skip(len))).await
}
},
Value::Edges(v) => {
let val = v.clone();
match path.len() {
0 => Ok(Value::Edges(val)),
_ => {
let stm = SelectStatement {
expr: Fields(vec![Field::All], false),
what: Values(vec![Value::from(val)]),
..SelectStatement::default()
};
let v = stk.run(|stk| stm.compute(stk, ctx, opt, None)).await?.all();
stk.run(|stk| v.get(stk, ctx, opt, None, path)).await?.flatten().ok()
}
}
}
Value::Thing(v) => {
let val = v.clone();
match path.len() {
0 => Ok(Value::Thing(val)),
_ => match p {
Part::Graph(g) => {
let stm = SelectStatement {
expr: Fields(vec![Field::All], false),
what: Values(vec![Value::from(Edges {
from: val,
dir: g.dir.clone(),
what: g.what.clone(),
})]),
cond: g.cond.clone(),
..SelectStatement::default()
};
match path.len() {
1 => {
let v = stk
.run(|stk| stm.compute(stk, ctx, opt, None))
.await?
.all();
stk.run(|stk| v.get(stk, ctx, opt, None, ID.as_ref()))
.await?
.flatten()
.ok()
}
_ => {
let v = stk
.run(|stk| stm.compute(stk, ctx, opt, None))
.await?
.all();
let res = stk
.run(|stk| v.get(stk, ctx, opt, None, path.next()))
.await?;
Ok(match path[1] {
Part::Graph(_) | Part::Where(_) => res.flatten(),
_ => res,
})
}
}
}
Part::Method(name, args) => {
let a = stk
.scope(|scope| {
try_join_all(args.iter().map(|v| {
scope.run(|stk| v.compute(stk, ctx, opt, doc))
}))
})
.await?;
let v = stk
.run(|stk| idiom(stk, ctx, opt, doc, v.clone().into(), name, a))
.await?;
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
Part::Optional => {
stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await
}
_ => {
let stm = SelectStatement {
expr: Fields(vec![Field::All], false),
what: Values(vec![Value::from(val)]),
..SelectStatement::default()
};
let v =
stk.run(|stk| stm.compute(stk, ctx, opt, None)).await?.first();
let next = match path.first() {
Some(Part::All) => path.next(),
_ => path,
};
stk.run(|stk| v.get(stk, ctx, opt, None, next)).await
}
},
}
}
v => {
match p {
Part::Optional => match &self {
Value::None => Ok(Value::None),
v => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
},
Part::Flatten => {
stk.run(|stk| v.get(stk, ctx, opt, None, path.next())).await
}
Part::Method(name, args) => {
let a = stk
.scope(|scope| {
try_join_all(
args.iter().map(|v| {
scope.run(|stk| v.compute(stk, ctx, opt, doc))
}),
)
})
.await?;
let v = stk
.run(|stk| idiom(stk, ctx, opt, doc, v.clone(), name, a))
.await?;
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
}
_ => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next_method()))
.await
}
}
}
},
None => Ok(self.clone()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dbs::test::mock;
use crate::sql::idiom::Idiom;
use crate::sql::{Id, Thing};
use crate::syn::Parse;
#[tokio::test]
async fn get_none() {
let (ctx, opt) = mock().await;
let idi = Idiom::default();
let val = Value::parse("{ test: { other: null, something: 123 } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, val);
}
#[tokio::test]
async fn get_basic() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something");
let val = Value::parse("{ test: { other: null, something: 123 } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(123));
}
#[tokio::test]
async fn get_basic_deep_ok() {
let (ctx, opt) = mock().await;
let depth = 20;
let idi = Idiom::parse(&format!("{}something", "test.".repeat(depth)));
let val = Value::parse(&format!(
"{} {{ other: null, something: 123 {} }}",
"{ test: ".repeat(depth),
"}".repeat(depth)
));
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(123));
}
#[tokio::test]
async fn get_basic_deep_ko() {
let (ctx, opt) = mock().await;
let depth = 2000;
let idi = Idiom::parse(&format!("{}something", "test.".repeat(depth)));
let val = Value::parse("{}"); let mut stack = reblessive::tree::TreeStack::new();
let err =
stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap_err();
assert!(
matches!(err, Error::ComputationDepthExceeded),
"expected computation depth exceeded, got {:?}",
err
);
}
#[tokio::test]
async fn get_thing() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.other");
let val = Value::parse("{ test: { other: test:tobie, something: 123 } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(Thing {
tb: String::from("test"),
id: Id::from("tobie")
})
);
}
#[tokio::test]
async fn get_array() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[1]");
let val = Value::parse("{ test: { something: [123, 456, 789] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(456));
}
#[tokio::test]
async fn get_array_thing() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[1]");
let val = Value::parse("{ test: { something: [test:tobie, test:jaime] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(Thing {
tb: String::from("test"),
id: Id::from("jaime")
})
);
}
#[tokio::test]
async fn get_array_field() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[1].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(36));
}
#[tokio::test]
async fn get_array_fields() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[*].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(vec![34, 36]));
}
#[tokio::test]
async fn get_array_fields_flat() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something.age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(vec![34, 36]));
}
#[tokio::test]
async fn get_array_where_field() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[WHERE age > 35].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(vec![36]));
}
#[tokio::test]
async fn get_array_where_fields() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[WHERE age > 35]");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(vec![Value::from(map! {
"age".to_string() => Value::from(36),
})])
);
}
#[tokio::test]
async fn get_array_where_fields_array_index() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[WHERE age > 30][0]");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(map! {
"age".to_string() => Value::from(34),
})
);
}
#[tokio::test]
async fn get_future_embedded_field() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test.something[WHERE age > 35]");
let val = Value::parse("{ test: <future> { { something: [{ age: 34 }, { age: 36 }] } } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(vec![Value::from(map! {
"age".to_string() => Value::from(36),
})])
);
}
#[tokio::test]
async fn get_future_embedded_field_with_reference() {
let (ctx, opt) = mock().await;
let doc = Value::parse("{ name: 'Tobie', something: [{ age: 34 }, { age: 36 }] }");
let idi = Idiom::parse("test.something[WHERE age > 35]");
let val = Value::parse("{ test: <future> { { something: something } } }");
let cur = doc.into();
let mut stack = reblessive::tree::TreeStack::new();
let res =
stack.enter(|stk| val.get(stk, &ctx, &opt, Some(&cur), &idi)).finish().await.unwrap();
assert_eq!(
res,
Value::from(vec![Value::from(map! {
"age".to_string() => Value::from(36),
})])
);
}
#[tokio::test]
async fn get_object_with_thing_based_key() {
let (ctx, opt) = mock().await;
let idi = Idiom::parse("test[city:london]");
let val =
Value::parse("{ test: { 'city:london': true, other: test:tobie, something: 123 } }");
let mut stack = reblessive::tree::TreeStack::new();
let res = stack.enter(|stk| val.get(stk, &ctx, &opt, None, &idi)).finish().await.unwrap();
assert_eq!(res, Value::from(true));
}
}