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_catalog::{
7	catalog::Catalog,
8	vtable::{system::flow_operator_store::FlowOperatorStore, user::registry::UserVTableRegistry},
9};
10use reifydb_core::util::ioc::IocContainer;
11use reifydb_function::{is, math, registry::Functions, series, subscription};
12use reifydb_metric::metric::MetricReader;
13use reifydb_rql::compiler::Compiler;
14use reifydb_runtime::clock::Clock;
15use reifydb_store_single::SingleStore;
16
17use crate::transform::registry::Transforms;
18
19/// Services is a container for shared resources used throughout the execution engine.
20///
21/// This struct provides a single location for all the shared resources that the VM,
22/// query operators, and other components need access to.
23pub struct Services {
24	pub catalog: Catalog,
25	pub clock: Clock,
26	pub compiler: Compiler,
27	pub functions: Functions,
28	pub transforms: Transforms,
29	pub flow_operator_store: FlowOperatorStore,
30	pub virtual_table_registry: UserVTableRegistry,
31	pub stats_reader: MetricReader<SingleStore>,
32	pub ioc: IocContainer,
33}
34
35impl Services {
36	pub fn new(
37		catalog: Catalog,
38		clock: Clock,
39		functions: Functions,
40		transforms: Transforms,
41		flow_operator_store: FlowOperatorStore,
42		stats_reader: MetricReader<SingleStore>,
43		ioc: IocContainer,
44	) -> Self {
45		Self {
46			compiler: Compiler::new(catalog.clone()),
47			catalog,
48			clock,
49			functions,
50			transforms,
51			flow_operator_store,
52			virtual_table_registry: UserVTableRegistry::new(),
53			stats_reader,
54			ioc,
55		}
56	}
57
58	#[allow(dead_code)]
59	pub fn testing() -> Arc<Self> {
60		let store = SingleStore::testing_memory();
61		Arc::new(Self::new(
62			Catalog::testing(),
63			Clock::default(),
64			Functions::builder()
65				.register_aggregate("math::sum", math::aggregate::sum::Sum::new)
66				.register_aggregate("math::min", math::aggregate::min::Min::new)
67				.register_aggregate("math::max", math::aggregate::max::Max::new)
68				.register_aggregate("math::avg", math::aggregate::avg::Avg::new)
69				.register_aggregate("math::count", math::aggregate::count::Count::new)
70				.register_scalar("math::abs", math::scalar::abs::Abs::new)
71				.register_scalar("math::avg", math::scalar::avg::Avg::new)
72				.register_scalar("is::some", is::some::IsSome::new)
73				.register_scalar("is::none", is::none::IsNone::new)
74				.register_scalar("is::type", is::r#type::IsType::new)
75				.register_scalar("gen::series", series::Series::new)
76				.register_generator("generate_series", series::GenerateSeries::new)
77				.register_generator(
78					"inspect_subscription",
79					subscription::inspect::InspectSubscription::new,
80				)
81				.build(),
82			Transforms::empty(),
83			FlowOperatorStore::new(),
84			MetricReader::new(store),
85			IocContainer::new(),
86		))
87	}
88}