mod reader;
mod retention;
mod writer;
use std::fs;
use std::path::Path;
use std::time::Duration;
pub(crate) use reader::PartitionReader;
pub use retention::Retention;
pub use writer::PartitionLog;
use crate::error::{Error, Result, corrupt};
use crate::record::{MAX_RECORD_SIZE, Record};
use crate::segment::{self, HEADER_SIZE, ReadRequest, SegmentRead};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FsyncPolicy {
Always,
Interval(Duration),
Never,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogConfig {
pub segment_max_bytes: u64,
pub index_interval_bytes: u64,
pub fsync: FsyncPolicy,
pub retention: Retention,
}
impl Default for LogConfig {
fn default() -> Self {
Self {
segment_max_bytes: 64 * 1024 * 1024,
index_interval_bytes: 4096,
fsync: FsyncPolicy::Always,
retention: Retention::default(),
}
}
}
const MAX_SEGMENT_BYTES: u64 = u32::MAX as u64 - MAX_RECORD_SIZE as u64;
impl LogConfig {
pub(crate) fn validate(&self) -> Result<()> {
if self.segment_max_bytes <= HEADER_SIZE || self.segment_max_bytes > MAX_SEGMENT_BYTES {
return Err(Error::InvalidConfig(format!(
"segment_max_bytes must be between {} and {MAX_SEGMENT_BYTES}",
HEADER_SIZE + 1
)));
}
if self.fsync == FsyncPolicy::Interval(Duration::ZERO) {
return Err(Error::InvalidConfig(
"the fsync interval must be positive".into(),
));
}
Ok(())
}
}
pub(crate) fn list_segment_bases(dir: &Path) -> Result<Vec<u64>> {
let mut bases = Vec::new();
for entry in fs::read_dir(dir)? {
let name = entry?.file_name();
let Some(stem) = name.to_str().and_then(|n| n.strip_suffix(".log")) else {
continue;
};
if stem.len() == 20 && stem.bytes().all(|b| b.is_ascii_digit()) {
let base = stem
.parse()
.map_err(|_| corrupt(dir.join(&name), "invalid segment name"))?;
bases.push(base);
}
}
bases.sort_unstable();
Ok(bases)
}
pub(crate) fn remove_orphan_indexes(dir: &Path, bases: &[u64]) -> Result<()> {
for entry in fs::read_dir(dir)? {
let path = entry?.path();
let base = path
.file_name()
.and_then(|n| n.to_str())
.and_then(|n| n.strip_suffix(".idx"))
.and_then(|stem| stem.parse::<u64>().ok());
if base.is_some_and(|b| !bases.contains(&b)) {
fs::remove_file(&path)?;
}
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReadStop {
BudgetReached,
SegmentsExhausted,
}
pub(crate) fn read_across(
bases: &[u64],
from: u64,
end: u64,
max_bytes: usize,
used: &mut usize,
out: &mut Vec<Record>,
mut read_segment: impl FnMut(usize, &ReadRequest, &mut Vec<Record>) -> Result<SegmentRead>,
) -> Result<ReadStop> {
let first = bases.partition_point(|&b| b <= from).saturating_sub(1);
for i in first..bases.len() {
let req = ReadRequest {
from,
end: bases.get(i + 1).map_or(end, |&next| next.min(end)),
budget: max_bytes.saturating_sub(*used),
take_one: out.is_empty(),
};
match read_segment(i, &req, out)? {
SegmentRead::Exhausted { taken } => *used += taken,
SegmentRead::BudgetReached => return Ok(ReadStop::BudgetReached),
}
}
Ok(ReadStop::SegmentsExhausted)
}
pub(crate) fn segment_log_path(dir: &Path, base_offset: u64) -> std::path::PathBuf {
segment::log_path(dir, base_offset)
}