pub struct DedupStore { /* private fields */ }Expand description
Remembers which keys have been seen, within a bounded window.
Caller-owned. The crate holds no state of its own, so a run is a pure
mapping from (input, store, as_of) to (outcome, store′) — which is what
makes a replay reproducible.
Not automatically evicting: evict is called by the caller,
at a cadence they choose, and reports how many keys it dropped. Automatic
eviction inside observe would make the store’s size depend on arrival
order, and therefore on how the caller batched their input.
Implementations§
Source§impl DedupStore
impl DedupStore
Sourcepub fn new(window: DedupWindow) -> Self
pub fn new(window: DedupWindow) -> Self
An empty store bound to a window.
The window is held by the store rather than passed to each eviction, so two calls cannot disagree about how long the promise runs — a store evicted against a shorter window than it was filled under silently loses duplicate detection for the difference.
Sourcepub fn with_capacity(window: DedupWindow, capacity: usize) -> Self
pub fn with_capacity(window: DedupWindow, capacity: usize) -> Self
An empty store with room for capacity keys.
Sourcepub const fn window(&self) -> DedupWindow
pub const fn window(&self) -> DedupWindow
The window this store remembers within.
Sourcepub fn observe(&mut self, key: RecordKey, at: OffsetDateTime) -> DedupDecision
pub fn observe(&mut self, key: RecordKey, at: OffsetDateTime) -> DedupDecision
Record an observation of key at at.
Idempotent: observing the same key repeatedly yields
DedupDecision::First exactly once, then
DedupDecision::Duplicate every time after — never twice, never zero
times. The first-seen instant is retained rather than refreshed, so a
stream of retransmissions cannot keep a key alive past its window.
Sourcepub fn evict(&mut self, as_of: OffsetDateTime) -> usize
pub fn evict(&mut self, as_of: OffsetDateTime) -> usize
Drop keys first seen before the window’s cutoff, returning how many.
After this, a duplicate of a dropped key will be classified as
DedupDecision::First and will be billed again. That is the window’s
stated consequence, not a defect — but it is why the bound must reflect
real partner behaviour.
Sourcepub fn first_seen(&self, key: &RecordKey) -> Option<OffsetDateTime>
pub fn first_seen(&self, key: &RecordKey) -> Option<OffsetDateTime>
When a key was first seen, if it is still remembered.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&RecordKey, OffsetDateTime)>
pub fn iter(&self) -> impl Iterator<Item = (&RecordKey, OffsetDateTime)>
Every key remembered, with when it was first seen.
The order is unspecified and varies between runs — the store is a hash map, and nothing about deduplication depends on iteration order. Sort the result if you need a stable rendering.
Sourcepub fn restore(
window: DedupWindow,
observations: impl IntoIterator<Item = (RecordKey, OffsetDateTime)>,
) -> Self
pub fn restore( window: DedupWindow, observations: impl IntoIterator<Item = (RecordKey, OffsetDateTime)>, ) -> Self
Rebuild a store from keys and their first-seen instants.
The other half of iter, and the reason both exist: the
crate performs no I/O, so a store that survives a restart is something
you serialise — and a store you cannot read out is one you cannot
persist, which would leave every restart silently re-billing a window’s
worth of retransmissions.
A repeated key keeps its earliest instant, so restoring from a concatenation of snapshots cannot extend a key’s life past its window.
use mediation::{DedupStore, DedupWindow, RecordKey};
use time::{Duration, macros::datetime};
let window = DedupWindow::from_bound(Duration::days(7)).unwrap();
let mut store = DedupStore::new(window);
store.observe(key.clone(), datetime!(2026-07-28 10:00 UTC));
// Round-trips through whatever you store it in.
let saved: Vec<_> = store.iter().map(|(k, at)| (k.clone(), at)).collect();
let restored = DedupStore::restore(window, saved);
assert_eq!(restored.first_seen(&key), store.first_seen(&key));