Skip to main content

antfly_embedded/
maintenance.rs

1// Copyright 2026 Antfly, Inc.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Typed convenience wrappers around integrity check, vacuum, compact, and
17//! stable-snapshot JSON results. Only compiled with the (default-on)
18//! `serde` feature. Mirrors `go/pkg/embedded/maintenance.go`.
19
20use 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/// Logical key/value payload, excluding physical headers.
30#[derive(Debug, Clone, Deserialize)]
31pub struct LiveByteCategory {
32    pub records: u64,
33    pub key_bytes: u64,
34    pub value_bytes: u64,
35}
36
37/// Logical payload attribution from an explicit integrity check.
38#[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/// The typed form of [`Database::check_json`] / [`crate::files::check_file_json`].
47#[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/// The typed form of [`Database::vacuum_json`].
65#[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/// The typed form of [`Database::compact_json`].
75#[derive(Debug, Clone, Deserialize)]
76pub struct CompactReport {
77    pub compacted: bool,
78    pub vacuum: VacuumReport,
79}
80
81/// The typed form of [`Database::copy_stable_snapshot_json`].
82#[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    /// Runs Lite integrity checks and returns the typed result.
93    pub fn check(&self) -> TypedResult<CheckReport> {
94        let body = self.check_json()?;
95        Ok(serde_json::from_slice(&body)?)
96    }
97
98    /// Compacts free space and returns the typed result.
99    pub fn vacuum(&self) -> TypedResult<VacuumReport> {
100        let body = self.vacuum_json()?;
101        Ok(serde_json::from_slice(&body)?)
102    }
103
104    /// Drains maintenance, compacts indexes, vacuums free space, and
105    /// returns the typed result.
106    pub fn compact(&self) -> TypedResult<CompactReport> {
107        let body = self.compact_json()?;
108        Ok(serde_json::from_slice(&body)?)
109    }
110
111    /// Copies a stable Lite snapshot to `dest_path` and returns the typed
112    /// result.
113    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
123/// Runs Lite integrity checks for `path` without opening a database handle
124/// and returns the typed result.
125pub 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
130/// Opens `src_path` read-only, copies a stable physical `.aflite` snapshot
131/// to `dest_path`, and returns the typed snapshot report. Both paths must
132/// end in `.aflite`.
133pub 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}