use std::collections::HashMap;
use std::sync::Mutex;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SnapshotSummaryCounts {
pub file_count: usize,
pub snapshot_row_count: usize,
}
#[derive(Debug, Default)]
pub struct SnapshotSummaryCache {
entries: Mutex<HashMap<String, SnapshotSummaryCounts>>,
}
impl SnapshotSummaryCache {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn get(&self, content_hash: &str) -> Option<SnapshotSummaryCounts> {
self.entries
.lock()
.expect("snapshot summary cache mutex is not poisoned")
.get(content_hash)
.copied()
}
pub(crate) fn insert(&self, content_hash: String, counts: SnapshotSummaryCounts) {
self.entries
.lock()
.expect("snapshot summary cache mutex is not poisoned")
.insert(content_hash, counts);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stores_and_returns_counts_by_content_hash() {
let cache = SnapshotSummaryCache::new();
assert_eq!(cache.get("sha256:abc"), None);
let counts = SnapshotSummaryCounts {
file_count: 3,
snapshot_row_count: 42,
};
cache.insert("sha256:abc".to_owned(), counts);
assert_eq!(cache.get("sha256:abc"), Some(counts));
assert_eq!(cache.get("sha256:other"), None);
}
}