use crate::error::Result;
use crate::lock_manager::LockManager;
use crate::types::{BackupInfo, CanonicalMeta, CommitInfo, EventId};
use std::path::Path;
#[derive(Debug, Clone)]
pub struct PreflightResult {
pub valid: bool,
pub errors: Vec<String>,
pub read_keys: Vec<Vec<u8>>,
pub write_keys: Vec<Vec<u8>>,
}
impl PreflightResult {
pub fn success() -> Self {
Self {
valid: true,
errors: Vec::new(),
read_keys: Vec::new(),
write_keys: Vec::new(),
}
}
pub fn failure(error: String) -> Self {
Self {
valid: false,
errors: vec![error],
read_keys: Vec::new(),
write_keys: Vec::new(),
}
}
pub fn with_keys(mut self, read_keys: Vec<Vec<u8>>, write_keys: Vec<Vec<u8>>) -> Self {
self.read_keys = read_keys;
self.write_keys = write_keys;
self
}
}
pub trait EventIter: Send {
fn next(&mut self) -> Result<Option<(EventId, Vec<u8>)>>;
}
pub trait StateIter {
fn next(&mut self) -> Result<Option<(Vec<u8>, Vec<u8>)>>;
}
pub trait CanonicalReadTxn {
fn get_state(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn exists(&self, key: &[u8]) -> Result<bool> {
Ok(self.get_state(key)?.is_some())
}
}
pub trait CanonicalTxn {
fn preflight(&mut self) -> Result<PreflightResult> {
Ok(PreflightResult::success())
}
fn get_state(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn put_state(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
fn del_state(&mut self, key: &[u8]) -> Result<()>;
fn iter_state(&self) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
Ok(Vec::new())
}
fn append_event(&mut self, event: &[u8]) -> Result<EventId>;
fn append_events(&mut self, events: &[Vec<u8>]) -> Result<(EventId, EventId)>;
fn commit(self) -> Result<CommitInfo>;
fn abort(self);
}
pub trait CanonicalStore: Send + Sync {
type Txn<'a>: CanonicalTxn
where
Self: 'a;
type ReadTxn<'a>: CanonicalReadTxn
where
Self: 'a;
fn open(cfg: crate::config::CanonicalConfig) -> Result<Self>
where
Self: Sized;
fn close(&self) -> Result<()>;
fn read_txn(&self) -> Result<Self::ReadTxn<'_>>;
fn write_txn(&self) -> Result<Self::Txn<'_>>;
fn iter_events(&self, from: EventId, to: Option<EventId>) -> Result<Box<dyn EventIter>>;
fn range(&self, start: &[u8], end: Option<&[u8]>) -> Result<Box<dyn StateIter>>;
fn scan_prefix(&self, prefix: &[u8]) -> Result<Box<dyn StateIter>>;
fn seal(&self) -> Result<EventId>;
fn lock_manager(&self) -> &LockManager;
fn pause_ingestion(&self) -> Result<()>;
fn resume_ingestion(&self) -> Result<()>;
fn is_paused(&self) -> bool;
fn backup_to(&self, dir: &Path) -> Result<BackupInfo>;
fn restore_from(dir: &Path, cfg: crate::config::CanonicalConfig) -> Result<Self>
where
Self: Sized;
fn meta(&self) -> Result<CanonicalMeta>;
}