pub struct DDSketch<M: IndexMapping = LogarithmicMapping, S: Store = CollapsingLowestDenseStore> { /* private fields */ }Expand description
A fast and fully-mergeable quantile sketch with relative-error guarantees.
This implementation supports most of the capabilities of the various official DDSketch implementations, such as:
- support for tracking negative and positive values
- multiple store types (sparse, dense, collapsing)
- configurable index interpolation schemes (only logarithmic currently supported)
Defaults to using a “low collapsing” dense store with a logarithmic index mapping. This works well for tracking values like time durations/latencies where the tail latencies (higher percentiles) matter most.
§Example
use ddsketch::canonical::DDSketch;
let mut sketch = DDSketch::with_relative_accuracy(0.01).unwrap();
sketch.add(1.0);
sketch.add(2.0);
sketch.add(3.0);
let median = sketch.quantile(0.5).unwrap();Implementations§
Source§impl DDSketch<LogarithmicMapping, CollapsingLowestDenseStore>
impl DDSketch<LogarithmicMapping, CollapsingLowestDenseStore>
Sourcepub fn with_relative_accuracy(
relative_accuracy: f64,
) -> Result<Self, &'static str>
pub fn with_relative_accuracy( relative_accuracy: f64, ) -> Result<Self, &'static str>
Creates a new DDSketch with the given relative accuracy.
Defaults to logarithmic mapping and the “low collapsing” dense store, with a maximum of 2048 bins per store.
§Errors
If the relative accuracy is not between 0 and 1, an error is returned.
Source§impl<M: IndexMapping, S: Store> DDSketch<M, S>
impl<M: IndexMapping, S: Store> DDSketch<M, S>
Sourcepub fn new(mapping: M, positive_store: S, negative_store: S) -> Self
pub fn new(mapping: M, positive_store: S, negative_store: S) -> Self
Creates a new DDSketch with the given mapping and stores.
Sourcepub fn add_n(&mut self, value: f64, n: u64)
pub fn add_n(&mut self, value: f64, n: u64)
Adds a value to the sketch with the given count.
This is useful for weighted values or pre-aggregated data.
Sourcepub fn quantile(&self, q: f64) -> Option<f64>
pub fn quantile(&self, q: f64) -> Option<f64>
Returns the approximate value at the given quantile.
The quantile must be in the range of [0, 1].
Returns None if the sketch is empty, or if the quantile is out of bounds. Otherwise, returns the approximate
value.
Sourcepub fn merge(&mut self, other: &Self)where
M: PartialEq,
pub fn merge(&mut self, other: &Self)where
M: PartialEq,
Merges another sketch into this one.
The other sketch must use the same mapping type.
Sourcepub fn positive_store(&self) -> &S
pub fn positive_store(&self) -> &S
Returns a reference to the positive value store.
Sourcepub fn negative_store(&self) -> &S
pub fn negative_store(&self) -> &S
Returns a reference to the negative value store.
Sourcepub fn zero_count(&self) -> u64
pub fn zero_count(&self) -> u64
Returns the count of values mapped to zero.
Sourcepub fn relative_accuracy(&self) -> f64
pub fn relative_accuracy(&self) -> f64
Returns the relative accuracy of this sketch.
Sourcepub fn from_proto(
proto: &ProtoDDSketch,
mapping: M,
) -> Result<Self, ProtoConversionError>where
S: Default,
pub fn from_proto(
proto: &ProtoDDSketch,
mapping: M,
) -> Result<Self, ProtoConversionError>where
S: Default,
Creates a DDSketch from a protobuf DDSketch message.
This validates that the protobuf’s index mapping is compatible with
the mapping type M, then populates the stores with the bin data.
§Arguments
proto- The protobufDDSketchmessage to convert frommapping- The mapping instance to use (must be compatible with proto’s mapping)
§Errors
Returns an error if:
- The protobuf is missing a mapping
- The mapping parameters don’t match the provided mapping
- Any bin counts are negative or non-integer
- The zero count is negative or non-integer
§Note
The protobuf DDSketch does not include sum, min, max, or count fields.
These are computed or set to defaults:
count: sum of all bin counts plus zero_countsum,min,max: set to sentinel defaults (cannot be recovered from proto)
Trait Implementations§
impl<M: IndexMapping + PartialEq, S: Store + PartialEq> Eq for DDSketch<M, S>
Auto Trait Implementations§
impl<M, S> Freeze for DDSketch<M, S>
impl<M, S> RefUnwindSafe for DDSketch<M, S>where
M: RefUnwindSafe,
S: RefUnwindSafe,
impl<M, S> Send for DDSketch<M, S>
impl<M, S> Sync for DDSketch<M, S>
impl<M, S> Unpin for DDSketch<M, S>
impl<M, S> UnwindSafe for DDSketch<M, S>where
M: UnwindSafe,
S: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request