This is an automated email from the ASF dual-hosted git repository. tisonkun pushed a commit to branch codex/refine-theta-sketch-views in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
commit cc31ce288d0726f1ca457d1c64c7120752b238aa Author: tison <[email protected]> AuthorDate: Sun Aug 9 15:13:22 2026 +0800 refactor: seal theta family sketch views --- datasketches/src/thetafamily/common/intersection.rs | 4 ++++ .../src/thetafamily/common/jaccard_similarity.rs | 7 +++++++ datasketches/src/thetafamily/common/mod.rs | 18 ++++++++++++++++++ datasketches/src/thetafamily/common/union.rs | 4 ++++ datasketches/src/thetafamily/theta/hash_table.rs | 3 +++ datasketches/src/thetafamily/theta/sketch.rs | 7 +++++++ datasketches/src/thetafamily/tuple/hash_table.rs | 3 +++ datasketches/src/thetafamily/tuple/sketch.rs | 11 +++++++++-- 8 files changed, 55 insertions(+), 2 deletions(-) diff --git a/datasketches/src/thetafamily/common/intersection.rs b/datasketches/src/thetafamily/common/intersection.rs index 00e5ed4..0b97865 100644 --- a/datasketches/src/thetafamily/common/intersection.rs +++ b/datasketches/src/thetafamily/common/intersection.rs @@ -268,6 +268,8 @@ mod tests { } impl RetainedEntry for TestEntry { + fn __private(&self, _: crate::thetacommon::sealed::Token) {} + fn hash(&self) -> u64 { self.hash } @@ -289,6 +291,8 @@ mod tests { } impl ThetaKeySketchView for TestSketch { + fn __private(&self, _: crate::thetacommon::sealed::Token) {} + fn seed_hash(&self) -> u16 { compute_seed_hash(DEFAULT_UPDATE_SEED) } diff --git a/datasketches/src/thetafamily/common/jaccard_similarity.rs b/datasketches/src/thetafamily/common/jaccard_similarity.rs index d1164dc..77dd4a8 100644 --- a/datasketches/src/thetafamily/common/jaccard_similarity.rs +++ b/datasketches/src/thetafamily/common/jaccard_similarity.rs @@ -30,6 +30,7 @@ use crate::thetacommon::constants::MIN_LG_K; use crate::thetacommon::hash_table::CompactSketchParts; use crate::thetacommon::intersection::IntersectionMergePolicy; use crate::thetacommon::intersection::IntersectionState; +use crate::thetacommon::sealed; use crate::thetacommon::union::UnionMergePolicy; use crate::thetacommon::union::UnionState; @@ -116,6 +117,8 @@ struct KeyEntry { } impl RetainedEntry for KeyEntry { + fn __private(&self, _: sealed::Token) {} + fn hash(&self) -> u64 { self.hash } @@ -132,6 +135,8 @@ impl<'a, S> KeySketchView<'a, S> { } impl<S: ThetaKeySketchView> ThetaKeySketchView for KeySketchView<'_, S> { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { self.sketch.seed_hash() } @@ -185,6 +190,8 @@ struct CompactKeySketchView { } impl ThetaKeySketchView for CompactKeySketchView { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { self.seed_hash } diff --git a/datasketches/src/thetafamily/common/mod.rs b/datasketches/src/thetafamily/common/mod.rs index 199e5cc..931219a 100644 --- a/datasketches/src/thetafamily/common/mod.rs +++ b/datasketches/src/thetafamily/common/mod.rs @@ -27,8 +27,18 @@ pub(crate) mod union; pub use self::jaccard_similarity::JaccardSimilarity; +pub(crate) mod sealed { + pub struct Token; +} + /// An entry retained by a Theta sketch family hash table. +/// +/// This trait is sealed because the shared set-operation state machines rely on entry invariants +/// maintained by the sketch implementations in this crate. pub trait RetainedEntry { + #[doc(hidden)] + fn __private(&self, _: sealed::Token); + /// Return the hash used as this entry's key. fn hash(&self) -> u64; } @@ -37,7 +47,13 @@ pub trait RetainedEntry { /// /// Key-only operations use this interface without requiring access to, or cloning, payloads such /// as Tuple summaries. +/// +/// This trait is sealed because set operations rely on the reported metadata, retained count, and +/// iterators describing the same sketch state. pub trait ThetaKeySketchView { + #[doc(hidden)] + fn __private(&self, _: sealed::Token); + /// Return the 16-bit seed hash. fn seed_hash(&self) -> u16; @@ -61,6 +77,8 @@ pub trait ThetaKeySketchView { /// /// This trait extends [`ThetaKeySketchView`] with complete retained entries, so operations such as /// union and intersection can preserve and combine Tuple summaries. +/// +/// Like [`ThetaKeySketchView`], this trait is sealed and cannot be implemented outside this crate. pub trait ThetaFamilySketchView: ThetaKeySketchView { /// The retained entry representation yielded by this view. type Entry: RetainedEntry; diff --git a/datasketches/src/thetafamily/common/union.rs b/datasketches/src/thetafamily/common/union.rs index 5b4aebd..d734a2f 100644 --- a/datasketches/src/thetafamily/common/union.rs +++ b/datasketches/src/thetafamily/common/union.rs @@ -174,6 +174,8 @@ mod tests { } impl RetainedEntry for TestEntry { + fn __private(&self, _: crate::thetacommon::sealed::Token) {} + fn hash(&self) -> u64 { self.hash } @@ -184,6 +186,8 @@ mod tests { } impl ThetaKeySketchView for TestSketch { + fn __private(&self, _: crate::thetacommon::sealed::Token) {} + fn seed_hash(&self) -> u16 { compute_seed_hash(DEFAULT_UPDATE_SEED) } diff --git a/datasketches/src/thetafamily/theta/hash_table.rs b/datasketches/src/thetafamily/theta/hash_table.rs index b10ae57..e4cf1b5 100644 --- a/datasketches/src/thetafamily/theta/hash_table.rs +++ b/datasketches/src/thetafamily/theta/hash_table.rs @@ -20,6 +20,7 @@ use std::num::NonZeroU64; use crate::thetacommon::RetainedEntry; use crate::thetacommon::hash_table::SketchHashTable; +use crate::thetacommon::sealed; /// Specific hash table for theta sketch /// @@ -49,6 +50,8 @@ impl ThetaEntry { } impl RetainedEntry for ThetaEntry { + fn __private(&self, _: sealed::Token) {} + fn hash(&self) -> u64 { self.hash.get() } diff --git a/datasketches/src/thetafamily/theta/sketch.rs b/datasketches/src/thetafamily/theta/sketch.rs index 6a9662a..e550a15 100644 --- a/datasketches/src/thetafamily/theta/sketch.rs +++ b/datasketches/src/thetafamily/theta/sketch.rs @@ -56,16 +56,21 @@ use crate::thetacommon::constants::FLAGS_IS_READ_ONLY; use crate::thetacommon::constants::MAX_LG_K; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::sealed; /// Read-only view for Theta sketches. /// /// This trait provides a unified input abstraction for APIs that can accept either /// mutable [`ThetaSketch`] or immutable [`CompactThetaSketch`]. +/// +/// This trait is sealed and cannot be implemented outside this crate. pub trait ThetaSketchView: ThetaFamilySketchView<Entry = ThetaEntry> {} impl<T: ThetaFamilySketchView<Entry = ThetaEntry>> ThetaSketchView for T {} impl ThetaKeySketchView for ThetaSketch { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { ThetaSketch::seed_hash(self) } @@ -877,6 +882,8 @@ impl CompactThetaSketch { } impl ThetaKeySketchView for CompactThetaSketch { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { CompactThetaSketch::seed_hash(self) } diff --git a/datasketches/src/thetafamily/tuple/hash_table.rs b/datasketches/src/thetafamily/tuple/hash_table.rs index e64520f..df9e62d 100644 --- a/datasketches/src/thetafamily/tuple/hash_table.rs +++ b/datasketches/src/thetafamily/tuple/hash_table.rs @@ -21,6 +21,7 @@ use std::num::NonZeroU64; use crate::thetacommon::RetainedEntry; use crate::thetacommon::hash_table::SketchHashTable; use crate::thetacommon::intersection::IntersectionMergePolicy; +use crate::thetacommon::sealed; use crate::thetacommon::union::UnionMergePolicy; use crate::tuple::SummaryCombinePolicy; @@ -64,6 +65,8 @@ impl<S> TupleEntry<S> { pub(super) type TupleHashTable<S> = SketchHashTable<TupleEntry<S>>; impl<S> RetainedEntry for TupleEntry<S> { + fn __private(&self, _: sealed::Token) {} + fn hash(&self) -> u64 { self.hash.get() } diff --git a/datasketches/src/thetafamily/tuple/sketch.rs b/datasketches/src/thetafamily/tuple/sketch.rs index 21fa290..18b0ef2 100644 --- a/datasketches/src/thetafamily/tuple/sketch.rs +++ b/datasketches/src/thetafamily/tuple/sketch.rs @@ -47,6 +47,7 @@ use crate::thetacommon::constants::FLAGS_IS_READ_ONLY; use crate::thetacommon::constants::MAX_LG_K; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::sealed; use crate::tuple::hash_table::TupleEntry; use crate::tuple::hash_table::TupleHashTable; use crate::tuple::policy::SummaryPolicy; @@ -61,6 +62,8 @@ use crate::tuple::serialization::TupleSummaryValue; /// /// This interface does not inspect summary values and therefore does not require them to implement /// [`Clone`]. +/// +/// This trait is sealed and cannot be implemented outside this crate. pub trait TupleKeySketchView: ThetaKeySketchView {} /// Read-only retained-entry view for Tuple sketches. @@ -69,8 +72,8 @@ pub trait TupleKeySketchView: ThetaKeySketchView {} /// either a mutable [`TupleSketch`] or an immutable [`CompactTupleSketch`]. `S` is the /// summary type retained by the sketch. /// -/// It is blanket-implemented for every [`TupleKeySketchView`] that also implements -/// [`ThetaFamilySketchView`] with [`TupleEntry<S>`] as its associated entry type. +/// This trait is sealed and cannot be implemented outside this crate. The mutable and compact +/// Tuple sketch types implement it when `S: Clone`. pub trait TupleSketchView<S>: TupleKeySketchView + ThetaFamilySketchView<Entry = TupleEntry<S>> { @@ -273,6 +276,8 @@ impl<P> ThetaKeySketchView for TupleSketch<P> where P: SummaryPolicy, { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { self.table.seed_hash() } @@ -603,6 +608,8 @@ impl<S> CompactTupleSketch<S> { } impl<S> ThetaKeySketchView for CompactTupleSketch<S> { + fn __private(&self, _: sealed::Token) {} + fn seed_hash(&self) -> u16 { self.seed_hash } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
