Skip to main content

fff_search/dbs/
db_healthcheck.rs

1use crate::error::Result;
2
3/// Health information about a database
4#[derive(Debug, Clone)]
5pub struct DbHealth {
6    /// Path to the database file
7    pub path: String,
8    /// Size on disk in bytes
9    pub disk_size: u64,
10    /// Entry counts by table name
11    pub entry_counts: Vec<(&'static str, u64)>,
12    /// Set to `false` if can not acquire the write lock
13    pub healthy: bool,
14}
15
16pub trait DbHealthChecker {
17    fn get_env(&self) -> &heed::Env;
18    fn is_healthy(&self) -> bool;
19    /// Entries per database, each group has a static string label
20    fn count_entries(&self) -> Result<Vec<(&'static str, u64)>>;
21
22    /// Health summary of the database, returns summary struct
23    fn get_health(&self) -> Result<DbHealth> {
24        let env = self.get_env();
25
26        let size = env
27            .real_disk_size()
28            .map_err(crate::error::Error::GenericDbError)?;
29        let path = env.path().to_string_lossy().to_string();
30        let entry_counts = self.count_entries()?;
31
32        Ok(DbHealth {
33            path,
34            disk_size: size,
35            entry_counts,
36            healthy: self.is_healthy(),
37        })
38    }
39}