reifydb_flow_operator_sdk/
lib.rs

1//! ReifyDB Operator SDK
2
3use std::collections::HashMap;
4
5use reifydb_core::{
6	CommitVersion, Row,
7	interface::{FlowNodeId, SourceId},
8};
9use reifydb_type::{RowNumber, Value};
10
11pub mod change;
12pub mod context;
13pub mod error;
14pub mod ffi;
15pub mod marshal;
16pub mod stateful;
17pub mod testing;
18
19pub use change::FlowChangeBuilder;
20pub use context::OperatorContext;
21pub use error::{FFIError, Result};
22pub use reifydb_core::{
23	CowVec,
24	key::EncodableKey,
25	value::encoded::{EncodedKey, EncodedValues},
26};
27pub use stateful::State;
28
29/// Origin of a flow change
30#[derive(Debug, Clone)]
31pub enum FlowChangeOrigin {
32	/// Change originated from an external source (table, view, ring buffer)
33	External(SourceId),
34	/// Change originated from an internal flow node
35	Internal(FlowNodeId),
36}
37
38/// Represents a single diff in a flow change
39#[derive(Debug, Clone)]
40pub enum FlowDiff {
41	/// Insert a new row
42	Insert {
43		/// The row to insert
44		post: Row,
45	},
46	/// Update an existing row
47	Update {
48		/// The previous value
49		pre: Row,
50		/// The new value
51		post: Row,
52	},
53	/// Remove an existing row
54	Remove {
55		/// The row to remove
56		pre: Row,
57	},
58}
59
60/// Represents a flow change with insertions, updates, and deletions
61#[derive(Debug, Clone)]
62pub struct FlowChange {
63	/// Origin of this change
64	pub origin: FlowChangeOrigin,
65	/// The list of diffs (changes) in this flow change
66	pub diffs: Vec<FlowDiff>,
67	/// Version of this change
68	pub version: CommitVersion,
69}
70
71impl FlowChange {
72	/// Create a flow change from an external source
73	pub fn external(source: SourceId, version: CommitVersion, diffs: Vec<FlowDiff>) -> Self {
74		Self {
75			origin: FlowChangeOrigin::External(source),
76			diffs,
77			version,
78		}
79	}
80
81	/// Create a flow change from an internal flow node
82	pub fn internal(from: FlowNodeId, version: CommitVersion, diffs: Vec<FlowDiff>) -> Self {
83		Self {
84			origin: FlowChangeOrigin::Internal(from),
85			diffs,
86			version,
87		}
88	}
89}
90
91/// Static metadata about an operator type
92/// This trait provides compile-time constant metadata
93pub trait FFIOperatorMetadata {
94	/// Operator name (must be unique within a library)
95	const NAME: &'static str;
96	/// Operator version
97	const VERSION: u32;
98}
99
100/// Runtime operator behavior
101/// Operators must be Send + Sync for thread safety
102pub trait FFIOperator: Send + Sync + 'static {
103	/// Create a new operator instance with the operator ID and configuration
104	fn new(operator_id: FlowNodeId, config: &HashMap<String, Value>) -> Result<Self>
105	where
106		Self: Sized;
107
108	/// Process a flow change (inserts, updates, removes)
109	fn apply(&mut self, ctx: &mut OperatorContext, input: FlowChange) -> Result<FlowChange>;
110
111	/// Get specific rows by row number
112	fn get_rows(&mut self, ctx: &mut OperatorContext, row_numbers: &[RowNumber]) -> Result<Vec<Option<Row>>>;
113}
114
115pub trait FFIOperatorWithMetadata: FFIOperator + FFIOperatorMetadata {}
116impl<T> FFIOperatorWithMetadata for T where T: FFIOperator + FFIOperatorMetadata {}
117
118// Prelude module for convenient imports
119pub mod prelude {
120	pub use reifydb_core::{
121		CowVec, Row,
122		key::EncodableKey,
123		value::encoded::{EncodedKey, EncodedValues},
124	};
125	pub use reifydb_type::{RowNumber, Value};
126
127	pub use crate::{
128		FFIOperator, FFIOperatorMetadata, FFIOperatorWithMetadata, FlowChange, FlowChangeBuilder,
129		FlowChangeOrigin, FlowDiff,
130		context::OperatorContext,
131		error::{FFIError, Result},
132		stateful::State,
133	};
134}