use crate::checkpoint::CheckpointContext;
use crate::context::RuntimeContext;
use crate::error::StreamResult;
use async_trait::async_trait;
use std::collections::HashMap;
#[async_trait]
pub trait LifecycleAware {
async fn open(&mut self, _ctx: &mut RuntimeContext) -> StreamResult<()> {
Ok(())
}
async fn close(&mut self, _ctx: &mut RuntimeContext) -> StreamResult<()> {
Ok(())
}
async fn on_checkpoint(
&mut self,
_runtime: &mut RuntimeContext,
_ckpt: &CheckpointContext,
) -> StreamResult<()> {
Ok(())
}
async fn on_checkpoint_end(
&mut self,
_runtime: &mut RuntimeContext,
_ckpt: &CheckpointContext,
) -> StreamResult<()> {
Ok(())
}
fn report_metrics(&self) -> HashMap<String, f64> {
HashMap::new()
}
}
#[macro_export]
macro_rules! lifecycle {
($t:ty) => {
impl $crate::functions::LifecycleAware for $t {}
};
}
#[async_trait]
pub trait MapFunction<T, O>: LifecycleAware {
async fn map(&mut self, input: T, runtime: &mut RuntimeContext) -> StreamResult<O>;
}
#[async_trait]
pub trait FilterFunction<T>: LifecycleAware {
async fn filter(&mut self, input: &T, runtime: &mut RuntimeContext) -> StreamResult<bool>;
}
#[async_trait]
pub trait FlatMapFunction<T, O>: LifecycleAware {
async fn flat_map(&mut self, input: T, runtime: &mut RuntimeContext) -> StreamResult<Vec<O>>;
}
#[async_trait]
pub trait SinkFunction<T>: LifecycleAware {
async fn accept(&mut self, input: T, runtime: &mut RuntimeContext) -> StreamResult<()>;
}
#[async_trait]
pub trait SourceFunction<T: Send>: LifecycleAware {
async fn poll_next(&mut self) -> StreamResult<Option<T>>;
}