Skip to main content

DedupStore

Struct DedupStore 

Source
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

Source

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.

Source

pub fn with_capacity(window: DedupWindow, capacity: usize) -> Self

An empty store with room for capacity keys.

Source

pub const fn window(&self) -> DedupWindow

The window this store remembers within.

Source

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.

Source

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.

Source

pub fn first_seen(&self, key: &RecordKey) -> Option<OffsetDateTime>

When a key was first seen, if it is still remembered.

Source

pub fn len(&self) -> usize

How many keys are remembered.

Source

pub fn is_empty(&self) -> bool

true when nothing is remembered.

Source

pub fn clear(&mut self)

Forget everything.

Source

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.

Source

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));

Trait Implementations§

Source§

impl Clone for DedupStore

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DedupStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoIterator for &'a DedupStore

Source§

type Item = (&'a RecordKey, OffsetDateTime)

The type of the elements being iterated over.
Source§

type IntoIter = Map<Iter<'a, RecordKey, OffsetDateTime>, fn((&'a RecordKey, &'a OffsetDateTime)) -> (&'a RecordKey, OffsetDateTime)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.