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::FlowOperatorStore, user::registry::UserVTableRegistry},
10};
11use reifydb_core::util::ioc::IocContainer;
12use reifydb_function::{is, math, registry::Functions, series, subscription};
13use reifydb_metric::metric::MetricReader;
14use reifydb_rql::compiler::Compiler;
15use reifydb_runtime::clock::Clock;
16use reifydb_store_single::SingleStore;
17use reifydb_type::value::sumtype::SumTypeId;
18
19#[cfg(not(target_arch = "wasm32"))]
20use crate::remote::RemoteRegistry;
21use crate::{
22	procedure::{Procedure, registry::Procedures},
23	transform::registry::Transforms,
24};
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 clock: Clock,
33	pub compiler: Compiler,
34	pub functions: Functions,
35	pub procedures: Procedures,
36	pub transforms: Transforms,
37	pub flow_operator_store: FlowOperatorStore,
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		clock: Clock,
50		functions: Functions,
51		procedures: Procedures,
52		transforms: Transforms,
53		flow_operator_store: FlowOperatorStore,
54		stats_reader: MetricReader<SingleStore>,
55		ioc: IocContainer,
56		#[cfg(not(target_arch = "wasm32"))] remote_registry: Option<RemoteRegistry>,
57	) -> Self {
58		Self {
59			compiler: Compiler::new(catalog.clone()),
60			catalog,
61			clock,
62			functions,
63			procedures,
64			transforms,
65			flow_operator_store,
66			virtual_table_registry: UserVTableRegistry::new(),
67			stats_reader,
68			ioc,
69			auth_registry: AuthenticationRegistry::new(),
70			#[cfg(not(target_arch = "wasm32"))]
71			remote_registry,
72		}
73	}
74
75	pub fn get_handlers(&self, sumtype_id: SumTypeId, variant_tag: u8) -> Vec<Box<dyn Procedure>> {
76		self.procedures.get_handlers(&self.catalog.materialized, sumtype_id, variant_tag)
77	}
78
79	pub fn get_procedure(&self, name: &str) -> Option<Box<dyn Procedure>> {
80		self.procedures.get_procedure(name)
81	}
82
83	#[allow(dead_code)]
84	pub fn testing() -> Arc<Self> {
85		let store = SingleStore::testing_memory();
86		let mut services = Self::new(
87			Catalog::testing(),
88			Clock::default(),
89			Functions::builder()
90				.register_aggregate("math::sum", math::aggregate::sum::Sum::new)
91				.register_aggregate("math::min", math::aggregate::min::Min::new)
92				.register_aggregate("math::max", math::aggregate::max::Max::new)
93				.register_aggregate("math::avg", math::aggregate::avg::Avg::new)
94				.register_aggregate("math::count", math::aggregate::count::Count::new)
95				.register_scalar("math::abs", math::scalar::abs::Abs::new)
96				.register_scalar("math::avg", math::scalar::avg::Avg::new)
97				.register_scalar("is::some", is::some::IsSome::new)
98				.register_scalar("is::none", is::none::IsNone::new)
99				.register_scalar("is::type", is::r#type::IsType::new)
100				.register_scalar("gen::series", series::Series::new)
101				.register_generator("generate_series", series::GenerateSeries::new)
102				.register_generator(
103					"inspect_subscription",
104					subscription::inspect::InspectSubscription::new,
105				)
106				.build(),
107			Procedures::empty(),
108			Transforms::empty(),
109			FlowOperatorStore::new(),
110			MetricReader::new(store),
111			IocContainer::new(),
112			#[cfg(not(target_arch = "wasm32"))]
113			None,
114		);
115		services.auth_registry = AuthenticationRegistry::new();
116		Arc::new(services)
117	}
118}