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