Skip to main content

Module cpc

Module cpc 

Source
Expand description

CPC (Compressed Probabilistic Counting) sketch family: cardinality estimation with a more compact serialized form than HLL or Theta.

Unlike the theta module, CPC has no set operations beyond union — no intersection, a-not-b, or Jaccard similarity.

use apache_datasketches::cpc::CpcSketchBuilder;

let mut sketch = CpcSketchBuilder::new().lg_k(11).build()?;
sketch.update_u64(42);
println!("estimate: {}", sketch.get_estimate());
  • CpcSketch / CpcSketchBuilder — the sketch; build with CpcSketchBuilder::new().lg_k(..).build().
  • CpcUnion / CpcUnionBuilder — merges multiple sketches.
  • get_max_serialized_size_bytes — the estimated maximum compressed serialized size, in bytes, for a given lg_k.
  • init — eagerly initializes CPC’s global decompression tables, as a one-time latency optimization; see its own doc comment for details.

Structs§

CpcSketch
A CPC (Compressed Probabilistic Counting) sketch: estimates the number of distinct items added via update_*. Unlike Theta’s ThetaSketch, there is no separate compact/wrapped variant — this single type is both the mutable/update type and the serializable type, since CPC’s serialized form is always compressed by construction. Build one with CpcSketchBuilder.
CpcSketchBuilder
Builder for crate::cpc::CpcSketch, mirroring upstream’s cpc_sketch_alloc constructor. lg_k defaults to 11 (cpc_constants::DEFAULT_LG_K). The seed is never exposed — every sketch built by this crate always uses upstream’s DEFAULT_SEED.
CpcUnion
Merges multiple CpcSketches into one, e.g. combining per-shard or per-day counts into a total distinct count across all of them. Build one with CpcUnionBuilder.
CpcUnionBuilder
Builder for crate::cpc::CpcUnion, mirroring upstream’s cpc_union_alloc constructor. lg_k defaults to 11 (cpc_constants::DEFAULT_LG_K). The seed is never exposed, same as CpcSketchBuilder.

Functions§

get_max_serialized_size_bytes
The estimated maximum compressed serialized size, in bytes, of a CPC sketch built with the given lg_k. Useful for pre-allocating buffers.
init
Eagerly initializes CPC’s global decompression tables, used during serialization/deserialization.