Skip to main content

reifydb_flow_operator_abi/
callbacks.rs

1//! Host callback definitions for FFI operators
2
3use crate::types::*;
4
5/// Memory management callbacks
6#[repr(C)]
7#[derive(Clone, Copy)]
8pub struct MemoryCallbacks {
9	/// Allocate memory from the host
10	///
11	/// # Parameters
12	/// - `size`: Number of bytes to allocate
13	///
14	/// # Returns
15	/// - Pointer to allocated memory, or null on failure
16	pub alloc: extern "C" fn(size: usize) -> *mut u8,
17
18	/// Free memory previously allocated by alloc
19	///
20	/// # Parameters
21	/// - `ptr`: Pointer to memory to free
22	/// - `size`: Size of allocation (must match original alloc size)
23	pub free: extern "C" fn(ptr: *mut u8, size: usize),
24
25	/// Reallocate memory
26	///
27	/// # Parameters
28	/// - `ptr`: Current pointer (may be null)
29	/// - `old_size`: Current size (0 if ptr is null)
30	/// - `new_size`: Desired new size
31	///
32	/// # Returns
33	/// - Pointer to reallocated memory, or null on failure
34	pub realloc: extern "C" fn(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8,
35}
36
37/// State management callbacks
38#[repr(C)]
39#[derive(Clone, Copy)]
40pub struct StateCallbacks {
41	/// Get a value from operator state
42	///
43	/// # Parameters
44	/// - `operator_id`: Operator ID
45	/// - `ctx`: FFI context
46	/// - `key`: Key bytes
47	/// - `key_len`: Length of key
48	/// - `output`: Buffer to receive value
49	///
50	/// # Returns
51	/// - 0 if value exists and retrieved, 1 if key not found, negative on error
52	pub get: extern "C" fn(
53		operator_id: u64,
54		ctx: *mut FFIContext,
55		key: *const u8,
56		key_len: usize,
57		output: *mut BufferFFI,
58	) -> i32,
59
60	/// Set a value in operator state
61	///
62	/// # Parameters
63	/// - `operator_id`: Operator ID
64	/// - `ctx`: FFI context
65	/// - `key`: Key bytes
66	/// - `key_len`: Length of key
67	/// - `value`: Value bytes
68	/// - `value_len`: Length of value
69	///
70	/// # Returns
71	/// - 0 on success, negative error code on failure
72	pub set: extern "C" fn(
73		operator_id: u64,
74		ctx: *mut FFIContext,
75		key: *const u8,
76		key_len: usize,
77		value: *const u8,
78		value_len: usize,
79	) -> i32,
80
81	/// Remove a value from operator state
82	///
83	/// # Parameters
84	/// - `operator_id`: Operator ID
85	/// - `ctx`: FFI context
86	/// - `key`: Key bytes
87	/// - `key_len`: Length of key
88	///
89	/// # Returns
90	/// - 0 on success, negative error code on failure
91	pub remove: extern "C" fn(operator_id: u64, ctx: *mut FFIContext, key: *const u8, key_len: usize) -> i32,
92
93	/// Clear all state for an operator
94	///
95	/// # Parameters
96	/// - `operator_id`: Operator ID
97	/// - `ctx`: FFI context
98	///
99	/// # Returns
100	/// - 0 on success, negative error code on failure
101	pub clear: extern "C" fn(operator_id: u64, ctx: *mut FFIContext) -> i32,
102
103	/// Create an iterator for state keys with a given prefix
104	///
105	/// # Parameters
106	/// - `operator_id`: Operator ID
107	/// - `ctx`: FFI context
108	/// - `prefix`: Prefix bytes
109	/// - `prefix_len`: Length of prefix
110	/// - `iterator_out`: Pointer to receive iterator handle
111	///
112	/// # Returns
113	/// - 0 on success, negative error code on failure
114	pub prefix: extern "C" fn(
115		operator_id: u64,
116		ctx: *mut FFIContext,
117		prefix: *const u8,
118		prefix_len: usize,
119		iterator_out: *mut *mut StateIteratorFFI,
120	) -> i32,
121
122	/// Get the next key-value pair from a state iterator
123	///
124	/// # Parameters
125	/// - `iterator`: Iterator handle
126	/// - `key_out`: Buffer to receive key
127	/// - `value_out`: Buffer to receive value
128	///
129	/// # Returns
130	/// - 0 on success, 1 if end of iteration, negative on error
131	pub iterator_next: extern "C" fn(
132		iterator: *mut StateIteratorFFI,
133		key_out: *mut BufferFFI,
134		value_out: *mut BufferFFI,
135	) -> i32,
136
137	/// Free a state iterator
138	///
139	/// # Parameters
140	/// - `iterator`: Iterator to free
141	pub iterator_free: extern "C" fn(iterator: *mut StateIteratorFFI),
142}
143
144/// Logging callbacks
145#[repr(C)]
146#[derive(Clone, Copy)]
147pub struct LogCallbacks {
148	/// Log a message
149	///
150	/// # Parameters
151	/// - `operator_id`: Operator ID for identifying the logging operator
152	/// - `level`: Log level (0=trace, 1=debug, 2=info, 3=warn, 4=error)
153	/// - `message`: Message bytes
154	/// - `message_len`: Length of message in bytes
155	pub message: extern "C" fn(operator_id: u64, level: u32, message: *const u8, message_len: usize),
156}
157
158/// Store access callbacks (read-only access to underlying store)
159#[repr(C)]
160#[derive(Clone, Copy)]
161pub struct StoreCallbacks {
162	/// Get a value from store by key
163	///
164	/// # Parameters
165	/// - `ctx`: FFI context
166	/// - `key`: Key bytes
167	/// - `key_len`: Length of key
168	/// - `output`: Buffer to receive value
169	///
170	/// # Returns
171	/// - 0 if value exists and retrieved, 1 if key not found, negative on error
172	pub get: extern "C" fn(ctx: *mut FFIContext, key: *const u8, key_len: usize, output: *mut BufferFFI) -> i32,
173
174	/// Check if a key exists in store
175	///
176	/// # Parameters
177	/// - `ctx`: FFI context
178	/// - `key`: Key bytes
179	/// - `key_len`: Length of key
180	/// - `result`: Pointer to receive result (1 if exists, 0 if not)
181	///
182	/// # Returns
183	/// - 0 on success, negative error code on failure
184	pub contains_key: extern "C" fn(ctx: *mut FFIContext, key: *const u8, key_len: usize, result: *mut u8) -> i32,
185
186	/// Create an iterator for store keys with a given prefix
187	///
188	/// # Parameters
189	/// - `ctx`: FFI context
190	/// - `prefix`: Prefix bytes
191	/// - `prefix_len`: Length of prefix
192	/// - `iterator_out`: Pointer to receive iterator handle
193	///
194	/// # Returns
195	/// - 0 on success, negative error code on failure
196	pub prefix: extern "C" fn(
197		ctx: *mut FFIContext,
198		prefix: *const u8,
199		prefix_len: usize,
200		iterator_out: *mut *mut StoreIteratorFFI,
201	) -> i32,
202
203	/// Create an iterator for store keys in a range
204	///
205	/// # Parameters
206	/// - `ctx`: FFI context
207	/// - `start`: Start key bytes
208	/// - `start_len`: Length of start key
209	/// - `start_bound_type`: Bound type for start (0=Unbounded, 1=Included, 2=Excluded)
210	/// - `end`: End key bytes
211	/// - `end_len`: Length of end key
212	/// - `end_bound_type`: Bound type for end (0=Unbounded, 1=Included, 2=Excluded)
213	/// - `iterator_out`: Pointer to receive iterator handle
214	///
215	/// # Returns
216	/// - 0 on success, negative error code on failure
217	pub range: extern "C" fn(
218		ctx: *mut FFIContext,
219		start: *const u8,
220		start_len: usize,
221		start_bound_type: u8,
222		end: *const u8,
223		end_len: usize,
224		end_bound_type: u8,
225		iterator_out: *mut *mut StoreIteratorFFI,
226	) -> i32,
227
228	/// Get the next key-value pair from a store iterator
229	///
230	/// # Parameters
231	/// - `iterator`: Iterator handle
232	/// - `key_out`: Buffer to receive key
233	/// - `value_out`: Buffer to receive value
234	///
235	/// # Returns
236	/// - 0 on success, 1 if end of iteration, negative on error
237	pub iterator_next: extern "C" fn(
238		iterator: *mut StoreIteratorFFI,
239		key_out: *mut BufferFFI,
240		value_out: *mut BufferFFI,
241	) -> i32,
242
243	/// Free a store iterator
244	///
245	/// # Parameters
246	/// - `iterator`: Iterator to free
247	pub iterator_free: extern "C" fn(iterator: *mut StoreIteratorFFI),
248}
249
250/// Catalog access callbacks (read-only access to catalog system)
251#[repr(C)]
252#[derive(Clone, Copy)]
253pub struct CatalogCallbacks {
254	/// Find a namespace by ID at specific version
255	///
256	/// # Parameters
257	/// - `ctx`: FFI context
258	/// - `namespace_id`: Namespace ID
259	/// - `version`: Commit version for time-travel queries
260	/// - `output`: Pointer to receive namespace definition
261	///
262	/// # Returns
263	/// - 0 if namespace exists, 1 if not found, negative on error
264	pub find_namespace: extern "C" fn(
265		ctx: *mut FFIContext,
266		namespace_id: u64,
267		version: u64,
268		output: *mut crate::ffi::FFINamespaceDef,
269	) -> i32,
270
271	/// Find a namespace by name at specific version
272	///
273	/// # Parameters
274	/// - `ctx`: FFI context
275	/// - `name_ptr`: Namespace name bytes (UTF-8)
276	/// - `name_len`: Length of name
277	/// - `version`: Commit version for time-travel queries
278	/// - `output`: Pointer to receive namespace definition
279	///
280	/// # Returns
281	/// - 0 if namespace exists, 1 if not found, negative on error
282	pub find_namespace_by_name: extern "C" fn(
283		ctx: *mut FFIContext,
284		name_ptr: *const u8,
285		name_len: usize,
286		version: u64,
287		output: *mut crate::ffi::FFINamespaceDef,
288	) -> i32,
289
290	/// Find a table by ID at specific version
291	///
292	/// # Parameters
293	/// - `ctx`: FFI context
294	/// - `table_id`: Table ID
295	/// - `version`: Commit version for time-travel queries
296	/// - `output`: Pointer to receive table definition
297	///
298	/// # Returns
299	/// - 0 if table exists, 1 if not found, negative on error
300	pub find_table: extern "C" fn(
301		ctx: *mut FFIContext,
302		table_id: u64,
303		version: u64,
304		output: *mut crate::ffi::FFITableDef,
305	) -> i32,
306
307	/// Find a table by name in a namespace at specific version
308	///
309	/// # Parameters
310	/// - `ctx`: FFI context
311	/// - `namespace_id`: Namespace ID
312	/// - `name_ptr`: Table name bytes (UTF-8)
313	/// - `name_len`: Length of name
314	/// - `version`: Commit version for time-travel queries
315	/// - `output`: Pointer to receive table definition
316	///
317	/// # Returns
318	/// - 0 if table exists, 1 if not found, negative on error
319	pub find_table_by_name: extern "C" fn(
320		ctx: *mut FFIContext,
321		namespace_id: u64,
322		name_ptr: *const u8,
323		name_len: usize,
324		version: u64,
325		output: *mut crate::ffi::FFITableDef,
326	) -> i32,
327
328	/// Free a namespace definition allocated by the host
329	///
330	/// # Parameters
331	/// - `namespace`: Pointer to namespace to free
332	pub free_namespace: extern "C" fn(namespace: *mut crate::ffi::FFINamespaceDef),
333
334	/// Free a table definition allocated by the host
335	///
336	/// # Parameters
337	/// - `table`: Pointer to table to free
338	pub free_table: extern "C" fn(table: *mut crate::ffi::FFITableDef),
339}
340
341/// Host-provided callbacks for FFI operators
342///
343/// These callbacks allow operators to request services from the host system
344#[repr(C)]
345#[derive(Clone, Copy)]
346pub struct HostCallbacks {
347	pub memory: MemoryCallbacks,
348	pub state: StateCallbacks,
349	pub log: LogCallbacks,
350	pub store: StoreCallbacks,
351	pub catalog: CatalogCallbacks,
352}