SpaceSavingStat
Heavy-hitters tracker; keeps the capacity most-frequent keys with one-sided overestimate guarantees on their counts.
Use cases: top-k key discovery under bounded memory; most-frequent users, top error fingerprints, hot cache keys. Pair with CountMinSketchStat if you need point-frequency lookups on arbitrary keys, not just the top set.
Memory: O(capacity) keys, counts and errors, plus a key-to-slot index in classic mode.
Update: Expected O(1) for a tracked key in classic mode; O(capacity) on a miss. Relaxed mode scans the slots and may retry under contention.
Concurrency: Two algorithms run depending on level:
Concurrency.None, Concurrency.Strict, Concurrency.HighWrite: classic Space-Saving (Metwally, Agrawal, El Abbadi 2005). On a miss when full, the minimum-count slot is evicted and the new key inherits the old count plus its weight, with the old count recorded as the new key's overestimate bound. Reported counts are one-sided overestimates:
count >= true count, gap <=error. Strict/HighWrite serialise against reads/merges via an outer lock.Concurrency.Relaxed: lock-free Misra-Gries variant. On a miss when full, all counts are decremented by one in best-effort fashion; a freed slot is claimed via CAS. Counts under this mode are not overestimates; they underestimate by at most the number of decrement rounds, which HeavyHittersResult.deficit reports. Heavy hitters still surface; small/cold keys are bled out.
Constructors
SpaceSavingStat
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.
Functions
create
Create an independent accumulator at the configured baseline. A null concurrency preserves this accumulator's mode; a non-null value overrides it. Modality subtypes narrow the return type to preserve the input shape.
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.