Evict oldest snapshots when over the limit
(&self, snapshots: &mut HashMap<String, Vec<FileSnapshot>>)
| 114 | |
| 115 | /// Evict oldest snapshots when over the limit |
| 116 | fn evict_if_needed(&self, snapshots: &mut HashMap<String, Vec<FileSnapshot>>) { |
| 117 | let total: usize = snapshots.values().map(|v| v.len()).sum(); |
| 118 | if total <= self.max_snapshots { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | let to_remove = total - self.max_snapshots; |
| 123 | |
| 124 | // Collect all snapshots with their file path, sorted by timestamp |
| 125 | let mut all_entries: Vec<(String, usize, DateTime<Utc>)> = Vec::new(); |
| 126 | for (path, versions) in snapshots.iter() { |
| 127 | for snapshot in versions { |
| 128 | all_entries.push((path.clone(), snapshot.version, snapshot.timestamp)); |
| 129 | } |
| 130 | } |
| 131 | all_entries.sort_by_key(|e| e.2); |
| 132 | |
| 133 | // Remove the oldest entries |
| 134 | for (path, version, _) in all_entries.into_iter().take(to_remove) { |
| 135 | if let Some(versions) = snapshots.get_mut(&path) { |
| 136 | versions.retain(|s| s.version != version); |
| 137 | if versions.is_empty() { |
| 138 | snapshots.remove(&path); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// Check if a tool name is a file-modifying tool that should trigger snapshots |
no test coverage detected