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