use reifydb_core::encoded::{encoded::EncodedValues, key::EncodedKey, schema::Schema};
use super::{FFIRawStatefulOperator, utils};
use crate::{error::Result, operator::context::OperatorContext};
pub trait FFIWindowStateful: FFIRawStatefulOperator {
fn schema(&self) -> Schema;
fn create_state(&self) -> EncodedValues {
let schema = self.schema();
schema.allocate()
}
fn load_state(&self, ctx: &mut OperatorContext, window_key: &EncodedKey) -> Result<EncodedValues> {
utils::load_or_create_row(ctx, window_key, &self.schema())
}
fn save_state(&self, ctx: &mut OperatorContext, window_key: &EncodedKey, row: &EncodedValues) -> Result<()> {
utils::save_row(ctx, window_key, row)
}
fn remove_window(&self, ctx: &mut OperatorContext, window_key: &EncodedKey) -> Result<()> {
self.state_remove(ctx, window_key)
}
fn update_window<F>(&self, ctx: &mut OperatorContext, window_key: &EncodedKey, f: F) -> Result<EncodedValues>
where
F: FnOnce(&Schema, &mut EncodedValues) -> Result<()>,
{
let schema = self.schema();
let mut row = self.load_state(ctx, window_key)?;
f(&schema, &mut row)?;
self.save_state(ctx, window_key, &row)?;
Ok(row)
}
}