use std::path::{Path, PathBuf};
use pigeonhole_engine::{Engine, ExtentKind};
use crate::Result;
pub fn check(path: impl AsRef<Path>, options: CheckOptions) -> Result<CheckReport> {
let vfs: pigeonhole_io::VfsRef = pigeonhole_io::pread::PreadVfs::new(0);
let engine = pigeonhole_engine::CheckOptions::default()
.deep(options.deep)
.allow_fuse(options.allow_fuse);
Ok(CheckReport::from_engine(Engine::check(
&vfs,
path.as_ref(),
&engine,
)?))
}
#[derive(Clone, Default)]
pub struct CheckOptions {
deep: bool,
allow_fuse: bool,
}
impl std::fmt::Debug for CheckOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CheckOptions")
.field("deep", &self.deep)
.field("allow_fuse", &self.allow_fuse)
.finish_non_exhaustive()
}
}
impl CheckOptions {
pub fn deep(mut self, deep: bool) -> Self {
self.deep = deep;
self
}
pub fn allow_fuse(mut self, allow: bool) -> Self {
self.allow_fuse = allow;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Structure {
Superblock,
Manifest,
Allocation,
Sst,
Blob,
Wal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Severity {
Corruption,
Warning,
Info,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Finding {
pub structure: Structure,
pub severity: Severity,
pub object: Option<u64>,
pub offset: Option<u64>,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RegionKind {
Superblock {
slot: u8,
},
ManifestSnapshot,
ManifestLog,
Sst {
sst: u64,
table: String,
family: String,
level: u8,
used: u64,
},
Blob {
blob_file: u32,
index: u32,
family: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Region {
pub kind: RegionKind,
pub offset: u64,
pub len: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WalFile {
pub stream: u32,
pub path: PathBuf,
pub len: u64,
pub segment_size: u64,
pub segments: u64,
pub records: u64,
pub checkpoint: WalPosition,
pub end: WalPosition,
#[doc(hidden)]
pub end_slot: Option<u64>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub struct WalPosition {
pub epoch: u32,
pub offset: u64,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct CheckCounts {
pub superblocks: u64,
pub tables: u64,
pub ssts: u64,
pub sst_entries: u64,
pub blob_files: u64,
pub blob_records: u64,
pub blob_pointers: u64,
pub wal_files: u64,
pub wal_records: u64,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CheckReport {
pub findings: Vec<Finding>,
pub regions: Vec<Region>,
pub wal: Vec<WalFile>,
pub counts: CheckCounts,
pub file_bytes: u64,
pub clean_shutdown: bool,
}
impl CheckReport {
pub fn is_ok(&self) -> bool {
!self
.findings
.iter()
.any(|f| f.severity == Severity::Corruption)
}
}
impl CheckReport {
pub(crate) fn from_engine(r: pigeonhole_engine::CheckReport) -> Self {
use pigeonhole_engine::{Severity as S, Structure as T};
Self {
findings: r
.findings
.into_iter()
.map(|f| Finding {
structure: match f.structure {
T::Superblock => Structure::Superblock,
T::Manifest => Structure::Manifest,
T::Allocation => Structure::Allocation,
T::Sst => Structure::Sst,
T::Blob => Structure::Blob,
_ => Structure::Wal,
},
severity: match f.severity {
S::Warning => Severity::Warning,
S::Info => Severity::Info,
_ => Severity::Corruption,
},
object: f.object,
offset: f.offset,
message: f.message,
})
.collect(),
regions: r
.extents
.into_iter()
.map(|e| Region {
kind: match e.kind {
ExtentKind::Superblock { slot } => RegionKind::Superblock { slot },
ExtentKind::ManifestSnapshot => RegionKind::ManifestSnapshot,
ExtentKind::ManifestLog => RegionKind::ManifestLog,
ExtentKind::Sst {
sst,
table,
family,
level,
used,
} => RegionKind::Sst {
sst,
table,
family,
level,
used,
},
ExtentKind::Blob {
blob_file,
index,
family,
} => RegionKind::Blob {
blob_file,
index,
family,
},
_ => unreachable!("an extent kind this build does not know"),
},
offset: e.offset,
len: e.len,
})
.collect(),
wal: r
.wal
.into_iter()
.map(|w| WalFile {
stream: w.stream,
path: w.path,
len: w.len,
segment_size: w.segment_size,
segments: w.segments,
records: w.records,
checkpoint: WalPosition {
epoch: w.checkpoint.0,
offset: u64::from(w.checkpoint.1),
},
end: WalPosition {
epoch: w.end.0,
offset: w.end.1,
},
end_slot: w.end_slot,
})
.collect(),
counts: CheckCounts {
superblocks: r.counts.superblocks,
tables: r.counts.tables,
ssts: r.counts.ssts,
sst_entries: r.counts.sst_entries,
blob_files: r.counts.blob_files,
blob_records: r.counts.blob_records,
blob_pointers: r.counts.blob_pointers,
wal_files: r.counts.wal_files,
wal_records: r.counts.wal_records,
},
file_bytes: r.file_bytes,
clean_shutdown: r.clean_shutdown,
}
}
}