ReservoirHistogramStat
Weighted reservoir sample of size capacity via Algorithm A-Res (Efraimidis & Spirakis): each item gets a key u^(1/w) and the top-k keys are retained, giving an unbiased weight-proportional sample.
Use cases: keeping a representative sample of a stream you intend to post-process; model training over a uniform/weighted subsample, ad-hoc percentile lookups via the sample distribution, scratch-pad for downstream analytics that need raw values rather than aggregates.
Memory: O(capacity) key/value slots and, outside relaxed mode, a priority index.
Update: O(1) to reject a key and O(log capacity) to replace one with the priority index. Equal minimum priorities or a merged NaN priority require O(capacity) scans to preserve slot selection. Relaxed mode scans per CAS attempt and can retry under contention.
Concurrency: Concurrency.Strict and Concurrency.HighWrite use an outer lock across updates, merges, reads, and resets. Concurrency.Relaxed uses CAS admission of each key/value pair and a separate RNG lock; reads are not an atomic snapshot across slots, and no sampling-error bound is guaranteed under contention. Concurrency.None runs without synchronisation.
Constructors
ReservoirHistogramStat
Properties
capacity
concurrency
Thread-safety mode fixed at construction. See Concurrency for the shared contract and the implementation's KDoc for its cell and lock strategy.
seed
Functions
create
Derives the copy's seed rather than passing this stat's own.
A windowed reservoir builds one of these per slice, and slices sharing a seed would draw the same admission keys in the same order - so whether an observation survived would turn on its position within its slice rather than on its weight. The trees and forests derive for the same reason; advancing a counter here is what makes each copy distinct.
merge
Fold a compatible snapshot values into this accumulator at timestampNanos. Implementations document whether the combine is exact or approximate and which state the snapshot omits. The source accumulator is unaffected.
Time-dependent statistics, such as windows, place the snapshot at timestampNanos, which must share the clock the caller uses for updates and reads. Time-independent statistics ignore it.
read
Return the current result evaluated at timestampNanos. Time-independent statistics ignore the timestamp. Reading does not add an observation.
Snapshot consistency depends on the implementation's synchronisation. Locks protect coupled state under Concurrency.Strict and Concurrency.HighWrite; independently updated cells and JVM striped adders can still yield a best-effort snapshot. Concurrency.Relaxed permits drift across coupled cells.
reset
Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.