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
use crate::api::{Key, KeyValue, Value};
use std::cmp::Ordering;
use std::collections::{btree_map, BTreeMap};
use std::hash::{Hash, Hasher};
use std::iter::Peekable;
mod encoder;
pub use encoder::{default_encoder, new_encoder_id, DefaultLabelEncoder, Encoder, EncoderId};
#[derive(Clone, Debug, Default)]
pub struct LabelSet {
labels: BTreeMap<Key, Value>,
}
impl LabelSet {
pub fn from_labels<T: IntoIterator<Item = KeyValue>>(labels: T) -> Self {
LabelSet {
labels: labels.into_iter().map(|kv| (kv.key, kv.value)).collect(),
}
}
pub fn len(&self) -> usize {
self.labels.len()
}
pub fn is_empty(&self) -> bool {
self.labels.is_empty()
}
pub fn iter(&self) -> Iter {
self.into_iter()
}
pub fn encoded(&self, encoder: Option<&dyn Encoder>) -> String {
encoder.map_or_else(String::new, |encoder| encoder.encode(&mut self.iter()))
}
}
impl<'a> IntoIterator for &'a LabelSet {
type Item = (&'a Key, &'a Value);
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
Iter(self.labels.iter())
}
}
#[derive(Debug)]
pub struct Iter<'a>(btree_map::Iter<'a, Key, Value>);
impl<'a> Iterator for Iter<'a> {
type Item = (&'a Key, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub fn hash_labels<'a, H: Hasher, I: IntoIterator<Item = (&'a Key, &'a Value)>>(
state: &mut H,
labels: I,
) {
for (key, value) in labels.into_iter() {
key.hash(state);
hash_value(state, value);
}
}
fn hash_value<H: Hasher>(state: &mut H, value: &Value) {
match value {
Value::Bool(b) => b.hash(state),
Value::I64(i) => i.hash(state),
Value::U64(u) => u.hash(state),
Value::F64(f) => {
f.to_bits().hash(state)
}
Value::String(s) => s.hash(state),
Value::Bytes(b) => state.write(b),
Value::Array(arr) => {
for val in arr {
hash_value(state, val);
}
}
}
}
pub fn merge_iters<
'a,
'b,
A: Iterator<Item = (&'a Key, &'a Value)>,
B: Iterator<Item = (&'b Key, &'b Value)>,
>(
a: A,
b: B,
) -> MergeIter<'a, 'b, A, B> {
MergeIter {
a: a.peekable(),
b: b.peekable(),
}
}
#[derive(Debug)]
pub struct MergeIter<'a, 'b, A, B>
where
A: Iterator<Item = (&'a Key, &'a Value)>,
B: Iterator<Item = (&'b Key, &'b Value)>,
{
a: Peekable<A>,
b: Peekable<B>,
}
impl<'a, A: Iterator<Item = (&'a Key, &'a Value)>, B: Iterator<Item = (&'a Key, &'a Value)>>
Iterator for MergeIter<'a, 'a, A, B>
{
type Item = (&'a Key, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
let which = match (self.a.peek(), self.b.peek()) {
(Some(a), Some(b)) => Some(a.0.cmp(&b.0)),
(Some(_), None) => Some(Ordering::Less),
(None, Some(_)) => Some(Ordering::Greater),
(None, None) => None,
};
match which {
Some(Ordering::Less) => self.a.next(),
Some(Ordering::Equal) => self.a.next(),
Some(Ordering::Greater) => self.b.next(),
None => None,
}
}
}