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::storage::metric::MetricReader;
14use reifydb_routine::{
15	function::default_native_functions,
16	procedure::default_native_procedures,
17	routine::{Procedure, registry::Routines},
18};
19use reifydb_rql::compiler::Compiler;
20use reifydb_runtime::context::{RuntimeContext, clock::Clock};
21use reifydb_store_single::SingleStore;
22use reifydb_type::value::sumtype::VariantRef;
23
24#[cfg(not(reifydb_single_threaded))]
25use crate::remote::RemoteRegistry;
26
27/// Configuration bundle for engine services.
28///
29/// Groups the shared extension registries and runtime context that flow through
30/// `StandardEngine::new` -> `Executor::new` -> `Services::new`.
31pub struct EngineConfig {
32	pub runtime_context: RuntimeContext,
33	pub routines: Routines,
34	pub transforms: Transforms,
35	pub ioc: IocContainer,
36	#[cfg(not(reifydb_single_threaded))]
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 routines: Routines,
49	pub transforms: Transforms,
50	pub flow_operator_store: SystemFlowOperatorStore,
51	pub virtual_table_registry: UserVTableRegistry,
52	pub stats_reader: MetricReader<SingleStore>,
53	pub ioc: IocContainer,
54	pub auth_registry: AuthenticationRegistry,
55	#[cfg(not(reifydb_single_threaded))]
56	pub remote_registry: Option<RemoteRegistry>,
57}
58
59impl Services {
60	pub fn new(
61		catalog: Catalog,
62		config: EngineConfig,
63		flow_operator_store: SystemFlowOperatorStore,
64		stats_reader: MetricReader<SingleStore>,
65	) -> Self {
66		let auth_registry = AuthenticationRegistry::new(config.runtime_context.clock.clone());
67		Self {
68			compiler: Compiler::new(catalog.clone()),
69			catalog,
70			runtime_context: config.runtime_context,
71			routines: config.routines,
72			transforms: config.transforms,
73			flow_operator_store,
74			virtual_table_registry: UserVTableRegistry::new(),
75			stats_reader,
76			ioc: config.ioc,
77			auth_registry,
78			#[cfg(not(reifydb_single_threaded))]
79			remote_registry: config.remote_registry,
80		}
81	}
82
83	pub fn get_handlers(&self, variant: VariantRef) -> Vec<Arc<dyn Procedure>> {
84		self.routines.get_handlers(&self.catalog.materialized, variant)
85	}
86
87	pub fn get_procedure(&self, name: &str) -> Option<Arc<dyn Procedure>> {
88		self.routines.get_procedure(name)
89	}
90
91	#[allow(dead_code)]
92	pub fn testing() -> Arc<Self> {
93		let store = SingleStore::testing_memory();
94		// Build the default Routines registry: native functions + native procedures.
95		let routines_builder = Routines::builder();
96		let routines_builder = default_native_functions(routines_builder);
97		let routines_builder = default_native_procedures(routines_builder);
98		let routines = routines_builder.configure();
99
100		let mut services = Self::new(
101			Catalog::testing(),
102			EngineConfig {
103				runtime_context: RuntimeContext::with_clock(Clock::Real),
104				routines,
105				transforms: Transforms::empty(),
106				ioc: IocContainer::new(),
107				#[cfg(not(reifydb_single_threaded))]
108				remote_registry: None,
109			},
110			SystemFlowOperatorStore::new(),
111			MetricReader::new(store),
112		);
113		services.auth_registry = AuthenticationRegistry::default();
114		Arc::new(services)
115	}
116}