-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathawareness.rs
417 lines (374 loc) · 12.8 KB
/
awareness.rs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use fxhash::FxHashMap;
use loro_common::{LoroValue, PeerID};
use serde::{Deserialize, Serialize};
use crate::change::{get_sys_timestamp, Timestamp};
use crate::{SubscriberSetWithQueue, Subscription};
/// `Awareness` is a structure that tracks the ephemeral state of peers.
///
/// It can be used to synchronize cursor positions, selections, and the names of the peers.
///
/// The state of a specific peer is expected to be removed after a specified timeout. Use
/// `remove_outdated` to eliminate outdated states.
#[derive(Debug, Clone)]
#[deprecated(since = "1.4.6", note = "Use `EphemeralStore` instead.")]
pub struct Awareness {
peer: PeerID,
peers: FxHashMap<PeerID, PeerInfo>,
timeout: i64,
}
#[derive(Debug, Clone)]
pub struct PeerInfo {
pub state: LoroValue,
pub counter: i32,
// This field is generated locally
pub timestamp: i64,
}
#[derive(Serialize, Deserialize)]
struct EncodedPeerInfo {
peer: PeerID,
counter: i32,
record: LoroValue,
}
#[allow(deprecated)]
impl Awareness {
pub fn new(peer: PeerID, timeout: i64) -> Awareness {
Awareness {
peer,
timeout,
peers: FxHashMap::default(),
}
}
pub fn encode(&self, peers: &[PeerID]) -> Vec<u8> {
let mut peers_info = Vec::new();
let now = get_sys_timestamp() as Timestamp;
for peer in peers {
if let Some(peer_info) = self.peers.get(peer) {
if now - peer_info.timestamp > self.timeout {
continue;
}
let encoded_peer_info = EncodedPeerInfo {
peer: *peer,
record: peer_info.state.clone(),
counter: peer_info.counter,
};
peers_info.push(encoded_peer_info);
}
}
postcard::to_allocvec(&peers_info).unwrap()
}
pub fn encode_all(&self) -> Vec<u8> {
let mut peers_info = Vec::new();
let now = get_sys_timestamp() as Timestamp;
for (peer, peer_info) in self.peers.iter() {
if now - peer_info.timestamp > self.timeout {
continue;
}
let encoded_peer_info = EncodedPeerInfo {
peer: *peer,
record: peer_info.state.clone(),
counter: peer_info.counter,
};
peers_info.push(encoded_peer_info);
}
postcard::to_allocvec(&peers_info).unwrap()
}
/// Returns (updated, added)
pub fn apply(&mut self, encoded_peers_info: &[u8]) -> (Vec<PeerID>, Vec<PeerID>) {
let peers_info: Vec<EncodedPeerInfo> = postcard::from_bytes(encoded_peers_info).unwrap();
let mut changed_peers = Vec::new();
let mut added_peers = Vec::new();
let now = get_sys_timestamp() as Timestamp;
for peer_info in peers_info {
match self.peers.get(&peer_info.peer) {
Some(x) if x.counter >= peer_info.counter || peer_info.peer == self.peer => {
// do nothing
}
_ => {
let old = self.peers.insert(
peer_info.peer,
PeerInfo {
counter: peer_info.counter,
state: peer_info.record,
timestamp: now,
},
);
if old.is_some() {
changed_peers.push(peer_info.peer);
} else {
added_peers.push(peer_info.peer);
}
}
}
}
(changed_peers, added_peers)
}
pub fn set_local_state(&mut self, value: impl Into<LoroValue>) {
self._set_local_state(value.into());
}
fn _set_local_state(&mut self, value: LoroValue) {
let peer = self.peers.entry(self.peer).or_insert_with(|| PeerInfo {
state: Default::default(),
counter: 0,
timestamp: 0,
});
peer.state = value;
peer.counter += 1;
peer.timestamp = get_sys_timestamp() as Timestamp;
}
pub fn get_local_state(&self) -> Option<LoroValue> {
self.peers.get(&self.peer).map(|x| x.state.clone())
}
pub fn remove_outdated(&mut self) -> Vec<PeerID> {
let now = get_sys_timestamp() as Timestamp;
let mut removed = Vec::new();
self.peers.retain(|id, v| {
if now - v.timestamp > self.timeout {
removed.push(*id);
false
} else {
true
}
});
removed
}
pub fn get_all_states(&self) -> &FxHashMap<PeerID, PeerInfo> {
&self.peers
}
pub fn peer(&self) -> PeerID {
self.peer
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EphemeralEventTrigger {
Local,
Remote,
Timeout,
}
#[derive(Debug)]
pub struct EphemeralStoreEvent {
pub by: EphemeralEventTrigger,
pub added: Vec<String>,
pub updated: Vec<String>,
pub removed: Vec<String>,
}
pub type LocalEphemeralCallback = Box<dyn Fn(&Vec<u8>) -> bool + Send + Sync + 'static>;
pub type EphemeralSubscriber = Box<dyn Fn(&EphemeralStoreEvent) -> bool + Send + Sync + 'static>;
/// `EphemeralStore` is a structure that tracks the ephemeral state of peers.
///
/// It can be used to synchronize cursor positions, selections, and the names of the peers.
/// We use the latest timestamp as the tie-breaker for LWW (Last-Write-Wins) conflict resolution.
pub struct EphemeralStore {
states: FxHashMap<String, State>,
local_subs: SubscriberSetWithQueue<(), LocalEphemeralCallback, Vec<u8>>,
subscribers: SubscriberSetWithQueue<(), EphemeralSubscriber, EphemeralStoreEvent>,
timeout: i64,
}
impl std::fmt::Debug for EphemeralStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"AwarenessV2 {{ states: {:?}, timeout: {:?} }}",
self.states, self.timeout
)
}
}
#[derive(Serialize, Deserialize)]
struct EncodedState<'a> {
#[serde(borrow)]
key: &'a str,
value: Option<LoroValue>,
timestamp: i64,
}
#[derive(Debug, Clone)]
struct State {
state: Option<LoroValue>,
timestamp: i64,
}
impl EphemeralStore {
pub fn new(timeout: i64) -> EphemeralStore {
EphemeralStore {
timeout,
states: FxHashMap::default(),
local_subs: SubscriberSetWithQueue::new(),
subscribers: SubscriberSetWithQueue::new(),
}
}
pub fn encode(&self, key: &str) -> Vec<u8> {
let mut peers_info = Vec::new();
let now = get_sys_timestamp() as Timestamp;
if let Some(peer_state) = self.states.get(key) {
if now - peer_state.timestamp > self.timeout {
return vec![];
}
let encoded_peer_info = EncodedState {
key,
value: peer_state.state.clone(),
timestamp: peer_state.timestamp,
};
peers_info.push(encoded_peer_info);
}
postcard::to_allocvec(&peers_info).unwrap()
}
pub fn encode_all(&self) -> Vec<u8> {
let mut peers_info = Vec::new();
let now = get_sys_timestamp() as Timestamp;
for (key, peer_state) in self.states.iter() {
if now - peer_state.timestamp > self.timeout {
continue;
}
let encoded_peer_info = EncodedState {
key,
value: peer_state.state.clone(),
timestamp: peer_state.timestamp,
};
peers_info.push(encoded_peer_info);
}
postcard::to_allocvec(&peers_info).unwrap()
}
pub fn apply(&mut self, data: &[u8]) {
let peers_info: Vec<EncodedState> = postcard::from_bytes(data).unwrap();
let mut updated_keys = Vec::new();
let mut added_keys = Vec::new();
let mut removed_keys = Vec::new();
let now = get_sys_timestamp() as Timestamp;
for EncodedState {
key,
value: record,
timestamp,
} in peers_info
{
match self.states.get_mut(key) {
Some(peer_info) if peer_info.timestamp >= timestamp => {
// do nothing
}
_ => {
let old = self.states.insert(
key.to_string(),
State {
state: record.clone(),
timestamp: now,
},
);
match (old, record) {
(Some(_), Some(_)) => updated_keys.push(key.to_string()),
(None, Some(_)) => added_keys.push(key.to_string()),
(Some(_), None) => removed_keys.push(key.to_string()),
(None, None) => {}
}
}
}
}
if !self.subscribers.inner().is_empty() {
self.subscribers.emit(
&(),
EphemeralStoreEvent {
by: EphemeralEventTrigger::Remote,
added: added_keys.clone(),
updated: updated_keys.clone(),
removed: removed_keys.clone(),
},
);
}
}
pub fn set(&mut self, key: &str, value: impl Into<LoroValue>) {
self._set_local_state(key, Some(value.into()));
}
pub fn delete(&mut self, key: &str) {
self._set_local_state(key, None);
}
pub fn get(&self, key: &str) -> Option<LoroValue> {
self.states.get(key).and_then(|x| x.state.clone())
}
pub fn remove_outdated(&mut self) {
let now = get_sys_timestamp() as Timestamp;
let mut removed = Vec::new();
self.states.retain(|key, state| {
if now - state.timestamp > self.timeout {
if state.state.is_some() {
removed.push(key.clone());
}
false
} else {
true
}
});
if !self.subscribers.inner().is_empty() {
self.subscribers.emit(
&(),
EphemeralStoreEvent {
by: EphemeralEventTrigger::Timeout,
added: vec![],
updated: vec![],
removed,
},
);
}
}
pub fn get_all_states(&self) -> FxHashMap<String, LoroValue> {
self.states
.iter()
.filter(|(_, v)| v.state.is_some())
.map(|(k, v)| (k.clone(), v.state.clone().unwrap()))
.collect()
}
pub fn keys(&self) -> impl Iterator<Item = &str> {
self.states
.keys()
.filter(|&k| self.states.get(k).unwrap().state.is_some())
.map(|s| s.as_str())
}
pub fn subscribe_local_updates(&self, callback: LocalEphemeralCallback) -> Subscription {
let (sub, activate) = self.local_subs.inner().insert((), callback);
activate();
sub
}
pub fn subscribe(&self, callback: EphemeralSubscriber) -> Subscription {
let (sub, activate) = self.subscribers.inner().insert((), callback);
activate();
sub
}
fn _set_local_state(&mut self, key: &str, value: Option<LoroValue>) {
let is_delete = value.is_none();
let old = self.states.insert(
key.to_string(),
State {
state: value,
timestamp: get_sys_timestamp() as Timestamp,
},
);
if !self.local_subs.inner().is_empty() {
self.local_subs.emit(&(), self.encode(key));
}
if !self.subscribers.inner().is_empty() {
if old.is_some() {
self.subscribers.emit(
&(),
EphemeralStoreEvent {
by: EphemeralEventTrigger::Local,
added: vec![],
updated: if !is_delete {
vec![key.to_string()]
} else {
vec![]
},
removed: if !is_delete {
vec![]
} else {
vec![key.to_string()]
},
},
);
} else if !is_delete {
self.subscribers.emit(
&(),
EphemeralStoreEvent {
by: EphemeralEventTrigger::Local,
added: vec![key.to_string()],
updated: vec![],
removed: vec![],
},
);
}
}
}
}