summaryrefslogtreecommitdiffstats
path: root/src/store.rs
blob: 1f1398e54b154282624ea0a52e14ef97db8975d6 (plain)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! JSON view over the Automerge document with conflict-aware merging.

use anyhow::anyhow;
use automerge::{
    transaction::Transactable, ActorId, AutoCommit, ObjId, ObjType, ReadDoc, ScalarValue, Value,
};
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use serde::Serialize;
use serde_json::Value as JsonValue;
use std::collections::BTreeSet;

#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    #[error("path crosses a non-map value")]
    Conflict,
    #[error("value does not exist")]
    NotFound,
    #[error(transparent)]
    Doc(#[from] automerge::AutomergeError),
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

/// Automerge's total order on operations: Lamport counter, then actor.
fn op_order(id: &ObjId) -> (u64, Option<&ActorId>) {
    match id {
        ObjId::Root => (0, None),
        ObjId::Id(counter, actor, _) => (*counter, Some(actor)),
    }
}

#[derive(Debug, Clone)]
enum Resolved {
    /// Concurrent maps presented as one. Newest first; `[0]` is canonical for writes.
    Maps(Vec<ObjId>),
    /// Any other value: the newest concurrent candidate.
    Other(Value<'static>, ObjId),
}

/// All concurrent values of `key` across a merged set of parent maps, newest first.
fn candidates(
    doc: &AutoCommit,
    parents: &[ObjId],
    key: &str,
) -> Result<Vec<(Value<'static>, ObjId)>, StoreError> {
    let mut all = Vec::new();
    for parent in parents {
        for (value, id) in doc.get_all(parent, key)? {
            all.push((value.into_owned(), id));
        }
    }
    all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1)));
    Ok(all)
}

fn resolve(
    doc: &AutoCommit,
    parents: &[ObjId],
    key: &str,
    log: Option<&mut ConflictLog>,
) -> Result<Option<Resolved>, StoreError> {
    let mut all = candidates(doc, parents, key)?;
    if all.is_empty() {
        return Ok(None);
    }

    let merged = matches!(all[0].0, Value::Object(ObjType::Map));

    if all.len() > 1 {
        if let Some(log) = log {
            log.record(doc, key, &all, merged)?;
        }
    }

    if merged {
        let maps = all
            .into_iter()
            .filter(|(value, _)| matches!(value, Value::Object(ObjType::Map)))
            .map(|(_, id)| id)
            .collect();
        Ok(Some(Resolved::Maps(maps)))
    } else {
        let (value, id) = all.swap_remove(0);
        Ok(Some(Resolved::Other(value, id)))
    }
}

fn merged_keys(doc: &AutoCommit, maps: &[ObjId]) -> BTreeSet<String> {
    maps.iter().flat_map(|map| doc.keys(map)).collect()
}

fn resolved_to_json(
    doc: &AutoCommit,
    resolved: &Resolved,
    mut log: Option<&mut ConflictLog>,
) -> Result<JsonValue, StoreError> {
    let maps = match resolved {
        Resolved::Maps(maps) => maps,
        Resolved::Other(Value::Scalar(scalar), _) => {
            return scalar_to_json(scalar.clone().into_owned())
        }
        Resolved::Other(Value::Object(_), id) => return object_to_json(doc, id, log),
    };

    let mut out = serde_json::Map::new();

    if let [map] = maps.as_slice() {
        // Fast path: one pass; get_all only for keys Automerge flags as conflicted.
        for item in doc.map_range(map, ..) {
            let key = item.key.to_string();

            let child = if item.conflict {
                match resolve(doc, maps, &key, log.as_deref_mut())? {
                    Some(child) => child,
                    None => continue,
                }
            } else {
                let id = item.id();
                match Value::from(item.value) {
                    Value::Object(ObjType::Map) => Resolved::Maps(vec![id]),
                    value => Resolved::Other(value.into_owned(), id),
                }
            };

            let json = nested(&mut log, &key, |log| resolved_to_json(doc, &child, log))?;
            out.insert(key, json);
        }
    } else {
        // Several concurrent maps: union of their keys.
        for key in merged_keys(doc, maps) {
            let Some(child) = resolve(doc, maps, &key, log.as_deref_mut())? else {
                continue;
            };
            let json = nested(&mut log, &key, |log| resolved_to_json(doc, &child, log))?;
            out.insert(key, json);
        }
    }

    Ok(JsonValue::Object(out))
}

fn object_to_json(
    doc: &AutoCommit,
    id: &ObjId,
    mut log: Option<&mut ConflictLog>,
) -> Result<JsonValue, StoreError> {
    match doc.object_type(id)? {
        ObjType::Map => resolved_to_json(doc, &Resolved::Maps(vec![id.clone()]), log),
        ObjType::List => {
            let mut out = Vec::new();
            for (index, item) in doc.list_range(id, ..).enumerate() {
                let child_id = item.id();
                out.push(match Value::from(item.value) {
                    Value::Object(_) => nested(&mut log, &index.to_string(), |log| {
                        object_to_json(doc, &child_id, log)
                    })?,
                    Value::Scalar(scalar) => scalar_to_json(scalar.into_owned())?,
                });
            }
            Ok(JsonValue::Array(out))
        }
        ObjType::Text => Ok(JsonValue::String(doc.text(id)?)),
        ObjType::Table => Err(anyhow!("Automerge tables are not supported").into()),
    }
}

fn delete_in_all(doc: &mut AutoCommit, maps: &[ObjId], key: &str) -> Result<(), StoreError> {
    for map in maps {
        if doc.get(map, key)?.is_some() {
            doc.delete(map, key)?;
        }
    }
    Ok(())
}

fn read_path_inner(
    doc: &AutoCommit,
    path: &[String],
    mut log: Option<&mut ConflictLog>,
) -> Result<Option<JsonValue>, StoreError> {
    let mut current = Resolved::Maps(vec![automerge::ROOT]);

    for (index, segment) in path.iter().enumerate() {
        let Resolved::Maps(maps) = &current else {
            return Ok(None);
        };
        if let Some(log) = log.as_deref_mut() {
            // The last segment is the requested value itself, not an ancestor.
            log.in_ancestors = index + 1 < path.len();
        }
        match resolve(doc, maps, segment, log.as_deref_mut())? {
            Some(next) => current = next,
            None => return Ok(None),
        }
        if let Some(log) = log.as_deref_mut() {
            log.path.push(segment.clone());
        }
    }

    if let Some(log) = log.as_deref_mut() {
        log.in_ancestors = false;
    }
    Ok(Some(resolved_to_json(doc, &current, log)?))
}

/// Reads a path; the empty path is the whole store.
pub fn read_path(doc: &AutoCommit, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
    read_path_inner(doc, path, None)
}

pub fn read_path_with_conflicts(
    doc: &AutoCommit,
    path: &[String],
) -> Result<Option<(JsonValue, Vec<ConflictReport>)>, StoreError> {
    let mut log = ConflictLog {
        want_values: true,
        ..Default::default()
    };
    let value = read_path_inner(doc, path, Some(&mut log))?;
    Ok(value.map(|value| (value, log.reports)))
}

/// For streams: the value, plus whether the path or subtree involves any conflict.
/// Reports are collected without values, so this costs little more than read_path.
pub fn read_path_traced(
    doc: &AutoCommit,
    path: &[String],
) -> Result<(Option<JsonValue>, bool), StoreError> {
    let mut log = ConflictLog::default();
    let value = read_path_inner(doc, path, Some(&mut log))?;
    Ok((value, !log.reports.is_empty()))
}

/// Keys of the merged map at `path`; empty if it doesn't exist or isn't a map.
pub fn keys_at(doc: &AutoCommit, path: &[String]) -> Result<Vec<String>, StoreError> {
    let mut maps = vec![automerge::ROOT];
    for segment in path {
        maps = match resolve(doc, &maps, segment, None)? {
            Some(Resolved::Maps(next)) => next,
            _ => return Ok(Vec::new()),
        };
    }
    Ok(merged_keys(doc, &maps).into_iter().collect())
}

/// Writes (replaces) the value at `path`, creating intermediate maps.
pub fn write_path(
    doc: &mut AutoCommit,
    path: &[String],
    value: &JsonValue,
) -> Result<(), StoreError> {
    let (leaf, parents) = path.split_last().ok_or(StoreError::NotFound)?;

    let mut maps = vec![automerge::ROOT];
    for segment in parents {
        maps = match resolve(doc, &maps, segment, None)? {
            Some(Resolved::Maps(next)) => next,
            Some(Resolved::Other(..)) => return Err(StoreError::Conflict),
            // A concurrent creation elsewhere is merged on read.
            None => vec![doc.put_object(&maps[0], segment.as_str(), ObjType::Map)?],
        };
    }

    // Replace semantics: clear the key in every merged parent, or an old map
    // would merge into the new value.
    delete_in_all(doc, &maps, leaf)?;
    json_to_automerge(doc, &maps[0], leaf, value)?;
    Ok(())
}

/// Deletes the value at `path`, optionally pruning ancestors that become empty.
pub fn delete_path(doc: &mut AutoCommit, path: &[String], prune: bool) -> Result<(), StoreError> {
    let (leaf, parents) = path.split_last().ok_or(StoreError::NotFound)?;

    // levels[d] = merged maps at path[..d]
    let mut levels = vec![vec![automerge::ROOT]];
    for segment in parents {
        let next = match resolve(doc, levels.last().expect("non-empty"), segment, None)? {
            Some(Resolved::Maps(maps)) => maps,
            _ => return Err(StoreError::NotFound),
        };
        levels.push(next);
    }

    let maps = levels.last().expect("non-empty");
    if resolve(doc, maps, leaf, None)?.is_none() {
        return Err(StoreError::NotFound);
    }
    delete_in_all(doc, maps, leaf)?;

    if prune {
        // Never prune the root (depth 0).
        for depth in (1..levels.len()).rev() {
            if !merged_keys(doc, &levels[depth]).is_empty() {
                break;
            }
            delete_in_all(doc, &levels[depth - 1], &path[depth - 1])?;
        }
    }

    Ok(())
}

// --- materialization ---------------------------------------------------------

/// Copies the merged view of `src` into `dst` as fresh objects.
///
/// * Conflicts collapse exactly as reads resolve them: concurrent maps merge, everything
///   else keeps the newest candidate.
/// * Automerge types survive: text stays text, counters stay counters (normalized to
///   their current value), bytes, timestamps and floats are copied bit-exact.
/// * Map keys are visited in sorted order and list elements in order, so two documents
///   with the same view produce the same operations in the same order.
///
/// `skip` sees the path of every map entry and list element (list indices as decimal
/// strings); returning true leaves that subtree out.
pub(crate) fn materialize_into(
    src: &AutoCommit,
    dst: &mut AutoCommit,
    skip: &dyn Fn(&[String]) -> bool,
) -> Result<(), StoreError> {
    let mut path = Vec::new();
    copy_maps(
        src,
        &[automerge::ROOT],
        dst,
        &automerge::ROOT,
        &mut path,
        skip,
    )
}

fn copy_maps(
    src: &AutoCommit,
    maps: &[ObjId],
    dst: &mut AutoCommit,
    target: &ObjId,
    path: &mut Vec<String>,
    skip: &dyn Fn(&[String]) -> bool,
) -> Result<(), StoreError> {
    // merged_keys is a BTreeSet: sorted, so the operation order is deterministic.
    for key in merged_keys(src, maps) {
        path.push(key.clone());
        if !skip(path) {
            if let Some(resolved) = resolve(src, maps, &key, None)? {
                copy_resolved(src, &resolved, dst, target, Slot::Key(&key), path, skip)?;
            }
        }
        path.pop();
    }
    Ok(())
}

fn copy_resolved(
    src: &AutoCommit,
    resolved: &Resolved,
    dst: &mut AutoCommit,
    parent: &ObjId,
    slot: Slot,
    path: &mut Vec<String>,
    skip: &dyn Fn(&[String]) -> bool,
) -> Result<(), StoreError> {
    match resolved {
        Resolved::Maps(maps) => {
            let id = put_object(dst, parent, slot, ObjType::Map)?;
            copy_maps(src, maps, dst, &id, path, skip)
        }
        Resolved::Other(Value::Scalar(scalar), _) => {
            let scalar = match scalar.as_ref() {
                // A read counter carries its increment history; copy only its value, so
                // equal counters produce equal operations.
                ScalarValue::Counter(counter) => ScalarValue::counter(i64::from(counter.clone())),
                other => other.clone(),
            };
            put_scalar(dst, parent, slot, scalar)
        }
        Resolved::Other(Value::Object(obj_type), id) => {
            copy_object(src, *obj_type, id, dst, parent, slot, path, skip)
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn copy_object(
    src: &AutoCommit,
    obj_type: ObjType,
    id: &ObjId,
    dst: &mut AutoCommit,
    parent: &ObjId,
    slot: Slot,
    path: &mut Vec<String>,
    skip: &dyn Fn(&[String]) -> bool,
) -> Result<(), StoreError> {
    match obj_type {
        ObjType::Map => copy_resolved(
            src,
            &Resolved::Maps(vec![id.clone()]),
            dst,
            parent,
            slot,
            path,
            skip,
        ),
        ObjType::List => {
            let list = put_object(dst, parent, slot, ObjType::List)?;
            // Skipped elements leave no gap in the copy.
            let mut position = 0;
            for (index, item) in src.list_range(id, ..).enumerate() {
                path.push(index.to_string());
                if !skip(path) {
                    let child_id = item.id();
                    let child = match Value::from(item.value).into_owned() {
                        Value::Object(ObjType::Map) => Resolved::Maps(vec![child_id]),
                        value => Resolved::Other(value, child_id),
                    };
                    copy_resolved(src, &child, dst, &list, Slot::Index(position), path, skip)?;
                    position += 1;
                }
                path.pop();
            }
            Ok(())
        }
        ObjType::Text => {
            let text = put_object(dst, parent, slot, ObjType::Text)?;
            dst.splice_text(&text, 0, 0, &src.text(id)?)?;
            Ok(())
        }
        ObjType::Table => Err(anyhow!("Automerge tables are not supported").into()),
    }
}

/// Where a value is placed inside its parent object.
#[derive(Clone, Copy)]
enum Slot<'a> {
    /// A key in a map.
    Key(&'a str),
    /// An index in a list. The value is inserted here, not overwritten.
    Index(usize),
}

fn put_object(
    doc: &mut AutoCommit,
    parent: &ObjId,
    slot: Slot,
    obj_type: ObjType,
) -> Result<ObjId, StoreError> {
    Ok(match slot {
        Slot::Key(key) => doc.put_object(parent, key, obj_type)?,
        Slot::Index(index) => doc.insert_object(parent, index, obj_type)?,
    })
}

fn put_scalar(
    doc: &mut AutoCommit,
    parent: &ObjId,
    slot: Slot,
    value: impl Into<ScalarValue>,
) -> Result<(), StoreError> {
    match slot {
        Slot::Key(key) => doc.put(parent, key, value)?,
        Slot::Index(index) => doc.insert(parent, index, value)?,
    }
    Ok(())
}

fn write_value(
    doc: &mut AutoCommit,
    parent: &ObjId,
    slot: Slot,
    value: &JsonValue,
) -> Result<(), StoreError> {
    match value {
        JsonValue::Object(object) => {
            let id = put_object(doc, parent, slot, ObjType::Map)?;
            for (child_key, child_value) in object {
                write_value(doc, &id, Slot::Key(child_key), child_value)?;
            }
        }
        JsonValue::Array(items) => {
            let id = put_object(doc, parent, slot, ObjType::List)?;
            for (index, item) in items.iter().enumerate() {
                write_value(doc, &id, Slot::Index(index), item)?;
            }
        }
        JsonValue::String(value) => put_scalar(doc, parent, slot, value.as_str())?,
        JsonValue::Bool(value) => put_scalar(doc, parent, slot, *value)?,
        JsonValue::Number(value) => {
            if let Some(value) = value.as_i64() {
                put_scalar(doc, parent, slot, value)?;
            } else if let Some(value) = value.as_u64() {
                put_scalar(doc, parent, slot, value)?;
            } else if let Some(value) = value.as_f64() {
                put_scalar(doc, parent, slot, value)?;
            } else {
                return Err(anyhow!("Unsupported JSON number").into());
            }
        }
        JsonValue::Null => put_scalar(doc, parent, slot, ScalarValue::Null)?,
    }

    Ok(())
}

fn json_to_automerge(
    doc: &mut AutoCommit,
    parent: &ObjId,
    key: &str,
    value: &JsonValue,
) -> Result<(), StoreError> {
    write_value(doc, parent, Slot::Key(key), value)
}

fn scalar_to_json(scalar: ScalarValue) -> Result<JsonValue, StoreError> {
    match scalar {
        ScalarValue::Null => Ok(JsonValue::Null),
        ScalarValue::Boolean(b) => Ok(JsonValue::Bool(b)),
        ScalarValue::Int(i) => Ok(JsonValue::Number(i.into())),
        ScalarValue::Uint(u) => Ok(JsonValue::Number(u.into())),
        ScalarValue::F64(f) => serde_json::Number::from_f64(f)
            .map(JsonValue::Number)
            .ok_or(anyhow!("Non-finite float cannot be represented as JSON").into()),
        ScalarValue::Str(s) => Ok(JsonValue::String(s.to_string())),
        ScalarValue::Bytes(b) => Ok(JsonValue::String(BASE64.encode(b))),
        ScalarValue::Counter(c) => Ok(JsonValue::Number(i64::from(c).into())),
        ScalarValue::Timestamp(t) => Ok(JsonValue::Number(t.into())),
        ScalarValue::Unknown { .. } => Err(anyhow!("Unsupported Automerge scalar type").into()),
    }
}

// --- conflict log ------------------------------------------------------------

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Resolution {
    MergedMaps,
    NewestWins,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CandidateReport {
    pub op_id: String,
    pub selected: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<JsonValue>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConflictReport {
    pub path: Vec<String>,
    /// On the way to the requested path; reported without candidate values.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub ancestor: bool,
    pub resolution: Resolution,
    pub candidates: Vec<CandidateReport>,
}

#[derive(Default)]
struct ConflictLog {
    path: Vec<String>,
    reports: Vec<ConflictReport>,
    /// Render candidate values (only within the requested subtree).
    want_values: bool,
    /// Currently walking the ancestors of the requested path.
    in_ancestors: bool,
}

fn op_id_string(id: &ObjId) -> String {
    match id {
        ObjId::Root => "_root".to_owned(),
        ObjId::Id(counter, actor, _) => format!("{counter}@{actor}"),
    }
}

fn value_to_json(doc: &AutoCommit, value: &Value<'_>, id: &ObjId) -> Result<JsonValue, StoreError> {
    match value {
        Value::Scalar(scalar) => Ok(scalar_to_json(scalar.clone().into_owned())?),
        Value::Object(_) => object_to_json(doc, id, None),
    }
}

impl ConflictLog {
    fn record(
        &mut self,
        doc: &AutoCommit,
        key: &str,
        all: &[(Value<'static>, ObjId)],
        merged: bool,
    ) -> Result<(), StoreError> {
        let with_values = self.want_values && !self.in_ancestors;

        let candidates = all
            .iter()
            .enumerate()
            .map(|(index, (value, id))| {
                let selected = if merged {
                    matches!(value, Value::Object(ObjType::Map))
                } else {
                    index == 0
                };
                Ok(CandidateReport {
                    op_id: op_id_string(id),
                    selected,
                    value: if with_values {
                        Some(value_to_json(doc, value, id)?)
                    } else {
                        None
                    },
                })
            })
            .collect::<Result<Vec<_>, StoreError>>()?;

        let mut path = self.path.clone();
        path.push(key.to_owned());

        self.reports.push(ConflictReport {
            path,
            ancestor: self.in_ancestors,
            resolution: if merged {
                Resolution::MergedMaps
            } else {
                Resolution::NewestWins
            },
            candidates,
        });
        Ok(())
    }
}

/// Runs `f` with `segment` appended to the conflict path.
fn nested<T>(
    log: &mut Option<&mut ConflictLog>,
    segment: &str,
    f: impl FnOnce(Option<&mut ConflictLog>) -> T,
) -> T {
    if let Some(log) = log.as_deref_mut() {
        log.path.push(segment.to_owned());
    }
    let out = f(log.as_deref_mut());
    if let Some(log) = log.as_deref_mut() {
        log.path.pop();
    }
    out
}