Skip to main content

dioxus_swdir_tree_core/
cache.rs

1//! Cache of raw, **unfiltered** scan results, keyed by path.
2//!
3//! The cache exists so [`crate::DirectoryTree::set_filter`] can re-derive
4//! every loaded node's child list in memory with zero I/O. It is never
5//! explicitly invalidated in normal use; a newer accepted scan for the
6//! same path simply overwrites the entry.
7
8use std::collections::HashMap;
9use std::path::{Path, PathBuf};
10
11use crate::entry::LoadedEntry;
12
13/// One completed scan: the generation it was accepted under and the
14/// complete (unfiltered) entry list.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct CachedScan {
17    /// Generation the result carried when it was accepted.
18    pub generation: u32,
19    /// Raw entries, exactly as returned by the scan.
20    pub entries: Vec<LoadedEntry>,
21}
22
23/// Flat map from scanned path to its latest accepted result.
24#[derive(Debug, Clone, Default, PartialEq, Eq)]
25pub struct TreeCache {
26    map: HashMap<PathBuf, CachedScan>,
27}
28
29impl TreeCache {
30    /// Store (or overwrite) the result for `path`.
31    pub(crate) fn insert(&mut self, path: PathBuf, generation: u32, entries: Vec<LoadedEntry>) {
32        self.map.insert(
33            path,
34            CachedScan {
35                generation,
36                entries,
37            },
38        );
39    }
40
41    /// Latest accepted result for `path`, if any.
42    pub fn get(&self, path: &Path) -> Option<&CachedScan> {
43        self.map.get(path)
44    }
45
46    /// `true` if a result for `path` is cached.
47    pub fn contains(&self, path: &Path) -> bool {
48        self.map.contains_key(path)
49    }
50
51    /// Number of cached scans.
52    pub fn len(&self) -> usize {
53        self.map.len()
54    }
55
56    /// `true` if nothing has been cached yet.
57    pub fn is_empty(&self) -> bool {
58        self.map.is_empty()
59    }
60}