-
Notifications
You must be signed in to change notification settings - Fork 164
/
lib.rs
473 lines (444 loc) · 17.5 KB
/
lib.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use automerge_protocol::{
ActorId, ChangeHash, MapType, ObjectId, Op, OpId, Patch, UncompressedChange,
};
mod error;
mod mutation;
mod path;
mod state_tree;
mod value;
pub use error::{
AutomergeFrontendError, InvalidChangeRequest, InvalidInitialStateError, InvalidPatch,
};
pub use mutation::{LocalChange, MutableDocument};
pub use path::Path;
use path::PathElement;
use state_tree::ResolvedPath;
use std::convert::TryFrom;
use std::error::Error;
use std::{collections::HashMap, fmt::Debug};
pub use value::{Conflicts, Cursor, Primitive, Value};
/// Tracks the possible states of the frontend
///
/// What does this mean and why do we need it? The reason the frontend/backend
/// split exists in the first place is that we want to quickly apply local
/// changes (local in this sense means something like "on the UI thread") on a
/// low latency local cache whilst also shipping those same changes off to a
/// backend, which can reconcile them with historical changes and new changes
/// received over the network - work which may be more compute intensive and
/// so have to high a latency to be acceptable on the UI thread.
///
/// This frontend/backend split implies that we need to optimistically apply
/// local changes somehow. In order to do this we immediately apply changes to
/// a copy of the local state (state being an instance of [StateTree]) and
/// add the sequence number of the new change to a list of in flight requests.
/// In detail the logic looks like this:
///
/// When we receive a patch from the backend:
/// 1. Check that if the patch is for our actor ID then the sequence number of
/// the patch is the same as the sequence number of the oldest in flight
/// request.
/// 2. Apply the patch to the `reconciled_state` of the current state
/// 3. If there are no in flight requests remaining then transition from
/// the `WaitingForInFlightRequests` state to the `Reconciled` state,
/// moving the `reconciled_state` into the `Reconciled` enum branch
#[derive(Clone, Debug)]
enum FrontendState {
WaitingForInFlightRequests {
in_flight_requests: Vec<u64>,
reconciled_root_state: state_tree::StateTree,
optimistically_updated_root_state: state_tree::StateTree,
max_op: u64,
},
Reconciled {
root_state: state_tree::StateTree,
max_op: u64,
deps_of_last_received_patch: Vec<ChangeHash>,
},
}
impl FrontendState {
/// Apply a patch received from the backend to this frontend state,
/// returns the updated cached value (if it has changed) and a new
/// `FrontendState` which replaces this one
fn apply_remote_patch(self, self_actor: &ActorId, patch: &Patch) -> Result<Self, InvalidPatch> {
match self {
FrontendState::WaitingForInFlightRequests {
in_flight_requests,
reconciled_root_state,
optimistically_updated_root_state,
max_op,
} => {
let mut new_in_flight_requests = in_flight_requests;
// If the actor ID and seq exist then this patch corresponds
// to a local change (i.e it came from Backend::apply_local_change
// so we don't need to apply it, we just need to remove it from
// the in_flight_requests vector
if let (Some(patch_actor), Some(patch_seq)) = (&patch.actor, patch.seq) {
// If this is a local change corresponding to our actor then we
// need to match it against in flight requests
if self_actor == patch_actor {
// Check that if the patch is for our actor ID then it is not
// out of order
if new_in_flight_requests[0] != patch_seq {
return Err(InvalidPatch::MismatchedSequenceNumber {
expected: new_in_flight_requests[0],
actual: patch_seq,
});
}
// unwrap should be fine here as `in_flight_requests` should never have zero length
// because we transition to reconciled state when that happens
let (_, remaining_requests) = new_in_flight_requests.split_first().unwrap();
new_in_flight_requests = remaining_requests.iter().copied().collect();
}
}
let new_reconciled_root_state = if let Some(diff) = &patch.diffs {
reconciled_root_state.apply_diff(diff)?
} else {
reconciled_root_state
};
Ok(match new_in_flight_requests[..] {
[] => FrontendState::Reconciled {
root_state: new_reconciled_root_state,
max_op: patch.max_op,
deps_of_last_received_patch: patch.deps.clone(),
},
_ => FrontendState::WaitingForInFlightRequests {
in_flight_requests: new_in_flight_requests,
reconciled_root_state: new_reconciled_root_state,
optimistically_updated_root_state,
max_op,
},
})
}
FrontendState::Reconciled { root_state, .. } => {
let new_root_state = if let Some(diff) = &patch.diffs {
root_state.apply_diff(diff)?
} else {
root_state
};
Ok(FrontendState::Reconciled {
root_state: new_root_state,
max_op: patch.max_op,
deps_of_last_received_patch: patch.deps.clone(),
})
}
}
}
fn get_object_id(&self, path: &Path) -> Option<ObjectId> {
self.resolve_path(path).and_then(|r| r.object_id())
}
fn get_value(&self, path: &Path) -> Option<Value> {
self.resolve_path(path).map(|r| r.default_value())
}
fn resolve_path(&self, path: &Path) -> Option<ResolvedPath> {
let root = match self {
FrontendState::WaitingForInFlightRequests {
optimistically_updated_root_state,
..
} => optimistically_updated_root_state,
FrontendState::Reconciled { root_state, .. } => root_state,
};
root.resolve_path(path)
}
/// Apply a patch. The change closure will be passed a `MutableDocument`
/// which it can use to query the document state and make changes. It
/// can also throw an error of type `E`. If an error is thrown in the
/// closure no chnages are made and the error is returned.
pub fn optimistically_apply_change<F, E>(
self,
actor: &ActorId,
change_closure: F,
seq: u64,
) -> Result<OptimisticChangeResult, E>
where
E: Error,
F: FnOnce(&mut dyn MutableDocument) -> Result<(), E>,
{
match self {
FrontendState::WaitingForInFlightRequests {
mut in_flight_requests,
reconciled_root_state,
optimistically_updated_root_state,
max_op,
} => {
let mut mutation_tracker = mutation::MutationTracker::new(
optimistically_updated_root_state,
max_op,
actor.clone(),
);
change_closure(&mut mutation_tracker)?;
let new_root_state = mutation_tracker.state.clone();
in_flight_requests.push(seq);
Ok(OptimisticChangeResult {
ops: mutation_tracker.ops(),
new_state: FrontendState::WaitingForInFlightRequests {
in_flight_requests,
optimistically_updated_root_state: new_root_state,
reconciled_root_state,
max_op: mutation_tracker.max_op,
},
deps: Vec::new(),
})
}
FrontendState::Reconciled {
root_state,
max_op,
deps_of_last_received_patch,
} => {
let mut mutation_tracker =
mutation::MutationTracker::new(root_state.clone(), max_op, actor.clone());
change_closure(&mut mutation_tracker)?;
let new_root_state = mutation_tracker.state.clone();
let in_flight_requests = vec![seq];
Ok(OptimisticChangeResult {
ops: mutation_tracker.ops(),
new_state: FrontendState::WaitingForInFlightRequests {
in_flight_requests,
optimistically_updated_root_state: new_root_state,
reconciled_root_state: root_state,
max_op: mutation_tracker.max_op,
},
deps: deps_of_last_received_patch,
})
}
}
}
fn in_flight_requests(&self) -> Vec<u64> {
match self {
FrontendState::WaitingForInFlightRequests {
in_flight_requests, ..
} => in_flight_requests.clone(),
_ => Vec::new(),
}
}
fn max_op(&self) -> u64 {
match self {
FrontendState::WaitingForInFlightRequests { max_op, .. } => *max_op,
FrontendState::Reconciled { max_op, .. } => *max_op,
}
}
fn value(&self) -> Value {
match self {
FrontendState::WaitingForInFlightRequests {
optimistically_updated_root_state,
..
} => optimistically_updated_root_state.value(),
FrontendState::Reconciled { root_state, .. } => root_state.value(),
}
}
}
pub struct Frontend {
pub actor_id: ActorId,
pub seq: u64,
/// The current state of the frontend, see the description of
/// `FrontendState` for details. It's an `Option` to allow consuming it
/// using Option::take whilst behind a mutable reference.
state: Option<FrontendState>,
/// A cache of the value of this frontend
cached_value: Option<Value>,
/// A function for generating timestamps
timestamper: Box<dyn Fn() -> Option<i64>>,
}
impl Debug for Frontend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let Frontend {
actor_id,
seq,
state,
cached_value,
timestamper: _,
} = self;
{
let mut builder = f.debug_struct("Person");
let _ = builder.field("actor_id", &actor_id);
let _ = builder.field("seq", &seq);
let _ = builder.field("state", &state);
let _ = builder.field("cached_value", &cached_value);
builder.finish()
}
}
}
#[cfg(feature = "std")]
impl Default for Frontend {
fn default() -> Self {
Self::new()
}
}
impl Frontend {
#[cfg(feature = "std")]
pub fn new() -> Self {
let system_time = || {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.and_then(|d| i64::try_from(d.as_millis()).ok())
};
Self::new_with_timestamper(Box::new(system_time))
}
#[cfg(feature = "std")]
pub fn new_with_actor_id(actor_id: uuid::Uuid) -> Self {
let system_time = || {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.and_then(|d| i64::try_from(d.as_millis()).ok())
};
Self::new_with_timestamper_and_actor_id(Box::new(system_time), actor_id)
}
pub fn new_with_timestamper(t: Box<dyn Fn() -> Option<i64>>) -> Self {
Self::new_with_timestamper_and_actor_id(t, uuid::Uuid::new_v4())
}
pub fn new_with_timestamper_and_actor_id(
t: Box<dyn Fn() -> Option<i64>>,
actor_id: uuid::Uuid,
) -> Self {
let root_state = state_tree::StateTree::new();
Frontend {
actor_id: ActorId::from_bytes(actor_id.as_bytes()),
seq: 0,
state: Some(FrontendState::Reconciled {
root_state,
max_op: 0,
deps_of_last_received_patch: Vec::new(),
}),
cached_value: None,
timestamper: t,
}
}
#[cfg(feature = "std")]
pub fn new_with_initial_state(
initial_state: Value,
) -> Result<(Self, UncompressedChange), InvalidInitialStateError> {
match &initial_state {
Value::Map(kvs, MapType::Map) => {
let mut front = Frontend::new();
let (init_ops, _) =
kvs.iter()
.fold((Vec::new(), 1), |(mut ops, max_op), (k, v)| {
let (more_ops, max_op) = value::value_to_op_requests(
&front.actor_id,
max_op,
ObjectId::Root,
&k.into(),
v,
false,
);
ops.extend(more_ops);
(ops, max_op)
});
let init_change_request = UncompressedChange {
actor_id: front.actor_id.clone(),
start_op: 1,
time: (front.timestamper)().unwrap_or(0),
seq: 1,
message: Some("Initialization".to_string()),
hash: None,
deps: Vec::new(),
operations: init_ops,
extra_bytes: Vec::new(),
};
// Unwrap here is fine because it should be impossible to
// cause an error applying a local change from a `Value`. If
// that happens we've made an error, not the user.
front.change(Some("initialization".into()), |doc| {
doc.add_change(LocalChange::set(Path::root(), initial_state))
.map_err(|_| InvalidInitialStateError::InitialStateMustBeMap)
})?;
Ok((front, init_change_request))
}
_ => Err(InvalidInitialStateError::InitialStateMustBeMap),
}
}
pub fn state(&mut self) -> &Value {
if let Some(ref v) = self.cached_value {
v
} else {
let value = self.state.as_ref().unwrap().value();
self.cached_value = Some(value);
self.cached_value.as_ref().unwrap()
}
}
pub fn change<F, E>(
&mut self,
message: Option<String>,
change_closure: F,
) -> Result<Option<UncompressedChange>, E>
where
E: Error,
F: FnOnce(&mut dyn MutableDocument) -> Result<(), E>,
{
let start_op = self.state.as_ref().unwrap().max_op() + 1;
// TODO this leaves the `state` as `None` if there's an error, it shouldn't
let change_result = self.state.take().unwrap().optimistically_apply_change(
&self.actor_id,
change_closure,
self.seq + 1,
)?;
self.cached_value = None;
self.state = Some(change_result.new_state);
if let Some(ops) = change_result.ops {
self.seq += 1;
let change = UncompressedChange {
start_op,
actor_id: self.actor_id.clone(),
seq: self.seq,
time: (self.timestamper)().unwrap_or(0),
message,
hash: None,
deps: change_result.deps,
operations: ops,
extra_bytes: Vec::new(),
};
Ok(Some(change))
} else {
Ok(None)
}
}
pub fn apply_patch(&mut self, patch: Patch) -> Result<(), InvalidPatch> {
// TODO this leaves the `state` as `None` if there's an error, it shouldn't
self.cached_value = None;
let new_state = self
.state
.take()
.unwrap()
.apply_remote_patch(&self.actor_id, &patch)?;
self.state = Some(new_state);
if let Some(seq) = patch.clock.get(&self.actor_id) {
if *seq > self.seq {
self.seq = *seq;
}
}
Ok(())
}
pub fn get_object_id(&self, path: &Path) -> Option<ObjectId> {
self.state.as_ref().and_then(|s| s.get_object_id(path))
}
pub fn in_flight_requests(&self) -> Vec<u64> {
self.state
.as_ref()
.map(|s| s.in_flight_requests())
.unwrap_or_default()
}
/// Gets the set of values for `path`, returns None if the path does not
/// exist
pub fn get_conflicts(&self, path: &Path) -> Option<HashMap<OpId, Value>> {
self.state
.as_ref()
.and_then(|s| s.resolve_path(path))
.map(|o| o.values())
}
pub fn get_value(&self, path: &Path) -> Option<Value> {
self.state.as_ref().and_then(|s| s.get_value(path))
}
/// Returns the value given by path, if it exists
pub fn value_at_path(&self, path: &Path) -> Option<Value> {
self.state
.as_ref()
.and_then(|s| s.resolve_path(&path))
.map(|o| o.default_value())
}
}
struct OptimisticChangeResult {
ops: Option<Vec<Op>>,
new_state: FrontendState,
deps: Vec<ChangeHash>,
}