use super::super::{
CacheMetrics, EPISODES_TABLE, HEURISTICS_TABLE, PATTERNS_TABLE, with_db_timeout,
};
use crate::{RedbStorage, StorageStatistics};
use do_memory_core::{Error, Result};
use redb::{ReadableDatabase, ReadableTableMetadata};
use std::sync::Arc;
impl RedbStorage {
pub async fn get_statistics(&self) -> Result<StorageStatistics> {
let db = Arc::clone(&self.db);
with_db_timeout(move || {
let read_txn = db
.begin_read()
.map_err(|e| Error::Storage(format!("Failed to begin read transaction: {}", e)))?;
let episodes_table = read_txn
.open_table(EPISODES_TABLE)
.map_err(|e| Error::Storage(format!("Failed to open episodes table: {}", e)))?;
let patterns_table = read_txn
.open_table(PATTERNS_TABLE)
.map_err(|e| Error::Storage(format!("Failed to open patterns table: {}", e)))?;
let heuristics_table = read_txn
.open_table(HEURISTICS_TABLE)
.map_err(|e| Error::Storage(format!("Failed to open heuristics table: {}", e)))?;
let episode_count = episodes_table
.len()
.map_err(|e| Error::Storage(format!("Failed to get episodes count: {}", e)))?
as usize;
let pattern_count = patterns_table
.len()
.map_err(|e| Error::Storage(format!("Failed to get patterns count: {}", e)))?
as usize;
let heuristic_count = heuristics_table
.len()
.map_err(|e| Error::Storage(format!("Failed to get heuristics count: {}", e)))?
as usize;
Ok(StorageStatistics {
episode_count,
pattern_count,
heuristic_count,
})
})
.await
}
pub async fn health_check(&self) -> Result<bool> {
let db = Arc::clone(&self.db);
with_db_timeout(move || match db.begin_read() {
Ok(_) => Ok(true),
Err(_) => Ok(false),
})
.await
}
pub async fn get_cache_metrics(&self) -> CacheMetrics {
self.cache.get_metrics().await
}
pub async fn cleanup_cache(&self) -> usize {
self.cache.cleanup_expired().await
}
}