use std::pin::Pin;
use std::time::Duration;
use async_trait::async_trait;
use futures::stream::Stream;
use serde_json::Value;
use crate::error::Result;
use crate::models::Event;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StorageCapabilities {
pub supports_get_event: bool,
pub max_replay_window: Option<Duration>,
pub telemetry_label: &'static str,
}
impl StorageCapabilities {
#[must_use]
pub const fn mem() -> Self {
Self {
supports_get_event: true,
max_replay_window: None,
telemetry_label: "mem",
}
}
#[must_use]
pub const fn sqlite() -> Self {
Self {
supports_get_event: true,
max_replay_window: None,
telemetry_label: "sqlite",
}
}
#[must_use]
pub const fn broker(label: &'static str) -> Self {
Self {
supports_get_event: false,
max_replay_window: Some(Duration::from_mins(15)),
telemetry_label: label,
}
}
}
#[async_trait]
pub trait StoragePort: Send + Sync {
fn capabilities(&self) -> StorageCapabilities;
async fn append(
&self,
topic_name: &str,
topic_key: Option<&str>,
actor_json: Value,
payload_json: Value,
) -> Result<Event>;
fn subscribe(
&self,
topic_name: String,
topic_key_filter: Option<String>,
after_seq: Option<i64>,
) -> Pin<Box<dyn Stream<Item = Result<Event>> + Send>>;
async fn get_event(&self, event_id: &str) -> Result<Option<Event>>;
async fn load_checkpoint(
&self,
subscription_name: &str,
topic_name: &str,
topic_key: Option<&str>,
) -> Result<Option<i64>>;
async fn commit_checkpoint(
&self,
subscription_name: &str,
topic_name: &str,
topic_key: Option<&str>,
last_seq: i64,
) -> Result<()>;
async fn truncate_before(
&self,
topic_name: &str,
topic_key: Option<&str>,
truncate_bound: i64,
) -> Result<u64> {
let _ = (topic_name, topic_key, truncate_bound);
Ok(0)
}
async fn delivery_seq_pin(
&self,
topic_name: &str,
topic_key: Option<&str>,
) -> Option<i64> {
let _ = (topic_name, topic_key);
None
}
}