A simple duplicate filter for filtering shows of duplicate ids
This commit is contained in:
parent
e079a361ba
commit
23d66aaae3
|
@ -0,0 +1,34 @@
|
|||
/*! Filter duplicate shows of credentials and open invitations by id
|
||||
* (which will typically be a Scalar). This implementation just keeps
|
||||
* the table of seen ids in memory, but a production one would of course
|
||||
* use a disk-backed database. */
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::cmp::Eq;
|
||||
|
||||
/// Each instance of DupFilter maintains its own independent table of
|
||||
/// seen ids. IdType will typically be Scalar.
|
||||
#[derive(Default,Debug)]
|
||||
pub struct DupFilter<IdType> {
|
||||
seen_table: HashMap<IdType, ()>,
|
||||
}
|
||||
|
||||
impl<IdType: Hash + Eq + Copy> DupFilter<IdType> {
|
||||
/// Check to see if the id is in the seen table, but do not add it
|
||||
/// to the seen table. Return true if it is already in the table,
|
||||
/// false if not.
|
||||
pub fn check(&self, id: &IdType) -> bool {
|
||||
self.seen_table.contains_key(id)
|
||||
}
|
||||
|
||||
/// As atomically as possible, check to see if the id is in the seen
|
||||
/// table, and add it if not. Return Ok(()) if it was not already
|
||||
/// in the table, and Err(()) if it was.
|
||||
pub fn filter(&mut self, id: &IdType) -> Result<(),()> {
|
||||
match self.seen_table.insert(*id, ()) {
|
||||
None => Ok(()),
|
||||
Some(()) => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue