Skip to content

refactor: encapsulate read-only Theta sketch views#151

Open
ZENOTME wants to merge 1 commit into
apache:mainfrom
ZENOTME:codex/theta-sketch-view
Open

refactor: encapsulate read-only Theta sketch views#151
ZENOTME wants to merge 1 commit into
apache:mainfrom
ZENOTME:codex/theta-sketch-view

Conversation

@ZENOTME

@ZENOTME ZENOTME commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • keep RawThetaSketchView crate-private while preserving the raw algorithm abstraction
  • add a transparent ThetaSketchView that can only be created by supported sketches
  • expose the six read-only view methods and accept views in Theta union/intersection
  • add reference forwarding plus From conversions for mutable and compact Theta sketches

Validation

  • cargo test -p datasketches --features theta
  • cargo clippy --workspace --all-features --all-targets -- -D warnings
  • RUSTDOCFLAGS="-D warnings" cargo doc -p datasketches --features theta --no-deps

@ZENOTME
ZENOTME marked this pull request as ready for review July 19, 2026 03:49
@ZENOTME ZENOTME changed the title WIP: encapsulate read-only Theta sketch views refactor: encapsulate read-only Theta sketch views Jul 19, 2026
Comment on lines +66 to +70
#[allow(private_bounds)]
pub fn update<V>(&mut self, sketch: ThetaSketchView<V>) -> Result<(), Error>
where
V: RawThetaSketchView<ThetaEntry>,
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make the document of update cannot linked to RawThetaSketchView and remain a dead link on docs.rs.

Let me think of how to expose the symbols here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid trait here, we can use enum to dispatch.🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way is to export using new trait like TupleSketchView. We don't need to test it, and it can be sealed::Sealed. And thetacommon can internal and not export to user.

pub trait TupleSketchView<S>: sealed::Sealed {
    /// Return the 16-bit seed hash.
    fn seed_hash(&self) -> u16;

    /// Return theta as a `u64` threshold.
    fn theta(&self) -> u64;

    /// Return whether this sketch has not received any updates.
    fn is_empty(&self) -> bool;

    /// Return whether retained entries are ordered by ascending hash.
    fn is_ordered(&self) -> bool;

    /// Return an iterator over retained entries.
    fn iter(&self) -> impl Iterator<Item = TupleEntry<S>> + '_;

    /// Return the number of retained entries.
    fn num_retained(&self) -> usize;
}

impl<S, T> RawThetaSketchView<TupleEntry<S>> for T
where
    T: TupleSketchView<S> + ?Sized,
{
    fn seed_hash(&self) -> u16 {
        TupleSketchView::seed_hash(self)
    }

    fn theta(&self) -> u64 {
        TupleSketchView::theta(self)
    }

    fn is_empty(&self) -> bool {
        TupleSketchView::is_empty(self)
    }

    fn is_ordered(&self) -> bool {
        TupleSketchView::is_ordered(self)
    }

    fn iter(&self) -> impl Iterator<Item = TupleEntry<S>> + '_ {
        TupleSketchView::iter(self)
    }

    fn num_retained(&self) -> usize {
        TupleSketchView::num_retained(self)
    }
}

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an incorrect abstraction.

I think we can review and merge the tuple sketches code and later try to factor out the abstraction.

So far, I don't see a concrete reason for all these abstratcions. We can inline the ThetaSketchView if this is the only impl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants