This is an automated email from the ASF dual-hosted git repository.
tisonkun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
The following commit(s) were added to refs/heads/main by this push:
new 488ae30 feat: complete diagnostic Debug summaries across sketches
(#352)
488ae30 is described below
commit 488ae302b26541312f441a31cd170407245ae972
Author: tison <[email protected]>
AuthorDate: Thu Nov 12 09:48:13 2026 +0800
feat: complete diagnostic Debug summaries across sketches (#352)
---
CHANGELOG.md | 4 ++
datasketches/src/bloom/sketch.rs | 15 ++++++-
datasketches/src/countmin/sketch.rs | 15 ++++++-
.../src/frequencies/reverse_purge_item_hash_map.rs | 12 +++---
datasketches/src/frequencies/sketch.rs | 16 +++++++-
datasketches/src/hll/union.rs | 18 +++++++-
datasketches/src/kll/sketch.rs | 19 ++++++++-
datasketches/src/kll/sorted_view.rs | 14 ++++++-
datasketches/src/req/sketch.rs | 20 ++++++++-
datasketches/src/req/sorted_view.rs | 15 ++++++-
datasketches/src/tdigest/sketch.rs | 30 +++++++++++++-
.../src/thetafamily/common/intersection.rs | 26 ++++++++++++
datasketches/src/thetafamily/common/union.rs | 27 ++++++++++++
datasketches/src/thetafamily/theta/intersection.rs | 9 +++-
datasketches/src/thetafamily/theta/sketch.rs | 48 ++++++++++++++++++++--
datasketches/src/thetafamily/theta/union.rs | 9 +++-
datasketches/src/thetafamily/tuple/intersection.rs | 9 +++-
datasketches/src/thetafamily/tuple/sketch.rs | 46 +++++++++++++++++++--
datasketches/src/thetafamily/tuple/union.rs | 9 +++-
datasketches/src/xor/filter.rs | 26 +++++++++++-
20 files changed, 359 insertions(+), 28 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce4e41e..5eb32a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,10 @@ All significant changes to this project will be documented
in this file.
* Add `CpcUnion::serialize`, `HllUnion::serialize`, `ThetaUnion::serialize`,
and `TupleUnion::serialize` to encode union results without calling
`to_sketch`. Direct serialization avoids intermediate sketch allocations for
`HllUnion` and `CpcUnion`, writes an `HllType::Hll8` image at the union's
effective `lg_k` for `HllUnion`, and encodes `TupleUnion` summaries without
requiring `Clone` or cloning heap-backed summaries.
* Add `XorFilter`, `XorFilterBuilder`, and `XorFilterType` behind the `xor`
feature for immutable membership queries on values or precomputed hashes.
Filters support 8-, 16-, and 32-bit fingerprints and can be serialized for
later queries.
+### Improvements
+
+* Improve the readability of `Debug` output across sketches, filters, views,
and stateful set operators, and implement `Debug` for `TDigest`.
+
### Performance improvements
* Speed up `BloomFilter::insert` when adding new items to filters configured
with multiple hash functions; single-hash, repeated-item, and saturated-filter
workloads see smaller gains.
diff --git a/datasketches/src/bloom/sketch.rs b/datasketches/src/bloom/sketch.rs
index 678801c..b1ec95c 100644
--- a/datasketches/src/bloom/sketch.rs
+++ b/datasketches/src/bloom/sketch.rs
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
@@ -40,7 +41,7 @@ const EMPTY_FLAG_MASK: u8 = 1 << 2;
/// * Constant space usage
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Clone, PartialEq)]
pub struct BloomFilter {
/// Hash seed for all hash functions
seed: u64,
@@ -650,6 +651,18 @@ impl BloomFilter {
}
}
+impl fmt::Debug for BloomFilter {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("BloomFilter")
+ .field("seed", &self.seed)
+ .field("num_hashes", &self.num_hashes)
+ .field("capacity", &self.capacity())
+ .field("bits_used", &self.num_bits_set)
+ .field("is_empty", &self.is_empty())
+ .finish()
+ }
+}
+
/// Builder for creating [`BloomFilter`] instances.
///
/// Provides two construction modes:
diff --git a/datasketches/src/countmin/sketch.rs
b/datasketches/src/countmin/sketch.rs
index 88bf287..1784cdf 100644
--- a/datasketches/src/countmin/sketch.rs
+++ b/datasketches/src/countmin/sketch.rs
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
@@ -46,7 +47,7 @@ const MAX_TABLE_ENTRIES: usize = 1 << 30;
/// with configurable relative error and confidence.
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Clone, PartialEq)]
pub struct CountMinSketch<T: CountMinValue> {
num_hashes: u8,
buckets: BucketCount,
@@ -61,6 +62,18 @@ pub struct CountMinSketch<T: CountMinValue> {
hash_seeds: Vec<u64>,
}
+impl<T: CountMinValue + fmt::Debug> fmt::Debug for CountMinSketch<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("CountMinSketch")
+ .field("num_hashes", &self.num_hashes)
+ .field("num_buckets", &self.buckets.count)
+ .field("seed", &self.seed)
+ .field("is_empty", &(self.total_weight == T::ZERO))
+ .field("total_weight", &self.total_weight)
+ .finish()
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum SerialVersion {
diff --git a/datasketches/src/frequencies/reverse_purge_item_hash_map.rs
b/datasketches/src/frequencies/reverse_purge_item_hash_map.rs
index bb5ebff..69aa20f 100644
--- a/datasketches/src/frequencies/reverse_purge_item_hash_map.rs
+++ b/datasketches/src/frequencies/reverse_purge_item_hash_map.rs
@@ -41,6 +41,13 @@ pub struct ReversePurgeItemHashMap<T> {
num_active: usize,
}
+impl<T> ReversePurgeItemHashMap<T> {
+ /// Returns the number of active keys in the map.
+ pub fn num_active(&self) -> usize {
+ self.num_active
+ }
+}
+
impl<T: Eq + Hash> ReversePurgeItemHashMap<T> {
/// Creates a new map with arrays of length `map_size` (must be a power of
two).
///
@@ -210,11 +217,6 @@ impl<T: Eq + Hash> ReversePurgeItemHashMap<T> {
self.load_threshold
}
- /// Returns the number of active keys in the map.
- pub fn num_active(&self) -> usize {
- self.num_active
- }
-
/// Returns an estimate of memory usage in bytes.
pub fn estimated_size(&self) -> usize {
self.keys.capacity() * size_of::<Option<T>>()
diff --git a/datasketches/src/frequencies/sketch.rs
b/datasketches/src/frequencies/sketch.rs
index 0ecb50e..efcdfd8 100644
--- a/datasketches/src/frequencies/sketch.rs
+++ b/datasketches/src/frequencies/sketch.rs
@@ -18,6 +18,7 @@
//! Frequent items sketch implementations.
use std::borrow::Borrow;
+use std::fmt;
use std::hash::Hash;
use crate::codec::SketchBytes;
@@ -141,7 +142,7 @@ impl<T> Row<T> {
/// guaranteed upper and lower bounds.
///
/// See the [module level documentation](super) for an overview and error
guarantees.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct FrequentItemsSketch<T> {
lg_max_map_size: u8,
cur_map_cap: usize,
@@ -151,6 +152,19 @@ pub struct FrequentItemsSketch<T> {
hash_map: ReversePurgeItemHashMap<T>,
}
+impl<T> fmt::Debug for FrequentItemsSketch<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("FrequentItemsSketch")
+ .field("lg_max_map_size", &self.lg_max_map_size)
+ .field("cur_map_cap", &self.cur_map_cap)
+ .field("is_empty", &(self.stream_weight == 0))
+ .field("num_active_items", &self.hash_map.num_active())
+ .field("total_weight", &self.stream_weight)
+ .field("maximum_error", &self.offset)
+ .finish()
+ }
+}
+
impl<T: Eq + Hash> FrequentItemsSketch<T> {
/// Creates a new sketch with the given maximum map size (power of two).
///
diff --git a/datasketches/src/hll/union.rs b/datasketches/src/hll/union.rs
index a9f0093..202e6f8 100644
--- a/datasketches/src/hll/union.rs
+++ b/datasketches/src/hll/union.rs
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
use std::hash::Hash;
use crate::common::NumStdDev;
@@ -36,13 +37,28 @@ use crate::hll::mode::Mode;
/// its statistical accuracy.
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct HllUnion {
lg_max_k: u8,
/// Array modes always use `Array8`; packed results are produced by
`to_sketch`.
gadget: HllSketch,
}
+impl fmt::Debug for HllUnion {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("HllUnion")
+ .field("lg_max_k", &self.lg_max_k)
+ .field("lg_config_k", &self.gadget.lg_config_k())
+ .field("is_empty", &self.is_empty())
+ .field("estimate", &self.estimate())
+ .field(
+ "bounds",
+
&(self.lower_bound(NumStdDev::One)..=self.upper_bound(NumStdDev::One)),
+ )
+ .finish()
+ }
+}
+
impl HllUnion {
/// Creates an empty union with a maximum of `2^lg_max_k` registers.
///
diff --git a/datasketches/src/kll/sketch.rs b/datasketches/src/kll/sketch.rs
index 17e13cc..dc68d78 100644
--- a/datasketches/src/kll/sketch.rs
+++ b/datasketches/src/kll/sketch.rs
@@ -16,6 +16,7 @@
// under the License.
use std::cmp::Ordering;
+use std::fmt;
use super::DEFAULT_K;
use super::DEFAULT_M;
@@ -49,7 +50,7 @@ use crate::error::Error;
/// KLL sketch for estimating quantiles and ranks.
///
/// See the [kll module level documentation](crate::kll) for more.
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Clone, PartialEq)]
pub struct KllSketch<T> {
k: u16,
m: u8,
@@ -63,6 +64,22 @@ pub struct KllSketch<T> {
max_item: Option<T>,
}
+impl<T: fmt::Debug> fmt::Debug for KllSketch<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("KllSketch")
+ .field("k", &self.k)
+ .field("min_k", &self.min_k)
+ .field("n", &self.n)
+ .field("is_empty", &(self.n == 0))
+ .field("is_estimation_mode", &(self.levels.len() > 1))
+ .field("num_levels", &self.levels.len())
+ .field("num_retained", &self.num_retained)
+ .field("min_item", &self.min_item)
+ .field("max_item", &self.max_item)
+ .finish()
+ }
+}
+
impl<T: Clone + Ord> Default for KllSketch<T> {
fn default() -> Self {
Self::make(DEFAULT_K, DEFAULT_K, 0, vec![Vec::new()], None, None,
false)
diff --git a/datasketches/src/kll/sorted_view.rs
b/datasketches/src/kll/sorted_view.rs
index 35eba5d..bc5fa4e 100644
--- a/datasketches/src/kll/sorted_view.rs
+++ b/datasketches/src/kll/sorted_view.rs
@@ -16,6 +16,7 @@
// under the License.
use std::cmp::Ordering;
+use std::fmt;
use crate::common::SearchCriteria;
use crate::error::Error;
@@ -24,7 +25,7 @@ use crate::error::Error;
///
/// Build one with [`KllSketch::sorted_view`](super::KllSketch::sorted_view)
when running repeated
/// queries against the same sketch state.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct SortedView<T: Clone> {
entries: Vec<Entry<T>>,
total_weight: u64,
@@ -32,6 +33,17 @@ pub struct SortedView<T: Clone> {
max_item: Option<T>,
}
+impl<T: Clone + fmt::Debug> fmt::Debug for SortedView<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("SortedView")
+ .field("len", &self.entries.len())
+ .field("total_weight", &self.total_weight)
+ .field("min_item", &self.min_item)
+ .field("max_item", &self.max_item)
+ .finish()
+ }
+}
+
#[derive(Debug, Clone)]
struct Entry<T> {
item: T,
diff --git a/datasketches/src/req/sketch.rs b/datasketches/src/req/sketch.rs
index 3740ca6..abc240e 100644
--- a/datasketches/src/req/sketch.rs
+++ b/datasketches/src/req/sketch.rs
@@ -17,6 +17,8 @@
//! Generic REQ sketch implementation.
+use std::fmt;
+
use crate::codec::SketchBytes;
use crate::codec::SketchSlice;
use crate::codec::assert::insufficient_data;
@@ -47,7 +49,7 @@ use crate::req::value::ReqValue;
/// A Relative Error Quantiles sketch for approximate quantile estimation.
///
/// See the [module level documentation](super) for item ordering and
floating-point requirements.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct ReqSketch<T> {
k: u16,
rank_accuracy: RankAccuracy,
@@ -60,6 +62,22 @@ pub struct ReqSketch<T> {
max_item: Option<T>,
}
+impl<T: fmt::Debug> fmt::Debug for ReqSketch<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("ReqSketch")
+ .field("k", &self.k)
+ .field("rank_accuracy", &self.rank_accuracy)
+ .field("n", &self.n)
+ .field("is_empty", &(self.n == 0))
+ .field("is_estimation_mode", &(self.compactors.len() > 1))
+ .field("num_levels", &self.compactors.len())
+ .field("num_retained", &self.num_retained)
+ .field("min_item", &self.min_item)
+ .field("max_item", &self.max_item)
+ .finish()
+ }
+}
+
impl<T> Default for ReqSketch<T>
where
T: Clone + Ord,
diff --git a/datasketches/src/req/sorted_view.rs
b/datasketches/src/req/sorted_view.rs
index c0cdd78..9e13530 100644
--- a/datasketches/src/req/sorted_view.rs
+++ b/datasketches/src/req/sorted_view.rs
@@ -17,6 +17,8 @@
//! Sorted view implementation for efficient quantile queries.
+use std::fmt;
+
use crate::common::SearchCriteria;
use crate::error::Error;
@@ -27,7 +29,7 @@ use crate::error::Error;
/// receiving updates, and it keeps answering from the state it was taken at.
Building it costs
/// `O(retained ยท log retained)`; each subsequent query is `O(log retained)`,
so it is the right
/// tool for repeated quantile/rank queries.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct SortedView<T> {
items: Vec<T>,
cumulative_weights: Vec<u64>,
@@ -36,6 +38,17 @@ pub struct SortedView<T> {
max_item: Option<T>,
}
+impl<T: fmt::Debug> fmt::Debug for SortedView<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("SortedView")
+ .field("len", &self.items.len())
+ .field("total_weight", &self.total_weight)
+ .field("min_item", &self.min_item)
+ .field("max_item", &self.max_item)
+ .finish()
+ }
+}
+
impl<T> SortedView<T>
where
T: Clone + Ord,
diff --git a/datasketches/src/tdigest/sketch.rs
b/datasketches/src/tdigest/sketch.rs
index e04811e..55d7549 100644
--- a/datasketches/src/tdigest/sketch.rs
+++ b/datasketches/src/tdigest/sketch.rs
@@ -16,6 +16,7 @@
// under the License.
use std::cmp::Ordering;
+use std::fmt;
use std::num::NonZeroU64;
use crate::codec::SketchBytes;
@@ -38,7 +39,7 @@ const DEFAULT_WEIGHT: NonZeroU64 =
NonZeroU64::new(1).unwrap();
/// T-Digest sketch for estimating quantiles and ranks.
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct TDigestMut {
k: u16,
@@ -52,6 +53,20 @@ pub struct TDigestMut {
compressed_weight: u64,
}
+impl fmt::Debug for TDigestMut {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TDigestMut")
+ .field("k", &self.k)
+ .field("is_empty", &self.is_empty())
+ .field("total_weight", &self.total_weight())
+ .field("compressed_centroids",
&self.buffer.compressed_prefix_len())
+ .field("unmerged_values", &self.buffer.unmerged_len())
+ .field("min_value", &self.min_value())
+ .field("max_value", &self.max_value())
+ .finish()
+ }
+}
+
impl Default for TDigestMut {
fn default() -> Self {
Self::from_parts(
@@ -603,6 +618,19 @@ pub struct TDigest {
centroids_weight: u64,
}
+impl fmt::Debug for TDigest {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TDigest")
+ .field("k", &self.k)
+ .field("is_empty", &self.is_empty())
+ .field("total_weight", &self.total_weight())
+ .field("num_centroids", &self.centroids.len())
+ .field("min_value", &self.min_value())
+ .field("max_value", &self.max_value())
+ .finish()
+ }
+}
+
impl TDigest {
/// Returns the compression parameter `k` used to configure this t-digest.
pub fn k(&self) -> u16 {
diff --git a/datasketches/src/thetafamily/common/intersection.rs
b/datasketches/src/thetafamily/common/intersection.rs
index 6b03629..c7db3c4 100644
--- a/datasketches/src/thetafamily/common/intersection.rs
+++ b/datasketches/src/thetafamily/common/intersection.rs
@@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
+
use crate::error::Error;
use crate::error::ErrorKind;
use crate::hash::check_seed_hash;
@@ -239,6 +241,30 @@ where
size_of::<P>() + self.table.estimated_size()
}
+ /// Formats a concise diagnostic summary for Theta-family intersection
operators.
+ pub fn fmt_debug(&self, f: &mut fmt::Formatter<'_>, name: &str) ->
fmt::Result {
+ let mut debug = f.debug_struct(name);
+ debug
+ .field("seed_hash", &self.table.seed_hash())
+ .field("has_result", &self.has_result());
+ match self.result_state {
+ IntersectionResultState::Uninitialized => {}
+ IntersectionResultState::Empty => {
+ debug
+ .field("is_empty", &true)
+ .field("theta64", &MAX_THETA)
+ .field("num_retained", &0usize);
+ }
+ IntersectionResultState::NonEmpty => {
+ debug
+ .field("is_empty", &false)
+ .field("theta64", &self.table.retention_theta())
+ .field("num_retained", &self.table.num_retained());
+ }
+ }
+ debug.finish()
+ }
+
/// Returns the current intersection as canonical compact-sketch state.
pub fn to_compact_sketch_state(&self, ordered: bool) ->
Option<CompactSketchState<E>>
where
diff --git a/datasketches/src/thetafamily/common/union.rs
b/datasketches/src/thetafamily/common/union.rs
index 5d53862..4ff6336 100644
--- a/datasketches/src/thetafamily/common/union.rs
+++ b/datasketches/src/thetafamily/common/union.rs
@@ -15,12 +15,15 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
+
use crate::common::ResizeFactor;
use crate::error::Error;
use crate::error::ErrorKind;
use crate::hash::check_seed_hash;
use crate::thetacommon::EntrySketch;
use crate::thetacommon::SketchEntry;
+use crate::thetacommon::constants::MAX_THETA;
use crate::thetacommon::hash_table::SketchHashTable;
use crate::thetacommon::sketch_state::CompactSketchState;
use crate::thetacommon::sketch_state::ThetaFamilySketchMetadata;
@@ -197,4 +200,28 @@ where
pub fn estimated_size(&self) -> usize {
size_of::<P>() + self.table.estimated_size()
}
+
+ /// Formats a concise diagnostic summary for Theta-family union operators.
+ pub fn fmt_debug(&self, f: &mut fmt::Formatter<'_>, name: &str) ->
fmt::Result {
+ let is_empty = self.result_theta.is_none();
+ let union_theta = self
+ .result_theta
+ .map_or(MAX_THETA, |theta|
theta.min(self.table.retention_theta()));
+ let num_retained = match self.result_theta {
+ None => 0,
+ Some(theta) if theta >= self.table.retention_theta() =>
self.table.num_retained(),
+ Some(theta) => self
+ .table
+ .iter_entries()
+ .filter(|entry| entry.hash() < theta)
+ .count(),
+ };
+ f.debug_struct(name)
+ .field("lg_k", &self.table.lg_nom_size())
+ .field("seed_hash", &self.table.seed_hash())
+ .field("is_empty", &is_empty)
+ .field("union_theta", &union_theta)
+ .field("num_retained", &num_retained)
+ .finish()
+ }
}
diff --git a/datasketches/src/thetafamily/theta/intersection.rs
b/datasketches/src/thetafamily/theta/intersection.rs
index 133c5a7..3425aed 100644
--- a/datasketches/src/thetafamily/theta/intersection.rs
+++ b/datasketches/src/thetafamily/theta/intersection.rs
@@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
+
use crate::error::Error;
use crate::hash::DEFAULT_UPDATE_SEED;
use crate::theta::CompactThetaSketch;
@@ -28,11 +30,16 @@ use crate::thetacommon::intersection::IntersectionState;
/// A newly created operator has no result. [`has_result`](Self::has_result)
returns `false` and
/// [`to_sketch`](Self::to_sketch) returns `None` until the first successful
/// [`update`](Self::update).
-#[derive(Debug)]
pub struct ThetaIntersection {
state: IntersectionState<ThetaEntry, NoopIntersectionPolicy>,
}
+impl fmt::Debug for ThetaIntersection {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.state.fmt_debug(f, "ThetaIntersection")
+ }
+}
+
impl Default for ThetaIntersection {
fn default() -> Self {
Self::with_seed(DEFAULT_UPDATE_SEED).unwrap()
diff --git a/datasketches/src/thetafamily/theta/sketch.rs
b/datasketches/src/thetafamily/theta/sketch.rs
index 40d9025..a973a20 100644
--- a/datasketches/src/thetafamily/theta/sketch.rs
+++ b/datasketches/src/thetafamily/theta/sketch.rs
@@ -20,6 +20,7 @@
//! This module provides ThetaSketch (mutable) and CompactThetaSketch
(immutable)
//! for cardinality estimation.
+use std::fmt;
use std::hash::Hash;
use std::iter::FusedIterator;
use std::slice;
@@ -73,15 +74,27 @@ use
crate::thetacommon::sketch_state::ThetaFamilySketchMetadata;
/// let view = sketch.as_view();
/// assert_eq!(view.num_retained(), 1);
/// ```
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy)]
pub struct ThetaSketchView<'a>(ThetaSketchViewState<'a>);
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy)]
enum ThetaSketchViewState<'a> {
Mutable(&'a ThetaSketch),
Compact(&'a CompactThetaSketch),
}
+impl fmt::Debug for ThetaSketchView<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("ThetaSketchView")
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_ordered", &self.is_ordered())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .finish()
+ }
+}
+
enum ThetaSketchIter<'a> {
Mutable(SketchHashTableIter<'a, ThetaEntry>),
Compact(slice::Iter<'a, u64>),
@@ -222,13 +235,26 @@ impl<'a> From<&'a CompactThetaSketch> for
ThetaSketchView<'a> {
/// Mutable theta sketch for building from input data.
///
/// See the [module level documentation](super) for more.
-#[derive(Debug)]
pub struct ThetaSketch {
table: ThetaHashTable,
// Public emptiness tracks update calls, not retained entries: theta may
screen every update.
is_empty: bool,
}
+impl fmt::Debug for ThetaSketch {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("ThetaSketch")
+ .field("lg_k", &self.lg_k())
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_estimation_mode", &self.is_estimation_mode())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .field("estimate", &self.estimate())
+ .finish()
+ }
+}
+
impl ThetaSketch {
/// Returns a read-only view accepted by Theta set operations.
pub fn as_view(&self) -> ThetaSketchView<'_> {
@@ -452,11 +478,25 @@ impl ThetaSketch {
///
/// This is the serialized-friendly form of a theta sketch: a compact array of
retained hash values
/// plus theta and a 16-bit seed hash. It can be ordered (sorted ascending) or
unordered.
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct CompactThetaSketch {
compact_state: CompactSketchState<u64>,
}
+impl fmt::Debug for CompactThetaSketch {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("CompactThetaSketch")
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_ordered", &self.is_ordered())
+ .field("is_estimation_mode", &self.is_estimation_mode())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .field("estimate", &self.estimate())
+ .finish()
+ }
+}
+
impl CompactThetaSketch {
pub(super) fn from_compact_state(compact_state: CompactSketchState<u64>)
-> Self {
Self { compact_state }
diff --git a/datasketches/src/thetafamily/theta/union.rs
b/datasketches/src/thetafamily/theta/union.rs
index eee30de..3da51ed 100644
--- a/datasketches/src/thetafamily/theta/union.rs
+++ b/datasketches/src/thetafamily/theta/union.rs
@@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
+
use crate::common::ResizeFactor;
use crate::error::Error;
use crate::hash::DEFAULT_UPDATE_SEED;
@@ -27,11 +29,16 @@ use crate::thetacommon::union::UnionMergePolicy;
use crate::thetacommon::union::UnionState;
/// Stateful union operator for Theta sketches.
-#[derive(Debug)]
pub struct ThetaUnion {
state: UnionState<ThetaEntry, NoopUnionPolicy>,
}
+impl fmt::Debug for ThetaUnion {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.state.fmt_debug(f, "ThetaUnion")
+ }
+}
+
#[derive(Debug)]
struct NoopUnionPolicy;
diff --git a/datasketches/src/thetafamily/tuple/intersection.rs
b/datasketches/src/thetafamily/tuple/intersection.rs
index 58dce8f..28e6974 100644
--- a/datasketches/src/thetafamily/tuple/intersection.rs
+++ b/datasketches/src/thetafamily/tuple/intersection.rs
@@ -25,6 +25,8 @@
//! Unlike the union there is no default policy: how to combine the summaries
of keys present in
//! both inputs is application-specific, so a policy must always be supplied.
+use std::fmt;
+
use crate::error::Error;
use crate::hash::DEFAULT_UPDATE_SEED;
use crate::thetacommon::intersection::IntersectionState;
@@ -84,7 +86,6 @@ use crate::tuple::sketch::TupleSketchView;
/// assert_eq!(result.num_retained(), 1); // only "shared"
/// assert_eq!(result.iter().next().unwrap().summary(), &7); // 3 + 4
/// ```
-#[derive(Debug)]
pub struct TupleIntersection<P>
where
P: SummaryCombinePolicy,
@@ -92,6 +93,12 @@ where
state: IntersectionState<TupleEntry<P::Summary>, P>,
}
+impl<P: SummaryCombinePolicy> fmt::Debug for TupleIntersection<P> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.state.fmt_debug(f, "TupleIntersection")
+ }
+}
+
impl<P> TupleIntersection<P>
where
P: SummaryCombinePolicy,
diff --git a/datasketches/src/thetafamily/tuple/sketch.rs
b/datasketches/src/thetafamily/tuple/sketch.rs
index c529083..0abeecc 100644
--- a/datasketches/src/thetafamily/tuple/sketch.rs
+++ b/datasketches/src/thetafamily/tuple/sketch.rs
@@ -22,6 +22,7 @@
//! created by a [`SummaryPolicy`] and updated through one or more
[`SummaryUpdatePolicy`]
//! implementations.
+use std::fmt;
use std::hash::Hash;
use std::iter::FusedIterator;
use std::slice;
@@ -78,10 +79,8 @@ use
crate::tuple::serialization::serialize_compact_uncompressed;
/// let view = sketch.as_view();
/// assert_eq!(view.iter().next().unwrap().summary(), &1);
/// ```
-#[derive(Debug)]
pub struct TupleSketchView<'a, S>(TupleSketchViewState<'a, S>);
-#[derive(Debug)]
enum TupleSketchViewState<'a, S> {
Mutable {
table: &'a TupleHashTable<S>,
@@ -90,6 +89,18 @@ enum TupleSketchViewState<'a, S> {
Compact(&'a CompactTupleSketch<S>),
}
+impl<S> fmt::Debug for TupleSketchView<'_, S> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TupleSketchView")
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_ordered", &self.is_ordered())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .finish()
+ }
+}
+
enum TupleSketchIter<'a, S> {
Mutable(SketchHashTableIter<'a, TupleEntry<S>>),
Compact(slice::Iter<'a, TupleEntry<S>>),
@@ -279,7 +290,6 @@ impl<'a, S> From<&'a CompactTupleSketch<S>> for
TupleSketchView<'a, S> {
/// assert!(sketch.estimate() >= 1.0);
/// assert_eq!(sketch.num_retained(), 1);
/// ```
-#[derive(Debug)]
pub struct TupleSketch<P>
where
P: SummaryPolicy,
@@ -290,6 +300,20 @@ where
policy: P,
}
+impl<P: SummaryPolicy> fmt::Debug for TupleSketch<P> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TupleSketch")
+ .field("lg_k", &self.lg_k())
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_estimation_mode", &self.is_estimation_mode())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .field("estimate", &self.estimate())
+ .finish()
+ }
+}
+
impl<P> TupleSketch<P>
where
P: SummaryPolicy,
@@ -467,11 +491,25 @@ where
///
/// This is the serialization-friendly form: a compact array of retained
hash-summary pairs plus
/// theta and a 16-bit seed hash. It can be ordered (sorted ascending by hash)
or unordered.
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct CompactTupleSketch<S> {
compact_state: CompactSketchState<TupleEntry<S>>,
}
+impl<S> fmt::Debug for CompactTupleSketch<S> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("CompactTupleSketch")
+ .field("seed_hash", &self.seed_hash())
+ .field("is_empty", &self.is_empty())
+ .field("is_ordered", &self.is_ordered())
+ .field("is_estimation_mode", &self.is_estimation_mode())
+ .field("num_retained", &self.num_retained())
+ .field("theta64", &self.theta64())
+ .field("estimate", &self.estimate())
+ .finish()
+ }
+}
+
impl<S> CompactTupleSketch<S> {
pub(super) fn from_compact_state(compact_state:
CompactSketchState<TupleEntry<S>>) -> Self {
Self { compact_state }
diff --git a/datasketches/src/thetafamily/tuple/union.rs
b/datasketches/src/thetafamily/tuple/union.rs
index f2d419a..55d477d 100644
--- a/datasketches/src/thetafamily/tuple/union.rs
+++ b/datasketches/src/thetafamily/tuple/union.rs
@@ -22,6 +22,8 @@
//! already exists in the union, the two summaries are combined with a
[`SummaryCombinePolicy`]
//! instead of one being dropped.
+use std::fmt;
+
use crate::common::ResizeFactor;
use crate::error::Error;
use crate::hash::DEFAULT_UPDATE_SEED;
@@ -66,7 +68,6 @@ use crate::tuple::sketch::TupleSketchView;
/// let result = union.to_sketch(true);
/// assert_eq!(result.num_retained(), 3); // apple, banana, cherry
/// ```
-#[derive(Debug)]
pub struct TupleUnion<P>
where
P: SummaryCombinePolicy,
@@ -74,6 +75,12 @@ where
state: UnionState<TupleEntry<P::Summary>, P>,
}
+impl<P: SummaryCombinePolicy> fmt::Debug for TupleUnion<P> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.state.fmt_debug(f, "TupleUnion")
+ }
+}
+
impl<P> TupleUnion<P>
where
P: SummaryCombinePolicy,
diff --git a/datasketches/src/xor/filter.rs b/datasketches/src/xor/filter.rs
index af9c2ac..eb2a651 100644
--- a/datasketches/src/xor/filter.rs
+++ b/datasketches/src/xor/filter.rs
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::BitXor;
@@ -96,13 +97,23 @@ impl Fingerprints {
/// [`XorFilter::contains_hash`].
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub struct XorFilterBuilder {
filter_type: XorFilterType,
seed: u64,
hashes: Vec<u64>,
}
+impl fmt::Debug for XorFilterBuilder {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("XorFilterBuilder")
+ .field("filter_type", &self.filter_type)
+ .field("seed", &self.seed)
+ .field("num_hashes", &self.hashes.len())
+ .finish()
+ }
+}
+
impl XorFilterBuilder {
/// Creates a builder with the given fingerprint width and the default
construction seed (`0`).
///
@@ -165,7 +176,7 @@ impl XorFilterBuilder {
/// changes.
///
/// See the [module level documentation](super) for more.
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Clone, PartialEq, Eq)]
pub struct XorFilter {
// Cache the derived length to avoid division on every query.
segment_length: usize,
@@ -492,6 +503,17 @@ impl XorFilter {
}
}
+impl fmt::Debug for XorFilter {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("XorFilter")
+ .field("filter_type", &self.filter_type())
+ .field("seed", &self.seed)
+ .field("segment_length", &self.segment_length)
+ .field("fingerprint_bytes", &self.fingerprints.byte_len())
+ .finish()
+ }
+}
+
fn hash_item<T: Hash>(item: T) -> u64 {
let mut hasher = XxHash64::with_seed(HASH_SEED);
item.hash(&mut hasher);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]