reifydb-engine 0.4.12

Query execution and processing engine for ReifyDB
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::sync::Arc;

use reifydb_auth::registry::AuthenticationRegistry;
use reifydb_catalog::{
	catalog::Catalog,
	vtable::{system::flow_operator_store::SystemFlowOperatorStore, user::registry::UserVTableRegistry},
};
use reifydb_core::util::ioc::IocContainer;
use reifydb_extension::transform::registry::Transforms;
use reifydb_metric::storage::metric::MetricReader;
use reifydb_routine::{
	function::{default_functions, registry::Functions},
	procedure::{Procedure, registry::Procedures},
};
use reifydb_rql::compiler::Compiler;
use reifydb_runtime::context::{RuntimeContext, clock::Clock};
use reifydb_store_single::SingleStore;
use reifydb_type::value::sumtype::VariantRef;

#[cfg(not(reifydb_single_threaded))]
use crate::remote::RemoteRegistry;

/// Configuration bundle for engine services.
///
/// Groups the shared extension registries and runtime context that flow through
/// `StandardEngine::new` -> `Executor::new` -> `Services::new`.
pub struct EngineConfig {
	pub runtime_context: RuntimeContext,
	pub functions: Functions,
	pub procedures: Procedures,
	pub transforms: Transforms,
	pub ioc: IocContainer,
	#[cfg(not(reifydb_single_threaded))]
	pub remote_registry: Option<RemoteRegistry>,
}

/// Services is a container for shared resources used throughout the execution engine.
///
/// This struct provides a single location for all the shared resources that the VM,
/// query operators, and other components need access to.
pub struct Services {
	pub catalog: Catalog,
	pub runtime_context: RuntimeContext,
	pub compiler: Compiler,
	pub functions: Functions,
	pub procedures: Procedures,
	pub transforms: Transforms,
	pub flow_operator_store: SystemFlowOperatorStore,
	pub virtual_table_registry: UserVTableRegistry,
	pub stats_reader: MetricReader<SingleStore>,
	pub ioc: IocContainer,
	pub auth_registry: AuthenticationRegistry,
	#[cfg(not(reifydb_single_threaded))]
	pub remote_registry: Option<RemoteRegistry>,
}

impl Services {
	pub fn new(
		catalog: Catalog,
		config: EngineConfig,
		flow_operator_store: SystemFlowOperatorStore,
		stats_reader: MetricReader<SingleStore>,
	) -> Self {
		let auth_registry = AuthenticationRegistry::new(config.runtime_context.clock.clone());
		Self {
			compiler: Compiler::new(catalog.clone()),
			catalog,
			runtime_context: config.runtime_context,
			functions: config.functions,
			procedures: config.procedures,
			transforms: config.transforms,
			flow_operator_store,
			virtual_table_registry: UserVTableRegistry::new(),
			stats_reader,
			ioc: config.ioc,
			auth_registry,
			#[cfg(not(reifydb_single_threaded))]
			remote_registry: config.remote_registry,
		}
	}

	pub fn get_handlers(&self, variant: VariantRef) -> Vec<Box<dyn Procedure>> {
		self.procedures.get_handlers(&self.catalog.materialized, variant)
	}

	pub fn get_procedure(&self, name: &str) -> Option<Box<dyn Procedure>> {
		self.procedures.get_procedure(name)
	}

	#[allow(dead_code)]
	pub fn testing() -> Arc<Self> {
		let store = SingleStore::testing_memory();
		let mut services = Self::new(
			Catalog::testing(),
			EngineConfig {
				runtime_context: RuntimeContext::with_clock(Clock::Real),
				functions: default_functions().configure(),
				procedures: Procedures::empty(),
				transforms: Transforms::empty(),
				ioc: IocContainer::new(),
				#[cfg(not(reifydb_single_threaded))]
				remote_registry: None,
			},
			SystemFlowOperatorStore::new(),
			MetricReader::new(store),
		);
		services.auth_registry = AuthenticationRegistry::default();
		Arc::new(services)
	}
}