use super::metrics::MetricsCollector;
use std::sync::Arc;
#[salsa::db]
#[derive(Clone)]
pub struct TypeDatabase {
storage: salsa::Storage<Self>,
metrics: Arc<MetricsCollector>,
}
impl TypeDatabase {
pub fn new() -> Self {
Self {
storage: salsa::Storage::default(),
metrics: Arc::new(MetricsCollector::new()),
}
}
pub fn metrics(&self) -> &MetricsCollector {
&self.metrics
}
pub fn metrics_snapshot(&self) -> super::MetricsSnapshot {
self.metrics.snapshot()
}
pub fn record_query(&self, name: &str, duration: std::time::Duration, cached: bool) {
self.metrics.record_query(name, duration, cached);
}
}
impl Default for TypeDatabase {
fn default() -> Self {
Self::new()
}
}
#[salsa::db]
impl salsa::Database for TypeDatabase {}
pub use salsa::Database;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_database_creation() {
let db = TypeDatabase::new();
let _ = db.metrics();
}
#[test]
fn test_metrics_snapshot() {
let db = TypeDatabase::new();
let snapshot = db.metrics_snapshot();
assert_eq!(snapshot.total_queries, 0);
}
}