1use std::{collections::HashMap, path::PathBuf, sync::Arc};
5
6#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
7use reifydb_core::interface::flow::to_bitmask;
8use reifydb_core::{
9 common::{OperatorClass, WindowRequirements},
10 event::operator::OperatorColumn,
11 interface::catalog::flow::OperatorId,
12 operator_with::ApplyWith,
13};
14use reifydb_flow_async::operator::BoxedHostOperator;
15#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
16use reifydb_sdk::flow::operator::mount::mount;
17#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
18use reifydb_sdk::flow::operator::{
19 ManagedMount, ManagedOperator, MountedOperator, NostateMount, NostateOperator, UnmanagedMount,
20 UnmanagedOperator,
21 context::{ClassValue, Windowed},
22 windowed::operator::WindowDriver,
23};
24#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
25use reifydb_sdk::flow::operator::{OperatorMetadata, column::operator::OperatorColumn as SdkOperatorColumn};
26use reifydb_value::{Result, config::ExtensionParams};
27
28pub(crate) type OperatorFactory =
29 Arc<dyn Fn(OperatorId, &ExtensionParams, &ApplyWith) -> Result<BoxedHostOperator> + Send + Sync>;
30
31#[derive(Clone)]
32pub struct CustomOperatorEntry {
33 pub factory: OperatorFactory,
34 pub abi: Option<u32>,
35 pub version: String,
36 pub description: String,
37 pub capabilities: u32,
38 pub input: Vec<OperatorColumn>,
39 pub output: Vec<OperatorColumn>,
40 pub class: OperatorClass,
41 pub window: WindowRequirements,
42 pub unmanaged_because: Option<String>,
43}
44
45#[derive(Clone, Default)]
46pub struct CustomOperators {
47 inner: Arc<HashMap<String, CustomOperatorEntry>>,
48}
49
50impl CustomOperators {
51 pub(crate) fn new(map: HashMap<String, CustomOperatorEntry>) -> Self {
52 Self {
53 inner: Arc::new(map),
54 }
55 }
56
57 pub(crate) fn get(&self, name: &str) -> Option<&OperatorFactory> {
58 self.inner.get(name).map(|entry| &entry.factory)
59 }
60
61 pub(crate) fn iter(&self) -> impl Iterator<Item = (&String, &CustomOperatorEntry)> {
62 self.inner.iter()
63 }
64}
65
66#[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
67fn describe_columns(columns: &[SdkOperatorColumn]) -> Vec<OperatorColumn> {
68 columns.iter()
69 .map(|column| OperatorColumn {
70 name: column.name.to_string(),
71 field_type: column.type_constraint.clone(),
72 description: column.description.to_string(),
73 })
74 .collect()
75}
76
77pub struct FlowConfigurator {
78 operators_dir: Option<PathBuf>,
79 custom_operators: HashMap<String, CustomOperatorEntry>,
80}
81
82impl Default for FlowConfigurator {
83 fn default() -> Self {
84 Self::new()
85 }
86}
87
88impl FlowConfigurator {
89 pub fn new() -> Self {
90 Self {
91 operators_dir: None,
92 custom_operators: HashMap::new(),
93 }
94 }
95
96 pub fn operators_dir(mut self, path: PathBuf) -> Self {
97 self.operators_dir = Some(path);
98 self
99 }
100
101 #[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
102 pub fn register_managed_operator<T>(self) -> Self
103 where
104 T: ManagedOperator + 'static,
105 {
106 self.register_mounted::<ManagedMount<T>>()
107 }
108
109 #[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
110 pub fn register_unmanaged_operator<T>(self) -> Self
111 where
112 T: UnmanagedOperator + 'static,
113 {
114 self.register_mounted::<UnmanagedMount<T>>()
115 }
116
117 #[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
118 pub fn register_nostate_operator<T>(self) -> Self
119 where
120 T: NostateOperator + 'static,
121 {
122 self.register_mounted::<NostateMount<T>>()
123 }
124
125 #[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
126 pub fn register_windowed_operator<T, M>(self) -> Self
127 where
128 T: WindowDriver<M> + 'static,
129 <T as WindowDriver<M>>::Driver: MountedOperator<Class = Windowed> + OperatorMetadata + 'static,
130 {
131 self.register_mounted::<<T as WindowDriver<M>>::Driver>()
132 }
133
134 #[cfg(all(reifydb_target = "host", not(reifydb_dst)))]
135 fn register_mounted<M>(mut self) -> Self
136 where
137 M: MountedOperator + OperatorMetadata + 'static,
138 {
139 self.custom_operators.insert(
140 M::NAME.to_string(),
141 CustomOperatorEntry {
142 factory: Arc::new(|operator, params, with| {
143 let logic = M::create(operator, params, with)?;
144 Ok(mount(logic, operator, M::CAPABILITIES))
145 }),
146 abi: None,
147 version: <M as OperatorMetadata>::VERSION.to_string(),
148 description: <M as OperatorMetadata>::DESCRIPTION.to_string(),
149 capabilities: to_bitmask(<M as OperatorMetadata>::CAPABILITIES),
150 input: describe_columns(<M as OperatorMetadata>::INPUT_COLUMNS),
151 output: describe_columns(<M as OperatorMetadata>::OUTPUT_COLUMNS),
152 class: <M::Class as ClassValue>::CLASS,
153 window: <M as MountedOperator>::WINDOW,
154 unmanaged_because: <M as MountedOperator>::UNMANAGED_BECAUSE.map(str::to_string),
155 },
156 );
157 self
158 }
159
160 pub(crate) fn configure(self) -> FlowConfig {
161 FlowConfig {
162 operators_dir: self.operators_dir,
163 custom_operators: CustomOperators::new(self.custom_operators),
164 }
165 }
166}
167
168pub struct FlowConfig {
169 pub operators_dir: Option<PathBuf>,
170
171 pub custom_operators: CustomOperators,
172}