diff --git a/Cargo.lock b/Cargo.lock index b29be2bbc4..0e07909f40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3128,6 +3128,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.5" @@ -3138,6 +3152,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -3153,6 +3176,28 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -5116,6 +5161,7 @@ dependencies = [ "mime", "moka", "nom", + "num", "num_cpus", "once_cell", "opentelemetry 0.23.0", diff --git a/Cargo.toml b/Cargo.toml index 3a5fc97694..5bffcadf33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -159,6 +159,7 @@ tokio-test = "0.4.4" base64 = "0.22.1" tailcall-hasher = { path = "tailcall-hasher" } serde_json_borrow = "0.5.0" +num = "0.4.3" [dev-dependencies] tailcall-prettier = { path = "tailcall-prettier" } diff --git a/src/core/counter.rs b/src/core/counter.rs index afb7b2c138..5474e90300 100644 --- a/src/core/counter.rs +++ b/src/core/counter.rs @@ -1,15 +1,38 @@ use std::cell::Cell; +use std::sync::Mutex; + +pub trait Count { + type Item; + fn next(&self) -> Self::Item; +} -#[allow(unused)] #[derive(Default)] -pub struct Counter(Cell); -impl Counter { - pub fn new(start: usize) -> Self { +pub struct Counter(Cell); +impl Counter { + pub fn new(start: A) -> Self { Self(Cell::new(start)) } - pub fn next(&self) -> usize { +} + +impl Count for Counter { + type Item = A; + + fn next(&self) -> A { let curr = self.0.get(); - self.0.set(curr + 1); + self.0.set(curr + A::one()); curr } } + +#[derive(Default)] +pub struct AtomicCounter(Mutex); + +impl Count for AtomicCounter { + type Item = A; + + fn next(&self) -> A { + let mut x = self.0.lock().unwrap(); + *x = *x + A::one(); + *x + } +} diff --git a/src/core/generator/json/mod.rs b/src/core/generator/json/mod.rs index a67776fe3b..bde44f982c 100644 --- a/src/core/generator/json/mod.rs +++ b/src/core/generator/json/mod.rs @@ -10,10 +10,10 @@ pub use query_generator::QueryGenerator; pub use schema_generator::SchemaGenerator; pub use types_generator::TypesGenerator; -use crate::core::counter::Counter; +use crate::core::counter::{Count, Counter}; pub struct NameGenerator { - counter: Counter, + counter: Counter, prefix: String, } diff --git a/src/core/ir/jit/builder.rs b/src/core/ir/jit/builder.rs index 8f05b4831b..9adca712f3 100644 --- a/src/core/ir/jit/builder.rs +++ b/src/core/ir/jit/builder.rs @@ -6,14 +6,14 @@ use async_graphql_parser::types::{OperationType, SelectionSet}; use super::field_index::{FieldIndex, QueryField}; use super::model::*; use crate::core::blueprint::Blueprint; -use crate::core::counter::Counter; +use crate::core::counter::{Count, Counter}; use crate::core::merge_right::MergeRight; #[allow(unused)] pub struct ExecutionPlanBuilder { pub index: FieldIndex, - pub arg_id: Counter, - pub field_id: Counter, + pub arg_id: Counter, + pub field_id: Counter, pub document: ExecutableDocument, } diff --git a/src/core/ir/jit/context.rs b/src/core/ir/jit/context.rs index f32a4703b9..1d8beebbf5 100644 --- a/src/core/ir/jit/context.rs +++ b/src/core/ir/jit/context.rs @@ -5,12 +5,12 @@ use serde_json_borrow::OwnedValue; use super::model::{ExecutionPlan, Field, FieldId, Parent}; use super::store::Store; -use crate::core::ir::{EvaluationError, IoId, IR}; +use crate::core::ir::{CallId, EvaluationError, IR}; #[allow(unused)] pub struct ExecutionContext { plan: ExecutionPlan, - store: Mutex>, + store: Mutex>, } #[allow(unused)] @@ -67,7 +67,7 @@ impl ExecutionContext { .collect::>() } - pub async fn execute(self) -> Result, EvaluationError> { + pub async fn execute(self) -> Result, EvaluationError> { future::join_all( self.root() .iter() diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__default_value.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__default_value.snap index 8f0379be4a..102f565a68 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__default_value.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__default_value.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "createPost", @@ -48,7 +48,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "createPost", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__fragments.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__fragments.snap index 0d5df6472b..ce618841ee 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__fragments.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__fragments.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "name", @@ -39,7 +39,7 @@ ExecutionPlan { ], }, ], - field_children: [ + children: [ Field { id: 0, name: "name", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__from_document.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__from_document.snap index 13663a037e..f4cd66f7ea 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__from_document.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__from_document.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "posts", @@ -36,7 +36,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "posts", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__multiple_operations.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__multiple_operations.snap index 863d09bfb3..9f47fa7dbf 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__multiple_operations.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__multiple_operations.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "user", @@ -62,7 +62,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "user", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_mutation.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_mutation.snap index 446fe4dd67..bfa496f510 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_mutation.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_mutation.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "createUser", @@ -103,7 +103,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "createUser", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_query.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_query.snap index 057fe0f27f..8909f45e90 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_query.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__simple_query.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "posts", @@ -28,7 +28,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "posts", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__unions.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__unions.snap index 2232a7cfa8..927b6fe3f2 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__unions.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__unions.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "getUserIdOrEmail", @@ -24,7 +24,7 @@ ExecutionPlan { ], }, ], - field_children: [ + children: [ Field { id: 0, name: "getUserIdOrEmail", diff --git a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__variables.snap b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__variables.snap index 079c8349c5..340f673c8b 100644 --- a/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__variables.snap +++ b/src/core/ir/jit/snapshots/tailcall__core__ir__jit__builder__tests__variables.snap @@ -3,7 +3,7 @@ source: src/core/ir/jit/builder.rs expression: plan --- ExecutionPlan { - fields: [ + parent: [ Field { id: 0, name: "user", @@ -42,7 +42,7 @@ ExecutionPlan { ), }, ], - field_children: [ + children: [ Field { id: 0, name: "user", diff --git a/src/core/ir/jit/synth.rs b/src/core/ir/jit/synth.rs index b6fba5f7c5..e823af8e30 100644 --- a/src/core/ir/jit/synth.rs +++ b/src/core/ir/jit/synth.rs @@ -2,22 +2,22 @@ pub use serde_json_borrow::*; use super::model::{Children, Field}; use super::store::{Data, Store}; -use crate::core::ir::IoId; +use crate::core::ir::CallId; #[allow(unused)] pub struct Synth { operation: Field, - store: Store, + store: Store, } #[allow(unused)] impl Synth { - pub fn new(operation: Field, store: Store) -> Self { + pub fn new(operation: Field, store: Store) -> Self { Synth { operation, store } } pub fn synthesize(&self) -> Value<'_> { - let value = self.store.get(&IoId::new(0)); + let value = self.store.get(&CallId::new(0)); self.iter(&self.operation, value) } @@ -28,7 +28,7 @@ impl Synth { pub fn iter<'a>( &'a self, node: &'a Field, - parent: Option<&'a Data>, + parent: Option<&'a Data>, ) -> Value<'a> { match parent { Some(parent) => match parent.data.as_ref().map(|v| v.get_value()) { @@ -55,7 +55,7 @@ impl Synth { &'a self, node: &'a Field, parent: Option<&'a Value<'a>>, - value: &Data, + value: &Data, ) -> Value<'a> { match parent { Some(Value::Object(obj)) => { @@ -117,7 +117,7 @@ mod tests { use crate::core::ir::jit::model::FieldId; use crate::core::ir::jit::store::{Data, Store}; use crate::core::ir::jit::synth::Synth; - use crate::core::ir::IoId; + use crate::core::ir::CallId; use crate::core::valid::Validator; const POSTS: &str = r#" @@ -148,7 +148,7 @@ mod tests { const CONFIG: &str = include_str!("./fixtures/jsonplaceholder-mutation.graphql"); - fn synth(query: &str, data: Vec<(IoId, Data)>) -> String { + fn synth(query: &str, data: Vec<(CallId, Data)>) -> String { let config = Config::from_sdl(CONFIG).to_result().unwrap(); let blueprint = Blueprint::try_from(&config.into()).unwrap(); let document = async_graphql::parser::parse_query(query).unwrap(); @@ -172,18 +172,18 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /posts ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(POSTS).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), ]; @@ -205,18 +205,18 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /users ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(USERS).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), ]; @@ -237,15 +237,15 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /user/:id ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(USER).unwrap()), extras: Default::default(), @@ -269,23 +269,23 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /posts/:id ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(POST).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(3), IoId::new(2))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(3), CallId::new(2))].into_iter()), }, ), // Insert /user/:id ( - IoId::new(2), + CallId::new(2), Data { data: Some(OwnedValue::from_str(USER).unwrap()), extras: Default::default(), @@ -310,23 +310,23 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /posts ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(POSTS).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(3), IoId::new(2))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(3), CallId::new(2))].into_iter()), }, ), // Insert /user/:id ( - IoId::new(2), + CallId::new(2), Data { data: Some(OwnedValue::from_str(USER).unwrap()), extras: Default::default(), @@ -351,30 +351,30 @@ mod tests { let store = vec![ // Insert Root ( - IoId::new(0), + CallId::new(0), Data { data: None, - extras: HashMap::from_iter(vec![(FieldId::new(0), IoId::new(1))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(0), CallId::new(1))].into_iter()), }, ), // Insert /posts ( - IoId::new(1), + CallId::new(1), Data { data: Some(OwnedValue::from_str(POSTS).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(2), IoId::new(2))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(2), CallId::new(2))].into_iter()), }, ), // Insert /user/:id ( - IoId::new(2), + CallId::new(2), Data { data: Some(OwnedValue::from_str(USER).unwrap()), - extras: HashMap::from_iter(vec![(FieldId::new(4), IoId::new(3))].into_iter()), + extras: HashMap::from_iter(vec![(FieldId::new(4), CallId::new(3))].into_iter()), }, ), ( - IoId::new(3), + CallId::new(3), Data { data: Some(OwnedValue::from_str(TODO).unwrap()), extras: Default::default(), diff --git a/src/core/ir/mod.rs b/src/core/ir/mod.rs index 5f202ea75b..00d0944d35 100644 --- a/src/core/ir/mod.rs +++ b/src/core/ir/mod.rs @@ -25,6 +25,15 @@ use crate::core::blueprint::DynamicValue; use crate::core::json::JsonLike; use crate::core::serde_value_ext::ValueExt; +#[derive(Eq, Hash, PartialEq, Clone, Debug)] +pub struct CallId(usize); + +impl CallId { + pub fn new(id: usize) -> Self { + Self(id) + } +} + #[derive(Clone, Debug)] pub enum IR { Context(Context),