-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alessandro Passaro <[email protected]>
- Loading branch information
Showing
8 changed files
with
250 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::time::Duration; | ||
|
||
use linked_hash_map::LinkedHashMap; | ||
|
||
use super::{expiry::Expiry, InodeNo}; | ||
|
||
/// A bounded set of (parent_ino, child_name) entries that expire after a fixed time. | ||
#[derive(Debug)] | ||
pub struct ExpirableSet { | ||
map: LinkedHashMap<Key, Expiry>, | ||
size: usize, | ||
max_size: usize, | ||
validity: Duration, | ||
} | ||
|
||
#[derive(Debug, Hash, PartialEq, Eq)] | ||
struct Key { | ||
parent_ino: InodeNo, | ||
child_name: String, | ||
} | ||
|
||
impl ExpirableSet { | ||
pub fn new(max_size: usize, validity: Duration) -> Self { | ||
Self { | ||
map: Default::default(), | ||
size: 0, | ||
max_size, | ||
validity, | ||
} | ||
} | ||
|
||
pub fn contains(&self, parent_ino: InodeNo, child_name: &str) -> bool { | ||
let key = Key { | ||
parent_ino, | ||
child_name: child_name.to_owned(), | ||
}; | ||
let Some(value) = self.map.get(&key) else { | ||
return false; | ||
}; | ||
value.is_valid() | ||
} | ||
|
||
pub fn remove(&mut self, parent_ino: InodeNo, child_name: &str) { | ||
let key = Key { | ||
parent_ino, | ||
child_name: child_name.to_owned(), | ||
}; | ||
if self.map.remove(&key).is_some() { | ||
self.size -= 1; | ||
} | ||
} | ||
|
||
pub fn insert(&mut self, parent_ino: InodeNo, child_name: &str) { | ||
let expiry = Expiry::from_now(self.validity); | ||
let key = Key { | ||
parent_ino, | ||
child_name: child_name.to_owned(), | ||
}; | ||
if self.map.insert(key, expiry).is_none() { | ||
self.size += 1; | ||
|
||
while self.size > self.max_size || self.map.front().is_some_and(|(_, e)| !e.is_valid()) { | ||
if self.map.pop_front().is_none() { | ||
break; | ||
} | ||
self.size -= 1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.