1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use serde::{Deserialize, Serialize};
use thiserror::Error;

use histogram::Snapshot;

/// This histogram is a sparse, columnar representation of the regular
/// Histogram. It is significantly smaller than a regular Histogram
/// when a large number of buckets are zero, which is a frequent
/// occurence; consequently it is used as the serialization format
/// of the Histogram. It stores an individual vector for each field
/// of non-zero buckets. Assuming index[0] = n, (index[0], count[0])
/// corresponds to the nth bucket.
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Histogram {
    /// parameters representing the resolution and the range of
    /// the histogram tracking request latencies
    pub grouping_power: u8,
    pub max_value_power: u8,
    /// indices for the non-zero buckets in the histogram
    pub index: Vec<usize>,
    /// histogram bucket counts corresponding to the indices
    pub count: Vec<u64>,
}

/// Errors returned for operations on histograms.
#[non_exhaustive]
#[derive(Error, Debug, PartialEq)]
pub enum Error {
    #[error("histograms with different parameters can't be merged")]
    MismatchedParams,
}

impl Histogram {
    fn add_bucket(&mut self, idx: usize, n: u64) {
        self.index.push(idx);
        self.count.push(n);
    }

    /// Merges two Histograms and returns the results in a new Histogram.
    ///
    /// Both histograms must have the same configuration parameters.
    /// Buckets which have values in both histograms are allowed to wrap.
    #[allow(clippy::comparison_chain)]
    pub fn merge(&self, h: &Histogram) -> Result<Histogram, Error> {
        if self.grouping_power != h.grouping_power || self.max_value_power != h.max_value_power {
            return Err(Error::MismatchedParams);
        }

        let mut histogram = Histogram {
            grouping_power: self.grouping_power,
            max_value_power: self.max_value_power,
            index: Vec::new(),
            count: Vec::new(),
        };

        // Sort and merge buckets from both histograms
        let (mut i, mut j) = (0, 0);
        while i < self.index.len() && j < h.index.len() {
            let (k1, v1) = (self.index[i], self.count[i]);
            let (k2, v2) = (h.index[j], h.count[j]);

            if k1 == k2 {
                histogram.add_bucket(k1, v1 + v2);
                (i, j) = (i + 1, j + 1);
            } else if k1 < k2 {
                histogram.add_bucket(k1, v1);
                i += 1;
            } else {
                histogram.add_bucket(k2, v2);
                j += 1;
            }
        }

        // Fill remaining values, if any, from the left histogram
        if i < self.index.len() {
            histogram.index.extend(&self.index[i..self.index.len()]);
            histogram.count.extend(&self.count[i..self.count.len()]);
        }

        // Fill remaining values, if any, from the left histogram
        if j < h.index.len() {
            histogram.index.extend(&h.index[i..h.index.len()]);
            histogram.count.extend(&h.count[i..h.count.len()]);
        }

        Ok(histogram)
    }
}

impl From<&Snapshot> for Histogram {
    fn from(snapshot: &Snapshot) -> Self {
        let mut index = Vec::new();
        let mut count = Vec::new();

        for (i, bucket) in snapshot
            .into_iter()
            .enumerate()
            .filter(|(_i, bucket)| bucket.count() != 0)
        {
            index.push(i);
            count.push(bucket.count());
        }

        let config = snapshot.config();
        Self {
            grouping_power: config.grouping_power(),
            max_value_power: config.max_value_power(),
            index,
            count,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn merge() {
        let h1 = Histogram {
            grouping_power: 8,
            max_value_power: 32,
            index: vec![1, 3, 5],
            count: vec![6, 12, 7],
        };

        let h2 = Histogram {
            grouping_power: 8,
            max_value_power: 32,
            index: Vec::new(),
            count: Vec::new(),
        };

        let h3 = Histogram {
            grouping_power: 8,
            max_value_power: 32,
            index: vec![2, 3, 4, 11],
            count: vec![5, 7, 3, 15],
        };

        let h = h1.merge(&Histogram::default());
        assert_eq!(h, Err(Error::MismatchedParams));

        let h = h1.merge(&h2).unwrap();
        assert_eq!(h.index, vec![1, 3, 5]);
        assert_eq!(h.count, vec![6, 12, 7]);

        let h = h2.merge(&h3).unwrap();
        assert_eq!(h.index, vec![2, 3, 4, 11]);
        assert_eq!(h.count, vec![5, 7, 3, 15]);

        let h = h1.merge(&h3).unwrap();
        assert_eq!(h.index, vec![1, 2, 3, 4, 5, 11]);
        assert_eq!(h.count, vec![6, 5, 19, 3, 7, 15]);
    }
}