Repository navigation
Expand file tree
/
Copy pathdistributed_pool.rs
More file actions
216 lines (192 loc) · 5.78 KB
/
Copy pathdistributed_pool.rs
File metadata and controls
216 lines (192 loc) · 5.78 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::{cmp::Ord, collections::BTreeMap, ops::RangeBounds};
use crate::bug_condition;
#[derive(Clone)]
pub struct DistributedPool<State, Key: Ord> {
counter: u64,
list: BTreeMap<u64, State>,
by_key: BTreeMap<Key, u64>,
}
impl<State, Key: Ord> Default for DistributedPool<State, Key> {
fn default() -> Self {
Self {
counter: 0,
list: Default::default(),
by_key: Default::default(),
}
}
}
impl<State, Key> DistributedPool<State, Key>
where
State: AsRef<Key>,
Key: Ord + Clone,
{
pub fn is_empty(&self) -> bool {
self.list.is_empty()
}
pub fn len(&self) -> usize {
self.list.len()
}
pub fn contains(&self, key: &Key) -> bool {
self.by_key
.get(key)
.is_some_and(|i| self.list.contains_key(i))
}
pub fn get(&self, key: &Key) -> Option<&State> {
self.by_key.get(key).and_then(|i| self.list.get(i))
}
fn get_mut(&mut self, key: &Key) -> Option<&mut State> {
self.by_key.get(key).and_then(|i| self.list.get_mut(i))
}
pub fn range<R>(&self, range: R) -> impl '_ + DoubleEndedIterator<Item = (u64, &'_ State)>
where
R: RangeBounds<u64>,
{
self.list.range(range).map(|(k, v)| (*k, v))
}
pub fn last_index(&self) -> u64 {
self.list.last_key_value().map_or(0, |(k, _)| *k)
}
pub fn insert(&mut self, state: State) {
let key = state.as_ref().clone();
self.list.insert(self.counter, state);
self.by_key.insert(key, self.counter);
self.counter = self.counter.saturating_add(1);
}
pub fn update<F, R>(&mut self, key: &Key, f: F) -> Option<R>
where
F: FnOnce(&mut State) -> R,
{
let mut state = self.remove(key)?;
let res = f(&mut state);
self.insert(state);
Some(res)
}
/// Don't use if the change needs to be synced with other peers.
pub fn silent_update<F, R>(&mut self, key: &Key, f: F) -> Option<R>
where
F: FnOnce(&mut State) -> R,
{
self.get_mut(key).map(f)
}
pub fn remove(&mut self, key: &Key) -> Option<State> {
let index = self.by_key.remove(key)?;
self.list.remove(&index)
}
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&Key, &State) -> bool,
{
self.retain_and_update(|key, state| f(key, state))
}
pub fn retain_and_update<F>(&mut self, mut f: F)
where
F: FnMut(&Key, &mut State) -> bool,
{
let list = &mut self.list;
self.by_key.retain(|key, index| {
let Some(v) = list.get_mut(index) else {
bug_condition!("Pool: key found in the index, but the item not found");
return false;
};
if f(key, v) {
return true;
}
list.remove(index);
false
});
}
pub fn states(&self) -> impl Iterator<Item = &State> {
self.list.values()
}
}
impl<State, Key> DistributedPool<State, Key>
where
State: AsRef<Key>,
Key: Ord + Clone,
{
pub fn next_messages_to_send<F, T>(
&self,
(index, limit): (u64, u8),
extract_message: F,
) -> (Vec<T>, u64, u64)
where
F: Fn(&State) -> Option<T>,
{
if limit == 0 {
let index = index.saturating_sub(1);
return (vec![], index, index);
}
self.range(index..)
.try_fold(
(vec![], None),
|(mut list, mut first_index), (index, job)| {
if let Some(data) = extract_message(job) {
let first_index = *first_index.get_or_insert(index);
list.push(data);
if list.len() >= limit as usize {
return Err((list, first_index, index));
}
}
Ok((list, first_index))
},
)
// Loop iterated on whole list.
.map(|(list, first_index)| (list, first_index.unwrap_or(index), self.last_index()))
// Loop preemptively ended.
.unwrap_or_else(|v| v)
}
}
mod ser {
use super::*;
use serde::{ser::SerializeStruct, Deserialize, Serialize};
#[derive(Deserialize)]
struct Pool<State> {
counter: u64,
list: BTreeMap<u64, State>,
}
impl<State, Key> Serialize for super::DistributedPool<State, Key>
where
State: Serialize,
Key: Ord,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut s = serializer.serialize_struct("Pool", 2)?;
s.serialize_field("counter", &self.counter)?;
s.serialize_field("list", &self.list)?;
s.end()
}
}
impl<'de, State, Key> Deserialize<'de> for super::DistributedPool<State, Key>
where
State: Deserialize<'de> + AsRef<Key>,
Key: Ord + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let v = Pool::<State>::deserialize(deserializer)?;
let by_key = v
.list
.iter()
.map(|(k, v)| (v.as_ref().clone(), *k))
.collect();
Ok(Self {
counter: v.counter,
list: v.list,
by_key,
})
}
}
}
impl<State, Key: Ord> std::fmt::Debug for DistributedPool<State, Key> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pool")
.field("counter", &self.counter)
.field("len", &self.list.len())
.finish()
}
}