Skip to main content

reifydb_engine/vm/
services.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
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
19use crate::{
20	procedure::{Procedure, registry::Procedures},
21	transform::registry::Transforms,
22};
23
24/// Services is a container for shared resources used throughout the execution engine.
25///
26/// This struct provides a single location for all the shared resources that the VM,
27/// query operators, and other components need access to.
28pub struct Services {
29	pub catalog: Catalog,
30	pub clock: Clock,
31	pub compiler: Compiler,
32	pub functions: Functions,
33	pub procedures: Procedures,
34	pub transforms: Transforms,
35	pub flow_operator_store: FlowOperatorStore,
36	pub virtual_table_registry: UserVTableRegistry,
37	pub stats_reader: MetricReader<SingleStore>,
38	pub ioc: IocContainer,
39	pub auth_registry: AuthenticationRegistry,
40}
41
42impl Services {
43	pub fn new(
44		catalog: Catalog,
45		clock: Clock,
46		functions: Functions,
47		procedures: Procedures,
48		transforms: Transforms,
49		flow_operator_store: FlowOperatorStore,
50		stats_reader: MetricReader<SingleStore>,
51		ioc: IocContainer,
52	) -> Self {
53		Self {
54			compiler: Compiler::new(catalog.clone()),
55			catalog,
56			clock,
57			functions,
58			procedures,
59			transforms,
60			flow_operator_store,
61			virtual_table_registry: UserVTableRegistry::new(),
62			stats_reader,
63			ioc,
64			auth_registry: AuthenticationRegistry::new(),
65		}
66	}
67
68	pub fn get_handlers(&self, sumtype_id: SumTypeId, variant_tag: u8) -> Vec<Box<dyn Procedure>> {
69		self.procedures.get_handlers(&self.catalog.materialized, sumtype_id, variant_tag)
70	}
71
72	pub fn get_procedure(&self, name: &str) -> Option<Box<dyn Procedure>> {
73		self.procedures.get_procedure(name)
74	}
75
76	#[allow(dead_code)]
77	pub fn testing() -> Arc<Self> {
78		let store = SingleStore::testing_memory();
79		let mut services = Self::new(
80			Catalog::testing(),
81			Clock::default(),
82			Functions::builder()
83				.register_aggregate("math::sum", math::aggregate::sum::Sum::new)
84				.register_aggregate("math::min", math::aggregate::min::Min::new)
85				.register_aggregate("math::max", math::aggregate::max::Max::new)
86				.register_aggregate("math::avg", math::aggregate::avg::Avg::new)
87				.register_aggregate("math::count", math::aggregate::count::Count::new)
88				.register_scalar("math::abs", math::scalar::abs::Abs::new)
89				.register_scalar("math::avg", math::scalar::avg::Avg::new)
90				.register_scalar("is::some", is::some::IsSome::new)
91				.register_scalar("is::none", is::none::IsNone::new)
92				.register_scalar("is::type", is::r#type::IsType::new)
93				.register_scalar("gen::series", series::Series::new)
94				.register_generator("generate_series", series::GenerateSeries::new)
95				.register_generator(
96					"inspect_subscription",
97					subscription::inspect::InspectSubscription::new,
98				)
99				.build(),
100			Procedures::empty(),
101			Transforms::empty(),
102			FlowOperatorStore::new(),
103			MetricReader::new(store),
104			IocContainer::new(),
105		);
106		services.auth_registry = AuthenticationRegistry::new();
107		Arc::new(services)
108	}
109}