reifydb_flow_operator_sdk/stateful/
mod.rs1pub mod keyed;
4mod raw;
5pub mod row;
6pub mod single;
7pub mod utils;
8pub mod window;
9
10pub use keyed::FFIKeyedStateful;
12use reifydb_core::value::encoded::{EncodedKey, EncodedValues};
13pub use row::RowNumberProvider;
14pub use single::FFISingleStateful;
15pub use utils::*;
16pub use window::FFIWindowStateful;
17
18use crate::{FFIOperator, context::OperatorContext, error::Result};
19
20pub struct State<'a> {
22 ctx: &'a mut OperatorContext,
23}
24
25impl<'a> State<'a> {
26 pub(crate) fn new(ctx: &'a mut OperatorContext) -> Self {
28 Self {
29 ctx,
30 }
31 }
32
33 pub fn get(&self, key: &EncodedKey) -> Result<Option<EncodedValues>> {
35 raw::raw_state_get(self.ctx, key)
36 }
37
38 pub fn set(&mut self, key: &EncodedKey, value: &EncodedValues) -> Result<()> {
40 raw::raw_state_set(self.ctx, key, value)
41 }
42
43 pub fn remove(&mut self, key: &EncodedKey) -> Result<()> {
45 raw::raw_state_remove(self.ctx, key)
46 }
47
48 pub fn contains(&self, key: &EncodedKey) -> Result<bool> {
50 Ok(raw::raw_state_get(self.ctx, key)?.is_some())
51 }
52
53 pub fn clear(&mut self) -> Result<()> {
55 raw::raw_state_clear(self.ctx)
56 }
57
58 pub fn scan_prefix(&self, prefix: &EncodedKey) -> Result<Vec<(EncodedKey, EncodedValues)>> {
60 raw::raw_state_prefix(self.ctx, prefix)
61 }
62
63 pub fn keys_with_prefix(&self, prefix: &EncodedKey) -> Result<Vec<EncodedKey>> {
65 let entries = self.scan_prefix(prefix)?;
66 Ok(entries.into_iter().map(|(k, _)| k).collect())
67 }
68}
69
70pub trait FFIRawStatefulOperator: FFIOperator {
88 fn state_get(&self, ctx: &mut OperatorContext, key: &EncodedKey) -> Result<Option<EncodedValues>> {
90 ctx.state().get(key)
91 }
92
93 fn state_set(&self, ctx: &mut OperatorContext, key: &EncodedKey, value: &EncodedValues) -> Result<()> {
95 ctx.state().set(key, value)
96 }
97
98 fn state_remove(&self, ctx: &mut OperatorContext, key: &EncodedKey) -> Result<()> {
100 ctx.state().remove(key)
101 }
102
103 fn state_scan_prefix(
105 &self,
106 ctx: &mut OperatorContext,
107 prefix: &EncodedKey,
108 ) -> Result<Vec<(EncodedKey, EncodedValues)>> {
109 ctx.state().scan_prefix(prefix)
110 }
111
112 fn state_keys_with_prefix(&self, ctx: &mut OperatorContext, prefix: &EncodedKey) -> Result<Vec<EncodedKey>> {
114 ctx.state().keys_with_prefix(prefix)
115 }
116
117 fn state_contains(&self, ctx: &mut OperatorContext, key: &EncodedKey) -> Result<bool> {
119 ctx.state().contains(key)
120 }
121
122 fn state_clear(&self, ctx: &mut OperatorContext) -> Result<()> {
124 ctx.state().clear()
125 }
126}