kumulant

SpaceSavingStat

class SpaceSavingStat(val capacity: Int, val concurrency: Concurrency = Concurrency.None) : DiscreteStat<HeavyHittersResult> (source)

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

constructor(capacity: Int, concurrency: Concurrency = Concurrency.None)(source)

Properties

capacity

Maximum number of distinct keys tracked; smaller capacity means looser heavy-hitter guarantees but lower memory.

concurrency

open override val concurrency: Concurrency(source)

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

open override fun create(concurrency: Concurrency? = null): SpaceSavingStat(source)

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

open override fun merge(values: HeavyHittersResult, timestampNanos: Long = currentTimeNanos())(source)

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

open override fun read(timestampNanos: Long = currentTimeNanos()): HeavyHittersResult(source)

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

open override fun reset()(source)

Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.

validateMerge

open override fun validateMerge(values: HeavyHittersResult)(source)

Validate values for merge without changing accumulator state. Containers validate every child before merging any of them. Successful validation checks compatibility; it does not make a concurrent merge transactional.

Link copied to clipboard
open fun update(value: Long, weight: Double = 1.0)

Record value with weight at the current monotonic time.

open override fun update(value: Long, timestampNanos: Long, weight: Double = 1.0)

Record value with weight at timestampNanos.

update

open override fun update(value: Long, timestampNanos: Long, weight: Double = 1.0)(source)

Record value with weight at timestampNanos.