use std::path::Path;
use crate::Result;
pub(crate) mod codec;
pub(crate) mod file;
pub(crate) mod memory;
pub(crate) const FORMAT_VERSION: u32 = 2;
#[cfg(feature = "ttl")]
pub(crate) const FLAG_TTL: u32 = 1 << 0;
#[cfg(feature = "nested")]
pub(crate) const FLAG_NESTED: u32 = 1 << 1;
#[must_use]
pub(crate) fn build_flags() -> u32 {
ttl_flag() | nested_flag()
}
#[cfg(feature = "ttl")]
const fn ttl_flag() -> u32 {
FLAG_TTL
}
#[cfg(not(feature = "ttl"))]
const fn ttl_flag() -> u32 {
0
}
#[cfg(feature = "nested")]
const fn nested_flag() -> u32 {
FLAG_NESTED
}
#[cfg(not(feature = "nested"))]
const fn nested_flag() -> u32 {
0
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Op {
Insert {
key: Vec<u8>,
value: Vec<u8>,
expires_at: Option<u64>,
},
Remove {
key: Vec<u8>,
},
Clear,
Checkpoint {
record_count: u32,
},
BatchBegin {
tx_id: u64,
op_count: u32,
},
BatchEnd {
tx_id: u64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FlushPolicy {
OnEachWrite,
EveryN(u32),
Manual,
}
impl Default for FlushPolicy {
fn default() -> Self {
Self::EveryN(64)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct SnapshotEntry<'a> {
pub(crate) key: &'a [u8],
pub(crate) value: &'a [u8],
pub(crate) expires_at: Option<u64>,
}
pub(crate) type SnapshotIter<'a> = Box<dyn Iterator<Item = SnapshotEntry<'a>> + 'a>;
pub(crate) trait Storage: Send {
fn append(&mut self, op: &Op) -> Result<()>;
fn flush(&mut self) -> Result<()>;
fn replay(&mut self, sink: &mut dyn FnMut(Op) -> Result<()>) -> Result<()>;
fn compact(&mut self, snapshot: SnapshotIter<'_>) -> Result<()>;
fn path(&self) -> Option<&Path>;
fn last_tx_id(&self) -> u64;
fn set_last_tx_id(&mut self, tx_id: u64) -> Result<()>;
}