use std::io;
use crate::format::BlockDecoder;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AppendOptions {
pub durable: bool,
}
impl Default for AppendOptions {
fn default() -> Self {
Self { durable: true }
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReadOrder {
Forward,
Backward,
}
pub trait AppendLogVisitor {
fn visit(&mut self, block: &[u8], context: BlockVisitContext) -> io::Result<bool>;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockVisitContext {
pub is_first_in_structure: bool,
pub is_last_in_structure: bool,
}
pub trait Layout: Send + Sync {
type StructureId: Clone + Ord + Send + Sync + 'static;
type Name: Clone + Send + Sync + 'static;
fn active_name(&self, structure_id: &Self::StructureId) -> Self::Name;
fn closed_name(&self, structure_id: &Self::StructureId) -> Self::Name;
fn archive_name(&self, structure_id: &Self::StructureId) -> Self::Name;
fn parse_active_name(&self, name: &Self::Name) -> Option<Self::StructureId>;
fn parse_closed_name(&self, name: &Self::Name) -> Option<Self::StructureId>;
fn parse_archive_name(&self, name: &Self::Name) -> Option<Self::StructureId>;
}
pub trait AppendLog: Send + Sync {
type Block: AsRef<[u8]> + Clone + Send + Sync + 'static;
type Closed: Clone + Send + Sync + 'static;
async fn append(&self, block: Self::Block, options: AppendOptions) -> io::Result<u64>;
async fn rotate(&self) -> io::Result<Option<Self::Closed>>;
async fn archive(&self, closed: Self::Closed) -> io::Result<()>;
}
pub struct BuildResult<S>
where
S: AppendLog,
{
pub storage: S,
pub recovered_closed: Vec<S::Closed>,
}
pub trait AppendLogBuilder: Send {
type Storage: AppendLog;
async fn build<D, V>(
self,
decoder: &D,
order: ReadOrder,
visitor: &mut V,
) -> io::Result<BuildResult<Self::Storage>>
where
D: BlockDecoder<Block = <Self::Storage as AppendLog>::Block> + Send + Sync,
V: AppendLogVisitor + Send;
}