Expand description
Generic Tuple sketches: cardinality estimation where each distinct key carries a summary of a type you define.
Implement TupleSummary on your own type and it becomes usable as a
sketch summary. C++ calls back into Rust to clone and combine summaries;
see TupleSummary’s documentation for which methods must not panic.
For the common case of a fixed-width array of f64 per key, prefer
ArrayOfDoublesSketch — it binds a
concrete C++ instantiation with no callback overhead.
use apache_datasketches::tuple::generic::{TupleSketch, TupleSketchBuilder, TupleSummary};
#[derive(Clone)]
struct Count(u64);
impl TupleSummary for Count {
type Update = ();
fn create(_: &()) -> Self { Count(1) }
fn union_combine(&mut self, other: &Self) { self.0 += other.0; }
fn intersection_combine(&mut self, other: &Self) { self.0 += other.0; }
}
let mut sketch: TupleSketch<Count> = TupleSketchBuilder::new().build()?;
for key in 0..100u64 {
sketch.update_u64(key, &());
}
// Updating a key that is already present combines the new summary into
// the retained one with `union_combine`, so key 42 ends up at 2.
sketch.update_u64(42, &());
println!("estimate: {}", sketch.get_estimate());
// `compact` freezes the sketch into an immutable snapshot; `entries`
// hands back each retained `(hash, summary)` pair, with the summary
// cloned back out of C++ as an owned `Count`.
let compact = sketch.compact(true);
assert_eq!(compact.get_num_retained(), 100);
assert!(compact.is_ordered());
let entries: Vec<(u64, Count)> = compact.entries().collect();
assert_eq!(entries.len(), 100);
// 99 keys were seen once and key 42 was seen twice.
assert_eq!(entries.iter().map(|(_, c)| c.0).sum::<u64>(), 101);Structs§
- Compact
Tuple Sketch - An immutable snapshot of a generic Tuple sketch, produced by
TupleSketch::compactor by any set operation’s result. - Tuple
AnotB - Computes the set difference
a - bover generic Tuple sketches. - Tuple
Intersection - Computes the intersection of generic Tuple sketches fed via
Self::update. Summaries of keys present in every input are merged withTupleSummary::intersection_combine; a key present in only some of the inputs is dropped and its summary never reaches the callback. - Tuple
Sketch - A mutable, update-only Tuple sketch carrying a user-defined summary
Sper distinct key. Build one withTupleSketchBuilder. - Tuple
Sketch Builder - Builder for
TupleSketch, mirroring upstream’supdate_tuple_sketch::builder.lg_kdefaults to12,resize_factortoResizeFactor::X8, andpto1.0(no sampling). The seed is never exposed. - Tuple
Union - A streaming union over generic Tuple sketches. Summaries for a key present
in more than one input are merged with
TupleSummary::union_combine. - Tuple
Union Builder - Builder for
TupleUnion.lg_kdefaults to12,resize_factortoResizeFactor::X8,pto1.0.
Traits§
- Tuple
Input - Either generic Tuple sketch type can be fed into this module’s set operations. Sealed — the shims have concrete overloads for these two types only.
- Tuple
Summary - A user-defined per-entry summary for
TupleSketch.
Functions§
- tuple_
jaccard_ similarity - Estimates the Jaccard index (intersection-over-union) of two generic Tuple sketches.