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::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;
20use reifydb_store_single::SingleStore;
21use reifydb_type::value::sumtype::VariantRef;
22
23#[cfg(not(target_arch = "wasm32"))]
24use crate::remote::RemoteRegistry;
25
26/// Services is a container for shared resources used throughout the execution engine.
27///
28/// This struct provides a single location for all the shared resources that the VM,
29/// query operators, and other components need access to.
30pub struct Services {
31	pub catalog: Catalog,
32	pub runtime_context: RuntimeContext,
33	pub compiler: Compiler,
34	pub functions: Functions,
35	pub procedures: Procedures,
36	pub transforms: Transforms,
37	pub flow_operator_store: SystemFlowOperatorStore,
38	pub virtual_table_registry: UserVTableRegistry,
39	pub stats_reader: MetricReader<SingleStore>,
40	pub ioc: IocContainer,
41	pub auth_registry: AuthenticationRegistry,
42	#[cfg(not(target_arch = "wasm32"))]
43	pub remote_registry: Option<RemoteRegistry>,
44}
45
46impl Services {
47	pub fn new(
48		catalog: Catalog,
49		runtime_context: RuntimeContext,
50		functions: Functions,
51		procedures: Procedures,
52		transforms: Transforms,
53		flow_operator_store: SystemFlowOperatorStore,
54		stats_reader: MetricReader<SingleStore>,
55		ioc: IocContainer,
56		#[cfg(not(target_arch = "wasm32"))] remote_registry: Option<RemoteRegistry>,
57	) -> Self {
58		let auth_registry = AuthenticationRegistry::new(runtime_context.clock.clone());
59		Self {
60			compiler: Compiler::new(catalog.clone()),
61			catalog,
62			runtime_context,
63			functions,
64			procedures,
65			transforms,
66			flow_operator_store,
67			virtual_table_registry: UserVTableRegistry::new(),
68			stats_reader,
69			ioc,
70			auth_registry,
71			#[cfg(not(target_arch = "wasm32"))]
72			remote_registry,
73		}
74	}
75
76	pub fn get_handlers(&self, variant: VariantRef) -> Vec<Box<dyn Procedure>> {
77		self.procedures.get_handlers(&self.catalog.materialized, variant)
78	}
79
80	pub fn get_procedure(&self, name: &str) -> Option<Box<dyn Procedure>> {
81		self.procedures.get_procedure(name)
82	}
83
84	#[allow(dead_code)]
85	pub fn testing() -> Arc<Self> {
86		let store = SingleStore::testing_memory();
87		let mut services = Self::new(
88			Catalog::testing(),
89			RuntimeContext::default(),
90			default_functions().build(),
91			Procedures::empty(),
92			Transforms::empty(),
93			SystemFlowOperatorStore::new(),
94			MetricReader::new(store),
95			IocContainer::new(),
96			#[cfg(not(target_arch = "wasm32"))]
97			None,
98		);
99		services.auth_registry = AuthenticationRegistry::default();
100		Arc::new(services)
101	}
102}