pub(crate) mod internal_storage;
mod state_cache;
use super::ballot_leader_election::Ballot;
#[cfg(feature = "unicache")]
use crate::unicache::*;
use crate::ClusterConfig;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{error::Error, fmt::Debug};
pub trait Entry: Clone + Debug {
#[cfg(not(feature = "serde"))]
type Snapshot: Snapshot<Self>;
#[cfg(feature = "serde")]
type Snapshot: Snapshot<Self> + Serialize + for<'a> Deserialize<'a>;
#[cfg(feature = "unicache")]
type Encoded: Encoded;
#[cfg(feature = "unicache")]
type Encodable: Encodable;
#[cfg(feature = "unicache")]
type NotEncodable: NotEncodable;
#[cfg(all(feature = "unicache", not(feature = "serde")))]
type EncodeResult: Clone + Debug;
#[cfg(all(feature = "unicache", feature = "serde"))]
type EncodeResult: Clone + Debug + Serialize + for<'a> Deserialize<'a>;
#[cfg(all(feature = "unicache", not(feature = "serde")))]
type UniCache: UniCache<T = Self>;
#[cfg(all(feature = "unicache", feature = "serde"))]
type UniCache: UniCache<T = Self> + Serialize + for<'a> Deserialize<'a>;
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StopSign {
pub next_config: ClusterConfig,
pub metadata: Option<Vec<u8>>,
}
impl StopSign {
pub fn with(next_config: ClusterConfig, metadata: Option<Vec<u8>>) -> Self {
StopSign {
next_config,
metadata,
}
}
}
#[allow(missing_docs)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SnapshotType<T>
where
T: Entry,
{
Complete(T::Snapshot),
Delta(T::Snapshot),
}
pub trait Snapshot<T>: Clone + Debug
where
T: Entry,
{
fn create(entries: &[T]) -> Self;
fn merge(&mut self, delta: Self);
fn use_snapshots() -> bool;
}
pub type StorageResult<T> = Result<T, Box<dyn Error>>;
#[derive(Debug)]
pub enum StorageOp<T: Entry> {
AppendEntry(T),
AppendEntries(Vec<T>),
AppendOnPrefix(usize, Vec<T>),
SetPromise(Ballot),
SetDecidedIndex(usize),
SetAcceptedRound(Ballot),
SetCompactedIdx(usize),
Trim(usize),
SetStopsign(Option<StopSign>),
SetSnapshot(Option<T::Snapshot>),
}
pub trait Storage<T>
where
T: Entry,
{
fn write_atomically(&mut self, ops: Vec<StorageOp<T>>) -> StorageResult<()>;
fn append_entry(&mut self, entry: T) -> StorageResult<()>;
fn append_entries(&mut self, entries: Vec<T>) -> StorageResult<()>;
fn append_on_prefix(&mut self, from_idx: usize, entries: Vec<T>) -> StorageResult<()>;
fn set_promise(&mut self, n_prom: Ballot) -> StorageResult<()>;
fn set_decided_idx(&mut self, ld: usize) -> StorageResult<()>;
fn get_decided_idx(&self) -> StorageResult<usize>;
fn set_accepted_round(&mut self, na: Ballot) -> StorageResult<()>;
fn get_accepted_round(&self) -> StorageResult<Option<Ballot>>;
fn get_entries(&self, from: usize, to: usize) -> StorageResult<Vec<T>>;
fn get_log_len(&self) -> StorageResult<usize>;
fn get_suffix(&self, from: usize) -> StorageResult<Vec<T>>;
fn get_promise(&self) -> StorageResult<Option<Ballot>>;
fn set_stopsign(&mut self, s: Option<StopSign>) -> StorageResult<()>;
fn get_stopsign(&self) -> StorageResult<Option<StopSign>>;
fn trim(&mut self, idx: usize) -> StorageResult<()>;
fn set_compacted_idx(&mut self, idx: usize) -> StorageResult<()>;
fn get_compacted_idx(&self) -> StorageResult<usize>;
fn set_snapshot(&mut self, snapshot: Option<T::Snapshot>) -> StorageResult<()>;
fn get_snapshot(&self) -> StorageResult<Option<T::Snapshot>>;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NoSnapshot;
impl<T: Entry> Snapshot<T> for NoSnapshot {
fn create(_entries: &[T]) -> Self {
panic!("NoSnapshot should not be created");
}
fn merge(&mut self, _delta: Self) {
panic!("NoSnapshot should not be merged");
}
fn use_snapshots() -> bool {
false
}
}