flow_gates/filtering/
cache.rs1use std::hash::{Hash, Hasher};
2use std::sync::Arc;
3
4#[derive(Debug, Clone, Eq)]
11pub struct FilterCacheKey {
12 pub file_guid: Arc<str>,
14 pub gate_id: Arc<str>,
16 pub parent_chain: Vec<Arc<str>>,
19}
20
21impl FilterCacheKey {
22 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 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
67pub trait FilterCache: Send + Sync {
72 fn get(&self, key: &FilterCacheKey) -> Option<Arc<Vec<usize>>>;
76
77 fn insert(&self, key: FilterCacheKey, value: Arc<Vec<usize>>);
83}