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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use Error;
use dot::{Dot, SiteId, Counter, Summary};
use map_tuple_vec;
use serde::ser::Serialize;
use serde::de::DeserializeOwned;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
pub trait SetElement: Clone + Eq + Hash + Serialize + DeserializeOwned {}
impl<T: Clone + Eq + Hash + Serialize + DeserializeOwned> SetElement for T {}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(bound(deserialize = ""))]
pub struct Set<T: SetElement> {
inner: Inner<T>,
summary: Summary,
site_id: SiteId,
cached_ops: Vec<Op<T>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(bound(deserialize = ""))]
pub struct SetState<'a, T: SetElement + 'a>{
inner: Cow<'a, Inner<T>>,
summary: Cow<'a, Summary>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct Inner<T: SetElement>(#[serde(with = "map_tuple_vec")] pub HashMap<T, Vec<Dot>>);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Op<T> {
value: T,
inserted_dot: Option<Dot>,
removed_dots: Vec<Dot>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum LocalOp<T> {
Insert(T),
Remove(T),
}
impl<T: SetElement> Set<T> {
pub fn new() -> Self {
let inner = Inner::new();
let summary = Summary::default();
let site_id = 1;
Set{inner, summary, site_id, cached_ops: vec![]}
}
pub fn contains(&self, value: &T) -> bool {
self.inner.contains(value)
}
pub fn insert(&mut self, value: T) -> Result<Op<T>, Error> {
let counter = self.summary.increment(self.site_id);
let op = self.inner.insert(value, self.site_id, counter);
self.after_op(op)
}
pub fn remove(&mut self, value: &T) -> Option<Result<Op<T>, Error>> {
let op = self.inner.remove(value)?;
Some(self.after_op(op))
}
crdt_impl2! {
Set,
SetState<T>,
SetState<'static, T>,
SetState,
Inner<T>,
Op<T>,
Option<LocalOp<T>>,
HashSet<T>,
}
}
impl<T: SetElement> Inner<T> {
fn new() -> Self {
Inner(HashMap::new())
}
fn contains(&self, value: &T) -> bool {
self.0.contains_key(value)
}
fn insert(&mut self, value: T, site_id: SiteId, counter: Counter) -> Op<T> {
let inserted_dot = Dot{site_id, counter};
let removed_dots = self.0.insert(value.clone(), vec![inserted_dot]).unwrap_or(vec![]);
Op{value, inserted_dot: Some(inserted_dot), removed_dots}
}
fn remove(&mut self, value: &T) -> Option<Op<T>> {
let removed_dots = self.0.remove(value)?;
Some(Op{value: value.clone(), inserted_dot: None, removed_dots})
}
fn execute_op(&mut self, op: Op<T>) -> Option<LocalOp<T>> {
let mut dots = self.0.remove(&op.value).unwrap_or_else(|| vec![]);
let exists_before = !dots.is_empty();
dots.retain(|r| !op.removed_dots.contains(r));
if let Some(new_dot) = op.inserted_dot {
if let Err(idx) = dots.binary_search_by(|r| r.cmp(&new_dot)) {
dots.insert(idx, new_dot);
}
}
let exists_after = !dots.is_empty();
if exists_before && exists_after {
self.0.insert(op.value, dots);
None
} else if exists_after {
self.0.insert(op.value.clone(), dots);
Some(LocalOp::Insert(op.value))
} else if exists_before {
Some(LocalOp::Remove(op.value))
} else {
None
}
}
fn merge(&mut self, other: Inner<T>, summary: &Summary, other_summary: &Summary) {
let mut other_elements = other.0;
self.0.retain(|value, dots| {
let mut other_dots = other_elements.remove(value).unwrap_or_else(|| vec![]);
dots.retain(|r| other_dots.contains(r) || !other_summary.contains(r));
other_dots.retain(|r| !dots.contains(r) && !summary.contains(r));
dots.append(&mut other_dots);
dots.sort();
!dots.is_empty()
});
for (value, mut dots) in other_elements.to_owned() {
dots.retain(|r| !summary.contains(r));
if !dots.is_empty() {
self.0.insert(value, dots);
}
}
}
fn add_site_id(&mut self, site_id: SiteId) {
for dots in self.0.values_mut() {
for dot in dots {
if dot.site_id == 0 { dot.site_id = site_id };
}
}
}
fn validate_no_unassigned_sites(&self) -> Result<(), Error> {
for dots in self.0.values() {
for dot in dots {
if dot.site_id == 0 {
return Err(Error::InvalidSiteId);
}
}
}
Ok(())
}
fn local_value(&self) -> HashSet<T> {
self.0.keys().cloned().collect()
}
}
impl<T: SetElement> Op<T> {
pub fn value(&self) -> &T { &self.value }
pub fn inserted_dot(&self) -> Option<Dot> { self.inserted_dot }
pub fn removed_dots(&self) -> &[Dot] { &self.removed_dots }
pub fn add_site_id(&mut self, site_id: SiteId) {
if let Some(ref mut r) = self.inserted_dot {
if r.site_id == 0 { r.site_id = site_id };
}
for r in &mut self.removed_dots {
if r.site_id == 0 { r.site_id = site_id };
}
}
pub fn validate(&self, site_id: SiteId) -> Result<(), Error> {
if let Some(ref r) = self.inserted_dot {
if r.site_id != site_id { return Err(Error::InvalidOp) };
}
Ok(())
}
pub(crate) fn inserted_dots(&self) -> Vec<Dot> {
if let Some(dot) = self.inserted_dot { vec![dot] } else { vec![] }
}
}