-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sandipsinh Rathod <zotbysandip@gmail.com> Co-authored-by: Sandipsinh Dilipsinh Rathod <62684960+ssddOnTop@users.noreply.github.com>
- Loading branch information
1 parent
c5a5094
commit a67feec
Showing
7 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,6 @@ impl JsonPlaceholder { | |
store | ||
}); | ||
|
||
Synth::new(plan.into_children(), store) | ||
Synth::new(plan, store) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use super::ExecutionPlan; | ||
use crate::core::runtime::TargetRuntime; | ||
|
||
struct Exec { | ||
runtime: TargetRuntime, | ||
plan: ExecutionPlan, | ||
} | ||
|
||
impl Exec { | ||
pub fn new(runtime: TargetRuntime, plan: ExecutionPlan) -> Self { | ||
Self { runtime, plan } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use serde_json_borrow::Value; | ||
|
||
use crate::core::ir::Error; | ||
use crate::core::jit::ir::IR; | ||
use crate::core::runtime::TargetRuntime; | ||
|
||
/// An async executor for the IR. | ||
pub struct Eval { | ||
#[allow(unused)] | ||
runtime: TargetRuntime, | ||
} | ||
|
||
impl Eval { | ||
pub fn new(runtime: TargetRuntime) -> Self { | ||
Self { runtime } | ||
} | ||
|
||
#[async_recursion::async_recursion] | ||
#[allow(clippy::only_used_in_recursion)] | ||
pub async fn eval<'a>( | ||
&'a self, | ||
ir: &'a IR, | ||
value: Option<Value<'a>>, | ||
) -> Result<Value<'a>, Error> { | ||
match ir { | ||
IR::Path(path) => { | ||
let value = value.unwrap_or(Value::Null); | ||
let value = get_path(value, path).unwrap_or(Value::Null); | ||
Ok(value) | ||
} | ||
IR::Dynamic(value) => Ok(Value::from(value)), | ||
IR::IO(_) => todo!(), | ||
IR::Cache(_) => todo!(), | ||
IR::Protect => todo!(), | ||
IR::Map(_) => todo!(), | ||
IR::Pipe(first, second) => { | ||
let first = self.eval(first, value).await?; | ||
self.eval(second, Some(first)).await | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn get_path<'a, T: AsRef<str>>(value: Value<'a>, path: &'a [T]) -> Option<Value<'a>> { | ||
let (head, tail) = path.split_first()?; | ||
let value = match value { | ||
Value::Object(map) => map | ||
.into_vec() | ||
.into_iter() | ||
.find(|(k, _)| k == head.as_ref()) | ||
.map(|(_, v)| v), | ||
Value::Array(arr) => { | ||
let index = head.as_ref().parse::<usize>().ok()?; | ||
arr.into_iter().nth(index) | ||
} | ||
_ => None, | ||
}; | ||
|
||
if tail.is_empty() { | ||
value | ||
} else { | ||
get_path(value?, tail) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json_borrow::ObjectAsVec; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_resolve_path_obj() { | ||
let mut obj = ObjectAsVec::default(); | ||
obj.insert("a", Value::Str("b".into())); | ||
let json = Value::Object(obj); | ||
|
||
let path = vec!["a"]; | ||
let result = get_path(json, &path); | ||
assert!(result.is_some()); | ||
assert_eq!(result.unwrap(), Value::Str("b".into())); | ||
} | ||
|
||
#[test] | ||
fn test_resolve_path_arr() { | ||
let arr = vec![ | ||
Value::Str("a".into()), | ||
Value::Str("b".into()), | ||
Value::Str("c".into()), | ||
]; | ||
|
||
let json = Value::Array(arr); | ||
let path = vec!["2"]; | ||
let result = get_path(json, &path); | ||
assert!(result.is_some()); | ||
assert_eq!(result.unwrap(), Value::Str("c".into())); | ||
} | ||
|
||
#[test] | ||
fn test_resolve_path_obj_and_arr() { | ||
let mut obj = ObjectAsVec::default(); | ||
obj.insert("a", Value::Str("b".into())); | ||
let json = Value::Object(obj); | ||
|
||
let arr = vec![Value::Str("a".into()), json, Value::Str("c".into())]; | ||
|
||
let json = Value::Array(arr); | ||
let path = vec!["1", "a"]; | ||
let result = get_path(json, &path); | ||
assert!(result.is_some()); | ||
assert_eq!(result.unwrap(), Value::Str("b".into())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::collections::HashMap; | ||
use std::num::NonZeroU64; | ||
|
||
use crate::core::blueprint::DynamicValue; | ||
use crate::core::config::group_by::GroupBy; | ||
use crate::core::http::HttpFilter; | ||
use crate::core::{graphql, grpc, http}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum IR { | ||
Dynamic(DynamicValue<serde_json::Value>), | ||
IO(IO), | ||
Cache(Cache), | ||
Path(Vec<String>), | ||
Protect, | ||
Map(Map), | ||
Pipe(Box<IR>, Box<IR>), | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Map { | ||
pub input: Box<IR>, | ||
pub map: HashMap<String, String>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Cache { | ||
pub max_age: NonZeroU64, | ||
pub io: IO, | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Clone, Debug)] | ||
pub struct IO { | ||
pub group_by: Option<GroupBy>, | ||
pub protocol: Protocol, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Protocol { | ||
Http { | ||
template: http::RequestTemplate, | ||
http_filter: Option<HttpFilter>, | ||
}, | ||
GraphQL { | ||
template: graphql::RequestTemplate, | ||
field_name: String, | ||
batch: bool, | ||
}, | ||
Grpc { | ||
req_template: grpc::RequestTemplate, | ||
}, | ||
Script { | ||
name: String, | ||
}, | ||
} | ||
|
||
impl Cache { | ||
pub fn new(max_age: NonZeroU64, io: IO) -> Self { | ||
Self { max_age, io } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
mod builder; | ||
mod eval_ir; | ||
mod model; | ||
mod store; | ||
mod synth; | ||
pub use builder::*; | ||
pub use eval_ir::*; | ||
pub use model::*; | ||
pub use store::*; | ||
pub use synth::*; | ||
|
||
// NOTE: Only used in tests and benchmarks | ||
pub mod common; | ||
pub mod ir; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a67feec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running 30s test @ http://localhost:8000/graphql
4 threads and 100 connections
450467 requests in 30.01s, 2.26GB read
Requests/sec: 15012.40
Transfer/sec: 77.05MB