Skip to main content

reifydb_sdk/operator/
context.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_abi::context::context::ContextFFI;
5use reifydb_core::{encoded::key::EncodedKey, interface::catalog::flow::FlowNodeId};
6use reifydb_type::{
7	params::Params,
8	value::{frame::frame::Frame, row_number::RowNumber},
9};
10
11use crate::{
12	catalog::Catalog,
13	error::Result,
14	operator::{builder::ColumnsBuilder, diff::DiffStart},
15	rql::raw_rql,
16	state::{State, row::RowNumberProvider},
17	store::Store,
18};
19
20pub struct OperatorContext {
21	pub(crate) ctx: *mut ContextFFI,
22}
23
24impl OperatorContext {
25	pub fn new(ctx: *mut ContextFFI) -> Self {
26		assert!(!ctx.is_null(), "ContextFFI pointer must not be null");
27		Self {
28			ctx,
29		}
30	}
31
32	pub fn operator_id(&self) -> FlowNodeId {
33		unsafe { FlowNodeId((*self.ctx).operator_id) }
34	}
35
36	pub fn state(&mut self) -> State<'_> {
37		State::new(self)
38	}
39
40	pub fn store(&mut self) -> Store<'_> {
41		Store::new(self)
42	}
43
44	pub fn catalog(&mut self) -> Catalog<'_> {
45		Catalog::new(self)
46	}
47
48	pub fn get_or_create_row_number(&mut self, key: &EncodedKey) -> Result<(RowNumber, bool)> {
49		let provider = RowNumberProvider::new(self.operator_id());
50		provider.get_or_create_row_number(self, key)
51	}
52
53	pub fn get_or_create_row_numbers(&mut self, keys: &[EncodedKey]) -> Result<Vec<RowNumber>> {
54		let provider = RowNumberProvider::new(self.operator_id());
55		Ok(provider.get_or_create_row_numbers_batch(self, keys.iter())?.into_iter().map(|(rn, _)| rn).collect())
56	}
57
58	pub fn rql(&self, rql: &str, params: Params) -> Result<Vec<Frame>> {
59		raw_rql(self, rql, params)
60	}
61
62	pub fn builder(&mut self) -> ColumnsBuilder<'_> {
63		ColumnsBuilder::new(self)
64	}
65
66	pub fn diff(&mut self) -> DiffStart<'_> {
67		DiffStart::new(self)
68	}
69}