use alloc::vec::Vec;
use std::time::SystemTime;
use crate::contract::Contract;
use crate::error::Result;
use crate::model::{DurabilitySample, Page, Selector, StoreStats};
pub const DEFAULT_PAGE: usize = 1024;
pub trait DurabilityStore: Send + Sync {
fn set_contract(&self, topic: &str, contract: Contract) -> Result<()>;
fn store(&self, sample: DurabilitySample) -> Result<()>;
fn query(&self, topic: &str, selector: &Selector) -> Result<Page>;
fn unregister(&self, topic: &str, instance_key: &[u8; 16], now: SystemTime) -> Result<()>;
fn cleanup(&self, now: SystemTime) -> Result<usize>;
fn stats(&self, topic: &str) -> Result<StoreStats>;
fn replay_for_topic(&self, topic: &str) -> Result<Vec<DurabilitySample>> {
let mut out = Vec::new();
let mut sel = Selector {
limit: Some(DEFAULT_PAGE),
..Selector::default()
};
loop {
let page = self.query(topic, &sel)?;
let got = page.samples.len();
out.extend(page.samples);
match page.next {
Some(cursor) if got > 0 => sel = sel.after_cursor(cursor),
_ => break,
}
}
Ok(out)
}
}