pub struct CardinalityTracker { /* private fields */ }Expand description
Real-time cardinality tracker using HyperLogLog sketches
Maintains per-column HLL sketches for sub-microsecond cardinality queries with <1% standard error.
§Math
Standard error = 1.04 / sqrt(2^precision)
Precision=14: SE = 0.81%, memory = 16KB per column (dense)
Sparse mode: memory = O(cardinality) for low-cardinality columns§Thread Safety
Uses fine-grained locking per table for concurrent updates across multiple ingestion threads.
Implementations§
Source§impl CardinalityTracker
impl CardinalityTracker
Sourcepub fn with_precision(precision: u8) -> Self
pub fn with_precision(precision: u8) -> Self
Create with custom HLL precision
Precision affects accuracy vs memory:
- 10: SE=3.25%, 1KB/column
- 12: SE=1.63%, 4KB/column
- 14: SE=0.81%, 16KB/column (default)
- 16: SE=0.41%, 64KB/column
Sourcepub fn set_drift_threshold(&mut self, threshold: f64)
pub fn set_drift_threshold(&mut self, threshold: f64)
Set drift threshold for cache invalidation
Sourcepub fn observe<T: Hash>(&self, table: &str, column: &str, value: &T)
pub fn observe<T: Hash>(&self, table: &str, column: &str, value: &T)
Observe a value for a column (call on INSERT/UPDATE)
O(1) operation - safe to call on every write.
Sourcepub fn observe_batch<T: Hash>(
&self,
table: &str,
column: &str,
values: impl Iterator<Item = T>,
)
pub fn observe_batch<T: Hash>( &self, table: &str, column: &str, values: impl Iterator<Item = T>, )
Observe multiple values in batch (more efficient for bulk inserts)
Sourcepub fn increment_row_count(&self, table: &str, delta: usize)
pub fn increment_row_count(&self, table: &str, delta: usize)
Increment row count for a table
Sourcepub fn estimate(&self, table: &str, column: &str) -> Option<CardinalityEstimate>
pub fn estimate(&self, table: &str, column: &str) -> Option<CardinalityEstimate>
Estimate cardinality for a column
O(1) operation - returns sub-microsecond.
Sourcepub fn get_table_cardinalities(&self, table: &str) -> HashMap<String, usize>
pub fn get_table_cardinalities(&self, table: &str) -> HashMap<String, usize>
Get all column cardinalities for a table
Sourcepub fn get_row_count(&self, table: &str) -> usize
pub fn get_row_count(&self, table: &str) -> usize
Get row count estimate for a table
Sourcepub fn has_cardinality_drift(
&self,
table: &str,
cached_cardinalities: &HashMap<String, usize>,
) -> bool
pub fn has_cardinality_drift( &self, table: &str, cached_cardinalities: &HashMap<String, usize>, ) -> bool
Check if cardinality has drifted beyond threshold
Returns true if any column’s cardinality has changed by more than
drift_threshold (default 20%).
Sourcepub fn merge(&self, table: &str, column: &str, other_hll: &HyperLogLog)
pub fn merge(&self, table: &str, column: &str, other_hll: &HyperLogLog)
Merge HLL from another tracker (for distributed scenarios)
Sourcepub fn clear_table(&self, table: &str)
pub fn clear_table(&self, table: &str)
Clear all tracking data for a table
Sourcepub fn memory_usage(&self) -> CardinalityTrackerStats
pub fn memory_usage(&self) -> CardinalityTrackerStats
Get memory usage statistics
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for CardinalityTracker
impl !RefUnwindSafe for CardinalityTracker
impl Send for CardinalityTracker
impl Sync for CardinalityTracker
impl Unpin for CardinalityTracker
impl UnsafeUnpin for CardinalityTracker
impl UnwindSafe for CardinalityTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more