pub mod disk;
pub use disk::{DiskStorage, DiskStorageError};
use std::future::Future;
use std::collections::BTreeSet;
use yggr_core::{LogEntry, LogIndex, NodeId, Term};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoredHardState {
pub current_term: Term,
pub voted_for: Option<NodeId>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoredSnapshot {
pub last_included_index: LogIndex,
pub last_included_term: Term,
pub peers: BTreeSet<NodeId>,
pub bytes: Vec<u8>,
}
#[derive(Debug, Clone, Default)]
pub struct RecoveredState<C> {
pub hard_state: Option<StoredHardState>,
pub snapshot: Option<StoredSnapshot>,
pub log: Vec<LogEntry<C>>,
}
pub trait Storage<C>: Send + 'static
where
C: Send + 'static,
{
type Error: std::error::Error + Send + Sync + 'static;
fn recover(&mut self) -> impl Future<Output = Result<RecoveredState<C>, Self::Error>> + Send;
fn persist_hard_state(
&mut self,
state: StoredHardState,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn append_log(
&mut self,
entries: Vec<LogEntry<C>>,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn truncate_log(
&mut self,
from: LogIndex,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn persist_snapshot(
&mut self,
snapshot: StoredSnapshot,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
}