| 257 | // Cleans up any stale mappings that point to collections no longer in the cache |
| 258 | #[allow(dead_code)] |
| 259 | pub fn cleanup_stale_mappings(&self) -> Result<usize, WaCustomError> { |
| 260 | let mut removed_count = 0; |
| 261 | |
| 262 | // Create a list of collection names to avoid mutating during iteration |
| 263 | let collection_names: Vec<String> = self |
| 264 | .name_to_key |
| 265 | .iter() |
| 266 | .map(|entry| entry.key().clone()) |
| 267 | .collect(); |
| 268 | |
| 269 | for name in collection_names { |
| 270 | if let Some(key) = self.name_to_key.get(&name) { |
| 271 | // Check if the collection is still in the cache |
| 272 | if self.cache.get(&key.clone()).is_none() { |
| 273 | // If not in cache, remove the mappings |
| 274 | self.name_to_key.remove(&name); |
| 275 | removed_count += 1; |
| 276 | info!("Cleaned up stale mapping for collection: {}", name); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | info!( |
| 282 | "Cleanup completed: removed {} stale mappings", |
| 283 | removed_count |
| 284 | ); |
| 285 | Ok(removed_count) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Extension trait for AppContext to easily update collection cache |