reifydb_flow_operator_sdk/
context.rs

1//! Operator context providing access to state and resources
2
3use reifydb_core::{EncodedKey, interface::FlowNodeId};
4use reifydb_flow_operator_abi::FFIContext;
5use reifydb_type::RowNumber;
6
7use crate::{
8	stateful::{RowNumberProvider, State},
9	store::Store,
10};
11
12/// Operator context providing access to state and other resources
13pub struct OperatorContext {
14	pub(crate) ctx: *mut FFIContext,
15}
16
17impl OperatorContext {
18	/// Create a new operator context from an FFI context pointer
19	///
20	/// # Safety
21	/// The caller must ensure ctx is non-null and valid for the lifetime of this context
22	pub fn new(ctx: *mut FFIContext) -> Self {
23		assert!(!ctx.is_null(), "FFIContext pointer must not be null");
24		Self {
25			ctx,
26		}
27	}
28
29	/// Get the operator ID from the FFI context
30	pub fn operator_id(&self) -> FlowNodeId {
31		unsafe { FlowNodeId((*self.ctx).operator_id) }
32	}
33
34	/// Get a state manager
35	pub fn state(&mut self) -> State<'_> {
36		State::new(self)
37	}
38
39	/// Get read-only access to the underlying store
40	pub fn store(&mut self) -> Store<'_> {
41		Store::new(self)
42	}
43
44	/// Get read-only access to the catalog
45	pub fn catalog(&mut self) -> crate::Catalog<'_> {
46		crate::Catalog::new(self)
47	}
48
49	/// Get or create a row number for a given key
50	///
51	/// This is a convenience method that creates a RowNumberProvider and
52	/// delegates to its `get_or_create_row_number` method.
53	///
54	/// Returns `(RowNumber, is_new)` where `is_new` indicates if this is
55	/// a newly created row number.
56	/// ```
57	pub fn get_or_create_row_number(&mut self, key: &EncodedKey) -> crate::Result<(RowNumber, bool)> {
58		let provider = RowNumberProvider::new(self.operator_id());
59		provider.get_or_create_row_number(self, key)
60	}
61}