OXYGENHUB

Coverage / crates/oxy-core/src/state.rs 92.6%

2067 of 2233 lines run, 436 of 633 functions · run #294 on main · run missed

1
//! A state: a dependency-closed set of changes, applied to a tree graph and
2
//! per-node file graphs (spec §8.3), with materialization (spec §9.5, §11).
3
4
use crate::cbor::{self, Value};
5
use crate::graph::{AtomKey, DepOracle, FileGraph, Materialized, Vertex};
6
use crate::hash::KeyId;
7
use crate::hash::{ChangeHash, ObjectRef};
8
use crate::ids::NodeId;
9
use crate::objects::{
10
    self as o, AtomRef, Change, ChangeKind, Edit, Hlc, Mode, NodeKind, ObjectError, TreeOp,
11
};
12
use crate::tree::{TreeGraph, TreeView};
13
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
14
use std::sync::Arc;
15
16
/// Checkpoint payload version 2 (spec §11): file graphs by reference.
17
pub const CHECKPOINT_INDEX_VERSION: u64 = 2;
18
19
/// A file graph encoded in fewer bytes stays inline in a version 2 index.
20
pub const INLINE_GRAPH_LIMIT: usize = 2048;
21
22
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
23
pub enum StateError {
24
    #[error("change {1} depends on {0}, which is not in the state")]
25
    MissingDep(ChangeHash, ChangeHash),
26
}
27
28
/// One `Replace` edit: `(change, old blob, new blob)`.
29
type ReplaceRecord = (ChangeHash, Option<ObjectRef>, Option<ObjectRef>);
30
31
/// What a state remembers about a change: everything but its operations,
32
/// which live in the graphs once applied (and in the store).
33
#[derive(Clone, Debug, PartialEq, Eq)]
34
pub struct ChangeMeta {
35
    pub change_id: crate::ids::ChangeId,
36
    pub deps: Vec<ChangeHash>,
37
    pub author: KeyId,
38
    pub hlc: Hlc,
39
    pub message: String,
40
    pub kind: ChangeKind,
41
}
42
43
impl ChangeMeta {
44
    /// In the flat layout of local indexes and caches ([`crate::flat`]).
451536×
    pub(crate) fn write_flat(&self, w: &mut crate::flat::Writer) {
461536×
        w.fixed(&self.change_id.0);
471536×
        w.count(self.deps.len());
481536×
        for d in &self.deps {
491082×
            w.object(d);
501082×
        }
511536×
        w.object(&self.author);
521536×
        w.u64(self.hlc.wall_ms);
531536×
        w.u32(self.hlc.logical);
541536×
        w.bytes(self.message.as_bytes());
551536×
        w.u8(self.kind.tag() as u8);
561536×
    }
57
581647×
    pub(crate) fn read_flat(r: &mut crate::flat::Reader) -> Result<ChangeMeta, ObjectError> {
591647×
        let change_id = crate::ids::ChangeId(r.fixed::<16>()?);
601647×
        let mut deps = Vec::new();
611647×
        for _ in 0..r.count()? {
62835×
            deps.push(r.object()?);
63
        }
64
        Ok(ChangeMeta {
651647×
            change_id,
661647×
            deps,
671647×
            author: r.object()?,
68
            hlc: Hlc {
691647×
                wall_ms: r.u64()?,
701647×
                logical: r.u32()?,
71
            },
721647×
            message: r.text()?,
731647×
            kind: ChangeKind::from_tag(r.u8()? as u64)?,
74
        })
751647×
    }
76
7715416×
    pub fn of(c: &Change) -> ChangeMeta {
7815416×
        ChangeMeta {
7915416×
            change_id: c.change_id,
8015416×
            deps: c.deps.clone(),
8115416×
            author: c.author,
8215416×
            hlc: c.hlc,
8315416×
            message: c.message.clone(),
8415416×
            kind: c.kind,
8515416×
        }
8615416×
    }
87
}
88
89
#[derive(Default)]
90
pub struct State {
91
    /// Shared between clones, like `files` and `deferred`: a state kept
92
    /// beside another that differs by a few changes shares the rest.
93
    changes: crate::fasthash::FastMap<ChangeHash, Arc<ChangeMeta>>,
94
    tree: TreeGraph,
95
    /// Shared between clones: a clone copies a graph only when an edit
96
    /// changes it (`Arc::make_mut`), so keeping several states that differ
97
    /// by a few changes costs little more than one.
98
    files: crate::fasthash::FastMap<NodeId, Arc<FileGraph>>,
99
    replaces: HashMap<NodeId, Vec<ReplaceRecord>>,
100
    /// Memoised transitive dependencies, bounded by the hashes they hold
101
    /// (each set is the whole history below a change, so keeping one for
102
    /// every change asked about grew with the square of history: #25). A
103
    /// mutex, not a `RefCell`, so a loaded state can be shared across
104
    /// threads by a host that caches it.
105
    closure: std::sync::RwLock<ClosureMemo>,
106
    /// Each change's level: 0 with no dependencies, else one more than its
107
    /// highest dependency. A change can only depend on changes of lower
108
    /// level, which bounds the walk [`DepOracle::depends_on`] does.
109
    levels: std::sync::RwLock<crate::fasthash::FastMap<ChangeHash, u32>>,
110
    /// `None` for a full state. For a history-only state (loaded for its
111
    /// changes and tree), the file graphs not built yet, by node: `files`
112
    /// holds only those loaded with [`State::load_graphs`].
113
    deferred: Option<crate::fasthash::FastMap<NodeId, Arc<Deferred>>>,
114
}
115
116
use crate::objects::TextRef;
117
118
/// A file graph a history-only state has not built: its base from a
119
/// version 2 checkpoint, if any, and the edits of later changes in the
120
/// order they were applied (spec §11). Building it later gives the same
121
/// graph, since graph construction does not depend on order (§8).
122
///
123
/// Read from a state index, it stays in the index, unread, until first
124
/// used: a command that builds a few files reads and checks only their
125
/// histories (oxygen#94).
126
#[derive(Clone, Default)]
127
struct Deferred {
128
    /// The history: `raw` read and decoded, with `more` after it. An
129
    /// error when `raw` does not match its hash.
130
    body: std::sync::OnceLock<Result<DeferredBody, String>>,
131
    /// The history as an index holds it, not read yet.
132
    raw: Option<RawHistory>,
133
    /// Changes added since the index was read, kept beside `raw`, so that
134
    /// adding one neither reads nor decodes the history.
135
    more: Vec<(ChangeHash, Vec<Edit>)>,
136
    more_texts: Vec<TextRef>,
137
    /// Its graph key, once worked out (or read from an index); reset by
138
    /// any edit added.
139
    key: std::sync::OnceLock<[u8; 32]>,
140
}
141
142
/// Where a state index's file histories are: the index file itself, read
143
/// on first use, or bytes in memory.
144
#[derive(Clone)]
145
pub enum Histories {
146
    Bytes(Arc<Vec<u8>>),
147
    /// The file, where its histories start, and how many bytes they take.
148
    File(Arc<std::fs::File>, u64, u64),
149
}
150
151
impl Histories {
15216694×
    fn len(&self) -> u64 {
15316694×
        match self {
1544527×
            Histories::Bytes(b) => b.len() as u64,
15512167×
            Histories::File(_, _, len) => *len,
156
        }
15716694×
    }
158
15921847×
    fn read(&self, at: u64, len: usize) -> std::io::Result<Vec<u8>> {
16021847×
        match self {
1614527×
            Histories::Bytes(b) => Ok(b[at as usize..at as usize + len].to_vec()),
16217320×
            Histories::File(f, start, _) => {
16317320×
                let mut buf = vec![0u8; len];
16417320×
                read_exact_at(f, &mut buf, start + at)?;
16517320×
                Ok(buf)
166
            }
167
        }
16821847×
    }
169
}
170
171
#[cfg(unix)]
17217320×
fn read_exact_at(f: &std::fs::File, buf: &mut [u8], at: u64) -> std::io::Result<()> {
17317320×
    std::os::unix::fs::FileExt::read_exact_at(f, buf, at)
17417320×
}
175
176
#[cfg(windows)]
177
fn read_exact_at(f: &std::fs::File, mut buf: &mut [u8], mut at: u64) -> std::io::Result<()> {
178
    while !buf.is_empty() {
179
        match std::os::windows::fs::FileExt::seek_read(f, buf, at)? {
180
            0 => return Err(std::io::ErrorKind::UnexpectedEof.into()),
181
            n => {
182
                buf = &mut buf[n..];
183
                at += n as u64;
184
            }
185
        }
186
    }
187
    Ok(())
188
}
189
190
/// One file's history in a state index: where it is and its hash.
191
#[derive(Clone)]
192
struct RawHistory {
193
    from: Histories,
194
    at: u64,
195
    len: usize,
196
    sum: [u8; 32],
197
}
198
199
impl RawHistory {
200
    /// The history's bytes, checked against the hash the index holds.
20121847×
    fn bytes(&self) -> Result<Vec<u8>, ObjectError> {
20221847×
        let damaged = |why: String| ObjectError::DamagedIndex(why);
20321847×
        let b = self
20421847×
            .from
20521847×
            .read(self.at, self.len)
20621847×
            .map_err(|e| damaged(format!("a file's history cannot be read: {e}")))?;
20721847×
        if *blake3::hash(&b).as_bytes() != self.sum {
2083×
            return Err(damaged("a file's history does not match its hash".into()));
20921844×
        }
21021844×
        Ok(b)
21121847×
    }
212
}
213
214
/// The closures kept, and how many hashes they hold in all.
215
#[derive(Default)]
216
struct ClosureMemo {
217
    sets: HashMap<ChangeHash, Arc<HashSet<ChangeHash>>>,
218
    held: usize,
219
}
220
221
/// The most hashes the closure memo holds before it starts again: a few
222
/// hundred megabytes at most.
223
const CLOSURE_MEMO_HASHES: usize = 4_000_000;
224
225
#[derive(Clone, Default)]
226
struct DeferredBody {
227
    base: Option<DeferredBase>,
228
    edits: Vec<(ChangeHash, Vec<Edit>)>,
229
    /// The text blob and line count of each insert in `edits` left without
230
    /// its lines, in order: read when the graph is built.
231
    texts: Vec<TextRef>,
232
}
233
234
impl Deferred {
235141×
    fn from_body(body: DeferredBody) -> Deferred {
236141×
        Deferred {
237141×
            body: std::sync::OnceLock::from(Ok(body)),
238141×
            ..Deferred::default()
239141×
        }
240141×
    }
241
242
    /// The history, read and checked on first use.
243102625×
    fn body(&self) -> Result<&DeferredBody, ObjectError> {
244102625×
        self.body
245102625×
            .get_or_init(|| {
24637638×
                let mut b = match &self.raw {
247
                    // Matching its hash, it is what `write_index` wrote: it
248
                    // decodes.
2495475×
                    Some(raw) => raw
2505475×
                        .bytes()
2515475×
                        .and_then(|b| State::read_deferred_body(&b))
2525475×
                        .map_err(|e| e.to_string())?,
25332163×
                    None => DeferredBody::default(),
254
                };
25537637×
                b.edits.extend(self.more.iter().cloned());
25637637×
                b.texts.extend(self.more_texts.iter().cloned());
25737637×
                Ok(b)
25837638×
            })
259102625×
            .as_ref()
260102625×
            .map_err(|e| ObjectError::DamagedIndex(e.clone()))
261102625×
    }
262
263
    /// Add a change's edits to the history: beside the index's encoding
264
    /// while it is unread, so that nothing is read or decoded to add them.
26535506×
    fn add(&mut self, h: ChangeHash, edits: Vec<Edit>, texts: Vec<TextRef>) {
26635506×
        if self.raw.is_some() && !matches!(self.body.get(), Some(Ok(_))) {
2672×
            self.more.push((h, edits));
2682×
            self.more_texts.extend(texts);
269
            // A body found damaged stays so: what was added cannot mend it.
2702×
            return;
27135504×
        }
272
        // No encoding to read, or decoded already: this cannot fail.
27335504×
        let b = self.body_mut().expect("a history in memory");
27435504×
        b.edits.push((h, edits));
27535504×
        b.texts.extend(texts);
27635506×
    }
277
27835504×
    fn body_mut(&mut self) -> Result<&mut DeferredBody, ObjectError> {
27935504×
        self.body()?;
28035504×
        self.raw = None;
28135504×
        self.more.clear();
28235504×
        self.more_texts.clear();
28335504×
        Ok(self
28435504×
            .body
28535504×
            .get_mut()
28635504×
            .and_then(|b| b.as_mut().ok())
28735504×
            .expect("decoded above"))
28835504×
    }
289
29022798×
    fn into_body(mut self) -> Result<DeferredBody, ObjectError> {
29122798×
        self.body()?;
29222797×
        Ok(self
29322797×
            .body
29422797×
            .take()
29522797×
            .and_then(|b| b.ok())
29622797×
            .expect("decoded above"))
29722798×
    }
298
}
299
300
#[derive(Clone)]
301
enum DeferredBase {
302
    Inline(Value),
303
    Blob(ObjectRef),
304
}
305
306
/// A local index of a history-only state (oxygen#41), in the flat layout
307
/// of [`crate::flat`]: its change metadata, tree (with its view), replace
308
/// records and every file's deferred history with its graph key, so a
309
/// command reads it rather than replaying history. Derived only: written
310
/// from a state loaded the long way, and read back as the same state.
311
impl State {
312
    const INDEX_MAGIC: &'static [u8] = b"oxy state index 4\n";
313
314
    /// The index of this state, or `None` when it has anything an index
315
    /// does not hold: a built graph, or text in memory (a change added
316
    /// whole rather than as its skeleton).
317924×
    pub fn write_index(&self) -> Option<Vec<u8>> {
318
        use crate::flat::Writer;
319924×
        let deferred = self.deferred.as_ref()?;
320924×
        if !self.files.is_empty() {
3210×
            return None;
322924×
        }
323
        // The changes first, as a block a reader can take on its own (see
324
        // [`State::read_index_changes`]).
325924×
        let mut w = Writer::default();
326924×
        w.fixed(Self::INDEX_MAGIC);
327924×
        let mut changes: Vec<(&ChangeHash, &Arc<ChangeMeta>)> = self.changes.iter().collect();
328924×
        changes.sort_by_key(|(h, _)| **h);
329924×
        w.count(changes.len());
330924×
        for (h, m) in changes {
331262×
            w.object(h);
332262×
            m.write_flat(&mut w);
333262×
        }
334924×
        let changes_block = std::mem::take(&mut w);
335924×
        w.bytes(&changes_block.0);
336
        // The tree and the files' histories as blocks of their own, so a
337
        // reader decodes them side by side.
338924×
        let mut tw = Writer::default();
339924×
        self.tree.write_flat(&mut tw);
340924×
        w.bytes(&tw.0);
341924×
        let mut replaces: Vec<(&NodeId, &Vec<ReplaceRecord>)> = self.replaces.iter().collect();
342924×
        replaces.sort_by_key(|(n, _)| **n);
343924×
        w.count(replaces.len());
344924×
        for (n, recs) in replaces {
3454×
            w.fixed(&n.0);
3464×
            w.count(recs.len());
347
            // In any order: which record is live does not depend on it.
3484×
            let mut recs: Vec<&ReplaceRecord> = recs.iter().collect();
3494×
            recs.sort();
3504×
            for (c, old, new) in recs {
3514×
                w.object(c);
3524×
                w.opt_object(old);
3534×
                w.opt_object(new);
3544×
            }
355
        }
356924×
        let mut front = w;
357
        // The table: per file its key and where its history is, with the
358
        // history's hash, which is checked when it is read (oxygen#94).
359924×
        let mut table = Writer::default();
360924×
        let mut histories: Vec<u8> = Vec::new();
361924×
        let mut nodes: Vec<(&NodeId, &Arc<Deferred>)> = deferred.iter().collect();
362924×
        nodes.sort_by_key(|(n, _)| **n);
363924×
        table.count(nodes.len());
36432732×
        for (n, d) in nodes {
36532732×
            let key = match d.key.get() {
36616372×
                Some(k) => *k,
367
                None => {
36816360×
                    let mut h = blake3::Hasher::new();
36916360×
                    h.update(b"oxy graph key 1\0");
37016360×
                    h.update(&n.0);
37116360×
                    let k = d.graph_key(h).ok()?;
37216360×
                    let _ = d.key.set(k);
37316360×
                    k
374
                }
375
            };
376
            // Unchanged since read: its bytes as they are, checked.
37732732×
            let bytes = match &d.raw {
37816372×
                Some(raw) if d.more.is_empty() => raw.bytes().ok()?,
37916360×
                _ => Self::write_deferred_body(d.body().ok()?)?,
380
            };
38132730×
            table.fixed(&n.0);
38232730×
            table.fixed(&key);
38332730×
            table.u64(histories.len() as u64);
38432730×
            table.u32(u32::try_from(bytes.len()).ok()?);
38532730×
            table.fixed(blake3::hash(&bytes).as_bytes());
38632730×
            histories.extend_from_slice(&bytes);
387
        }
388922×
        front.bytes(&table.0);
389922×
        let mut out = Writer::default();
390922×
        out.u64(front.0.len() as u64);
391922×
        out.fixed(&front.0);
392922×
        out.fixed(&histories);
393922×
        Some(out.0)
394924×
    }
395
396
    /// One file's history as an index encodes it; `None` when it holds
397
    /// text in memory, which an index does not.
39816360×
    fn write_deferred_body(d: &DeferredBody) -> Option<Vec<u8>> {
399
        use crate::flat::Writer;
40032926×
        let atom = |w: &mut Writer, a: &AtomRef| match a {
40116357×
            AtomRef::Start => w.u8(0),
40216463×
            AtomRef::End => w.u8(1),
403106×
            AtomRef::Atom { change, index } => {
404106×
                w.u8(2);
405106×
                w.object(change);
406106×
                w.u32(*index);
407106×
            }
40832926×
        };
40916360×
        let mut w = Writer::default();
4103×
        match &d.base {
41116357×
            None => w.u8(0),
4120×
            Some(DeferredBase::Blob(r)) => {
4130×
                w.u8(1);
4140×
                w.object(r);
4150×
            }
4163×
            Some(DeferredBase::Inline(v)) => {
4173×
                w.u8(2);
4183×
                w.bytes(&cbor::encode(v));
4193×
            }
420
        }
421
        // Each change's edits with the texts of its inserts, sorted by
422
        // change: a graph does not depend on the order changes applied
423
        // in, and the index is then the same however history was read.
42416360×
        let mut texts = d.texts.iter();
42516360×
        let mut per_change: Vec<(&ChangeHash, &Vec<Edit>, Vec<&TextRef>)> = Vec::new();
42616463×
        for (c, edits) in &d.edits {
42716463×
            let n = edits
42816463×
                .iter()
42916463×
                .filter(|e| matches!(e, Edit::Insert { lines, .. } if lines.is_empty()))
43016463×
                .count();
43116463×
            per_change.push((c, edits, texts.by_ref().take(n).collect()));
432
        }
43316360×
        per_change.sort_by_key(|(c, _, _)| **c);
43416360×
        w.count(per_change.len());
43516463×
        for (c, edits, _) in &per_change {
43616463×
            w.object(c);
43716463×
            w.count(edits.len());
43816463×
            for e in edits.iter() {
43916463×
                match e {
440
                    Edit::Insert {
44116463×
                        node,
44216463×
                        after,
44316463×
                        before,
44416463×
                        lines,
445
                    } => {
44616463×
                        if !lines.is_empty() {
4470×
                            return None;
44816463×
                        }
44916463×
                        w.u8(0);
45016463×
                        w.fixed(&node.0);
45116463×
                        atom(&mut w, after);
45216463×
                        atom(&mut w, before);
453
                    }
4540×
                    Edit::Delete { node, atoms } => {
4550×
                        w.u8(1);
4560×
                        w.fixed(&node.0);
4570×
                        w.count(atoms.len());
4580×
                        for r in atoms {
4590×
                            w.object(&r.change);
4600×
                            w.u32(r.from);
4610×
                            w.u32(r.to);
4620×
                        }
463
                    }
4640×
                    Edit::Replace { .. } => return None,
465
                }
466
            }
467
        }
46816360×
        let texts: Vec<&TextRef> = per_change
46916360×
            .iter()
47016463×
            .flat_map(|(_, _, t)| t.iter().copied())
47116360×
            .collect();
47216360×
        w.count(texts.len());
47316463×
        for t in texts {
47416463×
            match t {
4750×
                TextRef::Blob(r, count, form) => {
4760×
                    w.u8(0);
4770×
                    w.object(r);
4780×
                    w.u32(*count);
4790×
                    w.u8(match form {
4800×
                        crate::objects::TextForm::Cbor => 0,
4810×
                        crate::objects::TextForm::Raw => 1,
4820×
                        crate::objects::TextForm::CborFallback => 2,
483
                    });
484
                }
48516463×
                TextRef::Inline { change, start, end } => {
48616463×
                    w.u8(1);
48716463×
                    w.object(change);
48816463×
                    w.u32(*start);
48916463×
                    w.u32(*end);
49016463×
                }
491
            }
492
        }
49316360×
        Some(w.0)
49416360×
    }
495
496
    /// The state an index holds (see [`State::write_index`]).
497211×
    pub fn read_index(bytes: &[u8]) -> Result<State, ObjectError> {
498211×
        let bad = || ObjectError::Schema("state index: truncated".into());
499211×
        let front_len =
500211×
            usize::try_from(crate::flat::Reader::new(bytes).u64()?).map_err(|_| bad())?;
501211×
        let front = bytes.get(8..8 + front_len).ok_or_else(bad)?;
502211×
        let histories = Histories::Bytes(Arc::new(bytes[8 + front_len..].to_vec()));
503211×
        Self::read_index_front(front, histories)
504211×
    }
505
506
    /// An index's front, everything but the files' histories, with where
507
    /// those are: each is read, and checked against the hash the front
508
    /// holds, only when its graph is built (oxygen#94).
5091844×
    pub fn read_index_front(bytes: &[u8], histories: Histories) -> Result<State, ObjectError> {
510
        use crate::flat::Reader;
5111844×
        let bad = |what: &str| ObjectError::Schema(format!("state index: {what}"));
5121844×
        let mut r = Reader::new(bytes);
5131844×
        let mut st = State::new_history_only();
514
        {
5151844×
            let _t = crate::timing::phase("index: changes");
5161844×
            st.changes = Self::read_index_changes(r.bytes()?)?;
517
        }
5181844×
        let tree_bytes = r.bytes()?;
5191844×
        for _ in 0..r.count()? {
5202×
            let n = NodeId(r.fixed::<16>()?);
5212×
            let mut recs = Vec::new();
5222×
            for _ in 0..r.count()? {
5232×
                recs.push((r.object()?, r.opt_object()?, r.opt_object()?));
524
            }
5252×
            st.replaces.insert(n, recs);
526
        }
5271844×
        let deferred_bytes = r.bytes()?;
5281844×
        if !r.at_end() {
5290×
            return Err(bad("trailing bytes"));
5301844×
        }
5311844×
        let (tree, deferred) = rayon::join(
5321844×
            || TreeGraph::read_flat(&mut Reader::new(tree_bytes)),
5331844×
            || Self::read_deferred(deferred_bytes, &histories),
534
        );
5351844×
        st.tree = tree?;
5361844×
        st.deferred = Some(deferred?);
5371844×
        Ok(st)
5381844×
    }
539
540
    /// The changes of a state index: its first block, which a reader can
541
    /// take without the rest (the log needs no more).
5422004×
    pub fn read_index_changes(
5432004×
        bytes: &[u8],
5442004×
    ) -> Result<crate::fasthash::FastMap<ChangeHash, Arc<ChangeMeta>>, ObjectError> {
545
        use crate::flat::Reader;
5462004×
        let mut r = Reader::new(bytes);
5472004×
        if r.fixed::<18>()?.as_slice() != Self::INDEX_MAGIC {
5480×
            return Err(ObjectError::Schema("state index: not a state index".into()));
5492004×
        }
5502004×
        let n = r.count()?;
5512004×
        let mut changes = crate::fasthash::FastMap::default();
5522004×
        changes.reserve(n);
5532004×
        for _ in 0..n {
554170×
            let h = r.object()?;
555170×
            changes.insert(h, Arc::new(ChangeMeta::read_flat(&mut r)?));
556
        }
5572004×
        if !r.at_end() {
5580×
            return Err(ObjectError::Schema("state index: trailing bytes".into()));
5592004×
        }
5602004×
        Ok(changes)
5612004×
    }
562
563
    /// The deferred block: each node with its graph key and its body,
564
    /// left encoded (see [`Deferred`]).
5651844×
    fn read_deferred(
5661844×
        table: &[u8],
5671844×
        histories: &Histories,
5681844×
    ) -> Result<crate::fasthash::FastMap<NodeId, Arc<Deferred>>, ObjectError> {
569
        use crate::flat::Reader;
5701844×
        let _t = crate::timing::phase("index: deferred");
5711844×
        let mut r = Reader::new(table);
5721844×
        let mut deferred = crate::fasthash::FastMap::default();
5731844×
        let n = r.count()?;
5741844×
        deferred.reserve(n);
5751844×
        for _ in 0..n {
57616694×
            let id = NodeId(r.fixed::<16>()?);
57716694×
            let key = r.fixed::<32>()?;
578
            // The length is of bytes in the histories, not in this table:
579
            // checked against the histories' size below.
58016694×
            let (at, len, sum) = (r.u64()?, r.u32()? as usize, r.fixed::<32>()?);
58116694×
            if at
58216694×
                .checked_add(len as u64)
58316694×
                .is_none_or(|end| end > histories.len())
584
            {
5850×
                return Err(ObjectError::Schema(
5860×
                    "state index: a file's history lies outside it".into(),
5870×
                ));
58816694×
            }
58916694×
            let d = Deferred {
59016694×
                raw: Some(RawHistory {
59116694×
                    from: histories.clone(),
59216694×
                    at,
59316694×
                    len,
59416694×
                    sum,
59516694×
                }),
59616694×
                key: std::sync::OnceLock::from(key),
59716694×
                ..Deferred::default()
59816694×
            };
59916694×
            deferred.insert(id, Arc::new(d));
600
        }
6011844×
        if !r.at_end() {
6020×
            return Err(ObjectError::Schema("state index: trailing bytes".into()));
6031844×
        }
6041844×
        Ok(deferred)
6051844×
    }
606
607
    /// One node's deferred history as the index encodes it.
6085474×
    fn read_deferred_body(bytes: &[u8]) -> Result<DeferredBody, ObjectError> {
609
        use crate::flat::Reader;
6105474×
        let bad = |what: &str| ObjectError::Schema(format!("state index: {what}"));
6115485×
        let node = |r: &mut Reader| -> Result<NodeId, ObjectError> { Ok(NodeId(r.fixed::<16>()?)) };
61210970×
        let atom = |r: &mut Reader| -> Result<AtomRef, ObjectError> {
61310970×
            Ok(match r.u8()? {
6145468×
                0 => AtomRef::Start,
6155485×
                1 => AtomRef::End,
616
                2 => AtomRef::Atom {
61717×
                    change: r.object()?,
61817×
                    index: r.u32()?,
619
                },
6200×
                _ => return Err(bad("atom")),
621
            })
62210970×
        };
6235474×
        let mut r = Reader::new(bytes);
6245474×
        let base = match r.u8()? {
6255468×
            0 => None,
6260×
            1 => Some(DeferredBase::Blob(r.object()?)),
6276×
            _ => Some(DeferredBase::Inline(cbor::decode(r.bytes()?)?)),
628
        };
6295474×
        let mut edits = Vec::new();
6305474×
        for _ in 0..r.count()? {
6315485×
            let c = r.object()?;
6325485×
            let mut es = Vec::new();
6335485×
            for _ in 0..r.count()? {
6345485×
                es.push(match r.u8()? {
635
                    0 => Edit::Insert {
6365485×
                        node: node(&mut r)?,
6375485×
                        after: atom(&mut r)?,
6385485×
                        before: atom(&mut r)?,
6395485×
                        lines: crate::lines::Lines::default(),
640
                    },
641
                    1 => {
6420×
                        let node = node(&mut r)?;
6430×
                        let mut atoms = Vec::new();
6440×
                        for _ in 0..r.count()? {
6450×
                            atoms.push(crate::objects::AtomRange {
6460×
                                change: r.object()?,
6470×
                                from: r.u32()?,
6480×
                                to: r.u32()?,
649
                            });
650
                        }
6510×
                        Edit::Delete { node, atoms }
652
                    }
6530×
                    _ => return Err(bad("edit")),
654
                });
655
            }
6565485×
            edits.push((c, es));
657
        }
6585474×
        let mut texts = Vec::new();
6595474×
        for _ in 0..r.count()? {
6605485×
            texts.push(match r.u8()? {
661
                0 => TextRef::Blob(
6620×
                    r.object()?,
6630×
                    r.u32()?,
6640×
                    match r.u8()? {
6650×
                        0 => crate::objects::TextForm::Cbor,
6660×
                        1 => crate::objects::TextForm::Raw,
6670×
                        _ => crate::objects::TextForm::CborFallback,
668
                    },
669
                ),
670
                _ => TextRef::Inline {
6715485×
                    change: r.object()?,
6725485×
                    start: r.u32()?,
6735485×
                    end: r.u32()?,
674
                },
675
            });
676
        }
6775474×
        if !r.at_end() {
6780×
            return Err(bad("trailing bytes in a file's history"));
6795474×
        }
6805474×
        Ok(DeferredBody { base, edits, texts })
6815474×
    }
682
}
683
684
impl Clone for State {
6851402×
    fn clone(&self) -> State {
6861402×
        State {
6871402×
            changes: self.changes.clone(),
6881402×
            tree: self.tree.clone(),
6891402×
            files: self.files.clone(),
6901402×
            replaces: self.replaces.clone(),
6911402×
            // A cache: the clone builds its own.
6921402×
            closure: Default::default(),
6931402×
            levels: Default::default(),
6941402×
            deferred: self.deferred.clone(),
6951402×
        }
6961402×
    }
697
}
698
699
#[derive(Clone, Debug, PartialEq, Eq)]
700
pub enum FileContent {
701
    Text {
702
        bytes: Vec<u8>,
703
        materialized: Materialized,
704
    },
705
    /// Live blobs; more than one is a replace conflict (spec §9.3).
706
    Opaque {
707
        blobs: Vec<ObjectRef>,
708
    },
709
    Dir,
710
}
711
712
#[derive(Clone, Debug, PartialEq, Eq)]
713
pub struct FileState {
714
    pub node: NodeId,
715
    pub kind: NodeKind,
716
    pub mode: Mode,
717
    pub content: FileContent,
718
}
719
720
impl FileState {
72113562×
    pub fn is_conflicted(&self) -> bool {
72213562×
        match &self.content {
72312638×
            FileContent::Text { materialized, .. } => materialized.is_conflicted(),
724243×
            FileContent::Opaque { blobs } => blobs.len() > 1,
725681×
            FileContent::Dir => false,
726
        }
72713562×
    }
728
}
729
730
/// See `State::conflict_regions`.
731
#[derive(Clone, Debug, Default, PartialEq, Eq)]
732
pub struct ConflictRegions {
733
    pub plain: Vec<(AtomKey, Vec<u8>)>,
734
    pub region_atoms: Vec<AtomKey>,
735
    pub contributors: Vec<ChangeHash>,
736
}
737
738
/// The whole tree of a state, materialized.
739
#[derive(Clone, Debug, Default)]
740
pub struct Snapshot {
741
    pub tree: TreeView,
742
    pub files: BTreeMap<String, FileState>,
743
    /// Text files left unrendered by [`State::materialize_except`]: their
744
    /// `bytes` are empty and they are known not to be conflicted.
745
    pub unrendered: BTreeSet<String>,
746
}
747
748
impl Snapshot {
7491984×
    pub fn is_conflicted(&self) -> bool {
75013243×
        self.tree.is_conflicted() || self.files.values().any(|f| f.is_conflicted())
7511984×
    }
752
}
753
75462272×
fn vertex(r: AtomRef) -> Vertex {
75562272×
    match r {
75626066×
        AtomRef::Start => Vertex::Start,
75727254×
        AtomRef::End => Vertex::End,
7588952×
        AtomRef::Atom { change, index } => Vertex::Atom((change, index)),
759
    }
76062272×
}
761
762
/// Replace self references in a tree op with the concrete change hash.
76338513×
pub fn resolve_tree_op(op: &TreeOp, me: ChangeHash) -> TreeOp {
76438513×
    match op {
765856×
        TreeOp::RemoveNode { node, placement } => TreeOp::RemoveNode {
766856×
            node: *node,
767856×
            placement: placement.resolve(me),
768856×
        },
769
        TreeOp::Move {
770152×
            node,
771152×
            old,
772152×
            parent,
773152×
            name,
774152×
        } => TreeOp::Move {
775152×
            node: *node,
776152×
            old: old.resolve(me),
777152×
            parent: *parent,
778152×
            name: name.clone(),
779152×
        },
78022×
        TreeOp::SetMode { node, old, mode } => TreeOp::SetMode {
78122×
            node: *node,
78222×
            old: old.resolve(me),
78322×
            mode: *mode,
78422×
        },
78537483×
        add @ TreeOp::AddNode { .. } => add.clone(),
786
    }
78738513×
}
788
789
/// Apply one change's edits of one file (inserts and deletes; replaces
790
/// live beside the graphs) to that file's graph.
79125865×
fn apply_file_edits(g: &mut FileGraph, h: ChangeHash, edits: Vec<Edit>) {
792
    // The graph's kept order, if any, follows the edits (see `Order`).
79325865×
    let order = g.take_order();
79425865×
    let mut inserts: Vec<(Vertex, Vertex, u32, u32)> = Vec::new();
79525865×
    let mut deleted: Vec<crate::graph::AtomKey> = Vec::new();
79625865×
    let mut next = 0u32;
79728018×
    for e in edits {
79828018×
        match e {
799
            Edit::Insert {
80026620×
                after,
80126620×
                before,
80226620×
                lines,
803
                ..
80426620×
            } => {
80526620×
                let first = next;
80626620×
                let count = lines.len() as u32;
80726620×
                next += count;
80826620×
                let (after, before) = (vertex(after.resolve(h)), vertex(before.resolve(h)));
80926620×
                inserts.push((after, before, first, count));
81026620×
                g.insert_owned(h, first, after, before, lines);
81126620×
            }
8121398×
            Edit::Delete { atoms, .. } => {
8132491×
                for r in atoms {
8142491×
                    let r = r.resolve(h);
8152588×
                    deleted.extend((r.from..r.to).map(|i| (r.change, i)));
8162491×
                    g.delete(h, &r);
817
                }
818
            }
8190×
            Edit::Replace { .. } => {}
820
        }
821
    }
82225865×
    if let Some(o) = order {
82373×
        g.keep_spliced(o, h, &inserts, &deleted);
82425792×
    }
82525865×
}
826
827
/// Build the graphs of deferred entries: their checkpoint bases and the
828
/// texts of inserts read without their lines are loaded in one call, then
829
/// the graphs are built in parallel (they are independent).
8303671×
fn build_deferred(
8313671×
    taken: Vec<(NodeId, Deferred)>,
8323671×
    load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
8333671×
    read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
8343671×
) -> Result<Vec<(NodeId, FileGraph)>, ObjectError> {
8353671×
    let mut taken: Vec<(NodeId, DeferredBody)> = taken
8363671×
        .into_iter()
83722798×
        .map(|(n, d)| Ok((n, d.into_body()?)))
8383671×
        .collect::<Result<_, ObjectError>>()?;
8393670×
    let mut refs: Vec<ObjectRef> = taken
8403670×
        .iter()
8413670×
        .filter_map(|(_, d)| match &d.base {
8426×
            Some(DeferredBase::Blob(r)) => Some(*r),
84322791×
            _ => None,
84422797×
        })
8453670×
        .collect();
846
    // Text blobs of inserts read without their lines, after the bases.
8473670×
    let bases = refs.len();
84822797×
    refs.extend(taken.iter().flat_map(|(_, d)| {
84925948×
        d.texts.iter().filter_map(|t| match t {
85070×
            TextRef::Blob(r, _, _) => Some(*r),
85125878×
            TextRef::Inline { .. } => None,
85225948×
        })
85322797×
    }));
854
    // And the encodings of changes whose inline inserts are needed,
855
    // each read once.
8563670×
    let reading = crate::timing::phase("graphs: read changes and blobs");
8573670×
    let mut changes: HashMap<ChangeHash, Arc<Vec<u8>>> = HashMap::new();
85825948×
    for t in taken.iter().flat_map(|(_, d)| d.texts.iter()) {
85925948×
        if let TextRef::Inline { change, .. } = t {
86025878×
            if !changes.contains_key(change) {
8615900×
                changes.insert(*change, read_change(change)?);
86219978×
            }
86370×
        }
864
    }
8653670×
    let n_changes = changes.len();
8663670×
    drop(reading);
8673670×
    let reading = crate::timing::phase("graphs:   of which blobs");
8683670×
    let mut blobs = if refs.is_empty() {
8693599×
        Vec::new()
870
    } else {
87171×
        load(&refs)?
872
    };
8733667×
    if crate::timing::enabled() {
8742×
        eprintln!(
8752×
            "timing graphs: {} graphs, {n_changes} changes, {} blobs",
8762×
            taken.len(),
8772×
            refs.len()
8782×
        );
8793665×
    }
8803667×
    if blobs.len() != refs.len() {
8810×
        return Err(ObjectError::Schema("graph blobs: wrong count".into()));
8823667×
    }
8833667×
    drop(reading);
8843667×
    let lines = crate::timing::phase("graphs: hand out blobs");
8853667×
    let mut texts = blobs.split_off(bases).into_iter();
8863667×
    let mut blobs = blobs.into_iter();
887
    // Hand each graph its own blobs, in order, then fill in its inserts'
888
    // lines and build it, all in parallel: graphs are independent.
889
    type Work = (NodeId, DeferredBody, Option<Vec<u8>>, Vec<Vec<u8>>);
8903667×
    let mut work: Vec<Work> = Vec::with_capacity(taken.len());
89122794×
    for (node, d) in taken.drain(..) {
89222794×
        let wanted = d
89322794×
            .texts
89422794×
            .iter()
89525946×
            .filter(|t| matches!(t, TextRef::Blob(..)))
89622794×
            .count();
89722794×
        let mine: Vec<Vec<u8>> = texts.by_ref().take(wanted).collect();
89822794×
        if mine.len() != wanted {
8990×
            return Err(ObjectError::Schema("text blobs: wrong count".into()));
90022794×
        }
90122794×
        let bytes = match &d.base {
902
            Some(DeferredBase::Blob(_)) => Some(
9035×
                blobs
9045×
                    .next()
9055×
                    .ok_or_else(|| ObjectError::Schema("graph blobs: wrong count".into()))?,
906
            ),
90722789×
            _ => None,
908
        };
90922794×
        work.push((node, d, bytes, mine));
910
    }
9113667×
    drop(lines);
9123667×
    let _t = crate::timing::phase("graphs: lines and build");
913
    use rayon::prelude::*;
9143667×
    let changes = &changes;
9153667×
    let built: Vec<(NodeId, FileGraph)> = work
9163667×
        .into_par_iter()
91722794×
        .map(|(node, mut d, bytes, mine)| {
918
            // Fill in the lines of inserts read without them, in order.
91922794×
            let mut want = std::mem::take(&mut d.texts).into_iter();
92022794×
            let mut mine = mine.into_iter();
92125384×
            for (_, edits) in d.edits.iter_mut() {
92227093×
                for e in edits.iter_mut() {
92327093×
                    if let Edit::Insert { lines, .. } = e {
92425953×
                        if lines.is_empty() {
92525946×
                            *lines = match want.next() {
92668×
                                Some(TextRef::Blob(_, count, form)) => {
92768×
                                    let content = mine.next().ok_or_else(|| {
9280×
                                        ObjectError::Schema("text blobs: wrong count".into())
9290×
                                    })?;
93068×
                                    crate::objects::text_lines(&content, count, form)?
931
                                }
93225878×
                                Some(TextRef::Inline { change, start, end }) => {
93325878×
                                    let bytes = &changes[&change];
93425878×
                                    let range = bytes
93525878×
                                        .get(start as usize..end as usize)
93625878×
                                        .ok_or_else(|| {
9370×
                                            ObjectError::Schema(
9380×
                                                "an inline insert lies outside its change".into(),
9390×
                                            )
9400×
                                        })?;
94125878×
                                    crate::objects::inline_lines(range)?
942
                                }
943
                                None => {
9440×
                                    return Err(ObjectError::Schema(
9450×
                                        "an insert's text is missing".into(),
9460×
                                    ))
947
                                }
948
                            };
9497×
                        }
9501140×
                    }
951
                }
952
            }
95322794×
            let mut graph = match (d.base.take(), bytes) {
95496×
                (Some(DeferredBase::Inline(v)), _) => FileGraph::from_value(&v)?,
9555×
                (Some(DeferredBase::Blob(_)), Some(bytes)) => {
9565×
                    FileGraph::from_value(&cbor::decode(&bytes)?)?
957
                }
95822693×
                _ => FileGraph::default(),
959
            };
96025384×
            for (h, edits) in std::mem::take(&mut d.edits) {
96125384×
                apply_file_edits(&mut graph, h, edits);
96225384×
            }
96322794×
            Ok((node, graph))
96422794×
        })
9653667×
        .collect::<Result<_, ObjectError>>()?;
9663667×
    Ok(built)
9673671×
}
968
969
impl State {
970696×
    pub fn new() -> Self {
971696×
        Self::default()
972696×
    }
973
974
    /// An empty state for history only: changes and tree are kept, file
975
    /// edits are recorded but no file graph is built until
976
    /// [`State::load_graphs`] asks for it. For log and other commands that
977
    /// need no file contents, or only a few files.
9783532×
    pub fn new_history_only() -> Self {
9793532×
        State {
9803532×
            deferred: Some(Default::default()),
9813532×
            ..Self::default()
9823532×
        }
9833532×
    }
984
985
    /// Whether `node`'s file graph is built (always, in a full state).
98640×
    pub fn is_built(&self, node: &NodeId) -> bool {
98740×
        self.deferred.is_none() || self.files.contains_key(node)
98840×
    }
989
990
    /// Whether this state builds file graphs only when asked
991
    /// ([`State::new_history_only`]), even if it has none waiting.
9923307×
    pub fn is_history_only(&self) -> bool {
9933307×
        self.deferred.is_some()
9943307×
    }
995
996
    /// Whether this state holds every file graph.
9972729×
    pub fn is_full(&self) -> bool {
9982729×
        self.deferred.as_ref().is_none_or(|d| d.is_empty())
9992729×
    }
1000
1001
    /// The graph of `node`, which must be loaded: asking a history-only
1002
    /// state for a graph it has not built is a bug, never an empty file.
100325092×
    fn graph(&self, node: &NodeId) -> Option<&FileGraph> {
100425092×
        if let Some(d) = &self.deferred {
100510025×
            assert!(
100610025×
                !d.contains_key(node),
1007
                "file graph {} of a history-only state was not loaded (State::load_graphs)",
10081×
                node.short(12)
1009
            );
101015067×
        }
101125091×
        self.files.get(node).map(|g| g.as_ref())
101225091×
    }
1013
1014
    /// Operations over every file need every graph.
10152722×
    fn require_full(&self, what: &str) {
10162722×
        assert!(
10172722×
            self.is_full(),
1018
            "{what} needs every file graph; this state was loaded for history only"
1019
        );
10202721×
    }
1021
1022
    /// Build the graphs of `nodes` in a history-only state: from their
1023
    /// checkpoint base (blobs read in one batch through `load`) and their
1024
    /// deferred edits. Nodes already built, or with no graph, are skipped.
10253594×
    pub fn load_graphs(
10263594×
        &mut self,
10273594×
        nodes: &[NodeId],
10283594×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
10293594×
        read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
10303594×
    ) -> Result<(), ObjectError> {
10313594×
        let Some(deferred) = self.deferred.as_mut() else {
10320×
            return Ok(());
1033
        };
10343594×
        let taken: Vec<(NodeId, Deferred)> = nodes
10353594×
            .iter()
103622952×
            .filter_map(|n| deferred.remove(n).map(|d| (*n, Arc::unwrap_or_clone(d))))
10373594×
            .collect();
10383594×
        let built = build_deferred(taken, load, read_change)?;
10393590×
        self.files
104022733×
            .extend(built.into_iter().map(|(n, g)| (n, Arc::new(g))));
10413590×
        Ok(())
10423594×
    }
1043
1044
    /// The graphs of `nodes`, built from this history-only state's deferred
1045
    /// edits without building them in it: the state stays history only, so
1046
    /// changes read without their lines can still be added to it and every
1047
    /// graph keeps its key. For a caller that renders a few files of many
1048
    /// states in turn (verifying an import).
104977×
    pub fn build_graphs_detached(
105077×
        &self,
105177×
        nodes: &[NodeId],
105277×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
105377×
        read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
105477×
    ) -> Result<Vec<(NodeId, FileGraph)>, ObjectError> {
105577×
        let Some(deferred) = self.deferred.as_ref() else {
10560×
            return Ok(Vec::new());
1057
        };
1058
        // A file this state has built has no deferred history: its graph
1059
        // is the built one, copied. Built from an empty history instead, it
1060
        // rendered as an empty file, and a merge taking this path recorded
1061
        // the whole file as inserted again, conflicting with itself (#71).
106277×
        let mut built: Vec<(NodeId, FileGraph)> = Vec::new();
106377×
        let mut copies: Vec<(NodeId, Deferred)> = Vec::new();
106477×
        for n in nodes {
106564×
            match (self.files.get(n), deferred.get(n)) {
10663×
                (Some(g), _) => built.push((*n, (**g).clone())),
106761×
                (None, d) => copies.push((*n, d.map(|d| (**d).clone()).unwrap_or_default())),
1068
            }
1069
        }
107077×
        let mut out = build_deferred(copies, load, read_change)?;
107177×
        out.extend(built);
107277×
        Ok(out)
107377×
    }
1074
1075
    /// Render a graph built for this state (see
1076
    /// [`State::build_graphs_detached`]), as [`State::render_file`] does.
107764×
    pub fn render_graph(&self, g: &FileGraph) -> (Vec<u8>, Materialized) {
107864×
        let m = g.materialize(self);
107964×
        let bytes = g.render(&m, &|h| self.label(h));
108064×
        (bytes, m)
108164×
    }
1082
1083
    /// Every file graph a history-only state has not built, deleted files'
1084
    /// included.
108577×
    pub fn unbuilt_nodes(&self) -> Vec<NodeId> {
108677×
        self.deferred
108777×
            .as_ref()
108877×
            .map(|d| d.keys().copied().collect())
108977×
            .unwrap_or_default()
109077×
    }
1091
1092
    /// Drop the built graphs of `nodes`, for a caller rendering a large
1093
    /// tree a batch at a time. The state can no longer render those files,
1094
    /// so it must not be used for them again.
1095430×
    pub fn drop_graphs(&mut self, nodes: &[NodeId]) {
10966009×
        for n in nodes {
10976009×
            self.files.remove(n);
10986009×
        }
1099430×
    }
1100
1101
    /// Build every graph a history-only state has not built, making it full.
11021802×
    pub fn load_all_graphs(
11031802×
        &mut self,
11041802×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
11051802×
        read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
11061802×
    ) -> Result<(), ObjectError> {
11071802×
        let nodes: Vec<NodeId> = match &self.deferred {
11081802×
            Some(d) => d.keys().copied().collect(),
11090×
            None => return Ok(()),
1110
        };
11111802×
        self.load_graphs(&nodes, load, read_change)?;
11121802×
        self.deferred = None;
11131802×
        Ok(())
11141802×
    }
1115
1116
    /// The one text a file's history is, when it is a single insert of
1117
    /// the whole file into nothing (a file added once and never edited,
1118
    /// with no checkpoint base): its rendering is that text, so it needs
1119
    /// no graph. `None` otherwise, or when the graph is built.
11205862×
    pub fn single_text(&self, node: &NodeId) -> Option<TextRef> {
11215862×
        if self.files.contains_key(node) {
11220×
            return None;
11235862×
        }
1124
        // A history that cannot be read is no single text: building its
1125
        // graph reports why.
11265862×
        let d = self.deferred.as_ref()?.get(node)?.body().ok()?;
11275814×
        let [(_, edits)] = d.edits.as_slice() else {
1128263×
            return None;
1129
        };
1130
        let [Edit::Insert {
1131
            after: AtomRef::Start,
1132
            before: AtomRef::End,
11335551×
            lines,
1134
            ..
11355551×
        }] = edits.as_slice()
1136
        else {
11370×
            return None;
1138
        };
11395551×
        match (&d.base, d.texts.as_slice()) {
11405551×
            (None, [t]) if lines.is_empty() => Some(*t),
11410×
            _ => None,
1142
        }
11435862×
    }
1144
1145
    /// A key for the graph of `node` in a history-only state that has not
1146
    /// built it: equal keys mean equal graphs, so equal renderings (a
1147
    /// rendering depends only on the graph and on the changes' own
1148
    /// immutable metadata). It names the node, its checkpoint base (a blob,
1149
    /// an inline value, or none) and the set of later changes that edited
1150
    /// the file.
1151
    /// `None` for a full state or a graph already built.
115228636×
    pub fn graph_key(&self, node: &NodeId) -> Option<[u8; 32]> {
115328636×
        let deferred = self.deferred.as_ref()?;
115428599×
        if self.files.contains_key(node) {
1155440×
            return None;
115628159×
        }
115728159×
        let mut h = blake3::Hasher::new();
115828159×
        h.update(b"oxy graph key 1\0");
1159
        // A change's edits are applied per node: the node is part of it.
116028159×
        h.update(&node.0);
116128159×
        let Some(d) = deferred.get(node) else {
1162
            // Never edited: the empty graph.
1163168×
            h.update(&[0]);
1164168×
            return Some(*h.finalize().as_bytes());
1165
        };
1166
        // A history that cannot be read has no key: the file is read.
116727991×
        if let Some(k) = d.key.get() {
116822202×
            return Some(*k);
11695789×
        }
11705789×
        let k = d.graph_key(h).ok()?;
11715789×
        Some(*d.key.get_or_init(|| k))
117228636×
    }
1173
11743458×
    pub fn len(&self) -> usize {
11753458×
        self.changes.len()
11763458×
    }
1177
1178
    /// Turn this history-only state's built graphs back into deferred
1179
    /// histories, each starting from its graph encoded as a checkpoint
1180
    /// graph and stored by `store` (it returns the blob's reference): what
1181
    /// they held stays, and memory goes back to what a history-only state
1182
    /// costs. A graph built later again decodes that blob and applies only
1183
    /// the edits that came after. Graph keys change (the base is now a
1184
    /// blob), which only means a cached rendering is not reused.
11851×
    pub fn shed_graphs<E>(
11861×
        &mut self,
11871×
        mut store: impl FnMut(&[u8]) -> Result<ObjectRef, E>,
11881×
    ) -> Result<usize, E> {
1189
        use rayon::prelude::*;
11901×
        let Some(deferred) = self.deferred.as_mut() else {
11910×
            return Ok(0);
1192
        };
11931×
        let graphs: Vec<(NodeId, Arc<FileGraph>)> = self.files.drain().collect();
11941×
        let encoded: Vec<(NodeId, Vec<u8>)> = graphs
11951×
            .par_iter()
11962×
            .map(|(n, g)| (*n, cbor::encode(&g.to_value())))
11971×
            .collect();
11981×
        drop(graphs);
11991×
        let shed = encoded.len();
12002×
        for (node, bytes) in encoded {
12012×
            let blob = store(&bytes)?;
12022×
            deferred.insert(
12032×
                node,
12042×
                Arc::new(Deferred::from_body(DeferredBody {
12052×
                    base: Some(DeferredBase::Blob(blob)),
12062×
                    ..DeferredBody::default()
12072×
                })),
1208
            );
1209
        }
12101×
        Ok(shed)
12111×
    }
1212
1213
    /// How many file graphs this state has built.
121477×
    pub fn built_count(&self) -> usize {
121577×
        self.files.len()
121677×
    }
1217
1218
    /// Atoms in this state's built graphs, live and deleted: what their
1219
    /// memory follows, since a graph keeps every line its file ever had.
122051×
    pub fn built_atoms(&self) -> usize {
122156×
        self.files.values().map(|g| g.atom_count()).sum()
122251×
    }
1223
1224
    /// Edits held in the deferred histories of files not built, and the
1225
    /// most any one file holds (for measuring, #25).
12260×
    pub fn deferred_edit_count(&self) -> (usize, usize) {
12270×
        let Some(d) = self.deferred.as_ref() else {
12280×
            return (0, 0);
1229
        };
12300×
        let mut total = 0;
12310×
        let mut most = 0;
12320×
        for v in d.values() {
12330×
            let n: usize = v
12340×
                .body()
12350×
                .map_or(0, |b| b.edits.iter().map(|(_, e)| e.len()).sum());
12360×
            total += n;
12370×
            most = most.max(n);
1238
        }
12390×
        (total, most)
12400×
    }
1241
}
1242
1243
impl Deferred {
1244
    /// See [`State::graph_key`]: `h` has taken the node already.
124522149×
    fn graph_key(&self, mut h: blake3::Hasher) -> Result<[u8; 32], ObjectError> {
124622149×
        let d = self.body()?;
124713×
        match &d.base {
124822136×
            None => h.update(&[0]),
12490×
            Some(DeferredBase::Blob(r)) => h.update(&[1]).update(&r.0),
125013×
            Some(DeferredBase::Inline(v)) => h.update(&[2]).update(&cbor::encode(v)),
1251
        };
125222149×
        let mut changes: Vec<&ChangeHash> = d.edits.iter().map(|(c, _)| c).collect();
125322149×
        changes.sort();
125422149×
        changes.dedup();
125522149×
        h.update(&(changes.len() as u64).to_le_bytes());
125623647×
        for c in changes {
125723647×
            h.update(&c.0);
125823647×
        }
125922149×
        Ok(*h.finalize().as_bytes())
126022149×
    }
1261
}
1262
1263
impl State {
12640×
    pub fn is_empty(&self) -> bool {
12650×
        self.changes.is_empty()
12660×
    }
126713973×
    pub fn contains(&self, h: &ChangeHash) -> bool {
126813973×
        self.changes.contains_key(h)
126913973×
    }
127064×
    pub fn get(&self, h: &ChangeHash) -> Option<&ChangeMeta> {
127164×
        self.changes.get(h).map(|m| &**m)
127264×
    }
12731395×
    pub fn changes(&self) -> impl Iterator<Item = (&ChangeHash, &ChangeMeta)> {
12743444×
        self.changes.iter().map(|(h, m)| (h, &**m))
12751395×
    }
127617698×
    pub fn tree(&self) -> &TreeGraph {
127717698×
        &self.tree
127817698×
    }
1279591×
    pub fn file(&self, node: &NodeId) -> Option<&FileGraph> {
1280591×
        self.graph(node)
1281591×
    }
1282
1283
    /// Add a change whose dependencies are already present.
12842872×
    pub fn add(&mut self, change: Change) -> Result<ChangeHash, StateError> {
12852872×
        let h = change.hash();
12862872×
        self.add_hashed(h, change)
12872872×
    }
1288
1289
    /// [`State::add`] for a change whose hash `h` is already known (it was
1290
    /// just stored), so it is not encoded again.
12913530×
    pub fn add_hashed(&mut self, h: ChangeHash, change: Change) -> Result<ChangeHash, StateError> {
12923530×
        if self.changes.contains_key(&h) {
129338×
            return Ok(h);
12943492×
        }
12955027×
        for d in &change.deps {
12965027×
            if !self.changes.contains_key(d) {
12972×
                return Err(StateError::MissingDep(*d, h));
12985025×
            }
1299
        }
13003490×
        self.add_known(h, change, Vec::new());
13013490×
        Ok(h)
13023530×
    }
1303
1304
    /// Add a change in any order; call `validate` once all are in.
13051017×
    pub fn add_unchecked(&mut self, change: Change) -> ChangeHash {
13061017×
        let h = change.hash();
13071017×
        self.add_known(h, change, Vec::new());
13081017×
        h
13091017×
    }
1310
1311
    /// Add a change just stored under hash `h` to a history-only state that
1312
    /// has built some graphs: an edit of a built graph is applied whole
1313
    /// (from `full`), and one of a file not built waits without its text
1314
    /// (from `skeleton` and its `texts`, as the store reads it back), so the
1315
    /// state holds no text it has not built. A Git import adds each change
1316
    /// it makes this way, keeping memory to what changes.
131792×
    pub fn add_known_mixed(
131892×
        &mut self,
131992×
        h: ChangeHash,
132092×
        full: Change,
132192×
        skeleton: Change,
132292×
        texts: Vec<TextRef>,
132392×
    ) {
132492×
        if self.deferred.is_none() || self.changes.contains_key(&h) {
13250×
            self.add_known(h, full, Vec::new());
13260×
            return;
132792×
        }
1328
        let Change {
132992×
            version,
133092×
            change_id,
133192×
            deps,
133292×
            author,
133392×
            hlc,
133492×
            message,
133592×
            tree_ops,
133692×
            edits: full_edits,
133792×
            kind,
133892×
        } = full;
133992×
        let mut texts = texts.into_iter();
134092×
        let mut edits = Vec::with_capacity(full_edits.len());
134192×
        let mut kept = Vec::new();
134297×
        for (f, s) in full_edits.into_iter().zip(skeleton.edits) {
134397×
            let text = match &s {
134477×
                Edit::Insert { lines, .. } if lines.is_empty() => texts.next(),
134520×
                _ => None,
1346
            };
134797×
            if self.files.contains_key(&f.node()) {
134845×
                edits.push(f);
134952×
            } else {
135052×
                edits.push(s);
135152×
                kept.extend(text);
135252×
            }
1353
        }
135492×
        let c = Change {
135592×
            version,
135692×
            change_id,
135792×
            deps,
135892×
            author,
135992×
            hlc,
136092×
            message,
136192×
            tree_ops,
136292×
            edits,
136392×
            kind,
136492×
        };
136592×
        self.add_known(h, c, kept);
136692×
    }
1367
1368
    /// Add a change read from the store under hash `h`, without hashing it
1369
    /// again (for a version 2 change that means chunking all its text).
1370
    /// `texts` names, in order, the inserts left without their lines (see
1371
    /// [`crate::store::Store::get_change_lazy`]); only a history-only state
1372
    /// takes them, and only for graphs it has not built.
137312391×
    pub fn add_known(&mut self, h: ChangeHash, change: Change, texts: Vec<TextRef>) {
137412391×
        if self.changes.contains_key(&h) {
13750×
            return;
137612391×
        }
137712391×
        let meta = ChangeMeta::of(&change);
137812391×
        self.apply_with(h, change, texts);
137912391×
        self.changes.insert(h, Arc::new(meta));
138012391×
        {
138112391×
            let mut m = self.closure.write().unwrap();
138212391×
            m.sets.clear();
138312391×
            m.held = 0;
138412391×
        }
138512391×
    }
1386
1387
    // ------------------------------------------------------------ checkpoints (spec §11)
1388
1389
    /// Serialize the whole state in payload version 1: change metadata,
1390
    /// tree, file graphs, replaces, all in one value (spec §11).
1391380×
    pub fn checkpoint_payload(&self) -> Vec<u8> {
1392380×
        self.require_full("a checkpoint");
1393380×
        let mut nodes: Vec<&NodeId> = self.files.keys().collect();
1394380×
        nodes.sort();
1395380×
        let files = Value::Array(
1396380×
            nodes
1397380×
                .into_iter()
13981473×
                .map(|n| Value::Array(vec![Value::Bytes(n.0.to_vec()), self.files[n].to_value()]))
1399380×
                .collect(),
1400
        );
1401380×
        cbor::encode(&self.payload_value(o::FORMAT_VERSION, files))
1402380×
    }
1403
1404
    /// Each file graph encoded on its own, by node, for a version 2 payload
1405
    /// (spec §11): the graphs of large files become blobs of their own.
140618×
    pub fn checkpoint_file_graphs(&self) -> Vec<(NodeId, Vec<u8>)> {
1407
        use rayon::prelude::*;
140818×
        self.require_full("a checkpoint");
140918×
        let mut nodes: Vec<(&NodeId, &FileGraph)> =
141053×
            self.files.iter().map(|(n, g)| (n, g.as_ref())).collect();
141118×
        nodes.sort_by_key(|(n, _)| **n);
141218×
        nodes
141318×
            .into_par_iter()
141453×
            .map(|(n, g)| (*n, cbor::encode(&g.to_value())))
141518×
            .collect()
141618×
    }
1417
1418
    /// The payload version 2 index for the graphs of
1419
    /// [`State::checkpoint_file_graphs`]: a graph encoded in fewer than
1420
    /// [`INLINE_GRAPH_LIMIT`] bytes is kept inline, a larger one is named by
1421
    /// `blob_of` (the ObjectRef of the blob holding its encoding).
142218×
    pub fn checkpoint_index(
142318×
        &self,
142418×
        graphs: &[(NodeId, Vec<u8>)],
142518×
        mut blob_of: impl FnMut(&[u8]) -> ObjectRef,
142618×
    ) -> Vec<u8> {
142718×
        let files = Value::Array(
142818×
            graphs
142918×
                .iter()
143053×
                .map(|(n, enc)| {
143153×
                    let entry = if enc.len() < INLINE_GRAPH_LIMIT {
143246×
                        cbor::decode(enc).expect("an encoded graph decodes")
1433
                    } else {
14347×
                        Value::Bytes(blob_of(enc).0.to_vec())
1435
                    };
143653×
                    Value::Array(vec![Value::Bytes(n.0.to_vec()), entry])
143753×
                })
143818×
                .collect(),
1439
        );
144018×
        cbor::encode(&self.payload_value(CHECKPOINT_INDEX_VERSION, files))
144118×
    }
1442
1443
    /// The payload map around `files`: changes, tree and replaces as in version 1.
1444398×
    fn payload_value(&self, version: u64, files: Value) -> Value {
1445398×
        let mut hashes: Vec<&ChangeHash> = self.changes.keys().collect();
1446398×
        hashes.sort();
1447398×
        let changes = Value::Array(
1448398×
            hashes
1449398×
                .into_iter()
14502308×
                .map(|h| {
14512308×
                    let m = &self.changes[h];
14522308×
                    Value::Array(vec![
14532308×
                        Value::Bytes(h.0.to_vec()),
14542308×
                        Value::Bytes(m.change_id.0.to_vec()),
14552308×
                        Value::Array(m.deps.iter().map(|d| Value::Bytes(d.0.to_vec())).collect()),
14562308×
                        Value::Text(m.message.clone()),
14572308×
                        Value::Uint(m.hlc.wall_ms),
14582308×
                        Value::Uint(m.hlc.logical as u64),
14592308×
                        Value::Uint(m.kind.tag()),
14602308×
                        Value::Bytes(m.author.0.to_vec()),
1461
                    ])
14622308×
                })
1463398×
                .collect(),
1464
        );
1465398×
        let mut rnodes: Vec<&NodeId> = self.replaces.keys().collect();
1466398×
        rnodes.sort();
1467398×
        let opt =
146810×
            |o: &Option<ObjectRef>| o.map(|r| Value::Bytes(r.0.to_vec())).unwrap_or(Value::Null);
1469398×
        let replaces = Value::Array(
1470398×
            rnodes
1471398×
                .into_iter()
1472398×
                .map(|n| {
14735×
                    Value::Array(vec![
14745×
                        Value::Bytes(n.0.to_vec()),
1475
                        Value::Array(
14765×
                            self.replaces[n]
14775×
                                .iter()
14785×
                                .map(|(c, o, nw)| {
14795×
                                    Value::Array(vec![Value::Bytes(c.0.to_vec()), opt(o), opt(nw)])
14805×
                                })
14815×
                                .collect(),
1482
                        ),
1483
                    ])
14845×
                })
1485398×
                .collect(),
1486
        );
1487398×
        Value::map([
1488398×
            (0, Value::Uint(version)),
1489398×
            (1, changes),
1490398×
            (2, self.tree.to_value()),
1491398×
            (3, files),
1492398×
            (4, replaces),
1493398×
        ])
1494398×
    }
1495
1496
    /// Decode a version 1 payload (the whole state in one value). A
1497
    /// version 2 payload needs its graph blobs: use [`State::from_checkpoint`].
149837×
    pub fn from_checkpoint_payload(bytes: &[u8]) -> Result<State, ObjectError> {
149937×
        State::from_checkpoint(bytes, |_| {
15001×
            Err(ObjectError::Schema(
15011×
                "a version 2 checkpoint needs its graph blobs".into(),
15021×
            ))
15031×
        })
150437×
    }
1505
1506
    /// Decode a checkpoint payload of either version (spec §11). For
1507
    /// version 2, `load` returns the contents of the named graph blobs, in
1508
    /// order; the graphs are then decoded in parallel.
150960×
    pub fn from_checkpoint(
151060×
        bytes: &[u8],
151160×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
151260×
    ) -> Result<State, ObjectError> {
1513
        use rayon::prelude::*;
151460×
        let v = cbor::decode(bytes)?;
151559×
        let m = o::map(&v, "checkpoint payload")?;
151659×
        let version = o::uint(o::field(m, 0, "version")?, "version")?;
151759×
        if version != o::FORMAT_VERSION && version != CHECKPOINT_INDEX_VERSION {
15181×
            return Err(ObjectError::Schema(format!(
15191×
                "checkpoint payload version {version} is not supported"
15201×
            )));
152158×
        }
152258×
        let mut st = State::payload_common(m)?;
152358×
        let entries = o::array(o::field(m, 3, "files")?, "files")?;
152458×
        let mut parsed: Vec<(NodeId, &Value)> = Vec::with_capacity(entries.len());
1525230×
        for f in entries {
1526230×
            let e = o::array(f, "file")?;
1527230×
            if e.len() != 2 {
15280×
                return Err(ObjectError::Schema("file: expected [node, graph]".into()));
1529230×
            }
1530230×
            let node = NodeId::from_slice(o::bytes(&e[0], "node")?)
1531230×
                .ok_or_else(|| ObjectError::Schema("node: expected 16 bytes".into()))?;
1532230×
            parsed.push((node, &e[1]));
1533
        }
1534
        // Version 2 names large graphs by blob: fetch those in one batch.
153558×
        let mut fetched: HashMap<NodeId, Vec<u8>> = HashMap::new();
153658×
        if version == CHECKPOINT_INDEX_VERSION {
153720×
            let wanted: Vec<(NodeId, ObjectRef)> = parsed
153820×
                .iter()
153990×
                .filter(|(_, e)| matches!(e, Value::Bytes(_)))
154020×
                .map(|(n, e)| Ok((*n, o::oref(e, "graph blob")?)))
154120×
                .collect::<Result<_, ObjectError>>()?;
154220×
            if !wanted.is_empty() {
15437×
                let refs: Vec<ObjectRef> = wanted.iter().map(|(_, r)| *r).collect();
15443×
                let contents = {
15457×
                    let _t = crate::timing::phase("load:   graph blobs read");
15467×
                    load(&refs)?
1547
                };
15483×
                if contents.len() != refs.len() {
15490×
                    return Err(ObjectError::Schema("graph blobs: wrong count".into()));
15503×
                }
15513×
                fetched.extend(wanted.iter().map(|(n, _)| *n).zip(contents));
155213×
            }
1553140×
        } else if parsed.iter().any(|(_, e)| matches!(e, Value::Bytes(_))) {
15540×
            return Err(ObjectError::Schema(
15550×
                "a version 1 payload holds graphs inline".into(),
15560×
            ));
155738×
        }
1558
        // File graphs are independent; decode and build them in parallel.
155954×
        let graphs = parsed
156054×
            .par_iter()
1561224×
            .map(|(node, entry)| {
1562224×
                let graph = match fetched.get(node) {
15633×
                    Some(bytes) => FileGraph::from_value(&cbor::decode(bytes)?)?,
1564221×
                    None => FileGraph::from_value(entry)?,
1565
                };
1566224×
                Ok((*node, graph))
1567224×
            })
156854×
            .collect::<Result<Vec<_>, ObjectError>>()?;
156954×
        st.files
1570224×
            .extend(graphs.into_iter().map(|(n, g)| (n, Arc::new(g))));
157154×
        Ok(st)
157260×
    }
1573
1574
    /// The graph blobs a payload names: empty for version 1.
15759×
    pub fn checkpoint_graph_refs(bytes: &[u8]) -> Result<Vec<ObjectRef>, ObjectError> {
15769×
        let v = cbor::decode(bytes)?;
15779×
        let m = o::map(&v, "checkpoint payload")?;
15789×
        let mut out = Vec::new();
157927×
        for f in o::array(o::field(m, 3, "files")?, "files")? {
158027×
            if let Some(entry @ Value::Bytes(_)) = o::array(f, "file")?.get(1) {
15816×
                out.push(o::oref(entry, "graph blob")?);
158221×
            }
1583
        }
15849×
        Ok(out)
15859×
    }
1586
1587
    /// Decode a checkpoint payload for history only: a version 2 payload's
1588
    /// graphs are noted, not read or built, and come later through
1589
    /// [`State::load_graphs`]. A version 1 payload holds them inline and is
1590
    /// decoded in full.
159142×
    pub fn from_checkpoint_history(bytes: &[u8]) -> Result<State, ObjectError> {
159242×
        let v = cbor::decode(bytes)?;
159342×
        let m = o::map(&v, "checkpoint payload")?;
159442×
        let version = o::uint(o::field(m, 0, "version")?, "version")?;
159542×
        if version != CHECKPOINT_INDEX_VERSION {
15961×
            return State::from_checkpoint_payload(bytes);
159741×
        }
159841×
        let mut st = State::payload_common(m)?;
159941×
        let mut deferred = crate::fasthash::FastMap::default();
1600139×
        for f in o::array(o::field(m, 3, "files")?, "files")? {
1601139×
            let e = o::array(f, "file")?;
1602139×
            if e.len() != 2 {
16030×
                return Err(ObjectError::Schema("file: expected [node, graph]".into()));
1604139×
            }
1605139×
            let node = NodeId::from_slice(o::bytes(&e[0], "node")?)
1606139×
                .ok_or_else(|| ObjectError::Schema("node: expected 16 bytes".into()))?;
1607139×
            let base = match &e[1] {
16084×
                Value::Bytes(_) => DeferredBase::Blob(o::oref(&e[1], "graph blob")?),
1609135×
                other => DeferredBase::Inline(other.clone()),
1610
            };
1611139×
            deferred.insert(
1612139×
                node,
1613139×
                Arc::new(Deferred::from_body(DeferredBody {
1614139×
                    base: Some(base),
1615139×
                    ..DeferredBody::default()
1616139×
                })),
1617
            );
1618
        }
161941×
        st.deferred = Some(deferred);
162041×
        Ok(st)
162142×
    }
1622
1623
    /// Changes, tree and replaces of a payload of either version.
162499×
    fn payload_common(m: &BTreeMap<u64, Value>) -> Result<State, ObjectError> {
162599×
        let mut st = State::new();
1626507×
        for c in o::array(o::field(m, 1, "changes")?, "changes")? {
1627507×
            let e = o::array(c, "change meta")?;
1628507×
            if e.len() != 8 {
16290×
                return Err(ObjectError::Schema("change meta: expected 8 fields".into()));
1630507×
            }
1631507×
            let hash = o::oref(&e[0], "hash")?;
1632507×
            let meta = ChangeMeta {
1633507×
                change_id: crate::ids::ChangeId::from_slice(o::bytes(&e[1], "change id")?)
1634507×
                    .ok_or_else(|| ObjectError::Schema("change id: expected 16 bytes".into()))?,
1635507×
                deps: o::array(&e[2], "deps")?
1636507×
                    .iter()
1637626×
                    .map(|d| o::oref(d, "dep"))
1638507×
                    .collect::<Result<Vec<_>, _>>()?,
1639507×
                message: o::text(&e[3], "message")?.to_owned(),
1640
                hlc: Hlc {
1641507×
                    wall_ms: o::uint(&e[4], "wall")?,
1642507×
                    logical: o::u32_(&e[5], "logical")?,
1643
                },
1644507×
                kind: ChangeKind::from_tag(o::uint(&e[6], "kind")?)?,
1645507×
                author: o::oref(&e[7], "author")?,
1646
            };
1647507×
            st.changes.insert(hash, Arc::new(meta));
1648
        }
164999×
        st.tree = TreeGraph::from_value(o::field(m, 2, "tree")?)?;
165099×
        let opt = |v: &Value| -> Result<Option<ObjectRef>, ObjectError> {
165124×
            if v.is_null() {
165212×
                Ok(None)
1653
            } else {
165412×
                o::oref(v, "blob").map(Some)
1655
            }
165624×
        };
165799×
        for r in o::array(o::field(m, 4, "replaces")?, "replaces")? {
165812×
            let e = o::array(r, "replace")?;
165912×
            if e.len() != 2 {
16600×
                return Err(ObjectError::Schema(
16610×
                    "replace: expected [node, records]".into(),
16620×
                ));
166312×
            }
166412×
            let node = NodeId::from_slice(o::bytes(&e[0], "node")?)
166512×
                .ok_or_else(|| ObjectError::Schema("node: expected 16 bytes".into()))?;
166612×
            let mut recs = Vec::new();
166712×
            for rec in o::array(&e[1], "records")? {
166812×
                let x = o::array(rec, "record")?;
166912×
                if x.len() != 3 {
16700×
                    return Err(ObjectError::Schema("record: expected 3 fields".into()));
167112×
                }
167212×
                recs.push((o::oref(&x[0], "change")?, opt(&x[1])?, opt(&x[2])?));
1673
            }
167412×
            st.replaces.insert(node, recs);
1675
        }
167699×
        Ok(st)
167799×
    }
1678
16793478×
    pub fn validate(&self) -> Result<(), StateError> {
16808972×
        for (h, c) in &self.changes {
16818972×
            for d in &c.deps {
16825523×
                if !self.changes.contains_key(d) {
16830×
                    return Err(StateError::MissingDep(*d, *h));
16845523×
                }
1685
            }
1686
        }
16873478×
        Ok(())
16883478×
    }
1689
169012391×
    fn apply_with(&mut self, h: ChangeHash, c: Change, texts: Vec<TextRef>) {
169138513×
        for (i, op) in c.tree_ops.iter().enumerate() {
169238513×
            self.tree.apply(h, i as u32, &resolve_tree_op(op, h));
169338513×
        }
169412391×
        if let Some(deferred) = self.deferred.as_mut() {
1695
            // History only: edits of files not built wait, grouped per file,
1696
            // with the texts of inserts read without their lines.
16977226×
            let mut texts = texts.into_iter();
16987226×
            let mut per_node: HashMap<NodeId, (Vec<Edit>, Vec<TextRef>)> = HashMap::new();
169938659×
            for e in c.edits {
170038659×
                match e {
1701614×
                    Edit::Replace { node, old, new } => {
1702614×
                        self.replaces.entry(node).or_default().push((h, old, new))
1703
                    }
170438045×
                    e => {
170538045×
                        let slot = per_node.entry(e.node()).or_default();
170636229×
                        if matches!(&e, Edit::Insert { lines, .. } if lines.is_empty()) {
170732626×
                            slot.1.extend(texts.next());
170832626×
                        }
170938045×
                        slot.0.push(e);
1710
                    }
1711
                }
1712
            }
171335987×
            for (node, (edits, node_texts)) in per_node {
171435987×
                match self.files.get_mut(&node) {
1715481×
                    Some(g) => {
1716481×
                        assert!(
1717481×
                            node_texts.is_empty(),
1718
                            "a change read without its text applied to a built graph"
1719
                        );
1720481×
                        apply_file_edits(Arc::make_mut(g), h, edits)
1721
                    }
172235506×
                    None => {
172335506×
                        let d = Arc::make_mut(deferred.entry(node).or_default());
172435506×
                        d.add(h, edits, node_texts);
172535506×
                        d.key = Default::default();
172635506×
                    }
1727
                }
1728
            }
17297226×
            return;
17305165×
        }
17315165×
        assert!(
17325165×
            texts.is_empty(),
1733
            "a change read without its text applied to a full state"
1734
        );
17355165×
        let mut counters: HashMap<NodeId, u32> = HashMap::new();
17366179×
        for e in c.edits {
17376179×
            match e {
1738
                Edit::Insert {
17394516×
                    node,
17404516×
                    after,
17414516×
                    before,
17424516×
                    lines,
17434516×
                } => {
17444516×
                    let idx = counters.entry(node).or_default();
17454516×
                    let first = *idx;
17464516×
                    *idx += lines.len() as u32;
17474516×
                    Arc::make_mut(self.files.entry(node).or_default()).insert_owned(
17484516×
                        h,
17494516×
                        first,
17504516×
                        vertex(after.resolve(h)),
17514516×
                        vertex(before.resolve(h)),
17524516×
                        lines,
17534516×
                    );
17544516×
                }
17551550×
                Edit::Delete { node, atoms } => {
17561550×
                    let g = Arc::make_mut(self.files.entry(node).or_default());
17571929×
                    for r in atoms {
17581929×
                        g.delete(h, &r.resolve(h));
17591929×
                    }
1760
                }
1761113×
                Edit::Replace { node, old, new } => {
1762113×
                    self.replaces.entry(node).or_default().push((h, old, new));
1763113×
                }
1764
            }
1765
        }
176612391×
    }
1767
1768
    /// Transitive dependencies of `h`, excluding `h` itself.
17699029×
    pub fn closure(&self, h: &ChangeHash) -> Arc<HashSet<ChangeHash>> {
17709029×
        if let Some(c) = self.closure.read().unwrap().sets.get(h) {
17717650×
            return c.clone();
17721379×
        }
17731379×
        let mut out = HashSet::new();
17741379×
        let mut stack: Vec<ChangeHash> = self
17751379×
            .changes
17761379×
            .get(h)
17771379×
            .map(|c| c.deps.clone())
17781379×
            .unwrap_or_default();
177916761×
        while let Some(d) = stack.pop() {
178015382×
            if out.insert(d) {
178110881×
                if let Some(c) = self.changes.get(&d) {
178210881×
                    stack.extend(c.deps.iter().copied());
178310881×
                }
17844501×
            }
1785
        }
17861379×
        let rc = Arc::new(out);
17871379×
        let mut memo = self.closure.write().unwrap();
17881379×
        if memo.held + rc.len() > CLOSURE_MEMO_HASHES {
17890×
            memo.sets.clear();
17900×
            memo.held = 0;
17911379×
        }
17921379×
        memo.held += rc.len();
17931379×
        memo.sets.insert(*h, rc.clone());
17941379×
        rc
17959029×
    }
1796
1797
    /// `h`'s level (see `levels`), worked out for it and its ancestors not
1798
    /// yet known, without recursion: histories are deeper than a stack. An
1799
    /// unknown change has level 0.
1800401216×
    fn level(&self, h: &ChangeHash) -> u32 {
1801
        // Nearly always known: read without excluding the other threads
1802
        // rendering beside this one (oxygen#96).
1803401216×
        if let Some(l) = self.levels.read().unwrap().get(h) {
1804398845×
            return *l;
18052371×
        }
18062371×
        let mut levels = self.levels.write().unwrap();
18072371×
        if let Some(l) = levels.get(h) {
180824×
            return *l;
18092347×
        }
18102347×
        let mut stack: Vec<(ChangeHash, bool)> = vec![(*h, false)];
18117900×
        while let Some((c, expanded)) = stack.pop() {
18125553×
            if levels.contains_key(&c) {
181343×
                continue;
18145510×
            }
18155510×
            let deps: &[ChangeHash] = self
18165510×
                .changes
18175510×
                .get(&c)
18185510×
                .map(|m| m.deps.as_slice())
18195510×
                .unwrap_or(&[]);
18205510×
            if expanded {
18212755×
                let l = deps
18222755×
                    .iter()
18233661×
                    .map(|d| levels.get(d).map_or(0, |l| l + 1))
18242755×
                    .max()
18252755×
                    .unwrap_or(0);
18262755×
                levels.insert(c, l);
1827
            } else {
18282755×
                stack.push((c, true));
18293661×
                for d in deps {
18303661×
                    if !levels.contains_key(d) {
1831451×
                        stack.push((*d, false));
18323210×
                    }
1833
                }
1834
            }
1835
        }
18362347×
        levels[h]
1837401216×
    }
1838
1839
    /// Changes that no other change depends on directly, sorted.
1840397×
    pub fn heads(&self) -> Vec<ChangeHash> {
1841397×
        let depended: crate::fasthash::FastSet<&ChangeHash> =
18423478×
            self.changes.values().flat_map(|c| c.deps.iter()).collect();
1843397×
        let mut heads: Vec<ChangeHash> = self
1844397×
            .changes
1845397×
            .keys()
18463478×
            .filter(|h| !depended.contains(h))
1847397×
            .copied()
1848397×
            .collect();
1849397×
        heads.sort();
1850397×
        heads
1851397×
    }
1852
1853
    /// Maximal elements among `hashes` (those not in the closure of another),
1854
    /// sorted. One walk down from all of them at once: whatever the walk
1855
    /// reaches is below some other element. Linear in the changes reached,
1856
    /// where comparing every pair's closures was quadratic in the number
1857
    /// of heads, which a bookmark of independent changes has many of.
18588883×
    pub fn maximal(&self, hashes: &[ChangeHash]) -> Vec<ChangeHash> {
18598883×
        let set: BTreeSet<ChangeHash> = hashes.iter().copied().collect();
18608883×
        let mut below: crate::fasthash::FastSet<ChangeHash> = Default::default();
18618883×
        let mut stack: Vec<ChangeHash> = set
18628883×
            .iter()
186315642×
            .filter_map(|h| self.changes.get(h))
186413290×
            .flat_map(|c| c.deps.iter().copied())
18658883×
            .collect();
186621814×
        while let Some(d) = stack.pop() {
186712931×
            if below.insert(d) {
18686318×
                if let Some(c) = self.changes.get(&d) {
18696318×
                    stack.extend(c.deps.iter().copied());
18706318×
                }
18716613×
            }
1872
        }
187315642×
        set.into_iter().filter(|h| !below.contains(h)).collect()
18748883×
    }
1875
18760×
    pub fn materialize_file(&self, node: &NodeId) -> Materialized {
18770×
        self.graph(node)
18780×
            .map(|g| g.materialize(self))
18790×
            .unwrap_or_default()
18800×
    }
1881
1882
    /// Live lines of a text node in materialized order.
188320×
    pub fn live_lines(&self, node: &NodeId) -> Vec<(AtomKey, Vec<u8>)> {
188420×
        let Some(g) = self.graph(node) else {
18852×
            return vec![];
1886
        };
188718×
        let m = g.materialize(self);
188818×
        m.live_atoms()
188918×
            .into_iter()
189044×
            .map(|a| (a, g.content(&a).unwrap_or_default().to_vec()))
189118×
            .collect()
189220×
    }
1893
1894
    /// Live blobs of an opaque node: every `new` that no later `Replace` retired.
1895846×
    pub fn live_blobs(&self, node: &NodeId) -> Vec<ObjectRef> {
1896846×
        let mut live: Vec<ObjectRef> = self
1897846×
            .live_replaces(node)
1898846×
            .into_iter()
1899846×
            .map(|(_, n)| n)
1900846×
            .collect();
1901846×
        live.sort();
1902846×
        live.dedup();
1903846×
        live
1904846×
    }
1905
1906
    /// The replace records whose blob is live, with the change that set it
1907
    /// (spec §9.3). A record's blob is superseded by a later replacement of
1908
    /// that blob: one whose change depends on the record's change, as every
1909
    /// replacement depends on the change that made its old blob current
1910
    /// (§8.1). Two replacements that do not know each other both stay live
1911
    /// (a replace conflict), and a file reverted to an earlier blob keeps it.
1912
    /// A replacement without that dependency, from before the rule, is read
1913
    /// as before: it supersedes every record that did not come after it.
1914886×
    pub fn live_replaces(&self, node: &NodeId) -> Vec<(ChangeHash, ObjectRef)> {
1915886×
        let Some(rs) = self.replaces.get(node) else {
19160×
            return vec![];
1917
        };
1918
        // Dependence by the bounded walk rather than whole closures: on a
1919
        // long history a closure is a hundred thousand changes, and the
1920
        // memo cannot keep them all (#107).
1921886×
        let dep =
1922147×
            |a: &ChangeHash, b: &ChangeHash| a != b && <Self as DepOracle>::depends_on(self, a, b);
1923886×
        let knows_its_old = |s: &ChangeHash, old: &ObjectRef| {
192411×
            rs.iter()
192520×
                .any(|(q, _, n)| n.as_ref() == Some(old) && q != s && dep(s, q))
192611×
        };
1927886×
        rs.iter()
19281026×
            .filter_map(|(c, _, n)| n.map(|n| (*c, n)))
19291026×
            .filter(|(c, n)| {
19301310×
                !rs.iter().any(|(s, o, _)| {
19311310×
                    s != c && *o == Some(*n) && (dep(s, c) || (!knows_its_old(s, n) && !dep(c, s)))
19321310×
                })
19331026×
            })
1934886×
            .collect()
1935886×
    }
1936
1937
    /// Short label for conflict markers: `changeid-prefix "message"`.
19381109×
    pub fn label(&self, h: &ChangeHash) -> String {
19391109×
        match self.changes.get(h) {
19401109×
            Some(c) => format!(
1941
                "{} \"{}\"",
19421109×
                c.change_id.short(8),
19431109×
                c.message.lines().next().unwrap_or("")
1944
            ),
19450×
            None => format!("#{}", h.short(12)),
1946
        }
19471109×
    }
1948
1949
    /// For resolving a conflicted text node (spec §9.6): the live atoms that
1950
    /// are not part of any conflict region in order, every live atom that is
1951
    /// part of a region, and every change that contributed to a region.
1952
    /// The changes that introduced the live blobs of an opaque node, for a
1953
    /// replace conflict (spec §9.3).
19542×
    pub fn blob_contributors(&self, node: &NodeId, blobs: &[ObjectRef]) -> Vec<ChangeHash> {
19552×
        let mut out: Vec<ChangeHash> = Vec::new();
19566×
        for (h, _, new) in self.replaces.get(node).map(|v| v.as_slice()).unwrap_or(&[]) {
19576×
            if new.is_some_and(|b| blobs.contains(&b)) && !out.contains(h) {
19584×
                out.push(*h);
19594×
            }
1960
        }
19612×
        out.sort();
19622×
        out
19632×
    }
1964
196527×
    pub fn conflict_regions(&self, node: &NodeId) -> ConflictRegions {
196627×
        let mut out = ConflictRegions::default();
196727×
        let Some(g) = self.graph(node) else {
19680×
            return out;
1969
        };
197027×
        let m = g.materialize(self);
197127×
        let mut contributors = BTreeSet::new();
197263×
        for seg in &m.segments {
197363×
            match seg {
197436×
                crate::graph::Segment::Lines(atoms) => {
197536×
                    for a in atoms {
197636×
                        out.plain
197736×
                            .push((*a, g.content(a).unwrap_or_default().to_vec()));
197836×
                    }
1979
                }
198027×
                crate::graph::Segment::Conflict(sides) => {
198154×
                    for side in sides {
198256×
                        for a in side {
198356×
                            out.region_atoms.push(*a);
198456×
                            contributors.insert(a.0);
198556×
                        }
1986
                    }
1987
                }
19880×
                crate::graph::Segment::Zombie { deleted, inserted } => {
19890×
                    for a in deleted {
19900×
                        contributors.insert(a.0);
19910×
                        contributors.extend(g.deleters(a).copied());
19920×
                    }
19930×
                    for a in inserted {
19940×
                        out.region_atoms.push(*a);
19950×
                        contributors.insert(a.0);
19960×
                    }
1997
                }
1998
            }
1999
        }
200027×
        out.contributors = contributors.into_iter().collect();
200127×
        out
200227×
    }
2003
200424454×
    pub fn render_file(&self, node: &NodeId) -> (Vec<u8>, Materialized) {
200524454×
        let Some(g) = self.graph(node) else {
2006341×
            return (Vec::new(), Materialized::default());
2007
        };
200824113×
        let m = g.materialize(self);
200924113×
        let bytes = g.render(&m, &|h| self.label(h));
201024113×
        (bytes, m)
201124454×
    }
2012
2013
    /// Every file rendered, in parallel: files are independent, and
2014
    /// rendering them is most of a snapshot's cost in a large tree.
20152324×
    pub fn materialize(&self) -> Snapshot {
2016
        use rayon::prelude::*;
20172324×
        self.require_full("materializing a state");
20182324×
        let tree = self.tree.view();
20192324×
        let entries: Vec<(&String, &crate::tree::TreeEntry)> = tree.entries.iter().collect();
20202324×
        let rendered: Vec<(String, FileState)> = entries
20212324×
            .par_iter()
202216640×
            .map(|(path, e)| (path.to_string(), self.file_state(e)))
20232324×
            .collect();
20242324×
        let files: BTreeMap<String, FileState> = rendered.into_iter().collect();
20252324×
        Snapshot {
20262324×
            tree,
20272324×
            files,
20282324×
            unrendered: BTreeSet::new(),
20292324×
        }
20302324×
    }
2031
2032
    /// Like [`State::materialize`], leaving the text files of nodes in
2033
    /// `skip` unrendered (listed in [`Snapshot::unrendered`]); every other
2034
    /// file's graph must be loaded. For a caller that already knows those
2035
    /// files are unchanged and not conflicted.
203620×
    pub fn materialize_except(&self, skip: &HashSet<NodeId>) -> Snapshot {
203720×
        self.materialize_leaving(|_, e| skip.contains(&e.node))
203820×
    }
2039
2040
    /// As [`State::materialize_except`], rendering only the text files at
2041
    /// `render` (a few, where the caller knows the rest unchanged).
2042849×
    pub fn materialize_only(&self, render: &std::collections::HashSet<&str>) -> Snapshot {
20437852×
        self.materialize_leaving(|p, _| !render.contains(p))
2044849×
    }
2045
2046
    /// As [`State::materialize_except`], rendering only the text files of
2047
    /// the nodes in `render` (a change's edited files), without a set of
2048
    /// every other file to leave out.
2049228×
    pub fn materialize_nodes(&self, render: &HashSet<NodeId>) -> Snapshot {
2050610×
        self.materialize_leaving(|_, e| !render.contains(&e.node))
2051228×
    }
2052
20531097×
    fn materialize_leaving(
20541097×
        &self,
20551097×
        leave: impl Fn(&str, &crate::tree::TreeEntry) -> bool + Sync,
20561097×
    ) -> Snapshot {
2057
        use rayon::prelude::*;
20581097×
        let tree = self.tree.view();
20591097×
        let entries: Vec<(&String, &crate::tree::TreeEntry)> = tree.entries.iter().collect();
20601097×
        let rendered: Vec<(String, FileState, bool)> = entries
20611097×
            .par_iter()
20628826×
            .map(|(path, e)| {
20638826×
                if e.kind == NodeKind::File && !e.mode.opaque && leave(path, e) {
20644575×
                    let f = FileState {
20654575×
                        node: e.node,
20664575×
                        kind: e.kind,
20674575×
                        mode: e.mode,
20684575×
                        content: FileContent::Text {
20694575×
                            bytes: Vec::new(),
20704575×
                            materialized: Materialized::default(),
20714575×
                        },
20724575×
                    };
20734575×
                    (path.to_string(), f, true)
2074
                } else {
20754251×
                    (path.to_string(), self.file_state(e), false)
2076
                }
20778826×
            })
20781097×
            .collect();
2079
        // In the view's path order, so the maps are built without searching.
20801097×
        let unrendered: BTreeSet<String> = rendered
20811097×
            .iter()
20821097×
            .filter(|(_, _, skipped)| *skipped)
20834575×
            .map(|(p, _, _)| p.clone())
20841097×
            .collect();
20851097×
        let files: BTreeMap<String, FileState> =
20868826×
            rendered.into_iter().map(|(p, f, _)| (p, f)).collect();
20871097×
        Snapshot {
20881097×
            tree,
20891097×
            files,
20901097×
            unrendered,
20911097×
        }
20921097×
    }
2093
2094
    /// A snapshot of only `paths` (those `view` has), `view` being this
2095
    /// state's tree view: for a caller that works from what changed. The
2096
    /// graphs of those text files must be loaded.
209789×
    pub fn materialize_paths<'p>(
209889×
        &self,
209989×
        view: crate::tree::TreeView,
210089×
        paths: impl IntoIterator<Item = &'p str>,
210189×
    ) -> Snapshot {
2102
        use rayon::prelude::*;
2103
        // On every core: a merge may render thousands (oxygen#96).
210489×
        let entries: Vec<(&str, &crate::tree::TreeEntry)> = paths
210589×
            .into_iter()
2106130×
            .filter_map(|p| Some((p, view.entries.get(p)?)))
210789×
            .collect();
210889×
        let files = entries
210989×
            .par_iter()
2110106×
            .map(|(p, e)| (p.to_string(), self.file_state(e)))
211189×
            .collect::<Vec<_>>()
211289×
            .into_iter()
211389×
            .collect();
211489×
        Snapshot {
211589×
            tree: view,
211689×
            files,
211789×
            unrendered: BTreeSet::new(),
211889×
        }
211989×
    }
2120
2121
    /// The state of one tree entry: a directory, an opaque file's live
2122
    /// blobs, or a text file rendered from its graph (which must be built).
21230×
    pub fn entry_state(&self, e: &crate::tree::TreeEntry) -> FileState {
21240×
        self.file_state(e)
21250×
    }
2126
212720997×
    fn file_state(&self, e: &crate::tree::TreeEntry) -> FileState {
2128
        {
212920997×
            let content = match e.kind {
21301505×
                NodeKind::Dir => FileContent::Dir,
2131104×
                NodeKind::Symlink | NodeKind::Subrepo => FileContent::Opaque {
2132104×
                    blobs: self.live_blobs(&e.node),
2133104×
                },
2134541×
                NodeKind::File if e.mode.opaque => FileContent::Opaque {
2135541×
                    blobs: self.live_blobs(&e.node),
2136541×
                },
2137
                NodeKind::File => {
213818847×
                    let (bytes, materialized) = self.render_file(&e.node);
213918847×
                    FileContent::Text {
214018847×
                        bytes,
214118847×
                        materialized,
214218847×
                    }
2143
                }
2144
            };
214520997×
            FileState {
214620997×
                node: e.node,
214720997×
                kind: e.kind,
214820997×
                mode: e.mode,
214920997×
                content,
215020997×
            }
2151
        }
215220997×
    }
2153
}
2154
2155
impl DepOracle for State {
2156
    /// Whether `a` depends on `b`, by a walk down from `a` that stops at
2157
    /// changes no higher than `b`: they cannot have `b` below them. Nothing
2158
    /// of the walk is kept, so asking about many pairs costs no more memory
2159
    /// than one walk (#25); a closure already memoised answers at once.
216066469×
    fn depends_on(&self, a: &ChangeHash, b: &ChangeHash) -> bool {
216166469×
        if a == b {
2162800×
            return true;
216365669×
        }
216465669×
        if let Some(c) = self.closure.read().unwrap().sets.get(a) {
21650×
            return c.contains(b);
216665669×
        }
216765669×
        if !self.changes.contains_key(b) {
2168800×
            return false;
216964869×
        }
217064869×
        let lb = self.level(b);
217164869×
        if self.level(a) <= lb {
217235915×
            return false;
217328954×
        }
217428954×
        let mut seen: crate::fasthash::FastSet<ChangeHash> = Default::default();
217528954×
        let mut stack: Vec<ChangeHash> = self
217628954×
            .changes
217728954×
            .get(a)
217828954×
            .map(|m| m.deps.clone())
217928954×
            .unwrap_or_default();
2180358756×
        while let Some(c) = stack.pop() {
2181339405×
            if c == *b {
21829603×
                return true;
2183329802×
            }
2184329802×
            if !seen.insert(c) || self.level(&c) <= lb {
2185221548×
                continue;
2186108254×
            }
2187108254×
            if let Some(m) = self.changes.get(&c) {
2188108254×
                stack.extend(m.deps.iter().copied());
2189108254×
            }
2190
        }
219119351×
        false
219266469×
    }
2193
}
2194
2195
#[cfg(test)]
2196
mod tests {
2197
    use super::*;
2198
    use crate::ids::ChangeId;
2199
    use crate::objects::{AtomRange, ChangeKind, Hlc, OpRef};
2200
22012023×
    fn change(deps: Vec<ChangeHash>, tree_ops: Vec<TreeOp>, edits: Vec<Edit>) -> Change {
22022023×
        let mut c = Change {
22032023×
            version: crate::objects::CHANGE_VERSION,
22042023×
            change_id: ChangeId::random(),
22052023×
            deps,
22062023×
            author: ObjectRef::ZERO,
22072023×
            hlc: Hlc {
22082023×
                wall_ms: 1,
22092023×
                logical: 0,
22102023×
            },
22112023×
            message: "m".into(),
22122023×
            tree_ops,
22132023×
            edits,
22142023×
            kind: ChangeKind::Normal,
22152023×
        };
22162023×
        c.normalize();
22172023×
        c
22182023×
    }
2219
2220
    #[test]
22211×
    fn build_edit_and_materialize() {
22221×
        let mut st = State::new();
22231×
        let f = NodeId::random();
22241×
        let c1 = change(
22251×
            vec![],
22261×
            vec![TreeOp::AddNode {
22271×
                node: f,
22281×
                kind: NodeKind::File,
22291×
                mode: Mode::TEXT,
22301×
                parent: NodeId::ROOT,
22311×
                name: "a.txt".into(),
22321×
            }],
22331×
            vec![Edit::Insert {
22341×
                node: f,
22351×
                after: AtomRef::Start,
22361×
                before: AtomRef::End,
22371×
                lines: vec![b"one\n".to_vec(), b"two\n".to_vec()].into(),
22381×
            }],
2239
        );
22401×
        let h1 = st.add(c1).unwrap();
2241
        // Depends on h1: append a line and delete "one".
22421×
        let c2 = change(
22431×
            vec![h1],
22441×
            vec![],
22451×
            vec![
22461×
                Edit::Delete {
22471×
                    node: f,
22481×
                    atoms: vec![AtomRange {
22491×
                        change: h1,
22501×
                        from: 0,
22511×
                        to: 1,
22521×
                    }],
22531×
                },
22541×
                Edit::Insert {
22551×
                    node: f,
22561×
                    after: AtomRef::Atom {
22571×
                        change: h1,
22581×
                        index: 1,
22591×
                    },
22601×
                    before: AtomRef::End,
22611×
                    lines: vec![b"three\n".to_vec()].into(),
22621×
                },
2263
            ],
2264
        );
22651×
        let h2 = st.add(c2).unwrap();
22661×
        assert_eq!(st.heads(), vec![h2]);
22671×
        assert!(st.depends_on(&h2, &h1));
22681×
        assert!(!st.depends_on(&h1, &h2));
22691×
        let snap = st.materialize();
22701×
        assert!(!snap.is_conflicted());
22711×
        let a = &snap.files["a.txt"];
22721×
        assert!(matches!(&a.content, FileContent::Text { bytes, .. } if bytes == b"two\nthree\n"));
22731×
        assert_eq!(st.live_lines(&f).len(), 2);
22741×
    }
2275
2276
    #[test]
22771×
    fn missing_dependency_is_rejected() {
22781×
        let mut st = State::new();
22791×
        let c = change(vec![ObjectRef([1; 32])], vec![], vec![]);
22801×
        assert!(matches!(st.add(c), Err(StateError::MissingDep(..))));
22811×
    }
2282
2283
    #[test]
22841×
    fn self_references_and_multi_insert_indexing() {
22851×
        let mut st = State::new();
22861×
        let f = NodeId::random();
22871×
        let me = OpRef {
22881×
            change: ObjectRef::ZERO,
22891×
            index: 0,
22901×
        };
22911×
        let c1 = change(
22921×
            vec![],
22931×
            vec![
22941×
                TreeOp::AddNode {
22951×
                    node: f,
22961×
                    kind: NodeKind::File,
22971×
                    mode: Mode::TEXT,
22981×
                    parent: NodeId::ROOT,
22991×
                    name: "x".into(),
23001×
                },
23011×
                TreeOp::SetMode {
23021×
                    node: f,
23031×
                    old: me,
23041×
                    mode: Mode {
23051×
                        exec: true,
23061×
                        opaque: false,
23071×
                    },
23081×
                },
2309
            ],
23101×
            vec![
23111×
                Edit::Insert {
23121×
                    node: f,
23131×
                    after: AtomRef::Start,
23141×
                    before: AtomRef::End,
23151×
                    lines: vec![b"a\n".to_vec(), b"c\n".to_vec()].into(),
23161×
                },
2317
                // Insert between the two lines just inserted, using self references.
23181×
                Edit::Insert {
23191×
                    node: f,
23201×
                    after: AtomRef::Atom {
23211×
                        change: ObjectRef::ZERO,
23221×
                        index: 0,
23231×
                    },
23241×
                    before: AtomRef::Atom {
23251×
                        change: ObjectRef::ZERO,
23261×
                        index: 1,
23271×
                    },
23281×
                    lines: vec![b"b\n".to_vec()].into(),
23291×
                },
2330
            ],
2331
        );
23321×
        st.add(c1).unwrap();
23331×
        let snap = st.materialize();
23341×
        let x = &snap.files["x"];
23351×
        assert_eq!(
2336
            x.mode,
2337
            Mode {
2338
                exec: true,
2339
                opaque: false
2340
            }
2341
        );
23421×
        assert!(matches!(&x.content, FileContent::Text { bytes, .. } if bytes == b"a\nb\nc\n"));
23431×
    }
2344
2345
    #[test]
23461×
    fn opaque_replace_and_conflict() {
23471×
        let mut st = State::new();
23481×
        let f = NodeId::random();
23491×
        let b1 = ObjectRef([1; 32]);
23501×
        let b2 = ObjectRef([2; 32]);
23511×
        let b3 = ObjectRef([3; 32]);
23521×
        let h1 = st
23531×
            .add(change(
23541×
                vec![],
23551×
                vec![TreeOp::AddNode {
23561×
                    node: f,
23571×
                    kind: NodeKind::File,
23581×
                    mode: Mode::OPAQUE,
23591×
                    parent: NodeId::ROOT,
23601×
                    name: "img".into(),
23611×
                }],
23621×
                vec![Edit::Replace {
23631×
                    node: f,
23641×
                    old: None,
23651×
                    new: Some(b1),
23661×
                }],
2367
            ))
23681×
            .unwrap();
23691×
        assert_eq!(st.live_blobs(&f), vec![b1]);
23701×
        st.add(change(
23711×
            vec![h1],
23721×
            vec![],
23731×
            vec![Edit::Replace {
23741×
                node: f,
23751×
                old: Some(b1),
23761×
                new: Some(b2),
23771×
            }],
2378
        ))
23791×
        .unwrap();
23801×
        st.add(change(
23811×
            vec![h1],
23821×
            vec![],
23831×
            vec![Edit::Replace {
23841×
                node: f,
23851×
                old: Some(b1),
23861×
                new: Some(b3),
23871×
            }],
2388
        ))
23891×
        .unwrap();
23901×
        let snap = st.materialize();
23911×
        assert!(snap.is_conflicted());
23921×
        assert!(
23931×
            matches!(&snap.files["img"].content, FileContent::Opaque { blobs } if blobs == &vec![b2, b3])
2394
        );
23951×
    }
2396
2397
    #[test]
23981×
    fn checkpoint_payload_round_trip_and_replay() {
23991×
        let mut st = State::new();
24001×
        let f = NodeId::random();
24011×
        let h1 = st
24021×
            .add(change(
24031×
                vec![],
24041×
                vec![TreeOp::AddNode {
24051×
                    node: f,
24061×
                    kind: NodeKind::File,
24071×
                    mode: Mode::TEXT,
24081×
                    parent: NodeId::ROOT,
24091×
                    name: "a".into(),
24101×
                }],
24111×
                vec![Edit::Insert {
24121×
                    node: f,
24131×
                    after: AtomRef::Start,
24141×
                    before: AtomRef::End,
24151×
                    lines: vec![b"1\n".to_vec(), b"2\n".to_vec()].into(),
24161×
                }],
2417
            ))
24181×
            .unwrap();
24191×
        let h2 = st
24201×
            .add(change(
24211×
                vec![h1],
24221×
                vec![],
24231×
                vec![Edit::Delete {
24241×
                    node: f,
24251×
                    atoms: vec![AtomRange {
24261×
                        change: h1,
24271×
                        from: 0,
24281×
                        to: 1,
24291×
                    }],
24301×
                }],
2431
            ))
24321×
            .unwrap();
24331×
        let payload = st.checkpoint_payload();
24341×
        let back = State::from_checkpoint_payload(&payload).unwrap();
24351×
        assert_eq!(back.len(), 2);
24361×
        assert_eq!(back.heads(), vec![h2]);
24371×
        assert_eq!(back.get(&h1).unwrap().message, "m");
24381×
        assert_eq!(back.checkpoint_payload(), payload, "deterministic");
2439
        // Replaying a further change onto the restored state equals a full build.
24401×
        let c3 = change(
24411×
            vec![h2],
24421×
            vec![],
24431×
            vec![Edit::Insert {
24441×
                node: f,
24451×
                after: AtomRef::Atom {
24461×
                    change: h1,
24471×
                    index: 1,
24481×
                },
24491×
                before: AtomRef::End,
24501×
                lines: vec![b"3\n".to_vec()].into(),
24511×
            }],
2452
        );
24531×
        let mut full = st.clone();
24541×
        full.add(c3.clone()).unwrap();
24551×
        let mut restored = back;
24561×
        restored.add(c3).unwrap();
24571×
        let (a, b) = (full.materialize(), restored.materialize());
24581×
        assert_eq!(
24591×
            a.files.keys().collect::<Vec<_>>(),
24601×
            b.files.keys().collect::<Vec<_>>()
2461
        );
24621×
        assert_eq!(a.files["a"].content, b.files["a"].content);
24631×
        assert!(
24641×
            matches!(&b.files["a"].content, FileContent::Text { bytes, .. } if bytes == b"2\n3\n")
2465
        );
24661×
        assert!(State::from_checkpoint_payload(b"nonsense").is_err());
24671×
    }
2468
2469
    /// History-only states (O10f): the same state as a full load once its
2470
    /// graphs are built, by replay and from a version 2 checkpoint with
2471
    /// later changes; unloaded graphs are never read as empty.
2472
    #[test]
24731×
    fn graph_keys_change_exactly_when_a_graph_does() {
24741×
        let (f, g) = (NodeId::random(), NodeId::random());
24751×
        let add = |node: NodeId, name: &str| TreeOp::AddNode {
24762×
            node,
24772×
            kind: NodeKind::File,
2478
            mode: Mode::TEXT,
2479
            parent: NodeId::ROOT,
24802×
            name: name.into(),
24812×
        };
24821×
        let ins = |node: NodeId, line: &[u8]| Edit::Insert {
24834×
            node,
24844×
            after: AtomRef::Start,
24854×
            before: AtomRef::End,
24864×
            lines: vec![line.to_vec()].into(),
24874×
        };
24881×
        let c1 = change(
24891×
            vec![],
24901×
            vec![add(f, "f"), add(g, "g")],
24911×
            vec![ins(f, b"f\n"), ins(g, b"g\n")],
2492
        );
24931×
        let h1 = c1.hash();
24941×
        let c2 = change(vec![h1], vec![], vec![ins(g, b"g2\n")]);
24951×
        let h2 = c2.hash();
24961×
        let c3 = change(vec![h2], vec![], vec![ins(f, b"f2\n")]);
24976×
        let load = |cs: &[&Change]| {
24986×
            let mut st = State::new_history_only();
249910×
            for c in cs {
250010×
                st.add((*c).clone()).unwrap();
250110×
            }
25026×
            st
25036×
        };
25041×
        let s1 = load(&[&c1]);
25051×
        let s2 = load(&[&c1, &c2]);
25061×
        let s3 = load(&[&c1, &c2, &c3]);
250718×
        let key = |s: &State, n: &NodeId| s.graph_key(n).unwrap();
25081×
        assert_eq!(key(&s1, &f), key(&load(&[&c1]), &f), "same state, same key");
25091×
        assert_eq!(key(&s1, &f), key(&s2, &f), "an edit elsewhere keeps it");
25101×
        assert_ne!(key(&s1, &g), key(&s2, &g), "an edit of the file changes it");
25111×
        assert_ne!(key(&s2, &f), key(&s3, &f));
25121×
        assert_ne!(key(&s1, &f), key(&s1, &g), "different files differ");
25131×
        let mut built = load(&[&c1]);
25141×
        built
25151×
            .load_graphs(&[f], |_| unreachable!(), |_| unreachable!())
25161×
            .unwrap();
25171×
        assert!(built.graph_key(&f).is_none(), "a built graph has no key");
25181×
        let mut full = State::new();
25191×
        full.add(c1.clone()).unwrap();
25201×
        assert!(full.graph_key(&f).is_none(), "nor has a full state");
2521
2522
        // Through a version 2 checkpoint of c1, keys name its graph blobs
2523
        // or inline values: stable across loads, changed by later edits.
25241×
        let graphs = full.checkpoint_file_graphs();
25251×
        let index = full.checkpoint_index(&graphs, |enc| {
25260×
            crate::hash::hash_object(crate::hash::ObjectType::Blob, enc)
25270×
        });
25288×
        let cp = |extra: &[&Change]| {
25298×
            let mut st = State::from_checkpoint_history(&index).unwrap();
25308×
            for c in extra {
25315×
                st.add((*c).clone()).unwrap();
25325×
            }
25338×
            st
25348×
        };
25351×
        assert_eq!(key(&cp(&[]), &f), key(&cp(&[]), &f));
25361×
        assert_eq!(key(&cp(&[]), &f), key(&cp(&[&c2]), &f));
25371×
        assert_ne!(key(&cp(&[]), &g), key(&cp(&[&c2]), &g));
25381×
        assert_ne!(key(&cp(&[&c2]), &f), key(&cp(&[&c2, &c3]), &f));
2539
2540
        // Partial rendering: skipped files are listed and empty, the others
2541
        // render as in a full state.
25421×
        let mut part = load(&[&c1, &c2]);
25431×
        part.load_graphs(&[g], |_| unreachable!(), |_| unreachable!())
25441×
            .unwrap();
25451×
        let snap = part.materialize_except(&[f].into_iter().collect());
25461×
        assert_eq!(snap.unrendered, ["f".to_string()].into_iter().collect());
25471×
        let mut whole = State::new();
25481×
        whole.add(c1.clone()).unwrap();
25491×
        whole.add(c2.clone()).unwrap();
25501×
        assert_eq!(snap.files["g"], whole.materialize().files["g"]);
25511×
        assert!(
25521×
            matches!(&snap.files["f"].content, FileContent::Text { bytes, .. } if bytes.is_empty())
2553
        );
25541×
    }
2555
2556
    #[test]
25571×
    fn history_only_states_build_the_same_graphs_on_demand() {
25581×
        let small = NodeId::random();
25591×
        let big = NodeId::random();
25601×
        let add = |node: NodeId, name: &str| TreeOp::AddNode {
25612×
            node,
25622×
            kind: NodeKind::File,
2563
            mode: Mode::TEXT,
2564
            parent: NodeId::ROOT,
25652×
            name: name.into(),
25662×
        };
25671×
        let lines: crate::lines::Lines = (0..300)
2568300×
            .map(|i| format!("line {i}\n").into_bytes())
25691×
            .collect();
25701×
        let c1 = change(
25711×
            vec![],
25721×
            vec![add(small, "small"), add(big, "big")],
25731×
            vec![
25741×
                Edit::Insert {
25751×
                    node: small,
25761×
                    after: AtomRef::Start,
25771×
                    before: AtomRef::End,
25781×
                    lines: vec![b"x\n".to_vec()].into(),
25791×
                },
25801×
                Edit::Insert {
25811×
                    node: big,
25821×
                    after: AtomRef::Start,
25831×
                    before: AtomRef::End,
25841×
                    lines,
25851×
                },
2586
            ],
2587
        );
25881×
        let h1 = c1.hash();
25891×
        let c2 = change(
25901×
            vec![h1],
25911×
            vec![],
25921×
            vec![
25931×
                Edit::Delete {
25941×
                    node: big,
25951×
                    atoms: vec![AtomRange {
25961×
                        change: h1,
25971×
                        from: 2,
25981×
                        to: 9,
25991×
                    }],
26001×
                },
26011×
                Edit::Insert {
26021×
                    node: big,
26031×
                    after: AtomRef::Atom {
26041×
                        change: h1,
26051×
                        index: 20,
26061×
                    },
26071×
                    before: AtomRef::Atom {
26081×
                        change: h1,
26091×
                        index: 21,
26101×
                    },
26111×
                    lines: vec![b"new a\n".to_vec(), b"new b\n".to_vec()].into(),
26121×
                },
26131×
                Edit::Insert {
26141×
                    node: small,
26151×
                    after: AtomRef::Atom {
26161×
                        change: h1,
26171×
                        index: 0,
26181×
                    },
26191×
                    before: AtomRef::End,
26201×
                    lines: vec![b"y\n".to_vec()].into(),
26211×
                },
2622
            ],
2623
        );
26241×
        let h2 = c2.hash();
26251×
        let c3 = change(
26261×
            vec![h2],
26271×
            vec![],
26281×
            vec![Edit::Delete {
26291×
                node: small,
26301×
                atoms: vec![AtomRange {
26311×
                    change: h1,
26321×
                    from: 0,
26331×
                    to: 1,
26341×
                }],
26351×
            }],
2636
        );
2637
26381×
        let mut full = State::new();
26393×
        for c in [&c1, &c2, &c3] {
26403×
            full.add(c.clone()).unwrap();
26413×
        }
2642
        // By replay: nothing built until asked, then exactly the full graphs.
26431×
        let mut hist = State::new_history_only();
26443×
        for c in [&c1, &c2, &c3] {
26453×
            hist.add(c.clone()).unwrap();
26463×
        }
26471×
        assert!(!hist.is_full() && hist.files.is_empty(), "no graph built");
26481×
        assert_eq!(hist.len(), 3);
26491×
        hist.load_graphs(
26501×
            &[big],
26510×
            |_| unreachable!("no checkpoint"),
26520×
            |_| unreachable!(),
2653
        )
26541×
        .unwrap();
26551×
        assert_eq!(
26561×
            hist.render_file(&big).0,
26571×
            full.render_file(&big).0,
2658
            "one file on its own"
2659
        );
26601×
        assert!(!hist.is_full());
26611×
        hist.load_graphs(&[small, big], |_| unreachable!(), |_| unreachable!())
26621×
            .unwrap();
26631×
        assert!(hist.is_full());
26641×
        assert_eq!(hist.checkpoint_payload(), full.checkpoint_payload());
2665
2666
        // From a version 2 checkpoint of c1+c2, then c3 applied later.
26671×
        let mut base = State::new();
26681×
        base.add(c1.clone()).unwrap();
26691×
        base.add(c2.clone()).unwrap();
26701×
        let graphs = base.checkpoint_file_graphs();
26711×
        let mut blobs: HashMap<ObjectRef, Vec<u8>> = HashMap::new();
26721×
        let index = base.checkpoint_index(&graphs, |enc| {
26731×
            let r = crate::hash::hash_object(crate::hash::ObjectType::Blob, enc);
26741×
            blobs.insert(r, enc.to_vec());
26751×
            r
26761×
        });
26771×
        let mut from_cp = State::from_checkpoint_history(&index).unwrap();
26781×
        from_cp.add(c3.clone()).unwrap();
26791×
        assert!(!from_cp.is_full());
26801×
        let mut fetched = 0;
26811×
        from_cp
26821×
            .load_graphs(
26831×
                &[small, big],
26841×
                |refs| {
26851×
                    fetched += refs.len();
26861×
                    refs.iter()
26871×
                        .map(|r| {
26881×
                            blobs
26891×
                                .get(r)
26901×
                                .cloned()
26911×
                                .ok_or_else(|| ObjectError::Schema("missing".into()))
26921×
                        })
26931×
                        .collect()
26941×
                },
26950×
                |_| unreachable!(),
2696
            )
26971×
            .unwrap();
26981×
        assert_eq!(
2699
            fetched, 1,
2700
            "the large graph's blob, in one batch; the small one was inline"
2701
        );
27021×
        assert_eq!(from_cp.checkpoint_payload(), full.checkpoint_payload());
27031×
    }
2704
2705
    #[test]
2706
    #[should_panic(expected = "was not loaded")]
27071×
    fn a_history_only_state_never_reads_an_unloaded_graph_as_empty() {
27081×
        let f = NodeId::random();
27091×
        let mut st = State::new_history_only();
27101×
        st.add(change(
27111×
            vec![],
27121×
            vec![TreeOp::AddNode {
27131×
                node: f,
27141×
                kind: NodeKind::File,
27151×
                mode: Mode::TEXT,
27161×
                parent: NodeId::ROOT,
27171×
                name: "a".into(),
27181×
            }],
27191×
            vec![Edit::Insert {
27201×
                node: f,
27211×
                after: AtomRef::Start,
27221×
                before: AtomRef::End,
27231×
                lines: vec![b"a\n".to_vec()].into(),
27241×
            }],
2725
        ))
27261×
        .unwrap();
27271×
        st.render_file(&f);
27281×
    }
2729
2730
    #[test]
2731
    #[should_panic(expected = "needs every file graph")]
27321×
    fn materializing_a_history_only_state_is_refused() {
27331×
        let f = NodeId::random();
27341×
        let mut st = State::new_history_only();
27351×
        st.add(change(
27361×
            vec![],
27371×
            vec![TreeOp::AddNode {
27381×
                node: f,
27391×
                kind: NodeKind::File,
27401×
                mode: Mode::TEXT,
27411×
                parent: NodeId::ROOT,
27421×
                name: "a".into(),
27431×
            }],
27441×
            vec![Edit::Insert {
27451×
                node: f,
27461×
                after: AtomRef::Start,
27471×
                before: AtomRef::End,
27481×
                lines: vec![b"a\n".to_vec()].into(),
27491×
            }],
2750
        ))
27511×
        .unwrap();
27521×
        st.materialize();
27531×
    }
2754
2755
    /// Version 2 payloads (spec §11): small graphs inline, large ones by
2756
    /// blob; the state read back is the same as from version 1.
2757
    #[test]
27581×
    fn checkpoint_index_round_trip_matches_version_1() {
27591×
        let mut st = State::new();
27601×
        let small = NodeId::random();
27611×
        let big = NodeId::random();
27621×
        let add = |node: NodeId, name: &str| TreeOp::AddNode {
27632×
            node,
27642×
            kind: NodeKind::File,
2765
            mode: Mode::TEXT,
2766
            parent: NodeId::ROOT,
27672×
            name: name.into(),
27682×
        };
27691×
        let big_lines: crate::lines::Lines = (0..400)
2770400×
            .map(|i| format!("line {i} of a long file\n").into_bytes())
27711×
            .collect();
27721×
        let h1 = st
27731×
            .add(change(
27741×
                vec![],
27751×
                vec![add(small, "small"), add(big, "big")],
27761×
                vec![
27771×
                    Edit::Insert {
27781×
                        node: small,
27791×
                        after: AtomRef::Start,
27801×
                        before: AtomRef::End,
27811×
                        lines: vec![b"x\n".to_vec()].into(),
27821×
                    },
27831×
                    Edit::Insert {
27841×
                        node: big,
27851×
                        after: AtomRef::Start,
27861×
                        before: AtomRef::End,
27871×
                        lines: big_lines,
27881×
                    },
2789
                ],
2790
            ))
27911×
            .unwrap();
27921×
        st.add(change(
27931×
            vec![h1],
27941×
            vec![],
27951×
            vec![Edit::Delete {
27961×
                node: big,
27971×
                atoms: vec![AtomRange {
27981×
                    change: h1,
27991×
                    from: 3,
28001×
                    to: 7,
28011×
                }],
28021×
            }],
2803
        ))
28041×
        .unwrap();
2805
28061×
        let graphs = st.checkpoint_file_graphs();
28071×
        let mut blobs: HashMap<ObjectRef, Vec<u8>> = HashMap::new();
28081×
        let index = st.checkpoint_index(&graphs, |enc| {
28091×
            let r = crate::hash::hash_object(crate::hash::ObjectType::Blob, enc);
28101×
            blobs.insert(r, enc.to_vec());
28111×
            r
28121×
        });
28131×
        assert_eq!(blobs.len(), 1, "only the large graph is a blob of its own");
28141×
        assert_eq!(
28151×
            State::checkpoint_graph_refs(&index).unwrap(),
28161×
            blobs.keys().copied().collect::<Vec<_>>()
2817
        );
28181×
        assert!(
28191×
            State::checkpoint_graph_refs(&st.checkpoint_payload())
28201×
                .unwrap()
28211×
                .is_empty(),
2822
            "version 1 names none"
2823
        );
28241×
        assert!(index.len() < st.checkpoint_payload().len());
2825
28261×
        let back = State::from_checkpoint(&index, |refs| {
28271×
            refs.iter()
28281×
                .map(|r| {
28291×
                    blobs
28301×
                        .get(r)
28311×
                        .cloned()
28321×
                        .ok_or_else(|| ObjectError::Schema("missing".into()))
28331×
                })
28341×
                .collect()
28351×
        })
28361×
        .unwrap();
28371×
        assert_eq!(
28381×
            back.checkpoint_payload(),
28391×
            st.checkpoint_payload(),
2840
            "the same state as version 1"
2841
        );
28421×
        assert_eq!(
28431×
            back.materialize().files["big"].content,
28441×
            st.materialize().files["big"].content
2845
        );
2846
        // Deterministic: the same state gives the same index.
28471×
        assert_eq!(
28481×
            back.checkpoint_index(&back.checkpoint_file_graphs(), |enc| {
28491×
                crate::hash::hash_object(crate::hash::ObjectType::Blob, enc)
28501×
            }),
2851
            index
2852
        );
2853
        // A missing graph blob is an error, not an empty file.
28541×
        assert!(
28551×
            State::from_checkpoint(&index, |_| Err(ObjectError::Schema("gone".into()))).is_err()
2856
        );
28571×
        assert!(
28581×
            State::from_checkpoint_payload(&index).is_err(),
2859
            "version 2 needs its blobs"
2860
        );
2861
        // Version 1 payloads still read, and unknown versions do not.
28621×
        assert!(State::from_checkpoint(&st.checkpoint_payload(), |_| unreachable!()).is_ok());
28631×
        let mut v3 = cbor::decode(&index).unwrap();
28641×
        if let Value::Map(m) = &mut v3 {
28651×
            m.insert(0, Value::Uint(3));
28661×
        }
28671×
        assert!(State::from_checkpoint(&cbor::encode(&v3), |_| unreachable!()).is_err());
28681×
    }
2869
2870
    #[test]
28711×
    fn maximal_elements() {
28721×
        let mut st = State::new();
28731×
        let h1 = st.add(change(vec![], vec![], vec![])).unwrap();
28741×
        let h2 = st.add(change(vec![h1], vec![], vec![])).unwrap();
28751×
        let h3 = st.add(change(vec![], vec![], vec![])).unwrap();
28761×
        let mut m = st.maximal(&[h1, h2, h3]);
28771×
        m.sort();
28781×
        let mut want = vec![h2, h3];
28791×
        want.sort();
28801×
        assert_eq!(m, want);
28811×
        assert_eq!(st.heads(), want);
28821×
    }
2883
2884
    /// The bounded walk of `depends_on` agrees with the closure on random
2885
    /// graphs, and the memo stays within its bound.
2886
    #[test]
28871×
    fn depends_on_matches_the_closure() {
2888
        use rand::{Rng, SeedableRng};
28891×
        let mut rng = rand::rngs::StdRng::seed_from_u64(11);
28901×
        for _ in 0..10 {
289110×
            let mut st = State::new();
289210×
            let mut all: Vec<ChangeHash> = Vec::new();
289310×
            for _ in 0..80 {
2894800×
                let deps: Vec<ChangeHash> =
289531600×
                    all.iter().copied().filter(|_| rng.gen_bool(0.06)).collect();
2896800×
                all.push(st.add(change(deps, vec![], vec![])).unwrap());
2897
            }
289810×
            let unknown = ObjectRef([0xee; 32]);
2899800×
            for a in &all {
2900800×
                let want = st.closure(a).clone();
2901800×
                st.closure.write().unwrap().sets.clear();
2902800×
                st.closure.write().unwrap().held = 0;
290364800×
                for b in all.iter().chain([&unknown]) {
290464800×
                    assert_eq!(
290564800×
                        st.depends_on(a, b),
290664800×
                        a == b || want.contains(b),
2907
                        "{a:?} on {b:?}"
2908
                    );
2909
                }
2910
            }
291110×
            assert!(st.closure.write().unwrap().held <= CLOSURE_MEMO_HASHES);
2912
        }
29131×
    }
2914
2915
    /// The one-walk `maximal` agrees with the definition (not in the
2916
    /// closure of another element) on random graphs, with repeated and
2917
    /// unknown hashes among the input.
2918
    #[test]
29191×
    fn maximal_matches_its_definition() {
2920
        use rand::{Rng, SeedableRng};
29211×
        let mut rng = rand::rngs::StdRng::seed_from_u64(7);
29221×
        for _ in 0..20 {
292320×
            let mut st = State::new();
292420×
            let mut all: Vec<ChangeHash> = Vec::new();
292520×
            for _ in 0..60 {
29261200×
                let deps: Vec<ChangeHash> =
292735400×
                    all.iter().copied().filter(|_| rng.gen_bool(0.05)).collect();
29281200×
                all.push(st.add(change(deps, vec![], vec![])).unwrap());
2929
            }
293020×
            let mut pick: Vec<ChangeHash> =
29311200×
                all.iter().copied().filter(|_| rng.gen_bool(0.4)).collect();
293220×
            pick.extend(pick.clone().into_iter().take(3));
293320×
            pick.push(ObjectRef([0xee; 32]));
293420×
            let set: BTreeSet<ChangeHash> = pick.iter().copied().collect();
293520×
            let want: Vec<ChangeHash> = set
293620×
                .iter()
29378394×
                .filter(|h| !set.iter().any(|o| o != *h && st.closure(o).contains(h)))
293820×
                .copied()
293920×
                .collect();
294020×
            assert_eq!(st.maximal(&pick), want);
294120×
            assert_eq!(st.maximal(&all), st.heads());
2942
        }
29431×
    }
2944
}