OXYGENHUB

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

2076 of 2233 lines run, 436 of 633 functions · run #298 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`]).
451485×
    pub(crate) fn write_flat(&self, w: &mut crate::flat::Writer) {
461485×
        w.fixed(&self.change_id.0);
471485×
        w.count(self.deps.len());
481485×
        for d in &self.deps {
491012×
            w.object(d);
501012×
        }
511485×
        w.object(&self.author);
521485×
        w.u64(self.hlc.wall_ms);
531485×
        w.u32(self.hlc.logical);
541485×
        w.bytes(self.message.as_bytes());
551485×
        w.u8(self.kind.tag() as u8);
561485×
    }
57
581535×
    pub(crate) fn read_flat(r: &mut crate::flat::Reader) -> Result<ChangeMeta, ObjectError> {
591535×
        let change_id = crate::ids::ChangeId(r.fixed::<16>()?);
601535×
        let mut deps = Vec::new();
611535×
        for _ in 0..r.count()? {
62738×
            deps.push(r.object()?);
63
        }
64
        Ok(ChangeMeta {
651535×
            change_id,
661535×
            deps,
671535×
            author: r.object()?,
68
            hlc: Hlc {
691535×
                wall_ms: r.u64()?,
701535×
                logical: r.u32()?,
71
            },
721535×
            message: r.text()?,
731535×
            kind: ChangeKind::from_tag(r.u8()? as u64)?,
74
        })
751535×
    }
76
7715138×
    pub fn of(c: &Change) -> ChangeMeta {
7815138×
        ChangeMeta {
7915138×
            change_id: c.change_id,
8015138×
            deps: c.deps.clone(),
8115138×
            author: c.author,
8215138×
            hlc: c.hlc,
8315138×
            message: c.message.clone(),
8415138×
            kind: c.kind,
8515138×
        }
8615138×
    }
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 {
15216734×
    fn len(&self) -> u64 {
15316734×
        match self {
1544567×
            Histories::Bytes(b) => b.len() as u64,
15512167×
            Histories::File(_, _, len) => *len,
156
        }
15716734×
    }
158
15921887×
    fn read(&self, at: u64, len: usize) -> std::io::Result<Vec<u8>> {
16021887×
        match self {
1614567×
            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
        }
16821887×
    }
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.
20121887×
    fn bytes(&self) -> Result<Vec<u8>, ObjectError> {
20221887×
        let damaged = |why: String| ObjectError::DamagedIndex(why);
20321887×
        let b = self
20421887×
            .from
20521887×
            .read(self.at, self.len)
20621887×
            .map_err(|e| damaged(format!("a file's history cannot be read: {e}")))?;
20721887×
        if *blake3::hash(&b).as_bytes() != self.sum {
2087×
            return Err(damaged("a file's history does not match its hash".into()));
20921880×
        }
21021880×
        Ok(b)
21121887×
    }
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.
243101879×
    fn body(&self) -> Result<&DeferredBody, ObjectError> {
244101879×
        self.body
245101879×
            .get_or_init(|| {
24637413×
                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())?,
25331938×
                    None => DeferredBody::default(),
254
                };
25537412×
                b.edits.extend(self.more.iter().cloned());
25637412×
                b.texts.extend(self.more_texts.iter().cloned());
25737412×
                Ok(b)
25837413×
            })
259101879×
            .as_ref()
260101879×
            .map_err(|e| ObjectError::DamagedIndex(e.clone()))
261101879×
    }
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.
26534979×
    fn add(&mut self, h: ChangeHash, edits: Vec<Edit>, texts: Vec<TextRef>) {
26634979×
        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;
27134977×
        }
272
        // No encoding to read, or decoded already: this cannot fail.
27334977×
        let b = self.body_mut().expect("a history in memory");
27434977×
        b.edits.push((h, edits));
27534977×
        b.texts.extend(texts);
27634979×
    }
277
27834977×
    fn body_mut(&mut self) -> Result<&mut DeferredBody, ObjectError> {
27934977×
        self.body()?;
28034977×
        self.raw = None;
28134977×
        self.more.clear();
28234977×
        self.more_texts.clear();
28334977×
        Ok(self
28434977×
            .body
28534977×
            .get_mut()
28634977×
            .and_then(|b| b.as_mut().ok())
28734977×
            .expect("decoded above"))
28834977×
    }
289
29022699×
    fn into_body(mut self) -> Result<DeferredBody, ObjectError> {
29122699×
        self.body()?;
29222698×
        Ok(self
29322698×
            .body
29422698×
            .take()
29522698×
            .and_then(|b| b.ok())
29622698×
            .expect("decoded above"))
29722699×
    }
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).
317921×
    pub fn write_index(&self) -> Option<Vec<u8>> {
318
        use crate::flat::Writer;
319921×
        let deferred = self.deferred.as_ref()?;
320921×
        if !self.files.is_empty() {
3210×
            return None;
322921×
        }
323
        // The changes first, as a block a reader can take on its own (see
324
        // [`State::read_index_changes`]).
325921×
        let mut w = Writer::default();
326921×
        w.fixed(Self::INDEX_MAGIC);
327921×
        let mut changes: Vec<(&ChangeHash, &Arc<ChangeMeta>)> = self.changes.iter().collect();
328921×
        changes.sort_by_key(|(h, _)| **h);
329921×
        w.count(changes.len());
330921×
        for (h, m) in changes {
331258×
            w.object(h);
332258×
            m.write_flat(&mut w);
333258×
        }
334921×
        let changes_block = std::mem::take(&mut w);
335921×
        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.
338921×
        let mut tw = Writer::default();
339921×
        self.tree.write_flat(&mut tw);
340921×
        w.bytes(&tw.0);
341921×
        let mut replaces: Vec<(&NodeId, &Vec<ReplaceRecord>)> = self.replaces.iter().collect();
342921×
        replaces.sort_by_key(|(n, _)| **n);
343921×
        w.count(replaces.len());
344921×
        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
        }
356921×
        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).
359921×
        let mut table = Writer::default();
360921×
        let mut histories: Vec<u8> = Vec::new();
361921×
        let mut nodes: Vec<(&NodeId, &Arc<Deferred>)> = deferred.iter().collect();
362921×
        nodes.sort_by_key(|(n, _)| **n);
363921×
        table.count(nodes.len());
36432788×
        for (n, d) in nodes {
36532788×
            let key = match d.key.get() {
36616412×
                Some(k) => *k,
367
                None => {
36816376×
                    let mut h = blake3::Hasher::new();
36916376×
                    h.update(b"oxy graph key 1\0");
37016376×
                    h.update(&n.0);
37116376×
                    let k = d.graph_key(h).ok()?;
37216376×
                    let _ = d.key.set(k);
37316376×
                    k
374
                }
375
            };
376
            // Unchanged since read: its bytes as they are, checked.
37732788×
            let bytes = match &d.raw {
37816412×
                Some(raw) if d.more.is_empty() => raw.bytes().ok()?,
37916376×
                _ => Self::write_deferred_body(d.body().ok()?)?,
380
            };
38132782×
            table.fixed(&n.0);
38232782×
            table.fixed(&key);
38332782×
            table.u64(histories.len() as u64);
38432782×
            table.u32(u32::try_from(bytes.len()).ok()?);
38532782×
            table.fixed(blake3::hash(&bytes).as_bytes());
38632782×
            histories.extend_from_slice(&bytes);
387
        }
388915×
        front.bytes(&table.0);
389915×
        let mut out = Writer::default();
390915×
        out.u64(front.0.len() as u64);
391915×
        out.fixed(&front.0);
392915×
        out.fixed(&histories);
393915×
        Some(out.0)
394921×
    }
395
396
    /// One file's history as an index encodes it; `None` when it holds
397
    /// text in memory, which an index does not.
39816376×
    fn write_deferred_body(d: &DeferredBody) -> Option<Vec<u8>> {
399
        use crate::flat::Writer;
40033506×
        let atom = |w: &mut Writer, a: &AtomRef| match a {
40116379×
            AtomRef::Start => w.u8(0),
40216467×
            AtomRef::End => w.u8(1),
403660×
            AtomRef::Atom { change, index } => {
404660×
                w.u8(2);
405660×
                w.object(change);
406660×
                w.u32(*index);
407660×
            }
40833506×
        };
40916376×
        let mut w = Writer::default();
4103×
        match &d.base {
41116373×
            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.
42416376×
        let mut texts = d.texts.iter();
42516376×
        let mut per_change: Vec<(&ChangeHash, &Vec<Edit>, Vec<&TextRef>)> = Vec::new();
42616495×
        for (c, edits) in &d.edits {
42716495×
            let n = edits
42816495×
                .iter()
42916753×
                .filter(|e| matches!(e, Edit::Insert { lines, .. } if lines.is_empty()))
43016495×
                .count();
43116495×
            per_change.push((c, edits, texts.by_ref().take(n).collect()));
432
        }
43316376×
        per_change.sort_by_key(|(c, _, _)| **c);
43416376×
        w.count(per_change.len());
43516495×
        for (c, edits, _) in &per_change {
43616495×
            w.object(c);
43716495×
            w.count(edits.len());
43816793×
            for e in edits.iter() {
43916793×
                match e {
440
                    Edit::Insert {
44116753×
                        node,
44216753×
                        after,
44316753×
                        before,
44416753×
                        lines,
445
                    } => {
44616753×
                        if !lines.is_empty() {
4470×
                            return None;
44816753×
                        }
44916753×
                        w.u8(0);
45016753×
                        w.fixed(&node.0);
45116753×
                        atom(&mut w, after);
45216753×
                        atom(&mut w, before);
453
                    }
45440×
                    Edit::Delete { node, atoms } => {
45540×
                        w.u8(1);
45640×
                        w.fixed(&node.0);
45740×
                        w.count(atoms.len());
458286×
                        for r in atoms {
459286×
                            w.object(&r.change);
460286×
                            w.u32(r.from);
461286×
                            w.u32(r.to);
462286×
                        }
463
                    }
4640×
                    Edit::Replace { .. } => return None,
465
                }
466
            }
467
        }
46816376×
        let texts: Vec<&TextRef> = per_change
46916376×
            .iter()
47016495×
            .flat_map(|(_, _, t)| t.iter().copied())
47116376×
            .collect();
47216376×
        w.count(texts.len());
47316753×
        for t in texts {
47416753×
            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
                }
48516753×
                TextRef::Inline { change, start, end } => {
48616753×
                    w.u8(1);
48716753×
                    w.object(change);
48816753×
                    w.u32(*start);
48916753×
                    w.u32(*end);
49016753×
                }
491
            }
492
        }
49316376×
        Some(w.0)
49416376×
    }
495
496
    /// The state an index holds (see [`State::write_index`]).
497213×
    pub fn read_index(bytes: &[u8]) -> Result<State, ObjectError> {
498213×
        let bad = || ObjectError::Schema("state index: truncated".into());
499213×
        let front_len =
500213×
            usize::try_from(crate::flat::Reader::new(bytes).u64()?).map_err(|_| bad())?;
501213×
        let front = bytes.get(8..8 + front_len).ok_or_else(bad)?;
502213×
        let histories = Histories::Bytes(Arc::new(bytes[8 + front_len..].to_vec()));
503213×
        Self::read_index_front(front, histories)
504213×
    }
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).
5091818×
    pub fn read_index_front(bytes: &[u8], histories: Histories) -> Result<State, ObjectError> {
510
        use crate::flat::Reader;
5111818×
        let bad = |what: &str| ObjectError::Schema(format!("state index: {what}"));
5121818×
        let mut r = Reader::new(bytes);
5131818×
        let mut st = State::new_history_only();
514
        {
5151818×
            let _t = crate::timing::phase("index: changes");
5161818×
            st.changes = Self::read_index_changes(r.bytes()?)?;
517
        }
5181818×
        let tree_bytes = r.bytes()?;
5191818×
        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
        }
5271818×
        let deferred_bytes = r.bytes()?;
5281818×
        if !r.at_end() {
5290×
            return Err(bad("trailing bytes"));
5301818×
        }
5311818×
        let (tree, deferred) = rayon::join(
5321818×
            || TreeGraph::read_flat(&mut Reader::new(tree_bytes)),
5331818×
            || Self::read_deferred(deferred_bytes, &histories),
534
        );
5351818×
        st.tree = tree?;
5361818×
        st.deferred = Some(deferred?);
5371818×
        Ok(st)
5381818×
    }
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).
5421978×
    pub fn read_index_changes(
5431978×
        bytes: &[u8],
5441978×
    ) -> Result<crate::fasthash::FastMap<ChangeHash, Arc<ChangeMeta>>, ObjectError> {
545
        use crate::flat::Reader;
5461978×
        let mut r = Reader::new(bytes);
5471978×
        if r.fixed::<18>()?.as_slice() != Self::INDEX_MAGIC {
5480×
            return Err(ObjectError::Schema("state index: not a state index".into()));
5491978×
        }
5501978×
        let n = r.count()?;
5511978×
        let mut changes = crate::fasthash::FastMap::default();
5521978×
        changes.reserve(n);
5531978×
        for _ in 0..n {
554172×
            let h = r.object()?;
555172×
            changes.insert(h, Arc::new(ChangeMeta::read_flat(&mut r)?));
556
        }
5571978×
        if !r.at_end() {
5580×
            return Err(ObjectError::Schema("state index: trailing bytes".into()));
5591978×
        }
5601978×
        Ok(changes)
5611978×
    }
562
563
    /// The deferred block: each node with its graph key and its body,
564
    /// left encoded (see [`Deferred`]).
5651818×
    fn read_deferred(
5661818×
        table: &[u8],
5671818×
        histories: &Histories,
5681818×
    ) -> Result<crate::fasthash::FastMap<NodeId, Arc<Deferred>>, ObjectError> {
569
        use crate::flat::Reader;
5701818×
        let _t = crate::timing::phase("index: deferred");
5711818×
        let mut r = Reader::new(table);
5721818×
        let mut deferred = crate::fasthash::FastMap::default();
5731818×
        let n = r.count()?;
5741818×
        deferred.reserve(n);
5751818×
        for _ in 0..n {
57616734×
            let id = NodeId(r.fixed::<16>()?);
57716734×
            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.
58016734×
            let (at, len, sum) = (r.u64()?, r.u32()? as usize, r.fixed::<32>()?);
58116734×
            if at
58216734×
                .checked_add(len as u64)
58316734×
                .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×
                ));
58816734×
            }
58916734×
            let d = Deferred {
59016734×
                raw: Some(RawHistory {
59116734×
                    from: histories.clone(),
59216734×
                    at,
59316734×
                    len,
59416734×
                    sum,
59516734×
                }),
59616734×
                key: std::sync::OnceLock::from(key),
59716734×
                ..Deferred::default()
59816734×
            };
59916734×
            deferred.insert(id, Arc::new(d));
600
        }
6011818×
        if !r.at_end() {
6020×
            return Err(ObjectError::Schema("state index: trailing bytes".into()));
6031818×
        }
6041818×
        Ok(deferred)
6051818×
    }
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 {
6851404×
    fn clone(&self) -> State {
6861404×
        State {
6871404×
            changes: self.changes.clone(),
6881404×
            tree: self.tree.clone(),
6891404×
            files: self.files.clone(),
6901404×
            replaces: self.replaces.clone(),
6911404×
            // A cache: the clone builds its own.
6921404×
            closure: Default::default(),
6931404×
            levels: Default::default(),
6941404×
            deferred: self.deferred.clone(),
6951404×
        }
6961404×
    }
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 {
72113468×
    pub fn is_conflicted(&self) -> bool {
72213468×
        match &self.content {
72312563×
            FileContent::Text { materialized, .. } => materialized.is_conflicted(),
724243×
            FileContent::Opaque { blobs } => blobs.len() > 1,
725662×
            FileContent::Dir => false,
726
        }
72713468×
    }
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 {
7491959×
    pub fn is_conflicted(&self) -> bool {
75013160×
        self.tree.is_conflicted() || self.files.values().any(|f| f.is_conflicted())
7511959×
    }
752
}
753
75461482×
fn vertex(r: AtomRef) -> Vertex {
75561482×
    match r {
75625733×
        AtomRef::Start => Vertex::Start,
75727125×
        AtomRef::End => Vertex::End,
7588624×
        AtomRef::Atom { change, index } => Vertex::Atom((change, index)),
759
    }
76061482×
}
761
762
/// Replace self references in a tree op with the concrete change hash.
76338229×
pub fn resolve_tree_op(op: &TreeOp, me: ChangeHash) -> TreeOp {
76438229×
    match op {
765690×
        TreeOp::RemoveNode { node, placement } => TreeOp::RemoveNode {
766690×
            node: *node,
767690×
            placement: placement.resolve(me),
768690×
        },
769
        TreeOp::Move {
770171×
            node,
771171×
            old,
772171×
            parent,
773171×
            name,
774171×
        } => TreeOp::Move {
775171×
            node: *node,
776171×
            old: old.resolve(me),
777171×
            parent: *parent,
778171×
            name: name.clone(),
779171×
        },
78022×
        TreeOp::SetMode { node, old, mode } => TreeOp::SetMode {
78122×
            node: *node,
78222×
            old: old.resolve(me),
78322×
            mode: *mode,
78422×
        },
78537346×
        add @ TreeOp::AddNode { .. } => add.clone(),
786
    }
78738229×
}
788
789
/// Apply one change's edits of one file (inserts and deletes; replaces
790
/// live beside the graphs) to that file's graph.
79125618×
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`).
79325618×
    let order = g.take_order();
79425618×
    let mut inserts: Vec<(Vertex, Vertex, u32, u32)> = Vec::new();
79525618×
    let mut deleted: Vec<crate::graph::AtomKey> = Vec::new();
79625618×
    let mut next = 0u32;
79727752×
    for e in edits {
79827752×
        match e {
799
            Edit::Insert {
80026393×
                after,
80126393×
                before,
80226393×
                lines,
803
                ..
80426393×
            } => {
80526393×
                let first = next;
80626393×
                let count = lines.len() as u32;
80726393×
                next += count;
80826393×
                let (after, before) = (vertex(after.resolve(h)), vertex(before.resolve(h)));
80926393×
                inserts.push((after, before, first, count));
81026393×
                g.insert_owned(h, first, after, before, lines);
81126393×
            }
8121359×
            Edit::Delete { atoms, .. } => {
8132405×
                for r in atoms {
8142405×
                    let r = r.resolve(h);
8152468×
                    deleted.extend((r.from..r.to).map(|i| (r.change, i)));
8162405×
                    g.delete(h, &r);
817
                }
818
            }
8190×
            Edit::Replace { .. } => {}
820
        }
821
    }
82225618×
    if let Some(o) = order {
82367×
        g.keep_spliced(o, h, &inserts, &deleted);
82425551×
    }
82525618×
}
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).
8303645×
fn build_deferred(
8313645×
    taken: Vec<(NodeId, Deferred)>,
8323645×
    load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
8333645×
    read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
8343645×
) -> Result<Vec<(NodeId, FileGraph)>, ObjectError> {
8353645×
    let mut taken: Vec<(NodeId, DeferredBody)> = taken
8363645×
        .into_iter()
83722699×
        .map(|(n, d)| Ok((n, d.into_body()?)))
8383645×
        .collect::<Result<_, ObjectError>>()?;
8393644×
    let mut refs: Vec<ObjectRef> = taken
8403644×
        .iter()
8413644×
        .filter_map(|(_, d)| match &d.base {
8426×
            Some(DeferredBase::Blob(r)) => Some(*r),
84322692×
            _ => None,
84422698×
        })
8453644×
        .collect();
846
    // Text blobs of inserts read without their lines, after the bases.
8473644×
    let bases = refs.len();
84822698×
    refs.extend(taken.iter().flat_map(|(_, d)| {
84925742×
        d.texts.iter().filter_map(|t| match t {
85070×
            TextRef::Blob(r, _, _) => Some(*r),
85125672×
            TextRef::Inline { .. } => None,
85225742×
        })
85322698×
    }));
854
    // And the encodings of changes whose inline inserts are needed,
855
    // each read once.
8563644×
    let reading = crate::timing::phase("graphs: read changes and blobs");
8573644×
    let mut changes: HashMap<ChangeHash, Arc<Vec<u8>>> = HashMap::new();
85825742×
    for t in taken.iter().flat_map(|(_, d)| d.texts.iter()) {
85925742×
        if let TextRef::Inline { change, .. } = t {
86025672×
            if !changes.contains_key(change) {
8615807×
                changes.insert(*change, read_change(change)?);
86219865×
            }
86370×
        }
864
    }
8653644×
    let n_changes = changes.len();
8663644×
    drop(reading);
8673644×
    let reading = crate::timing::phase("graphs:   of which blobs");
8683644×
    let mut blobs = if refs.is_empty() {
8693573×
        Vec::new()
870
    } else {
87171×
        load(&refs)?
872
    };
8733641×
    if crate::timing::enabled() {
8742×
        eprintln!(
8752×
            "timing graphs: {} graphs, {n_changes} changes, {} blobs",
8762×
            taken.len(),
8772×
            refs.len()
8782×
        );
8793639×
    }
8803641×
    if blobs.len() != refs.len() {
8810×
        return Err(ObjectError::Schema("graph blobs: wrong count".into()));
8823641×
    }
8833641×
    drop(reading);
8843641×
    let lines = crate::timing::phase("graphs: hand out blobs");
8853641×
    let mut texts = blobs.split_off(bases).into_iter();
8863641×
    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>>);
8903641×
    let mut work: Vec<Work> = Vec::with_capacity(taken.len());
89122695×
    for (node, d) in taken.drain(..) {
89222695×
        let wanted = d
89322695×
            .texts
89422695×
            .iter()
89525740×
            .filter(|t| matches!(t, TextRef::Blob(..)))
89622695×
            .count();
89722695×
        let mine: Vec<Vec<u8>> = texts.by_ref().take(wanted).collect();
89822695×
        if mine.len() != wanted {
8990×
            return Err(ObjectError::Schema("text blobs: wrong count".into()));
90022695×
        }
90122695×
        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
            ),
90722690×
            _ => None,
908
        };
90922695×
        work.push((node, d, bytes, mine));
910
    }
9113641×
    drop(lines);
9123641×
    let _t = crate::timing::phase("graphs: lines and build");
913
    use rayon::prelude::*;
9143641×
    let changes = &changes;
9153641×
    let built: Vec<(NodeId, FileGraph)> = work
9163641×
        .into_par_iter()
91722695×
        .map(|(node, mut d, bytes, mine)| {
918
            // Fill in the lines of inserts read without them, in order.
91922695×
            let mut want = std::mem::take(&mut d.texts).into_iter();
92022695×
            let mut mine = mine.into_iter();
92125159×
            for (_, edits) in d.edits.iter_mut() {
92226853×
                for e in edits.iter_mut() {
92326853×
                    if let Edit::Insert { lines, .. } = e {
92425747×
                        if lines.is_empty() {
92525740×
                            *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
                                }
93225672×
                                Some(TextRef::Inline { change, start, end }) => {
93325672×
                                    let bytes = &changes[&change];
93425672×
                                    let range = bytes
93525672×
                                        .get(start as usize..end as usize)
93625672×
                                        .ok_or_else(|| {
9370×
                                            ObjectError::Schema(
9380×
                                                "an inline insert lies outside its change".into(),
9390×
                                            )
9400×
                                        })?;
94125672×
                                    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×
                        }
9501106×
                    }
951
                }
952
            }
95322695×
            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
                }
95822594×
                _ => FileGraph::default(),
959
            };
96025159×
            for (h, edits) in std::mem::take(&mut d.edits) {
96125159×
                apply_file_edits(&mut graph, h, edits);
96225159×
            }
96322695×
            Ok((node, graph))
96422695×
        })
9653641×
        .collect::<Result<_, ObjectError>>()?;
9663641×
    Ok(built)
9673645×
}
968
969
impl State {
970700×
    pub fn new() -> Self {
971700×
        Self::default()
972700×
    }
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.
9783495×
    pub fn new_history_only() -> Self {
9793495×
        State {
9803495×
            deferred: Some(Default::default()),
9813495×
            ..Self::default()
9823495×
        }
9833495×
    }
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.
9923272×
    pub fn is_history_only(&self) -> bool {
9933272×
        self.deferred.is_some()
9943272×
    }
995
996
    /// Whether this state holds every file graph.
9972718×
    pub fn is_full(&self) -> bool {
9982718×
        self.deferred.as_ref().is_none_or(|d| d.is_empty())
9992718×
    }
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.
100325072×
    fn graph(&self, node: &NodeId) -> Option<&FileGraph> {
100425072×
        if let Some(d) = &self.deferred {
10059992×
            assert!(
10069992×
                !d.contains_key(node),
1007
                "file graph {} of a history-only state was not loaded (State::load_graphs)",
10081×
                node.short(12)
1009
            );
101015080×
        }
101125071×
        self.files.get(node).map(|g| g.as_ref())
101225071×
    }
1013
1014
    /// Operations over every file need every graph.
10152711×
    fn require_full(&self, what: &str) {
10162711×
        assert!(
10172711×
            self.is_full(),
1018
            "{what} needs every file graph; this state was loaded for history only"
1019
        );
10202710×
    }
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.
10253568×
    pub fn load_graphs(
10263568×
        &mut self,
10273568×
        nodes: &[NodeId],
10283568×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
10293568×
        read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
10303568×
    ) -> Result<(), ObjectError> {
10313568×
        let Some(deferred) = self.deferred.as_mut() else {
10320×
            return Ok(());
1033
        };
10343568×
        let taken: Vec<(NodeId, Deferred)> = nodes
10353568×
            .iter()
103622868×
            .filter_map(|n| deferred.remove(n).map(|d| (*n, Arc::unwrap_or_clone(d))))
10373568×
            .collect();
10383568×
        let built = build_deferred(taken, load, read_change)?;
10393564×
        self.files
104022634×
            .extend(built.into_iter().map(|(n, g)| (n, Arc::new(g))));
10413564×
        Ok(())
10423568×
    }
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.
1095426×
    pub fn drop_graphs(&mut self, nodes: &[NodeId]) {
10965996×
        for n in nodes {
10975996×
            self.files.remove(n);
10985996×
        }
1099426×
    }
1100
1101
    /// Build every graph a history-only state has not built, making it full.
11021791×
    pub fn load_all_graphs(
11031791×
        &mut self,
11041791×
        load: impl FnOnce(&[ObjectRef]) -> Result<Vec<Vec<u8>>, ObjectError>,
11051791×
        read_change: impl Fn(&ChangeHash) -> Result<Arc<Vec<u8>>, ObjectError>,
11061791×
    ) -> Result<(), ObjectError> {
11071791×
        let nodes: Vec<NodeId> = match &self.deferred {
11081791×
            Some(d) => d.keys().copied().collect(),
11090×
            None => return Ok(()),
1110
        };
11111791×
        self.load_graphs(&nodes, load, read_change)?;
11121791×
        self.deferred = None;
11131791×
        Ok(())
11141791×
    }
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.
11205849×
    pub fn single_text(&self, node: &NodeId) -> Option<TextRef> {
11215849×
        if self.files.contains_key(node) {
11220×
            return None;
11235849×
        }
1124
        // A history that cannot be read is no single text: building its
1125
        // graph reports why.
11265849×
        let d = self.deferred.as_ref()?.get(node)?.body().ok()?;
11275781×
        let [(_, edits)] = d.edits.as_slice() else {
1128238×
            return None;
1129
        };
1130
        let [Edit::Insert {
1131
            after: AtomRef::Start,
1132
            before: AtomRef::End,
11335543×
            lines,
1134
            ..
11355543×
        }] = edits.as_slice()
1136
        else {
11370×
            return None;
1138
        };
11395543×
        match (&d.base, d.texts.as_slice()) {
11405543×
            (None, [t]) if lines.is_empty() => Some(*t),
11410×
            _ => None,
1142
        }
11435849×
    }
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.
115228558×
    pub fn graph_key(&self, node: &NodeId) -> Option<[u8; 32]> {
115328558×
        let deferred = self.deferred.as_ref()?;
115428520×
        if self.files.contains_key(node) {
1155418×
            return None;
115628102×
        }
115728102×
        let mut h = blake3::Hasher::new();
115828102×
        h.update(b"oxy graph key 1\0");
1159
        // A change's edits are applied per node: the node is part of it.
116028102×
        h.update(&node.0);
116128102×
        let Some(d) = deferred.get(node) else {
1162
            // Never edited: the empty graph.
1163223×
            h.update(&[0]);
1164223×
            return Some(*h.finalize().as_bytes());
1165
        };
1166
        // A history that cannot be read has no key: the file is read.
116727879×
        if let Some(k) = d.key.get() {
116822209×
            return Some(*k);
11695670×
        }
11705670×
        let k = d.graph_key(h).ok()?;
11715670×
        Some(*d.key.get_or_init(|| k))
117228558×
    }
1173
11743392×
    pub fn len(&self) -> usize {
11753392×
        self.changes.len()
11763392×
    }
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.
124522046×
    fn graph_key(&self, mut h: blake3::Hasher) -> Result<[u8; 32], ObjectError> {
124622046×
        let d = self.body()?;
124713×
        match &d.base {
124822033×
            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
        };
125222046×
        let mut changes: Vec<&ChangeHash> = d.edits.iter().map(|(c, _)| c).collect();
125322046×
        changes.sort();
125422046×
        changes.dedup();
125522046×
        h.update(&(changes.len() as u64).to_le_bytes());
125623401×
        for c in changes {
125723401×
            h.update(&c.0);
125823401×
        }
125922046×
        Ok(*h.finalize().as_bytes())
126022046×
    }
1261
}
1262
1263
impl State {
12640×
    pub fn is_empty(&self) -> bool {
12650×
        self.changes.is_empty()
12660×
    }
126713442×
    pub fn contains(&self, h: &ChangeHash) -> bool {
126813442×
        self.changes.contains_key(h)
126913442×
    }
127064×
    pub fn get(&self, h: &ChangeHash) -> Option<&ChangeMeta> {
127164×
        self.changes.get(h).map(|m| &**m)
127264×
    }
12731397×
    pub fn changes(&self) -> impl Iterator<Item = (&ChangeHash, &ChangeMeta)> {
12743447×
        self.changes.iter().map(|(h, m)| (h, &**m))
12751397×
    }
127617589×
    pub fn tree(&self) -> &TreeGraph {
127717589×
        &self.tree
127817589×
    }
1279581×
    pub fn file(&self, node: &NodeId) -> Option<&FileGraph> {
1280581×
        self.graph(node)
1281581×
    }
1282
1283
    /// Add a change whose dependencies are already present.
12842832×
    pub fn add(&mut self, change: Change) -> Result<ChangeHash, StateError> {
12852832×
        let h = change.hash();
12862832×
        self.add_hashed(h, change)
12872832×
    }
1288
1289
    /// [`State::add`] for a change whose hash `h` is already known (it was
1290
    /// just stored), so it is not encoded again.
12913480×
    pub fn add_hashed(&mut self, h: ChangeHash, change: Change) -> Result<ChangeHash, StateError> {
12923480×
        if self.changes.contains_key(&h) {
129339×
            return Ok(h);
12943441×
        }
12954957×
        for d in &change.deps {
12964957×
            if !self.changes.contains_key(d) {
12972×
                return Err(StateError::MissingDep(*d, h));
12984955×
            }
1299
        }
13003439×
        self.add_known(h, change, Vec::new());
13013439×
        Ok(h)
13023480×
    }
1303
1304
    /// Add a change in any order; call `validate` once all are in.
1305967×
    pub fn add_unchecked(&mut self, change: Change) -> ChangeHash {
1306967×
        let h = change.hash();
1307967×
        self.add_known(h, change, Vec::new());
1308967×
        h
1309967×
    }
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.
137312165×
    pub fn add_known(&mut self, h: ChangeHash, change: Change, texts: Vec<TextRef>) {
137412165×
        if self.changes.contains_key(&h) {
13750×
            return;
137612165×
        }
137712165×
        let meta = ChangeMeta::of(&change);
137812165×
        self.apply_with(h, change, texts);
137912165×
        self.changes.insert(h, Arc::new(meta));
138012165×
        {
138112165×
            let mut m = self.closure.write().unwrap();
138212165×
            m.sets.clear();
138312165×
            m.held = 0;
138412165×
        }
138512165×
    }
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()
13981418×
                .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()
14502198×
                .map(|h| {
14512198×
                    let m = &self.changes[h];
14522198×
                    Value::Array(vec![
14532198×
                        Value::Bytes(h.0.to_vec()),
14542198×
                        Value::Bytes(m.change_id.0.to_vec()),
14552198×
                        Value::Array(m.deps.iter().map(|d| Value::Bytes(d.0.to_vec())).collect()),
14562198×
                        Value::Text(m.message.clone()),
14572198×
                        Value::Uint(m.hlc.wall_ms),
14582198×
                        Value::Uint(m.hlc.logical as u64),
14592198×
                        Value::Uint(m.kind.tag()),
14602198×
                        Value::Bytes(m.author.0.to_vec()),
1461
                    ])
14622198×
                })
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());
1525225×
        for f in entries {
1526225×
            let e = o::array(f, "file")?;
1527225×
            if e.len() != 2 {
15280×
                return Err(ObjectError::Schema("file: expected [node, graph]".into()));
1529225×
            }
1530225×
            let node = NodeId::from_slice(o::bytes(&e[0], "node")?)
1531225×
                .ok_or_else(|| ObjectError::Schema("node: expected 16 bytes".into()))?;
1532225×
            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×
            }
1553135×
        } 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()
1561219×
            .map(|(node, entry)| {
1562219×
                let graph = match fetched.get(node) {
15633×
                    Some(bytes) => FileGraph::from_value(&cbor::decode(bytes)?)?,
1564216×
                    None => FileGraph::from_value(entry)?,
1565
                };
1566219×
                Ok((*node, graph))
1567219×
            })
156854×
            .collect::<Result<Vec<_>, ObjectError>>()?;
156954×
        st.files
1570219×
            .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();
1626497×
        for c in o::array(o::field(m, 1, "changes")?, "changes")? {
1627497×
            let e = o::array(c, "change meta")?;
1628497×
            if e.len() != 8 {
16290×
                return Err(ObjectError::Schema("change meta: expected 8 fields".into()));
1630497×
            }
1631497×
            let hash = o::oref(&e[0], "hash")?;
1632497×
            let meta = ChangeMeta {
1633497×
                change_id: crate::ids::ChangeId::from_slice(o::bytes(&e[1], "change id")?)
1634497×
                    .ok_or_else(|| ObjectError::Schema("change id: expected 16 bytes".into()))?,
1635497×
                deps: o::array(&e[2], "deps")?
1636497×
                    .iter()
1637612×
                    .map(|d| o::oref(d, "dep"))
1638497×
                    .collect::<Result<Vec<_>, _>>()?,
1639497×
                message: o::text(&e[3], "message")?.to_owned(),
1640
                hlc: Hlc {
1641497×
                    wall_ms: o::uint(&e[4], "wall")?,
1642497×
                    logical: o::u32_(&e[5], "logical")?,
1643
                },
1644497×
                kind: ChangeKind::from_tag(o::uint(&e[6], "kind")?)?,
1645497×
                author: o::oref(&e[7], "author")?,
1646
            };
1647497×
            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
16793443×
    pub fn validate(&self) -> Result<(), StateError> {
16808796×
        for (h, c) in &self.changes {
16818796×
            for d in &c.deps {
16825212×
                if !self.changes.contains_key(d) {
16830×
                    return Err(StateError::MissingDep(*d, *h));
16845212×
                }
1685
            }
1686
        }
16873443×
        Ok(())
16883443×
    }
1689
169012165×
    fn apply_with(&mut self, h: ChangeHash, c: Change, texts: Vec<TextRef>) {
169138229×
        for (i, op) in c.tree_ops.iter().enumerate() {
169238229×
            self.tree.apply(h, i as u32, &resolve_tree_op(op, h));
169338229×
        }
169412165×
        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.
16977008×
            let mut texts = texts.into_iter();
16987008×
            let mut per_node: HashMap<NodeId, (Vec<Edit>, Vec<TextRef>)> = HashMap::new();
169938050×
            for e in c.edits {
170038050×
                match e {
1701614×
                    Edit::Replace { node, old, new } => {
1702614×
                        self.replaces.entry(node).or_default().push((h, old, new))
1703
                    }
170437436×
                    e => {
170537436×
                        let slot = per_node.entry(e.node()).or_default();
170635717×
                        if matches!(&e, Edit::Insert { lines, .. } if lines.is_empty()) {
170732141×
                            slot.1.extend(texts.next());
170832141×
                        }
170937436×
                        slot.0.push(e);
1710
                    }
1711
                }
1712
            }
171335438×
            for (node, (edits, node_texts)) in per_node {
171435438×
                match self.files.get_mut(&node) {
1715459×
                    Some(g) => {
1716459×
                        assert!(
1717459×
                            node_texts.is_empty(),
1718
                            "a change read without its text applied to a built graph"
1719
                        );
1720459×
                        apply_file_edits(Arc::make_mut(g), h, edits)
1721
                    }
172234979×
                    None => {
172334979×
                        let d = Arc::make_mut(deferred.entry(node).or_default());
172434979×
                        d.add(h, edits, node_texts);
172534979×
                        d.key = Default::default();
172634979×
                    }
1727
                }
1728
            }
17297008×
            return;
17305157×
        }
17315157×
        assert!(
17325157×
            texts.is_empty(),
1733
            "a change read without its text applied to a full state"
1734
        );
17355157×
        let mut counters: HashMap<NodeId, u32> = HashMap::new();
17365961×
        for e in c.edits {
17375961×
            match e {
1738
                Edit::Insert {
17394348×
                    node,
17404348×
                    after,
17414348×
                    before,
17424348×
                    lines,
17434348×
                } => {
17444348×
                    let idx = counters.entry(node).or_default();
17454348×
                    let first = *idx;
17464348×
                    *idx += lines.len() as u32;
17474348×
                    Arc::make_mut(self.files.entry(node).or_default()).insert_owned(
17484348×
                        h,
17494348×
                        first,
17504348×
                        vertex(after.resolve(h)),
17514348×
                        vertex(before.resolve(h)),
17524348×
                        lines,
17534348×
                    );
17544348×
                }
17551500×
                Edit::Delete { node, atoms } => {
17561500×
                    let g = Arc::make_mut(self.files.entry(node).or_default());
17571789×
                    for r in atoms {
17581789×
                        g.delete(h, &r.resolve(h));
17591789×
                    }
1760
                }
1761113×
                Edit::Replace { node, old, new } => {
1762113×
                    self.replaces.entry(node).or_default().push((h, old, new));
1763113×
                }
1764
            }
1765
        }
176612165×
    }
1767
1768
    /// Transitive dependencies of `h`, excluding `h` itself.
17698955×
    pub fn closure(&self, h: &ChangeHash) -> Arc<HashSet<ChangeHash>> {
17708955×
        if let Some(c) = self.closure.read().unwrap().sets.get(h) {
17717576×
            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
17958955×
    }
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.
1800401566×
    fn level(&self, h: &ChangeHash) -> u32 {
1801
        // Nearly always known: read without excluding the other threads
1802
        // rendering beside this one (oxygen#96).
1803401566×
        if let Some(l) = self.levels.read().unwrap().get(h) {
1804399181×
            return *l;
18052385×
        }
18062385×
        let mut levels = self.levels.write().unwrap();
18072385×
        if let Some(l) = levels.get(h) {
180816×
            return *l;
18092369×
        }
18102369×
        let mut stack: Vec<(ChangeHash, bool)> = vec![(*h, false)];
18117807×
        while let Some((c, expanded)) = stack.pop() {
18125438×
            if levels.contains_key(&c) {
181320×
                continue;
18145418×
            }
18155418×
            let deps: &[ChangeHash] = self
18165418×
                .changes
18175418×
                .get(&c)
18185418×
                .map(|m| m.deps.as_slice())
18195418×
                .unwrap_or(&[]);
18205418×
            if expanded {
18212709×
                let l = deps
18222709×
                    .iter()
18233452×
                    .map(|d| levels.get(d).map_or(0, |l| l + 1))
18242709×
                    .max()
18252709×
                    .unwrap_or(0);
18262709×
                levels.insert(c, l);
1827
            } else {
18282709×
                stack.push((c, true));
18293452×
                for d in deps {
18303452×
                    if !levels.contains_key(d) {
1831360×
                        stack.push((*d, false));
18323092×
                    }
1833
                }
1834
            }
1835
        }
18362369×
        levels[h]
1837401566×
    }
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> =
18423368×
            self.changes.values().flat_map(|c| c.deps.iter()).collect();
1843397×
        let mut heads: Vec<ChangeHash> = self
1844397×
            .changes
1845397×
            .keys()
18463368×
            .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.
18588745×
    pub fn maximal(&self, hashes: &[ChangeHash]) -> Vec<ChangeHash> {
18598745×
        let set: BTreeSet<ChangeHash> = hashes.iter().copied().collect();
18608745×
        let mut below: crate::fasthash::FastSet<ChangeHash> = Default::default();
18618745×
        let mut stack: Vec<ChangeHash> = set
18628745×
            .iter()
186315339×
            .filter_map(|h| self.changes.get(h))
186413065×
            .flat_map(|c| c.deps.iter().copied())
18658745×
            .collect();
186621187×
        while let Some(d) = stack.pop() {
186712442×
            if below.insert(d) {
18686130×
                if let Some(c) = self.changes.get(&d) {
18696130×
                    stack.extend(c.deps.iter().copied());
18706130×
                }
18716312×
            }
1872
        }
187315339×
        set.into_iter().filter(|h| !below.contains(h)).collect()
18748745×
    }
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()
192521×
                .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)| {
19301309×
                !rs.iter().any(|(s, o, _)| {
19311309×
                    s != c && *o == Some(*n) && (dep(s, c) || (!knows_its_old(s, n) && !dep(c, s)))
19321309×
                })
19331026×
            })
1934886×
            .collect()
1935886×
    }
1936
1937
    /// Short label for conflict markers: `changeid-prefix "message"`.
19381113×
    pub fn label(&self, h: &ChangeHash) -> String {
19391113×
        match self.changes.get(h) {
19401113×
            Some(c) => format!(
1941
                "{} \"{}\"",
19421113×
                c.change_id.short(8),
19431113×
                c.message.lines().next().unwrap_or("")
1944
            ),
19450×
            None => format!("#{}", h.short(12)),
1946
        }
19471113×
    }
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
200424444×
    pub fn render_file(&self, node: &NodeId) -> (Vec<u8>, Materialized) {
200524444×
        let Some(g) = self.graph(node) else {
2006456×
            return (Vec::new(), Materialized::default());
2007
        };
200823988×
        let m = g.materialize(self);
200923988×
        let bytes = g.render(&m, &|h| self.label(h));
201023988×
        (bytes, m)
201124444×
    }
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.
20152313×
    pub fn materialize(&self) -> Snapshot {
2016
        use rayon::prelude::*;
20172313×
        self.require_full("materializing a state");
20182313×
        let tree = self.tree.view();
20192313×
        let entries: Vec<(&String, &crate::tree::TreeEntry)> = tree.entries.iter().collect();
20202313×
        let rendered: Vec<(String, FileState)> = entries
20212313×
            .par_iter()
202216614×
            .map(|(path, e)| (path.to_string(), self.file_state(e)))
20232313×
            .collect();
20242313×
        let files: BTreeMap<String, FileState> = rendered.into_iter().collect();
20252313×
        Snapshot {
20262313×
            tree,
20272313×
            files,
20282313×
            unrendered: BTreeSet::new(),
20292313×
        }
20302313×
    }
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).
2042847×
    pub fn materialize_only(&self, render: &std::collections::HashSet<&str>) -> Snapshot {
20437852×
        self.materialize_leaving(|p, _| !render.contains(p))
2044847×
    }
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.
2049218×
    pub fn materialize_nodes(&self, render: &HashSet<NodeId>) -> Snapshot {
2050583×
        self.materialize_leaving(|_, e| !render.contains(&e.node))
2051218×
    }
2052
20531085×
    fn materialize_leaving(
20541085×
        &self,
20551085×
        leave: impl Fn(&str, &crate::tree::TreeEntry) -> bool + Sync,
20561085×
    ) -> Snapshot {
2057
        use rayon::prelude::*;
20581085×
        let tree = self.tree.view();
20591085×
        let entries: Vec<(&String, &crate::tree::TreeEntry)> = tree.entries.iter().collect();
20601085×
        let rendered: Vec<(String, FileState, bool)> = entries
20611085×
            .par_iter()
20628793×
            .map(|(path, e)| {
20638793×
                if e.kind == NodeKind::File && !e.mode.opaque && leave(path, e) {
20644572×
                    let f = FileState {
20654572×
                        node: e.node,
20664572×
                        kind: e.kind,
20674572×
                        mode: e.mode,
20684572×
                        content: FileContent::Text {
20694572×
                            bytes: Vec::new(),
20704572×
                            materialized: Materialized::default(),
20714572×
                        },
20724572×
                    };
20734572×
                    (path.to_string(), f, true)
2074
                } else {
20754221×
                    (path.to_string(), self.file_state(e), false)
2076
                }
20778793×
            })
20781085×
            .collect();
2079
        // In the view's path order, so the maps are built without searching.
20801085×
        let unrendered: BTreeSet<String> = rendered
20811085×
            .iter()
20821085×
            .filter(|(_, _, skipped)| *skipped)
20834572×
            .map(|(p, _, _)| p.clone())
20841085×
            .collect();
20851085×
        let files: BTreeMap<String, FileState> =
20868793×
            rendered.into_iter().map(|(p, f, _)| (p, f)).collect();
20871085×
        Snapshot {
20881085×
            tree,
20891085×
            files,
20901085×
            unrendered,
20911085×
        }
20921085×
    }
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
212720941×
    fn file_state(&self, e: &crate::tree::TreeEntry) -> FileState {
2128
        {
212920941×
            let content = match e.kind {
21301455×
                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 => {
213818841×
                    let (bytes, materialized) = self.render_file(&e.node);
213918841×
                    FileContent::Text {
214018841×
                        bytes,
214118841×
                        materialized,
214218841×
                    }
2143
                }
2144
            };
214520941×
            FileState {
214620941×
                node: e.node,
214720941×
                kind: e.kind,
214820941×
                mode: e.mode,
214920941×
                content,
215020941×
            }
2151
        }
215220941×
    }
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.
216066544×
    fn depends_on(&self, a: &ChangeHash, b: &ChangeHash) -> bool {
216166544×
        if a == b {
2162800×
            return true;
216365744×
        }
216465744×
        if let Some(c) = self.closure.read().unwrap().sets.get(a) {
21650×
            return c.contains(b);
216665744×
        }
216765744×
        if !self.changes.contains_key(b) {
2168800×
            return false;
216964944×
        }
217064944×
        let lb = self.level(b);
217164944×
        if self.level(a) <= lb {
217236000×
            return false;
217328944×
        }
217428944×
        let mut seen: crate::fasthash::FastSet<ChangeHash> = Default::default();
217528944×
        let mut stack: Vec<ChangeHash> = self
217628944×
            .changes
217728944×
            .get(a)
217828944×
            .map(|m| m.deps.clone())
217928944×
            .unwrap_or_default();
2180360023×
        while let Some(c) = stack.pop() {
2181340711×
            if c == *b {
21829632×
                return true;
2183331079×
            }
2184331079×
            if !seen.insert(c) || self.level(&c) <= lb {
2185223187×
                continue;
2186107892×
            }
2187107892×
            if let Some(m) = self.changes.get(&c) {
2188107892×
                stack.extend(m.deps.iter().copied());
2189107892×
            }
2190
        }
219119312×
        false
219266544×
    }
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()
29378307×
                .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
}