Skip to content

Commit

Permalink
feat(query-engine-common): move more stuff to "query-engine-common"
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Dec 11, 2023
1 parent ad26b19 commit 1af7189
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 194 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions libs/query-engine-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ query-connector = { path = "../../query-engine/connectors/query-connector" }
query-core = { path = "../../query-engine/core" }
user-facing-errors = { path = "../user-facing-errors" }
serde_json.workspace = true
serde.workspace = true
connection-string.workspace = true
psl.workspace = true
async-trait = "0.1"
Expand All @@ -23,3 +24,8 @@ opentelemetry = { version = "0.17"}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
napi.workspace = true
query-engine-metrics = { path = "../../query-engine/metrics" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen.workspace = true
tsify.workspace = true
97 changes: 95 additions & 2 deletions libs/query-engine-common/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
use std::collections::HashMap;

use crate::error::ApiError;
use query_core::{protocol::EngineProtocol, schema::QuerySchema, QueryExecutor};
use serde::Deserialize;
use std::{
collections::{BTreeMap, HashMap},
path::PathBuf,
sync::Arc,
};

#[cfg(target_arch = "wasm32")]
use tsify::Tsify;

/// The state of the engine.
pub enum Inner {
/// Not connected, holding all data to form a connection.
Builder(EngineBuilder),
/// A connected engine, holding all data to disconnect and form a new
/// connection. Allows querying when on this state.
Connected(ConnectedEngine),
}

impl Inner {
/// Returns a builder if the engine is not connected
pub fn as_builder(&self) -> crate::Result<&EngineBuilder> {
match self {
Inner::Builder(ref builder) => Ok(builder),
Inner::Connected(_) => Err(ApiError::AlreadyConnected),
}
}

/// Returns the engine if connected
pub fn as_engine(&self) -> crate::Result<&ConnectedEngine> {
match self {
Inner::Builder(_) => Err(ApiError::NotConnected),
Inner::Connected(ref engine) => Ok(engine),
}
}
}

/// Everything needed to connect to the database and have the core running.
pub struct EngineBuilder {
pub schema: Arc<psl::ValidatedSchema>,
pub config_dir: PathBuf,
pub env: HashMap<String, String>,
pub engine_protocol: EngineProtocol,
}

/// Internal structure for querying and reconnecting with the engine.
pub struct ConnectedEngine {
pub schema: Arc<psl::ValidatedSchema>,
pub query_schema: Arc<QuerySchema>,
pub executor: crate::Executor,
pub config_dir: PathBuf,
pub env: HashMap<String, String>,
pub engine_protocol: EngineProtocol,

#[cfg(not(target_arch = "wasm32"))]
pub metrics: Option<query_engine_metrics::MetricRegistry>,
}

impl ConnectedEngine {
/// The schema AST for Query Engine core.
pub fn query_schema(&self) -> &Arc<QuerySchema> {
&self.query_schema
}

/// The query executor.
pub fn executor(&self) -> &(dyn QueryExecutor + Send + Sync) {
self.executor.as_ref()
}

pub fn engine_protocol(&self) -> EngineProtocol {
self.engine_protocol
}
}

/// Parameters defining the construction of an engine.
#[derive(Debug, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
#[cfg_attr(target_arch = "wasm32", tsify(from_wasm_abi))]
#[serde(rename_all = "camelCase")]
pub struct ConstructorOptions {
pub datamodel: String,
pub log_level: String,
#[serde(default)]
pub log_queries: bool,
#[serde(default)]
pub datasource_overrides: BTreeMap<String, String>,
#[serde(default)]
pub env: serde_json::Value,
pub config_dir: PathBuf,
#[serde(default)]
pub ignore_env_var_errors: bool,
#[serde(default)]
pub engine_protocol: Option<EngineProtocol>,
}

pub fn map_known_error(err: query_core::CoreError) -> crate::Result<String> {
let user_error: user_facing_errors::Error = err.into();
Expand Down
1 change: 1 addition & 0 deletions libs/query-engine-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod logger;
pub mod tracer;

pub type Result<T> = std::result::Result<T, error::ApiError>;
pub type Executor = Box<dyn query_core::QueryExecutor + Send + Sync>;
99 changes: 7 additions & 92 deletions query-engine/query-engine-node-api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ use napi_derive::napi;
use psl::PreviewFeature;
use query_core::{
protocol::EngineProtocol,
schema::{self, QuerySchema},
telemetry, QueryExecutor, TransactionOptions, TxId,
schema::{self},
telemetry, TransactionOptions, TxId,
};
use query_engine_common::engine::{map_known_error, stringify_env_values};
use query_engine_metrics::{MetricFormat, MetricRegistry};
use query_engine_common::engine::{
map_known_error, stringify_env_values, ConnectedEngine, ConstructorOptions, EngineBuilder, Inner,
};
use query_engine_metrics::MetricFormat;
use request_handlers::{dmmf, load_executor, render_graphql_schema, ConnectorMode, RequestBody, RequestHandler};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
collections::{BTreeMap, HashMap},
future::Future,
panic::AssertUnwindSafe,
path::PathBuf,
sync::Arc,
};
use std::{collections::HashMap, future::Future, panic::AssertUnwindSafe, sync::Arc};
use tokio::sync::RwLock;
use tracing::{field, instrument::WithSubscriber, Instrument, Span};
use tracing_subscriber::filter::LevelFilter;
Expand All @@ -33,34 +29,6 @@ pub struct QueryEngine {
logger: Logger,
}

/// The state of the engine.
enum Inner {
/// Not connected, holding all data to form a connection.
Builder(EngineBuilder),
/// A connected engine, holding all data to disconnect and form a new
/// connection. Allows querying when on this state.
Connected(ConnectedEngine),
}

/// Everything needed to connect to the database and have the core running.
struct EngineBuilder {
schema: Arc<psl::ValidatedSchema>,
config_dir: PathBuf,
env: HashMap<String, String>,
engine_protocol: EngineProtocol,
}

/// Internal structure for querying and reconnecting with the engine.
struct ConnectedEngine {
schema: Arc<psl::ValidatedSchema>,
query_schema: Arc<QuerySchema>,
executor: crate::Executor,
config_dir: PathBuf,
env: HashMap<String, String>,
metrics: Option<MetricRegistry>,
engine_protocol: EngineProtocol,
}

/// Returned from the `serverInfo` method in javascript.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -84,59 +52,6 @@ impl MetricOptions {
}
}

impl ConnectedEngine {
/// The schema AST for Query Engine core.
pub fn query_schema(&self) -> &Arc<QuerySchema> {
&self.query_schema
}

/// The query executor.
pub fn executor(&self) -> &(dyn QueryExecutor + Send + Sync) {
self.executor.as_ref()
}

pub fn engine_protocol(&self) -> EngineProtocol {
self.engine_protocol
}
}

/// Parameters defining the construction of an engine.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ConstructorOptions {
datamodel: String,
log_level: String,
#[serde(default)]
log_queries: bool,
#[serde(default)]
datasource_overrides: BTreeMap<String, String>,
#[serde(default)]
env: serde_json::Value,
config_dir: PathBuf,
#[serde(default)]
ignore_env_var_errors: bool,
#[serde(default)]
engine_protocol: Option<EngineProtocol>,
}

impl Inner {
/// Returns a builder if the engine is not connected
fn as_builder(&self) -> crate::Result<&EngineBuilder> {
match self {
Inner::Builder(ref builder) => Ok(builder),
Inner::Connected(_) => Err(ApiError::AlreadyConnected),
}
}

/// Returns the engine if connected
fn as_engine(&self) -> crate::Result<&ConnectedEngine> {
match self {
Inner::Builder(_) => Err(ApiError::NotConnected),
Inner::Connected(ref engine) => Ok(engine),
}
}
}

#[napi]
impl QueryEngine {
/// Parse a validated datamodel and configuration to allow connecting later on.
Expand Down
1 change: 0 additions & 1 deletion query-engine/query-engine-node-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pub mod logger;
mod tracer;

pub(crate) type Result<T> = std::result::Result<T, error::ApiError>;
pub(crate) type Executor = Box<dyn query_core::QueryExecutor + Send + Sync>;
2 changes: 0 additions & 2 deletions query-engine/query-engine-wasm/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ pub mod error;
pub mod functions;
pub mod logger;
mod tracer;

pub(crate) type Executor = Box<dyn query_core::QueryExecutor + Send + Sync>;
Loading

0 comments on commit 1af7189

Please sign in to comment.