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