Skip to main content

reifydb_engine/vm/
services.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::sync::Arc;
5
6use reifydb_auth::registry::AuthenticationRegistry;
7use reifydb_catalog::{
8	catalog::Catalog,
9	vtable::{system::flow_operator_store::SystemFlowOperatorStore, user::registry::UserVTableRegistry},
10};
11use reifydb_core::util::ioc::IocContainer;
12use reifydb_extension::transform::registry::Transforms;
13use reifydb_metric_old::metric::MetricReader;
14use reifydb_routine::{
15	function::{default_functions, registry::Functions},
16	procedure::{Procedure, registry::Procedures},
17};
18use reifydb_rql::compiler::Compiler;
19use reifydb_runtime::context::{RuntimeContext, clock::Clock};
20use reifydb_store_single::SingleStore;
21use reifydb_type::value::sumtype::VariantRef;
22
23#[cfg(not(target_arch = "wasm32"))]
24use crate::remote::RemoteRegistry;
25
26/// Configuration bundle for engine services.
27///
28/// Groups the shared extension registries and runtime context that flow through
29/// `StandardEngine::new` -> `Executor::new` -> `Services::new`.
30pub struct EngineConfig {
31	pub runtime_context: RuntimeContext,
32	pub functions: Functions,
33	pub procedures: Procedures,
34	pub transforms: Transforms,
35	pub ioc: IocContainer,
36	#[cfg(not(target_arch = "wasm32"))]
37	pub remote_registry: Option<RemoteRegistry>,
38}
39
40/// Services is a container for shared resources used throughout the execution engine.
41///
42/// This struct provides a single location for all the shared resources that the VM,
43/// query operators, and other components need access to.
44pub struct Services {
45	pub catalog: Catalog,
46	pub runtime_context: RuntimeContext,
47	pub compiler: Compiler,
48	pub functions: Functions,
49	pub procedures: Procedures,
50	pub transforms: Transforms,
51	pub flow_operator_store: SystemFlowOperatorStore,
52	pub virtual_table_registry: UserVTableRegistry,
53	pub stats_reader: MetricReader<SingleStore>,
54	pub ioc: IocContainer,
55	pub auth_registry: AuthenticationRegistry,
56	#[cfg(not(target_arch = "wasm32"))]
57	pub remote_registry: Option<RemoteRegistry>,
58}
59
60impl Services {
61	pub fn new(
62		catalog: Catalog,
63		config: EngineConfig,
64		flow_operator_store: SystemFlowOperatorStore,
65		stats_reader: MetricReader<SingleStore>,
66	) -> Self {
67		let auth_registry = AuthenticationRegistry::new(config.runtime_context.clock.clone());
68		Self {
69			compiler: Compiler::new(catalog.clone()),
70			catalog,
71			runtime_context: config.runtime_context,
72			functions: config.functions,
73			procedures: config.procedures,
74			transforms: config.transforms,
75			flow_operator_store,
76			virtual_table_registry: UserVTableRegistry::new(),
77			stats_reader,
78			ioc: config.ioc,
79			auth_registry,
80			#[cfg(not(target_arch = "wasm32"))]
81			remote_registry: config.remote_registry,
82		}
83	}
84
85	pub fn get_handlers(&self, variant: VariantRef) -> Vec<Box<dyn Procedure>> {
86		self.procedures.get_handlers(&self.catalog.materialized, variant)
87	}
88
89	pub fn get_procedure(&self, name: &str) -> Option<Box<dyn Procedure>> {
90		self.procedures.get_procedure(name)
91	}
92
93	#[allow(dead_code)]
94	pub fn testing() -> Arc<Self> {
95		let store = SingleStore::testing_memory();
96		let mut services = Self::new(
97			Catalog::testing(),
98			EngineConfig {
99				runtime_context: RuntimeContext::with_clock(Clock::Real),
100				functions: default_functions().configure(),
101				procedures: Procedures::empty(),
102				transforms: Transforms::empty(),
103				ioc: IocContainer::new(),
104				#[cfg(not(target_arch = "wasm32"))]
105				remote_registry: None,
106			},
107			SystemFlowOperatorStore::new(),
108			MetricReader::new(store),
109		);
110		services.auth_registry = AuthenticationRegistry::default();
111		Arc::new(services)
112	}
113}