use crate::Result;
#[allow(dead_code)]
pub(crate) mod codec;
#[allow(dead_code)]
pub(crate) mod file;
#[allow(dead_code)]
pub(crate) mod migrate;
#[allow(dead_code)]
pub(crate) mod page;
#[allow(dead_code)]
pub(crate) mod page_store;
#[allow(dead_code)]
pub(crate) mod wal;
#[allow(dead_code)]
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)]
pub(crate) enum OpRef<'a> {
Insert {
key: &'a [u8],
value: &'a [u8],
expires_at: Option<u64>,
},
Remove {
key: &'a [u8],
},
Clear,
Checkpoint {
record_count: u32,
},
BatchBegin {
tx_id: u64,
op_count: u32,
},
BatchEnd {
tx_id: u64,
},
}
impl<'a> From<&'a Op> for OpRef<'a> {
fn from(op: &'a Op) -> Self {
match op {
Op::Insert {
key,
value,
expires_at,
} => OpRef::Insert {
key: key.as_slice(),
value: value.as_slice(),
expires_at: *expires_at,
},
Op::Remove { key } => OpRef::Remove {
key: key.as_slice(),
},
Op::Clear => OpRef::Clear,
Op::Checkpoint { record_count } => OpRef::Checkpoint {
record_count: *record_count,
},
Op::BatchBegin { tx_id, op_count } => OpRef::BatchBegin {
tx_id: *tx_id,
op_count: *op_count,
},
Op::BatchEnd { tx_id } => OpRef::BatchEnd { tx_id: *tx_id },
}
}
}
#[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: OpRef<'_>) -> 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 last_tx_id(&self) -> u64;
fn set_last_tx_id(&mut self, tx_id: u64) -> Result<()>;
}