flow_gates/filtering/
cache.rs

1use std::hash::{Hash, Hasher};
2use std::sync::Arc;
3
4/// Cache key for filtered event indices
5///
6/// This key uniquely identifies a cached filter result based on:
7/// - The file being filtered
8/// - The gate being applied
9/// - The parent gate chain (for hierarchical filtering)
10#[derive(Debug, Clone, Eq)]
11pub struct FilterCacheKey {
12    /// File GUID
13    pub file_guid: Arc<str>,
14    /// Gate ID
15    pub gate_id: Arc<str>,
16    /// Parent gate chain (for hierarchical filtering)
17    /// Stored as a sorted, deduplicated list for consistent hashing
18    pub parent_chain: Vec<Arc<str>>,
19}
20
21impl FilterCacheKey {
22    /// Create a new cache key
23    pub fn new(
24        file_guid: impl Into<Arc<str>>,
25        gate_id: impl Into<Arc<str>>,
26        parent_chain: Vec<impl Into<Arc<str>>>,
27    ) -> Self {
28        let mut chain: Vec<Arc<str>> = parent_chain.into_iter().map(|s| s.into()).collect();
29        chain.sort();
30        chain.dedup();
31
32        Self {
33            file_guid: file_guid.into(),
34            gate_id: gate_id.into(),
35            parent_chain: chain,
36        }
37    }
38
39    /// Create a simple key without parent chain
40    pub fn simple(file_guid: impl Into<Arc<str>>, gate_id: impl Into<Arc<str>>) -> Self {
41        Self {
42            file_guid: file_guid.into(),
43            gate_id: gate_id.into(),
44            parent_chain: Vec::new(),
45        }
46    }
47}
48
49impl PartialEq for FilterCacheKey {
50    fn eq(&self, other: &Self) -> bool {
51        self.file_guid == other.file_guid
52            && self.gate_id == other.gate_id
53            && self.parent_chain == other.parent_chain
54    }
55}
56
57impl Hash for FilterCacheKey {
58    fn hash<H: Hasher>(&self, state: &mut H) {
59        self.file_guid.hash(state);
60        self.gate_id.hash(state);
61        for parent in &self.parent_chain {
62            parent.hash(state);
63        }
64    }
65}
66
67/// Trait for caching filtered event indices
68///
69/// This trait allows the filtering system to work with any cache implementation.
70/// The application crate should implement this trait for its FilterCache type.
71pub trait FilterCache: Send + Sync {
72    /// Get cached filtered indices for a key
73    ///
74    /// Returns `Some(Arc<Vec<usize>>)` if the value is cached, `None` otherwise
75    fn get(&self, key: &FilterCacheKey) -> Option<Arc<Vec<usize>>>;
76
77    /// Insert filtered indices into the cache
78    ///
79    /// # Arguments
80    /// * `key` - Cache key
81    /// * `value` - Filtered event indices to cache
82    fn insert(&self, key: FilterCacheKey, value: Arc<Vec<usize>>);
83}