use std::path::Path;
use serde::Deserialize;
use crate::db::Database;
use crate::error::Error;
use crate::ffi::path_has_suffix;
use crate::status::TypedResult;
#[derive(Debug, Clone, Deserialize)]
pub struct LiveByteCategory {
pub records: u64,
pub key_bytes: u64,
pub value_bytes: u64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct LiveByteAccounting {
pub metadata_catalog: LiveByteCategory,
pub index_catalog: LiveByteCategory,
pub control_documents: LiveByteCategory,
pub documents: LiveByteCategory,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CheckReport {
#[serde(default)]
pub live_accounting: Option<LiveByteAccounting>,
pub valid: bool,
pub file_size: u64,
pub valid_prefix_size: u64,
pub tail_bytes: u64,
pub record_count: u64,
pub live_file_count: u64,
pub live_bytes: u64,
pub compact_size: u64,
pub reclaimable_bytes: u64,
#[serde(default)]
pub issue: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct VacuumReport {
pub before_size: u64,
pub after_size: u64,
pub reclaimed_bytes: u64,
pub live_file_count: u64,
pub live_bytes: u64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CompactReport {
pub compacted: bool,
pub vacuum: VacuumReport,
}
#[derive(Debug, Clone, Deserialize)]
pub struct StableSnapshotReport {
pub source_size: u64,
pub snapshot_size: u64,
pub checkpoint_sequence: u64,
pub page_count: u64,
pub tail_bytes: u64,
}
impl Database {
pub fn check(&self) -> TypedResult<CheckReport> {
let body = self.check_json()?;
Ok(serde_json::from_slice(&body)?)
}
pub fn vacuum(&self) -> TypedResult<VacuumReport> {
let body = self.vacuum_json()?;
Ok(serde_json::from_slice(&body)?)
}
pub fn compact(&self) -> TypedResult<CompactReport> {
let body = self.compact_json()?;
Ok(serde_json::from_slice(&body)?)
}
pub fn copy_stable_snapshot(
&self,
dest_path: impl AsRef<Path>,
replace: bool,
) -> TypedResult<StableSnapshotReport> {
let body = self.copy_stable_snapshot_json(dest_path, replace)?;
Ok(serde_json::from_slice(&body)?)
}
}
pub fn check_file(path: impl AsRef<Path>) -> TypedResult<CheckReport> {
let body = crate::files::check_file_json(path)?;
Ok(serde_json::from_slice(&body)?)
}
pub fn copy_stable_snapshot_file(
src_path: impl AsRef<Path>,
dest_path: impl AsRef<Path>,
replace: bool,
) -> TypedResult<StableSnapshotReport> {
let src_path = src_path.as_ref();
let dest_path = dest_path.as_ref();
if !path_has_suffix(src_path, ".aflite") || !path_has_suffix(dest_path, ".aflite") {
return Err(Error::InvalidArgument.into());
}
let body = crate::files::copy_stable_snapshot_file_json(src_path, dest_path, replace)?;
Ok(serde_json::from_slice(&body)?)
}