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
157
158
use std::fmt;
use std::fmt::Write;
use std::u64;
use slog::{OwnedKVList, Record, KV};
use crate::eraftpb::{Entry, Message};
use crate::HashSet;
use protobuf::Message as PbMessage;
use slog::{b, record_static};
pub const NO_LIMIT: u64 = u64::MAX;
pub fn limit_size<T: PbMessage + Clone>(entries: &mut Vec<T>, max: Option<u64>) {
if entries.len() <= 1 {
return;
}
let max = match max {
None | Some(NO_LIMIT) => return,
Some(max) => max,
};
let mut size = 0;
let limit = entries
.iter()
.take_while(|&e| {
if size == 0 {
size += u64::from(e.compute_size());
true
} else {
size += u64::from(e.compute_size());
size <= max
}
})
.count();
entries.truncate(limit);
}
pub fn is_continuous_ents(msg: &Message, ents: &[Entry]) -> bool {
if !msg.entries.is_empty() && !ents.is_empty() {
let expected_next_idx = msg.entries.last().unwrap().index + 1;
return expected_next_idx == ents.first().unwrap().index;
}
true
}
struct FormatKeyValueList {
pub buffer: String,
}
impl slog::Serializer for FormatKeyValueList {
fn emit_arguments(&mut self, key: slog::Key, val: &fmt::Arguments) -> slog::Result {
if !self.buffer.is_empty() {
write!(&mut self.buffer, ", {}: {}", key, val).unwrap();
} else {
write!(&mut self.buffer, "{}: {}", key, val).unwrap();
}
Ok(())
}
}
pub(crate) fn format_kv_list(kv_list: &OwnedKVList) -> String {
let mut formatter = FormatKeyValueList {
buffer: "".to_owned(),
};
let record = record_static!(slog::Level::Trace, "");
kv_list
.serialize(
&Record::new(&record, &format_args!(""), b!()),
&mut formatter,
)
.unwrap();
formatter.buffer
}
#[inline]
pub fn majority(total: usize) -> usize {
(total / 2) + 1
}
pub struct Union<'a> {
first: &'a HashSet<u64>,
second: &'a HashSet<u64>,
}
impl<'a> Union<'a> {
pub fn new(first: &'a HashSet<u64>, second: &'a HashSet<u64>) -> Union<'a> {
Union { first, second }
}
#[inline]
pub fn contains(&self, id: u64) -> bool {
self.first.contains(&id) || self.second.contains(&id)
}
pub fn iter(&self) -> impl Iterator<Item = u64> + '_ {
self.first.union(self.second).cloned()
}
pub fn is_empty(&self) -> bool {
self.first.is_empty() && self.second.is_empty()
}
pub fn len(&self) -> usize {
self.first.len() + self.second.len() - self.second.intersection(&self.first).count()
}
}