ddsketch/canonical/mapping/
mod.rs

1//! Index mapping.
2
3use datadog_protos::sketches::{index_mapping::Interpolation, IndexMapping as ProtoIndexMapping};
4
5use super::error::ProtoConversionError;
6
7mod logarithmic;
8pub use self::logarithmic::LogarithmicMapping;
9
10/// Maps values to bin indices and vice versa.
11///
12/// The mapping defines the relationship between floating-point values and integer bin indices, determining the relative
13/// accuracy of the sketch.
14pub trait IndexMapping: Clone + Send + Sync {
15    /// Returns the index of the bin for the given positive value.
16    ///
17    /// The value must be positive. For negative values, use the index of the absolute value and store in the negative
18    /// store.
19    fn index(&self, value: f64) -> i32;
20
21    /// Returns the representative value for the given index.
22    ///
23    /// This is typically the geometric mean of the bin's lower and upper bounds.
24    fn value(&self, index: i32) -> f64;
25
26    /// Returns the lower bound of the bin at the given index.
27    fn lower_bound(&self, index: i32) -> f64;
28
29    /// Returns the relative accuracy of this mapping.
30    ///
31    /// The relative accuracy is the maximum relative error guaranteed for any quantile query.
32    fn relative_accuracy(&self) -> f64;
33
34    /// Returns the minimum positive value that can be indexed.
35    fn min_indexable_value(&self) -> f64;
36
37    /// Returns the maximum positive value that can be indexed.
38    fn max_indexable_value(&self) -> f64;
39
40    /// Returns the gamma value (base of the logarithm) for this mapping.
41    fn gamma(&self) -> f64;
42
43    /// Returns the index offset used by this mapping.
44    ///
45    /// The index offset shifts all bin indices by a constant value.
46    fn index_offset(&self) -> f64;
47
48    /// Returns the interpolation mode used by this mapping.
49    ///
50    /// The interpolation mode determines how the logarithm is approximated.
51    fn interpolation(&self) -> Interpolation;
52
53    /// Validates that a protobuf `IndexMapping` is compatible with this mapping.
54    ///
55    /// # Errors
56    ///
57    /// If the given protobuf mapping parameters do not match this mapping's configuration, an error describing the
58    /// mismatch is returned.
59    fn validate_proto_mapping(&self, proto: &ProtoIndexMapping) -> Result<(), ProtoConversionError>;
60
61    /// Converts this mapping to a protobuf `IndexMapping`.
62    fn to_proto(&self) -> ProtoIndexMapping;
63}