velesdb_core/database/
stats.rs1use crate::{Error, Result};
4
5use super::Database;
6
7impl Database {
8 #[allow(deprecated)]
15 pub fn analyze_collection(
16 &self,
17 name: &str,
18 ) -> Result<crate::collection::stats::CollectionStats> {
19 let collection = self
20 .get_collection(name)
21 .ok_or_else(|| Error::CollectionNotFound(name.to_string()))?;
22 let stats = collection.analyze()?;
23
24 self.collection_stats
25 .write()
26 .insert(name.to_string(), stats.clone());
27
28 let stats_path = self.data_dir.join(name).join("collection.stats.json");
29 let serialized = serde_json::to_vec_pretty(&stats)
30 .map_err(|e| Error::Serialization(format!("failed to serialize stats: {e}")))?;
31 std::fs::write(&stats_path, serialized)?;
32
33 Ok(stats)
34 }
35
36 pub fn get_collection_stats(
43 &self,
44 name: &str,
45 ) -> Result<Option<crate::collection::stats::CollectionStats>> {
46 if let Some(stats) = self.collection_stats.read().get(name).cloned() {
47 return Ok(Some(stats));
48 }
49
50 let stats_path = self.data_dir.join(name).join("collection.stats.json");
51 if !stats_path.exists() {
52 return Ok(None);
53 }
54
55 let bytes = std::fs::read(stats_path)?;
56 let stats: crate::collection::stats::CollectionStats = serde_json::from_slice(&bytes)
57 .map_err(|e| Error::Serialization(format!("failed to parse stats: {e}")))?;
58 self.collection_stats
59 .write()
60 .insert(name.to_string(), stats.clone());
61 Ok(Some(stats))
62 }
63}