Skip to main content

reifydb_sub_flow/operator/
provider.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
5use std::sync::Arc;
6
7#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
8use reifydb_codec::value::encode_params;
9#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
10use reifydb_core::operator_with::encode_apply_with;
11use reifydb_core::{interface::catalog::flow::OperatorId, operator_with::ApplyWith};
12#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
13use reifydb_extension::operator::extern_c::loader::extern_c_operator_loader;
14use reifydb_flow::error::FlowGraphError;
15#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
16use reifydb_flow_async::error::FlowStateError;
17use reifydb_flow_async::operator::{BoxedHostOperator, provider::OperatorProvider};
18#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
19use reifydb_value::params::Params;
20use reifydb_value::{Result, config::ExtensionParams, error::Error};
21#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
22use tracing::instrument;
23
24use crate::builder::CustomOperators;
25#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
26use crate::{
27	error::ExternOperatorError, operator::extern_c::ExternCOperatorHandle,
28	operator::extern_rust::extern_rust_operator_loader,
29};
30
31pub struct StandardOperatorProvider {
32	custom: CustomOperators,
33}
34
35impl StandardOperatorProvider {
36	pub fn new(custom: CustomOperators) -> Self {
37		Self {
38			custom,
39		}
40	}
41}
42
43impl OperatorProvider for StandardOperatorProvider {
44	fn provide(
45		&self,
46		operator_id: OperatorId,
47		params: &ExtensionParams,
48		with: &ApplyWith,
49	) -> Result<BoxedHostOperator> {
50		let operator = params.name();
51
52		if let Some(factory) = self.custom.get(operator) {
53			return factory(operator_id, params, with);
54		}
55
56		#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
57		{
58			if extern_rust_operator_loader().read().has_operator(operator) {
59				return extern_rust_operator_loader().write().create_operator_by_name(
60					operator,
61					operator_id,
62					params,
63					with,
64				);
65			}
66
67			if extern_c_operator_loader().read().has_operator(operator) {
68				return self.create_extern_c_operator(operator, operator_id, params, with);
69			}
70
71			Err(Error::from(FlowGraphError::UnknownOperator {
72				operator: operator.to_string(),
73			}))
74		}
75
76		#[cfg(not(all(reifydb_target = "host", not(reifydb_dst))))]
77		{
78			Err(Error::from(FlowGraphError::ExternUnsupportedOnWasm))
79		}
80	}
81}
82
83#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
84impl StandardOperatorProvider {
85	#[instrument(name = "flow::provider::create_extern_c_operator", level = "debug", skip(self, params, with), fields(operator = %operator, operator_id = ?operator_id))]
86	fn create_extern_c_operator(
87		&self,
88		operator: &str,
89		operator_id: OperatorId,
90		params: &ExtensionParams,
91		with: &ApplyWith,
92	) -> Result<BoxedHostOperator> {
93		let loader = extern_c_operator_loader();
94		let mut loader_write = loader.write();
95
96		let named = Params::Named(Arc::new(params.iter().map(|(k, v)| (k.clone(), v.clone())).collect()));
97		let params_bytes = encode_params(&named).map_err(|e| {
98			Error::from(FlowStateError::Encode {
99				state: "operator params",
100				cause: e.to_string(),
101			})
102		})?;
103
104		let with_bytes = encode_apply_with(with)?;
105
106		let created = loader_write.create_operator_by_name(operator, operator_id, &params_bytes, &with_bytes);
107		let (descriptor, class, instance) = match created {
108			Ok(created) => created,
109			Err(e) => {
110				return Err(Error::from(ExternOperatorError::CreateFailed {
111					cause: format!("{:?}", e),
112				}));
113			}
114		};
115
116		Ok(Box::new(ExternCOperatorHandle::new(descriptor, class, instance, operator_id)))
117	}
118}