antfly_embedded/
maintenance.rs1use std::path::Path;
21
22use serde::Deserialize;
23
24use crate::db::Database;
25use crate::error::Error;
26use crate::ffi::path_has_suffix;
27use crate::status::TypedResult;
28
29#[derive(Debug, Clone, Deserialize)]
31pub struct LiveByteCategory {
32 pub records: u64,
33 pub key_bytes: u64,
34 pub value_bytes: u64,
35}
36
37#[derive(Debug, Clone, Deserialize)]
39pub struct LiveByteAccounting {
40 pub metadata_catalog: LiveByteCategory,
41 pub index_catalog: LiveByteCategory,
42 pub control_documents: LiveByteCategory,
43 pub documents: LiveByteCategory,
44}
45
46#[derive(Debug, Clone, Deserialize)]
48pub struct CheckReport {
49 #[serde(default)]
50 pub live_accounting: Option<LiveByteAccounting>,
51 pub valid: bool,
52 pub file_size: u64,
53 pub valid_prefix_size: u64,
54 pub tail_bytes: u64,
55 pub record_count: u64,
56 pub live_file_count: u64,
57 pub live_bytes: u64,
58 pub compact_size: u64,
59 pub reclaimable_bytes: u64,
60 #[serde(default)]
61 pub issue: Option<String>,
62}
63
64#[derive(Debug, Clone, Deserialize)]
66pub struct VacuumReport {
67 pub before_size: u64,
68 pub after_size: u64,
69 pub reclaimed_bytes: u64,
70 pub live_file_count: u64,
71 pub live_bytes: u64,
72}
73
74#[derive(Debug, Clone, Deserialize)]
76pub struct CompactReport {
77 pub compacted: bool,
78 pub vacuum: VacuumReport,
79}
80
81#[derive(Debug, Clone, Deserialize)]
83pub struct StableSnapshotReport {
84 pub source_size: u64,
85 pub snapshot_size: u64,
86 pub checkpoint_sequence: u64,
87 pub page_count: u64,
88 pub tail_bytes: u64,
89}
90
91impl Database {
92 pub fn check(&self) -> TypedResult<CheckReport> {
94 let body = self.check_json()?;
95 Ok(serde_json::from_slice(&body)?)
96 }
97
98 pub fn vacuum(&self) -> TypedResult<VacuumReport> {
100 let body = self.vacuum_json()?;
101 Ok(serde_json::from_slice(&body)?)
102 }
103
104 pub fn compact(&self) -> TypedResult<CompactReport> {
107 let body = self.compact_json()?;
108 Ok(serde_json::from_slice(&body)?)
109 }
110
111 pub fn copy_stable_snapshot(
114 &self,
115 dest_path: impl AsRef<Path>,
116 replace: bool,
117 ) -> TypedResult<StableSnapshotReport> {
118 let body = self.copy_stable_snapshot_json(dest_path, replace)?;
119 Ok(serde_json::from_slice(&body)?)
120 }
121}
122
123pub fn check_file(path: impl AsRef<Path>) -> TypedResult<CheckReport> {
126 let body = crate::files::check_file_json(path)?;
127 Ok(serde_json::from_slice(&body)?)
128}
129
130pub fn copy_stable_snapshot_file(
134 src_path: impl AsRef<Path>,
135 dest_path: impl AsRef<Path>,
136 replace: bool,
137) -> TypedResult<StableSnapshotReport> {
138 let src_path = src_path.as_ref();
139 let dest_path = dest_path.as_ref();
140 if !path_has_suffix(src_path, ".aflite") || !path_has_suffix(dest_path, ".aflite") {
141 return Err(Error::InvalidArgument.into());
142 }
143 let body = crate::files::copy_stable_snapshot_file_json(src_path, dest_path, replace)?;
144 Ok(serde_json::from_slice(&body)?)
145}