use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::types::BlockId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DurabilityMode {
Synchronous,
SynchronousRelaxed { sync_every_n_blocks: usize },
Async { max_pending_blocks: usize },
AsyncRelaxed {
max_pending_blocks: usize,
sync_every_n_blocks: usize,
},
}
impl DurabilityMode {
pub fn max_pending_blocks(&self) -> usize {
match self {
DurabilityMode::Synchronous => 1,
DurabilityMode::SynchronousRelaxed { .. } => 1,
DurabilityMode::Async { max_pending_blocks } => *max_pending_blocks,
DurabilityMode::AsyncRelaxed {
max_pending_blocks, ..
} => *max_pending_blocks,
}
}
pub fn is_async(&self) -> bool {
matches!(
self,
DurabilityMode::Async { .. } | DurabilityMode::AsyncRelaxed { .. }
)
}
pub fn sync_every_n_blocks(&self) -> usize {
match self {
DurabilityMode::Synchronous => 0,
DurabilityMode::SynchronousRelaxed {
sync_every_n_blocks,
} => *sync_every_n_blocks,
DurabilityMode::Async { .. } => 0,
DurabilityMode::AsyncRelaxed {
sync_every_n_blocks,
..
} => *sync_every_n_blocks,
}
}
pub fn is_relaxed(&self) -> bool {
matches!(
self,
DurabilityMode::SynchronousRelaxed { .. } | DurabilityMode::AsyncRelaxed { .. }
)
}
pub fn async_relaxed() -> Self {
Self::AsyncRelaxed {
max_pending_blocks: 1024,
sync_every_n_blocks: 100,
}
}
}
impl Default for DurabilityMode {
fn default() -> Self {
DurabilityMode::Async {
max_pending_blocks: 1024,
}
}
}
#[derive(Debug, Clone)]
pub struct PersistenceSettings {
pub durability_mode: DurabilityMode,
pub snapshot_interval: Duration,
pub max_snapshot_interval: Duration,
pub min_rollback_window: BlockId,
pub prune_interval: Duration,
}
impl Default for PersistenceSettings {
fn default() -> Self {
Self {
durability_mode: DurabilityMode::default(),
snapshot_interval: Duration::from_secs(3600),
max_snapshot_interval: Duration::from_secs(3600),
min_rollback_window: BlockId::MAX,
prune_interval: Duration::from_secs(10),
}
}
}