use crate::{
backend::NotificationStream,
model::{ConsumerGroupStatus, Event, NewEvent},
};
use async_trait::async_trait;
#[async_trait]
pub trait StreamBackend: Send + Sync {
async fn publish(&self, stream: &str, event: NewEvent) -> anyhow::Result<i64>;
async fn subscribe_stream(
&self,
stream: &str,
consumer_group: &str,
last_seq: Option<i64>,
) -> anyhow::Result<NotificationStream>;
async fn ack(&self, stream: &str, consumer_group: &str, seq: i64) -> anyhow::Result<()>;
async fn read_events(
&self,
stream: &str,
after_seq: i64,
limit: i64,
) -> anyhow::Result<Vec<Event>>;
async fn read_next(
&self,
stream: &str,
consumer_group: &str,
limit: i64,
) -> anyhow::Result<Vec<Event>> {
let last_acked_seq = self
.consumer_group_info(stream)
.await?
.into_iter()
.find(|status| status.consumer_group == consumer_group)
.map(|status| status.last_acked_seq)
.unwrap_or(0);
self.read_events(stream, last_acked_seq, limit).await
}
async fn prune_events(&self, stream: &str, through_seq: i64) -> anyhow::Result<u64>;
async fn consumer_group_info(&self, stream: &str) -> anyhow::Result<Vec<ConsumerGroupStatus>>;
}