Skip to main content

rete_core/
file.rs

1//! `.rete` file assembly and reading (SPEC.md §4, §9).
2//!
3//! v0 layout:
4//!
5//! ```text
6//! [0..1024)  header ([`crate::header::HEADER_LEN`])
7//! [dict]     dictionary container: 4 front-coded sections
8//! [index]    permutation container: 6 triple blocks (SPO/POS/OSP/SOP/PSO/OPS)
9//! [pyramid]  summary meta (and, in future, tile directories)
10//! [footer]   trailing magic
11//! ```
12//!
13//! The header points at the dictionary container (`dictionary_offset/len`) and
14//! the permutation container (`root_dir_offset/len`); routed readers can fetch a
15//! single permutation payload from that container.
16
17use crate::dictionary::Dictionary;
18use crate::header::{
19    Header, Section, SectionKind, FLAG_HAS_QUADS, FLAG_HAS_QUOTED_TRIPLES, FLAG_TILE_SYNOPSIS,
20    HEADER_LEN, MAGIC,
21};
22use crate::index::{GraphIndex, IndexPermutation, Pattern, PermSet, NUM_PERMS};
23use crate::meta::{ClassNode, CommunityDescriptor, LevelLinks, LevelRollup, PyramidMeta};
24use crate::pyramid::{build_dendrogram, project_graph, PyramidAlgo};
25use crate::reader::RangeReader;
26use crate::tiling::{choose_round_for_budget, summarize, SuperEdge};
27use crate::triples::Triple;
28use crate::varint::{read_uvarint, write_uvarint};
29
30#[cfg(test)]
31mod hardening_tests;
32
33/// Default per-tile byte budget `T` (SPEC.md §7.1).
34pub const DEFAULT_TILE_BUDGET: usize = 64 * 1024;
35
36/// Build the encoded pyramid-meta section for a graph: cluster, pick a round
37/// sized to `budget`, then emit the **summary** (quotient) graph. Returns
38/// `(encoded_meta, pyramid_levels)`.
39///
40/// Per-community tiles are *not* stored: they would duplicate every triple, and
41/// the exact ranged single-pattern path now routes into one permutation section
42/// without that fourth copy. Physical community-tile directories are the next
43/// storage step (SPEC §7.2).
44pub fn build_pyramid_meta(
45    dict: &Dictionary,
46    triples: &[(u32, u32, u32)],
47    budget: usize,
48) -> (Vec<u8>, u16) {
49    build_pyramid_meta_with(dict, triples, budget, None)
50}
51
52/// Like [`build_pyramid_meta`], but `type_override` forces the schema-pyramid's
53/// type predicate (e.g. `wdt:P31`) instead of auto-detection. Uses the default
54/// [`PyramidAlgo::Louvain`] community algorithm — byte-identical to before.
55pub fn build_pyramid_meta_with(
56    dict: &Dictionary,
57    triples: &[(u32, u32, u32)],
58    budget: usize,
59    type_override: Option<&str>,
60) -> (Vec<u8>, u16) {
61    build_pyramid_meta_algo(dict, triples, budget, type_override, PyramidAlgo::Louvain)
62}
63
64/// Like [`build_pyramid_meta_with`], but selects the community [`PyramidAlgo`].
65/// [`PyramidAlgo::Types`] partitions by `rdf:type` — the deterministic,
66/// parallelizable alternative to Louvain (one linear pass, no modularity) that
67/// still emits the full summary + `query_stats`; it falls back to Louvain when the
68/// graph has no usable typing. Everything downstream of the dendrogram (round
69/// choice, summary, schema pyramid, planner stats) is shared across algorithms.
70pub fn build_pyramid_meta_algo(
71    dict: &Dictionary,
72    triples: &[(u32, u32, u32)],
73    budget: usize,
74    type_override: Option<&str>,
75    algo: PyramidAlgo,
76) -> (Vec<u8>, u16) {
77    build_pyramid_meta_layout(
78        dict,
79        triples,
80        budget,
81        type_override,
82        algo,
83        PyramidLayout::Combined,
84    )
85}
86
87/// What a pyramid-meta section carries besides the community summary.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
89pub enum PyramidLayout {
90    /// Everything, as every build up to 0.3.x wrote it: the summary, the
91    /// per-community descriptors, the schema pyramid, `query_stats`, the
92    /// characteristic sets and the bounded (8,192-entry) label block.
93    #[default]
94    Combined,
95    /// Only what needs the community partition: the summary and the
96    /// per-community descriptors. The schema, `query_stats` and the
97    /// characteristic sets live in the SCHEMA section (kind 9) and the labels
98    /// in the LABEL_INDEX section (kind 8) — what `rete build` writes.
99    CommunityOnly,
100}
101
102/// Like [`build_pyramid_meta_algo`], choosing the [`PyramidLayout`].
103pub fn build_pyramid_meta_layout(
104    dict: &Dictionary,
105    triples: &[(u32, u32, u32)],
106    budget: usize,
107    type_override: Option<&str>,
108    algo: PyramidAlgo,
109    layout: PyramidLayout,
110) -> (Vec<u8>, u16) {
111    // Optional sub-phase timing (set RETE_BUILD_TIMING=1) — the pyramid build is
112    // the dominant cost of a big `rete build`; this shows where inside it.
113    // `Instant::now()` must stay behind the flag: `std::time` is unsupported on
114    // `wasm32-unknown-unknown` and panics ("time not implemented"), so an
115    // unconditional clock read would break every in-browser `build()`.
116    let timing = std::env::var_os("RETE_BUILD_TIMING").is_some();
117    let mut t = timing.then(std::time::Instant::now);
118    let mut lap = |label: &str| {
119        if let Some(t0) = &mut t {
120            eprintln!(
121                "  [pyramid] {label}: {:.0} ms",
122                t0.elapsed().as_secs_f64() * 1000.0
123            );
124            *t0 = std::time::Instant::now();
125        }
126    };
127
128    // The community partition — the only step that differs by algorithm.
129    let louvain = |lap: &mut dyn FnMut(&str)| {
130        let g = project_graph(dict, triples);
131        lap("project_graph");
132        let d = build_dendrogram(&g);
133        lap("build_dendrogram (Louvain)");
134        d
135    };
136    let dend = match algo {
137        PyramidAlgo::Louvain => louvain(&mut lap),
138        PyramidAlgo::Types => {
139            match crate::schema_pyramid::build_type_dendrogram(dict, triples, type_override) {
140                Some(d) => {
141                    lap("build_type_dendrogram");
142                    d
143                }
144                None => {
145                    eprintln!(
146                        "  [pyramid] --pyramid-algo types: no usable rdf:type \
147                         predicate — falling back to louvain"
148                    );
149                    louvain(&mut lap)
150                }
151            }
152        }
153    };
154    let round = choose_round_for_budget(dict, triples, &dend, budget);
155    lap("choose_round_for_budget");
156    let summary = summarize(dict, triples, &dend, round);
157    lap("summarize");
158    // Attach the v2 schema pyramid (the non-exclusive subClassOf DAG + per-level
159    // type rollups + per-level lateral class relations + per-community
160    // descriptors). Empty when the graph has no usable typing, in which case the
161    // encoding stays byte-identical to a v1 pyramid-meta.
162    if layout == PyramidLayout::CommunityOnly {
163        // Descriptors only: the schema pyramid's other fields, the planner
164        // stats, the shapes and the labels have sections of their own.
165        let descriptors = crate::schema_pyramid::build_community_descriptors(
166            dict,
167            triples,
168            &dend,
169            round,
170            type_override,
171        );
172        lap("build_community_descriptors");
173        let meta = PyramidMeta::new(round as u32, summary, &[]).with_schema(
174            Vec::new(),
175            Vec::new(),
176            Vec::new(),
177            descriptors,
178            Vec::new(),
179            Vec::new(),
180            Vec::new(),
181        );
182        let out = (meta.encode(), dend.rounds() as u16);
183        lap("encode");
184        return out;
185    }
186    let sp = crate::schema_pyramid::build_schema_pyramid_with(
187        dict,
188        triples,
189        &dend,
190        round,
191        type_override,
192    );
193    lap("build_schema_pyramid");
194    let predicate_stats = compute_predicate_stats(triples);
195    lap("compute_predicate_stats");
196    let char_sets = compute_char_sets(triples);
197    lap("compute_char_sets");
198    let label_index = compute_label_index(dict, triples);
199    lap("compute_label_index");
200    let meta = PyramidMeta::new(round as u32, summary, &[])
201        .with_schema(
202            sp.class_hierarchy,
203            sp.level_rollups,
204            sp.level_links,
205            sp.descriptors,
206            sp.subclass_cycles,
207            sp.disjoint_pairs,
208            sp.equivalent_pairs,
209        )
210        .with_predicate_stats(predicate_stats)
211        .with_char_sets(char_sets)
212        .with_label_index(label_index);
213    let out = (meta.encode(), dend.rounds() as u16);
214    lap("encode");
215    out
216}
217
218/// The label predicates a [`compute_label_index`] entry can come from — the
219/// common "human-readable name of this subject" terms, angle-bracketed as the
220/// dictionary stores them. Order is irrelevant (we union their ids). The same
221/// list the LABEL_INDEX section's rule L1 uses.
222const LABEL_PREDICATES: &[&str] = crate::label_index::DIRECT_LABEL_PREDICATES;
223
224/// Build the bounded, **legacy** label block of a [`PyramidLayout::Combined`]
225/// pyramid-meta (the LABEL_INDEX section, kind 8, replaces it): the display labels of
226/// the most-connected labeled subjects, sorted by the label's lowercased form.
227/// Ranking keeps autocomplete useful on a huge graph (the prominent entities
228/// survive the bound); a graph with fewer than `MAX_LABELS` labels keeps them
229/// all. Deterministic (degree, then subject id, then label) so builds are
230/// reproducible. O(triples) transient memory, freed before the file is written.
231fn compute_label_index(
232    dict: &Dictionary,
233    triples: &[(u32, u32, u32)],
234) -> Vec<crate::meta::LabelEntry> {
235    use crate::terms::{is_literal, literal_lexical};
236    use std::collections::{HashMap, HashSet};
237    const MAX_LABELS: usize = 8192;
238
239    // Resolve the label predicates that actually occur in this graph.
240    let label_pids: HashSet<u32> = LABEL_PREDICATES
241        .iter()
242        .filter_map(|p| dict.predicate_id(p))
243        .collect();
244    if label_pids.is_empty() {
245        return Vec::new();
246    }
247    // Subject degree (triple count) — the ranking used to bound the index.
248    let mut degree: HashMap<u32, u32> = HashMap::new();
249    for &(s, _p, _o) in triples {
250        *degree.entry(s).or_insert(0) += 1;
251    }
252    // Candidate (subject, label) pairs, deduped on (subject, lowercased label).
253    let mut seen: HashSet<(u32, String)> = HashSet::new();
254    let mut candidates: Vec<(u32, String, u32)> = Vec::new(); // (degree, label, subject)
255    for &(s, p, o) in triples {
256        if !label_pids.contains(&p) {
257            continue;
258        }
259        let Some(term) = dict.object_term(o) else {
260            continue;
261        };
262        if !is_literal(&term) {
263            continue;
264        }
265        let Some(label) = literal_lexical(&term) else {
266            continue;
267        };
268        if label.is_empty() {
269            continue;
270        }
271        if seen.insert((s, label.to_lowercase())) {
272            candidates.push((*degree.get(&s).unwrap_or(&0), label, s));
273        }
274    }
275    // Keep the most-connected entities when over budget: rank by degree desc,
276    // then subject asc, then label asc (deterministic).
277    if candidates.len() > MAX_LABELS {
278        candidates.sort_by(|a, b| {
279            b.0.cmp(&a.0)
280                .then_with(|| a.2.cmp(&b.2))
281                .then_with(|| a.1.cmp(&b.1))
282        });
283        candidates.truncate(MAX_LABELS);
284    }
285    // Final order: by lowercased label (search key), then label, then subject.
286    candidates.sort_by(|a, b| {
287        a.1.to_lowercase()
288            .cmp(&b.1.to_lowercase())
289            .then_with(|| a.1.cmp(&b.1))
290            .then_with(|| a.2.cmp(&b.2))
291    });
292    candidates
293        .into_iter()
294        .map(|(_deg, label, subject)| crate::meta::LabelEntry { label, subject })
295        .collect()
296}
297
298/// Build the default-scope full-text index section (`token → subjects`) over
299/// every literal object of `triples`: tokenize each literal into words and
300/// record the subject that carries it. Empty when the graph has no literals.
301/// This is the pre-0.4.0 index, byte for byte; the builds call
302/// `text_index::build_section` with their scope (`--text-index-graphs`,
303/// `--text-index-include`/`-exclude`). O(literal bytes) transient. The token
304/// table is compressed with `writer_codec` (the reader decompresses with
305/// `block_codec`).
306pub fn compute_text_index(dict: &Dictionary, triples: &[(u32, u32, u32)]) -> Vec<u8> {
307    crate::text_index::build_section(
308        dict,
309        &crate::text_index::TextIndexScope::default(),
310        triples,
311        std::iter::empty(),
312        writer_codec(),
313    )
314}
315
316/// The top entity **shapes** (characteristic sets): group subjects by the exact
317/// set of predicates they carry, keep the most common. Bounded to `MAX_CHAR_SETS`
318/// and sorted deterministically (by subject count, then predicate list) so the
319/// encoding is reproducible. O(triples) transient memory.
320fn compute_char_sets(triples: &[(u32, u32, u32)]) -> Vec<crate::meta::CharSet> {
321    use std::collections::{BTreeSet, HashMap};
322    const MAX_CHAR_SETS: usize = 128;
323    let mut by_subject: HashMap<u32, BTreeSet<u32>> = HashMap::new();
324    for &(s, p, _o) in triples {
325        by_subject.entry(s).or_default().insert(p);
326    }
327    let mut shapes: HashMap<Vec<u32>, u64> = HashMap::new();
328    for set in by_subject.into_values() {
329        *shapes.entry(set.into_iter().collect()).or_insert(0) += 1;
330    }
331    let mut v: Vec<crate::meta::CharSet> = shapes
332        .into_iter()
333        .map(|(predicates, subjects)| crate::meta::CharSet {
334            predicates,
335            subjects,
336        })
337        .collect();
338    v.sort_by(|a, b| {
339        b.subjects
340            .cmp(&a.subjects)
341            .then_with(|| a.predicates.cmp(&b.predicates))
342    });
343    v.truncate(MAX_CHAR_SETS);
344    v
345}
346
347/// Per-predicate cardinality for the cost-based planner, in one pass over the
348/// triples (deduped, so a per-(subject,predicate) count is its distinct-object
349/// count). Returned sorted by predicate id for a reproducible encoding. Holds a
350/// transient `(subject -> count, object -> count)` map per predicate — O(triples)
351/// memory, freed before the file is written.
352fn compute_predicate_stats(triples: &[(u32, u32, u32)]) -> Vec<crate::meta::PredStat> {
353    use std::collections::HashMap;
354    #[allow(clippy::type_complexity)]
355    let mut acc: HashMap<u32, (HashMap<u32, u32>, HashMap<u32, u32>, u64)> = HashMap::new();
356    for &(s, p, o) in triples {
357        let e = acc.entry(p).or_default();
358        *e.0.entry(s).or_insert(0) += 1;
359        *e.1.entry(o).or_insert(0) += 1;
360        e.2 += 1;
361    }
362    let mut stats: Vec<crate::meta::PredStat> = acc
363        .into_iter()
364        .map(|(predicate, (subj, obj, count))| crate::meta::PredStat {
365            predicate,
366            count,
367            distinct_subjects: subj.len() as u64,
368            distinct_objects: obj.len() as u64,
369            max_objects_per_subject: subj.values().copied().max().unwrap_or(0),
370            max_subjects_per_object: obj.values().copied().max().unwrap_or(0),
371        })
372        .collect();
373    stats.sort_by_key(|p| p.predicate);
374    stats
375}
376
377/// No compression.
378pub const CODEC_NONE: u8 = 0;
379/// zstd compression (per section).
380pub const CODEC_ZSTD: u8 = 1;
381
382#[derive(Debug, thiserror::Error)]
383#[non_exhaustive]
384pub enum FileError {
385    #[error("header: {0}")]
386    Header(#[from] crate::header::HeaderError),
387    #[error("malformed container: {0}")]
388    Container(&'static str),
389    #[error("unknown codec: {0}")]
390    UnknownCodec(u8),
391    #[error("decompression failed: {0}")]
392    Decompress(std::io::Error),
393    #[error("io: {0}")]
394    Io(#[from] std::io::Error),
395}
396
397/// The codec the writer uses: zstd when the `compression` feature is on, else
398/// none. Reading honors whatever codec the header records (when supported).
399pub fn writer_codec() -> u8 {
400    if cfg!(feature = "compression") {
401        CODEC_ZSTD
402    } else {
403        CODEC_NONE
404    }
405}
406
407/// Intersection of two ascending-sorted, deduped id lists — the AND of two
408/// posting lists in a multi-word text search. Linear merge, output sorted.
409fn intersect_sorted(a: &[u32], b: &[u32]) -> Vec<u32> {
410    let mut out = Vec::with_capacity(a.len().min(b.len()));
411    let (mut i, mut j) = (0, 0);
412    while i < a.len() && j < b.len() {
413        match a[i].cmp(&b[j]) {
414            std::cmp::Ordering::Less => i += 1,
415            std::cmp::Ordering::Greater => j += 1,
416            std::cmp::Ordering::Equal => {
417                out.push(a[i]);
418                i += 1;
419                j += 1;
420            }
421        }
422    }
423    out
424}
425
426pub(crate) fn compress(codec: u8, bytes: &[u8]) -> Vec<u8> {
427    match codec {
428        // Level and frame shape are writer knobs (`crate::write_opts`); any
429        // combination decodes with every conformant zstd decoder.
430        #[cfg(feature = "compression")]
431        CODEC_ZSTD => {
432            let level = crate::write_opts::zstd_level();
433            match crate::write_opts::frame_shape() {
434                crate::write_opts::FrameShape::Stream => zstd::encode_all(bytes, level),
435                crate::write_opts::FrameShape::Sized => zstd::bulk::compress(bytes, level),
436            }
437            .expect("zstd encode is infallible in-memory")
438        }
439        _ => bytes.to_vec(),
440    }
441}
442
443/// The streaming twin of [`compress`]: the same codec, level and frame shape,
444/// read from `src` (exactly `src_len` bytes) and written to `dst`, for a
445/// payload the memory-bounded build keeps on disk (the TEXT_INDEX token
446/// table). A streaming frame is what `zstd::encode_all` writes, through the
447/// same `Encoder`. A sized frame is told the source size up front
448/// (`set_pledged_src_size`), so it picks the same small-input parameters and
449/// records the same content size as `zstd::bulk::compress`, without holding
450/// the payload. The encoder cuts blocks from its own input buffer, not from
451/// how the input was handed to it, so how `src` hands out its bytes never
452/// changes the output.
453///
454/// It equals [`compress`]'s one-shot output only while the payload fits the
455/// level's window (4 MiB at level 9): past it the streaming encoder's ring
456/// buffer wraps and it finds slightly different matches. A payload that one
457/// writer streams and another holds whole must therefore go through
458/// [`compress_streamed`] on the in-memory side — the token table does.
459/// `text_index_spill` pins both facts.
460#[cfg(not(target_arch = "wasm32"))]
461pub(crate) fn compress_to(
462    codec: u8,
463    src_len: u64,
464    src: &mut dyn std::io::Read,
465    dst: &mut dyn std::io::Write,
466) -> std::io::Result<()> {
467    match codec {
468        #[cfg(feature = "compression")]
469        CODEC_ZSTD => zstd_stream(src_len, src, dst),
470        _ => {
471            let _ = src_len;
472            std::io::copy(src, dst).map(|_| ())
473        }
474    }
475}
476
477/// [`compress`] through the streaming encoder: byte for byte what
478/// [`compress_to`] writes for the same payload, whatever its size. For the
479/// in-memory writer of a payload the memory-bounded build compresses from
480/// disk (the TEXT_INDEX token table), so the two builds write the same bytes
481/// past the zstd window too. Below it this is [`compress`]'s output.
482pub(crate) fn compress_streamed(codec: u8, bytes: &[u8]) -> Vec<u8> {
483    match codec {
484        #[cfg(feature = "compression")]
485        CODEC_ZSTD => {
486            let mut out = Vec::with_capacity(bytes.len() / 4 + 64);
487            zstd_stream(bytes.len() as u64, &mut &bytes[..], &mut out)
488                .expect("zstd encode is infallible in-memory");
489            out
490        }
491        _ => bytes.to_vec(),
492    }
493}
494
495/// The streaming zstd encoder at the writer's level and frame shape.
496#[cfg(feature = "compression")]
497fn zstd_stream(
498    src_len: u64,
499    src: &mut dyn std::io::Read,
500    dst: &mut dyn std::io::Write,
501) -> std::io::Result<()> {
502    let mut enc = zstd::stream::Encoder::new(dst, crate::write_opts::zstd_level())?;
503    if crate::write_opts::frame_shape() == crate::write_opts::FrameShape::Sized {
504        enc.set_pledged_src_size(Some(src_len))?;
505        enc.include_contentsize(true)?;
506    }
507    let copied = std::io::copy(src, &mut enc)?;
508    if copied != src_len {
509        return Err(std::io::Error::other(format!(
510            "zstd stream: {copied} bytes read, {src_len} announced"
511        )));
512    }
513    enc.finish()?;
514    Ok(())
515}
516
517pub(crate) fn decompress(codec: u8, bytes: &[u8]) -> Result<Vec<u8>, FileError> {
518    match codec {
519        CODEC_NONE => Ok(bytes.to_vec()),
520        // Native (the `compression` feature — always on for the CLI, off for
521        // wasm) decodes through the C `zstd` library, the same one
522        // [`compress`] encodes with. It is roughly 2× the pure-Rust decoder
523        // and speeds every read path, not just export; the multi-pass tight
524        // budget of a bounded export (`--memory-budget-mb`) re-decodes the
525        // dictionary, so a faster decoder is the single largest lever there.
526        // A valid zstd frame decodes to the same bytes under any conformant
527        // decoder, so the output is byte-for-byte what `ruzstd` produced.
528        #[cfg(feature = "compression")]
529        CODEC_ZSTD => zstd::decode_all(bytes).map_err(FileError::Decompress),
530        // Pure-Rust decode so a target WITHOUT a C toolchain (wasm, or a
531        // `--no-default-features` build) can still read compressed files.
532        // This is the ONLY decode path wasm ever compiles — the C `zstd`
533        // crate is gated behind `compression`, which wasm never enables.
534        #[cfg(not(feature = "compression"))]
535        CODEC_ZSTD => {
536            use std::io::Read;
537            let mut dec = ruzstd::decoding::StreamingDecoder::new(bytes)
538                .map_err(|e| FileError::Decompress(std::io::Error::other(e.to_string())))?;
539            let mut out = Vec::new();
540            dec.read_to_end(&mut out).map_err(FileError::Decompress)?;
541            Ok(out)
542        }
543        other => Err(FileError::UnknownCodec(other)),
544    }
545}
546
547/// The `Frame_Content_Size` a zstd frame header declares (RFC 8878 §3.1.1.1),
548/// or `None` when the frame does not state it (or `bytes` is not a zstd
549/// frame). Reads the header only.
550pub(crate) fn zstd_frame_content_size(b: &[u8]) -> Option<u64> {
551    if b.len() < 5 || b[..4] != [0x28, 0xB5, 0x2F, 0xFD] {
552        return None;
553    }
554    let fhd = b[4];
555    let fcs_flag = fhd >> 6;
556    let single_segment = fhd & 0x20 != 0;
557    let did_size = [0usize, 1, 2, 4][(fhd & 0x03) as usize];
558    let mut pos = 5 + usize::from(!single_segment) + did_size;
559    let fcs_size = match (fcs_flag, single_segment) {
560        (0, false) => return None,
561        (0, true) => 1,
562        (1, _) => 2,
563        (2, _) => 4,
564        _ => 8,
565    };
566    let f = b.get(pos..pos + fcs_size)?;
567    pos += fcs_size;
568    let _ = pos;
569    let mut v = 0u64;
570    for (i, x) in f.iter().enumerate() {
571        v |= (*x as u64) << (8 * i);
572    }
573    Some(if fcs_size == 2 { v + 256 } else { v })
574}
575
576/// [`decompress`] with the output bounded by `max` bytes: a frame whose header
577/// declares more is refused before any decoding, and a frame that does not
578/// declare its size is decoded at most `max + 1` bytes far and refused past
579/// `max`. Format `0x06` records each section's largest block, so a corrupt or
580/// hostile frame cannot make a reader allocate more than the writer did.
581pub(crate) fn decompress_bounded(codec: u8, bytes: &[u8], max: u64) -> Result<Vec<u8>, FileError> {
582    let too_big = |n: u64| {
583        FileError::Decompress(std::io::Error::other(format!(
584            "a block of {n} bytes exceeds its section's {max}-byte maximum"
585        )))
586    };
587    match codec {
588        CODEC_NONE => {
589            if bytes.len() as u64 > max {
590                return Err(too_big(bytes.len() as u64));
591            }
592            Ok(bytes.to_vec())
593        }
594        CODEC_ZSTD => {
595            let declared = zstd_frame_content_size(bytes);
596            if let Some(n) = declared.filter(|&n| n > max) {
597                return Err(too_big(n));
598            }
599            #[cfg(feature = "compression")]
600            {
601                if let Some(n) = declared {
602                    // One-shot into exactly the declared size: a frame that
603                    // lies about it fails here instead of growing the buffer.
604                    return zstd::bulk::decompress(bytes, n as usize)
605                        .map_err(FileError::Decompress);
606                }
607                use std::io::Read;
608                let mut out = Vec::new();
609                let mut dec = zstd::stream::read::Decoder::with_buffer(bytes)
610                    .map_err(FileError::Decompress)?;
611                dec.window_log_max(27).map_err(FileError::Decompress)?;
612                dec.take(max.saturating_add(1))
613                    .read_to_end(&mut out)
614                    .map_err(FileError::Decompress)?;
615                if out.len() as u64 > max {
616                    return Err(too_big(out.len() as u64));
617                }
618                Ok(out)
619            }
620            #[cfg(not(feature = "compression"))]
621            {
622                use std::io::Read;
623                let dec = ruzstd::decoding::StreamingDecoder::new(bytes)
624                    .map_err(|e| FileError::Decompress(std::io::Error::other(e.to_string())))?;
625                let mut out = Vec::new();
626                dec.take(max.saturating_add(1))
627                    .read_to_end(&mut out)
628                    .map_err(FileError::Decompress)?;
629                if out.len() as u64 > max {
630                    return Err(too_big(out.len() as u64));
631                }
632                Ok(out)
633            }
634        }
635        other => Err(FileError::UnknownCodec(other)),
636    }
637}
638
639/// The decompressed length of one stored block — from its frame header when
640/// it states it, else by decoding it (bounded by `ceiling`). What the `0x06`
641/// writer records as a section's largest block.
642pub(crate) fn block_raw_len(codec: u8, bytes: &[u8], ceiling: u64) -> Result<u64, FileError> {
643    match codec {
644        CODEC_NONE => Ok(bytes.len() as u64),
645        CODEC_ZSTD => match zstd_frame_content_size(bytes) {
646            Some(n) => Ok(n),
647            None => Ok(decompress_bounded(codec, bytes, ceiling)?.len() as u64),
648        },
649        other => Err(FileError::UnknownCodec(other)),
650    }
651}
652
653/// Bytes this close are cheaper fetched as one read than as two round trips:
654/// tiles are laid back-to-back so this only ever bridges a tile already made
655/// resident by an earlier window — keep it tight to avoid re-fetching it.
656const TILE_COALESCE_GAP: u64 = 4096;
657
658/// The dictionary chunks a query's output terms touch are scattered across the
659/// section (terms are sorted, output ids are not), so byte-adjacency is rare.
660/// A wider gap trades a little over-fetch for far fewer round trips — the right
661/// call on a latency-bound remote read, where one skipped 64 KiB chunk is much
662/// cheaper than another request's RTT.
663const DICT_COALESCE_GAP: u64 = 64 * 1024;
664
665/// The default `rete export --memory-budget-mb` when the flag is omitted — the
666/// same number `rete build`/`merge` default to, so one figure governs both the
667/// build and the export of a file. Comfortably above a sub-4-GB-dictionary
668/// file's decompressed dictionary (those export in a single pass at today's
669/// speed), and far under a large VM's RAM so a 50-GB-class file is bounded
670/// rather than OOM. See [`split_memory_budget`].
671pub const DEFAULT_EXPORT_BUDGET_MB: u64 = 4096;
672
673/// The largest default memory budget, in MiB, of a lazily-opened file — the
674/// bound on its dictionary chunk cache, index tile cache and raw block cache,
675/// split as [`split_memory_budget`] describes. The default actually applied is
676/// sized from the memory the process can still use, half of it, between
677/// [`crate::sysmem::MIN_DEFAULT_QUERY_BUDGET_MB`] and this figure (see
678/// [`crate::sysmem::default_query_budget_mb`]; this figure whenever the memory
679/// cannot be read). `rete sparql --memory-budget-mb` (and the other query
680/// commands) default to it too, and so does every client that opens through
681/// [`Rete::open_ranged_lazy`] (Python, R, Java, the relay), so a long-lived
682/// session no longer grows with every tile a query ever touched. The same
683/// figure as [`DEFAULT_EXPORT_BUDGET_MB`].
684pub const DEFAULT_QUERY_BUDGET_MB: u64 = DEFAULT_EXPORT_BUDGET_MB;
685
686/// The [`Rete::spill_threshold`] a memory budget implies: an eighth of it. A
687/// spilling `COUNT(DISTINCT …)` buffer can briefly occupy about twice its
688/// threshold while it sorts, so this keeps it within a quarter of the budget.
689pub fn spill_threshold_for(budget: u64) -> u64 {
690    budget / 8
691}
692
693/// The lowest byte cap the dictionary chunk cache is ever given, whatever the
694/// budget. It only has to guarantee forward progress and hold a handful of
695/// chunks (a single front-coded run can dwarf the 64 KiB chunk budget — a
696/// base64-embedded image, say). Correctness never depends on it: the cache's
697/// `Arc` handout keeps a chunk alive across its one decode even if it is
698/// evicted the instant it is inserted, so a full dump completes at *any* cap.
699/// This floor only keeps a pathologically small `--memory-budget-mb` from
700/// thrashing the whole dictionary chunk-by-chunk.
701const MIN_DICT_CACHE_CAP: u64 = 64 << 20; // 64 MiB
702
703/// Upper bound of the budget share [`split_memory_budget`] holds back for
704/// transient, uncached allocations (see the `reserve` there).
705const TRANSIENT_RESERVE_MAX: u64 = 128 << 20; // 128 MiB
706
707/// How a total `--memory-budget-mb` splits across the export's three bounded
708/// consumers. The three caps sum to the total less a small reserve (≤ 128 MiB)
709/// for the window's buffers and one window's in-flight reads, which no cache
710/// owns. [`Rete::set_memory_budget`] further subtracts the open's fixed
711/// directories ([`Rete::fixed_overhead`]) from the dictionary share, so the
712/// live heap of a dump stays within the total.
713#[derive(Debug, Clone, Copy)]
714pub struct MemoryBudget {
715    /// Raw file-block LRU (the `BlockCacheReader` that backs the ranged reader).
716    pub block_cache: u64,
717    /// Decompressed dictionary chunk bodies (the old ~4.3 GB export floor).
718    pub dict_cache: u64,
719    /// Decoded index tiles of the scanned permutation.
720    pub tile_cache: u64,
721}
722
723/// Split a total byte budget across the block cache, the dictionary chunk cache
724/// and the index tile cache (bounded-export phase 2, plan §5.2). `u64::MAX`
725/// (unlimited, e.g. `--in-memory` or `--memory-budget-mb 0`) passes straight
726/// through to every consumer.
727///
728/// The dictionary is the dominant consumer on scholar files (literal-heavy
729/// object section), and its chunks recur across windows, so it gets the bulk of
730/// the budget; the tile cache is small (a scanned permutation is far smaller
731/// than the dictionary) and the block cache stays modest, capped like every
732/// other ranged reader at [`crate::block_cache::DEFAULT_CACHE_CAP`].
733pub fn split_memory_budget(total: u64) -> MemoryBudget {
734    if total == u64::MAX {
735        return MemoryBudget {
736            block_cache: u64::MAX,
737            dict_cache: u64::MAX,
738            tile_cache: u64::MAX,
739        };
740    }
741    // Raw file blocks: min(256 MiB, total/8). Repeated directory/header reads
742    // must stay cheap, but this cache is not where the floor lived.
743    let block_cache = (total / 8).min(crate::block_cache::DEFAULT_CACHE_CAP);
744    // Scanned-permutation tiles: modest — tens of MB on epfl-class files. Cap
745    // at total/16 (≤ 256 MiB) so the dictionary keeps the rest.
746    let tile_cache = (total / 16).min(256 << 20);
747    // Held back for what no cache owns: the window's id and string buffers,
748    // the coalesced read spans and freshly decoded bodies of one window's
749    // prefetch before they enter the cache, and the program itself. Measured at
750    // ~80-120 MB on epfl and openaire-relation (dev/export-memory), so 1/32 of
751    // the budget, capped at 128 MiB.
752    let reserve = (total / 32).min(TRANSIENT_RESERVE_MAX);
753    // The dictionary chunk cache gets everything else, floored so a tiny budget
754    // still holds a working set of chunks.
755    let dict_cache = total
756        .saturating_sub(block_cache)
757        .saturating_sub(tile_cache)
758        .saturating_sub(reserve)
759        .max(MIN_DICT_CACHE_CAP);
760    MemoryBudget {
761        block_cache,
762        dict_cache,
763        tile_cache,
764    }
765}
766
767/// Heap an open `Rete` holds outside its bounded caches, by structure — see
768/// [`Rete::fixed_overhead`].
769#[derive(Debug, Clone, Copy, Default)]
770pub struct FixedOverhead {
771    /// The four dictionary sections' chunk directories.
772    pub dict_directory: u64,
773    /// The default graph's tile directories (every carried permutation).
774    pub index_directory: u64,
775    /// The named-graph directory plus the open graphs' tile directories.
776    pub named_graphs: u64,
777}
778
779impl FixedOverhead {
780    /// The sum of every part.
781    pub fn total(&self) -> u64 {
782        self.dict_directory + self.index_directory + self.named_graphs
783    }
784}
785
786/// Format a cache byte cap for `RETE_OPEN_DEBUG` (`unlimited` for `u64::MAX`).
787fn fmt_cap(cap: u64) -> String {
788    if cap == u64::MAX {
789        "unlimited".to_string()
790    } else {
791        format!("{}MiB", cap >> 20)
792    }
793}
794
795/// Fetch a set of ascending, disjoint byte ranges, coalescing ranges whose gap
796/// is at most `gap` into one span, then fetching the spans through
797/// [`RangeReader::read_many`] (which a parallelizable reader issues
798/// concurrently). Returns each requested range's bytes in order; `None` if any
799/// read fails.
800pub(crate) fn read_coalesced<R: RangeReader + ?Sized>(
801    reader: &R,
802    ranges: &[ByteRange],
803    gap: u64,
804) -> Option<Vec<Vec<u8>>> {
805    coalesced(reader, ranges, gap, false)
806}
807
808/// [`read_coalesced`] for ranges known to be scattered (a probe window's
809/// tiles): the spans go through [`RangeReader::read_many_scattered`], so the
810/// reader asks for them as they are.
811pub(crate) fn read_coalesced_scattered<R: RangeReader + ?Sized>(
812    reader: &R,
813    ranges: &[ByteRange],
814    gap: u64,
815) -> Option<Vec<Vec<u8>>> {
816    coalesced(reader, ranges, gap, true)
817}
818
819fn coalesced<R: RangeReader + ?Sized>(
820    reader: &R,
821    ranges: &[ByteRange],
822    gap: u64,
823    scattered: bool,
824) -> Option<Vec<Vec<u8>>> {
825    // Build the coalesced spans and remember which span each input range maps
826    // into, so the fetched span blobs can be sliced back apart in order.
827    let mut spans: Vec<(u64, u64)> = Vec::new();
828    let mut span_of: Vec<usize> = Vec::with_capacity(ranges.len());
829    let mut i = 0;
830    while i < ranges.len() {
831        let start = ranges[i].offset;
832        let mut end = ranges[i].offset.checked_add(ranges[i].len)?;
833        let mut j = i + 1;
834        while j < ranges.len() {
835            let r = &ranges[j];
836            if r.offset < end || r.offset - end > gap {
837                break;
838            }
839            end = r.offset.checked_add(r.len)?;
840            j += 1;
841        }
842        let si = spans.len();
843        spans.push((start, end - start));
844        for _ in i..j {
845            span_of.push(si);
846        }
847        i = j;
848    }
849    let blobs = if scattered {
850        reader.read_many_scattered(&spans)
851    } else {
852        reader.read_many(&spans)
853    }
854    .ok()?;
855    if blobs.len() != spans.len() {
856        return None;
857    }
858    let mut out = Vec::with_capacity(ranges.len());
859    for (k, r) in ranges.iter().enumerate() {
860        let (span_start, _) = spans[span_of[k]];
861        let blob = &blobs[span_of[k]];
862        out.push(checked_slice(blob, r.offset - span_start, r.len)?.to_vec());
863    }
864    Some(out)
865}
866
867/// Content hash (first 16 bytes of blake3) over the file payload sections.
868/// Identifies the immutable content independent of the header.
869fn content_hash(parts: &[&[u8]]) -> [u8; 16] {
870    let mut h = blake3::Hasher::new();
871    for p in parts {
872        h.update(p);
873    }
874    let mut out = [0u8; 16];
875    out.copy_from_slice(&h.finalize().as_bytes()[..16]);
876    out
877}
878
879/// A resolved triple as terms.
880pub type TermTriple = (String, String, String);
881
882/// What a filtered dump will cost, before it runs — see
883/// [`Rete::dump_plan`](crate::Rete::dump_plan).
884#[derive(Debug, Clone, Copy, PartialEq, Eq)]
885pub struct DumpPlan {
886    /// The routed scan this dump will run. `None` when it provably matches
887    /// nothing without touching the index: an absent graph IRI, or a bound term
888    /// the dictionary does not contain.
889    pub scan: Option<crate::index::ScanPlan>,
890    /// The file's whole dictionary section — the **ceiling** on what resolving
891    /// the matched rows' terms can fault, and on a literal-heavy file the
892    /// dominant term in a dump's real cost.
893    ///
894    /// It is deliberately not an estimate. A dump that matches one row faults
895    /// only the chunks that row's three terms live in, but nothing in the tile
896    /// directories says which chunks those are or how big they are, and a
897    /// preview that guessed would be worse than one that states the bound.
898    pub dictionary_bytes: u64,
899}
900
901/// One labelled byte region of a `.rete` file image (see
902/// [`Rete::file_layout`]). `kind` is a stable machine tag: `header`,
903/// `metadata`, `dictionary`, `directory`, `tile`, `pyramid`, `named-graphs`.
904#[derive(Debug, Clone)]
905pub struct LayoutSegment {
906    pub kind: &'static str,
907    pub label: String,
908    pub offset: u64,
909    pub len: u64,
910}
911
912/// A byte range in the `.rete` file image.
913#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
914pub struct ByteRange {
915    pub offset: u64,
916    pub len: u64,
917}
918
919impl ByteRange {
920    pub fn end(self) -> u64 {
921        self.offset + self.len
922    }
923}
924
925/// Why a triple-pattern result is present in the file.
926#[derive(Debug, Clone, PartialEq, Eq)]
927pub struct TripleProvenance {
928    /// Matched triple resolved to canonical N-Triples tokens.
929    pub terms: TermTriple,
930    /// Matched triple in dictionary ID space.
931    pub ids: Triple,
932    /// Named graph IRI, or `None` for the default graph.
933    pub graph: Option<String>,
934    /// The resolved ID-space pattern that was matched.
935    pub matched_pattern: Pattern,
936    /// Permutation selected to answer the pattern.
937    pub index_permutation: IndexPermutation,
938    /// File byte range containing the dictionary container.
939    pub dictionary_range: ByteRange,
940    /// File byte range containing the permutation index container.
941    pub index_range: ByteRange,
942    /// File byte range containing the selected permutation payload inside the
943    /// index container.
944    pub index_section_range: ByteRange,
945    /// File byte range containing the pyramid metadata, when present.
946    pub pyramid_range: Option<ByteRange>,
947    /// Physical tile identifier, once tile directories are materialized.
948    /// Physical tile identifier (`PERM/index`, e.g. `POS/3`) for tiled (v0.2)
949    /// files; `None` for pre-tiling files.
950    pub tile: Option<String>,
951    /// File byte range of that (compressed) tile — the exact bytes a ranged
952    /// client would fetch to re-derive this match.
953    pub tile_range: Option<ByteRange>,
954}
955
956/// Encode a length-prefixed container of byte sections, each compressed with
957/// `codec` independently (so a range-reading client decompresses only the
958/// sections it fetches). Stored length is the *compressed* length.
959fn encode_container(sections: &[&[u8]], codec: u8) -> Vec<u8> {
960    let mut out = Vec::new();
961    write_uvarint(&mut out, sections.len() as u64);
962    for s in sections {
963        let payload = compress(codec, s);
964        write_uvarint(&mut out, payload.len() as u64);
965        out.extend_from_slice(&payload);
966    }
967    out
968}
969
970/// Decode a container into owned, decompressed sections.
971fn decode_container(bytes: &[u8], codec: u8) -> Result<Vec<Vec<u8>>, FileError> {
972    let (n, mut pos) = read_uvarint(bytes).ok_or(FileError::Container("truncated count"))?;
973    // `n` is untrusted; each section needs ≥1 byte, so cap the pre-allocation at
974    // the buffer length rather than trusting the count (avoids an OOM on a bogus
975    // header pointing at a small region).
976    let n = wire_usize(n, "section count too large")?;
977    let mut out = Vec::with_capacity(n.min(bytes.len()));
978    for _ in 0..n {
979        let (len, used) = read_uvarint(bytes.get(pos..).unwrap_or(&[]))
980            .ok_or(FileError::Container("truncated length"))?;
981        pos += used;
982        // Checked: a hostile length near u64::MAX used to overflow `pos + len`
983        // (a panic), and on wasm32 `len as usize` kept the low 32 bits.
984        let end = pos
985            .checked_add(wire_usize(len, "section length too large")?)
986            .filter(|&end| end <= bytes.len())
987            .ok_or(FileError::Container("section overruns buffer"))?;
988        out.push(decompress(codec, &bytes[pos..end])?);
989        pos = end;
990    }
991    Ok(out)
992}
993
994fn checked_end(off: u64, len: u64) -> Result<u64, FileError> {
995    off.checked_add(len)
996        .ok_or(FileError::Container("section range overflows"))
997}
998
999/// A length or count read from the file, as an in-memory `usize` — or a clean
1000/// error. `as usize` truncates on wasm32, a 32-bit target: a length of
1001/// `2^32 + 5` read 5 bytes there and decoding went on as if nothing were wrong.
1002fn wire_usize(v: u64, what: &'static str) -> Result<usize, FileError> {
1003    usize::try_from(v).map_err(|_| FileError::Container(what))
1004}
1005
1006/// The in-memory slice `[off, off + len)` of `bytes`, or `None` when the range
1007/// does not fit this target's `usize` or overruns the buffer.
1008fn checked_slice(bytes: &[u8], off: u64, len: u64) -> Option<&[u8]> {
1009    let start = usize::try_from(off).ok()?;
1010    let end = start.checked_add(usize::try_from(len).ok()?)?;
1011    bytes.get(start..end)
1012}
1013
1014/// Per-chunk budget for dictionary section bodies — same reasoning as
1015/// [`crate::index::INDEX_TILE_BUDGET`]: one fetch, one decompress per touch.
1016const DICT_CHUNK_BUDGET: usize = crate::write_opts::DEFAULT_DICT_CHUNK_BYTES;
1017
1018/// Encode one dictionary section as a chunked payload (format v0.2):
1019/// `[header_len, raw header (term_count/interval/restart table)]
1020///  [num_chunks; per chunk: Δfirst_run, key_len, key, comp_len]
1021///  [individually compressed run-aligned body slices]`.
1022/// The header keeps its original encoding, so restart offsets stay valid in
1023/// the section's coordinate space.
1024///
1025/// `key` is the chunk's **routing separator**, not its first term: the
1026/// shortest string strictly above the previous chunk's last term and at most
1027/// the chunk's own first term (empty for chunk 0). See
1028/// [`crate::dict::SectionChunk::key`] for the invariant and
1029/// [`crate::dict::shortest_separator`] for the choice. Storing the first term
1030/// verbatim — what this wrote before — is the degenerate case of the same
1031/// invariant, so readers of either vintage route both correctly; the
1032/// separator is simply orders of magnitude smaller when the boundary term is a
1033/// long literal.
1034fn encode_chunked_dict_section(raw: &[u8], codec: u8) -> Vec<u8> {
1035    let meta = crate::dict::parse_meta(raw).unwrap_or(crate::dict::SectionMeta {
1036        term_count: 0,
1037        restart_interval: 1,
1038        restart_offsets: Vec::new(),
1039    });
1040    let body_start = meta
1041        .restart_offsets
1042        .first()
1043        .copied()
1044        .unwrap_or(raw.len() as u64);
1045    let header = &raw[..(body_start.min(raw.len() as u64)) as usize];
1046
1047    // Split runs into chunks by body-byte budget (whole runs only).
1048    let chunk_budget = crate::write_opts::dict_chunk_budget() as u64;
1049    let n_runs = meta.restart_offsets.len();
1050    let mut bounds: Vec<(usize, u64, u64)> = Vec::new(); // (first_run, start, end)
1051    let mut r = 0;
1052    while r < n_runs {
1053        let start = meta.restart_offsets[r];
1054        let mut r2 = r + 1;
1055        while r2 < n_runs && meta.restart_offsets[r2] - start < chunk_budget {
1056            r2 += 1;
1057        }
1058        let end = if r2 < n_runs {
1059            meta.restart_offsets[r2]
1060        } else {
1061            raw.len() as u64
1062        };
1063        bounds.push((r, start, end));
1064        r = r2;
1065    }
1066
1067    // Chunks are independent: compress them across all cores, as the index
1068    // tiles are (`par_iter` keeps the order, so the bytes are unchanged). At a
1069    // high `--zstd-level` this serial loop was most of a build's wall time.
1070    #[cfg(feature = "parallel")]
1071    let compressed: Vec<Vec<u8>> = {
1072        use rayon::prelude::*;
1073        bounds
1074            .par_iter()
1075            .map(|&(_, s, e)| compress(codec, &raw[s as usize..e as usize]))
1076            .collect()
1077    };
1078    #[cfg(not(feature = "parallel"))]
1079    let compressed: Vec<Vec<u8>> = bounds
1080        .iter()
1081        .map(|&(_, s, e)| compress(codec, &raw[s as usize..e as usize]))
1082        .collect();
1083    let mut out = Vec::new();
1084    write_uvarint(&mut out, header.len() as u64);
1085    out.extend_from_slice(header);
1086    write_uvarint(&mut out, bounds.len() as u64);
1087    let mut prev_run = 0usize;
1088    // The previous chunk's last term — the separator's lower bound. Decoding it
1089    // walks at most `restart_interval` front-coded entries of one chunk, which
1090    // is nothing beside the zstd pass over the same 64 KiB.
1091    let mut prev_last: Option<Vec<u8>> = None;
1092    for (i, (&(first_run, start, end), comp)) in bounds.iter().zip(&compressed).enumerate() {
1093        let key = if i == 0 {
1094            // Everything routes to chunk 0; it needs no separator at all.
1095            Vec::new()
1096        } else {
1097            let first = crate::dict::run_first_term(raw, start as usize).unwrap_or_default();
1098            match &prev_last {
1099                // Undecodable predecessor: the verbatim first term is always a
1100                // valid separator, just not a short one.
1101                None => first,
1102                Some(pl) => crate::dict::shortest_separator(pl, &first),
1103            }
1104        };
1105        if i + 1 < bounds.len() {
1106            // The last run of THIS chunk starts one run before the next chunk's.
1107            let last_run_off = meta.restart_offsets[bounds[i + 1].0 - 1] as usize;
1108            prev_last = crate::dict::run_last_term(raw, last_run_off, end as usize);
1109        }
1110        write_uvarint(&mut out, (first_run - prev_run) as u64);
1111        write_uvarint(&mut out, key.len() as u64);
1112        out.extend_from_slice(&key);
1113        write_uvarint(&mut out, comp.len() as u64);
1114        prev_run = first_run;
1115    }
1116    for comp in &compressed {
1117        out.extend_from_slice(comp);
1118    }
1119    out
1120}
1121
1122/// A parsed chunked-dict-section directory entry: the chunk's run/key/body
1123/// coordinates plus its compressed byte range *within the payload*. `key` is a
1124/// routing separator, not a term — see [`crate::dict::SectionChunk::key`].
1125pub(crate) struct DictChunkEntry {
1126    pub(crate) first_run: usize,
1127    pub(crate) key: Vec<u8>,
1128    body_start: u64,
1129    pub(crate) start: u64,
1130    pub(crate) end: u64,
1131}
1132
1133/// Parse a chunked dictionary section's header + directory (not the chunks).
1134/// `bytes` may be a prefix of the payload; compressed ranges validate against
1135/// `total_len`.
1136fn parse_chunked_dict_dir(
1137    bytes: &[u8],
1138    total_len: u64,
1139) -> Result<(crate::dict::SectionMeta, Vec<DictChunkEntry>), FileError> {
1140    let mut pos = 0usize;
1141    let take = |pos: &mut usize| -> Result<u64, FileError> {
1142        let (v, n) = read_uvarint(bytes.get(*pos..).unwrap_or(&[]))
1143            .ok_or(FileError::Container("truncated dict chunk directory"))?;
1144        *pos += n;
1145        Ok(v)
1146    };
1147    let header_len = wire_usize(take(&mut pos)?, "dict header too large")?;
1148    let header = bytes
1149        .get(pos..pos.saturating_add(header_len))
1150        .ok_or(FileError::Container("truncated dict header"))?;
1151    let meta = crate::dict::parse_meta(header)
1152        .map_err(|_| FileError::Container("malformed dict header"))?;
1153    pos += header_len;
1154
1155    let num_chunks = wire_usize(take(&mut pos)?, "dict chunk count too large")?;
1156    let mut entries = Vec::with_capacity(num_chunks.min(bytes.len()));
1157    let mut lens = Vec::with_capacity(num_chunks.min(bytes.len()));
1158    let mut prev_run = 0usize;
1159    for i in 0..num_chunks {
1160        let drun = wire_usize(take(&mut pos)?, "dict chunk run too large")?;
1161        let klen = wire_usize(take(&mut pos)?, "dict chunk key too large")?;
1162        let key = bytes
1163            .get(pos..pos.saturating_add(klen))
1164            .ok_or(FileError::Container("truncated dict chunk key"))?
1165            .to_vec();
1166        pos += klen;
1167        let clen = take(&mut pos)?;
1168        let first_run = next_chunk_run(i, prev_run, drun)?;
1169        let body_start = meta
1170            .restart_offsets
1171            .get(first_run)
1172            .copied()
1173            .ok_or(FileError::Container("dict chunk run out of range"))?;
1174        entries.push(DictChunkEntry {
1175            first_run,
1176            key,
1177            body_start,
1178            start: 0,
1179            end: 0,
1180        });
1181        lens.push(clen);
1182        prev_run = first_run;
1183    }
1184    let mut start = pos as u64;
1185    for (e, len) in entries.iter_mut().zip(lens) {
1186        let end = start
1187            .checked_add(len)
1188            .filter(|&e| e <= total_len)
1189            .ok_or(FileError::Container("dict chunk overruns section"))?;
1190        e.start = start;
1191        e.end = end;
1192        start = end;
1193    }
1194    Ok((meta, entries))
1195}
1196
1197/// The `i`-th chunk's first run from the previous chunk's and the stored delta.
1198/// Both writers start chunk 0 at run 0 and give every later chunk at least one
1199/// run, so the runs strictly ascend — which is what routing a term id to its
1200/// chunk (a binary search over `first_run`) relies on. A directory that breaks
1201/// that, or whose sum overflows, is corrupt: refuse it instead of routing ids
1202/// to the wrong chunk.
1203fn next_chunk_run(i: usize, prev_run: usize, drun: usize) -> Result<usize, FileError> {
1204    match (i, drun) {
1205        (0, 0) => Ok(0),
1206        (0, _) => Err(FileError::Container(
1207            "first dict chunk does not start at run 0",
1208        )),
1209        (_, 0) => Err(FileError::Container("dict chunk runs do not ascend")),
1210        _ => prev_run
1211            .checked_add(drun)
1212            .ok_or(FileError::Container("dict chunk run overflows")),
1213    }
1214}
1215
1216/// Fetch and parse a remote chunked dict section's header + directory: read a
1217/// small prefix and grow it geometrically until it parses, never fetching past
1218/// the section.
1219/// Read a chunked dictionary section's directory over a range reader WITHOUT
1220/// materializing the section-wide restart table. That table is one offset per
1221/// restart run — a 50 M-term section has ~3 M of them (~24 MiB resident), and
1222/// holding it is an iOS-Safari OOM on a big remote file. We read only the tiny
1223/// header prefix (term_count / interval) and the chunk directory, skipping the
1224/// restart-table bytes entirely; per-run offsets are derived per chunk on fault
1225/// (`SectionChunk::run_offsets`). The returned meta has an empty
1226/// `restart_offsets`, which the chunked lookups read as "derive per chunk".
1227pub(crate) fn read_dict_dir_ranged<R: RangeReader + ?Sized>(
1228    reader: &R,
1229    section: ByteRange,
1230) -> Result<(crate::dict::SectionMeta, Vec<DictChunkEntry>), FileError> {
1231    let total = section.len;
1232    // Initial prefix: the header prefix ([header_len][term_count][interval]) and,
1233    // for a *small* section, the whole chunk directory too — so those still cost
1234    // a single read. A big section has a huge restart table between the header
1235    // and the directory; we detect that (dir_start past the prefix) and range-
1236    // read only the directory below, never fetching the table.
1237    let init = 8192.min(total); // never over-read past the section (a tiny/empty
1238                                // section holds only its header + a stub directory)
1239    let head = reader.read_at(section.offset, init)?;
1240    let (header_len, n0) =
1241        read_uvarint(&head).ok_or(FileError::Container("truncated dict header len"))?;
1242    let hbase = n0; // first byte of the header body
1243    let (term_count, n1) = read_uvarint(head.get(hbase..).unwrap_or(&[]))
1244        .ok_or(FileError::Container("truncated dict term_count"))?;
1245    let (restart_interval, _n2) = read_uvarint(head.get(hbase + n1..).unwrap_or(&[]))
1246        .ok_or(FileError::Container("truncated dict interval"))?;
1247    if restart_interval == 0 {
1248        return Err(FileError::Container("zero restart interval"));
1249    }
1250    // The chunk directory begins right after the header body — i.e. past the
1251    // `header_len` bytes, which include the restart table we never materialize.
1252    let dir_start = (hbase as u64)
1253        .checked_add(header_len)
1254        .filter(|&d| d <= total)
1255        .ok_or(FileError::Container("dict header overruns section"))?;
1256    let dir_total = total - dir_start;
1257    let meta = crate::dict::SectionMeta {
1258        term_count: u32::try_from(term_count)
1259            .map_err(|_| FileError::Container("dict term count too large"))?,
1260        restart_interval: u32::try_from(restart_interval)
1261            .map_err(|_| FileError::Container("dict restart interval too large"))?,
1262        restart_offsets: Vec::new(),
1263    };
1264    // Runs in the section: what a chunk's first run must stay below.
1265    let num_runs = (meta.term_count as usize).div_ceil(meta.restart_interval as usize);
1266    let finish = |mut entries: Vec<DictChunkEntry>| {
1267        for e in &mut entries {
1268            e.start += dir_start; // dir-relative → section-relative
1269            e.end += dir_start;
1270        }
1271        (meta.clone(), entries)
1272    };
1273    // Fast path: the directory already sits in the prefix we read (small section
1274    // — its restart table is tiny, so the ~few KiB over-read is negligible).
1275    // A short prefix is not wasted: those bytes seed the probe below.
1276    let mut have: Vec<u8> = Vec::new();
1277    if dir_start < head.len() as u64 {
1278        match parse_chunk_dir_only(&head[dir_start as usize..], dir_total, num_runs)? {
1279            ChunkDirParse::Done(entries) => return Ok(finish(entries)),
1280            ChunkDirParse::Truncated { .. } => have = head[dir_start as usize..].to_vec(),
1281        }
1282    }
1283    // Big section: range-read the directory on its own, skipping the table.
1284    //
1285    // Nothing records the directory's byte length, so it has to be probed. Two
1286    // things keep that probe near ONE directory's worth of bytes. Each round
1287    // **appends** to the bytes already held instead of re-reading from the
1288    // start — the previous loop re-read the whole prefix every time, so a
1289    // 234 MB directory (epfl-infoscience's object section) cost 537 MB of range
1290    // reads to fetch. And a truncated parse reports how many entries fit in how
1291    // many bytes, which extrapolates the rest instead of blindly doubling. The
1292    // extrapolation may only quadruple a round, so a wild guess on a directory
1293    // with wildly uneven entries (one stored term can be hundreds of KB) can
1294    // never fetch the section body.
1295    let mut want = 4096u64.min(dir_total).max(1);
1296    loop {
1297        let held = have.len() as u64;
1298        if want > held {
1299            let extra = reader.read_at(section.offset + dir_start + held, want - held)?;
1300            if extra.is_empty() {
1301                return Err(FileError::Container("truncated dict chunk directory"));
1302            }
1303            have.extend_from_slice(&extra);
1304        }
1305        let held = have.len() as u64;
1306        match parse_chunk_dir_only(&have, dir_total, num_runs)? {
1307            ChunkDirParse::Done(entries) => return Ok(finish(entries)),
1308            ChunkDirParse::Truncated { .. } if held >= dir_total => {
1309                return Err(FileError::Container("truncated dict chunk directory"));
1310            }
1311            ChunkDirParse::Truncated {
1312                parsed,
1313                used,
1314                total,
1315            } => {
1316                // Extrapolate from a sample big enough to mean something;
1317                // otherwise just double. Undershooting only costs another
1318                // (append-only) round, so the estimate carries a small margin
1319                // rather than a generous one.
1320                let est = if parsed >= 16 && used > 0 {
1321                    let whole = ((used as u64) / (parsed as u64)).saturating_mul(total as u64);
1322                    whole.saturating_add(whole / 8).saturating_add(64)
1323                } else {
1324                    // Too small a sample to extrapolate from: double.
1325                    held.saturating_mul(2)
1326                };
1327                want = est
1328                    .max(held.saturating_add(1))
1329                    .min(held.saturating_mul(4))
1330                    .min(dir_total);
1331            }
1332        }
1333    }
1334}
1335
1336/// What [`parse_chunk_dir_only`] made of a chunk-directory prefix: either the
1337/// whole directory, or how far a truncated one got (the probe's step hint).
1338/// Truncation is not an error here — the caller is deliberately reading a
1339/// prefix and deciding how much more to fetch.
1340enum ChunkDirParse {
1341    Done(Vec<DictChunkEntry>),
1342    Truncated {
1343        /// Entries fully decoded from the prefix.
1344        parsed: usize,
1345        /// Bytes those entries occupied (directory-relative).
1346        used: usize,
1347        /// Chunk count the directory declares.
1348        total: usize,
1349    },
1350}
1351
1352/// Parse just the chunk directory (the bytes after a section header):
1353/// `[num_chunks][per chunk: Δfirst_run, key_len, key, comp_len]`.
1354/// Chunk byte ranges (`start`/`end`) come back relative to the directory's own
1355/// start; `body_start` is 0 (a lite section never uses it — lookups derive run
1356/// offsets per chunk). Bodies aren't needed here, so `dir` may end at the first
1357/// body as long as it covers the whole directory. A `dir` that stops mid-entry
1358/// is reported as [`ChunkDirParse::Truncated`], not an error: only bytes that
1359/// cannot be a directory at any length (a chunk range overrunning the section,
1360/// chunk runs that do not ascend from 0 or pass `num_runs`) fail.
1361fn parse_chunk_dir_only(
1362    dir: &[u8],
1363    dir_total: u64,
1364    num_runs: usize,
1365) -> Result<ChunkDirParse, FileError> {
1366    let mut pos = 0usize;
1367    let take = |pos: &mut usize| -> Option<u64> {
1368        let (v, n) = read_uvarint(dir.get(*pos..).unwrap_or(&[]))?;
1369        *pos += n;
1370        Some(v)
1371    };
1372    let Some(num_chunks) = take(&mut pos) else {
1373        return Ok(ChunkDirParse::Truncated {
1374            parsed: 0,
1375            used: 0,
1376            total: 0,
1377        });
1378    };
1379    let num_chunks = wire_usize(num_chunks, "dict chunk count too large")?;
1380    let mut entries = Vec::with_capacity(num_chunks.min(dir.len()));
1381    let mut lens: Vec<u64> = Vec::with_capacity(num_chunks.min(dir.len()));
1382    let mut prev_run = 0usize;
1383    for i in 0..num_chunks {
1384        let entry_start = pos;
1385        let short = ChunkDirParse::Truncated {
1386            parsed: entries.len(),
1387            used: entry_start,
1388            total: num_chunks,
1389        };
1390        let (Some(drun), Some(klen)) = (take(&mut pos), take(&mut pos)) else {
1391            return Ok(short);
1392        };
1393        let drun = wire_usize(drun, "dict chunk run too large")?;
1394        let klen = wire_usize(klen, "dict chunk key too large")?;
1395        let Some(key) = dir.get(pos..pos.saturating_add(klen)) else {
1396            return Ok(short);
1397        };
1398        let key = key.to_vec();
1399        pos += klen;
1400        let Some(clen) = take(&mut pos) else {
1401            return Ok(short);
1402        };
1403        let first_run = next_chunk_run(i, prev_run, drun)?;
1404        if first_run >= num_runs {
1405            return Err(FileError::Container("dict chunk run out of range"));
1406        }
1407        entries.push(DictChunkEntry {
1408            first_run,
1409            key,
1410            body_start: 0,
1411            start: 0,
1412            end: 0,
1413        });
1414        lens.push(clen);
1415        prev_run = first_run;
1416    }
1417    let mut start = pos as u64;
1418    for (e, len) in entries.iter_mut().zip(lens) {
1419        let end = start
1420            .checked_add(len)
1421            .filter(|&e| e <= dir_total)
1422            .ok_or(FileError::Container("dict chunk overruns section"))?;
1423        e.start = start;
1424        e.end = end;
1425        start = end;
1426    }
1427    Ok(ChunkDirParse::Done(entries))
1428}
1429
1430/// Decode one chunked dictionary section payload into a resident
1431/// [`crate::dict::ChunkedSection`] (chunks decompressed up front — the local
1432/// open path).
1433fn decode_chunked_dict_section(
1434    payload: &[u8],
1435    codec: u8,
1436    cache: std::sync::Arc<crate::chunk_cache::ChunkCache>,
1437    section_index: u8,
1438) -> Result<crate::dict::ChunkedSection, FileError> {
1439    let (meta, entries) = parse_chunked_dict_dir(payload, payload.len() as u64)?;
1440    let chunks = entries
1441        .into_iter()
1442        .map(|e| {
1443            let body = decompress(codec, &payload[e.start as usize..e.end as usize])?;
1444            Ok((
1445                crate::dict::SectionChunk::new(e.first_run, e.key, e.body_start),
1446                body,
1447            ))
1448        })
1449        .collect::<Result<Vec<_>, FileError>>()?;
1450    Ok(crate::dict::ChunkedSection::resident(
1451        meta,
1452        chunks,
1453        cache,
1454        section_index,
1455    ))
1456}
1457
1458fn decode_dictionary_container(bytes: &[u8], codec: u8) -> Result<Dictionary, FileError> {
1459    let dsecs = decode_container(bytes, CODEC_NONE)?;
1460    if dsecs.len() != 4 {
1461        return Err(FileError::Container("expected 4 dictionary sections"));
1462    }
1463    // The four sections share one (unlimited) cache — residency is identical to
1464    // the previous per-chunk `OnceLock`s.
1465    let cache = crate::chunk_cache::ChunkCache::unlimited_arc();
1466    let mut sections = Vec::with_capacity(4);
1467    for (si, sec) in dsecs.iter().enumerate() {
1468        sections.push(decode_chunked_dict_section(
1469            sec,
1470            codec,
1471            cache.clone(),
1472            si as u8,
1473        )?);
1474    }
1475    let arr: [crate::dict::ChunkedSection; 4] = sections
1476        .try_into()
1477        .map_err(|_| FileError::Container("expected 4 dictionary sections"))?;
1478    Ok(Dictionary::from_chunked_sections(arr))
1479}
1480
1481/// Encode one permutation's tiled section payload (format v0.2):
1482/// `[num_tiles][per tile: delta(min_a), max_a - min_a, compressed_len][tiles…]`,
1483/// each tile compressed independently with `codec` so a ranged reader can
1484/// fetch and decompress exactly the tiles a query routes to. The directory
1485/// itself is uncompressed (it must be readable before any tile).
1486fn encode_tiled_section(index: &GraphIndex, si: usize, codec: u8) -> Vec<u8> {
1487    let tiles = index.tile_sections()[si];
1488    let n = tiles.len();
1489    // Tile bodies live in the index's chunk cache (keyed by (section, tile));
1490    // fetch each as a shared handle. For a locally-built index every tile is
1491    // resident, so this is a cheap `Arc` clone under an uncontended lock.
1492    // Per-tile compression is the bulk of serialization time on a large graph and
1493    // the tiles are independent, so compress them across all cores. `par_iter`
1494    // preserves order, so the output is byte-identical to the serial map.
1495    #[cfg(feature = "parallel")]
1496    let compressed: Vec<Vec<u8>> = {
1497        use rayon::prelude::*;
1498        (0..n)
1499            .into_par_iter()
1500            .map(|ti| compress(codec, &index.tile_body(si, ti)))
1501            .collect()
1502    };
1503    #[cfg(not(feature = "parallel"))]
1504    let compressed: Vec<Vec<u8>> = (0..n)
1505        .map(|ti| compress(codec, &index.tile_body(si, ti)))
1506        .collect();
1507    let mut out = Vec::new();
1508    write_uvarint(&mut out, tiles.len() as u64);
1509    let mut prev_min = 0u32;
1510    for (tile, comp) in tiles.iter().zip(&compressed) {
1511        let (min_a, max_a) = tile.leading_range();
1512        write_uvarint(&mut out, (min_a - prev_min) as u64);
1513        write_uvarint(&mut out, (max_a - min_a) as u64);
1514        write_uvarint(&mut out, comp.len() as u64);
1515        prev_min = min_a;
1516    }
1517    for comp in &compressed {
1518        out.extend_from_slice(comp);
1519    }
1520    // Tile-synopsis trailer (FLAG_TILE_SYNOPSIS): per tile, the inclusive min/max
1521    // of the two non-leading columns `(min_b, span_b, min_c, span_c)`, derived
1522    // from each tile's zone map. Appended **after** the tile payloads so a reader
1523    // that predates the flag — which locates tiles by length and stops — never
1524    // reads it (backward-compatible). A reader honoring the flag reads it from the
1525    // section tail. On the (impossible for a built tile) parse failure, emit a
1526    // full range so nothing is ever wrongly pruned.
1527    for ti in 0..n {
1528        let body = index.tile_body(si, ti);
1529        let (min_b, max_b, min_c, max_c) = match crate::triples::TripleBlock::parse(&body) {
1530            Ok(b) => {
1531                let z = b.zone();
1532                (z.min_b, z.max_b, z.min_c, z.max_c)
1533            }
1534            Err(_) => (0, u32::MAX, 0, u32::MAX),
1535        };
1536        write_uvarint(&mut out, min_b as u64);
1537        write_uvarint(&mut out, (max_b - min_b) as u64);
1538        write_uvarint(&mut out, min_c as u64);
1539        write_uvarint(&mut out, (max_c - min_c) as u64);
1540    }
1541    out
1542}
1543
1544/// A parsed v0.2 tile-directory entry: leading-id range plus the tile's byte
1545/// range *within the section payload*.
1546pub(crate) struct TileDirEntry {
1547    pub(crate) min_a: u32,
1548    pub(crate) max_a: u32,
1549    pub(crate) start: u64,
1550    pub(crate) end: u64,
1551}
1552
1553/// One tile's synopsis: inclusive min/max of the two non-leading columns.
1554type TileSynopsis = (u32, u32, u32, u32);
1555
1556/// Parse the **tile-synopsis trailer** (when [`FLAG_TILE_SYNOPSIS`] is set): the
1557/// `num_tiles × (min_b, span_b, min_c, span_c)` records that follow the last tile
1558/// payload, starting at `trailer_start` within `payload`. Returns one synopsis per
1559/// tile, in directory order. `payload` may be just the trailer slice (remote) or
1560/// the whole section (local); `trailer_start` is the offset of the trailer within
1561/// it. A short/garbled trailer yields `None` (the caller keeps `None` synopses —
1562/// pruning simply doesn't fire, never a wrong result).
1563fn parse_tile_synopsis(
1564    payload: &[u8],
1565    trailer_start: usize,
1566    num_tiles: usize,
1567) -> Option<Vec<TileSynopsis>> {
1568    let mut pos = trailer_start;
1569    let take = |pos: &mut usize| -> Option<u32> {
1570        let (v, n) = read_uvarint(payload.get(*pos..)?)?;
1571        *pos += n;
1572        u32::try_from(v).ok()
1573    };
1574    let mut out = Vec::with_capacity(num_tiles.min(payload.len()));
1575    for _ in 0..num_tiles {
1576        let min_b = take(&mut pos)?;
1577        let max_b = min_b.checked_add(take(&mut pos)?)?;
1578        let min_c = take(&mut pos)?;
1579        let max_c = min_c.checked_add(take(&mut pos)?)?;
1580        out.push((min_b, max_b, min_c, max_c));
1581    }
1582    Some(out)
1583}
1584
1585/// Parse a tiled section payload's directory (not the tiles). `bytes` may be a
1586/// **prefix** of the payload (a ranged reader fetches the directory before any
1587/// tile); tile byte ranges are validated against `total_len`, the full payload
1588/// length. Every length is untrusted.
1589fn parse_tile_directory(bytes: &[u8], total_len: u64) -> Result<Vec<TileDirEntry>, FileError> {
1590    let mut pos = 0usize;
1591    let take = |pos: &mut usize| -> Result<u64, FileError> {
1592        let (v, n) = read_uvarint(bytes.get(*pos..).unwrap_or(&[]))
1593            .ok_or(FileError::Container("truncated tile directory"))?;
1594        *pos += n;
1595        Ok(v)
1596    };
1597    let num_tiles = wire_usize(take(&mut pos)?, "tile count too large")?;
1598    let mut entries = Vec::with_capacity(num_tiles.min(bytes.len()));
1599    let mut prev_min = 0u32;
1600    let mut lens = Vec::with_capacity(num_tiles.min(bytes.len()));
1601    // Leading ids are u32. A delta or span past that, or a sum that leaves
1602    // the id space, is corrupt: `as u32` and `wrapping_add` used to turn it
1603    // into some other, plausible id range and route lookups to the wrong tile.
1604    let id = |v: u64| u32::try_from(v).map_err(|_| FileError::Container("tile id out of range"));
1605    for _ in 0..num_tiles {
1606        let dmin = id(take(&mut pos)?)?;
1607        let span = id(take(&mut pos)?)?;
1608        let len = take(&mut pos)?;
1609        let min_a = prev_min
1610            .checked_add(dmin)
1611            .ok_or(FileError::Container("tile id out of range"))?;
1612        let max_a = min_a
1613            .checked_add(span)
1614            .ok_or(FileError::Container("tile id out of range"))?;
1615        entries.push(TileDirEntry {
1616            min_a,
1617            max_a,
1618            start: 0,
1619            end: 0,
1620        });
1621        lens.push(len);
1622        prev_min = min_a;
1623    }
1624    let mut start = pos as u64;
1625    for (e, len) in entries.iter_mut().zip(lens) {
1626        let end = start
1627            .checked_add(len)
1628            .filter(|&e| e <= total_len)
1629            .ok_or(FileError::Container("tile overruns section"))?;
1630        e.start = start;
1631        e.end = end;
1632        start = end;
1633    }
1634    Ok(entries)
1635}
1636
1637/// Fetch and parse a remote tiled section's directory: read a small prefix and
1638/// grow it geometrically until the directory parses, never fetching past the
1639/// section. A directory that still fails on the whole section is corrupt.
1640pub(crate) fn read_tile_directory_ranged<R: RangeReader + ?Sized>(
1641    reader: &R,
1642    section: ByteRange,
1643) -> Result<Vec<TileDirEntry>, FileError> {
1644    let total = section.len;
1645    let mut prefetch = 4096u64.min(total);
1646    loop {
1647        let prefix = reader.read_at(section.offset, prefetch)?;
1648        match parse_tile_directory(&prefix, total) {
1649            Ok(dir) => return Ok(dir),
1650            Err(_) if prefetch < total => prefetch = prefetch.saturating_mul(2).min(total),
1651            Err(e) => return Err(e),
1652        }
1653    }
1654}
1655
1656/// Fetch and parse a remote section's **tile-synopsis trailer** (only when the
1657/// header's [`FLAG_TILE_SYNOPSIS`] is set): one targeted range read of the bytes
1658/// past the last tile, parsed into one synopsis per tile (directory order). A
1659/// missing/short/garbled trailer degrades to all-`None` — pruning simply doesn't
1660/// fire, never a wrong result. The directory gives the trailer's start (the last
1661/// tile's end).
1662pub(crate) fn read_tile_synopsis_ranged<R: RangeReader + ?Sized>(
1663    reader: &R,
1664    section: ByteRange,
1665    dir: &[TileDirEntry],
1666) -> Vec<Option<TileSynopsis>> {
1667    let n = dir.len();
1668    let none = vec![None; n];
1669    let trailer_start = dir.iter().map(|e| e.end).max().unwrap_or(0);
1670    let total = section.len;
1671    if n == 0 || trailer_start >= total {
1672        return none; // no trailer bytes present
1673    }
1674    // Four varints per tile, each a u32 (at most 5 bytes): never read more,
1675    // whatever the section claims. The trailer is the section's tail, so a
1676    // hostile section length used to make this one read as large as it said.
1677    let max_trailer = (n as u64).saturating_mul(4 * 5);
1678    let trailer_len = (total - trailer_start).min(max_trailer);
1679    let Some(offset) = section.offset.checked_add(trailer_start) else {
1680        return none;
1681    };
1682    let Ok(bytes) = reader.read_at(offset, trailer_len) else {
1683        return none;
1684    };
1685    match parse_tile_synopsis(&bytes, 0, n) {
1686        Some(v) => v.into_iter().map(Some).collect(),
1687        None => none,
1688    }
1689}
1690
1691/// Per-tile absolute file ranges of each permutation section, for provenance.
1692/// A malformed directory yields an empty section (provenance degrades, queries
1693/// are unaffected).
1694fn tile_file_ranges(
1695    index_bytes: &[u8],
1696    container_offset: u64,
1697    section_ranges: &[ByteRange; NUM_PERMS],
1698) -> [Vec<(u32, u32, ByteRange)>; NUM_PERMS] {
1699    let mut out: [Vec<(u32, u32, ByteRange)>; NUM_PERMS] = Default::default();
1700    for (section, range) in out.iter_mut().zip(section_ranges) {
1701        // A permutation the file does not carry has a zeroed range. Its
1702        // `offset - container_offset` underflows, and before this guard it
1703        // panicked the whole `open_ranged` path on the first lean file.
1704        if range.len == 0 || range.offset < container_offset {
1705            continue;
1706        }
1707        let Some(payload) = checked_slice(index_bytes, range.offset - container_offset, range.len)
1708        else {
1709            continue;
1710        };
1711        if let Ok(dir) = parse_tile_directory(payload, payload.len() as u64) {
1712            *section = dir
1713                .into_iter()
1714                .map(|e| {
1715                    (
1716                        e.min_a,
1717                        e.max_a,
1718                        ByteRange {
1719                            offset: range.offset + e.start,
1720                            len: (e.end - e.start),
1721                        },
1722                    )
1723                })
1724                .collect();
1725        }
1726    }
1727    out
1728}
1729
1730/// Decode a tiled section payload into `(min_a, max_a, uncompressed tile)`
1731/// triples.
1732fn decode_tiled_section(payload: &[u8], codec: u8) -> Result<Vec<(u32, u32, Vec<u8>)>, FileError> {
1733    parse_tile_directory(payload, payload.len() as u64)?
1734        .into_iter()
1735        .map(|e| {
1736            Ok((
1737                e.min_a,
1738                e.max_a,
1739                decompress(codec, &payload[e.start as usize..e.end as usize])?,
1740            ))
1741        })
1742        .collect()
1743}
1744
1745/// Decode the index container: one raw tiled section payload per permutation
1746/// the file carries (`perms`, from the header — [`PermSet::ALL`] for every file
1747/// written before the mask existed), each tile compressed individually.
1748///
1749/// The section count is checked against `perms.len()`, so a file whose header
1750/// and container disagree is rejected rather than silently short-read. A reader
1751/// that does not know about lean files at all reaches the same conclusion by a
1752/// different route: it passes six, the container says three, and this errors —
1753/// which is why a lean file cannot be misread as an empty one.
1754fn decode_index_container(
1755    bytes: &[u8],
1756    codec: u8,
1757    perms: PermSet,
1758) -> Result<GraphIndex, FileError> {
1759    let isecs = decode_container(bytes, CODEC_NONE)?;
1760    if isecs.len() != perms.len() {
1761        return Err(FileError::Container(
1762            "index container section count does not match the header permutation mask",
1763        ));
1764    }
1765    decode_index_sections(isecs, codec, perms)
1766}
1767
1768fn decode_index_sections(
1769    mut isecs: Vec<Vec<u8>>,
1770    codec: u8,
1771    perms: PermSet,
1772) -> Result<GraphIndex, FileError> {
1773    let mut sections: [Vec<(u32, u32, Vec<u8>)>; NUM_PERMS] = Default::default();
1774    for (perm, sec) in perms.iter().zip(isecs.iter_mut()) {
1775        sections[perm.section_index()] = decode_tiled_section(sec, codec)?;
1776    }
1777    Ok(GraphIndex::from_tiles(sections, perms))
1778}
1779
1780/// Decode the file's **root** index container (the default graph): the
1781/// permutation sections, plus — on a quad-index file — the trailing seal, which
1782/// is checked and set aside. Returns the seal's digest of the QUAD_INDEX
1783/// section when there is one.
1784fn decode_root_index_container(
1785    bytes: &[u8],
1786    header: &Header,
1787) -> Result<(GraphIndex, Option<[u8; 16]>), FileError> {
1788    if !header.has_quad_index() {
1789        return Ok((
1790            decode_index_container(bytes, header.block_codec, header.perms)?,
1791            None,
1792        ));
1793    }
1794    let mut isecs = decode_container(bytes, CODEC_NONE)?;
1795    if isecs.len() != header.index_container_sections() {
1796        return Err(FileError::Container(
1797            "index container section count does not match the header permutation mask",
1798        ));
1799    }
1800    let seal = isecs.pop().unwrap_or_default();
1801    let digest = crate::quad::parse_seal(&seal)?;
1802    Ok((
1803        decode_index_sections(isecs, header.block_codec, header.perms)?,
1804        Some(digest),
1805    ))
1806}
1807
1808/// The named-graph layout a header announces must be the one its directory
1809/// holds: the quad flag with a QUAD_INDEX section and no NAMED_GRAPHS section,
1810/// or no flag and no QUAD_INDEX section. Anything else is a corrupt (or a
1811/// hand-edited) header, refused before either layout is read.
1812fn check_named_layout(header: &Header) -> Result<(), FileError> {
1813    let quad = header.quad_index_section().is_some();
1814    if header.has_quad_index() {
1815        if !quad {
1816            return Err(FileError::Container(
1817                "quad-index flag set but the file has no quad-index section",
1818            ));
1819        }
1820        if header.named_graphs_len > 0 {
1821            return Err(FileError::Container(
1822                "a file cannot carry both a quad index and a named-graphs section",
1823            ));
1824        }
1825        crate::quad::check_perms(header.perms)?;
1826    } else if quad {
1827        return Err(FileError::Container(
1828            "quad-index section present but the header does not flag it",
1829        ));
1830    }
1831    Ok(())
1832}
1833
1834fn container_section_payload_ranges(
1835    bytes: &[u8],
1836    container_offset: u64,
1837    expected_sections: usize,
1838) -> Result<Vec<ByteRange>, FileError> {
1839    let (section_count, mut pos) =
1840        read_uvarint(bytes).ok_or(FileError::Container("truncated count"))?;
1841    let section_count = usize::try_from(section_count)
1842        .map_err(|_| FileError::Container("section count too large"))?;
1843    if section_count != expected_sections {
1844        return Err(FileError::Container("unexpected section count"));
1845    }
1846
1847    let mut ranges = Vec::with_capacity(section_count);
1848    for _ in 0..section_count {
1849        let remaining = bytes
1850            .get(pos..)
1851            .ok_or(FileError::Container("truncated length"))?;
1852        let (payload_len, used) =
1853            read_uvarint(remaining).ok_or(FileError::Container("truncated length"))?;
1854        pos = pos
1855            .checked_add(used)
1856            .ok_or(FileError::Container("section range overflows"))?;
1857        let payload_len_usize = usize::try_from(payload_len)
1858            .map_err(|_| FileError::Container("section length too large"))?;
1859        let payload_end = pos
1860            .checked_add(payload_len_usize)
1861            .ok_or(FileError::Container("section range overflows"))?;
1862        if payload_end > bytes.len() {
1863            return Err(FileError::Container("section overruns buffer"));
1864        }
1865        ranges.push(ByteRange {
1866            offset: checked_end(container_offset, pos as u64)?,
1867            len: payload_len,
1868        });
1869        pos = payload_end;
1870    }
1871
1872    Ok(ranges)
1873}
1874
1875/// The permutation payload ranges of an index container holding `sections`
1876/// sections (the permutations, then — on a quad-index root — the seal, which
1877/// is not a permutation and is left out).
1878fn decode_index_section_ranges(
1879    bytes: &[u8],
1880    container_offset: u64,
1881    perms: PermSet,
1882    sections: usize,
1883) -> Result<[ByteRange; NUM_PERMS], FileError> {
1884    let ranges = container_section_payload_ranges(bytes, container_offset, sections)?;
1885    let mut out = [ByteRange { offset: 0, len: 0 }; NUM_PERMS];
1886    for (perm, range) in perms.iter().zip(ranges) {
1887        out[perm.section_index()] = range;
1888    }
1889    Ok(out)
1890}
1891
1892fn read_uvarint_at<R: RangeReader + ?Sized>(
1893    reader: &R,
1894    absolute_offset: u64,
1895    container_end: u64,
1896) -> Result<(u64, u64), FileError> {
1897    if absolute_offset >= container_end {
1898        return Err(FileError::Container("truncated container varint"));
1899    }
1900    let remaining = container_end - absolute_offset;
1901    let probe_len = remaining.min(10);
1902    let bytes = reader.read_at(absolute_offset, probe_len)?;
1903    read_uvarint(&bytes)
1904        .map(|(value, used)| (value, used as u64))
1905        .ok_or(FileError::Container("truncated container varint"))
1906}
1907
1908/// Locate one section's payload byte range inside a remote container, walking
1909/// only the (tiny) varint framing — no payload bytes are fetched.
1910pub(crate) fn locate_container_section_ranged<R: RangeReader + ?Sized>(
1911    reader: &R,
1912    container_offset: u64,
1913    container_len: u64,
1914    section_index: usize,
1915    expected_sections: u64,
1916) -> Result<ByteRange, FileError> {
1917    let container_end = checked_end(container_offset, container_len)?;
1918    let (section_count, used) = read_uvarint_at(reader, container_offset, container_end)?;
1919    if section_count != expected_sections {
1920        return Err(FileError::Container("unexpected container section count"));
1921    }
1922    if section_index >= section_count as usize {
1923        return Err(FileError::Container(
1924            "container section index out of bounds",
1925        ));
1926    }
1927
1928    let mut pos = checked_end(container_offset, used)?;
1929    for i in 0..section_count as usize {
1930        let (payload_len, len_used) = read_uvarint_at(reader, pos, container_end)?;
1931        pos = checked_end(pos, len_used)?;
1932        let payload_end = checked_end(pos, payload_len)?;
1933        if payload_end > container_end {
1934            return Err(FileError::Container("section overruns buffer"));
1935        }
1936        if i == section_index {
1937            return Ok(ByteRange {
1938                offset: pos,
1939                len: payload_len,
1940            });
1941        }
1942        pos = payload_end;
1943    }
1944    Err(FileError::Container("container section not found"))
1945}
1946
1947/// Open one index container (six tiled permutation sections) **lazily** over a
1948/// range reader: fetch each section's tile directory (and synopsis trailer,
1949/// when the file has one) but no tile payloads — tiles fault in on first scan.
1950/// Returns the index plus its section and tile file ranges (provenance).
1951///
1952/// This is the machinery `open_ranged_lazy` always used for the default
1953/// graph's container at `root_dir_offset`; a named graph's container is the
1954/// same format at a different offset, so the lazy named-graphs path opens a
1955/// LARGE graph through this too instead of decoding it resident.
1956#[allow(clippy::type_complexity)]
1957fn open_index_container_lazy(
1958    reader: &std::sync::Arc<dyn RangeReader + Send + Sync>,
1959    container: ByteRange,
1960    block_codec: u8,
1961    has_synopsis: bool,
1962    read_concurrency: usize,
1963    perms: PermSet,
1964    sections: usize,
1965) -> Result<
1966    (
1967        GraphIndex,
1968        [ByteRange; NUM_PERMS],
1969        [Vec<(u32, u32, ByteRange)>; NUM_PERMS],
1970    ),
1971    FileError,
1972> {
1973    let mut index_section_ranges = [ByteRange { offset: 0, len: 0 }; NUM_PERMS];
1974    let mut tile_ranges: [Vec<(u32, u32, ByteRange)>; NUM_PERMS] = Default::default();
1975    #[allow(clippy::type_complexity)]
1976    let mut directories: [Vec<(u32, u32, Option<TileSynopsis>)>; NUM_PERMS] = Default::default();
1977    for (pos, perm) in perms.iter().enumerate() {
1978        let si = perm.section_index();
1979        let section = locate_container_section_ranged(
1980            reader,
1981            container.offset,
1982            container.len,
1983            pos,
1984            sections as u64,
1985        )?;
1986        index_section_ranges[si] = section;
1987        let dir = read_tile_directory_ranged(reader, section)?;
1988        // Tile synopses (one extra small tail read per section) let a routed
1989        // scan prune a tile by a bound secondary component before faulting it.
1990        let syn = if has_synopsis {
1991            read_tile_synopsis_ranged(reader, section, &dir)
1992        } else {
1993            vec![None; dir.len()]
1994        };
1995        directories[si] = dir
1996            .iter()
1997            .zip(syn)
1998            .map(|(e, s)| (e.min_a, e.max_a, s))
1999            .collect();
2000        tile_ranges[si] = dir
2001            .into_iter()
2002            .map(|e| {
2003                (
2004                    e.min_a,
2005                    e.max_a,
2006                    ByteRange {
2007                        offset: section.offset + e.start,
2008                        len: (e.end - e.start),
2009                    },
2010                )
2011            })
2012            .collect();
2013    }
2014
2015    // The loader fetches and decompresses one tile per call; the bulk
2016    // loader serves multi-tile scans by coalescing adjacent tile ranges
2017    // into single range reads (tiles are back-to-back in their section,
2018    // so a full-section scan is typically one request).
2019    //
2020    // Either loader returns `None` (a failed load: the index marks itself
2021    // incomplete and a reset retries) unless the tile arrived whole and its
2022    // zone map parses. Without the length check, a short read of a raw
2023    // (uncompressed) tile decoded as a shorter tile and silently lost rows.
2024    let codec = block_codec;
2025    let loader_ranges = tile_ranges.clone();
2026    let loader_reader = reader.clone();
2027    let loader: crate::index::TileLoader = Box::new(move |si, ti| {
2028        let (_, _, range) = loader_ranges.get(si)?.get(ti)?;
2029        let bytes = loader_reader.read_at(range.offset, range.len).ok()?;
2030        if bytes.len() as u64 != range.len {
2031            return None;
2032        }
2033        checked_tile(codec, &bytes)
2034    });
2035    let bulk_ranges = tile_ranges.clone();
2036    let bulk_reader = reader.clone();
2037    let bulk: crate::index::TileBulkLoader = Box::new(move |si, tis| {
2038        let section = bulk_ranges.get(si)?;
2039        let want: Option<Vec<ByteRange>> = tis
2040            .iter()
2041            .map(|&ti| section.get(ti).map(|&(_, _, r)| r))
2042            .collect();
2043        let blobs = read_coalesced(bulk_reader.as_ref(), &want?, TILE_COALESCE_GAP)?;
2044        blobs.iter().map(|b| checked_tile(codec, b)).collect()
2045    });
2046    let mut index =
2047        GraphIndex::from_remote_directories(directories, perms, loader).with_bulk_loader(bulk);
2048    // Per-tile encoded lengths (from the directory) feed the join planner's
2049    // fatness gates — free here, unavailable later without a fetch.
2050    index.set_tile_lens(std::array::from_fn(|si| {
2051        tile_ranges[si]
2052            .iter()
2053            .map(|&(_, _, r)| r.len.min(u32::MAX as u64) as u32)
2054            .collect()
2055    }));
2056    // The reader's fan-out widens the planner's remote probe budget: a
2057    // desktop/CLI reader overlapping 16 range reads probes far more cheaply
2058    // than a phone's serial sync-XHR path.
2059    index.set_read_concurrency(read_concurrency);
2060    Ok((index, index_section_ranges, tile_ranges))
2061}
2062
2063/// A fetched tile, decompressed, or `None` when it does not decompress or its
2064/// zone map does not parse: a lazy tile load fails on the first scan that
2065/// touches it, instead of a corrupt tile reading as an empty or partial one.
2066fn checked_tile(codec: u8, bytes: &[u8]) -> Option<Vec<u8>> {
2067    let tile = decompress(codec, bytes).ok()?;
2068    crate::triples::TripleBlock::parse(&tile).ok()?;
2069    Some(tile)
2070}
2071
2072/// Serialize a complete `.rete` file image from a dictionary, index, and an
2073/// (optionally empty) encoded pyramid-meta section. `pyramid_levels` records the
2074/// number of dendrogram rounds the pyramid spans (0 if no pyramid).
2075///
2076/// The image is format `0x06`, the one layout every writer produces (the
2077/// legacy `0x05` image is [`write_file_v05`]).
2078pub fn write_file(
2079    dict: &Dictionary,
2080    index: &GraphIndex,
2081    has_quads: bool,
2082    pyramid_meta: &[u8],
2083    pyramid_levels: u16,
2084) -> Vec<u8> {
2085    write_dataset(dict, index, &[], has_quads, pyramid_meta, pyramid_levels)
2086}
2087
2088/// The legacy `0x05` image of [`write_file`]. The layout of the first stable
2089/// generation, kept for the tests that pin its byte layout, for the in-RAM
2090/// builders that hand their `0x05` image to the one `0x06` finish step, and for
2091/// `RETE_WRITE_FORMAT=5`.
2092#[doc(hidden)]
2093pub fn write_file_v05(
2094    dict: &Dictionary,
2095    index: &GraphIndex,
2096    has_quads: bool,
2097    pyramid_meta: &[u8],
2098    pyramid_levels: u16,
2099) -> Vec<u8> {
2100    write_dataset_v05(dict, index, &[], has_quads, pyramid_meta, pyramid_levels)
2101}
2102
2103/// A finished `0x05` image as the `0x06` file the public writers return (the
2104/// image is untouched under `RETE_WRITE_FORMAT=5`).
2105fn into_v06(image: Vec<u8>) -> Vec<u8> {
2106    crate::build_record::finish_in_ram(image, None)
2107        .unwrap_or_else(|e| panic!("writing format 0x06: {e}"))
2108}
2109
2110/// Encode an index container (v0.2): one raw tiled section payload per
2111/// permutation the index carries, in [`crate::index::ALL_PERMS`] order, tiles
2112/// compressed individually with `codec`. A three-permutation index writes three
2113/// sections — not six with three empty, which would be indistinguishable from
2114/// an empty graph to a reader that does not check the header mask.
2115pub(crate) fn encode_index_container(index: &GraphIndex, codec: u8) -> Vec<u8> {
2116    encode_index_container_sealed(index, codec, None)
2117}
2118
2119/// [`encode_index_container`] with an optional trailing **seal** section —
2120/// the root container of a quad-index file (see `quad.rs`). The seal is what
2121/// makes every reader that predates the quad index refuse the file: they all
2122/// check the container's section count before reading a payload.
2123fn encode_index_container_sealed(index: &GraphIndex, codec: u8, seal: Option<&[u8]>) -> Vec<u8> {
2124    let payloads: Vec<Vec<u8>> = index
2125        .perms()
2126        .iter()
2127        .map(|perm| encode_tiled_section(index, perm.section_index(), codec))
2128        .collect();
2129    let mut refs: Vec<&[u8]> = payloads.iter().map(|p| p.as_slice()).collect();
2130    if let Some(seal) = seal {
2131        refs.push(seal);
2132    }
2133    encode_container(&refs, CODEC_NONE)
2134}
2135
2136/// Encode the named-graphs section: each graph as `(iri, permutation container)`.
2137fn encode_named_graphs(named: &[(String, GraphIndex)], codec: u8) -> Vec<u8> {
2138    let mut out = Vec::new();
2139    write_uvarint(&mut out, named.len() as u64);
2140    for (iri, index) in named {
2141        write_uvarint(&mut out, iri.len() as u64);
2142        out.extend_from_slice(iri.as_bytes());
2143        let container = encode_index_container(index, codec);
2144        write_uvarint(&mut out, container.len() as u64);
2145        out.extend_from_slice(&container);
2146    }
2147    out
2148}
2149
2150fn decode_named_graphs(
2151    bytes: &[u8],
2152    codec: u8,
2153    perms: PermSet,
2154) -> Result<Vec<(String, GraphIndex)>, FileError> {
2155    let (n, mut pos) = read_uvarint(bytes).ok_or(FileError::Container("truncated graph count"))?;
2156    // Bounds-checked slice within this (already bounded) section. Lengths read
2157    // below are untrusted, so every range is validated before indexing.
2158    let bound = |start: usize, len: u64| -> Result<usize, FileError> {
2159        usize::try_from(len)
2160            .ok()
2161            .and_then(|len| start.checked_add(len))
2162            .filter(|&e| e <= bytes.len())
2163            .ok_or(FileError::Container("named-graph field overruns buffer"))
2164    };
2165    let n = wire_usize(n, "named-graph count too large")?;
2166    let mut out = Vec::with_capacity(n.min(bytes.len()));
2167    for _ in 0..n {
2168        let (ilen, u1) = read_uvarint(bytes.get(pos..).unwrap_or(&[]))
2169            .ok_or(FileError::Container("truncated iri len"))?;
2170        pos += u1;
2171        let iend = bound(pos, ilen)?;
2172        let iri = String::from_utf8_lossy(&bytes[pos..iend]).into_owned();
2173        pos = iend;
2174        let (clen, u2) = read_uvarint(bytes.get(pos..).unwrap_or(&[]))
2175            .ok_or(FileError::Container("truncated container len"))?;
2176        pos += u2;
2177        let cend = bound(pos, clen)?;
2178        let index = decode_index_container(&bytes[pos..cend], codec, perms)?;
2179        // Resident, so the count is a pass over tile headers; the planner
2180        // sizes this graph's patterns by it, not by the file's total.
2181        index.set_size_hint(u64::from(index.triple_count()));
2182        out.push((iri, index));
2183        pos = cend;
2184    }
2185    Ok(out)
2186}
2187
2188/// Serialize a full RDF *dataset*: the default-graph index plus zero or more
2189/// named graphs `(iri, index)`, all sharing one dictionary. Format `0x06`; named
2190/// graphs, given per graph here, are stored as the quad index (`0x06` has no
2191/// other layout).
2192pub fn write_dataset(
2193    dict: &Dictionary,
2194    default_index: &GraphIndex,
2195    named: &[(String, GraphIndex)],
2196    has_quads: bool,
2197    pyramid_meta: &[u8],
2198    pyramid_levels: u16,
2199) -> Vec<u8> {
2200    write_dataset_with_metadata(
2201        dict,
2202        default_index,
2203        named,
2204        has_quads,
2205        pyramid_meta,
2206        pyramid_levels,
2207        &[],
2208        &[],
2209    )
2210}
2211
2212/// The legacy `0x05` image of [`write_dataset`] (see [`write_file_v05`]).
2213#[doc(hidden)]
2214pub fn write_dataset_v05(
2215    dict: &Dictionary,
2216    default_index: &GraphIndex,
2217    named: &[(String, GraphIndex)],
2218    has_quads: bool,
2219    pyramid_meta: &[u8],
2220    pyramid_levels: u16,
2221) -> Vec<u8> {
2222    write_dataset_with_metadata_v05(
2223        dict,
2224        default_index,
2225        named,
2226        has_quads,
2227        pyramid_meta,
2228        pyramid_levels,
2229        &[],
2230        &[],
2231    )
2232}
2233
2234/// Serialize a dictionary to its on-file container bytes (4 front-coded, chunked
2235/// sections). Exposed so a low-RAM build can serialize **and drop** the live
2236/// `Dictionary` before building the permutation index — the index build works on
2237/// id-triples and never needs the dictionary.
2238pub(crate) fn encode_dict_container(dict: &Dictionary, codec: u8) -> Vec<u8> {
2239    let raw_sections = dict.sections();
2240    let dict_payloads: Vec<Vec<u8>> = raw_sections
2241        .iter()
2242        .map(|raw| encode_chunked_dict_section(raw, codec))
2243        .collect();
2244    encode_container(
2245        &[
2246            dict_payloads[0].as_slice(),
2247            dict_payloads[1].as_slice(),
2248            dict_payloads[2].as_slice(),
2249            dict_payloads[3].as_slice(),
2250        ],
2251        CODEC_NONE,
2252    )
2253}
2254
2255/// Serialize a dataset with an opaque **metadata** payload occupying the file's
2256/// metadata section (the application layer defines its meaning — the CLI stores a
2257/// JSON Dataset Card there). The section sits immediately after the header and
2258/// before the dictionary, so `metadata_offset` stays at `HEADER_LEN` and every
2259/// downstream section shifts by `metadata.len()`. The payload is folded into the
2260/// `content_hash`, so `verify` covers it and it is tamper-evident.
2261///
2262/// Passing an empty `metadata` is byte-identical to [`write_dataset`]: the section
2263/// is omitted (`metadata_len = 0`, `dictionary_offset = HEADER_LEN`) and the hash
2264/// is computed over exactly the same parts (a zero-length hash update is a no-op).
2265#[allow(clippy::too_many_arguments)]
2266pub fn write_dataset_with_metadata(
2267    dict: &Dictionary,
2268    default_index: &GraphIndex,
2269    named: &[(String, GraphIndex)],
2270    has_quads: bool,
2271    pyramid_meta: &[u8],
2272    pyramid_levels: u16,
2273    metadata: &[u8],
2274    text_index: &[u8],
2275) -> Vec<u8> {
2276    into_v06(write_dataset_with_metadata_v05(
2277        dict,
2278        default_index,
2279        named,
2280        has_quads,
2281        pyramid_meta,
2282        pyramid_levels,
2283        metadata,
2284        text_index,
2285    ))
2286}
2287
2288/// The legacy `0x05` image of [`write_dataset_with_metadata`] (see
2289/// [`write_file_v05`]).
2290#[doc(hidden)]
2291#[allow(clippy::too_many_arguments)]
2292pub fn write_dataset_with_metadata_v05(
2293    dict: &Dictionary,
2294    default_index: &GraphIndex,
2295    named: &[(String, GraphIndex)],
2296    has_quads: bool,
2297    pyramid_meta: &[u8],
2298    pyramid_levels: u16,
2299    metadata: &[u8],
2300    text_index: &[u8],
2301) -> Vec<u8> {
2302    let codec = writer_codec();
2303    let dict_container = encode_dict_container(dict, codec);
2304    // This entry point takes a finished section, so it reads the section's own
2305    // scope block to pick the directory kind (6, or 11 for a partial index) and
2306    // the header's named-graphs bit.
2307    let scope = (!text_index.is_empty())
2308        .then(|| crate::text_index::TextIndex::from_section(text_index, codec).ok())
2309        .flatten()
2310        .map(|ti| ti.scope().clone());
2311    let text_index_partial = scope
2312        .as_ref()
2313        .is_some_and(crate::text_index::TextIndexScope::restricts_predicates);
2314    let text_index_named = scope
2315        .as_ref()
2316        .is_some_and(crate::text_index::TextIndexScope::covers_named_graphs);
2317    write_dataset_from_parts(
2318        &dict_container,
2319        dict.term_count() as u64,
2320        default_index,
2321        NamedPart::PerGraph(named),
2322        has_quads,
2323        dict.has_quoted_triples(),
2324        pyramid_meta,
2325        pyramid_levels,
2326        metadata,
2327        text_index,
2328        text_index_partial,
2329        text_index_named,
2330        codec,
2331    )
2332}
2333
2334/// How the named graphs reach the writer: one index per graph (the
2335/// NAMED_GRAPHS section, kind 5), or an encoded quad index (kind 10).
2336pub(crate) enum NamedPart<'a> {
2337    PerGraph(&'a [(String, GraphIndex)]),
2338    Quad(&'a crate::quad::EncodedQuadIndex),
2339}
2340
2341/// Assemble the final file image from an **already-serialized** dictionary
2342/// container (so the caller can drop the live `Dictionary` before calling this)
2343/// plus the permutation index and optional sections. The byte output is identical
2344/// to serializing the dictionary inline.
2345#[allow(clippy::too_many_arguments)]
2346pub(crate) fn write_dataset_from_parts(
2347    dict_container: &[u8],
2348    term_count: u64,
2349    default_index: &GraphIndex,
2350    named: NamedPart<'_>,
2351    has_quads: bool,
2352    has_quoted_triples: bool,
2353    pyramid_meta: &[u8],
2354    pyramid_levels: u16,
2355    metadata: &[u8],
2356    text_index: &[u8],
2357    text_index_partial: bool,
2358    text_index_named: bool,
2359    codec: u8,
2360) -> Vec<u8> {
2361    // The named graphs: either the per-graph section (kind 5) or the quad index
2362    // (kind 10), whose digest the root container's seal carries. An empty quad
2363    // index is written as no named graphs at all.
2364    let (named_section, quad, named_quads) = match named {
2365        NamedPart::PerGraph(named) => (
2366            if named.is_empty() {
2367                Vec::new()
2368            } else {
2369                encode_named_graphs(named, codec)
2370            },
2371            None,
2372            named
2373                .iter()
2374                .map(|(_, idx)| idx.triple_count() as u64)
2375                .sum::<u64>(),
2376        ),
2377        NamedPart::Quad(q) if q.graph_count == 0 => (Vec::new(), None, 0),
2378        NamedPart::Quad(q) => (Vec::new(), Some(q), q.quad_count),
2379    };
2380    let seal = quad.map(|q| crate::quad::encode_seal(crate::quad::digest(&q.bytes)));
2381    let index_container = encode_index_container_sealed(default_index, codec, seal.as_deref());
2382    // A predicate-restricted index (kind 11) is outside the content hash, like
2383    // kinds 8 and 9, and starts with its own digest instead: a reader older
2384    // than 0.4.0 does not see kind 11, so a hashed one would make its `verify`
2385    // call every such file corrupt. A kind-6 index is hashed, as it always was.
2386    let text_index_partial = text_index_partial && !text_index.is_empty();
2387    let digested;
2388    let text_index: &[u8] = if text_index_partial {
2389        digested = crate::text_index::with_partial_digest(text_index);
2390        &digested
2391    } else {
2392        text_index
2393    };
2394
2395    // The metadata section (if any) sits between the header and the dictionary,
2396    // so the dictionary — and everything after it — shifts forward by its length.
2397    let meta_section_len = metadata.len() as u64;
2398    let dict_offset = HEADER_LEN as u64 + meta_section_len;
2399    let dict_len = dict_container.len() as u64;
2400    let index_offset = dict_offset + dict_len;
2401    let index_len = index_container.len() as u64;
2402    let pyr_offset = index_offset + index_len;
2403    let pyr_len = pyramid_meta.len() as u64;
2404    // Optional full-text index between the pyramid and the named graphs.
2405    let text_offset = pyr_offset + pyr_len;
2406    let text_len = text_index.len() as u64;
2407    let named_offset = text_offset + text_len;
2408    let named_len = named_section.len() as u64;
2409    // The quad index sits where the named-graphs section would (the two are
2410    // exclusive).
2411    let quad_len = quad.map_or(0, |q| q.bytes.len() as u64);
2412
2413    // Hash parts in physical order, with the metadata payload prepended when
2414    // present. Omitting it entirely (rather than hashing an empty slice) keeps the
2415    // no-metadata output's hash byte-identical to the pre-metadata writer.
2416    // `verify()` rebuilds this exact list from the header — any section added
2417    // here must be added there too (and covered by a tamper test).
2418    let mut parts: Vec<&[u8]> = Vec::with_capacity(5);
2419    if meta_section_len > 0 {
2420        parts.push(metadata);
2421    }
2422    parts.push(dict_container);
2423    parts.push(&index_container);
2424    parts.push(pyramid_meta);
2425    if text_len > 0 && !text_index_partial {
2426        parts.push(text_index);
2427    }
2428    if named_len > 0 {
2429        parts.push(&named_section);
2430    }
2431
2432    // Length of the trailing schema-pyramid block (0 if none), so a reader can
2433    // fetch just that block for an index/dictionary/summary-free Tier-0 read.
2434    let schema_meta_len = crate::meta::schema_block_len(pyramid_meta);
2435
2436    // The QUAD_INDEX section is not a hash part of its own: the seal inside the
2437    // index container carries its digest, so the content hash covers it — and a
2438    // reader that predates it computes the same hash over the same parts.
2439    let header = Header {
2440        version: crate::header::LEGACY_FORMAT_VERSION,
2441        flags: FLAG_TILE_SYNOPSIS
2442            | if has_quads { FLAG_HAS_QUADS } else { 0 }
2443            | if quad.is_some() {
2444                crate::header::FLAG_QUAD_INDEX
2445            } else {
2446                0
2447            }
2448            | if has_quoted_triples {
2449                FLAG_HAS_QUOTED_TRIPLES
2450            } else {
2451                0
2452            }
2453            | if text_len > 0 && text_index_named {
2454                crate::header::FLAG_TEXT_INDEX_NAMED_GRAPHS
2455            } else {
2456                0
2457            },
2458        metadata_offset: HEADER_LEN as u64,
2459        metadata_len: meta_section_len,
2460        dictionary_offset: dict_offset,
2461        dictionary_len: dict_len,
2462        root_dir_offset: index_offset,
2463        root_dir_len: index_len,
2464        pyramid_meta_offset: if pyr_len > 0 { pyr_offset } else { 0 },
2465        pyramid_meta_len: pyr_len,
2466        dict_codec: codec,
2467        block_codec: codec,
2468        pyramid_levels,
2469        id_widths: 0,
2470        // Every container in the file — default graph and named graphs alike —
2471        // is written from an index built with the same set, so the mask is a
2472        // file-level fact and the default index is its source of truth.
2473        perms: default_index.perms(),
2474        quad_count: default_index.triple_count() as u64 + named_quads,
2475        term_count,
2476        content_hash: content_hash(&parts),
2477        named_graphs_offset: if named_len > 0 { named_offset } else { 0 },
2478        named_graphs_len: named_len,
2479        schema_meta_len,
2480        text_index_offset: if text_len > 0 { text_offset } else { 0 },
2481        text_index_len: text_len,
2482        text_index_partial: text_len > 0 && text_index_partial,
2483        build_info_offset: 0,
2484        build_info_len: 0,
2485        extra_sections: if quad_len > 0 {
2486            vec![crate::header::Section {
2487                kind: crate::header::SectionKind::QuadIndex,
2488                flags: 0,
2489                offset: named_offset,
2490                length: quad_len,
2491            }]
2492        } else {
2493            Vec::new()
2494        },
2495    };
2496
2497    let mut out = Vec::with_capacity(
2498        HEADER_LEN
2499            + metadata.len()
2500            + dict_container.len()
2501            + index_container.len()
2502            + pyramid_meta.len()
2503            + text_index.len()
2504            + named_section.len()
2505            + quad_len as usize
2506            + MAGIC.len(),
2507    );
2508    out.extend_from_slice(&header.to_bytes());
2509    if meta_section_len > 0 {
2510        out.extend_from_slice(metadata);
2511    }
2512    out.extend_from_slice(dict_container);
2513    out.extend_from_slice(&index_container);
2514    out.extend_from_slice(pyramid_meta);
2515    if text_len > 0 {
2516        out.extend_from_slice(text_index);
2517    }
2518    if named_len > 0 {
2519        out.extend_from_slice(&named_section);
2520    }
2521    if let Some(q) = quad {
2522        out.extend_from_slice(&q.bytes);
2523    }
2524    out.extend_from_slice(&MAGIC); // footer marker
2525    out
2526}
2527
2528/// `rdf:type` — the predicate that assigns a class to a resource.
2529pub const RDF_TYPE: &str = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>";
2530
2531/// An **ontology-aware** coarse graph: instead of structural communities, group
2532/// entities by their `rdf:type` class and aggregate relations between classes.
2533/// Returns `(subject_class, predicate, object_class, count)` over the default
2534/// graph. Entities with no type are `(untyped)`; literals are `(literal)`.
2535/// `rdf:type` triples themselves define the classes and are not counted as
2536/// relations. This is the dataset's effective schema with instance volumes.
2537pub fn schema_summary(rete: &Rete) -> Vec<(String, String, String, u32)> {
2538    use std::collections::{BTreeMap, HashMap};
2539    let triples = rete.dump(None);
2540
2541    let mut class_of: HashMap<&str, &str> = HashMap::new();
2542    for (s, p, o) in &triples {
2543        if p == RDF_TYPE {
2544            class_of.insert(s.as_str(), o.as_str());
2545        }
2546    }
2547    let classify = |t: &str| -> String {
2548        if let Some(c) = class_of.get(t) {
2549            (*c).to_string()
2550        } else if t.starts_with('"') {
2551            "(literal)".to_string()
2552        } else {
2553            "(untyped)".to_string()
2554        }
2555    };
2556
2557    let mut counts: BTreeMap<(String, String, String), u32> = BTreeMap::new();
2558    for (s, p, o) in &triples {
2559        if p == RDF_TYPE {
2560            continue; // type assertions define classes, not data relations
2561        }
2562        *counts
2563            .entry((classify(s), p.clone(), classify(o)))
2564            .or_default() += 1;
2565    }
2566    counts
2567        .into_iter()
2568        .map(|((a, p, b), c)| (a, p, b, c))
2569        .collect()
2570}
2571
2572/// Class populations: the number of resources of each `rdf:type` class in the
2573/// default graph, descending by count. The instance-count companion to
2574/// [`schema_summary`].
2575pub fn schema_classes(rete: &Rete) -> Vec<(String, u32)> {
2576    use std::collections::BTreeMap;
2577    let mut counts: BTreeMap<String, u32> = BTreeMap::new();
2578    for (_s, p, o) in rete.dump(None) {
2579        if p == RDF_TYPE {
2580            *counts.entry(o).or_default() += 1;
2581        }
2582    }
2583    let mut out: Vec<(String, u32)> = counts.into_iter().collect();
2584    out.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
2585    out
2586}
2587
2588/// Fetch **only** the metadata section (the opaque Dataset Card blob) via a
2589/// [`RangeReader`]: read the 1 KiB header, then the metadata byte range —
2590/// nothing else. This is the index-free CARD tier of the exploration model: a
2591/// remote/S3 client learns the dataset's self-description in **two small range
2592/// requests**, never touching the dictionary, index, or pyramid. Returns `None`
2593/// when the file carries no metadata.
2594///
2595/// Companion to [`Rete::open_ranged`] (which deliberately *skips* the card to
2596/// keep the query path minimal); this is the explicit "I want the card" path.
2597pub fn read_metadata_ranged<R: RangeReader>(reader: &R) -> Result<Option<Vec<u8>>, FileError> {
2598    let head = reader.read_at(0, HEADER_LEN as u64)?;
2599    if crate::v06::open::is_v06(&head) {
2600        let h = crate::v06::Header06::from_bytes(&head)?;
2601        return Ok(crate::v06::open::read_card_build(reader, &h)?.0);
2602    }
2603    let header = Header::from_bytes(&head)?;
2604    if header.metadata_len == 0 {
2605        return Ok(None);
2606    }
2607    let bytes = reader.read_at(header.metadata_offset, header.metadata_len)?;
2608    Ok(Some(bytes))
2609}
2610
2611/// The card **with its annex** (format `0x07`) merged back in: the fields the
2612/// hot card could not hold (`extra`, `description` over 8 KiB) are fetched from
2613/// the cold annex, a second range request. For a file with no annex (and for
2614/// every older format) this is [`read_metadata_ranged`].
2615pub fn read_card_full_ranged<R: RangeReader>(reader: &R) -> Result<Option<Vec<u8>>, FileError> {
2616    let head = reader.read_at(0, HEADER_LEN as u64)?;
2617    if crate::v06::open::is_v06(&head) {
2618        return Ok(crate::v06::open::read_card_full(reader)?.0);
2619    }
2620    read_metadata_ranged(reader)
2621}
2622
2623/// Fetch the metadata (Dataset Card) **and** build-info sections via a
2624/// [`RangeReader`] in the fewest requests: the 1 KiB header, then — because the
2625/// writer lays the build-info section immediately after the metadata — **one**
2626/// coalesced range covering both. A file with only one of the two costs the
2627/// same two requests; a file with neither costs just the header read. This
2628/// keeps "card + build conditions" within the CARD tier's 1 header + 1 range
2629/// budget instead of adding a third request.
2630///
2631/// Returns `(metadata, build_info)`, each `None` when absent.
2632#[allow(clippy::type_complexity)]
2633pub fn read_card_and_build_info_ranged<R: RangeReader>(
2634    reader: &R,
2635) -> Result<(Option<Vec<u8>>, Option<Vec<u8>>), FileError> {
2636    read_card_and_build_info_with_header(reader).map(|(_, m, b)| (m, b))
2637}
2638
2639/// [`read_card_and_build_info_ranged`], also returning the parsed [`Header`] it
2640/// had to read anyway.
2641///
2642/// The header is not a bonus: it carries the content hash and the section
2643/// directory — including whether the file has a TEXT_INDEX section, which the
2644/// card does not state (see
2645/// [`read_text_index_token_table_len_ranged`]). A caller that wants both and
2646/// used the tuple-only form would issue a **second** 1 KiB request for bytes it
2647/// already had, turning the CARD tier's documented "one header + one coalesced
2648/// range" into three requests.
2649#[allow(clippy::type_complexity)]
2650pub fn read_card_and_build_info_with_header<R: RangeReader>(
2651    reader: &R,
2652) -> Result<(Header, Option<Vec<u8>>, Option<Vec<u8>>), FileError> {
2653    let head = reader.read_at(0, HEADER_LEN as u64)?;
2654    let header = Header::from_bytes(&head)?;
2655    if crate::v06::reads(header.version) {
2656        let h = crate::v06::Header06::from_bytes(&head)?;
2657        let (card, build) = crate::v06::open::read_card_build(reader, &h)?;
2658        return Ok((header, card, build));
2659    }
2660    let meta = (header.metadata_offset, header.metadata_len);
2661    let build = (header.build_info_offset, header.build_info_len);
2662    if meta.1 > 0 && build.1 > 0 && build.0 == meta.0 + meta.1 {
2663        // Adjacent (the layout this crate writes): one read spans both.
2664        let both = reader.read_at(meta.0, meta.1 + build.1)?;
2665        let (m, b) = usize::try_from(meta.1)
2666            .ok()
2667            .and_then(|mid| both.split_at_checked(mid))
2668            .ok_or(FileError::Container("short card read"))?;
2669        return Ok((header, Some(m.to_vec()), Some(b.to_vec())));
2670    }
2671    let fetch = |off: u64, len: u64| -> Result<Option<Vec<u8>>, FileError> {
2672        if len == 0 {
2673            return Ok(None);
2674        }
2675        Ok(Some(reader.read_at(off, len)?))
2676    };
2677    Ok((header, fetch(meta.0, meta.1)?, fetch(build.0, build.1)?))
2678}
2679
2680/// The build-info section of a whole file image, or `None` if absent. The
2681/// payload is an opaque application-layer blob (the CLI stores build-conditions
2682/// JSON there); it is **not** covered by the content hash — see
2683/// [`attach_build_info`].
2684pub fn read_build_info(bytes: &[u8]) -> Result<Option<Vec<u8>>, FileError> {
2685    if crate::v06::open::is_v06(bytes) {
2686        let h = crate::v06::Header06::from_bytes(bytes)?;
2687        let r = crate::reader::SliceReader::new(bytes);
2688        return Ok(crate::v06::open::read_card_build(&r, &h)?.1);
2689    }
2690    let header = Header::from_bytes(bytes)?;
2691    if header.build_info_len == 0 {
2692        return Ok(None);
2693    }
2694    let section = checked_slice(bytes, header.build_info_offset, header.build_info_len)
2695        .ok_or(FileError::Container("build-info section overruns buffer"))?;
2696    Ok(Some(section.to_vec()))
2697}
2698
2699/// Everything needed to splice a build-info section into a finished file
2700/// **without holding the file in memory**: the rewritten header, where the
2701/// section goes, and where the bytes that do not move resume.
2702///
2703/// See [`plan_build_info`] for what the fields mean together: the build-info
2704/// section always follows the metadata section, so the layout is
2705/// `[header][metadata][build info][everything else, verbatim]` — or, for a
2706/// card placed after the data, `[header][data…][metadata][build info][footer]`.
2707#[derive(Debug, Clone, PartialEq, Eq)]
2708pub struct BuildInfoPlan {
2709    /// The header bytes to write at offset 0.
2710    pub header: [u8; HEADER_LEN],
2711    /// Offset the new section is written at: the end of the metadata section
2712    /// (the length of the unchanged prefix up to and including the card).
2713    pub insert: u64,
2714    /// Offset in the **old** file where the bytes that do not move resume. The
2715    /// span `insert..tail_start` is the old build-info section, if any, and is
2716    /// the only part of the old file the new one drops.
2717    pub tail_start: u64,
2718    /// Length of the file this plan produces.
2719    pub new_len: u64,
2720}
2721
2722/// Plan the splice of a build-info section of `info_len` bytes into a file
2723/// whose first [`HEADER_LEN`] bytes are `head` and whose total length is
2724/// `file_len`.
2725///
2726/// The section goes immediately after the metadata section — adjacent, so
2727/// [`read_card_and_build_info_ranged`] fetches card + build info in one
2728/// coalesced range — and every later section's offset shifts by the length
2729/// delta. That shift is the whole of the arithmetic, and it lives here so the
2730/// in-memory splice ([`attach_build_info`]) and any streaming rewriter derive
2731/// the same header from the same rule instead of each carrying a copy that can
2732/// drift.
2733///
2734/// The content hash is **deliberately left untouched**: build info records the
2735/// facts that differ between two builds of identical data (timestamp, builder,
2736/// measured costs), and folding them into the hash would break the
2737/// reproducible-hash property. `verify` accordingly ignores this section, on an
2738/// old reader (which sees an unknown kind-7 entry) and a new one alike — so a
2739/// file that gains, loses or changes a build record **keeps its content
2740/// identity**.
2741///
2742/// `info_len` of 0 plans the removal of an existing section.
2743pub fn plan_build_info(
2744    head: &[u8],
2745    file_len: u64,
2746    info_len: u64,
2747) -> Result<BuildInfoPlan, FileError> {
2748    let mut header = Header::from_bytes(head)?;
2749    // The insert point: immediately after the metadata section, wherever it
2750    // lies — at HEADER_LEN as the in-RAM writers place it, or after the data
2751    // where the memory-bounded build and `recard --in-place` put it
2752    // (`crate::recard::plan_card_append`). No card: right after the header.
2753    let insert = if header.metadata_len > 0 {
2754        header
2755            .metadata_offset
2756            .checked_add(header.metadata_len)
2757            .filter(|&end| header.metadata_offset >= HEADER_LEN as u64 && end <= file_len)
2758            .ok_or(FileError::Container(
2759                "the metadata section lies outside the file",
2760            ))?
2761    } else {
2762        HEADER_LEN as u64
2763    };
2764    let old_len = header.build_info_len;
2765    if old_len > 0 && header.build_info_offset != insert {
2766        return Err(FileError::Container(
2767            "existing build-info section is not adjacent to the metadata",
2768        ));
2769    }
2770    let tail_start = insert.saturating_add(old_len);
2771    if tail_start > file_len || insert > file_len {
2772        return Err(FileError::Container("build-info splice out of bounds"));
2773    }
2774
2775    // Shift every section that lives at or after the old tail.
2776    let shift = |off: &mut u64, len: u64| {
2777        if len > 0 && *off >= tail_start {
2778            *off = *off - old_len + info_len;
2779        }
2780    };
2781    shift(&mut header.dictionary_offset, header.dictionary_len);
2782    shift(&mut header.root_dir_offset, header.root_dir_len);
2783    shift(&mut header.pyramid_meta_offset, header.pyramid_meta_len);
2784    shift(&mut header.named_graphs_offset, header.named_graphs_len);
2785    shift(&mut header.text_index_offset, header.text_index_len);
2786    for s in &mut header.extra_sections {
2787        if s.length > 0 && s.offset >= tail_start {
2788            s.offset = s.offset - old_len + info_len;
2789        }
2790    }
2791    if info_len == 0 {
2792        header.build_info_offset = 0;
2793        header.build_info_len = 0;
2794    } else {
2795        header.build_info_offset = insert;
2796        header.build_info_len = info_len;
2797    }
2798
2799    Ok(BuildInfoPlan {
2800        header: header.to_bytes(),
2801        insert,
2802        tail_start,
2803        new_len: file_len - old_len + info_len,
2804    })
2805}
2806
2807/// Attach (or replace) a **build-info** section in a finished file image,
2808/// returning the new image. Layout and hash semantics: [`plan_build_info`].
2809///
2810/// Passing an empty `info` removes an existing section (or returns the image
2811/// unchanged when there is none).
2812pub fn attach_build_info(image: &[u8], info: &[u8]) -> Result<Vec<u8>, FileError> {
2813    if crate::v06::open::is_v06(image) {
2814        // 0x06: the record lives in the card region; the card stays.
2815        return crate::v06::write::replace_card_with(
2816            image,
2817            crate::v06::write::NewCard::Keep,
2818            (!info.is_empty()).then_some(info),
2819        );
2820    }
2821    let plan = plan_build_info(image, image.len() as u64, info.len() as u64)?;
2822    let mut out = Vec::with_capacity(plan.new_len as usize);
2823    out.extend_from_slice(&plan.header);
2824    out.extend_from_slice(&image[HEADER_LEN..plan.insert as usize]);
2825    out.extend_from_slice(info);
2826    out.extend_from_slice(&image[plan.tail_start as usize..]);
2827    debug_assert_eq!(out.len() as u64, plan.new_len);
2828    Ok(out)
2829}
2830
2831/// Replace the **metadata** section (the Dataset Card blob) of a finished file
2832/// image, returning the new image. The section is the first one after the
2833/// header, so every later section's offset shifts by the length delta.
2834///
2835/// Unlike [`attach_build_info`], the metadata payload **is** inside the content
2836/// hash — it is part of what the file says about itself — so the hash is
2837/// recomputed here. The result is byte-identical to what the writer would have
2838/// produced had it been handed this payload in the first place
2839/// (`write_dataset_with_metadata`): the section sits at `HEADER_LEN`, every
2840/// later offset is derived from its length, and the hash folds the same parts
2841/// in the same order.
2842///
2843/// Why splice rather than rebuild: the only caller that needs this is a build
2844/// that has already assembled the file and then learned something about it by
2845/// **running** its own starter queries against it. Re-deriving the whole image
2846/// to correct a metadata blob would mean re-parsing the input; a splice costs
2847/// one memcpy and one rehash of an image that is already resident.
2848///
2849/// Passing an empty payload removes the section.
2850pub fn replace_metadata(image: &[u8], metadata: &[u8]) -> Result<Vec<u8>, FileError> {
2851    if crate::v06::open::is_v06(image) {
2852        // 0x06: the card region is rewritten; every section is kept.
2853        let h = crate::v06::Header06::from_bytes(image)?;
2854        let (_, build) =
2855            crate::v06::open::read_card_build(&crate::reader::SliceReader::new(image), &h)?;
2856        return crate::v06::write::replace_card(
2857            image,
2858            (!metadata.is_empty()).then_some(metadata),
2859            build.as_deref(),
2860        );
2861    }
2862    let mut header = Header::from_bytes(image)?;
2863    // The writers in this crate always place metadata (when present) at
2864    // HEADER_LEN; anything else is a file this function must not reshape.
2865    let insert = HEADER_LEN as u64;
2866    if header.metadata_len > 0 && header.metadata_offset != insert {
2867        return Err(FileError::Container(
2868            "metadata section does not sit immediately after the header",
2869        ));
2870    }
2871    let old_len = header.metadata_len;
2872    let tail_start = (insert + old_len) as usize;
2873    if tail_start > image.len() {
2874        return Err(FileError::Container("metadata splice out of bounds"));
2875    }
2876    let new_len = metadata.len() as u64;
2877
2878    let shift = |off: &mut u64, len: u64| {
2879        if len > 0 && *off >= insert + old_len {
2880            *off = *off - old_len + new_len;
2881        }
2882    };
2883    shift(&mut header.dictionary_offset, header.dictionary_len);
2884    shift(&mut header.root_dir_offset, header.root_dir_len);
2885    shift(&mut header.pyramid_meta_offset, header.pyramid_meta_len);
2886    shift(&mut header.named_graphs_offset, header.named_graphs_len);
2887    shift(&mut header.text_index_offset, header.text_index_len);
2888    shift(&mut header.build_info_offset, header.build_info_len);
2889    for s in &mut header.extra_sections {
2890        if s.length > 0 && s.offset >= insert + old_len {
2891            s.offset = s.offset - old_len + new_len;
2892        }
2893    }
2894    header.metadata_offset = insert;
2895    header.metadata_len = new_len;
2896
2897    let mut out = Vec::with_capacity(image.len() - old_len as usize + metadata.len());
2898    out.extend_from_slice(&header.to_bytes());
2899    out.extend_from_slice(metadata);
2900    out.extend_from_slice(&image[tail_start..]);
2901    // The payload changed, so the hash must: recompute it over the same part
2902    // list `verify` will rebuild from this very header, then re-serialize the
2903    // header carrying it.
2904    header.content_hash = hash_of_sections(&header, &out)?;
2905    out[..HEADER_LEN].copy_from_slice(&header.to_bytes());
2906    Ok(out)
2907}
2908
2909/// The content hash of a file image, folded over exactly the parts the writer
2910/// hashed (see `write_dataset_from_parts`): the metadata payload when present,
2911/// then dict, index, pyramid-meta, and — when present — the text index and the
2912/// named graphs. [`verify`] compares this against the header; [`replace_metadata`]
2913/// writes it.
2914fn hash_of_sections(header: &Header, bytes: &[u8]) -> Result<[u8; 16], FileError> {
2915    let slice = |off: u64, len: u64| -> Result<&[u8], FileError> {
2916        // `off` and `len` come STRAIGHT FROM THE FILE HEADER, so a crafted or
2917        // corrupt .rete can set them near u64::MAX and make `off + len` overflow
2918        // — which panics in debug and wraps in release, BEFORE `.get()` gets the
2919        // chance to reject the range. Found by the weekly fuzz run
2920        // (fuzz_targets/open.rs, "attempt to add with overflow"). Saturating the
2921        // sum turns both cases into the same clean Container error.
2922        // `checked_slice` also refuses an offset past this target's usize
2923        // (wasm32), which `as usize` used to wrap onto other bytes.
2924        checked_slice(bytes, off, len).ok_or(FileError::Container("section overruns buffer"))
2925    };
2926    let parts = content_hash_parts(header)
2927        .into_iter()
2928        .map(|(off, len)| slice(off, len))
2929        .collect::<Result<Vec<&[u8]>, FileError>>()?;
2930    Ok(content_hash(&parts))
2931}
2932
2933/// The byte ranges the content hash folds, **in the order it folds them**: the
2934/// metadata payload when present, then dictionary, index, pyramid-meta, and —
2935/// when present — a kind-6 text index and the named graphs. Build info, the
2936/// self-digested kinds (8 LABEL_INDEX, 9 SCHEMA, 11 predicate-restricted text
2937/// index) and any section kind this reader does not know are deliberately
2938/// outside it. The quad index (kind 10) is not a range here either: its digest
2939/// is the seal at the end of the index container, so it is hashed by reference.
2940///
2941/// The one list [`verify`], [`verify_ranged`] and [`replace_metadata`] hash
2942/// over, and what a streaming rewriter (`rete recard`) feeds a hasher while it
2943/// copies — so a section a future format adds to the hash is added here, once,
2944/// and every one of them follows.
2945pub fn content_hash_parts(header: &Header) -> Vec<(u64, u64)> {
2946    let mut parts: Vec<(u64, u64)> = Vec::with_capacity(6);
2947    if header.metadata_len > 0 {
2948        parts.push((header.metadata_offset, header.metadata_len));
2949    }
2950    parts.push((header.dictionary_offset, header.dictionary_len));
2951    parts.push((header.root_dir_offset, header.root_dir_len));
2952    // A zero-length pyramid folds nothing either way; it is listed only when
2953    // present so the ranges a rewriter walks are all real sections.
2954    if header.pyramid_meta_len > 0 {
2955        parts.push((header.pyramid_meta_offset, header.pyramid_meta_len));
2956    }
2957    // A kind-11 (predicate-restricted) text index is self-digested instead.
2958    if header.text_index_len > 0 && !header.text_index_partial {
2959        parts.push((header.text_index_offset, header.text_index_len));
2960    }
2961    if header.named_graphs_len > 0 {
2962        parts.push((header.named_graphs_offset, header.named_graphs_len));
2963    }
2964    parts
2965}
2966
2967/// `verify` for a `0x06` file over any reader: what failed (empty = all good).
2968/// `only` restricts the section checks to one kind (`verify --section`).
2969pub fn verify_ranged_v06(
2970    reader: &dyn crate::reader::RangeReader,
2971    head: &[u8],
2972    only: Option<u8>,
2973) -> Result<Vec<String>, FileError> {
2974    const SLICE: u64 = 8 << 20;
2975    let h = crate::v06::Header06::from_bytes(head)?;
2976    if h.file_len != reader.len() {
2977        return Ok(vec!["file length".to_string()]);
2978    }
2979    let mut bad = Vec::new();
2980    let root_bytes = reader.read_at(crate::v06::HEADER_LEN as u64, h.root_len as u64)?;
2981    if crate::v06::digest16(&root_bytes) != h.root_digest {
2982        bad.push("root".to_string());
2983        return Ok(bad);
2984    }
2985    let root = crate::v06::Root06::decode(&root_bytes, h.internal_codec)?;
2986    if crate::v06::content_hash(&h) != h.content_hash {
2987        bad.push("content hash".to_string());
2988    }
2989    let card = h.card_range();
2990    if only.is_none() && card.len > 0 {
2991        let stored = reader.read_at(card.offset, card.len)?;
2992        if crate::v06::digest16(&stored) != h.card_digest {
2993            bad.push("card".to_string());
2994        }
2995    }
2996    for s in &root.sections {
2997        if only.is_some_and(|k| k != s.kind) {
2998            continue;
2999        }
3000        let a = match s.abs(&h) {
3001            Ok(a) if a.offset + a.len <= h.file_len => a,
3002            _ => {
3003                bad.push(crate::v06::kind::name(s.kind).to_string());
3004                continue;
3005            }
3006        };
3007        let mut hs = blake3::Hasher::new();
3008        let mut at = a.offset;
3009        while at < a.offset + a.len {
3010            let n = (a.offset + a.len - at).min(SLICE);
3011            hs.update(&reader.read_at(at, n)?);
3012            at += n;
3013        }
3014        if hs.finalize().as_bytes()[..16] != s.digest {
3015            bad.push(crate::v06::kind::name(s.kind).to_string());
3016        }
3017    }
3018    if only.is_none() {
3019        let n = crate::v06::FOOTER.len() as u64;
3020        if reader.read_at(h.file_len - n, n)? != crate::v06::footer(h.version) {
3021            bad.push("footer".to_string());
3022        }
3023    }
3024    Ok(bad)
3025}
3026
3027/// Recompute the content hash from a file image and check it against the header
3028/// — detects corruption or truncation of the payload sections. The
3029/// LABEL_INDEX, SCHEMA and predicate-restricted TEXT_INDEX sections (kinds 8,
3030/// 9, 11) are outside that hash — an older reader's `verify` must keep passing
3031/// on a file that carries them — so each is checked against its own leading
3032/// digest instead. The QUAD_INDEX section (kind 10) is hashed by reference: its
3033/// digest is the seal inside the (hashed) index container, checked here.
3034pub fn verify(bytes: &[u8]) -> Result<bool, FileError> {
3035    if crate::v06::open::is_v06(bytes) {
3036        let r: std::sync::Arc<dyn RangeReader + Send + Sync> =
3037            std::sync::Arc::new(crate::reader::BytesReader(std::sync::Arc::from(bytes)));
3038        return crate::v06::open::V06File::open(r, bytes)?.verify(None);
3039    }
3040    let header = Header::from_bytes(bytes)?;
3041    if hash_of_sections(&header, bytes)? != header.content_hash {
3042        return Ok(false);
3043    }
3044    // The QUAD_INDEX section is covered through the seal's digest (inside the
3045    // hashed index container), so it is checked against that.
3046    if header.has_quad_index() || header.quad_index_section().is_some() {
3047        check_named_layout(&header)?;
3048        let slice = |off: u64, len: u64| -> Result<&[u8], FileError> {
3049            checked_slice(bytes, off, len).ok_or(FileError::Container("section overruns buffer"))
3050        };
3051        let root = slice(header.root_dir_offset, header.root_dir_len)?;
3052        let ranges = container_section_payload_ranges(
3053            root,
3054            header.root_dir_offset,
3055            header.index_container_sections(),
3056        )?;
3057        let seal = ranges
3058            .last()
3059            .ok_or(FileError::Container("missing quad-index seal"))?;
3060        let want = crate::quad::parse_seal(slice(seal.offset, seal.len)?)?;
3061        let q = header
3062            .quad_index_section()
3063            .ok_or(FileError::Container("missing quad-index section"))?;
3064        if crate::quad::digest(slice(q.offset, q.length)?) != want {
3065            return Ok(false);
3066        }
3067    }
3068    for s in self_digested_sections(&header) {
3069        let section = checked_slice(bytes, s.offset, s.length)
3070            .ok_or(FileError::Container("section overruns buffer"))?;
3071        if !crate::label_index::digest_ok(section) {
3072            return Ok(false);
3073        }
3074    }
3075    Ok(true)
3076}
3077
3078/// The sections that carry their own leading 16-byte digest rather than being
3079/// folded into the header content hash: LABEL_INDEX (8), SCHEMA (9) and a
3080/// predicate-restricted TEXT_INDEX (11).
3081fn self_digested_sections(header: &Header) -> Vec<Section> {
3082    [
3083        header.label_index_section(),
3084        header.schema_section(),
3085        header
3086            .section(crate::header::SectionKind::TextIndexPartial)
3087            .filter(|s| s.length > 0),
3088    ]
3089    .into_iter()
3090    .flatten()
3091    .collect()
3092}
3093
3094/// Append LABEL_INDEX / SCHEMA sections (kinds 8, 9) to a finished file image,
3095/// just before its 4-byte footer, and return the new image.
3096///
3097/// They sit outside the content hash, so nothing before them moves and the
3098/// hash is unchanged: only the header's directory gains entries. An empty
3099/// payload is skipped. Each kind may be appended once; a second append of a
3100/// kind the file already carries is an error, as is any other kind.
3101pub fn append_sections(
3102    image: &[u8],
3103    sections: &[(SectionKind, &[u8])],
3104) -> Result<Vec<u8>, FileError> {
3105    let lens: Vec<(SectionKind, u64)> =
3106        sections.iter().map(|(k, b)| (*k, b.len() as u64)).collect();
3107    let (header, body_end) = plan_append(image, image.len() as u64, &lens)?;
3108    let extra: usize = sections.iter().map(|(_, b)| b.len()).sum();
3109    let mut out = Vec::with_capacity(image.len() + extra);
3110    out.extend_from_slice(&header.to_bytes());
3111    out.extend_from_slice(&image[HEADER_LEN..body_end as usize]);
3112    for (_, b) in sections {
3113        out.extend_from_slice(b);
3114    }
3115    out.extend_from_slice(&MAGIC);
3116    Ok(out)
3117}
3118
3119/// The header an append produces, and where the old footer starts.
3120fn plan_append(
3121    head: &[u8],
3122    file_len: u64,
3123    sections: &[(SectionKind, u64)],
3124) -> Result<(Header, u64), FileError> {
3125    let mut header = Header::from_bytes(head)?;
3126    if header.expected_file_len() != Some(file_len) {
3127        return Err(FileError::Container(
3128            "cannot append sections: the file length does not match its header",
3129        ));
3130    }
3131    let body_end = file_len - MAGIC.len() as u64;
3132    let mut at = body_end;
3133    for &(kind, len) in sections {
3134        if !matches!(kind, SectionKind::LabelIndex | SectionKind::Schema) {
3135            return Err(FileError::Container(
3136                "only LABEL_INDEX and SCHEMA sections can be appended",
3137            ));
3138        }
3139        if len == 0 {
3140            continue;
3141        }
3142        if header.section(kind).is_some_and(|s| s.length > 0) {
3143            return Err(FileError::Container(
3144                "the file already carries that section",
3145            ));
3146        }
3147        header = header.with_section(kind, at, len);
3148        at += len;
3149    }
3150    Ok((header, body_end))
3151}
3152
3153/// Open a file's dictionary over a [`RangeReader`] with only its chunk
3154/// directories resident — for the memory-bounded build, which resolves the
3155/// SCHEMA section's few class and predicate ids through the file it just
3156/// wrote.
3157#[cfg(not(target_arch = "wasm32"))]
3158pub(crate) fn open_dictionary_lazy<R: RangeReader + Send + Sync + 'static>(
3159    reader: R,
3160) -> Result<Dictionary, FileError> {
3161    let head = reader.read_at(0, HEADER_LEN as u64)?;
3162    let header = Header::from_bytes(&head)?;
3163    ranged_chunked_dictionary(&std::sync::Arc::new(reader), &header, [true; 4])
3164}
3165
3166/// [`verify`] over a [`RangeReader`](crate::reader::RangeReader), streaming:
3167/// the hashed sections are read in bounded slices and folded into one BLAKE3
3168/// state, so peak memory is one slice, not the file. The same sections, in
3169/// the same order, as `hash_of_sections`; on a 49 GB file the in-memory form
3170/// needs 49 GB.
3171pub fn verify_ranged(reader: &dyn crate::reader::RangeReader) -> Result<bool, FileError> {
3172    /// Read size per step: large enough that a local or remote read is
3173    /// throughput-bound, small enough to be negligible next to any budget.
3174    const SLICE: u64 = 8 << 20;
3175    let len = reader.len();
3176    if len < HEADER_LEN as u64 {
3177        return Err(FileError::Container("file shorter than its header"));
3178    }
3179    let head = reader.read_at(0, HEADER_LEN as u64)?;
3180    if crate::v06::open::is_v06(&head) {
3181        return verify_ranged_v06(reader, &head, None).map(|bad| bad.is_empty());
3182    }
3183    let header = Header::from_bytes(&head)?;
3184    let mut h = blake3::Hasher::new();
3185    for (off, n) in content_hash_parts(&header) {
3186        // Offsets come straight from the header: reject an overrun (or an
3187        // overflowing sum) before reading, as `hash_of_sections` does.
3188        if off.checked_add(n).is_none_or(|end| end > len) {
3189            return Err(FileError::Container("section overruns buffer"));
3190        }
3191        let mut at = off;
3192        while at < off + n {
3193            let take = (off + n - at).min(SLICE);
3194            h.update(&reader.read_at(at, take)?);
3195            at += take;
3196        }
3197    }
3198    let mut out = [0u8; 16];
3199    out.copy_from_slice(&h.finalize().as_bytes()[..16]);
3200    if out != header.content_hash {
3201        return Ok(false);
3202    }
3203    // The QUAD_INDEX section, against the digest in the index container's seal
3204    // (see `verify`), streamed the same way.
3205    if header.has_quad_index() || header.quad_index_section().is_some() {
3206        check_named_layout(&header)?;
3207        let sections = header.index_container_sections();
3208        let seal = locate_container_section_ranged(
3209            reader,
3210            header.root_dir_offset,
3211            header.root_dir_len,
3212            sections - 1,
3213            sections as u64,
3214        )?;
3215        let want = crate::quad::parse_seal(&reader.read_at(seal.offset, seal.len)?)?;
3216        let q = header
3217            .quad_index_section()
3218            .ok_or(FileError::Container("missing quad-index section"))?;
3219        if q.offset.checked_add(q.length).is_none_or(|end| end > len) {
3220            return Err(FileError::Container("section overruns buffer"));
3221        }
3222        let mut h = blake3::Hasher::new();
3223        let mut at = q.offset;
3224        while at < q.offset + q.length {
3225            let take = (q.offset + q.length - at).min(SLICE);
3226            h.update(&reader.read_at(at, take)?);
3227            at += take;
3228        }
3229        let mut got = [0u8; 16];
3230        got.copy_from_slice(&h.finalize().as_bytes()[..16]);
3231        if got != want {
3232            return Ok(false);
3233        }
3234    }
3235    // The self-digested sections (kinds 8, 9, 11), streamed the same way.
3236    for s in self_digested_sections(&header) {
3237        if s.length < 16 || s.offset.checked_add(s.length).is_none_or(|end| end > len) {
3238            return Err(FileError::Container("section overruns buffer"));
3239        }
3240        let want = reader.read_at(s.offset, 16)?;
3241        let mut h = blake3::Hasher::new();
3242        let mut at = s.offset + 16;
3243        while at < s.offset + s.length {
3244            let take = (s.offset + s.length - at).min(SLICE);
3245            h.update(&reader.read_at(at, take)?);
3246            at += take;
3247        }
3248        if h.finalize().as_bytes()[..16] != want[..] {
3249            return Ok(false);
3250        }
3251    }
3252    Ok(true)
3253}
3254
3255/// Faults the pyramid meta in on first access. `None` = the fetch failed.
3256type PyramidLoader = Box<dyn Fn() -> Option<PyramidMeta> + Send + Sync>;
3257
3258/// The pyramid-meta section, held either resident (eager opens) or deferred
3259/// (the lazy remote open). SPARQL never touches the pyramid, but on a Wikidata
3260/// file it can be tens of MB (114k communities, millions of superedges), so a
3261/// remote SPARQL query must not pay to fetch it — it faults in only when a
3262/// community/pyramid query actually calls [`Rete::pyramid`].
3263enum PyramidSlot {
3264    Resident(Option<PyramidMeta>),
3265    Lazy {
3266        loader: PyramidLoader,
3267        cell: std::sync::OnceLock<Option<PyramidMeta>>,
3268    },
3269}
3270
3271/// A section held resident (eager opens) or faulted in on first use (lazy
3272/// opens) — the LABEL_INDEX and SCHEMA sections. `None` inside means the file
3273/// has no such section, or it could not be read or parsed: both are optional
3274/// and derived, so a bad one degrades to "absent" (and `verify` reports it).
3275pub(crate) enum SectionSlot<T> {
3276    Resident(Option<T>),
3277    Lazy {
3278        loader: Box<dyn Fn() -> Option<T> + Send + Sync>,
3279        cell: std::sync::OnceLock<Option<T>>,
3280    },
3281}
3282
3283impl<T> SectionSlot<T> {
3284    /// The section, faulting it in on the lazy path.
3285    pub(crate) fn get(&self) -> Option<&T> {
3286        match self {
3287            SectionSlot::Resident(v) => v.as_ref(),
3288            SectionSlot::Lazy { loader, cell } => cell.get_or_init(loader).as_ref(),
3289        }
3290    }
3291
3292    /// The section only if it is resident or already faulted — never a read.
3293    pub(crate) fn get_if_loaded(&self) -> Option<&T> {
3294        match self {
3295            SectionSlot::Resident(v) => v.as_ref(),
3296            SectionSlot::Lazy { cell, .. } => cell.get().and_then(|o| o.as_ref()),
3297        }
3298    }
3299}
3300
3301/// The LABEL_INDEX of a file image held in memory.
3302fn resident_label_slot<'a>(
3303    header: &Header,
3304    region: &dyn Fn(u64, u64) -> Result<&'a [u8], FileError>,
3305) -> SectionSlot<crate::label_index::LabelIndex> {
3306    SectionSlot::Resident(header.label_index_section().and_then(|s| {
3307        let bytes = region(s.offset, s.length).ok()?;
3308        crate::label_index::LabelIndex::from_section(bytes, header.block_codec).ok()
3309    }))
3310}
3311
3312/// The SCHEMA section of a file image held in memory.
3313fn resident_schema_slot<'a>(
3314    header: &Header,
3315    region: &dyn Fn(u64, u64) -> Result<&'a [u8], FileError>,
3316) -> SectionSlot<crate::schema_section::SchemaMeta> {
3317    SectionSlot::Resident(header.schema_section().and_then(|s| {
3318        crate::schema_section::SchemaMeta::decode(region(s.offset, s.length).ok()?).ok()
3319    }))
3320}
3321
3322/// The deferred LABEL_INDEX of a ranged open: the first search reads the
3323/// section head (preamble + root, ≤ [`crate::label_index::HEAD_READ`] bytes),
3324/// then one directory node per level and the data blocks it needs.
3325fn ranged_label_slot<R: RangeReader + Send + Sync + 'static>(
3326    reader: &std::sync::Arc<R>,
3327    header: &Header,
3328) -> SectionSlot<crate::label_index::LabelIndex> {
3329    let Some(s) = header.label_index_section() else {
3330        return SectionSlot::Resident(None);
3331    };
3332    let r = reader.clone();
3333    let codec = header.block_codec;
3334    SectionSlot::Lazy {
3335        loader: Box::new(move || {
3336            let head = r
3337                .read_at(s.offset, crate::label_index::HEAD_READ.min(s.length))
3338                .ok()?;
3339            let rr = r.clone();
3340            let loader = Box::new(move |off: u64, len: u64| rr.read_at(s.offset + off, len).ok());
3341            crate::label_index::LabelIndex::from_head(&head, s.length, codec, loader).ok()
3342        }),
3343        cell: std::sync::OnceLock::new(),
3344    }
3345}
3346
3347/// The deferred SCHEMA section of a ranged open: one range read, on first use.
3348fn ranged_schema_slot<R: RangeReader + Send + Sync + 'static>(
3349    reader: &std::sync::Arc<R>,
3350    header: &Header,
3351) -> SectionSlot<crate::schema_section::SchemaMeta> {
3352    let Some(s) = header.schema_section() else {
3353        return SectionSlot::Resident(None);
3354    };
3355    let r = reader.clone();
3356    SectionSlot::Lazy {
3357        loader: Box::new(move || {
3358            let bytes = r.read_at(s.offset, s.length).ok()?;
3359            crate::schema_section::SchemaMeta::decode(&bytes).ok()
3360        }),
3361        cell: std::sync::OnceLock::new(),
3362    }
3363}
3364
3365/// Read and decode a file's SCHEMA section over a [`RangeReader`] — the
3366/// header and one range. `Ok(None)` when the file has none (or it is
3367/// malformed: the section is optional and derived).
3368pub fn read_schema_ranged<R: RangeReader + ?Sized>(
3369    reader: &R,
3370) -> Result<Option<crate::schema_section::SchemaMeta>, FileError> {
3371    let head = reader.read_at(0, HEADER_LEN as u64)?;
3372    let header = Header::from_bytes(&head)?;
3373    Ok(read_schema_with_header(reader, &header))
3374}
3375
3376fn read_schema_with_header<R: RangeReader + ?Sized>(
3377    reader: &R,
3378    header: &Header,
3379) -> Option<crate::schema_section::SchemaMeta> {
3380    if crate::v06::reads(header.version) {
3381        let b = crate::v06::open::read_schema_bytes(reader).ok()??;
3382        return crate::schema_section::SchemaMeta::decode(&b).ok();
3383    }
3384    let s = header.schema_section()?;
3385    let bytes = reader.read_at(s.offset, s.length).ok()?;
3386    crate::schema_section::SchemaMeta::decode(&bytes).ok()
3387}
3388
3389/// Faults the text index in on first search. `None` = the file has none, or the
3390/// fetch/parse failed.
3391type TextIndexLoader = Box<dyn Fn() -> Option<crate::text_index::TextIndex> + Send + Sync>;
3392
3393/// Answers the token table's byte length from the section's first ≤10 bytes —
3394/// the length varint alone, never the table it measures.
3395type TokenTableProbe = Box<dyn Fn() -> Option<u64> + Send + Sync>;
3396
3397/// The TEXT_INDEX section, held either resident (eager opens decode the whole
3398/// thing) or deferred (the lazy remote open keeps only a loader that fetches the
3399/// token table on first search, then faults posting lists one at a time). SPARQL
3400/// never touches it, so the lazy remote path keeps its small range budget.
3401///
3402/// Both variants can also answer how long the section's leading **token table**
3403/// is without faulting it — the figure a caller needs to state what a first
3404/// search will cost, since `header.text_index_len` counts the postings blob too
3405/// and overstates it by several times on a literal-heavy graph.
3406enum TextIndexSlot {
3407    Resident {
3408        index: Option<crate::text_index::TextIndex>,
3409        /// Measured from the section bytes this open already held; `None` when
3410        /// the file carries no text index.
3411        token_table_len: Option<u64>,
3412    },
3413    Lazy {
3414        loader: TextIndexLoader,
3415        cell: std::sync::OnceLock<Option<crate::text_index::TextIndex>>,
3416        /// One ≤10-byte range read, memoized in `token_table_cell`.
3417        token_table: TokenTableProbe,
3418        token_table_cell: std::sync::OnceLock<Option<u64>>,
3419    },
3420}
3421
3422impl TextIndexSlot {
3423    /// The parsed index, faulting it in (token table first) on the lazy path.
3424    fn index(&self) -> Option<&crate::text_index::TextIndex> {
3425        match self {
3426            TextIndexSlot::Resident { index, .. } => index.as_ref(),
3427            TextIndexSlot::Lazy { loader, cell, .. } => cell.get_or_init(loader).as_ref(),
3428        }
3429    }
3430
3431    /// Byte length of the section's leading token table — see
3432    /// [`Rete::text_index_token_table_len`].
3433    fn token_table_len(&self) -> Option<u64> {
3434        match self {
3435            TextIndexSlot::Resident {
3436                token_table_len, ..
3437            } => *token_table_len,
3438            TextIndexSlot::Lazy {
3439                token_table,
3440                token_table_cell,
3441                ..
3442            } => *token_table_cell.get_or_init(token_table),
3443        }
3444    }
3445}
3446
3447/// Decode a whole TEXT_INDEX section held in memory, measuring its token table
3448/// on the way past. `section` is `None` when the file carries no text index.
3449fn resident_text_index_slot(section: Option<&[u8]>, codec: u8) -> Result<TextIndexSlot, FileError> {
3450    let Some(section) = section else {
3451        return Ok(TextIndexSlot::Resident {
3452            index: None,
3453            token_table_len: None,
3454        });
3455    };
3456    Ok(TextIndexSlot::Resident {
3457        index: Some(
3458            crate::text_index::TextIndex::from_section(section, codec)
3459                .map_err(|_| FileError::Container("malformed text index"))?,
3460        ),
3461        token_table_len: crate::text_index::TextIndex::postings_base(section).map(|b| b as u64),
3462    })
3463}
3464
3465/// Byte length of the TEXT_INDEX section's leading **token table**, from a
3466/// header a caller has already parsed — the prefix range a first
3467/// [`Rete::text_search`] fetches, and the honest figure to quote as its cost
3468/// (`header.text_index_len` counts the postings blob too and overstates it
3469/// several-fold). `None` when the file carries no text index, or its first
3470/// bytes could not be read.
3471///
3472/// Costs **one range read of ≤10 bytes** — the section's leading length varint
3473/// and nothing else, never the table it measures. This is the free-standing
3474/// companion to [`Rete::text_index_token_table_len`], for readers that hold a
3475/// [`Header`] and a [`RangeReader`] but never open the file (the CARD tier:
3476/// `rete card`, `rete card-url`).
3477pub fn read_text_index_token_table_len_ranged<R: RangeReader + ?Sized>(
3478    reader: &R,
3479    header: &Header,
3480) -> Option<u64> {
3481    if crate::v06::reads(header.version) {
3482        return crate::v06::open::read_token_table_bytes(reader);
3483    }
3484    let (ti_off, ti_len) = header.text_index_payload();
3485    if ti_len == 0 {
3486        return None;
3487    }
3488    read_token_table_len(reader, ti_off, ti_len)
3489}
3490
3491/// Byte length of the TEXT_INDEX section's leading token table — its length
3492/// varint plus the compressed table, i.e. the prefix range a first search
3493/// fetches. Reads the section's first ≤10 bytes and nothing else.
3494fn read_token_table_len<R: RangeReader + ?Sized>(reader: &R, off: u64, len: u64) -> Option<u64> {
3495    let head = reader.read_at(off, 10u64.min(len)).ok()?;
3496    let (ttlen, n) = crate::varint::read_uvarint(&head)?;
3497    Some((n as u64 + ttlen).min(len))
3498}
3499
3500/// A named graph's container decodes RESIDENT (one range read, one decode) up
3501/// to this size on the lazy path; a larger container opens as a remote-lazy
3502/// tile index instead, so one selective `GRAPH <g>` query over a multi-GB
3503/// graph faults only the tile directories plus the tiles it touches.
3504const NAMED_GRAPH_RESIDENT_MAX: u64 = 1 << 20; // 1 MiB
3505
3506/// Largest quad-index graph (in quads) a many-graph walk evaluates through an
3507/// owned, dropped view ([`Rete::named_graph_transient`]) rather than a memoised
3508/// one. A view is ~1–2 KB of descriptors whatever the graph's size, so keeping
3509/// one per graph is what grows with the graph count; past this size a graph is
3510/// worth memoising, and its rows are no longer worth materializing per graph.
3511pub const TRANSIENT_GRAPH_QUADS: u64 = 1024;
3512
3513/// Entries per lazily-allocated named-graph slab. The outer table is sized by
3514/// the section's leading count varint, which is UNTRUSTED — slabbing keeps a
3515/// hostile count from ballooning the allocation (the outer table costs a few
3516/// bytes per claimed slab; entry storage only materializes when touched).
3517const NAMED_SLAB: usize = 1024;
3518
3519/// First read of the named-graph walk, and the floor its ramp resets to. One
3520/// chunk covers a few dozen small-graph records, so a `LIMIT`ed `GRAPH ?g`
3521/// pays exactly one small read — the targeted-query win this size protects.
3522const NAMED_WALK_CHUNK: u64 = 64 * 1024;
3523
3524/// Ceiling of the walk's geometric read ramp — the read size an exhaustive
3525/// walk converges to (and starts at, when the demand is known exhaustive).
3526///
3527/// Sized from measurement, not taste. Against the nkod reference file
3528/// (67.9 MB, 31,974 named graphs, 65.4 MB section) the fixed 64 KiB walk cost
3529/// 262 requests / ~16.5 s in Chromium where the old eager whole-section fetch
3530/// cost ~8.5 s — i.e. ~31 ms of per-request overhead on ~8 s of transfer
3531/// (~7.7 MB/s). At 8 MiB per read a chunk transfers in ~1 s, putting the
3532/// request overhead near 3% of total time; halving the cap to 4 MiB doubles
3533/// that overhead, doubling the cap to 16 MiB buys back under 2% while doubling
3534/// both the walk buffer a wasm32 session must hold resident and the worst-case
3535/// over-read past the point a query stops. 8 MiB is the knee.
3536const NAMED_WALK_CHUNK_MAX: u64 = 8 * 1024 * 1024;
3537
3538/// One named graph, walked lazily out of the NAMED_GRAPHS section.
3539#[derive(Default)]
3540struct NamedEntry {
3541    /// `(iri, absolute byte range of the graph's index container)` — filled
3542    /// when the sequential directory walk reaches this slot.
3543    meta: std::sync::OnceLock<(String, ByteRange)>,
3544    /// The graph's permutation index, opened on first access. Boxed: most
3545    /// entries of a many-graph dataset are never touched by a given query.
3546    index: std::sync::OnceLock<Box<GraphIndex>>,
3547}
3548
3549/// Sequential position of the lazy named-graphs walk. The section has no
3550/// random-access directory — each entry's offset is the previous entry's end —
3551/// so entries are parsed strictly in order and memoised.
3552#[derive(Default)]
3553struct NamedWalk {
3554    /// Next entry index to parse.
3555    next: usize,
3556    /// Absolute file offset of that entry's header; `0` = the leading count
3557    /// varint has not been consumed yet (offset 0 is the file header, never
3558    /// inside a section).
3559    pos: u64,
3560    /// Read buffer carried ACROSS walk calls: `buf` holds the section bytes at
3561    /// `[buf_off, buf_off + buf.len())`. Entry-by-entry iteration (the
3562    /// `GRAPH ?g` path advances one entry per call) parses ~30 small-graph
3563    /// headers per 64 KiB chunk instead of re-reading a chunk per entry.
3564    /// [`LazyNamedGraphs::open_graph`] also serves whole small containers out
3565    /// of it, so a bulk chunk's ride-along payloads are never re-fetched.
3566    buf: Vec<u8>,
3567    buf_off: u64,
3568    /// Current read size of the geometric ramp (`0` = not started; treated as
3569    /// [`NAMED_WALK_CHUNK`]). Doubles on every buffer refill up to
3570    /// [`NAMED_WALK_CHUNK_MAX`], so a walk that keeps going converges to bulk
3571    /// reads after fetching at most as many bytes as it already used (the
3572    /// doubling bound: wasted read-ahead ≤ useful bytes). Reset to the floor
3573    /// whenever an oversized container is parsed — see `big_seen`.
3574    chunk: u64,
3575    /// A container larger than [`NAMED_GRAPH_RESIDENT_MAX`] has been parsed.
3576    /// Such graphs open tile-lazily (their payload bytes are NOT wanted by the
3577    /// walk), so ride-along prefetch stops paying off: the ramp resets and the
3578    /// exhaustive-demand hint stops seeding full-size reads. Without this, a
3579    /// header walk over a file of multi-MB containers would fetch the payloads
3580    /// it exists to hop over.
3581    big_seen: bool,
3582}
3583
3584/// The NAMED_GRAPHS section opened lazily over a [`RangeReader`]: nothing is
3585/// fetched at open. The section is a count varint followed by
3586/// `iri_len | iri | container_len | container` records, so the directory is
3587/// walked forward on demand — header bytes only, skipping container payloads —
3588/// and each touched graph's index is decoded (or opened tile-lazily) on first
3589/// access. A `LIMIT`ed `GRAPH ?g` query therefore reads a prefix of the
3590/// section; only a query that genuinely touches every graph (e.g. a full
3591/// `COUNT` over `GRAPH ?g`) walks it all — which is exactly what the eager
3592/// open used to fetch unconditionally.
3593///
3594/// **Failure contract** (same as lazy tiles/chunks): a failed read or a
3595/// malformed record sets a sticky flag and the accessor returns `None`;
3596/// nothing failed is ever memoised, so a later evaluation retries.
3597struct LazyNamedGraphs {
3598    reader: std::sync::Arc<dyn RangeReader + Send + Sync>,
3599    /// Absolute byte range of the NAMED_GRAPHS section.
3600    section: ByteRange,
3601    codec: u8,
3602    has_synopsis: bool,
3603    read_concurrency: usize,
3604    /// The file's permutation set — every graph container in a file carries
3605    /// the same one, so it comes from the header rather than per graph.
3606    perms: PermSet,
3607    /// `(count, slab table)` — set once the leading count varint is read.
3608    /// Slabs allocate on first touch; boxed slices never move, so `&` handed
3609    /// out to entries stay valid for `&self`'s lifetime with no unsafe.
3610    #[allow(clippy::type_complexity)]
3611    dir: std::sync::OnceLock<(usize, Box<[std::sync::OnceLock<Box<[NamedEntry]>>]>)>,
3612    walk: std::sync::Mutex<NamedWalk>,
3613    failed: std::sync::atomic::AtomicBool,
3614    /// Tile-cache byte cap applied to each LARGE (lazily-opened, loader-bearing)
3615    /// named graph as it is opened (bounded-export phase 2). `u64::MAX` =
3616    /// unlimited. Small resident graphs (no loader, ≤ 1 MiB) are never capped —
3617    /// an evicted tile could not be reloaded — but they are bounded by size and
3618    /// released per slot, so they need no cap.
3619    tile_cap: std::sync::atomic::AtomicU64,
3620}
3621
3622impl LazyNamedGraphs {
3623    fn new(
3624        reader: std::sync::Arc<dyn RangeReader + Send + Sync>,
3625        section: ByteRange,
3626        codec: u8,
3627        has_synopsis: bool,
3628        read_concurrency: usize,
3629        perms: PermSet,
3630    ) -> Self {
3631        LazyNamedGraphs {
3632            reader,
3633            section,
3634            codec,
3635            has_synopsis,
3636            read_concurrency,
3637            perms,
3638            dir: std::sync::OnceLock::new(),
3639            walk: std::sync::Mutex::new(NamedWalk::default()),
3640            failed: std::sync::atomic::AtomicBool::new(false),
3641            tile_cap: std::sync::atomic::AtomicU64::new(u64::MAX),
3642        }
3643    }
3644
3645    /// Set the tile-cache cap for large named graphs: records it for graphs
3646    /// opened later, and re-caps every large graph already opened. Small
3647    /// resident (loaderless) graphs are left unlimited.
3648    fn set_tile_cap(&self, cap: u64) {
3649        self.tile_cap
3650            .store(cap, std::sync::atomic::Ordering::Relaxed);
3651        self.for_each_opened(|g| {
3652            if g.is_lazy() {
3653                g.set_cache_cap(cap);
3654            }
3655        });
3656    }
3657
3658    /// Record a fetch/parse failure (checked by `index_incomplete`).
3659    fn fail(&self) {
3660        self.failed
3661            .store(true, std::sync::atomic::Ordering::Relaxed);
3662    }
3663
3664    /// The directory scaffold, reading the leading count varint on first call.
3665    #[allow(clippy::type_complexity)]
3666    fn directory(&self) -> Option<&(usize, Box<[std::sync::OnceLock<Box<[NamedEntry]>>]>)> {
3667        if let Some(d) = self.dir.get() {
3668            return Some(d);
3669        }
3670        let end = self.section.offset.checked_add(self.section.len)?;
3671        let (n, used) = match read_uvarint_at(&self.reader, self.section.offset, end) {
3672            Ok(v) => v,
3673            Err(_) => {
3674                self.fail();
3675                return None;
3676            }
3677        };
3678        // Each record needs at least 2 bytes of framing (two length varints),
3679        // so a count beyond len/2 is malformed — refuse before sizing by it.
3680        if n > self.section.len / 2 {
3681            self.fail();
3682            return None;
3683        }
3684        let Ok(n) = usize::try_from(n) else {
3685            self.fail();
3686            return None;
3687        };
3688        let slabs = n.div_ceil(NAMED_SLAB);
3689        let table: Box<[std::sync::OnceLock<Box<[NamedEntry]>>]> =
3690            (0..slabs).map(|_| std::sync::OnceLock::new()).collect();
3691        let _ = self.dir.set((n, table));
3692        {
3693            // Idempotent under an init race: both racers computed the same
3694            // `used` from the same bytes.
3695            let mut w = self.walk.lock().unwrap();
3696            if w.pos == 0 {
3697                w.pos = self.section.offset + used;
3698            }
3699        }
3700        self.dir.get()
3701    }
3702
3703    fn count(&self) -> usize {
3704        self.directory().map(|(n, _)| *n).unwrap_or(0)
3705    }
3706
3707    /// The `i`-th entry's cell (allocating its slab on first touch).
3708    fn entry(&self, i: usize) -> Option<&NamedEntry> {
3709        let (n, table) = self.directory()?;
3710        if i >= *n {
3711            return None;
3712        }
3713        let slab = table[i / NAMED_SLAB].get_or_init(|| {
3714            let len = NAMED_SLAB.min(n - (i / NAMED_SLAB) * NAMED_SLAB);
3715            (0..len).map(|_| NamedEntry::default()).collect()
3716        });
3717        slab.get(i % NAMED_SLAB)
3718    }
3719
3720    /// The next walk read's length, advancing the geometric ramp: at least
3721    /// `need` (a long IRI's header), at most the section remainder, and
3722    /// otherwise the current ramp value — which doubles per refill up to
3723    /// [`NAMED_WALK_CHUNK_MAX`]. The doubling keeps a stopping walk's wasted
3724    /// read-ahead bounded by the bytes it already used, while a walk that
3725    /// keeps going converges to bulk reads: the runtime safety net for
3726    /// exhaustive-looking queries the demand analysis could not prove.
3727    fn next_read_len(w: &mut NamedWalk, pos: u64, end: u64, need: u64) -> u64 {
3728        let cur = w.chunk.max(NAMED_WALK_CHUNK);
3729        w.chunk = (cur * 2).min(NAMED_WALK_CHUNK_MAX);
3730        cur.max(need).min(end - pos)
3731    }
3732
3733    /// Walk entry headers forward until `entries[upto].meta` is set. Reads the
3734    /// section in chunks and hops over container payloads, so the walk fetches
3735    /// O(headers) — not O(section) — where containers are large; small
3736    /// containers ride along in the same chunk and cost nothing extra.
3737    ///
3738    /// `exhaustive` is the demand hint from the query engine: the caller has
3739    /// PROVEN it will visit and open every named graph (an unrestricted
3740    /// `GRAPH ?g` under a consumer that drains the pipeline), so the walk
3741    /// starts at full-size reads instead of ramping up to them — the whole
3742    /// section arrives in `section/8 MiB` requests instead of hundreds. The
3743    /// hint is per-call, never memoised: a later targeted query on the same
3744    /// handle walks small again. It also defers to the layout: once an
3745    /// oversized (tile-lazy) container is seen, ride-along bytes stop being
3746    /// useful and the hint no longer seeds bulk reads.
3747    fn ensure_meta(&self, upto: usize, exhaustive: bool) -> Option<()> {
3748        let target = self.entry(upto)?;
3749        if target.meta.get().is_some() {
3750            return Some(());
3751        }
3752        let end = self.section.offset.checked_add(self.section.len)?;
3753        let mut w = self.walk.lock().unwrap();
3754        if exhaustive && !w.big_seen {
3755            w.chunk = NAMED_WALK_CHUNK_MAX;
3756        }
3757        while w.next <= upto {
3758            let pos = w.pos;
3759            if pos >= end {
3760                // The count varint promised more entries than the bytes hold.
3761                self.fail();
3762                return None;
3763            }
3764            // Make sure the whole header (two varints + the IRI, ≤ 20+iri_len
3765            // bytes) is buffered; refetch a larger window when the IRI is long.
3766            let have =
3767                |b: &[u8], off: u64, need: u64| pos >= off && pos + need <= off + b.len() as u64;
3768            if !have(&w.buf, w.buf_off, 20.min(end - pos)) {
3769                let len = Self::next_read_len(&mut w, pos, end, 20.min(end - pos));
3770                w.buf = match self.reader.read_at(pos, len) {
3771                    Ok(b) => b,
3772                    Err(_) => {
3773                        self.fail();
3774                        return None;
3775                    }
3776                };
3777                w.buf_off = pos;
3778            }
3779            // Every slice of `buf` below is checked: a reader may return fewer
3780            // bytes than asked, and the lengths are untrusted.
3781            let rel = (pos - w.buf_off) as usize;
3782            let Some((ilen, u1)) = w.buf.get(rel..).and_then(read_uvarint) else {
3783                self.fail();
3784                return None;
3785            };
3786            let Some(iri_end) = (u1 as u64)
3787                .checked_add(ilen)
3788                .and_then(|n| pos.checked_add(n))
3789                .filter(|&e| e <= end)
3790            else {
3791                self.fail(); // IRI overruns the section: malformed
3792                return None;
3793            };
3794            // iri_len + iri + container_len varint
3795            let header_need = (iri_end - pos).saturating_add(10);
3796            if !have(&w.buf, w.buf_off, header_need.min(end - pos)) {
3797                let len = Self::next_read_len(&mut w, pos, end, header_need);
3798                w.buf = match self.reader.read_at(pos, len) {
3799                    Ok(b) => b,
3800                    Err(_) => {
3801                        self.fail();
3802                        return None;
3803                    }
3804                };
3805                w.buf_off = pos;
3806            }
3807            let rel = (pos - w.buf_off) as usize;
3808            let istart = rel + u1;
3809            let Some(iend) = usize::try_from(ilen)
3810                .ok()
3811                .and_then(|l| istart.checked_add(l))
3812                .filter(|&e| e <= w.buf.len())
3813            else {
3814                self.fail(); // short read: the header is not all here
3815                return None;
3816            };
3817            let iri = String::from_utf8_lossy(&w.buf[istart..iend]).into_owned();
3818            let Some((clen, u2)) = read_uvarint(&w.buf[iend..]) else {
3819                self.fail();
3820                return None;
3821            };
3822            let cend = match iri_end
3823                .checked_add(u2 as u64)
3824                .and_then(|cstart| cstart.checked_add(clen))
3825            {
3826                Some(e) if e <= end => e,
3827                _ => {
3828                    self.fail(); // container overruns the section: malformed
3829                    return None;
3830                }
3831            };
3832            if clen > NAMED_GRAPH_RESIDENT_MAX {
3833                // This graph opens tile-lazily: the walk exists to HOP its
3834                // payload, so big read-ahead here is pure over-fetch. Drop the
3835                // ramp back to the floor and stop the exhaustive hint from
3836                // re-seeding it (headers stay cheap on payload-heavy files).
3837                w.big_seen = true;
3838                w.chunk = NAMED_WALK_CHUNK;
3839            }
3840            let range = ByteRange {
3841                offset: cend - clen,
3842                len: clen,
3843            };
3844            if let Some(e) = self.entry(w.next) {
3845                let _ = e.meta.set((iri, range));
3846            }
3847            w.pos = cend;
3848            w.next += 1;
3849        }
3850        Some(())
3851    }
3852
3853    /// The `i`-th graph's IRI — a header walk, never an index decode.
3854    /// `exhaustive`: see [`ensure_meta`](Self::ensure_meta).
3855    fn name_at(&self, i: usize, exhaustive: bool) -> Option<&str> {
3856        self.ensure_meta(i, exhaustive)?;
3857        self.entry(i)?.meta.get().map(|(iri, _)| iri.as_str())
3858    }
3859
3860    /// The `i`-th graph as `(iri, index)`, opening the index on first access.
3861    /// `exhaustive`: see [`ensure_meta`](Self::ensure_meta).
3862    fn graph_at(&self, i: usize, exhaustive: bool) -> Option<(&str, &GraphIndex)> {
3863        self.ensure_meta(i, exhaustive)?;
3864        let e = self.entry(i)?;
3865        let (iri, range) = e.meta.get()?;
3866        if e.index.get().is_none() {
3867            let opened = self.open_graph(*range)?;
3868            let _ = e.index.set(Box::new(opened));
3869        }
3870        Some((iri.as_str(), e.index.get()?.as_ref()))
3871    }
3872
3873    /// A small container's bytes: out of the walk buffer when the last chunk
3874    /// already carried them (bulk chunks always do — that is their point),
3875    /// else one targeted range read. Never called for tile-lazy containers.
3876    fn container_bytes(&self, range: ByteRange) -> Option<Vec<u8>> {
3877        {
3878            let w = self.walk.lock().unwrap();
3879            let buf_end = w.buf_off + w.buf.len() as u64;
3880            if range.offset >= w.buf_off && range.offset + range.len <= buf_end {
3881                let a = (range.offset - w.buf_off) as usize;
3882                return Some(w.buf[a..a + range.len as usize].to_vec());
3883            }
3884        }
3885        match self.reader.read_at(range.offset, range.len) {
3886            // A short read is a failed read, not a shorter container.
3887            Ok(b) if b.len() as u64 == range.len => Some(b),
3888            _ => {
3889                self.fail();
3890                None
3891            }
3892        }
3893    }
3894
3895    /// Open one graph's index container: small ones resident (one read),
3896    /// large ones as a remote-lazy tile index — the same machinery as the
3897    /// default graph, so a selective query over a huge named graph fetches
3898    /// only the tile directories plus the tiles it touches.
3899    fn open_graph(&self, range: ByteRange) -> Option<GraphIndex> {
3900        if range.len <= NAMED_GRAPH_RESIDENT_MAX {
3901            let bytes = self.container_bytes(range)?;
3902            match decode_index_container(&bytes, self.codec, self.perms) {
3903                Ok(g) => {
3904                    // Resident, so counting is a pass over tile headers: the
3905                    // planner sizes this graph's patterns by it.
3906                    g.set_size_hint(u64::from(g.triple_count()));
3907                    Some(g)
3908                }
3909                Err(_) => {
3910                    self.fail();
3911                    None
3912                }
3913            }
3914        } else {
3915            match open_index_container_lazy(
3916                &self.reader,
3917                range,
3918                self.codec,
3919                self.has_synopsis,
3920                self.read_concurrency,
3921                self.perms,
3922                self.perms.len(),
3923            ) {
3924                Ok((g, _, _)) => {
3925                    // A large graph faults its tiles through a loader, so bound
3926                    // its tile residency to the budget's share (an evicted tile
3927                    // simply re-faults). No-op when unlimited.
3928                    let cap = self.tile_cap.load(std::sync::atomic::Ordering::Relaxed);
3929                    if cap != u64::MAX {
3930                        g.set_cache_cap(cap);
3931                    }
3932                    Some(g)
3933                }
3934                Err(_) => {
3935                    self.fail();
3936                    None
3937                }
3938            }
3939        }
3940    }
3941
3942    /// Drop the decoded index of the graph named `iri`, if it has been opened,
3943    /// so its tiles and per-graph tile cache are freed. Takes `&mut self`:
3944    /// `OnceLock::take` needs unique access, which the export loop has between
3945    /// slots. Leaves the entry's `meta` (iri + range) so the graph re-opens
3946    /// lazily if touched again. A no-op if the graph is absent or never opened.
3947    fn release(&mut self, iri: &str) {
3948        let Some((_, table)) = self.dir.get_mut() else {
3949            return;
3950        };
3951        for slab in table.iter_mut() {
3952            let Some(entries) = slab.get_mut() else {
3953                continue;
3954            };
3955            for e in entries.iter_mut() {
3956                let matches = e
3957                    .meta
3958                    .get()
3959                    .map(|(name, _)| name.as_str() == iri)
3960                    .unwrap_or(false);
3961                if matches {
3962                    e.index.take();
3963                    return;
3964                }
3965            }
3966        }
3967    }
3968
3969    /// Visit every already-opened graph index (never triggers a fetch).
3970    fn for_each_opened(&self, mut f: impl FnMut(&GraphIndex)) {
3971        if let Some((_, table)) = self.dir.get() {
3972            for slab in table.iter().filter_map(|s| s.get()) {
3973                for e in slab.iter() {
3974                    if let Some(g) = e.index.get() {
3975                        f(g);
3976                    }
3977                }
3978            }
3979        }
3980    }
3981}
3982
3983/// The NAMED_GRAPHS section, held resident (in-memory and eager ranged opens —
3984/// unchanged) or walked lazily (the lazy ranged open, where fetching and
3985/// decoding every graph's index up front defeated remote laziness: 67 MB
3986/// fetched and ~32k indexes built before the first query on a many-graph file).
3987enum NamedGraphsSlot {
3988    Resident(Vec<(String, GraphIndex)>),
3989    Lazy(LazyNamedGraphs),
3990    /// The quad index (kind 10), on every open path: graphs are views over
3991    /// shared tiles, found through the dictionary and the sorted directory.
3992    Quad(crate::quad::QuadNamed),
3993}
3994
3995impl NamedGraphsSlot {
3996    /// Heap bytes the named-graph directory holds outside any cache: every
3997    /// walked entry's IRI and range, the walk buffer, and the directories of
3998    /// the graph indexes currently open (see [`GraphIndex::directory_bytes`]).
3999    fn directory_bytes(&self) -> u64 {
4000        match self {
4001            NamedGraphsSlot::Resident(v) => v
4002                .iter()
4003                .map(|(iri, g)| iri.capacity() as u64 + g.directory_bytes())
4004                .sum(),
4005            NamedGraphsSlot::Lazy(l) => {
4006                let mut bytes = l.walk.lock().map(|w| w.buf.capacity() as u64).unwrap_or(0);
4007                if let Some((_, table)) = l.dir.get() {
4008                    bytes += table.len() as u64
4009                        * std::mem::size_of::<std::sync::OnceLock<Box<[NamedEntry]>>>() as u64;
4010                    for slab in table.iter().filter_map(|s| s.get()) {
4011                        bytes += slab.len() as u64 * std::mem::size_of::<NamedEntry>() as u64;
4012                        for e in slab.iter() {
4013                            if let Some((iri, _)) = e.meta.get() {
4014                                bytes += iri.capacity() as u64;
4015                            }
4016                            if let Some(g) = e.index.get() {
4017                                bytes += g.directory_bytes();
4018                            }
4019                        }
4020                    }
4021                }
4022                bytes
4023            }
4024            NamedGraphsSlot::Quad(q) => q.directory_bytes(),
4025        }
4026    }
4027
4028    fn count(&self) -> usize {
4029        match self {
4030            NamedGraphsSlot::Resident(v) => v.len(),
4031            NamedGraphsSlot::Lazy(l) => l.count(),
4032            NamedGraphsSlot::Quad(q) => q.count(),
4033        }
4034    }
4035
4036    /// `exhaustive` is the walk-demand hint (resident opens ignore it): see
4037    /// [`LazyNamedGraphs::ensure_meta`]. `dict` names the graphs of a quad
4038    /// index (their names are dictionary terms there).
4039    fn name_at<'a>(&'a self, dict: &Dictionary, i: usize, exhaustive: bool) -> Option<&'a str> {
4040        match self {
4041            NamedGraphsSlot::Resident(v) => v.get(i).map(|(iri, _)| iri.as_str()),
4042            NamedGraphsSlot::Lazy(l) => l.name_at(i, exhaustive),
4043            NamedGraphsSlot::Quad(q) => q.name_at(dict, i, exhaustive),
4044        }
4045    }
4046
4047    /// [`name_at`](Self::name_at) without keeping the name: a quad-index walk
4048    /// over many graphs must not memoise one string per graph it passes.
4049    fn name_owned(&self, dict: &Dictionary, i: usize, exhaustive: bool) -> Option<String> {
4050        match self {
4051            NamedGraphsSlot::Quad(q) => q.name_owned(dict, i, exhaustive),
4052            _ => self.name_at(dict, i, exhaustive).map(str::to_owned),
4053        }
4054    }
4055
4056    fn graph_at<'a>(
4057        &'a self,
4058        dict: &Dictionary,
4059        i: usize,
4060        exhaustive: bool,
4061    ) -> Option<(&'a str, &'a GraphIndex)> {
4062        match self {
4063            NamedGraphsSlot::Resident(v) => v.get(i).map(|(iri, g)| (iri.as_str(), g)),
4064            NamedGraphsSlot::Lazy(l) => l.graph_at(i, exhaustive),
4065            NamedGraphsSlot::Quad(q) => q.graph_at(dict, i, exhaustive),
4066        }
4067    }
4068
4069    fn find<'a>(&'a self, dict: &Dictionary, iri: &str) -> Option<&'a GraphIndex> {
4070        match self {
4071            NamedGraphsSlot::Quad(q) => q.find(dict, iri),
4072            NamedGraphsSlot::Resident(v) => v.iter().find(|(name, _)| name == iri).map(|(_, g)| g),
4073            NamedGraphsSlot::Lazy(l) => {
4074                // Walk headers only until the IRI matches; decode just that
4075                // graph's index. An absent IRI costs a full header walk — the
4076                // layout has no by-name directory. A point lookup is targeted
4077                // demand: never hint the walk exhaustive.
4078                for i in 0..l.count() {
4079                    if l.name_at(i, false)? == iri {
4080                        return l.graph_at(i, false).map(|(_, g)| g);
4081                    }
4082                }
4083                None
4084            }
4085        }
4086    }
4087
4088    fn load_incomplete(&self) -> bool {
4089        match self {
4090            NamedGraphsSlot::Resident(v) => v.iter().any(|(_, g)| g.load_incomplete()),
4091            NamedGraphsSlot::Lazy(l) => {
4092                if l.failed.load(std::sync::atomic::Ordering::Relaxed) {
4093                    return true;
4094                }
4095                let mut bad = false;
4096                l.for_each_opened(|g| bad |= g.load_incomplete());
4097                bad
4098            }
4099            NamedGraphsSlot::Quad(q) => q.load_incomplete(),
4100        }
4101    }
4102
4103    fn reset_load_failures(&self) {
4104        match self {
4105            NamedGraphsSlot::Resident(v) => {
4106                for (_, g) in v {
4107                    g.reset_load_failure();
4108                }
4109            }
4110            NamedGraphsSlot::Lazy(l) => {
4111                l.failed.store(false, std::sync::atomic::Ordering::Relaxed);
4112                l.for_each_opened(|g| g.reset_load_failure());
4113            }
4114            NamedGraphsSlot::Quad(q) => q.reset_load_failures(),
4115        }
4116    }
4117
4118    /// The named-graph slot of a file opened with all its bytes in hand
4119    /// (`Rete::open`, `open_ranged`): the per-graph section decoded resident,
4120    /// or the quad index read through an owned copy of its section.
4121    fn resident(
4122        header: &Header,
4123        quad_section: impl FnOnce(u64, u64) -> Result<Vec<u8>, FileError>,
4124        per_graph: impl FnOnce() -> Result<Vec<(String, GraphIndex)>, FileError>,
4125    ) -> Result<Self, FileError> {
4126        check_named_layout(header)?;
4127        if let Some(q) = header.quad_index_section() {
4128            let bytes = quad_section(q.offset, q.length)?;
4129            return Ok(NamedGraphsSlot::Quad(crate::quad::QuadNamed::resident(
4130                bytes,
4131                header.block_codec,
4132                header.perms,
4133            )));
4134        }
4135        Ok(NamedGraphsSlot::Resident(if header.named_graphs_len > 0 {
4136            per_graph()?
4137        } else {
4138            Vec::new()
4139        }))
4140    }
4141}
4142
4143/// A read-only, in-memory view over a `.rete` file image.
4144pub struct Rete {
4145    header: Header,
4146    dict: Dictionary,
4147    index: GraphIndex,
4148    index_section_ranges: [ByteRange; NUM_PERMS],
4149    /// Per-permutation tile directories as absolute file ranges
4150    /// (`(min_a, max_a, compressed-tile range)`), for provenance. Empty for
4151    /// pre-tiling (v0.1) files.
4152    tile_ranges: [Vec<(u32, u32, ByteRange)>; NUM_PERMS],
4153    pyramid: PyramidSlot,
4154    text_index: TextIndexSlot,
4155    /// The LABEL_INDEX section (kind 8), if any.
4156    labels: SectionSlot<crate::label_index::LabelIndex>,
4157    /// The SCHEMA section (kind 9), if any.
4158    schema: SectionSlot<crate::schema_section::SchemaMeta>,
4159    named_graphs: NamedGraphsSlot,
4160    /// Raw bytes of the metadata section (empty if the file has none). The
4161    /// application layer decodes this (the CLI stores a JSON Dataset Card here).
4162    /// Only [`Rete::open`] populates it; [`Rete::open_ranged`] leaves it empty to
4163    /// preserve its minimal-fetch budget.
4164    metadata: Vec<u8>,
4165    /// Executes remote `SERVICE` blocks (SPARQL 1.1 federated query) — attached
4166    /// by the host via [`Rete::set_service_client`]; `None` means a non-SILENT
4167    /// `SERVICE` fails the query. Like the range readers, the engine never does
4168    /// I/O itself.
4169    service_client: Option<Box<dyn crate::service::ServiceClient>>,
4170    /// First failed non-SILENT `SERVICE` call of the current query. The row
4171    /// pipeline is infallible (the same contract as lazy tile fetches), so the
4172    /// failure is recorded here and taken by the top-level eval entry points.
4173    service_error: std::sync::Mutex<Option<String>>,
4174    /// Bytes a query's `COUNT(DISTINCT …)` set may hold before it spills to
4175    /// disk — see [`Rete::spill_threshold`].
4176    spill_threshold: std::sync::atomic::AtomicU64,
4177    /// The open `0x06` file (header, root, reader), for a `0x06` open.
4178    v06: Option<std::sync::Arc<crate::v06::open::V06File>>,
4179}
4180
4181impl Rete {
4182    /// Parse a full file image (v0 loads everything; a range-reading client
4183    /// will fetch only the sections it needs — same container format).
4184    pub fn open(bytes: &[u8]) -> Result<Self, FileError> {
4185        #[cfg(feature = "v06-parity")]
4186        if let Some(v6) = parity_image(bytes) {
4187            return Self::open(&v6);
4188        }
4189        if crate::v06::open::is_v06(bytes) {
4190            // A 0x06 image opens through the lazy machinery over the bytes in
4191            // memory: the same structures, faulted from RAM.
4192            let r: std::sync::Arc<dyn RangeReader + Send + Sync> =
4193                std::sync::Arc::new(crate::reader::BytesReader(std::sync::Arc::from(bytes)));
4194            let mut rete = Self::open_v06(r, bytes, false)?;
4195            rete.metadata = rete
4196                .v06
4197                .as_ref()
4198                .and_then(|f| f.card().ok().flatten())
4199                .unwrap_or_default();
4200            return Ok(rete);
4201        }
4202        let header = Header::from_bytes(bytes)?;
4203
4204        // Header offsets/lengths are untrusted (a `.rete` may be fetched truncated
4205        // or corrupt from an arbitrary URL). Slice through a checked helper so a
4206        // bad region yields an error instead of panicking on an OOB index.
4207        let region = |off: u64, len: u64| -> Result<&[u8], FileError> {
4208            checked_slice(bytes, off, len)
4209                .ok_or(FileError::Container("section range out of bounds"))
4210        };
4211
4212        let dict = decode_dictionary_container(
4213            region(header.dictionary_offset, header.dictionary_len)?,
4214            header.dict_codec,
4215        )?;
4216
4217        check_named_layout(&header)?;
4218        let index_bytes = region(header.root_dir_offset, header.root_dir_len)?;
4219        let (index, _) = decode_root_index_container(index_bytes, &header)?;
4220        let index_section_ranges = decode_index_section_ranges(
4221            index_bytes,
4222            header.root_dir_offset,
4223            header.perms,
4224            header.index_container_sections(),
4225        )?;
4226
4227        let pyramid = PyramidSlot::Resident(if header.pyramid_meta_len > 0 {
4228            Some(
4229                PyramidMeta::decode(region(header.pyramid_meta_offset, header.pyramid_meta_len)?)
4230                    .map_err(|_| FileError::Container("malformed pyramid meta"))?,
4231            )
4232        } else {
4233            None
4234        });
4235
4236        // The TEXT_INDEX section (opt-in `--text-index`); decode the whole thing
4237        // resident on a full-image open.
4238        let (ti_off, ti_len) = header.text_index_payload();
4239        let text_index = resident_text_index_slot(
4240            if ti_len > 0 {
4241                Some(region(ti_off, ti_len)?)
4242            } else {
4243                None
4244            },
4245            header.block_codec,
4246        )?;
4247        let labels = resident_label_slot(&header, &region);
4248        let schema = resident_schema_slot(&header, &region);
4249
4250        let named_graphs = NamedGraphsSlot::resident(
4251            &header,
4252            |off, len| Ok(region(off, len)?.to_vec()),
4253            || {
4254                decode_named_graphs(
4255                    region(header.named_graphs_offset, header.named_graphs_len)?,
4256                    header.block_codec,
4257                    header.perms,
4258                )
4259            },
4260        )?;
4261
4262        let metadata = if header.metadata_len > 0 {
4263            region(header.metadata_offset, header.metadata_len)?.to_vec()
4264        } else {
4265            Vec::new()
4266        };
4267
4268        let tile_ranges =
4269            tile_file_ranges(index_bytes, header.root_dir_offset, &index_section_ranges);
4270        Ok(Self {
4271            header,
4272            dict,
4273            index,
4274            index_section_ranges,
4275            tile_ranges,
4276            pyramid,
4277            text_index,
4278            labels,
4279            schema,
4280            named_graphs,
4281            metadata,
4282            service_client: None,
4283            service_error: std::sync::Mutex::new(None),
4284            spill_threshold: std::sync::atomic::AtomicU64::new(u64::MAX),
4285            v06: None,
4286        })
4287    }
4288
4289    /// Attach the client that executes `SERVICE <endpoint> { … }` blocks
4290    /// (SPARQL 1.1 federated query) against remote SPARQL endpoints. Without
4291    /// one, a non-SILENT `SERVICE` fails the query with a clear error and a
4292    /// `SERVICE SILENT` degrades to one empty solution, per the spec.
4293    pub fn set_service_client(&mut self, client: Box<dyn crate::service::ServiceClient>) {
4294        self.service_client = Some(client);
4295    }
4296
4297    pub(crate) fn service_client(&self) -> Option<&dyn crate::service::ServiceClient> {
4298        self.service_client.as_deref()
4299    }
4300
4301    /// Record a failed non-SILENT `SERVICE` call (first error wins).
4302    pub(crate) fn record_service_error(&self, msg: &str) {
4303        let mut e = self.service_error.lock().unwrap();
4304        if e.is_none() {
4305            *e = Some(msg.to_string());
4306        }
4307    }
4308
4309    /// Take (and clear) the pending `SERVICE` failure — called by every
4310    /// top-level eval entry so it can never leak into a later query.
4311    pub(crate) fn take_service_error(&self) -> Option<String> {
4312        self.service_error.lock().unwrap().take()
4313    }
4314
4315    pub fn header(&self) -> &Header {
4316        &self.header
4317    }
4318
4319    /// The file's byte layout, for visualization: header, metadata,
4320    /// dictionary, each index permutation's tile directory and individual
4321    /// tiles, pyramid summary, and named graphs — sorted by offset. Bytes not
4322    /// covered by any segment are container framing (section directories and
4323    /// length fields).
4324    pub fn file_layout(&self) -> Vec<LayoutSegment> {
4325        if let Some(f) = &self.v06 {
4326            return v06_layout(f);
4327        }
4328        let h = &self.header;
4329        let seg = |kind: &'static str, label: String, offset: u64, len: u64| LayoutSegment {
4330            kind,
4331            label,
4332            offset,
4333            len,
4334        };
4335        // The size in the label is DERIVED from the constant that defines the
4336        // span, never typed out: this line read "fixed 128 bytes" — the
4337        // pre-v0.3 header size — while emitting a 1024-byte span, and it is
4338        // what the file explorer shows a reader inspecting a real file.
4339        // `layout_header_label_matches_its_span` pins the two together.
4340        let mut out = vec![seg(
4341            "header",
4342            format!("header (fixed {} bytes)", crate::header::HEADER_LEN),
4343            0,
4344            crate::header::HEADER_LEN as u64,
4345        )];
4346        if h.metadata_len > 0 {
4347            out.push(seg(
4348                "metadata",
4349                "metadata (dataset card)".into(),
4350                h.metadata_offset,
4351                h.metadata_len,
4352            ));
4353        }
4354        out.push(seg(
4355            "dictionary",
4356            "dictionary (4 front-coded term sections)".into(),
4357            h.dictionary_offset,
4358            h.dictionary_len,
4359        ));
4360        for (si, perm) in crate::index::ALL_PERMS.into_iter().enumerate() {
4361            let sec = self.index_section_ranges[si];
4362            if sec.len == 0 {
4363                continue;
4364            }
4365            let first_tile = self.tile_ranges[si]
4366                .first()
4367                .map(|&(_, _, r)| r.offset)
4368                .unwrap_or(sec.offset + sec.len);
4369            if first_tile > sec.offset {
4370                out.push(seg(
4371                    "directory",
4372                    format!("{} tile directory", perm.name()),
4373                    sec.offset,
4374                    first_tile - sec.offset,
4375                ));
4376            }
4377            for (ti, &(min_a, max_a, r)) in self.tile_ranges[si].iter().enumerate() {
4378                out.push(seg(
4379                    "tile",
4380                    format!("{} tile {ti} (leading ids {min_a}..{max_a})", perm.name()),
4381                    r.offset,
4382                    r.len,
4383                ));
4384            }
4385        }
4386        if h.pyramid_meta_len > 0 {
4387            out.push(seg(
4388                "pyramid",
4389                "pyramid summary (communities + superedges)".into(),
4390                h.pyramid_meta_offset,
4391                h.pyramid_meta_len,
4392            ));
4393        }
4394        if h.named_graphs_len > 0 {
4395            out.push(seg(
4396                "named-graphs",
4397                format!("named graphs ({})", self.named_graphs.count()),
4398                h.named_graphs_offset,
4399                h.named_graphs_len,
4400            ));
4401        }
4402        if let Some(s) = h.label_index_section() {
4403            let label = match self.labels.get_if_loaded() {
4404                Some(li) => format!("label index ({} entries)", li.entry_count()),
4405                None => "label index".to_string(),
4406            };
4407            out.push(seg("label-index", label, s.offset, s.length));
4408        }
4409        if let Some(s) = h.schema_section() {
4410            out.push(seg(
4411                "schema",
4412                "schema (classes, query_stats, shapes)".into(),
4413                s.offset,
4414                s.length,
4415            ));
4416        }
4417        if let Some(q) = h.quad_index_section() {
4418            out.push(seg(
4419                "quad-index",
4420                format!("quad index ({} named graphs)", self.named_graphs.count()),
4421                q.offset,
4422                q.length,
4423            ));
4424        }
4425        out.sort_by_key(|s| s.offset);
4426        out
4427    }
4428
4429    /// Raw bytes of the file's metadata section, or `None` if it has none. The
4430    /// CLI stores a JSON Dataset Card here; `rete-core` treats it as opaque.
4431    /// Populated by [`Rete::open`] only — an [`Rete::open_ranged`] view returns
4432    /// `None` here (the card is not fetched on the minimal query path).
4433    pub fn metadata(&self) -> Option<&[u8]> {
4434        if self.metadata.is_empty() {
4435            None
4436        } else {
4437            Some(&self.metadata)
4438        }
4439    }
4440
4441    pub fn dictionary(&self) -> &Dictionary {
4442        &self.dict
4443    }
4444
4445    /// The pyramid metadata (summary graph + tiles), if the file has a pyramid.
4446    pub fn pyramid(&self) -> Option<&PyramidMeta> {
4447        match &self.pyramid {
4448            PyramidSlot::Resident(p) => p.as_ref(),
4449            // Faults the (possibly large) pyramid section on first access only.
4450            PyramidSlot::Lazy { loader, cell } => cell.get_or_init(loader).as_ref(),
4451        }
4452    }
4453
4454    /// The pyramid metadata **only if already resident or previously faulted** —
4455    /// never triggers a lazy range read. The query planner uses this for
4456    /// cardinality estimation so it is free for an in-memory file and never adds
4457    /// a fetch on the lazy remote path (which defers the pyramid by design).
4458    pub fn pyramid_if_loaded(&self) -> Option<&PyramidMeta> {
4459        match &self.pyramid {
4460            PyramidSlot::Resident(p) => p.as_ref(),
4461            PyramidSlot::Lazy { cell, .. } => cell.get().and_then(|o| o.as_ref()),
4462        }
4463    }
4464
4465    /// The SCHEMA section (kind 9): class hierarchy, level rollups and links,
4466    /// coherence axioms, `query_stats`, characteristic sets. Faults it in (one
4467    /// small range read) on the lazy path. `None` when the file has none — a
4468    /// file built before it existed keeps those fields in its pyramid-meta
4469    /// ([`schema_or_pyramid`](Self::schema_or_pyramid) reads either).
4470    pub fn schema(&self) -> Option<&crate::schema_section::SchemaMeta> {
4471        self.schema.get()
4472    }
4473
4474    /// The schema fields from the SCHEMA section when the file has one, else
4475    /// from the pyramid-meta (faulting either on the lazy path). `None` when
4476    /// the file carries neither.
4477    pub fn schema_or_pyramid(
4478        &self,
4479    ) -> Option<std::borrow::Cow<'_, crate::schema_section::SchemaMeta>> {
4480        if let Some(s) = self.schema() {
4481            return Some(std::borrow::Cow::Borrowed(s));
4482        }
4483        self.pyramid()
4484            .map(|p| std::borrow::Cow::Owned(crate::schema_section::SchemaMeta::from_pyramid(p)))
4485    }
4486
4487    /// Per-predicate planner statistics (`query_stats`): from the SCHEMA
4488    /// section (faulting it on the lazy path — it is small), else from the
4489    /// pyramid-meta when that is resident. Empty when the file has neither.
4490    /// See [`crate::meta::PredStat`].
4491    pub fn predicate_stats(&self) -> &[crate::meta::PredStat] {
4492        if let Some(s) = self.schema().filter(|s| !s.predicate_stats.is_empty()) {
4493            return &s.predicate_stats;
4494        }
4495        self.pyramid_if_loaded()
4496            .map(|p| p.predicate_stats.as_slice())
4497            .unwrap_or(&[])
4498    }
4499
4500    /// The statistics the planner may use **without a read it was not going to
4501    /// make**: the SCHEMA section if it is resident or already faulted — or,
4502    /// with `fault`, after faulting it (one small range, once per session) —
4503    /// else the resident pyramid-meta's block.
4504    pub(crate) fn planner_stats(&self, fault: bool) -> &[crate::meta::PredStat] {
4505        let schema = if fault {
4506            self.schema.get()
4507        } else {
4508            self.schema.get_if_loaded()
4509        };
4510        if let Some(s) = schema.filter(|s| !s.predicate_stats.is_empty()) {
4511            return &s.predicate_stats;
4512        }
4513        self.pyramid_if_loaded()
4514            .map(|p| p.predicate_stats.as_slice())
4515            .unwrap_or(&[])
4516    }
4517
4518    /// The entity shapes (characteristic sets): from the SCHEMA section, else
4519    /// from a resident pyramid-meta. See [`crate::meta::CharSet`].
4520    pub fn char_sets(&self) -> &[crate::meta::CharSet] {
4521        if let Some(s) = self.schema().filter(|s| !s.char_sets.is_empty()) {
4522            return &s.char_sets;
4523        }
4524        self.pyramid_if_loaded()
4525            .map(|p| p.char_sets.as_slice())
4526            .unwrap_or(&[])
4527    }
4528
4529    /// The **legacy** label block of a pyramid-meta (≤ 8,192 entries) — empty
4530    /// when the file has none or the pyramid isn't resident. Files built with
4531    /// `--label-index` carry a LABEL_INDEX section instead; see
4532    /// [`label_index_section`](Self::label_index_section). See
4533    /// [`crate::meta::LabelEntry`].
4534    pub fn label_index(&self) -> &[crate::meta::LabelEntry] {
4535        self.pyramid_if_loaded()
4536            .map(|p| p.label_index.as_slice())
4537            .unwrap_or(&[])
4538    }
4539
4540    /// Whether the file carries a LABEL_INDEX section (kind 8). Header only.
4541    pub fn has_label_index(&self) -> bool {
4542        self.header.label_index_section().is_some()
4543    }
4544
4545    /// The LABEL_INDEX section, faulting its head in on the lazy path.
4546    pub fn label_index_section(&self) -> Option<&crate::label_index::LabelIndex> {
4547        self.labels.get()
4548    }
4549
4550    /// Prefix-search the labels: the subjects whose label starts with `prefix`
4551    /// (case-insensitive), as `(label, subject term)`, capped at `limit`.
4552    /// Reads the LABEL_INDEX section when the file has one (every labelled
4553    /// subject, no dictionary read); otherwise the legacy pyramid-meta block,
4554    /// which **faults the pyramid** on the lazy path. Empty when the file
4555    /// carries neither.
4556    pub fn prefix_search(&self, prefix: &str, limit: usize) -> Vec<(String, String)> {
4557        if let Some(li) = self.labels.get() {
4558            return li.prefix_search(prefix, limit);
4559        }
4560        let Some(pyr) = self.pyramid() else {
4561            return Vec::new();
4562        };
4563        resolve_label_hits(&self.dict, pyr.prefix_search(prefix, limit))
4564    }
4565
4566    /// The full-text index (TEXT_INDEX section), faulting it in on first access
4567    /// on the lazy remote path. `None` when the file carries no text index.
4568    pub(crate) fn text_index(&self) -> Option<&crate::text_index::TextIndex> {
4569        self.text_index.index()
4570    }
4571
4572    /// Whether this file carries a full-text (TEXT_INDEX) section, i.e. it was
4573    /// built with `--text-index`. Cheap — reads the header, never faults.
4574    pub fn has_text_index(&self) -> bool {
4575        self.header.text_index_len > 0
4576    }
4577
4578    /// What the full-text index covers (graphs, predicates) — `None` when the
4579    /// file has none. Faults the token table on the lazy remote path, as a
4580    /// search does; the scope block lives in it.
4581    pub fn text_index_scope(&self) -> Option<crate::text_index::TextIndexScope> {
4582        self.text_index().map(|ti| ti.scope().clone())
4583    }
4584
4585    /// Byte length of the TEXT_INDEX section's leading **token table** — the
4586    /// length varint plus the compressed table, which is exactly what a first
4587    /// [`text_search`](Self::text_search) faults. `None` when the file carries
4588    /// no text index (or its head could not be read).
4589    ///
4590    /// This is the number to quote as the cost of a first search, NOT
4591    /// `header().text_index_len`: that counts the postings blob too, which is
4592    /// the bulk of the section and is only ever fetched one posting list at a
4593    /// time. On the published `epfl-infoscience.rete` the section is 195 MB and
4594    /// its token table 29 MB — a 6.5× difference.
4595    ///
4596    /// Costs nothing on a resident open (measured while the bytes were in hand);
4597    /// a lazy/ranged open pays one ≤10-byte range read, memoized, and still
4598    /// never faults the table itself.
4599    pub fn text_index_token_table_len(&self) -> Option<u64> {
4600        self.text_index.token_table_len()
4601    }
4602
4603    /// Full-text search over the literals: subject IRIs that carry **every** word
4604    /// in `words` (whole-word, case-insensitive — AND semantics), optionally also
4605    /// requiring a word that **starts with** `prefix` (token-prefix). Results are
4606    /// ordered by subject id and capped at `limit` (0 = uncapped). Empty when the
4607    /// file has no text index or nothing matches.
4608    ///
4609    /// Like [`prefix_search`](Self::prefix_search), this **faults** the index on
4610    /// the lazy remote path — a search is an explicit read, and only the queried
4611    /// posting lists are fetched, not the whole index.
4612    pub fn text_search(&self, words: &[&str], prefix: Option<&str>, limit: usize) -> Vec<String> {
4613        text_search_in(self.text_index(), &self.dict, words, prefix, limit)
4614    }
4615
4616    /// The default-graph permutation index.
4617    pub fn default_index(&self) -> &GraphIndex {
4618        &self.index
4619    }
4620
4621    /// Resolve every triple of a graph (`None` = default graph) back to terms.
4622    pub fn dump(&self, graph: Option<&str>) -> Vec<TermTriple> {
4623        let index = match graph {
4624            None => &self.index,
4625            Some(g) => match self.graph_index(g) {
4626                Some(i) => i,
4627                None => return Vec::new(),
4628            },
4629        };
4630        index
4631            .match_pattern((None, None, None))
4632            .into_iter()
4633            .filter_map(|(s, p, o)| {
4634                Some((
4635                    self.dict.subject_term(s)?,
4636                    self.dict.predicate_term(p)?,
4637                    self.dict.object_term(o)?,
4638                ))
4639            })
4640            .collect()
4641    }
4642
4643    /// Stream every triple of a graph (`None` = default) to `f`, resolving terms
4644    /// a batch at a time — no full `Vec` materialization, so it is safe on graphs far
4645    /// larger than RAM. `rete export` uses this to serialize 100M+ triple files
4646    /// that `dump()` (which collects every term into a `Vec<String>`) would OOM on.
4647    ///
4648    /// On a lazy/ranged open the dictionary is **not** prefetched whole: each
4649    /// batch faults only the chunks its own terms live in. Dumping ONE named
4650    /// graph therefore costs that graph's terms, not the file's. Dumping
4651    /// *everything* still reads every dictionary byte and leaves every chunk
4652    /// resident — that is inherent, and it, not the prefetch, is what sets the
4653    /// peak of a full export. See the comment in the body for both measurements.
4654    ///
4655    /// To dump a *slice* — one predicate, one subject, one object — call
4656    /// [`dump_filtered_each`](Self::dump_filtered_each), which is this method
4657    /// with a triple pattern and prunes tiles instead of filtering rows.
4658    pub fn dump_each<F: FnMut(&str, &str, &str)>(&self, graph: Option<&str>, f: F) {
4659        self.dump_filtered_each(graph, None, None, None, f)
4660    }
4661
4662    /// [`dump_each`](Self::dump_each) restricted to a triple **pattern** — the
4663    /// filtered dump. Any of `s` / `p` / `o` may be bound (canonical N-Triples
4664    /// term tokens); all three unbound is exactly `dump_each`.
4665    ///
4666    /// # Why this is cheap, and how much
4667    ///
4668    /// It is the same access path a routed *query* takes, not a full scan with a
4669    /// predicate test bolted on: [`GraphIndex::scan_iter`] picks the permutation
4670    /// with the longest bound prefix, binary-searches that section's tile
4671    /// directory down to the span a bound leading id can live in, and then drops
4672    /// every remaining tile whose recorded synopsis proves it cannot hold a
4673    /// bound secondary component — **all from directories, without fetching a
4674    /// tile**. So a filtered dump pays for the slice, which is the whole point
4675    /// of issue #117.
4676    ///
4677    /// Measured on `cordis.rete` (801 MB, a 417 MB dictionary, 26.4M quads,
4678    /// six named graphs), lazily opened; "before" is the only way to get a
4679    /// predicate slice out of a dump previously — dump the graph and throw away
4680    /// what does not match, in the consumer:
4681    ///
4682    /// ```text
4683    ///   graph=results  p=s66#doi     -> 337,811 rows
4684    ///     before  375.8 MB · 1797 req · 2105.8 MB RSS · 12.8 s · first row 409 ms
4685    ///     after    16.0 MB ·  105 req ·  182.6 MB RSS ·  0.41 s · first row  14 ms
4686    ///   graph=results  p=s66#isbn    ->  40,761 rows
4687    ///     before  375.8 MB · 1797 req · 2105.7 MB RSS · 12.8 s
4688    ///     after    15.5 MB ·  104 req ·  155.2 MB RSS ·  0.23 s
4689    /// ```
4690    ///
4691    /// # Where it does NOT help
4692    ///
4693    /// The index is pruned; the **dictionary is not**. Term resolution still
4694    /// faults whichever chunks the surviving rows' terms live in, and on a file
4695    /// whose payload *is* the literals those chunks are most of the file. Same
4696    /// file, a predicate whose objects are the long abstracts:
4697    ///
4698    /// ```text
4699    ///   graph=projects p=s66#abstract ->  80,206 rows
4700    ///     before  260.7 MB · 1142 req · 1496.9 MB RSS · 5.97 s
4701    ///     after   213.4 MB · 1606 req · 1144.4 MB RSS · 2.91 s   (1.2x, not 23x)
4702    /// ```
4703    ///
4704    /// An *unfiltered* dump is unchanged by construction — it is the floor, and
4705    /// its peak is the resident dictionary, not this. Two ceilings sit above
4706    /// both: a faulted dictionary chunk is a `OnceCell` nothing ever evicts, and
4707    /// on a literal-heavy file the chunk directory alone can be a third of the
4708    /// file before any dump work at all (#198).
4709    ///
4710    /// # Permutations
4711    ///
4712    /// Every one of the eight bound/unbound shapes routes inside
4713    /// [`PermSet::CORE`] — `{SPO, POS, OSP}`, the minimum a legal file carries
4714    /// (see `perm_routing_never_leaves_core`). A file built with
4715    /// `--permutations 3` therefore prunes *identically* to a six-permutation
4716    /// one: same tiles, same rows, no fallback path to get wrong.
4717    ///
4718    /// A bound term the dictionary does not know, or an absent graph IRI, yields
4719    /// nothing without touching the index.
4720    ///
4721    /// # Order
4722    ///
4723    /// Rows arrive in the **routed permutation's** order, which is the price of
4724    /// routing at all. Unfiltered (and subject-bound) that is SPO — canonical
4725    /// `(s, p, o)`, unchanged from [`dump_each`](Self::dump_each). A bound
4726    /// predicate routes to POS and streams `(p, o, s)`; a bound object routes to
4727    /// OSP. The *set* is identical to the unfiltered dump filtered; the order is
4728    /// not, so a consumer that needs canonical order must sort. N-Quads, the
4729    /// format `rete export` writes, is order-independent.
4730    ///
4731    /// [`PermSet::CORE`]: crate::index::PermSet::CORE
4732    pub fn dump_filtered_each<F: FnMut(&str, &str, &str)>(
4733        &self,
4734        graph: Option<&str>,
4735        s: Option<&str>,
4736        p: Option<&str>,
4737        o: Option<&str>,
4738        mut f: F,
4739    ) {
4740        let index = match graph {
4741            None => Some(&self.index),
4742            Some(g) => self.graph_index(g),
4743        };
4744        let Some(index) = index else { return };
4745        // A bound term the dictionary does not know can match nothing, and the
4746        // index is never touched — same contract as `query_iter`/`query_batch`.
4747        let Some(pattern) = self.resolve_query_pattern(s, p, o) else {
4748            return;
4749        };
4750        let dict = &self.dict;
4751        // Resolve in fixed-size batches with ONE coalesced dictionary fault per
4752        // batch — the same `prefetch_terms` call `dump_batch` makes, for the
4753        // same reason.
4754        //
4755        // This used to be `Dictionary::prefetch_all` up front, on the argument
4756        // that a full dump reaches every term anyway so the bytes are owed
4757        // either way. That argument only holds for a dump of EVERYTHING, and
4758        // this takes a graph AND a pattern. Measured on `cordis.rete` (801 MB,
4759        // a 417 MB dictionary, six named graphs), lazily opened:
4760        //
4761        //   dump_each(Some(fundingschemes))  417 MB read, 2510 MB peak RSS
4762        //                              →     173 MB read,  619 MB peak RSS
4763        //   dump_each(None) — the EMPTY default graph, which is the first thing
4764        //   `rete export` calls on a quads file:
4765        //                                    415 MB read, 2508 MB peak RSS
4766        //                              →       0 MB read,   95 MB peak RSS
4767        //
4768        // The 2.5 GB comes from asking for every chunk at once: the bulk loader
4769        // coalesces adjacent chunks into one span, so the whole dictionary
4770        // section is materialized three times over before the first triple —
4771        // the fetched span, the per-chunk copies sliced out of it, and the
4772        // decompressed bodies.
4773        //
4774        // Be clear about what this does NOT buy. A dump of the whole default
4775        // graph still faults every chunk, and chunks stay resident once
4776        // faulted, so `rete export` of a single-graph file is unchanged:
4777        // `figshare.rete` (222 MB, a 126 MB dictionary) peaks at 874 MB lazily
4778        // either way. The resident dictionary is the floor there, not this.
4779        //
4780        // Within each window, term resolution is CHUNK-ORDERED, not row-ordered
4781        // (`Dictionary::resolve_window`, bounded-export phase 1): the window's
4782        // ids are grouped by the `(section, chunk)` they live in and each chunk
4783        // is decoded once and its runs walked once, filling every slot that
4784        // chunk owns, then the rows are emitted in the original scan order. Two
4785        // things follow. First, a window's objects — which scatter across the
4786        // object-only section for one subject — touch each chunk a single time
4787        // instead of re-faulting it per row. Second, the per-id re-walk from a
4788        // run's start that the old row-order path did for *every* id collapses
4789        // to one monotonic forward pass per run. The output is byte-for-byte
4790        // identical (same bytes per id, same emission order, same
4791        // all-three-resolved emit rule); the cap is still unlimited, so this is
4792        // structural preparation for the real bound (phase 2), plus a small
4793        // free speedup. The coalesced bulk fault (`prefetch_terms`) is
4794        // unchanged, so the read pattern and peak residency are as before.
4795        const WINDOW: usize = 4096;
4796        // The whole-window coalesced prefetch (`prefetch_terms`) materializes
4797        // every distinct chunk the window touches at once — up to ~2·WINDOW
4798        // chunks for scattered objects. Under a GENEROUS budget that transient
4799        // fits and the coalesced reads are the fast path (small/normal files,
4800        // `--in-memory`, unlimited). Under a TIGHT budget it would blow the cap
4801        // and thrash (insert-then-evict every chunk, then re-fault it in
4802        // `resolve_window`), so skip it: `resolve_window` is chunk-ordered and
4803        // holds one chunk `Arc` at a time (plan §3.5), keeping the window's
4804        // working set to a single chunk regardless of how the objects scatter.
4805        // Output is byte-identical either way — prefetch only warms the cache.
4806        let cap = dict.cache_cap();
4807        let prefetch_window =
4808            cap == u64::MAX || cap >= (WINDOW as u64) * 2 * (DICT_CHUNK_BUDGET as u64);
4809        // Under a finite budget the prefetch also goes in batches of
4810        // PREFETCH_BATCH chunks: one bulk read holds everything it fetched
4811        // (span, slices, decoded bodies) until it is all cached, which for a
4812        // scattered window was thousands of chunks — up to ~1 GB on
4813        // openaire-relation — outside the cache cap. Unlimited keeps one read
4814        // per section (the latency-optimal shape for a remote file).
4815        const PREFETCH_BATCH: usize = 64;
4816        let prefetch_batch = if cap == u64::MAX {
4817            usize::MAX
4818        } else {
4819            PREFETCH_BATCH
4820        };
4821        let mut ids: Vec<(u32, u32, u32)> = Vec::with_capacity(WINDOW);
4822        let mut nodes: Vec<u32> = Vec::with_capacity(WINDOW * 2);
4823        let mut preds: Vec<u32> = Vec::with_capacity(WINDOW);
4824        let mut resolver = crate::dictionary::WindowResolver::default();
4825        let mut flush = |ids: &mut Vec<(u32, u32, u32)>,
4826                         resolver: &mut crate::dictionary::WindowResolver,
4827                         f: &mut F| {
4828            if ids.is_empty() {
4829                return;
4830            }
4831            // Coalesced bulk fault of exactly the chunks this window needs —
4832            // only under a generous budget (see `prefetch_window` above); a
4833            // tight budget faults chunk-by-chunk inside `resolve_window`.
4834            if prefetch_window {
4835                nodes.clear();
4836                preds.clear();
4837                for &(s, p, o) in ids.iter() {
4838                    nodes.push(dict.subject_node(s));
4839                    nodes.push(dict.object_node(o));
4840                    preds.push(p);
4841                }
4842                dict.prefetch_terms_batched(&nodes, &preds, prefetch_batch);
4843            }
4844            dict.resolve_window(ids, resolver, |s, p, o| f(s, p, o));
4845            ids.clear();
4846        };
4847        for t in index.scan_iter(pattern) {
4848            ids.push(t);
4849            if ids.len() == WINDOW {
4850                flush(&mut ids, &mut resolver, &mut f);
4851            }
4852        }
4853        flush(&mut ids, &mut resolver, &mut f);
4854    }
4855
4856    /// What a [`dump_filtered_each`](Self::dump_filtered_each) over this graph
4857    /// and pattern will fetch, **before** it starts — the dump twin of
4858    /// `rete cost`'s query preview.
4859    ///
4860    /// Costed from the tile directories, so it fetches no tile. It does read the
4861    /// dictionary far enough to resolve the bound terms (that is a real,
4862    /// unavoidable cost of the dump too) and to learn whether the graph exists.
4863    pub fn dump_plan(
4864        &self,
4865        graph: Option<&str>,
4866        s: Option<&str>,
4867        p: Option<&str>,
4868        o: Option<&str>,
4869    ) -> DumpPlan {
4870        let index = match graph {
4871            None => Some(&self.index),
4872            Some(g) => self.graph_index(g),
4873        };
4874        let scan = index
4875            .zip(self.resolve_query_pattern(s, p, o))
4876            .map(|(ix, pattern)| ix.scan_plan(pattern));
4877        DumpPlan {
4878            scan,
4879            dictionary_bytes: match &self.v06 {
4880                Some(f) => f.section(crate::v06::kind::DICT_DATA).map_or(0, |r| r.len),
4881                None => self.header.dictionary_len,
4882            },
4883        }
4884    }
4885
4886    /// The **pull** twin of [`Rete::dump_each`](Self::dump_each): every triple of a
4887    /// graph (`None` = default) as a lazy iterator of resolved terms.
4888    ///
4889    /// Same constant-memory scan — one triple decoded and resolved per `next()`,
4890    /// never a `Vec` of the whole graph — but the caller drives it, so it can
4891    /// stop early or be **suspended and resumed** across a foreign-function
4892    /// boundary. That is what the wasm/JS client's batched quad cursor needs:
4893    /// a callback cannot be paused mid-scan to hand control back to JavaScript,
4894    /// an iterator can.
4895    ///
4896    /// The dictionary is NOT prefetched: terms fault in as they are resolved, so
4897    /// stopping after a handful of triples costs a handful of dictionary reads
4898    /// rather than the whole dictionary — which on a lazy remote open of a large
4899    /// file would be gigabytes before the first triple, defeating the point of a
4900    /// pull API. [`dump_each`](Self::dump_each) does not prefetch it whole
4901    /// either; it batches the same per-term faults. Peak memory is
4902    /// O(faulted dictionary chunks + faulted index tiles), *not* O(triples).
4903    pub fn dump_iter(&self, graph: Option<&str>) -> impl Iterator<Item = TermTriple> + '_ {
4904        self.query_iter(graph, None, None, None)
4905    }
4906
4907    /// [`dump_iter`](Self::dump_iter) generalized to a triple **pattern**: the
4908    /// lazy pull form of [`query_in_graph`](Self::query_in_graph). A bound term
4909    /// unknown to the dictionary, or an unknown graph IRI, yields nothing.
4910    ///
4911    /// Same constant-memory guarantee — one triple decoded and resolved per
4912    /// `next()`, never a `Vec` of every match — and the same absence of a
4913    /// dictionary prefetch, so stopping after a handful of matches costs a
4914    /// handful of dictionary reads. `query_in_graph` is the eager twin; prefer
4915    /// this one wherever the consumer can stop early (`ASK`, `LIMIT`, an RDF4J
4916    /// `getStatements` that is about to be sliced).
4917    pub fn query_iter(
4918        &self,
4919        graph: Option<&str>,
4920        s: Option<&str>,
4921        p: Option<&str>,
4922        o: Option<&str>,
4923    ) -> impl Iterator<Item = TermTriple> + '_ {
4924        let index = match graph {
4925            None => Some(&self.index),
4926            Some(g) => self.graph_index(g),
4927        };
4928        index
4929            .zip(self.resolve_query_pattern(s, p, o))
4930            .into_iter()
4931            .flat_map(|(ix, pattern)| ix.scan_iter(pattern))
4932            .filter_map(move |(s, p, o)| {
4933                Some((
4934                    self.dict.subject_term(s)?,
4935                    self.dict.predicate_term(p)?,
4936                    self.dict.object_term(o)?,
4937                ))
4938            })
4939    }
4940
4941    /// One bounded, **resumable** slice of a pattern's matches inside one graph
4942    /// (`None` = default): `(triples, next_cursor, done)`. Start at `cursor = 0`
4943    /// and feed the returned cursor back until `done`.
4944    ///
4945    /// This is [`query_in_graph`](Self::query_in_graph) made streamable across a
4946    /// boundary that cannot hold a Rust borrow — the wasm/JVM one. The engine
4947    /// keeps no iterator alive between calls; the entire resume state is the
4948    /// opaque `u64` (see [`GraphIndex::scan_batch`]), so a client's cursor
4949    /// survives being suspended, and a cursor that is abandoned leaks nothing
4950    /// but a `u64`.
4951    ///
4952    /// The dictionary is faulted **per batch**, not whole: taking ten matches
4953    /// off the front of a 9.8-billion-quad file reads ten matches' worth of
4954    /// terms. That is the difference between `LIMIT 1` answering in bounded time
4955    /// and it reading a 30 GB dictionary first.
4956    ///
4957    /// `max_quads` is a floor (batches end on an a-group boundary), and every
4958    /// call either returns at least one row or reports `done`.
4959    pub fn query_batch(
4960        &self,
4961        graph: Option<&str>,
4962        s: Option<&str>,
4963        p: Option<&str>,
4964        o: Option<&str>,
4965        cursor: u64,
4966        max_quads: usize,
4967    ) -> (Vec<TermTriple>, u64, bool) {
4968        let index = match graph {
4969            None => &self.index,
4970            // An unknown graph IRI is an empty scan, not an error — same as
4971            // `query_in_graph` and `dump_batch`.
4972            Some(g) => match self.graph_index(g) {
4973                Some(i) => i,
4974                None => return (Vec::new(), cursor, true),
4975            },
4976        };
4977        // A bound term the dictionary does not know can match nothing, and the
4978        // index is never touched.
4979        let Some(pattern) = self.resolve_query_pattern(s, p, o) else {
4980            return (Vec::new(), cursor, true);
4981        };
4982        let (ids, next, done) = index.scan_batch(pattern, cursor, max_quads);
4983        // One coalesced dictionary fault for THIS batch — the same call
4984        // `query_in_graph` and `dump_batch` make, and the reason a partial walk
4985        // costs in proportion to what it actually read.
4986        if !ids.is_empty() {
4987            let mut nodes = Vec::with_capacity(ids.len() * 2);
4988            let mut preds = Vec::with_capacity(ids.len());
4989            for &(s, p, o) in &ids {
4990                nodes.push(self.dict.subject_node(s));
4991                nodes.push(self.dict.object_node(o));
4992                preds.push(p);
4993            }
4994            self.dict.prefetch_terms(&nodes, &preds);
4995        }
4996        let triples = ids
4997            .into_iter()
4998            .filter_map(|(s, p, o)| {
4999                Some((
5000                    self.dict.subject_term(s)?,
5001                    self.dict.predicate_term(p)?,
5002                    self.dict.object_term(o)?,
5003                ))
5004            })
5005            .collect();
5006        (triples, next, done)
5007    }
5008
5009    /// Resolve one bounded slice of a graph's triples. Returns
5010    /// `(triples, next_cursor, done)`; start with `cursor = 0` and feed the
5011    /// returned cursor back until `done`.
5012    ///
5013    /// This is the PULL half of the dump, and it exists because [`Rete::dump_each`] is
5014    /// push-based: it drives the scan itself and hands each triple to a
5015    /// callback. A client that wants to pull (a Python generator, a JS async
5016    /// iterator) has to hold the scan's stack between calls — which means a
5017    /// thread (unavailable on Pyodide and in wasm) or a self-referential struct
5018    /// holding an iterator that borrows the `Rete` stored next to it, whose
5019    /// soundness rests on drop order plus an unstated promise that this crate's
5020    /// lazily-faulted caches stay write-once. Neither is worth it for a dump.
5021    ///
5022    /// Instead the scan is made RESUMABLE: SPO tiles are ordered by subject id,
5023    /// so "every triple of subject `sid`, for ascending `sid`" visits exactly the
5024    /// tiles a full scan visits, in the same order, and the whole resume state
5025    /// collapses to one `u32`. No thread, no `unsafe`, no borrow held across
5026    /// calls — and unlike `(offset, limit)` it is O(n) overall rather than
5027    /// O(n²/limit), because nothing is ever re-scanned.
5028    ///
5029    /// `max_quads` is the FLOOR of a batch, not a hard cut: a batch always ends
5030    /// on a subject boundary, so no subject is split across two calls. Work per
5031    /// call is bounded by a probe budget too — a sparse named graph whose few
5032    /// subjects are spread over a huge id space returns early with `done = false`
5033    /// rather than grinding through absent ids, and the caller just asks again.
5034    pub fn dump_batch(
5035        &self,
5036        graph: Option<&str>,
5037        cursor: u32,
5038        max_quads: usize,
5039    ) -> (Vec<TermTriple>, u32, bool) {
5040        let index = match graph {
5041            None => self.default_index(),
5042            // An unknown graph IRI is an empty dump, not an error — same as
5043            // `dump_each`.
5044            Some(g) => match self.graph_index(g) {
5045                Some(i) => i,
5046                None => return (Vec::new(), cursor, true),
5047            },
5048        };
5049        // Tile leading ranges live in the section directory, so both the span and
5050        // the gap jumps below are free on a lazy/remote open — no tile is fetched
5051        // to decide where to look.
5052        let spo = IndexPermutation::Spo.section_index();
5053        let Some((first, last)) = index.leading_bounds(spo) else {
5054            return (Vec::new(), cursor, true); // empty graph
5055        };
5056
5057        let mut sid = cursor.max(first);
5058        let mut probes = max_quads.saturating_mul(4).max(1 << 16);
5059        let mut ids: Vec<(u32, u32, u32)> = Vec::new();
5060        let mut exhausted = false;
5061        while sid <= last && probes > 0 {
5062            probes -= 1;
5063            let hits = index.match_pattern((Some(sid), None, None));
5064            if hits.is_empty() {
5065                // No triples at this id: jump to the next subject the index
5066                // holds (the tile's group directory knows it), instead of
5067                // stepping through absent ids one at a time. That is what keeps
5068                // a quad-index view — whose tile shared with other graphs has
5069                // the conservative range `0..=u32::MAX` — from probing four
5070                // billion ids. Only when that lookup cannot answer (a failed
5071                // fetch, already recorded) does it fall back to the directory:
5072                // if the id falls in a hole BETWEEN tiles, jump to the next
5073                // tile's first id.
5074                match index.next_leading(IndexPermutation::Spo.section_index(), sid) {
5075                    Some(next) if next > sid => {
5076                        sid = next;
5077                        continue;
5078                    }
5079                    None if !index.load_incomplete() => {
5080                        exhausted = true;
5081                        break;
5082                    }
5083                    _ => {}
5084                }
5085                match index.next_tile_start(spo, sid) {
5086                    None => {
5087                        exhausted = true;
5088                        break;
5089                    }
5090                    Some(next) if next > sid => {
5091                        sid = next;
5092                        continue;
5093                    }
5094                    Some(_) => {}
5095                }
5096            } else {
5097                ids.extend(hits);
5098            }
5099            if sid == u32::MAX {
5100                exhausted = true;
5101                break;
5102            }
5103            sid += 1;
5104            if ids.len() >= max_quads {
5105                break;
5106            }
5107        }
5108        let done = exhausted || sid > last;
5109
5110        // One coalesced dictionary fault for this batch, then resolve.
5111        // Deliberately NOT `Dictionary::prefetch_all` (what `dump_each` uses):
5112        // on a lazy open that pulls the ENTIRE dictionary — gigabytes on a big
5113        // file — before the first quad, so taking five quads off the front would
5114        // cost as much as a full dump. Per-batch prefetch keeps a partial walk
5115        // proportional to what it actually read.
5116        let dict = self.dictionary();
5117        if !ids.is_empty() {
5118            let mut nodes = Vec::with_capacity(ids.len() * 2);
5119            let mut preds = Vec::with_capacity(ids.len());
5120            for &(s, p, o) in &ids {
5121                nodes.push(dict.subject_node(s));
5122                nodes.push(dict.object_node(o));
5123                preds.push(p);
5124            }
5125            dict.prefetch_terms(&nodes, &preds);
5126        }
5127        let triples = ids
5128            .into_iter()
5129            .filter_map(|(s, p, o)| {
5130                Some((
5131                    dict.subject_term(s)?,
5132                    dict.predicate_term(p)?,
5133                    dict.object_term(o)?,
5134                ))
5135            })
5136            .collect();
5137        (triples, sid, done)
5138    }
5139
5140    /// How many named graphs this dataset has. On a lazy ranged open this
5141    /// reads only the section's leading count varint — never the graphs.
5142    pub fn named_graph_count(&self) -> usize {
5143        self.named_graphs.count()
5144    }
5145
5146    /// The `i`-th named graph's quad count, when the file records it: a
5147    /// quad-index file keeps it in its graph directory, so this reads one
5148    /// directory block and never the graph's index. `None` for a per-graph
5149    /// (NAMED_GRAPHS section) file, or past the last graph.
5150    pub fn named_graph_quad_count(&self, i: usize) -> Option<u64> {
5151        match &self.named_graphs {
5152            NamedGraphsSlot::Quad(q) => q.quad_count_at(i),
5153            _ => None,
5154        }
5155    }
5156
5157    /// The `i`-th named graph as an **owned** index that is not memoised,
5158    /// when the file can make one cheaply: a quad-index file's graph of at
5159    /// most [`TRANSIENT_GRAPH_QUADS`] quads. A walk over every graph (`GRAPH
5160    /// ?g`, the union default graph, `query_quads`) evaluates each such graph
5161    /// and drops it, instead of keeping one index per graph for the life of
5162    /// the open — 1.5 M of them on the OpenCitations sample. `None` for a
5163    /// larger graph, a per-graph file, or a failed read: use
5164    /// [`named_graph_at`](Self::named_graph_at) then.
5165    pub fn named_graph_transient(&self, i: usize) -> Option<GraphIndex> {
5166        match &self.named_graphs {
5167            NamedGraphsSlot::Quad(q) => q.transient_view(i, TRANSIENT_GRAPH_QUADS),
5168            _ => None,
5169        }
5170    }
5171
5172    /// The ordinals (ascending, as [`named_graph_at`](Self::named_graph_at)
5173    /// counts them) of the named graphs holding at least one quad whose
5174    /// subject is one of `subjects` (subject ids), for a quad-index file: one
5175    /// sequential pass over its graph-leading SPO tiles, never a view per
5176    /// graph. `None` for a per-graph file, which has no such pass, or on a
5177    /// failed read (recorded, see [`index_incomplete`](Self::index_incomplete)).
5178    pub(crate) fn named_graphs_with_subjects(
5179        &self,
5180        subjects: &std::collections::BTreeSet<u32>,
5181    ) -> Option<Vec<usize>> {
5182        match &self.named_graphs {
5183            NamedGraphsSlot::Quad(q) => q.graphs_with_subjects(subjects),
5184            _ => None,
5185        }
5186    }
5187
5188    /// Deduplicated quads across every named graph, when the file records it
5189    /// (a quad-index file, from its preamble: one small read). `None` for a
5190    /// per-graph file — there it is the header's `quad_count` minus the
5191    /// default graph's triples.
5192    pub fn named_quad_count(&self) -> Option<u64> {
5193        match &self.named_graphs {
5194            NamedGraphsSlot::Quad(q) => Some(q.quad_count()),
5195            _ => None,
5196        }
5197    }
5198
5199    /// How many triples `index` holds, for sizing a pattern's tile estimate:
5200    /// a named graph's own count when it is known without a tile read (a
5201    /// quad-index graph's directory count, a resident per-graph index), else
5202    /// the header's total. The default graph keeps the total: its size is not
5203    /// recorded in the header, and the planner's thresholds were tuned on it.
5204    pub(crate) fn index_triples(&self, index: &GraphIndex) -> u64 {
5205        index.size_hint().unwrap_or(self.header().quad_count)
5206    }
5207
5208    /// The `i`-th named graph's IRI (stored order). On a lazy ranged open this
5209    /// walks entry HEADERS up to `i` — it never decodes a graph's index.
5210    pub fn named_graph_name_at(&self, i: usize) -> Option<&str> {
5211        self.named_graphs.name_at(&self.dict, i, false)
5212    }
5213
5214    /// The `i`-th named graph as `(iri, index)`. On a lazy ranged open the
5215    /// index is fetched and decoded on first access and memoised; check
5216    /// [`index_incomplete`](Self::index_incomplete) after evaluating.
5217    pub fn named_graph_at(&self, i: usize) -> Option<(&str, &GraphIndex)> {
5218        self.named_graphs.graph_at(&self.dict, i, false)
5219    }
5220
5221    /// The `i`-th graph's name as an owned string that the file does **not**
5222    /// keep: what a walk over millions of graphs uses, so the names it has
5223    /// passed are not all held at once. `exhaustive = true` tells a lazy walk
5224    /// the caller will visit every graph, so it may read the section in bulk
5225    /// chunks; purely a fetch strategy, results are identical either way. Only
5226    /// the SPARQL evaluator sets it, from shapes where full consumption is
5227    /// provable.
5228    pub(crate) fn named_graph_name_owned_demand(
5229        &self,
5230        i: usize,
5231        exhaustive: bool,
5232    ) -> Option<String> {
5233        self.named_graphs.name_owned(&self.dict, i, exhaustive)
5234    }
5235
5236    /// The dictionary node that names the quad-index graph at ordinal `i`, as
5237    /// a query row binds a graph variable: no term is decoded. `None` for
5238    /// another layout (whose graphs are named by strings).
5239    pub(crate) fn named_graph_node_demand(&self, i: usize, exhaustive: bool) -> Option<u32> {
5240        match &self.named_graphs {
5241            NamedGraphsSlot::Quad(q) => q.node_at(&self.dict, i, exhaustive),
5242            _ => None,
5243        }
5244    }
5245
5246    /// The ordinal of the quad-index graph named `iri`: a dictionary lookup and
5247    /// a directory binary search. `None` for another file layout, or no such
5248    /// graph.
5249    pub(crate) fn named_graph_ordinal(&self, iri: &str) -> Option<usize> {
5250        match &self.named_graphs {
5251            NamedGraphsSlot::Quad(q) => q.ordinal_of_iri(&self.dict, iri),
5252            _ => None,
5253        }
5254    }
5255
5256    /// The ordinal of the quad-index graph named by the dictionary node `node`,
5257    /// as a query row holds it: a directory search, no term decoded. `None`
5258    /// for another layout, or when the node names no graph.
5259    pub(crate) fn named_graph_ordinal_of_node(&self, node: u32) -> Option<usize> {
5260        match &self.named_graphs {
5261            NamedGraphsSlot::Quad(q) => q.ordinal_of_node(&self.dict, node),
5262            _ => None,
5263        }
5264    }
5265
5266    /// The ordinal of the quad-index graph whose subject id is `g` (as
5267    /// [`named_quad_scan`](Self::named_quad_scan) yields it).
5268    pub(crate) fn named_graph_ordinal_of_id(&self, g: u32) -> Option<usize> {
5269        match &self.named_graphs {
5270            NamedGraphsSlot::Quad(q) => q.ordinal_of_id(g),
5271            _ => None,
5272        }
5273    }
5274
5275    /// A streaming scan of the quads matching `pat` (dictionary ids: subject,
5276    /// predicate, object) over every named graph of a quad-index file, as
5277    /// `(graph subject id, (s, p, o))`; see [`crate::quad::QuadScan`]. `None`
5278    /// for another layout, or when the section cannot be read.
5279    pub(crate) fn named_quad_scan(
5280        &self,
5281        pat: (Option<u32>, Option<u32>, Option<u32>),
5282    ) -> Option<crate::quad::QuadScan> {
5283        match &self.named_graphs {
5284            NamedGraphsSlot::Quad(q) => q.scan(pat),
5285            _ => None,
5286        }
5287    }
5288
5289    /// The quad index, when the file has one.
5290    pub(crate) fn quad_named(&self) -> Option<&crate::quad::QuadNamed> {
5291        match &self.named_graphs {
5292            NamedGraphsSlot::Quad(q) => Some(q),
5293            _ => None,
5294        }
5295    }
5296
5297    /// [`named_graph_at`](Self::named_graph_at) under a declared demand: see
5298    /// [`named_graph_name_owned_demand`](Self::named_graph_name_owned_demand).
5299    pub(crate) fn named_graph_at_demand(
5300        &self,
5301        i: usize,
5302        exhaustive: bool,
5303    ) -> Option<(&str, &GraphIndex)> {
5304        self.named_graphs.graph_at(&self.dict, i, exhaustive)
5305    }
5306
5307    /// IRIs of the named graphs in this dataset (the default graph is unnamed).
5308    /// On a lazy ranged open this walks the whole directory (headers only) —
5309    /// prefer [`named_graph_count`](Self::named_graph_count) when the number
5310    /// is all that's needed.
5311    pub fn graph_names(&self) -> Vec<&str> {
5312        (0..self.named_graphs.count())
5313            .filter_map(|i| self.named_graphs.name_at(&self.dict, i, false))
5314            .collect()
5315    }
5316
5317    /// The permutation index of a named graph, or `None` if absent.
5318    pub fn graph_index(&self, iri: &str) -> Option<&GraphIndex> {
5319        self.named_graphs.find(&self.dict, iri)
5320    }
5321
5322    /// Match a triple pattern in dictionary-ID space (subject/predicate/object
5323    /// IDs), returning integer triples — the fast path used by the BGP engine.
5324    pub fn match_ids(
5325        &self,
5326        pattern: (Option<u32>, Option<u32>, Option<u32>),
5327    ) -> Vec<(u32, u32, u32)> {
5328        self.index.match_pattern(pattern)
5329    }
5330
5331    /// All `(subject_node, object_node)` pairs for a predicate, as unified node
5332    /// IDs — no term resolution. The fast path for graph traversal.
5333    pub fn predicate_pairs(&self, predicate: &str) -> Vec<(u32, u32)> {
5334        let pid = match self.dict.predicate_id(predicate) {
5335            Some(p) => p,
5336            None => return Vec::new(),
5337        };
5338        self.index
5339            .match_pattern((None, Some(pid), None))
5340            .into_iter()
5341            .map(|(s, _p, o)| (self.dict.subject_node(s), self.dict.object_node(o)))
5342            .collect()
5343    }
5344
5345    /// Open via a [`RangeReader`], fetching only the header and the named
5346    /// section ranges — never a linear scan of the whole resource. A full query
5347    /// open touches at most 4 ranges (header, dictionary, index, pyramid-meta).
5348    pub fn open_ranged<R: RangeReader>(reader: &R) -> Result<Self, FileError> {
5349        let head = reader.read_at(0, (HEADER_LEN as u64).min(reader.len()))?;
5350        if crate::v06::open::is_v06(&head) {
5351            // The eager path reads the whole image — the rest of it, in one
5352            // more request — and a 0x06 one then opens from memory.
5353            let mut all = head;
5354            let at = all.len() as u64;
5355            if reader.len() > at {
5356                all.extend_from_slice(&reader.read_at(at, reader.len() - at)?);
5357            }
5358            return Self::open(&all);
5359        }
5360        let header = Header::from_bytes(&head)?;
5361
5362        let dict_bytes = reader.read_at(header.dictionary_offset, header.dictionary_len)?;
5363        let dict = decode_dictionary_container(&dict_bytes, header.dict_codec)?;
5364
5365        check_named_layout(&header)?;
5366        let index_bytes = reader.read_at(header.root_dir_offset, header.root_dir_len)?;
5367        let (index, _) = decode_root_index_container(&index_bytes, &header)?;
5368        let index_section_ranges = decode_index_section_ranges(
5369            &index_bytes,
5370            header.root_dir_offset,
5371            header.perms,
5372            header.index_container_sections(),
5373        )?;
5374
5375        let pyramid = PyramidSlot::Resident(if header.pyramid_meta_len > 0 {
5376            let mb = reader.read_at(header.pyramid_meta_offset, header.pyramid_meta_len)?;
5377            Some(
5378                PyramidMeta::decode(&mb)
5379                    .map_err(|_| FileError::Container("malformed pyramid meta"))?,
5380            )
5381        } else {
5382            None
5383        });
5384
5385        // Fetch the whole TEXT_INDEX section resident (this opener does one range
5386        // read per section; the lazy opener below is the one that defers it).
5387        let (ti_off, ti_len) = header.text_index_payload();
5388        let text_index_bytes = if ti_len > 0 {
5389            Some(reader.read_at(ti_off, ti_len)?)
5390        } else {
5391            None
5392        };
5393        let text_index = resident_text_index_slot(text_index_bytes.as_deref(), header.block_codec)?;
5394        // LABEL_INDEX and SCHEMA: one range read each, held resident like the
5395        // other sections of this opener.
5396        let labels = SectionSlot::Resident(header.label_index_section().and_then(|s| {
5397            let b = reader.read_at(s.offset, s.length).ok()?;
5398            crate::label_index::LabelIndex::from_section(&b, header.block_codec).ok()
5399        }));
5400        let schema = SectionSlot::Resident(read_schema_with_header(reader, &header));
5401
5402        let named_graphs = NamedGraphsSlot::resident(
5403            &header,
5404            |off, len| Ok(reader.read_at(off, len)?),
5405            || {
5406                let nb = reader.read_at(header.named_graphs_offset, header.named_graphs_len)?;
5407                decode_named_graphs(&nb, header.block_codec, header.perms)
5408            },
5409        )?;
5410
5411        // The metadata section (Dataset Card) is deliberately NOT fetched here:
5412        // a ranged query open keeps to its small range budget. Use `Rete::open`
5413        // (or a dedicated card fetch) when the card is actually needed.
5414        let tile_ranges =
5415            tile_file_ranges(&index_bytes, header.root_dir_offset, &index_section_ranges);
5416        Ok(Self {
5417            header,
5418            dict,
5419            index,
5420            index_section_ranges,
5421            tile_ranges,
5422            pyramid,
5423            text_index,
5424            labels,
5425            schema,
5426            named_graphs,
5427            metadata: Vec::new(),
5428            service_client: None,
5429            service_error: std::sync::Mutex::new(None),
5430            spill_threshold: std::sync::atomic::AtomicU64::new(u64::MAX),
5431            v06: None,
5432        })
5433    }
5434
5435    /// Open a `0x06` file whose first bytes are `first`: the header and root
5436    /// come from them; the dictionary and index are two-level directories whose
5437    /// leaves fault on demand; text, labels, schema, pyramid and named graphs
5438    /// are deferred. `budget` applies the default memory budget (the lazy
5439    /// remote path) as [`open_ranged_lazy`](Self::open_ranged_lazy) does.
5440    fn open_v06(
5441        reader: std::sync::Arc<dyn RangeReader + Send + Sync>,
5442        first: &[u8],
5443        budget: bool,
5444    ) -> Result<Self, FileError> {
5445        let f = std::sync::Arc::new(crate::v06::open::V06File::open(reader.clone(), first)?);
5446        let header = crate::v06::logical_header(&f.h);
5447        let rc = reader.concurrency();
5448        let dict = f.dictionary([true; 4])?;
5449        let index = f.index(rc)?;
5450        let (pyramid, text_index, labels, schema) = v06_slots(&f);
5451        let named_graphs = match f.quad(rc) {
5452            Some(q) => NamedGraphsSlot::Quad(q?),
5453            None => NamedGraphsSlot::Resident(Vec::new()),
5454        };
5455        let rete = Self {
5456            header,
5457            dict,
5458            index,
5459            index_section_ranges: [ByteRange::default(); NUM_PERMS],
5460            tile_ranges: Default::default(),
5461            pyramid,
5462            text_index,
5463            labels,
5464            schema,
5465            named_graphs,
5466            metadata: Vec::new(),
5467            service_client: None,
5468            service_error: std::sync::Mutex::new(None),
5469            spill_threshold: std::sync::atomic::AtomicU64::new(u64::MAX),
5470            v06: Some(f),
5471        };
5472        #[cfg(not(target_arch = "wasm32"))]
5473        if budget {
5474            rete.set_memory_budget(match crate::sysmem::default_query_budget_mb() {
5475                0 => None,
5476                mb => Some(mb << 20),
5477            });
5478        }
5479        #[cfg(target_arch = "wasm32")]
5480        let _ = budget;
5481        Ok(rete)
5482    }
5483
5484    /// The open `0x06` file (header, root, reader), `None` for a `0x05` open.
5485    pub fn v06(&self) -> Option<&crate::v06::open::V06File> {
5486        self.v06.as_deref()
5487    }
5488
5489    /// Open via an **owned** [`RangeReader`] with lazy tile faulting (tiled
5490    /// v0.2 files): fetches the header, dictionary, pyramid meta, named graphs,
5491    /// and each permutation's tile **directory** — but no default-graph tile
5492    /// payloads. Tiles fault in (one range request each) the first time a scan
5493    /// touches them, so a selective SPARQL query fetches O(touched tiles)
5494    /// bytes instead of the whole index.
5495    ///
5496    /// **Failure contract:** scans are infallible by design, so a failed tile
5497    /// fetch yields an empty tile and sets a sticky flag — after evaluating,
5498    /// callers MUST check [`index_incomplete`](Self::index_incomplete) and
5499    /// surface an error instead of the (possibly partial) results.
5500    pub fn open_ranged_lazy<R: RangeReader + Send + Sync + 'static>(
5501        reader: R,
5502    ) -> Result<Self, FileError> {
5503        Self::open_ranged_lazy_with(reader, crate::v06::FIRST_READ)
5504    }
5505
5506    /// [`open_ranged_lazy`](Self::open_ranged_lazy) with an explicit first-read
5507    /// size (at least the 1 KiB a `0x05` header needs). The default is
5508    /// [`crate::v06::FIRST_READ`]; a smaller one keeps a byte-accounting test
5509    /// of a tiny `0x05` file from reading the whole file up front.
5510    pub fn open_ranged_lazy_with<R: RangeReader + Send + Sync + 'static>(
5511        reader: R,
5512        first_read: u64,
5513    ) -> Result<Self, FileError> {
5514        // One first read — 64 KiB, or the whole file when smaller — decides the
5515        // format, and every later read inside it is served from memory.
5516        let first_len = first_read.max(HEADER_LEN as u64).min(reader.len());
5517        let first: std::sync::Arc<[u8]> = std::sync::Arc::from(reader.read_at(0, first_len)?);
5518        #[cfg(feature = "v06-parity")]
5519        if !crate::v06::open::is_v06(&first) {
5520            let all = reader.read_at(0, reader.len())?;
5521            if let Some(v6) = parity_image(&all) {
5522                let r: std::sync::Arc<dyn RangeReader + Send + Sync> =
5523                    std::sync::Arc::new(crate::reader::BytesReader(std::sync::Arc::from(v6)));
5524                let head = r.read_at(0, crate::v06::FIRST_READ.min(r.len()))?;
5525                return Self::open_v06(r, &head, true);
5526            }
5527        }
5528        let reader = crate::reader::PrefixReader::new(reader, first.clone());
5529        if crate::v06::open::is_v06(&first) {
5530            let r: std::sync::Arc<dyn RangeReader + Send + Sync> = std::sync::Arc::new(reader);
5531            return Self::open_v06(r, &first, true);
5532        }
5533        let head = reader.read_at(0, HEADER_LEN as u64)?;
5534        let header = Header::from_bytes(&head)?;
5535        check_named_layout(&header)?;
5536        let reader = std::sync::Arc::new(reader);
5537        // Captured before the loader closures take the Arc: the reader's
5538        // concurrent-range fan-out, stamped onto the index for the planner.
5539        let read_concurrency = reader.concurrency();
5540
5541        // Lazily-chunked dictionary: locate the four sections, fetch each
5542        // section's header + restart table + chunk directory (small), and
5543        // fault the chunk bodies in on first term lookup.
5544        let open_dict = || ranged_chunked_dictionary(&reader, &header, [true; 4]);
5545
5546        // Locate the six index section payloads (container framing only) and
5547        // fetch just their tile directories — shared with the per-named-graph
5548        // lazy opener, which opens a large graph's container the same way.
5549        let reader_dyn: std::sync::Arc<dyn RangeReader + Send + Sync> = reader.clone();
5550        let open_index = || {
5551            open_index_container_lazy(
5552                &reader_dyn,
5553                ByteRange {
5554                    offset: header.root_dir_offset,
5555                    len: header.root_dir_len,
5556                },
5557                header.block_codec,
5558                header.has_tile_synopsis(),
5559                read_concurrency,
5560                header.perms,
5561                header.index_container_sections(),
5562            )
5563        };
5564
5565        // Each of the two is a chain of small dependent reads (a container's
5566        // sections are found by walking their length prefixes), ~8 round trips
5567        // apiece on a real file, and the header already says where both
5568        // containers start. So a reader that can keep several reads in flight
5569        // walks them at the same time: the open costs the longer chain, not
5570        // the sum. Same reads, same result; only their overlap changes.
5571        let (dict, index) = open_side_by_side(read_concurrency, open_dict, open_index);
5572        let dict = dict?;
5573        let (index, index_section_ranges, tile_ranges) = index?;
5574
5575        // The pyramid meta is large on real graphs (tens of MB) and SPARQL never
5576        // reads it, so defer its fetch: it faults in only if `pyramid()` is
5577        // called (community / pyramid_tree / inspect queries).
5578        let pyramid = ranged_pyramid_slot(&reader, &header);
5579
5580        // The TEXT_INDEX section is also deferred: a text search faults the token
5581        // table on first call (the leading varint then its compressed bytes), then
5582        // fetches individual posting lists by `(offset, len)` — never the whole
5583        // postings blob. A SPARQL query, which never searches, pays nothing.
5584        let text_index = ranged_text_index_slot(&reader, &header);
5585
5586        // LABEL_INDEX: the first prefix search reads its head and walks down.
5587        // SCHEMA: read once, on first use — a stats question, or the planner
5588        // ordering a join that needs a measured selectivity.
5589        let labels = ranged_label_slot(&reader, &header);
5590        let schema = ranged_schema_slot(&reader, &header);
5591
5592        // Named graphs are the last eagerly-fetched section standing on this
5593        // path — and on a many-graph file they dwarf everything else (67 MB
5594        // fetched and ~32k graph indexes decoded before the first query, on a
5595        // file whose queries then touch a handful of graphs). Defer them like
5596        // the pyramid and text index: nothing is read at open; a query walks
5597        // the directory and decodes only the graphs it touches.
5598        let named_graphs = if let Some(q) = header.quad_index_section() {
5599            // The quad index: nothing read at open either; the preamble on the
5600            // first named-graph touch, then only the routing parts and tiles
5601            // a query uses.
5602            NamedGraphsSlot::Quad(crate::quad::QuadNamed::new(
5603                reader_dyn,
5604                ByteRange {
5605                    offset: q.offset,
5606                    len: q.length,
5607                },
5608                header.block_codec,
5609                header.perms,
5610                read_concurrency,
5611            ))
5612        } else if header.named_graphs_len > 0 {
5613            NamedGraphsSlot::Lazy(LazyNamedGraphs::new(
5614                reader_dyn,
5615                ByteRange {
5616                    offset: header.named_graphs_offset,
5617                    len: header.named_graphs_len,
5618                },
5619                header.block_codec,
5620                header.has_tile_synopsis(),
5621                read_concurrency,
5622                header.perms,
5623            ))
5624        } else {
5625            NamedGraphsSlot::Resident(Vec::new())
5626        };
5627
5628        let rete = Self {
5629            header,
5630            dict,
5631            index,
5632            index_section_ranges,
5633            tile_ranges,
5634            pyramid,
5635            text_index,
5636            labels,
5637            schema,
5638            named_graphs,
5639            metadata: Vec::new(),
5640            service_client: None,
5641            service_error: std::sync::Mutex::new(None),
5642            spill_threshold: std::sync::atomic::AtomicU64::new(u64::MAX),
5643            v06: None,
5644        };
5645        // A lazy open is the path for files too big to read whole, and the
5646        // one a long-lived session (relay, Python/R/Java clients, `rete serve`)
5647        // keeps resident across queries. Unbounded, its dictionary and tile
5648        // caches grow with everything any query ever touched: a GROUP BY over
5649        // a 9.5-billion-triple file ran a 16 GB container out of memory. Cap
5650        // them by default; a caller that wants another figure (or unlimited)
5651        // calls `set_memory_budget` again. The default is sized from the
5652        // memory this process can still use (`RETE_QUERY_BUDGET_MB` overrides
5653        // it; see `crate::sysmem`): a fixed 4096 MiB did not fit a 4 GiB
5654        // container. Not on wasm32, whose 4 GiB address space makes the default
5655        // meaningless and whose callers bound their own range budgets.
5656        #[cfg(not(target_arch = "wasm32"))]
5657        rete.set_memory_budget(match crate::sysmem::default_query_budget_mb() {
5658            0 => None,
5659            mb => Some(mb << 20),
5660        });
5661        Ok(rete)
5662    }
5663
5664    /// Did any lazy fetch (index tile or dictionary chunk) fail since this
5665    /// `Rete` was opened? When true, query results may be silently incomplete —
5666    /// callers using [`Rete::open_ranged_lazy`] must check this after
5667    /// evaluating and turn it into an error.
5668    pub fn index_incomplete(&self) -> bool {
5669        self.index.load_incomplete()
5670            || self.dict.load_incomplete()
5671            || self.named_graphs.load_incomplete()
5672    }
5673
5674    /// Forget recorded lazy-fetch failures — the start-of-evaluation reset for
5675    /// a RESIDENT session (a browser worker holding one `Rete` across many
5676    /// queries): it makes [`index_incomplete`](Self::index_incomplete) a
5677    /// per-query verdict instead of a per-open one, so a single transient
5678    /// network failure no longer fails every subsequent query on the session.
5679    /// Sound because failed tiles/chunks are never cached — the next
5680    /// evaluation simply retries the fetch.
5681    pub fn reset_load_failures(&self) {
5682        self.index.reset_load_failure();
5683        self.dict.reset_load_failure();
5684        self.named_graphs.reset_load_failures();
5685    }
5686
5687    /// Bound peak resident memory to roughly `budget` bytes by capping the
5688    /// evictable dictionary chunk cache and the default-graph index tile cache
5689    /// (bounded-export phase 2). `None` or `u64::MAX` means unlimited — nothing
5690    /// is evicted and residency is identical to a plain open, which is what
5691    /// `--in-memory` and every query/wasm path use.
5692    ///
5693    /// Sound at any cap: the dictionary and the default-graph index both fault
5694    /// their chunks/tiles through a loader, so an evicted body is simply
5695    /// re-faulted on its next touch; the export dump reads each needed body
5696    /// while holding it (the cache `Arc` handout invariant), so eviction never
5697    /// drops bytes in use. Named-graph indexes are **not** capped here — a small
5698    /// resident graph has no loader and could never reload an evicted tile — so
5699    /// they stay unlimited and are released whole per slot instead (see
5700    /// [`release_named_graph`](Self::release_named_graph)).
5701    ///
5702    /// The open's **fixed** heap — chunk and tile directories, the named-graph
5703    /// directory walked so far ([`fixed_overhead`](Self::fixed_overhead)) — is
5704    /// paid whatever the caches hold, so it comes out of the dictionary share:
5705    /// the caps plus the fixed heap stay within `budget`. It grows with the
5706    /// file's chunk/tile/graph count (~18 MB of directories on the 14.75 GiB
5707    /// openaire-relation, 88 MB with its tile directories; 224 MB on a pre-#198
5708    /// epfl-infoscience whose chunk keys are whole boundary terms). Calling
5709    /// this again after a walk of the named-graph directory (as `rete export`
5710    /// does once it has listed the graph slots) re-subtracts the larger figure.
5711    /// The dictionary share never drops below its 64 MiB floor, which is the
5712    /// only way the total can be exceeded: a file whose directories alone
5713    /// approach the budget.
5714    ///
5715    /// Returns the split actually applied (dictionary share net of the fixed
5716    /// heap), for `RETE_OPEN_DEBUG` logging.
5717    pub fn set_memory_budget(&self, budget: Option<u64>) -> MemoryBudget {
5718        let total = budget.unwrap_or(u64::MAX);
5719        self.set_spill_threshold(budget.map(spill_threshold_for));
5720        let mut split = split_memory_budget(total);
5721        let fixed = if total == u64::MAX {
5722            0
5723        } else {
5724            self.fixed_overhead().total()
5725        };
5726        if split.dict_cache != u64::MAX {
5727            split.dict_cache = split
5728                .dict_cache
5729                .saturating_sub(fixed)
5730                .max(MIN_DICT_CACHE_CAP);
5731        }
5732        self.dict.set_cache_cap(split.dict_cache);
5733        self.index.set_cache_cap(split.tile_cache);
5734        // Large named graphs (opened lazily, loader-bearing) get the same tile
5735        // cap; small resident ones stay unlimited (bounded by size + per-slot
5736        // release). Combined with `release_named_graph`, a many-graph dump holds
5737        // one graph's tiles at a time, each capped.
5738        match &self.named_graphs {
5739            NamedGraphsSlot::Lazy(lazy) => lazy.set_tile_cap(split.tile_cache),
5740            // Every quad-index view, and the tiles they share, re-fault
5741            // through loaders — on every open path — so they take the cap too.
5742            NamedGraphsSlot::Quad(q) => q.set_tile_cap(split.tile_cache),
5743            NamedGraphsSlot::Resident(_) => {}
5744        }
5745        if std::env::var("RETE_OPEN_DEBUG").is_ok() {
5746            eprintln!(
5747                "[memory-budget] total={} dict_cache={} tile_cache={} block_cache={} fixed={:.1}MiB",
5748                if total == u64::MAX {
5749                    "unlimited".to_string()
5750                } else {
5751                    format!("{}MiB", total >> 20)
5752                },
5753                fmt_cap(split.dict_cache),
5754                fmt_cap(split.tile_cache),
5755                fmt_cap(split.block_cache),
5756                fixed as f64 / (1u64 << 20) as f64,
5757            );
5758        }
5759        split
5760    }
5761
5762    /// How many bytes a query's `COUNT(DISTINCT …)` keeps in memory before it
5763    /// writes sorted runs to the temporary directory (`TMPDIR`) and counts by
5764    /// merging them; the count is exact either way. `u64::MAX` (the default of
5765    /// an eager open) never spills. [`set_memory_budget`](Self::set_memory_budget)
5766    /// sets it to an eighth of the budget, so a lazy open spills past 512 MiB
5767    /// by default.
5768    pub fn spill_threshold(&self) -> u64 {
5769        self.spill_threshold
5770            .load(std::sync::atomic::Ordering::Relaxed)
5771    }
5772
5773    /// Set [`spill_threshold`](Self::spill_threshold) without touching the
5774    /// caches — for an eager (resident) open, whose caches cannot be capped.
5775    /// `None` = never spill.
5776    pub fn set_spill_threshold(&self, bytes: Option<u64>) {
5777        self.spill_threshold.store(
5778            bytes.unwrap_or(u64::MAX),
5779            std::sync::atomic::Ordering::Relaxed,
5780        );
5781    }
5782
5783    /// The dictionary chunk cache's observability counters (decoded chunks and
5784    /// bytes, hits/misses, evictions, resident bytes) — for the bounded-export
5785    /// work-accounting tests and `RETE_OPEN_DEBUG`.
5786    pub fn dict_cache_stats(&self) -> crate::chunk_cache::CacheStats {
5787        self.dict.cache_stats()
5788    }
5789
5790    /// Heap bytes this open holds outside the bounded caches — the dictionary
5791    /// chunk directories, the index tile directories and the named-graph
5792    /// directory — for `RETE_OPEN_DEBUG` / `RETE_MEM_TRACE` accounting.
5793    pub fn fixed_overhead(&self) -> FixedOverhead {
5794        FixedOverhead {
5795            dict_directory: self.dict.directory_bytes(),
5796            index_directory: self.index.directory_bytes(),
5797            named_graphs: self.named_graphs.directory_bytes(),
5798        }
5799    }
5800
5801    /// The default-graph index tile cache's observability counters.
5802    pub fn index_cache_stats(&self) -> crate::chunk_cache::CacheStats {
5803        self.index.cache_stats()
5804    }
5805
5806    /// The current dictionary chunk-cache byte cap (`u64::MAX` = unlimited).
5807    pub fn dict_cache_cap(&self) -> u64 {
5808        self.dict.cache_cap()
5809    }
5810
5811    /// Release a named graph's decoded index after its slot has been exported,
5812    /// dropping its `GraphIndex` (and, with it, that graph's decoded tiles and
5813    /// its own tile cache) so a many-graph dump does not accumulate one resident
5814    /// index per graph — the next linear floor on many-graph files once the
5815    /// dictionary is bounded (plan §6). A no-op for the default graph, an
5816    /// unknown IRI, a resident (in-memory/eager) open, or a graph never opened.
5817    ///
5818    /// Takes `&mut self`: the export loop finishes one slot's dump (which only
5819    /// borrows `&self`) before releasing, so no outstanding borrow of the index
5820    /// exists. Correct because a released index is simply re-opened (headers +
5821    /// tile directory) on a later access — the same lazy contract as a chunk
5822    /// that was never faulted.
5823    pub fn release_named_graph(&mut self, iri: &str) {
5824        match &mut self.named_graphs {
5825            NamedGraphsSlot::Lazy(lazy) => lazy.release(iri),
5826            NamedGraphsSlot::Quad(q) => q.release(&self.dict, iri),
5827            NamedGraphsSlot::Resident(_) => {}
5828        }
5829    }
5830
5831    fn resolve_query_pattern(
5832        &self,
5833        s: Option<&str>,
5834        p: Option<&str>,
5835        o: Option<&str>,
5836    ) -> Option<Pattern> {
5837        let sid = match s {
5838            Some(t) => match self.dict.subject_id(t) {
5839                Some(id) => Some(id),
5840                None => return None,
5841            },
5842            None => None,
5843        };
5844        let pid = match p {
5845            Some(t) => match self.dict.predicate_id(t) {
5846                Some(id) => Some(id),
5847                None => return None,
5848            },
5849            None => None,
5850        };
5851        let oid = match o {
5852            Some(t) => match self.dict.object_id(t) {
5853                Some(id) => Some(id),
5854                None => return None,
5855            },
5856            None => None,
5857        };
5858        Some((sid, pid, oid))
5859    }
5860
5861    /// Evaluate a triple pattern and include the file/index provenance for every
5862    /// matched result. A bound term that is unknown to the dictionary yields no
5863    /// matches.
5864    pub fn query_with_provenance(
5865        &self,
5866        s: Option<&str>,
5867        p: Option<&str>,
5868        o: Option<&str>,
5869    ) -> Vec<TripleProvenance> {
5870        let pattern = match self.resolve_query_pattern(s, p, o) {
5871            Some(pattern) => pattern,
5872            None => return Vec::new(),
5873        };
5874
5875        let index_permutation = GraphIndex::best_permutation_in(self.header.perms, pattern);
5876        let si = index_permutation.section_index();
5877        let (dictionary_range, index_range, index_section_range, pyramid_range) = match &self.v06 {
5878            Some(f) => {
5879                use crate::v06::kind;
5880                let sec = |k| f.section(k).unwrap_or_default();
5881                let perm = f
5882                    .root
5883                    .unit(crate::v06::tag::INDEX)
5884                    .and_then(|u| crate::v06::decode_index_root(u).ok())
5885                    .and_then(|v| v.into_iter().find(|p| p.si == si))
5886                    .map(|p| ByteRange {
5887                        offset: sec(kind::INDEX_DATA).offset + p.data_off,
5888                        len: p.data_len,
5889                    })
5890                    .unwrap_or_default();
5891                (
5892                    sec(kind::DICT_DATA),
5893                    sec(kind::INDEX_DATA),
5894                    perm,
5895                    f.section(kind::PYRAMID),
5896                )
5897            }
5898            None => (
5899                ByteRange {
5900                    offset: self.header.dictionary_offset,
5901                    len: self.header.dictionary_len,
5902                },
5903                ByteRange {
5904                    offset: self.header.root_dir_offset,
5905                    len: self.header.root_dir_len,
5906                },
5907                self.index_section_ranges[si],
5908                (self.header.pyramid_meta_len > 0).then_some(ByteRange {
5909                    offset: self.header.pyramid_meta_offset,
5910                    len: self.header.pyramid_meta_len,
5911                }),
5912            ),
5913        };
5914
5915        let tiles = &self.tile_ranges[si];
5916        let v06_tiles = self.v06.is_some().then(|| self.index.tile_sections()[si]);
5917        self.index
5918            .match_pattern(pattern)
5919            .into_iter()
5920            .filter_map(|(s, p, o)| {
5921                let terms = (
5922                    self.dict.subject_term(s)?,
5923                    self.dict.predicate_term(p)?,
5924                    self.dict.object_term(o)?,
5925                );
5926                // The physical tile holding this match: the one whose
5927                // leading-id range covers the match's permuted leading id.
5928                let a = index_permutation.forward((s, p, o)).0;
5929                let (tile, tile_range) = if let Some(tl) = v06_tiles {
5930                    let (ti, tj) = self.index.tile_span(si, Some(a));
5931                    match (ti < tj && ti < tl.len()).then(|| tl.range(ti)).flatten() {
5932                        Some((offset, len)) => (
5933                            Some(format!("{}/{ti}", index_permutation.name())),
5934                            Some(ByteRange { offset, len }),
5935                        ),
5936                        None => (None, None),
5937                    }
5938                } else {
5939                    let ti = tiles.partition_point(|&(_, max_a, _)| max_a < a);
5940                    match tiles.get(ti) {
5941                        Some(&(min_a, _, range)) if min_a <= a => (
5942                            Some(format!("{}/{ti}", index_permutation.name())),
5943                            Some(range),
5944                        ),
5945                        _ => (None, None),
5946                    }
5947                };
5948                Some(TripleProvenance {
5949                    terms,
5950                    ids: (s, p, o),
5951                    graph: None,
5952                    matched_pattern: pattern,
5953                    index_permutation,
5954                    dictionary_range,
5955                    index_range,
5956                    index_section_range,
5957                    pyramid_range,
5958                    tile,
5959                    tile_range,
5960                })
5961            })
5962            .collect()
5963    }
5964
5965    /// Evaluate a triple pattern given as optional term strings, returning
5966    /// matching triples resolved back to terms. A bound term that is unknown to
5967    /// the dictionary yields no matches.
5968    pub fn query(&self, s: Option<&str>, p: Option<&str>, o: Option<&str>) -> Vec<TermTriple> {
5969        self.query_with_provenance(s, p, o)
5970            .into_iter()
5971            .map(|m| m.terms)
5972            .collect()
5973    }
5974
5975    /// Match a triple pattern **within a single graph** — `None` is the default
5976    /// graph, `Some(iri)` a named graph — resolving matches to canonical terms.
5977    /// This is [`Rete::query`] (default-graph only) generalized to any graph: the
5978    /// graph-scoped primitive a quad-aware consumer (e.g. an RDF4J `Sail`'s
5979    /// `getStatements`) needs. An unknown graph IRI, or a bound term absent from
5980    /// the shared dictionary, yields an empty result. All graphs share one
5981    /// dictionary, so the pattern resolves once against that ID space.
5982    pub fn query_in_graph(
5983        &self,
5984        graph: Option<&str>,
5985        s: Option<&str>,
5986        p: Option<&str>,
5987        o: Option<&str>,
5988    ) -> Vec<TermTriple> {
5989        let pattern = match self.resolve_query_pattern(s, p, o) {
5990            Some(pattern) => pattern,
5991            None => return Vec::new(),
5992        };
5993        let index = match graph {
5994            None => &self.index,
5995            Some(g) => match self.graph_index(g) {
5996                Some(i) => i,
5997                None => return Vec::new(),
5998            },
5999        };
6000        self.resolve_matches(index.match_pattern(pattern))
6001    }
6002
6003    /// Resolve matched id-triples to terms, with one coalesced dictionary
6004    /// fault for exactly their terms.
6005    fn resolve_matches(&self, ids: Vec<Triple>) -> Vec<TermTriple> {
6006        // One coalesced dictionary fault for THIS pattern's output, not the
6007        // whole dictionary. `prefetch_all` used to sit here, which made a
6008        // bounded `(s, p, o, g)` lookup — the primitive an RDF4J `Sail`'s
6009        // `getStatements` calls, once per named graph via `query_quads` — pay
6010        // for every term in the file: on `cordis.rete` (801 MB, a 417 MB
6011        // dictionary) a 15-row answer read 416 MB and peaked at 2.5 GB RSS.
6012        // Same call `dump_batch` makes, for the same reason.
6013        if !ids.is_empty() {
6014            let mut nodes = Vec::with_capacity(ids.len() * 2);
6015            let mut preds = Vec::with_capacity(ids.len());
6016            for &(s, p, o) in &ids {
6017                nodes.push(self.dict.subject_node(s));
6018                nodes.push(self.dict.object_node(o));
6019                preds.push(p);
6020            }
6021            self.dict.prefetch_terms(&nodes, &preds);
6022        }
6023        ids.into_iter()
6024            .filter_map(|(s, p, o)| {
6025                Some((
6026                    self.dict.subject_term(s)?,
6027                    self.dict.predicate_term(p)?,
6028                    self.dict.object_term(o)?,
6029                ))
6030            })
6031            .collect()
6032    }
6033
6034    /// Match a triple pattern across the default graph **and every named graph**,
6035    /// tagging each match with its graph (`None` = default). The quad-level
6036    /// companion to [`Rete::query`]; default-graph matches come first, then each
6037    /// named graph in stored order.
6038    pub fn query_quads(
6039        &self,
6040        s: Option<&str>,
6041        p: Option<&str>,
6042        o: Option<&str>,
6043    ) -> Vec<(TermTriple, Option<String>)> {
6044        let mut out: Vec<(TermTriple, Option<String>)> = self
6045            .query_in_graph(None, s, p, o)
6046            .into_iter()
6047            .map(|t| (t, None))
6048            .collect();
6049        let Some(pattern) = self.resolve_query_pattern(s, p, o) else {
6050            return out;
6051        };
6052        for i in 0..self.named_graphs.count() {
6053            // A quad dump visits every graph by construction — exhaustive
6054            // demand, so a lazy walk may read the section in bulk chunks.
6055            let Some(iri) = self.named_graphs.name_at(&self.dict, i, true) else {
6056                continue;
6057            };
6058            // A small quad-index graph through an owned view, dropped here:
6059            // the walk holds one view at a time, not one per graph.
6060            let owned = self.named_graph_transient(i);
6061            let index = match &owned {
6062                Some(view) => view,
6063                None => match self.named_graphs.graph_at(&self.dict, i, true) {
6064                    Some((_, g)) => g,
6065                    None => continue,
6066                },
6067            };
6068            for triple in self.resolve_matches(index.match_pattern(pattern)) {
6069                out.push((triple, Some(iri.to_string())));
6070            }
6071        }
6072        out
6073    }
6074
6075    /// Evaluate one triple pattern through a [`RangeReader`] by fetching only
6076    /// the header, the dictionary, and — for a tiled (v0.2) file — the
6077    /// selected permutation section's tile **directory** plus the tile(s) the
6078    /// bound leading id routes to; an unbound leading id fetches the section's
6079    /// tile body in one request. v0.1 files fetch the whole selected section.
6080    /// Unknown bound terms return an empty result before touching the index.
6081    pub fn query_ranged<R: RangeReader>(
6082        reader: &R,
6083        s: Option<&str>,
6084        p: Option<&str>,
6085        o: Option<&str>,
6086    ) -> Result<Vec<TermTriple>, FileError> {
6087        let head = reader.read_at(0, (HEADER_LEN as u64).min(reader.len()))?;
6088        if crate::v06::open::is_v06(&head) {
6089            return Ok(v06_query_ranged(reader, head, s, p, o, true)?.unwrap_or_default());
6090        }
6091        let routed = match route_pattern(reader, head, s, p, o)? {
6092            Some(routed) => routed,
6093            None => return Ok(Vec::new()),
6094        };
6095        let matches = fetch_routed_matches(reader, &routed)?;
6096        Ok(matches
6097            .into_iter()
6098            .filter_map(|(s, p, o)| {
6099                Some((
6100                    routed.dict.subject_term(s)?,
6101                    routed.dict.predicate_term(p)?,
6102                    routed.dict.object_term(o)?,
6103                ))
6104            })
6105            .collect())
6106    }
6107
6108    /// Route one triple pattern to its permutation section without fetching
6109    /// any payload bytes. Returns `false` when a bound term is unknown and the
6110    /// index was skipped.
6111    pub fn route_pattern_ranged<R: RangeReader>(
6112        reader: &R,
6113        s: Option<&str>,
6114        p: Option<&str>,
6115        o: Option<&str>,
6116    ) -> Result<bool, FileError> {
6117        let head = reader.read_at(0, (HEADER_LEN as u64).min(reader.len()))?;
6118        if crate::v06::open::is_v06(&head) {
6119            return Ok(v06_query_ranged(reader, head, s, p, o, false)?.is_some());
6120        }
6121        Ok(route_pattern(reader, head, s, p, o)?.is_some())
6122    }
6123}
6124
6125/// [`Rete::query_ranged`] / [`Rete::route_pattern_ranged`] on a `0x06` file
6126/// through a borrowed reader: the front and the dictionary data — contiguous,
6127/// what the `0x05` path reads as header + dictionary — in one more request
6128/// after `head`, the pattern resolved there, then (with `fetch`) the routed
6129/// tiles in one span. `None` when a bound term is not in the dictionary.
6130fn v06_query_ranged<R: RangeReader>(
6131    reader: &R,
6132    head: Vec<u8>,
6133    s: Option<&str>,
6134    p: Option<&str>,
6135    o: Option<&str>,
6136    fetch: bool,
6137) -> Result<Option<Vec<TermTriple>>, FileError> {
6138    use crate::v06::{kind, open::SparseReader, Header06, Root06, HEADER_LEN};
6139    let len = reader.len();
6140    let h = Header06::from_bytes(&head)?;
6141    let root_end = (HEADER_LEN as u64 + h.root_len as u64).min(len);
6142    let root_bytes = if root_end <= head.len() as u64 {
6143        head[HEADER_LEN..root_end as usize].to_vec()
6144    } else {
6145        reader.read_at(HEADER_LEN as u64, root_end - HEADER_LEN as u64)?
6146    };
6147    let root = Root06::decode(&root_bytes, h.internal_codec)?;
6148    let dict = root.section(kind::DICT_DATA).and_then(|d| d.abs(&h).ok());
6149    // The open below reads only the 1 KiB it must (not the 64 KiB first
6150    // read): what it needs past that is the front and the dictionary, here.
6151    let end = dict
6152        .map_or(h.data_offset, |d| d.offset + d.len)
6153        .max(crate::header::HEADER_LEN as u64)
6154        .min(len);
6155    let mut held = head;
6156    if end > held.len() as u64 {
6157        let at = held.len() as u64;
6158        held.extend_from_slice(&reader.read_at(at, end - at)?);
6159    }
6160    let sparse = SparseReader::fetch(reader, held, &[])?;
6161    let rete = Rete::open_ranged_lazy_with(sparse, 0)?;
6162    let Some(pattern) = resolve_query_pattern(rete.dictionary(), s, p, o) else {
6163        return Ok(None);
6164    };
6165    if !fetch {
6166        return Ok(Some(Vec::new()));
6167    }
6168    let perm = GraphIndex::best_permutation_in(h.perms(), pattern);
6169    let si = perm.section_index();
6170    let index = rete.default_index();
6171    let [pa, _, _] = perm.order_pattern(pattern);
6172    // One span of tiles, contiguous in the file: one request. A clustered
6173    // section has a span per class run that can hold a bound subject.
6174    let mut out = Vec::new();
6175    for (start, stop) in index.routed_spans(si, pa) {
6176        let ranges: Vec<(u64, u64)> = (start..stop)
6177            .filter_map(|i| index.tile_range(si, i))
6178            .collect();
6179        if let (Some(&(lo, _)), Some(&(last, last_len))) = (ranges.first(), ranges.last()) {
6180            let body = reader.read_at(lo, last + last_len - lo)?;
6181            for &(off, l) in &ranges {
6182                let at = (off - lo) as usize;
6183                let tile = decompress(h.block_codec, &body[at..at + l as usize])?;
6184                out.extend(GraphIndex::match_serialized_block(&tile, perm, pattern));
6185            }
6186        }
6187    }
6188    out.sort_unstable();
6189    let d = rete.dictionary();
6190    Ok(Some(
6191        out.into_iter()
6192            .filter_map(|(s, p, o)| {
6193                Some((d.subject_term(s)?, d.predicate_term(p)?, d.object_term(o)?))
6194            })
6195            .collect(),
6196    ))
6197}
6198
6199/// A pattern routed to its permutation section: everything needed to fetch
6200/// matches, with no payload bytes read yet.
6201struct RoutedPattern {
6202    dict: Dictionary,
6203    pattern: Pattern,
6204    permutation: IndexPermutation,
6205    header: Header,
6206    /// Absolute byte range of the selected section's payload.
6207    section: ByteRange,
6208}
6209
6210/// Resolve a pattern against the remote dictionary and locate its permutation
6211/// section (header + dictionary + container framing only).
6212fn route_pattern<R: RangeReader>(
6213    reader: &R,
6214    head: Vec<u8>,
6215    s: Option<&str>,
6216    p: Option<&str>,
6217    o: Option<&str>,
6218) -> Result<Option<RoutedPattern>, FileError> {
6219    let header = Header::from_bytes(&head)?;
6220
6221    let dict_bytes = reader.read_at(header.dictionary_offset, header.dictionary_len)?;
6222    let dict = decode_dictionary_container(&dict_bytes, header.dict_codec)?;
6223
6224    let Some(pattern) = resolve_query_pattern(&dict, s, p, o) else {
6225        return Ok(None);
6226    };
6227    let permutation = GraphIndex::best_permutation_in(header.perms, pattern);
6228    let section = locate_container_section_ranged(
6229        reader,
6230        header.root_dir_offset,
6231        header.root_dir_len,
6232        header
6233            .perms
6234            .position(permutation)
6235            .ok_or(FileError::Container("routed to an absent permutation"))?,
6236        header.index_container_sections() as u64,
6237    )?;
6238    Ok(Some(RoutedPattern {
6239        dict,
6240        pattern,
6241        permutation,
6242        header,
6243        section,
6244    }))
6245}
6246
6247/// Fetch and scan a routed pattern's matches: read the tile directory, then only
6248/// the matching tile byte ranges (the run of covering tiles for a bound leading
6249/// id — one for ordinary groups, several for a split mega-group — the
6250/// O(matching bytes) promise).
6251fn fetch_routed_matches<R: RangeReader>(
6252    reader: &R,
6253    routed: &RoutedPattern,
6254) -> Result<Vec<Triple>, FileError> {
6255    let dir = read_tile_directory_ranged(reader, routed.section)?;
6256    let [pa, _, _] = routed.permutation.order_pattern(routed.pattern);
6257    let codec = routed.header.block_codec;
6258    let mut out = Vec::new();
6259    match pa {
6260        // Bound leading id: the run of covering tiles (several for a split
6261        // mega-group; one otherwise).
6262        Some(a) => {
6263            for e in dir.iter().filter(|e| e.min_a <= a && a <= e.max_a) {
6264                let len = e.end - e.start;
6265                let bytes = reader.read_at(checked_end(routed.section.offset, e.start)?, len)?;
6266                if bytes.len() as u64 != len {
6267                    return Err(FileError::Container("short tile read"));
6268                }
6269                let tile = decompress(codec, &bytes)?;
6270                out.extend(GraphIndex::match_serialized_block(
6271                    &tile,
6272                    routed.permutation,
6273                    routed.pattern,
6274                ));
6275            }
6276        }
6277        // Unbound leading id: every tile matters — fetch the contiguous tile
6278        // body in one request and slice it.
6279        None => {
6280            if let (Some(first), Some(last)) = (dir.first(), dir.last()) {
6281                let base = first.start;
6282                let body =
6283                    reader.read_at(checked_end(routed.section.offset, base)?, last.end - base)?;
6284                for e in &dir {
6285                    // Checked: a reader may return fewer bytes than asked.
6286                    let stored = checked_slice(&body, e.start - base, e.end - e.start)
6287                        .ok_or(FileError::Container("short tile read"))?;
6288                    let tile = decompress(codec, stored)?;
6289                    out.extend(GraphIndex::match_serialized_block(
6290                        &tile,
6291                        routed.permutation,
6292                        routed.pattern,
6293                    ));
6294                }
6295            }
6296        }
6297    }
6298    out.sort_unstable();
6299    Ok(out)
6300}
6301
6302fn resolve_query_pattern(
6303    dict: &Dictionary,
6304    s: Option<&str>,
6305    p: Option<&str>,
6306    o: Option<&str>,
6307) -> Option<Pattern> {
6308    let sid = match s {
6309        Some(t) => Some(dict.subject_id(t)?),
6310        None => None,
6311    };
6312    let pid = match p {
6313        Some(t) => Some(dict.predicate_id(t)?),
6314        None => None,
6315    };
6316    let oid = match o {
6317        Some(t) => Some(dict.object_id(t)?),
6318        None => None,
6319    };
6320    Some((sid, pid, oid))
6321}
6322
6323/// The lazily-chunked dictionary of a ranged open: per section, fetch the
6324/// header + chunk directory and fault the chunk bodies in on first term lookup.
6325///
6326/// `want` selects which of the four sections (0 shared, 1 subject-only,
6327/// 2 object-only, 3 predicates) are really read; a skipped one becomes an empty
6328/// section whose terms resolve to `None`. On a file written before #198 a
6329/// **directory is not small**: it carried each chunk's first term verbatim, so
6330/// the object-only section of a dataset that stores abstracts can run to
6331/// hundreds of megabytes, and fetching it is most of what a remote open costs.
6332/// Current builds key it by the shortest separator instead (a few bytes per
6333/// chunk), but that is write-side, so every already-published file still pays.
6334/// A reader that only ever resolves subjects (see [`SearchView`]) skips
6335/// sections 2 and 3 and pays none of it. [`Rete::open_ranged_lazy`] wants all
6336/// four.
6337fn ranged_chunked_dictionary<R: RangeReader + Send + Sync + 'static>(
6338    reader: &std::sync::Arc<R>,
6339    header: &Header,
6340    want: [bool; 4],
6341) -> Result<Dictionary, FileError> {
6342    // One (unlimited) cache shared across the four sections, keyed by
6343    // (section_index, chunk_index).
6344    let cache = crate::chunk_cache::ChunkCache::unlimited_arc();
6345    let mut dict_sections: Vec<crate::dict::ChunkedSection> = Vec::with_capacity(4);
6346    let mut section_ranges: [std::sync::Arc<Vec<ByteRange>>; 4] = Default::default();
6347    for si in 0..4 {
6348        if !want[si] {
6349            dict_sections.push(crate::dict::ChunkedSection::from_parts(
6350                crate::dict::SectionMeta {
6351                    term_count: 0,
6352                    restart_interval: 1,
6353                    restart_offsets: Vec::new(),
6354                },
6355                Vec::new(),
6356                None,
6357                cache.clone(),
6358                si as u8,
6359            ));
6360            continue;
6361        }
6362        let section = locate_container_section_ranged(
6363            reader.as_ref(),
6364            header.dictionary_offset,
6365            header.dictionary_len,
6366            si,
6367            4,
6368        )?;
6369        let (meta, entries) = read_dict_dir_ranged(reader.as_ref(), section)?;
6370        // One copy of the chunk ranges, shared by this section's loaders and
6371        // the dictionary's cross-section batch loader.
6372        let ranges: std::sync::Arc<Vec<ByteRange>> = std::sync::Arc::new(
6373            entries
6374                .iter()
6375                .map(|e| ByteRange {
6376                    offset: section.offset + e.start,
6377                    len: (e.end - e.start),
6378                })
6379                .collect(),
6380        );
6381        section_ranges[si] = ranges.clone();
6382        let chunks: Vec<crate::dict::SectionChunk> = entries
6383            .into_iter()
6384            .map(|e| crate::dict::SectionChunk::new(e.first_run, e.key, e.body_start))
6385            .collect();
6386        let chunk_reader = reader.clone();
6387        let codec = header.dict_codec;
6388        let loader_ranges = ranges.clone();
6389        let loader: crate::dict::ChunkLoader = Box::new(move |ci| {
6390            let range = loader_ranges.get(ci)?;
6391            let bytes = chunk_reader.read_at(range.offset, range.len).ok()?;
6392            decompress(codec, &bytes).ok()
6393        });
6394        // Full-section sweeps (export/dump) batch their chunk fetches:
6395        // adjacent chunk ranges coalesce into a handful of range reads.
6396        let bulk_reader = reader.clone();
6397        let bulk: crate::dict::ChunkBulkLoader = Box::new(move |cis| {
6398            let want: Option<Vec<ByteRange>> =
6399                cis.iter().map(|&ci| ranges.get(ci).copied()).collect();
6400            let blobs = read_coalesced(bulk_reader.as_ref(), &want?, DICT_COALESCE_GAP)?;
6401            blobs.iter().map(|b| decompress(codec, b).ok()).collect()
6402        });
6403        dict_sections.push(
6404            crate::dict::ChunkedSection::from_parts(
6405                meta,
6406                chunks,
6407                Some(loader),
6408                cache.clone(),
6409                si as u8,
6410            )
6411            .with_bulk_loader(bulk),
6412        );
6413    }
6414    let dict_arr: [crate::dict::ChunkedSection; 4] = dict_sections
6415        .try_into()
6416        .map_err(|_| FileError::Container("expected 4 dictionary sections"))?;
6417    // Decoding a result touches several sections at once; this fetches all
6418    // their missing chunks in one batch (pairs arrive section by section in
6419    // ascending chunk order, so ascending in the file, as coalescing wants).
6420    let multi_reader = reader.clone();
6421    let codec = header.dict_codec;
6422    let multi: crate::dict::MultiChunkBulkLoader = Box::new(move |pairs| {
6423        let want: Option<Vec<ByteRange>> = pairs
6424            .iter()
6425            .map(|&(si, ci)| section_ranges.get(si as usize)?.get(ci).copied())
6426            .collect();
6427        let blobs = read_coalesced(multi_reader.as_ref(), &want?, DICT_COALESCE_GAP)?;
6428        blobs.iter().map(|b| decompress(codec, b).ok()).collect()
6429    });
6430    Ok(Dictionary::from_chunked_sections(dict_arr).with_multi_bulk_loader(multi))
6431}
6432
6433/// Run two independent steps of an open: at the same time when the reader can
6434/// keep several reads in flight (`concurrency > 1`) on a target with threads,
6435/// else one after the other. Either way both run to completion and their
6436/// results come back as they would inline; a panic in either propagates.
6437#[cfg(not(target_arch = "wasm32"))]
6438fn open_side_by_side<A, B: Send>(
6439    concurrency: usize,
6440    a: impl FnOnce() -> A,
6441    b: impl FnOnce() -> B + Send,
6442) -> (A, B) {
6443    if concurrency <= 1 {
6444        return (a(), b());
6445    }
6446    std::thread::scope(|scope| {
6447        let hb = scope.spawn(b);
6448        let ra = a();
6449        let rb = hb
6450            .join()
6451            .unwrap_or_else(|panic| std::panic::resume_unwind(panic));
6452        (ra, rb)
6453    })
6454}
6455
6456/// wasm32 has no threads here: always one after the other.
6457#[cfg(target_arch = "wasm32")]
6458fn open_side_by_side<A, B>(
6459    _concurrency: usize,
6460    a: impl FnOnce() -> A,
6461    b: impl FnOnce() -> B,
6462) -> (A, B) {
6463    (a(), b())
6464}
6465
6466/// The `v06-parity` test hook: a 0x05 image converted to 0x06 (`None` for a
6467/// 0x06 image, or one the converter refuses, such as per-graph named graphs).
6468#[cfg(feature = "v06-parity")]
6469fn parity_image(bytes: &[u8]) -> Option<Vec<u8>> {
6470    if crate::v06::open::is_v06(bytes) || bytes.len() < HEADER_LEN {
6471        return None;
6472    }
6473    crate::v06::write::convert_image(bytes, crate::v06::write::WriteOpts::default()).ok()
6474}
6475
6476/// [`Rete::file_layout`] for a `0x06` file: the header, the root, the card
6477/// region, then every front and data section, in file order.
6478fn v06_layout(f: &crate::v06::open::V06File) -> Vec<LayoutSegment> {
6479    let h = &f.h;
6480    let seg = |kind: &'static str, label: String, offset: u64, len: u64| LayoutSegment {
6481        kind,
6482        label,
6483        offset,
6484        len,
6485    };
6486    let mut out = vec![
6487        seg(
6488            "header",
6489            format!("header (fixed {} bytes)", crate::v06::HEADER_LEN),
6490            0,
6491            crate::v06::HEADER_LEN as u64,
6492        ),
6493        seg(
6494            "directory",
6495            "root block (section table, directory roots)".into(),
6496            h.root_offset(),
6497            h.root_len as u64,
6498        ),
6499    ];
6500    let c = h.card_range();
6501    if c.len > 0 {
6502        out.push(seg(
6503            "metadata",
6504            "metadata (dataset card, zstd)".into(),
6505            c.offset,
6506            c.len,
6507        ));
6508    }
6509    let b = h.build_range();
6510    if b.len > 0 {
6511        out.push(seg("build-info", "build record".into(), b.offset, b.len));
6512    }
6513    for s in &f.root.sections {
6514        let Ok(a) = s.abs(h) else { continue };
6515        let kind: &'static str = match s.kind {
6516            crate::v06::kind::SCHEMA => "schema",
6517            crate::v06::kind::DICT_DATA => "dictionary",
6518            crate::v06::kind::INDEX_DATA => "tile",
6519            crate::v06::kind::QUAD_DATA => "named-graphs",
6520            crate::v06::kind::LABEL_DATA => "label-index",
6521            crate::v06::kind::TEXT_DATA => "text-index",
6522            crate::v06::kind::PYRAMID => "pyramid",
6523            _ => "directory",
6524        };
6525        out.push(seg(
6526            kind,
6527            crate::v06::kind::name(s.kind).to_string(),
6528            a.offset,
6529            a.len,
6530        ));
6531    }
6532    out.sort_by_key(|s| s.offset);
6533    out
6534}
6535
6536/// The deferred slots of a `0x06` open: pyramid, text index, label index and
6537/// schema, each faulted on first use.
6538#[allow(clippy::type_complexity)]
6539fn v06_slots(
6540    f: &std::sync::Arc<crate::v06::open::V06File>,
6541) -> (
6542    PyramidSlot,
6543    TextIndexSlot,
6544    SectionSlot<crate::label_index::LabelIndex>,
6545    SectionSlot<crate::schema_section::SchemaMeta>,
6546) {
6547    use crate::v06::{P_LABELS, P_PYRAMID, P_SCHEMA, P_TEXT};
6548    let pyramid = if f.h.has(P_PYRAMID) {
6549        let a = f.clone();
6550        PyramidSlot::Lazy {
6551            loader: Box::new(move || PyramidMeta::decode(&a.pyramid_bytes()?).ok()),
6552            cell: std::sync::OnceLock::new(),
6553        }
6554    } else {
6555        PyramidSlot::Resident(None)
6556    };
6557    let text_index = if f.h.has(P_TEXT) {
6558        let (a, b) = (f.clone(), f.clone());
6559        TextIndexSlot::Lazy {
6560            loader: Box::new(move || a.text_index()?.ok()),
6561            cell: std::sync::OnceLock::new(),
6562            token_table: Box::new(move || b.token_table_bytes()),
6563            token_table_cell: std::sync::OnceLock::new(),
6564        }
6565    } else {
6566        TextIndexSlot::Resident {
6567            index: None,
6568            token_table_len: None,
6569        }
6570    };
6571    let labels = if f.h.has(P_LABELS) {
6572        let a = f.clone();
6573        SectionSlot::Lazy {
6574            loader: Box::new(move || a.label_index()?.ok()),
6575            cell: std::sync::OnceLock::new(),
6576        }
6577    } else {
6578        SectionSlot::Resident(None)
6579    };
6580    let schema = if f.h.has(P_SCHEMA) {
6581        let a = f.clone();
6582        SectionSlot::Lazy {
6583            loader: Box::new(move || a.schema()),
6584            cell: std::sync::OnceLock::new(),
6585        }
6586    } else {
6587        SectionSlot::Resident(None)
6588    };
6589    (pyramid, text_index, labels, schema)
6590}
6591
6592/// The pyramid meta is large on real graphs (tens of MB) and SPARQL never reads
6593/// it, so a ranged open defers its fetch: it faults in only if `pyramid()` is
6594/// called (community / pyramid_tree / label-prefix queries).
6595fn ranged_pyramid_slot<R: RangeReader + Send + Sync + 'static>(
6596    reader: &std::sync::Arc<R>,
6597    header: &Header,
6598) -> PyramidSlot {
6599    if header.pyramid_meta_len == 0 {
6600        return PyramidSlot::Resident(None);
6601    }
6602    let pyr_reader = reader.clone();
6603    let pyr_off = header.pyramid_meta_offset;
6604    let pyr_len = header.pyramid_meta_len;
6605    PyramidSlot::Lazy {
6606        loader: Box::new(move || {
6607            let mb = pyr_reader.read_at(pyr_off, pyr_len).ok()?;
6608            PyramidMeta::decode(&mb).ok()
6609        }),
6610        cell: std::sync::OnceLock::new(),
6611    }
6612}
6613
6614/// The deferred TEXT_INDEX of a ranged open: a text search faults the token
6615/// table on first call (the leading varint then its compressed bytes), then
6616/// fetches individual posting lists by `(offset, len)` — never the whole
6617/// postings blob. A caller that never searches pays nothing.
6618fn ranged_text_index_slot<R: RangeReader + Send + Sync + 'static>(
6619    reader: &std::sync::Arc<R>,
6620    header: &Header,
6621) -> TextIndexSlot {
6622    let (ti_off, ti_len) = header.text_index_payload();
6623    if ti_len == 0 {
6624        return TextIndexSlot::Resident {
6625            index: None,
6626            token_table_len: None,
6627        };
6628    }
6629    let ti_reader = reader.clone();
6630    let probe_reader = reader.clone();
6631    let codec = header.block_codec;
6632    TextIndexSlot::Lazy {
6633        loader: Box::new(move || {
6634            // The section opens with `varint token_table_len`; read enough to
6635            // decode it (a uvarint is ≤ 10 bytes), then fetch the varint + the
6636            // compressed token table as one prefix range.
6637            let prefix_len = read_token_table_len(&*ti_reader, ti_off, ti_len)?;
6638            let prefix = ti_reader.read_at(ti_off, prefix_len).ok()?;
6639            let postings_base = crate::text_index::TextIndex::postings_base(&prefix)? as u64;
6640            let postings_abs = ti_off + postings_base;
6641            let pr = ti_reader.clone();
6642            let posting_loader =
6643                Box::new(move |off: u64, len: u64| pr.read_at(postings_abs + off, len).ok());
6644            crate::text_index::TextIndex::from_token_table(&prefix, codec, posting_loader).ok()
6645        }),
6646        cell: std::sync::OnceLock::new(),
6647        // The same leading varint the loader starts from, asked on its own so a
6648        // caller can *state* the cost of a first search without paying it.
6649        token_table: Box::new(move || read_token_table_len(&*probe_reader, ti_off, ti_len)),
6650        token_table_cell: std::sync::OnceLock::new(),
6651    }
6652}
6653
6654/// The `(label, subject term)` pairs of a legacy (pyramid-block) label search.
6655/// The hits' subjects are scattered over the dictionary, so their chunks are
6656/// faulted in one batch first (one round trip on a remote file, as
6657/// [`text_search_in`] does for full-text hits), then decoded in one
6658/// chunk-ordered pass. A subject that does not resolve drops its hit, as
6659/// before; the order is the search's.
6660fn resolve_label_hits(
6661    dict: &Dictionary,
6662    hits: Vec<&crate::meta::LabelEntry>,
6663) -> Vec<(String, String)> {
6664    let ids: Vec<u32> = hits.iter().map(|e| e.subject).collect();
6665    dict.prefetch_subject_terms(&ids);
6666    hits.iter()
6667        .zip(dict.subject_terms(&ids))
6668        .filter_map(|(e, iri)| Some((e.label.clone(), iri?)))
6669        .collect()
6670}
6671
6672/// Full-text search over `ti`, resolving the matched subject ids through `dict`.
6673/// The engine behind [`Rete::text_search`] and [`SearchView::text_search`].
6674fn text_search_in(
6675    ti: Option<&crate::text_index::TextIndex>,
6676    dict: &Dictionary,
6677    words: &[&str],
6678    prefix: Option<&str>,
6679    limit: usize,
6680) -> Vec<String> {
6681    let Some(ti) = ti else {
6682        return Vec::new();
6683    };
6684    // Each query word is tokenized exactly as at build time (so "Glucose"
6685    // matches the stored "glucose"); a word that splits into several tokens
6686    // requires all of them. AND across every required token + the prefix.
6687    let mut acc: Option<Vec<u32>> = None;
6688    if let Some(p) = prefix {
6689        acc = Some(ti.prefix(&p.to_lowercase()));
6690    }
6691    for w in words {
6692        for tok in crate::text_index::tokenize(w) {
6693            let posting = ti.lookup(&tok);
6694            acc = Some(match acc {
6695                Some(a) => intersect_sorted(&a, &posting),
6696                None => posting,
6697            });
6698            if acc.as_ref().is_some_and(|a| a.is_empty()) {
6699                return Vec::new();
6700            }
6701        }
6702    }
6703    let ids = acc.unwrap_or_default();
6704    let mut out = Vec::with_capacity(if limit > 0 {
6705        limit.min(ids.len())
6706    } else {
6707        ids.len()
6708    });
6709    // The hits' subject IRIs are scattered over many dictionary chunks: fault
6710    // the ones a page can return in one batch (on a remote file, one round
6711    // trip instead of one per chunk). Only warms the cache.
6712    let page = if limit > 0 {
6713        limit.min(ids.len())
6714    } else {
6715        ids.len()
6716    };
6717    dict.prefetch_subject_terms(&ids[..page]);
6718    for id in ids {
6719        if let Some(iri) = dict.subject_term(id) {
6720            out.push(iri);
6721            if limit > 0 && out.len() >= limit {
6722                break;
6723            }
6724        }
6725    }
6726    out
6727}
6728
6729/// A `.rete` opened for **search only**: the dictionary's chunk directories, and
6730/// the TEXT_INDEX / pyramid deferred behind their lazy slots. Nothing else.
6731///
6732/// What makes this cheap is skipping dictionary sections 2 and 3 (object-only
6733/// and predicates) — NOT skipping the index. Measured on the published
6734/// `epfl-infoscience.rete` (1.64 GB), a full [`Rete::open_ranged_lazy`] reads
6735/// 536,947,344 B, of which the six permutation tile directories are **49,940 B
6736/// in 40 reads** and the dictionary is 536,896,380 B in 36 reads: the
6737/// object-only chunk directory alone is 234,400,728 B, because that file stores
6738/// every chunk's first term verbatim. Files built since #198 key it by the
6739/// shortest separator (~600 KB on this graph); the published one still does
6740/// not, and only a rebuild changes that. This view opens sections 0 and 1
6741/// only — both search modes return subject IRIs — and costs **21,554 B in 9
6742/// reads** on the same file. It also skips the index container and defers the
6743/// TEXT_INDEX and pyramid, so it pays only for what it reads: the token table
6744/// on the first search, then one range per posting list and per dictionary
6745/// chunk holding a matched subject. This is what backs `rete search-url`.
6746///
6747/// On a file with a LABEL_INDEX section (kind 8) the dictionary is not opened
6748/// at all until a full-text search needs it: the section stores each subject's
6749/// term, so a prefix search costs the header, the section head, one directory
6750/// node per level and one data block.
6751pub struct SearchView {
6752    header: Header,
6753    dict: SectionSlot<Dictionary>,
6754    pyramid: PyramidSlot,
6755    text_index: TextIndexSlot,
6756    labels: SectionSlot<crate::label_index::LabelIndex>,
6757}
6758
6759impl SearchView {
6760    /// Open for search over any [`RangeReader`] (HTTP or local): header +
6761    /// dictionary chunk directories only (none at all on a file with a
6762    /// LABEL_INDEX, until a text search needs them), with the text index,
6763    /// label index and pyramid faulting in on first use.
6764    pub fn open_ranged<R: RangeReader + Send + Sync + 'static>(
6765        reader: R,
6766    ) -> Result<Self, FileError> {
6767        Self::open_ranged_with(reader, crate::v06::FIRST_READ)
6768    }
6769
6770    /// [`open_ranged`](Self::open_ranged) with an explicit first-read size
6771    /// (see [`Rete::open_ranged_lazy_with`]).
6772    pub fn open_ranged_with<R: RangeReader + Send + Sync + 'static>(
6773        reader: R,
6774        first_read: u64,
6775    ) -> Result<Self, FileError> {
6776        let first_len = first_read.max(HEADER_LEN as u64).min(reader.len());
6777        let first: std::sync::Arc<[u8]> = std::sync::Arc::from(reader.read_at(0, first_len)?);
6778        let reader = crate::reader::PrefixReader::new(reader, first.clone());
6779        if crate::v06::open::is_v06(&first) {
6780            let r: std::sync::Arc<dyn RangeReader + Send + Sync> = std::sync::Arc::new(reader);
6781            let f = std::sync::Arc::new(crate::v06::open::V06File::open(r, &first)?);
6782            let header = crate::v06::logical_header(&f.h);
6783            let (pyramid, text_index, labels, _) = v06_slots(&f);
6784            let want = [true, true, false, false];
6785            let dict = if f.h.has(crate::v06::P_LABELS) {
6786                let a = f.clone();
6787                SectionSlot::Lazy {
6788                    loader: Box::new(move || a.dictionary(want).ok()),
6789                    cell: std::sync::OnceLock::new(),
6790                }
6791            } else {
6792                SectionSlot::Resident(Some(f.dictionary(want)?))
6793            };
6794            return Ok(Self {
6795                header,
6796                dict,
6797                pyramid,
6798                text_index,
6799                labels,
6800            });
6801        }
6802        let head = reader.read_at(0, HEADER_LEN as u64)?;
6803        let header = Header::from_bytes(&head)?;
6804        let reader = std::sync::Arc::new(reader);
6805        // Sections 0 (shared) and 1 (subject-only) are the only ones a search
6806        // resolves — both modes return subject IRIs. Skipping 2 and 3 is what
6807        // makes this open cheap where a full one is not.
6808        let dict = if header.label_index_section().is_some() {
6809            let (r, h) = (reader.clone(), header.clone());
6810            SectionSlot::Lazy {
6811                loader: Box::new(move || {
6812                    ranged_chunked_dictionary(&r, &h, [true, true, false, false]).ok()
6813                }),
6814                cell: std::sync::OnceLock::new(),
6815            }
6816        } else {
6817            SectionSlot::Resident(Some(ranged_chunked_dictionary(
6818                &reader,
6819                &header,
6820                [true, true, false, false],
6821            )?))
6822        };
6823        let pyramid = ranged_pyramid_slot(&reader, &header);
6824        let text_index = ranged_text_index_slot(&reader, &header);
6825        let labels = ranged_label_slot(&reader, &header);
6826        Ok(Self {
6827            header,
6828            dict,
6829            pyramid,
6830            text_index,
6831            labels,
6832        })
6833    }
6834
6835    /// Whether the file can answer a prefix search: it carries a LABEL_INDEX
6836    /// section or a pyramid (whose meta may hold the legacy label block). A
6837    /// header read, no fault.
6838    pub fn has_labels(&self) -> bool {
6839        self.header.label_index_section().is_some() || self.header.pyramid_meta_len > 0
6840    }
6841
6842    /// Whether the file carries a LABEL_INDEX section (kind 8).
6843    pub fn has_label_index(&self) -> bool {
6844        self.header.label_index_section().is_some()
6845    }
6846
6847    /// The file header (already fetched by [`open_ranged`](Self::open_ranged)).
6848    pub fn header(&self) -> &Header {
6849        &self.header
6850    }
6851
6852    /// Whether the file carries a TEXT_INDEX section — a header read, no fault.
6853    pub fn has_text_index(&self) -> bool {
6854        self.header.text_index_len > 0
6855    }
6856
6857    /// Whether the file carries a pyramid (where the label index lives) — a
6858    /// header read, no fault.
6859    pub fn has_pyramid(&self) -> bool {
6860        self.header.pyramid_meta_len > 0
6861    }
6862
6863    /// Full-text search — see [`Rete::text_search`] for the semantics. Faults
6864    /// the token table on the first call, then only the queried postings.
6865    pub fn text_search(&self, words: &[&str], prefix: Option<&str>, limit: usize) -> Vec<String> {
6866        let Some(dict) = self.dict.get() else {
6867            return Vec::new();
6868        };
6869        text_search_in(self.text_index.index(), dict, words, prefix, limit)
6870    }
6871
6872    /// What that first search will cost — see
6873    /// [`Rete::text_index_token_table_len`]. One ≤10-byte range read, so a
6874    /// caller can quote the figure before deciding to search.
6875    pub fn text_index_token_table_len(&self) -> Option<u64> {
6876        self.text_index.token_table_len()
6877    }
6878
6879    /// Label-prefix search — see [`Rete::prefix_search`]. Reads the
6880    /// LABEL_INDEX section when the file has one; otherwise faults the pyramid
6881    /// (which carries the legacy label block), the pricier path.
6882    pub fn prefix_search(&self, prefix: &str, limit: usize) -> Vec<(String, String)> {
6883        if let Some(li) = self.labels.get() {
6884            return li.prefix_search(prefix, limit);
6885        }
6886        let pyr = match &self.pyramid {
6887            PyramidSlot::Resident(p) => p.as_ref(),
6888            PyramidSlot::Lazy { loader, cell } => cell.get_or_init(loader).as_ref(),
6889        };
6890        let (Some(pyr), Some(dict)) = (pyr, self.dict.get()) else {
6891            return Vec::new();
6892        };
6893        resolve_label_hits(dict, pyr.prefix_search(prefix, limit))
6894    }
6895}
6896
6897/// A lightweight, overview-only view of a file: the pyramid summary graph plus
6898/// just enough dictionary to label predicates. Fetched via ranges *without*
6899/// touching the (large) triple index — the "load the coarse graph first" path
6900/// from SPEC.md §7.2.
6901#[must_use]
6902pub struct SummaryView {
6903    pub round: u32,
6904    pub summary: Vec<SuperEdge>,
6905    /// The shipped `subClassOf` hierarchy (v2 schema pyramid; empty on v1 files).
6906    pub class_hierarchy: Vec<ClassNode>,
6907    /// Per-level type rollups — the leveled legend, read index-free.
6908    pub level_rollups: Vec<LevelRollup>,
6909    /// Per-level lateral class-relation graph (the non-`is-a` connections).
6910    pub level_links: Vec<LevelLinks>,
6911    /// Per-community descriptors (Phase 4 progressive refinement; may be empty).
6912    pub descriptors: Vec<CommunityDescriptor>,
6913    /// `subClassOf` cycles (v2.1; empty on older files).
6914    pub subclass_cycles: Vec<Vec<String>>,
6915    /// `owl:disjointWith` class pairs (v2.1; empty on older files).
6916    pub disjoint_pairs: Vec<(String, String)>,
6917    /// `owl:equivalentClass` class pairs (v2.1; empty on older files).
6918    pub equivalent_pairs: Vec<(String, String)>,
6919    dict: Dictionary,
6920}
6921
6922impl SummaryView {
6923    /// Read header → dictionary → pyramid-meta only (skips the index container).
6924    pub fn open_ranged<R: RangeReader>(reader: &R) -> Result<Option<Self>, FileError> {
6925        let head = reader.read_at(0, (HEADER_LEN as u64).min(reader.len()))?;
6926        let header = Header::from_bytes(&head)?;
6927        if header.pyramid_meta_len == 0 {
6928            return Ok(None);
6929        }
6930        if crate::v06::reads(header.version) {
6931            return Self::open_ranged_v06(reader, &header, head).map(Some);
6932        }
6933
6934        let dict_bytes = reader.read_at(header.dictionary_offset, header.dictionary_len)?;
6935        let dict = decode_dictionary_container(&dict_bytes, header.dict_codec)?;
6936
6937        let mb = reader.read_at(header.pyramid_meta_offset, header.pyramid_meta_len)?;
6938        let mut meta =
6939            PyramidMeta::decode(&mb).map_err(|_| FileError::Container("malformed pyramid meta"))?;
6940        // A file whose schema moved to its own section (kind 9) keeps only the
6941        // community material here: take the schema fields from that section.
6942        if let Some(s) = read_schema_with_header(reader, &header) {
6943            meta.class_hierarchy = s.class_hierarchy;
6944            meta.level_rollups = s.level_rollups;
6945            meta.level_links = s.level_links;
6946            meta.subclass_cycles = s.subclass_cycles;
6947            meta.disjoint_pairs = s.disjoint_pairs;
6948            meta.equivalent_pairs = s.equivalent_pairs;
6949        }
6950
6951        Ok(Some(SummaryView {
6952            round: meta.round,
6953            summary: meta.summary,
6954            class_hierarchy: meta.class_hierarchy,
6955            level_rollups: meta.level_rollups,
6956            level_links: meta.level_links,
6957            descriptors: meta.descriptors,
6958            subclass_cycles: meta.subclass_cycles,
6959            disjoint_pairs: meta.disjoint_pairs,
6960            equivalent_pairs: meta.equivalent_pairs,
6961            dict,
6962        }))
6963    }
6964
6965    /// [`open_ranged`](Self::open_ranged) for a `0x06` file: the pyramid
6966    /// section, the schema, and the dictionary (its leaves and chunks read
6967    /// whole, as the `0x05` path reads the whole dictionary container).
6968    fn open_ranged_v06<R: RangeReader>(
6969        reader: &R,
6970        header: &Header,
6971        head: Vec<u8>,
6972    ) -> Result<Self, FileError> {
6973        use crate::v06::{kind, open};
6974        // One first read, then every range the view needs past it — the
6975        // pyramid, the schema, the dictionary's leaves and chunks — in one
6976        // batch.
6977        let mut first = head;
6978        let want_first = crate::v06::FIRST_READ.min(reader.len());
6979        if want_first > first.len() as u64 {
6980            let at = first.len() as u64;
6981            first.extend_from_slice(&reader.read_at(at, want_first - at)?);
6982        }
6983        let pre = open::Prefixed {
6984            first: &first,
6985            r: reader,
6986        };
6987        let (h, root) = open::read_front(&pre)?;
6988        let want: Vec<ByteRange> = [
6989            kind::PYRAMID,
6990            kind::SCHEMA,
6991            kind::DICT_LEAVES,
6992            kind::DICT_DATA,
6993        ]
6994        .iter()
6995        .filter_map(|&k| root.section(k).and_then(|s| s.abs(&h).ok()))
6996        .collect();
6997        let sparse = open::SparseReader::fetch(&pre, first.clone(), &want)?;
6998        let mb = open::read_section(&sparse, &h, &root, kind::PYRAMID)?
6999            .ok_or(FileError::Container("0x06 file has no pyramid"))?;
7000        let mut meta =
7001            PyramidMeta::decode(&mb).map_err(|_| FileError::Container("malformed pyramid meta"))?;
7002        if let Some(s) = read_schema_with_header(&sparse, header) {
7003            meta.class_hierarchy = s.class_hierarchy;
7004            meta.level_rollups = s.level_rollups;
7005            meta.level_links = s.level_links;
7006            meta.subclass_cycles = s.subclass_cycles;
7007            meta.disjoint_pairs = s.disjoint_pairs;
7008            meta.equivalent_pairs = s.equivalent_pairs;
7009        }
7010        let f = open::V06File::open(std::sync::Arc::new(sparse), &first)?;
7011        let dict = f.dictionary([true; 4])?;
7012        Ok(SummaryView {
7013            round: meta.round,
7014            summary: meta.summary,
7015            class_hierarchy: meta.class_hierarchy,
7016            level_rollups: meta.level_rollups,
7017            level_links: meta.level_links,
7018            descriptors: meta.descriptors,
7019            subclass_cycles: meta.subclass_cycles,
7020            disjoint_pairs: meta.disjoint_pairs,
7021            equivalent_pairs: meta.equivalent_pairs,
7022            dict,
7023        })
7024    }
7025
7026    /// A schema-only view of a file **without a pyramid**, from its SCHEMA
7027    /// section (kind 9): the hierarchy, levels and axioms, with an empty
7028    /// community summary. Reads the header and the SCHEMA section only — the
7029    /// section carries its own class and predicate strings, so no dictionary.
7030    /// `Ok(None)` when the file has no SCHEMA section. Callers that need the
7031    /// summary (`cost`, federation source selection, progressive queries) use
7032    /// [`open_ranged`](Self::open_ranged), which keeps requiring a pyramid.
7033    pub fn open_schema_ranged<R: RangeReader>(reader: &R) -> Result<Option<Self>, FileError> {
7034        let head = reader.read_at(0, HEADER_LEN as u64)?;
7035        let header = Header::from_bytes(&head)?;
7036        let Some(s) = read_schema_with_header(reader, &header) else {
7037            return Ok(None);
7038        };
7039        let dict = crate::DictionaryBuilder::new().build();
7040        Ok(Some(SummaryView {
7041            round: 0,
7042            summary: Vec::new(),
7043            class_hierarchy: s.class_hierarchy,
7044            level_rollups: s.level_rollups,
7045            level_links: s.level_links,
7046            descriptors: Vec::new(),
7047            subclass_cycles: s.subclass_cycles,
7048            disjoint_pairs: s.disjoint_pairs,
7049            equivalent_pairs: s.equivalent_pairs,
7050            dict,
7051        }))
7052    }
7053
7054    /// Number of semantic-zoom levels in the schema pyramid (0 if none shipped).
7055    pub fn level_count(&self) -> usize {
7056        self.level_rollups.len()
7057    }
7058
7059    /// The type rollup at semantic level `k` (0 = coarsest/most abstract), or
7060    /// `None` if `k` is out of range. Index-free — answered from the pyramid-meta.
7061    pub fn level_rollup(&self, k: usize) -> Option<&LevelRollup> {
7062        self.level_rollups.get(k)
7063    }
7064
7065    /// Resolve a predicate ID in the summary to its term.
7066    pub fn predicate_term(&self, id: u32) -> Option<String> {
7067        self.dict.predicate_term(id)
7068    }
7069
7070    /// Exact number of triples using `predicate`, summed from the summary's
7071    /// superedge counts — answered without ever reading the triple index.
7072    pub fn predicate_total(&self, predicate: &str) -> u32 {
7073        match self.dict.predicate_id(predicate) {
7074            Some(pid) => self
7075                .summary
7076                .iter()
7077                .filter(|e| e.predicate == pid)
7078                .map(|e| e.count)
7079                .sum(),
7080            None => 0,
7081        }
7082    }
7083
7084    /// All predicates with their exact triple totals, descending by count.
7085    pub fn predicate_totals(&self) -> Vec<(String, u32)> {
7086        let mut by_pred: std::collections::BTreeMap<u32, u32> = std::collections::BTreeMap::new();
7087        for e in &self.summary {
7088            *by_pred.entry(e.predicate).or_default() += e.count;
7089        }
7090        let mut out: Vec<(String, u32)> = by_pred
7091            .into_iter()
7092            .filter_map(|(pid, c)| self.dict.predicate_term(pid).map(|t| (t, c)))
7093            .collect();
7094        out.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
7095        out
7096    }
7097
7098    /// Number of communities the summary spans (distinct supernode endpoints).
7099    pub fn community_count(&self) -> usize {
7100        let mut comms = std::collections::BTreeSet::new();
7101        for e in &self.summary {
7102            comms.insert(e.s_comm);
7103            comms.insert(e.o_comm);
7104        }
7105        comms.len()
7106    }
7107
7108    /// **Index-free T-Box coherence (Tier-0).** Detect schema-level incoherent
7109    /// points purely from the shipped schema pyramid — no triple index, no
7110    /// instance data, O(ontology) regardless of graph size:
7111    /// - `subclass-cycle`: a set of classes that are mutually `rdfs:subClassOf`.
7112    /// - `unsatisfiable-class`: a class whose ancestor closure (over all parents,
7113    ///   folded through `owl:equivalentClass`) contains both ends of an
7114    ///   `owl:disjointWith` pair, so no individual can ever be one.
7115    ///
7116    /// Soundness is bounded by what the pyramid ships: the `subClassOf` hierarchy
7117    /// is capped (`MAX_HIERARCHY` in `schema_pyramid`), so on a very large ontology
7118    /// a pruned ancestor can hide an unsatisfiable class (a false *coherent*, never
7119    /// a false *incoherent*). Instance-level clashes (a node typed into disjoint
7120    /// classes, functional-property clashes) are NOT visible here — they need the
7121    /// A-Box (Tier-1/Tier-2 `reason`).
7122    pub fn tbox_coherence(&self) -> Vec<crate::reason::Inconsistency> {
7123        schema_coherence(
7124            &self.class_hierarchy,
7125            &self.subclass_cycles,
7126            &self.disjoint_pairs,
7127            &self.equivalent_pairs,
7128        )
7129    }
7130
7131    /// True when [`tbox_coherence`](Self::tbox_coherence) finds no schema-level
7132    /// incoherent point.
7133    pub fn tbox_is_coherent(&self) -> bool {
7134        self.tbox_coherence().is_empty()
7135    }
7136}
7137
7138/// Compute T-Box coherence points from the schema-pyramid fields alone — no
7139/// dictionary, no index, no instance data. Shared by [`SummaryView::tbox_coherence`]
7140/// and the dictionary-free [`read_schema_coherence_ranged`]. Emits `subclass-cycle`
7141/// and `unsatisfiable-class` (a class whose ancestor closure — over all parents,
7142/// folded through `owl:equivalentClass` — contains both ends of a disjoint pair).
7143pub fn schema_coherence(
7144    class_hierarchy: &[ClassNode],
7145    subclass_cycles: &[Vec<String>],
7146    disjoint_pairs: &[(String, String)],
7147    equivalent_pairs: &[(String, String)],
7148) -> Vec<crate::reason::Inconsistency> {
7149    use crate::reason::Inconsistency;
7150    use std::collections::{BTreeMap, BTreeSet, VecDeque};
7151    const MAX_REACH: usize = 100_000;
7152
7153    let mut out: Vec<Inconsistency> = Vec::new();
7154
7155    for cyc in subclass_cycles {
7156        let detail = if cyc.len() == 1 {
7157            format!("{} is rdfs:subClassOf itself (a cycle)", cyc[0])
7158        } else {
7159            format!(
7160                "classes {{{}}} are mutually rdfs:subClassOf (a cycle)",
7161                cyc.join(", ")
7162            )
7163        };
7164        out.push(Inconsistency {
7165            kind: "subclass-cycle",
7166            detail,
7167        });
7168    }
7169
7170    if !disjoint_pairs.is_empty() {
7171        // Upward adjacency: subClassOf parents + bidirectional equivalence.
7172        let mut adj: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
7173        for n in class_hierarchy {
7174            let e = adj.entry(n.class.as_str()).or_default();
7175            for p in &n.parents {
7176                e.push(p.as_str());
7177            }
7178        }
7179        for (a, b) in equivalent_pairs {
7180            adj.entry(a.as_str()).or_default().push(b.as_str());
7181            adj.entry(b.as_str()).or_default().push(a.as_str());
7182        }
7183
7184        // Candidate focus classes: every class named anywhere in the schema.
7185        let mut focuses: BTreeSet<&str> =
7186            class_hierarchy.iter().map(|n| n.class.as_str()).collect();
7187        for (a, b) in disjoint_pairs.iter().chain(equivalent_pairs) {
7188            focuses.insert(a.as_str());
7189            focuses.insert(b.as_str());
7190        }
7191
7192        let mut seen: BTreeSet<&str> = BTreeSet::new();
7193        for &c in &focuses {
7194            // reach(c) = {c} ∪ ancestors (capped BFS over `adj`).
7195            let mut reach: BTreeSet<&str> = BTreeSet::new();
7196            let mut q: VecDeque<&str> = VecDeque::new();
7197            reach.insert(c);
7198            q.push_back(c);
7199            while let Some(x) = q.pop_front() {
7200                if reach.len() > MAX_REACH {
7201                    break;
7202                }
7203                if let Some(ns) = adj.get(x) {
7204                    for &p in ns {
7205                        if reach.insert(p) {
7206                            q.push_back(p);
7207                        }
7208                    }
7209                }
7210            }
7211            for (x, y) in disjoint_pairs {
7212                if reach.contains(x.as_str()) && reach.contains(y.as_str()) && seen.insert(c) {
7213                    out.push(Inconsistency {
7214                        kind: "unsatisfiable-class",
7215                        detail: format!(
7216                            "{c} is a subclass of both {x} and {y}, which are \
7217                             owl:disjointWith — no individual can be a {c}"
7218                        ),
7219                    });
7220                    break;
7221                }
7222            }
7223        }
7224    }
7225
7226    out.sort_by(|a, b| (a.kind, &a.detail).cmp(&(b.kind, &b.detail)));
7227    out
7228}
7229
7230/// **Dictionary-free Tier-0 coherence read.** Fetch only the header and the
7231/// pyramid-meta range (2 small range reads) and run [`schema_coherence`] over the
7232/// schema pyramid — never touching the **dictionary** (which a literal-heavy file
7233/// makes large) or the triple index. `Ok(None)` if the file ships no pyramid.
7234///
7235/// This is what makes the Tier-0 check cheap on big graphs: the schema pyramid
7236/// carries its own class-string table, so coherence needs none of the dictionary.
7237pub fn read_schema_coherence_ranged<R: RangeReader>(
7238    reader: &R,
7239) -> Result<Option<Vec<crate::reason::Inconsistency>>, FileError> {
7240    let head = reader.read_at(0, HEADER_LEN as u64)?;
7241    let header = Header::from_bytes(&head)?;
7242    // The SCHEMA section (kind 9) first: one range, and no pyramid needed.
7243    if let Some(s) = read_schema_with_header(reader, &header) {
7244        return Ok(Some(schema_coherence(
7245            &s.class_hierarchy,
7246            &s.subclass_cycles,
7247            &s.disjoint_pairs,
7248            &s.equivalent_pairs,
7249        )));
7250    }
7251    if header.pyramid_meta_len == 0 {
7252        return Ok(None);
7253    }
7254    // Fast path: the header records the trailing schema block's length, so read ONLY
7255    // that block (at the end of pyramid-meta) — never the community summary, the
7256    // dictionary, or the index. This is what makes Tier-0 flat at any graph size.
7257    if header.schema_meta_len > 0 && (header.schema_meta_len as u64) <= header.pyramid_meta_len {
7258        let off =
7259            header.pyramid_meta_offset + header.pyramid_meta_len - header.schema_meta_len as u64;
7260        let block = reader.read_at(off, header.schema_meta_len as u64)?;
7261        let (hierarchy, cycles, disjoint, equivalent) = crate::meta::decode_schema_block(&block)
7262            .map_err(|_| FileError::Container("malformed schema block"))?;
7263        return Ok(Some(schema_coherence(
7264            &hierarchy,
7265            &cycles,
7266            &disjoint,
7267            &equivalent,
7268        )));
7269    }
7270    // Fallback (pre-v0.2.1 files with no header field): decode the whole pyramid-meta.
7271    let mb = if crate::v06::reads(header.version) {
7272        crate::v06::open::read_pyramid(reader)?.unwrap_or_default()
7273    } else {
7274        reader.read_at(header.pyramid_meta_offset, header.pyramid_meta_len)?
7275    };
7276    let meta =
7277        PyramidMeta::decode(&mb).map_err(|_| FileError::Container("malformed pyramid meta"))?;
7278    Ok(Some(schema_coherence(
7279        &meta.class_hierarchy,
7280        &meta.subclass_cycles,
7281        &meta.disjoint_pairs,
7282        &meta.equivalent_pairs,
7283    )))
7284}
7285
7286/// The **schema summary** (per-class histogram + class relations at the finest
7287/// level) read over a [`RangeReader`] from the schema pyramid alone — the
7288/// index-free, range-readable source for a Schema view of a remote graph. Returns
7289/// `(classes, relations)` with `classes = [(class_iri, count)]` and `relations =
7290/// [(s_class, predicate, o_class, count)]`; `None` when the file has no schema
7291/// pyramid. Like [`read_schema_coherence_ranged`], it reads only the trailing
7292/// schema block, so it stays flat at any graph size.
7293#[allow(clippy::type_complexity)]
7294pub fn read_schema_summary_ranged<R: RangeReader>(
7295    reader: &R,
7296) -> Result<Option<(Vec<(String, u64)>, Vec<(String, String, String, u64)>)>, FileError> {
7297    let head = reader.read_at(0, HEADER_LEN as u64)?;
7298    let header = Header::from_bytes(&head)?;
7299    // The SCHEMA section (kind 9) first: one range, and no pyramid needed.
7300    let meta = if let Some(s) = read_schema_with_header(reader, &header) {
7301        PyramidMeta::new(0, Vec::new(), &[]).with_schema(
7302            Vec::new(),
7303            s.level_rollups,
7304            s.level_links,
7305            Vec::new(),
7306            Vec::new(),
7307            Vec::new(),
7308            Vec::new(),
7309        )
7310    } else {
7311        if header.pyramid_meta_len == 0 {
7312            return Ok(None);
7313        }
7314        if header.schema_meta_len > 0 && (header.schema_meta_len as u64) <= header.pyramid_meta_len
7315        {
7316            let off = header.pyramid_meta_offset + header.pyramid_meta_len
7317                - header.schema_meta_len as u64;
7318            let block = reader.read_at(off, header.schema_meta_len as u64)?;
7319            let summary = crate::meta::decode_schema_block_summary(&block)
7320                .map_err(|_| FileError::Container("malformed schema block"))?;
7321            return Ok(Some(summary));
7322        }
7323        // Fallback (pre-v0.2.1 files): decode the whole pyramid-meta, pull finest levels.
7324        let mb = if crate::v06::reads(header.version) {
7325            crate::v06::open::read_pyramid(reader)?.unwrap_or_default()
7326        } else {
7327            reader.read_at(header.pyramid_meta_offset, header.pyramid_meta_len)?
7328        };
7329        PyramidMeta::decode(&mb).map_err(|_| FileError::Container("malformed pyramid meta"))?
7330    };
7331    if meta.level_rollups.is_empty() && meta.level_links.is_empty() {
7332        return Ok(None);
7333    }
7334    let classes = meta
7335        .level_rollups
7336        .iter()
7337        .max_by_key(|r| r.depth)
7338        .map(|r| r.classes.clone())
7339        .unwrap_or_default();
7340    let relations = meta
7341        .level_links
7342        .iter()
7343        .max_by_key(|l| l.depth)
7344        .map(|l| {
7345            l.links
7346                .iter()
7347                .map(|c| {
7348                    (
7349                        c.s_class.clone(),
7350                        c.predicate.clone(),
7351                        c.o_class.clone(),
7352                        c.count,
7353                    )
7354                })
7355                .collect()
7356        })
7357        .unwrap_or_default();
7358    Ok(Some((classes, relations)))
7359}
7360
7361/// Every dictionary section's chunk **routing keys**, exactly as stored, from a
7362/// finished file image. Test-only: it exists so a test in another module of
7363/// this crate (`extbuild`) can assert on the directory the writers produce
7364/// without any of this becoming public surface.
7365#[cfg(test)]
7366pub(crate) fn dict_chunk_keys_for_test(image: &[u8]) -> Vec<Vec<Vec<u8>>> {
7367    if crate::v06::open::is_v06(image) {
7368        // 0x06: the routing keys sit in the leaf directories of the dictionary.
7369        use crate::v06::{decode_chunk_leaf, decode_dict_root, kind, open, tag};
7370        let r = crate::reader::SliceReader::new(image);
7371        let (h, root) = open::read_front(&r).unwrap();
7372        let roots = decode_dict_root(root.unit(tag::DICT).unwrap()).unwrap();
7373        let leaves = open::read_section(&r, &h, &root, kind::DICT_LEAVES)
7374            .unwrap()
7375            .unwrap();
7376        return roots
7377            .iter()
7378            .map(|s| {
7379                s.leaves
7380                    .iter()
7381                    .flat_map(|(l, _, _, _)| {
7382                        let stored = &leaves[l.off as usize..(l.off + l.len) as usize];
7383                        let raw = decompress(h.internal_codec, stored).unwrap();
7384                        decode_chunk_leaf(&raw, l.n).unwrap()
7385                    })
7386                    .map(|c| c.key)
7387                    .collect()
7388            })
7389            .collect();
7390    }
7391    let header = Header::from_bytes(&image[..HEADER_LEN]).unwrap();
7392    let s = header.dictionary_offset as usize;
7393    let e = s + header.dictionary_len as usize;
7394    decode_container(&image[s..e], CODEC_NONE)
7395        .unwrap()
7396        .iter()
7397        .map(|payload| {
7398            let (_meta, entries) = parse_chunked_dict_dir(payload, payload.len() as u64).unwrap();
7399            entries.into_iter().map(|c| c.key).collect()
7400        })
7401        .collect()
7402}
7403
7404#[cfg(test)]
7405mod tests {
7406    use super::*;
7407    use super::{
7408        write_dataset_v05 as write_dataset,
7409        write_dataset_with_metadata_v05 as write_dataset_with_metadata,
7410        write_file_v05 as write_file,
7411    };
7412    use crate::dictionary::DictionaryBuilder;
7413    use crate::index::GraphIndexBuilder;
7414
7415    /// The pure-Rust decode path (wasm, `--no-default-features`) reads a zstd
7416    /// frame back byte for byte. Guards the `ruzstd` API across upgrades.
7417    #[cfg(not(feature = "compression"))]
7418    #[test]
7419    fn ruzstd_decodes_a_zstd_frame() {
7420        let data: Vec<u8> = (0..50_000u32)
7421            .flat_map(|i| (i % 251).to_le_bytes())
7422            .collect();
7423        let frame = ruzstd::encoding::compress_to_vec(
7424            &data[..],
7425            ruzstd::encoding::CompressionLevel::Fastest,
7426        );
7427        assert!(frame.len() < data.len());
7428        assert_eq!(decompress(CODEC_ZSTD, &frame).unwrap(), data);
7429    }
7430
7431    /// Bounded decompression: a frame whose header declares more than the
7432    /// bound is refused before decoding; one that states no size is decoded at
7433    /// most one byte past the bound; one that lies about its size fails; and
7434    /// the size a header states is read without decoding.
7435    #[cfg(feature = "compression")]
7436    #[test]
7437    fn decompression_is_bounded() {
7438        let data: Vec<u8> = (0..100_000u32).map(|i| (i % 251) as u8).collect();
7439        let sized = zstd::bulk::compress(&data, 3).unwrap();
7440        assert_eq!(zstd_frame_content_size(&sized), Some(100_000));
7441        assert_eq!(block_raw_len(CODEC_ZSTD, &sized, 1 << 20).unwrap(), 100_000);
7442        assert_eq!(
7443            decompress_bounded(CODEC_ZSTD, &sized, 100_000).unwrap(),
7444            data
7445        );
7446        assert!(decompress_bounded(CODEC_ZSTD, &sized, 99_999).is_err());
7447        // A streamed frame states no size: bounded by decoding.
7448        let stream = zstd::encode_all(&data[..], 3).unwrap();
7449        assert_eq!(zstd_frame_content_size(&stream), None);
7450        assert_eq!(
7451            block_raw_len(CODEC_ZSTD, &stream, 1 << 20).unwrap(),
7452            100_000
7453        );
7454        assert_eq!(
7455            decompress_bounded(CODEC_ZSTD, &stream, 100_000).unwrap(),
7456            data
7457        );
7458        assert!(decompress_bounded(CODEC_ZSTD, &stream, 50_000).is_err());
7459        // A header that under-states its content fails instead of growing.
7460        let mut liar = sized.clone();
7461        let fhd = liar[4];
7462        let fcs_at = 5 + usize::from(fhd & 0x20 == 0);
7463        liar[fcs_at] = liar[fcs_at].wrapping_sub(1);
7464        assert!(decompress_bounded(CODEC_ZSTD, &liar, 200_000).is_err());
7465        // Uncompressed blocks: the length is the size.
7466        assert!(decompress_bounded(CODEC_NONE, &data, 99_999).is_err());
7467        assert_eq!(
7468            decompress_bounded(CODEC_NONE, &data, 100_000).unwrap(),
7469            data
7470        );
7471        assert_eq!(zstd_frame_content_size(b"RETE"), None);
7472    }
7473
7474    /// A three-permutation file must survive every opener — resident, ranged,
7475    /// ranged-lazy — and answer every pattern shape with the rows the
7476    /// six-permutation twin gives, including through a named graph's own
7477    /// container. The mask is a file-level fact, so getting it to the named
7478    /// graphs' decoder is a separate thing that can be forgotten.
7479    #[test]
7480    fn three_permutation_file_round_trips_every_open_path() {
7481        use crate::index::PermSet;
7482        use crate::reader::SliceReader;
7483
7484        let build = |perms: PermSet| {
7485            let mut db = crate::DictionaryBuilder::new();
7486            let mut triples = Vec::new();
7487            for i in 0..60u32 {
7488                let (s, p, o) = (
7489                    format!("<http://ex/s{}>", i % 11),
7490                    format!("<http://ex/p{}>", i % 3),
7491                    format!("<http://ex/o{}>", i % 7),
7492                );
7493                db.observe(&s, &p, &o);
7494                triples.push((s, p, o));
7495            }
7496            let dict = db.build();
7497            let ids: Vec<(u32, u32, u32)> = triples
7498                .iter()
7499                .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
7500                .collect();
7501            let def = GraphIndexBuilder::from_triples(ids.clone())
7502                .with_tile_budget(96)
7503                .with_perms(perms)
7504                .build();
7505            let named = GraphIndexBuilder::from_triples(ids)
7506                .with_tile_budget(96)
7507                .with_perms(perms)
7508                .build();
7509            write_dataset(
7510                &dict,
7511                &def,
7512                &[("<http://ex/g>".to_string(), named)],
7513                true,
7514                &[],
7515                0,
7516            )
7517        };
7518
7519        let six = build(PermSet::ALL);
7520        let three = build(PermSet::CORE);
7521        assert!(three.len() < six.len());
7522        assert_eq!(Header::from_bytes(&three).unwrap().perms, PermSet::CORE);
7523        assert_eq!(Header::from_bytes(&six).unwrap().perms, PermSet::ALL);
7524
7525        let shapes: Vec<(Option<&str>, Option<&str>, Option<&str>)> = vec![
7526            (None, None, None),
7527            (Some("<http://ex/s3>"), None, None),
7528            (None, Some("<http://ex/p1>"), None),
7529            (None, None, Some("<http://ex/o5>")),
7530            (Some("<http://ex/s3>"), Some("<http://ex/p1>"), None),
7531            (Some("<http://ex/s3>"), None, Some("<http://ex/o5>")),
7532            (None, Some("<http://ex/p1>"), Some("<http://ex/o5>")),
7533            (
7534                Some("<http://ex/s3>"),
7535                Some("<http://ex/p1>"),
7536                Some("<http://ex/o5>"),
7537            ),
7538        ];
7539
7540        for (image, tag) in [(&six, "six"), (&three, "three")] {
7541            let resident = Rete::open(image).unwrap();
7542            let leaked: &'static [u8] = Box::leak(image.clone().into_boxed_slice());
7543            let ranged = Rete::open_ranged(&SliceReader::new(leaked)).unwrap();
7544            let lazy =
7545                Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64).unwrap();
7546            for (s, p, o) in &shapes {
7547                let want: Vec<_> = {
7548                    let mut v = Rete::open(&six).unwrap().query(*s, *p, *o);
7549                    v.sort();
7550                    v
7551                };
7552                for (got, path) in [
7553                    (resident.query(*s, *p, *o), "resident"),
7554                    (ranged.query(*s, *p, *o), "ranged"),
7555                    (lazy.query(*s, *p, *o), "lazy"),
7556                ] {
7557                    let mut got = got;
7558                    got.sort();
7559                    assert_eq!(got, want, "{tag}/{path} disagreed on {:?}", (s, p, o));
7560                }
7561                // The named graph's container carries the same mask.
7562                let mut g = resident.query_in_graph(Some("<http://ex/g>"), *s, *p, *o);
7563                g.sort();
7564                assert_eq!(g, want, "{tag}/named disagreed on {:?}", (s, p, o));
7565            }
7566        }
7567
7568        // And the single-pattern ROUTED read (the `query-url` path) locates its
7569        // section by container position, not by the format's fixed six-wide
7570        // slot — the one place the two indexes differ.
7571        let leaked: &'static [u8] = Box::leak(three.clone().into_boxed_slice());
7572        let reader = SliceReader::new(leaked);
7573        let routed = Rete::query_ranged(&reader, None, None, Some("<http://ex/o5>")).unwrap();
7574        let mut want = Rete::open(&six)
7575            .unwrap()
7576            .query(None, None, Some("<http://ex/o5>"));
7577        want.sort();
7578        let mut got = routed;
7579        got.sort();
7580        assert_eq!(got, want, "routed single-pattern read on a lean file");
7581    }
7582
7583    #[test]
7584    fn read_coalesced_merges_within_gap_and_splits_beyond() {
7585        use crate::reader::{CountingReader, SliceReader};
7586        let bytes = vec![0u8; 4096];
7587        // Three 16-byte ranges: A..B gap = 32, B..C gap = 1024.
7588        let ranges = [
7589            ByteRange { offset: 0, len: 16 },
7590            ByteRange {
7591                offset: 48,
7592                len: 16,
7593            },
7594            ByteRange {
7595                offset: 1088,
7596                len: 16,
7597            },
7598        ];
7599        // Tight gap (16): nothing merges → one read per range.
7600        let r = CountingReader::new(SliceReader::new(&bytes));
7601        let out = read_coalesced(&r, &ranges, 16).unwrap();
7602        assert_eq!(out.len(), 3);
7603        assert_eq!(r.requests(), 3);
7604        // Gap 64 merges A+B (gap 32) but not C (gap 1024) → two reads.
7605        let r = CountingReader::new(SliceReader::new(&bytes));
7606        read_coalesced(&r, &ranges, 64).unwrap();
7607        assert_eq!(r.requests(), 2);
7608        // Gap 4096 merges all three into one read, over-fetching the gaps.
7609        let r = CountingReader::new(SliceReader::new(&bytes));
7610        read_coalesced(&r, &ranges, 4096).unwrap();
7611        assert_eq!(r.requests(), 1);
7612    }
7613
7614    fn build_image() -> Vec<u8> {
7615        let triples = [
7616            ("Alice", "knows", "Bob"),
7617            ("Bob", "knows", "Carol"),
7618            ("Alice", "age", "30"),
7619        ];
7620        let mut db = DictionaryBuilder::new();
7621        for (s, p, o) in triples {
7622            db.observe(s, p, o);
7623        }
7624        let dict = db.build();
7625
7626        let mut ib = GraphIndexBuilder::new();
7627        for (s, p, o) in triples {
7628            ib.push(dict.encode(s, p, o).unwrap());
7629        }
7630        let index = ib.build();
7631
7632        let (meta, levels) = build_pyramid_meta(&dict, &triples_ids(&dict), DEFAULT_TILE_BUDGET);
7633        write_file(&dict, &index, false, &meta, levels)
7634    }
7635
7636    fn triples_ids(dict: &Dictionary) -> Vec<(u32, u32, u32)> {
7637        [
7638            ("Alice", "knows", "Bob"),
7639            ("Bob", "knows", "Carol"),
7640            ("Alice", "age", "30"),
7641        ]
7642        .iter()
7643        .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
7644        .collect()
7645    }
7646
7647    #[test]
7648    fn file_round_trips_header_and_counts() {
7649        let bytes = build_image();
7650        let rete = Rete::open(&bytes).unwrap();
7651        assert_eq!(rete.header().quad_count, 3);
7652        assert!(rete.header().term_count >= 5);
7653        let expected_codec = writer_codec();
7654        assert_eq!(rete.header().dict_codec, expected_codec);
7655        assert_eq!(rete.header().block_codec, expected_codec);
7656        assert_eq!(&bytes[bytes.len() - 4..], &MAGIC); // footer marker
7657    }
7658
7659    /// A file whose index was built with a tiny tile budget (forcing many
7660    /// tiles per permutation) must round-trip through write/open and answer
7661    /// every query shape identically — through both the in-memory and the
7662    /// routed ranged read paths.
7663    #[test]
7664    fn multi_tile_file_round_trips_and_routes() {
7665        let triples: Vec<(String, String, String)> = (0..200)
7666            .map(|i| {
7667                (
7668                    format!("<http://ex/s/{i}>"),
7669                    format!("<http://ex/p/{}>", i % 5),
7670                    format!("<http://ex/o/{}>", i % 23),
7671                )
7672            })
7673            .collect();
7674        let mut db = DictionaryBuilder::new();
7675        for (s, p, o) in &triples {
7676            db.observe(s, p, o);
7677        }
7678        let dict = db.build();
7679        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
7680        for (s, p, o) in &triples {
7681            ib.push(dict.encode(s, p, o).unwrap());
7682        }
7683        let index = ib.build();
7684        assert!(
7685            index.tile_sections()[0].len() > 3,
7686            "tiny budget must force many tiles"
7687        );
7688        let bytes = write_file(&dict, &index, false, &[], 0);
7689
7690        let rete = Rete::open(&bytes).unwrap();
7691        assert_eq!(rete.header().version, crate::header::LEGACY_FORMAT_VERSION);
7692        assert_eq!(rete.query(None, None, None).len(), 200);
7693        assert_eq!(rete.query(Some("<http://ex/s/7>"), None, None).len(), 1);
7694        assert_eq!(
7695            rete.query(None, Some("<http://ex/p/3>"), None).len(),
7696            40,
7697            "predicate extent spans tiles"
7698        );
7699        assert_eq!(
7700            rete.query(None, None, Some("<http://ex/o/22>")).len(),
7701            8 // 22, 45, 68, ... < 200
7702        );
7703
7704        // Routed ranged read must agree (and only decompress matching tiles).
7705        use crate::reader::SliceReader;
7706        let reader = SliceReader::new(&bytes);
7707        let routed = Rete::query_ranged(&reader, Some("<http://ex/s/7>"), None, None).unwrap();
7708        assert_eq!(routed.len(), 1);
7709        let routed = Rete::query_ranged(&reader, None, Some("<http://ex/p/3>"), None).unwrap();
7710        assert_eq!(routed.len(), 40);
7711        let routed = Rete::query_ranged(&reader, None, None, Some("<http://ex/o/22>")).unwrap();
7712        assert_eq!(routed.len(), 8);
7713    }
7714
7715    /// The tile-synopsis trailer round-trips through encode/parse, and each parsed
7716    /// synopsis is **exactly** the tile block's own b/c zone — so the directory
7717    /// can never prune a tile the tile itself would have matched.
7718    /// Section-internal byte offsets are u64: a directory whose tiles sit past
7719    /// 4 GiB must parse with exact offsets on EVERY platform. On wasm32 (32-bit
7720    /// usize) the old parse truncated a >4 GiB section length and rejected the
7721    /// tail ("dict chunk overruns section" on the first >4 GiB dictionary —
7722    /// crossref's 5.2 GB g.obj — the playground regression this guards).
7723    #[test]
7724    fn tile_directory_offsets_survive_past_4gib() {
7725        let mut dir = Vec::new();
7726        write_uvarint(&mut dir, 2); // two tiles
7727        write_uvarint(&mut dir, 5); // tile 1: Δmin_a
7728        write_uvarint(&mut dir, 0); //         span
7729        write_uvarint(&mut dir, 3 << 30); //   len = 3 GiB
7730        write_uvarint(&mut dir, 1); // tile 2: Δmin_a
7731        write_uvarint(&mut dir, 0);
7732        write_uvarint(&mut dir, 2 << 30); //   len = 2 GiB
7733        let total = dir.len() as u64 + (3u64 << 30) + (2u64 << 30) + 64;
7734        let entries = parse_tile_directory(&dir, total).unwrap();
7735        assert_eq!(entries.len(), 2);
7736        assert_eq!(entries[1].start, dir.len() as u64 + (3u64 << 30));
7737        assert!(
7738            entries[1].end > u32::MAX as u64,
7739            "tail tile sits past 4 GiB"
7740        );
7741        // a total smaller than the tiles must still reject the directory
7742        assert!(parse_tile_directory(&dir, 1 << 20).is_err());
7743    }
7744
7745    #[test]
7746    fn tile_synopsis_trailer_round_trips() {
7747        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
7748        for i in 0..200u32 {
7749            ib.push((i, i % 7, i % 13));
7750        }
7751        let index = ib.build();
7752        let tile_count = index.tile_sections()[0].len();
7753        assert!(tile_count > 3, "tiny budget forces many tiles");
7754
7755        let payload = encode_tiled_section(&index, 0, CODEC_NONE);
7756        let dir = parse_tile_directory(&payload, payload.len() as u64).unwrap();
7757        assert_eq!(dir.len(), tile_count);
7758        // The trailer sits past the last tile; the old directory parse stops there.
7759        let trailer_start = dir.iter().map(|e| e.end).max().unwrap();
7760        assert!(
7761            trailer_start < payload.len() as u64,
7762            "a trailer follows the tiles"
7763        );
7764        for e in &dir {
7765            assert!(
7766                e.end <= payload.len() as u64,
7767                "tiles still located within the payload"
7768            );
7769        }
7770        let syn = parse_tile_synopsis(&payload, trailer_start as usize, dir.len()).unwrap();
7771        for (e, (min_b, max_b, min_c, max_c)) in dir.iter().zip(syn) {
7772            let block = decompress(CODEC_NONE, &payload[e.start as usize..e.end as usize]).unwrap();
7773            let z = *crate::triples::TripleBlock::parse(&block).unwrap().zone();
7774            assert_eq!(
7775                (min_b, max_b, min_c, max_c),
7776                (z.min_b, z.max_b, z.min_c, z.max_c),
7777                "synopsis equals the tile's own zone"
7778            );
7779        }
7780    }
7781
7782    /// End-to-end safety: a synopsis-carrying file, opened **lazily** (range
7783    /// reads), must return exactly the brute-force answer for every pattern shape
7784    /// — the synopsis prune may never drop a real match.
7785    #[test]
7786    fn tile_synopsis_lazy_matches_reference_every_shape() {
7787        use crate::reader::{CountingReader, SliceReader};
7788        let triples: Vec<(String, String, String)> = (0..200u32)
7789            .map(|i| {
7790                (
7791                    format!("<http://ex/s/{i:04}>"),
7792                    format!("<http://ex/p/{}>", i % 7),
7793                    format!("<http://ex/o/{:04}>", i % 13),
7794                )
7795            })
7796            .collect();
7797        let mut db = DictionaryBuilder::new();
7798        for (s, p, o) in &triples {
7799            db.observe(s, p, o);
7800        }
7801        let dict = db.build();
7802        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
7803        for (s, p, o) in &triples {
7804            ib.push(dict.encode(s, p, o).unwrap());
7805        }
7806        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
7807
7808        let eager = Rete::open(&bytes).unwrap();
7809        assert!(
7810            eager.header().has_tile_synopsis(),
7811            "new files set the synopsis flag"
7812        );
7813
7814        // `open_ranged_lazy` needs a `'static` reader; leak the image (the test
7815        // process exits straight after).
7816        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
7817        let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
7818        let lazy = Rete::open_ranged_lazy_with(reader, HEADER_LEN as u64).unwrap();
7819
7820        let brute = |s: Option<&str>, p: Option<&str>, o: Option<&str>| {
7821            let mut v: Vec<(String, String, String)> = triples
7822                .iter()
7823                .filter(|(a, b, c)| {
7824                    s.is_none_or(|x| x == a) && p.is_none_or(|x| x == b) && o.is_none_or(|x| x == c)
7825                })
7826                .cloned()
7827                .collect();
7828            v.sort();
7829            v
7830        };
7831        // Existing + absent terms in every position (and unbound) — 4×4×4 shapes.
7832        let sv = [
7833            None,
7834            Some("<http://ex/s/0007>"),
7835            Some("<http://ex/s/0130>"),
7836            Some("<http://ex/s/9999>"),
7837        ];
7838        let pv = [
7839            None,
7840            Some("<http://ex/p/3>"),
7841            Some("<http://ex/p/6>"),
7842            Some("<http://ex/p/999>"),
7843        ];
7844        let ov = [
7845            None,
7846            Some("<http://ex/o/0000>"),
7847            Some("<http://ex/o/0012>"),
7848            Some("<http://ex/o/9999>"),
7849        ];
7850        for &s in &sv {
7851            for &p in &pv {
7852                for &o in &ov {
7853                    let mut e = eager.query(s, p, o);
7854                    e.sort();
7855                    let mut l = lazy.query(s, p, o);
7856                    l.sort();
7857                    let r = brute(s, p, o);
7858                    assert_eq!(e, r, "eager {s:?} {p:?} {o:?}");
7859                    assert_eq!(l, r, "lazy {s:?} {p:?} {o:?} — synopsis over-pruned");
7860                }
7861            }
7862        }
7863        assert!(!lazy.index_incomplete(), "no lazy fetch failed");
7864    }
7865
7866    /// Byte-0 / polyglot experiment: a real `.rete` embedded behind a large HTML
7867    /// shell (so byte 0 is `<`, not `RETE`) still opens and queries LAZILY through
7868    /// an `OffsetReader`, touching only the graph's bytes and never the shell.
7869    #[test]
7870    fn polyglot_offset_reads_lazily() {
7871        use crate::reader::{
7872            detect_polyglot_base, CountingReader, OffsetReader, SliceReader, POLYGLOT_DIGITS,
7873            POLYGLOT_MARKER,
7874        };
7875
7876        // A real .rete image, hidden behind a 50 KB HTML shell that carries the
7877        // polyglot base-offset marker in its first bytes (a browser-ignored
7878        // comment). The .rete begins right after the shell.
7879        let image = build_image();
7880        let mut shell = Vec::new();
7881        shell.extend_from_slice(b"<!DOCTYPE html><html><head><!--");
7882        shell.extend_from_slice(POLYGLOT_MARKER);
7883        let digits_at = shell.len();
7884        shell.extend_from_slice(&[b'0'; POLYGLOT_DIGITS]); // patched below
7885        shell.extend_from_slice(b"--></head><body>a web page</body></html>\n");
7886        shell.resize(50_000, b' '); // >> the .rete, to prove the shell is untouched
7887        let base = shell.len() as u64;
7888        let digits = format!("{base:0width$}", width = POLYGLOT_DIGITS);
7889        shell[digits_at..digits_at + POLYGLOT_DIGITS].copy_from_slice(digits.as_bytes());
7890
7891        let mut poly = shell;
7892        poly.extend_from_slice(&image);
7893
7894        // byte 0 is a web page, not a .rete...
7895        assert_ne!(
7896            &poly[0..4],
7897            b"RETE",
7898            "polyglot must not start with the magic"
7899        );
7900        // ...but the marker in the first header window points at the embedded .rete.
7901        let detected = detect_polyglot_base(&poly[..crate::header::HEADER_LEN]).unwrap();
7902        assert_eq!(detected, base);
7903
7904        // Open it LAZILY through the offset shim and query it.
7905        let leaked: &'static [u8] = Box::leak(poly.into_boxed_slice());
7906        let counting = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
7907        let lazy = Rete::open_ranged_lazy_with(
7908            OffsetReader::new(counting.clone(), detected),
7909            HEADER_LEN as u64,
7910        )
7911        .unwrap();
7912
7913        // The polyglot must answer exactly like the plain .rete it embeds.
7914        let plain = Rete::open(&image).unwrap();
7915        let mut want = plain.query(None, None, None);
7916        want.sort();
7917        let mut got = lazy.query(None, None, None);
7918        got.sort();
7919        assert_eq!(
7920            got, want,
7921            "polyglot lazy query differs from the plain .rete"
7922        );
7923        assert!(!got.is_empty(), "expected some triples");
7924        assert!(!lazy.index_incomplete(), "a lazy fetch failed");
7925
7926        // It was LAZY: it read fewer bytes than the HTML shell alone, i.e. it
7927        // never touched the prefix — only the embedded .rete's own bytes.
7928        let read = counting.bytes_read();
7929        eprintln!(
7930            "polyglot lazy read: HTML shell {base} B + .rete image {} B; \
7931             the query touched only {read} B (never the shell).",
7932            image.len()
7933        );
7934        assert!(
7935            read < base,
7936            "read {read} bytes; the HTML shell alone is {base} — the reader \
7937             touched the prefix instead of only the embedded .rete"
7938        );
7939    }
7940
7941    /// Build a small file whose objects are string literals, **with** a text
7942    /// index, and return `(image, triples)`. Shared by the text-index tests.
7943    #[cfg(test)]
7944    fn build_text_indexed(triples: &[(String, String, String)]) -> Vec<u8> {
7945        let mut db = DictionaryBuilder::new();
7946        for (s, p, o) in triples {
7947            db.observe(s, p, o);
7948        }
7949        let dict = db.build();
7950        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
7951        let mut id_triples: Vec<(u32, u32, u32)> = Vec::with_capacity(triples.len());
7952        for (s, p, o) in triples {
7953            let t = dict.encode(s, p, o).unwrap();
7954            ib.push(t);
7955            id_triples.push(t);
7956        }
7957        let index = ib.build();
7958        let text_index = compute_text_index(&dict, &id_triples);
7959        assert!(
7960            !text_index.is_empty(),
7961            "literals should produce a text index"
7962        );
7963        write_dataset_with_metadata(&dict, &index, &[], false, &[], 0, &[], &text_index)
7964    }
7965
7966    /// A `--text-index` build round-trips: `text_search` returns exactly the
7967    /// subjects whose literals contain the queried word(s), with AND across words
7968    /// and token-prefix — matching a brute-force scan of the literals.
7969    #[test]
7970    fn text_index_eager_matches_brute_force() {
7971        let triples: Vec<(String, String, String)> = vec![
7972            (
7973                "<http://ex/s0>",
7974                "<http://ex/label>",
7975                "\"alpha glucose phosphate\"",
7976            ),
7977            ("<http://ex/s1>", "<http://ex/label>", "\"beta Glucose\""),
7978            ("<http://ex/s2>", "<http://ex/label>", "\"gamma fructose\""),
7979            (
7980                "<http://ex/s3>",
7981                "<http://ex/note>",
7982                "\"einstein relativity\"",
7983            ),
7984            (
7985                "<http://ex/s4>",
7986                "<http://ex/ref>",
7987                "<http://ex/not-a-literal>",
7988            ),
7989        ]
7990        .into_iter()
7991        .map(|(s, p, o)| (s.to_string(), p.to_string(), o.to_string()))
7992        .collect();
7993        let bytes = build_text_indexed(&triples);
7994        let rete = Rete::open(&bytes).unwrap();
7995        assert!(rete.has_text_index());
7996
7997        // Brute-force reference: subjects whose literal objects contain all words.
7998        let brute = |words: &[&str]| -> Vec<String> {
7999            let mut v: Vec<String> = triples
8000                .iter()
8001                .filter(|(_, _, o)| {
8002                    crate::terms::is_literal(o)
8003                        && words.iter().all(|w| {
8004                            let wl = w.to_lowercase();
8005                            crate::terms::literal_lexical(o)
8006                                .unwrap()
8007                                .split(|c: char| !c.is_alphanumeric())
8008                                .any(|t| t.to_lowercase() == wl)
8009                        })
8010                })
8011                .map(|(s, _, _)| s.clone())
8012                .collect();
8013            v.sort();
8014            v.dedup();
8015            v
8016        };
8017
8018        let mut got = rete.text_search(&["glucose"], None, 0);
8019        got.sort();
8020        assert_eq!(got, brute(&["glucose"]), "case-insensitive single word");
8021
8022        // AND across two words: only s0 has both.
8023        let mut got = rete.text_search(&["glucose", "phosphate"], None, 0);
8024        got.sort();
8025        assert_eq!(got, brute(&["glucose", "phosphate"]));
8026
8027        // A word nobody has → empty.
8028        assert!(rete.text_search(&["zzznope"], None, 0).is_empty());
8029
8030        // Token-prefix: "ein…" matches "einstein".
8031        let got = rete.text_search(&[], Some("ein"), 0);
8032        assert_eq!(got, vec!["<http://ex/s3>".to_string()]);
8033
8034        // No text index → empty, has_text_index() false.
8035        let mut db = DictionaryBuilder::new();
8036        for (s, p, o) in &triples {
8037            db.observe(s, p, o);
8038        }
8039        let dict = db.build();
8040        let mut ib = GraphIndexBuilder::new();
8041        for (s, p, o) in &triples {
8042            ib.push(dict.encode(s, p, o).unwrap());
8043        }
8044        let plain = write_dataset(&dict, &ib.build(), &[], false, &[], 0);
8045        let plain_rete = Rete::open(&plain).unwrap();
8046        assert!(!plain_rete.has_text_index());
8047        assert!(plain_rete.text_search(&["glucose"], None, 0).is_empty());
8048    }
8049
8050    /// The lazy/remote path returns the same subjects as the eager path **and**
8051    /// faults only the token table + the queried posting list — never the whole
8052    /// postings blob. A `CountingReader` proves the byte budget stays small.
8053    #[test]
8054    fn text_index_lazy_faults_only_queried_postings() {
8055        use crate::reader::{CountingReader, SliceReader};
8056        // Many subjects so the postings blob is large relative to one posting:
8057        // every subject carries "common", but only a few carry "rare".
8058        let mut triples: Vec<(String, String, String)> = (0..300u32)
8059            .map(|i| {
8060                (
8061                    format!("<http://ex/s/{i:04}>"),
8062                    "<http://ex/label>".to_string(),
8063                    format!("\"common word number {i}\""),
8064                )
8065            })
8066            .collect();
8067        for i in [3u32, 77, 250] {
8068            triples.push((
8069                format!("<http://ex/s/{i:04}>"),
8070                "<http://ex/tag>".to_string(),
8071                "\"raretoken\"".to_string(),
8072            ));
8073        }
8074        let bytes = build_text_indexed(&triples);
8075        let eager = Rete::open(&bytes).unwrap();
8076        let mut want = eager.text_search(&["raretoken"], None, 0);
8077        want.sort();
8078        assert_eq!(want.len(), 3);
8079
8080        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
8081        let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
8082        let lazy = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
8083        // Bytes pulled by the open itself (dict dirs, index dirs, named graphs) —
8084        // the text index is deferred and not touched yet.
8085        let before = reader.bytes_read();
8086        let mut got = lazy.text_search(&["raretoken"], None, 0);
8087        got.sort();
8088        assert_eq!(got, want, "lazy search matches eager");
8089        let pulled = reader.bytes_read() - before;
8090        // The search faulted the token table + the one "raretoken" posting; it must
8091        // be far less than the whole text-index section (300 "common" postings).
8092        let ti_len = eager.header().text_index_len;
8093        assert!(
8094            pulled < ti_len,
8095            "search pulled {pulled} B but the section is {ti_len} B — faulted too much"
8096        );
8097        assert!(!lazy.index_incomplete());
8098    }
8099
8100    /// `SearchView` answers exactly what a full open answers, having read
8101    /// strictly less: it skips the permutation tile directories and — the part
8102    /// that actually costs on a literal-heavy graph — the object-only
8103    /// dictionary's chunk directory.
8104    #[test]
8105    fn search_view_matches_full_open_for_far_fewer_bytes() {
8106        use crate::reader::{CountingReader, SliceReader};
8107        // Long object literals so the object-only dictionary (and its chunk
8108        // directory) dominates the file, as on a graph that stores abstracts.
8109        let filler = "lorem ipsum dolor sit amet consectetur adipiscing elit ".repeat(20);
8110        let mut triples: Vec<(String, String, String)> = (0..300u32)
8111            .map(|i| {
8112                (
8113                    format!("<http://ex/s/{i:04}>"),
8114                    "<http://ex/abstract>".to_string(),
8115                    format!("\"{filler} number {i}\""),
8116                )
8117            })
8118            .collect();
8119        for i in [3u32, 77, 250] {
8120            triples.push((
8121                format!("<http://ex/s/{i:04}>"),
8122                "<http://ex/tag>".to_string(),
8123                "\"raretoken\"".to_string(),
8124            ));
8125        }
8126        let bytes = build_text_indexed(&triples);
8127        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
8128
8129        let full_reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
8130        let full = Rete::open_ranged_lazy_with(full_reader.clone(), HEADER_LEN as u64).unwrap();
8131        let mut want = full.text_search(&["raretoken"], None, 0);
8132        want.sort();
8133        assert_eq!(want.len(), 3);
8134
8135        let search_reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
8136        let view = SearchView::open_ranged_with(search_reader.clone(), HEADER_LEN as u64).unwrap();
8137        assert!(view.has_text_index());
8138        let mut got = view.text_search(&["raretoken"], None, 0);
8139        got.sort();
8140        assert_eq!(got, want, "search view matches the full open");
8141        assert!(
8142            search_reader.bytes_read() < full_reader.bytes_read(),
8143            "search read {} B, full open read {} B — the narrow open bought nothing",
8144            search_reader.bytes_read(),
8145            full_reader.bytes_read()
8146        );
8147
8148        // A file without a text index says so from the header alone, and the
8149        // label-prefix mode still works through the same view.
8150        let mut prefix_hits = view.prefix_search("", 5);
8151        prefix_hits.sort();
8152        assert!(!prefix_hits.is_empty() || !view.has_pyramid());
8153    }
8154
8155    /// Nothing records a dictionary chunk directory's byte length, so a ranged
8156    /// open probes for it. That probe must cost about ONE directory, not several
8157    /// — it used to re-read the whole prefix on every round, so the sum was
8158    /// ~2x the last (already over-sized) read: 537 MB of range reads to fetch
8159    /// epfl-infoscience's 234 MB object-only directory. Here the same probe runs
8160    /// against a file whose object literals are long enough that its directory
8161    /// is well past the 8 KiB header prefix, and must (a) stay inside the
8162    /// append-only bound of 2x the directory and (b) beat what the old
8163    /// re-reading loop would have spent.
8164    #[test]
8165    fn dict_chunk_directory_probe_costs_about_one_directory() {
8166        use crate::reader::{CountingReader, SliceReader};
8167        // Long literals that share a 3 KiB prefix with their neighbour, each
8168        // ending in a 1 KiB tail of its own: the WKT-polygon / JSON-blob shape
8169        // (#198's weak case). The directory stores the SHORTEST SEPARATOR per
8170        // chunk, and a separator has to reproduce the whole shared prefix — so
8171        // these keys are still ~3 KiB and the directory still runs well past
8172        // the 8 KiB header prefix, which is what gives the probe something to
8173        // measure.
8174        //
8175        // This fixture used to put the varying bytes FIRST (`"{i:04} xxxx…"`).
8176        // That shape is the one separators annihilate: its keys collapse to ~4
8177        // bytes, the whole directory to 192 B, it fits the header prefix, and
8178        // the probe never runs — the assertion below then fails naming itself.
8179        // Keep the shared prefix.
8180        let head = "x".repeat(3000);
8181        let triples: Vec<(String, String, String)> = (0..2000u32)
8182            .map(|i| {
8183                let tail = char::from(b'a' + (i % 26) as u8).to_string().repeat(1000);
8184                (
8185                    format!("<http://ex/s/{i:04}>"),
8186                    "<http://ex/abstract>".to_string(),
8187                    format!("\"{head}{i:04}{tail}\""),
8188                )
8189            })
8190            .collect();
8191        let mut db = DictionaryBuilder::new();
8192        for (s, p, o) in &triples {
8193            db.observe(s, p, o);
8194        }
8195        let dict = db.build();
8196        let mut ib = GraphIndexBuilder::new();
8197        for (s, p, o) in &triples {
8198            ib.push(dict.encode(s, p, o).unwrap());
8199        }
8200        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
8201
8202        let reader = CountingReader::new(SliceReader::new(&bytes));
8203        let header = Header::from_bytes(&reader.read_at(0, HEADER_LEN as u64).unwrap()).unwrap();
8204        // Section 2 is object-only: the one carrying the long literals.
8205        let section = locate_container_section_ranged(
8206            &reader,
8207            header.dictionary_offset,
8208            header.dictionary_len,
8209            2,
8210            4,
8211        )
8212        .unwrap();
8213
8214        let before = reader.bytes_read();
8215        let (_meta, entries) = read_dict_dir_ranged(&reader, section).unwrap();
8216        let spent = reader.bytes_read() - before;
8217
8218        // The first chunk body starts where the directory ends.
8219        let dir_end = entries[0].start;
8220        let head = reader.read_at(section.offset, 64).unwrap();
8221        let (header_len, n0) = read_uvarint(&head).unwrap();
8222        let dir_start = n0 as u64 + header_len;
8223        let dir_len = dir_end - dir_start;
8224        let dir_total = section.len - dir_start;
8225        assert!(
8226            dir_start + dir_len > 8192,
8227            "directory fits the header prefix ({dir_len} B) — the probe never runs"
8228        );
8229
8230        // What the previous loop spent: every round re-read from the start.
8231        let mut old = 0u64;
8232        let mut p = 4096u64.min(dir_total).max(1);
8233        loop {
8234            let got = p.min(dir_total);
8235            old += got;
8236            if got >= dir_len || p >= dir_total {
8237                break;
8238            }
8239            p = p.saturating_mul(2).min(dir_total);
8240        }
8241        assert!(
8242            spent <= dir_len * 2,
8243            "probe read {spent} B for a {dir_len} B directory — past the append-only bound"
8244        );
8245        assert!(
8246            spent < old,
8247            "probe read {spent} B; the re-reading loop it replaced spent {old} B"
8248        );
8249    }
8250
8251    // ---------------------------------------------------------------------
8252    // Separator routing keys (#198 option (d))
8253    // ---------------------------------------------------------------------
8254
8255    /// A verbatim-keyed twin of a chunked dictionary section: same header, same
8256    /// chunking, byte-identical compressed bodies — only every directory key
8257    /// replaced by the chunk's actual first term. That is exactly what
8258    /// `encode_chunked_dict_section` wrote before separator keys, so it is the
8259    /// control for every comparison below (and for the proptest).
8260    fn verbatim_keyed_twin(payload: &[u8], codec: u8) -> Vec<u8> {
8261        let (_meta, entries) = parse_chunked_dict_dir(payload, payload.len() as u64).unwrap();
8262        let (header_len, n0) = read_uvarint(payload).unwrap();
8263        let mut out = Vec::new();
8264        write_uvarint(&mut out, header_len);
8265        out.extend_from_slice(&payload[n0..n0 + header_len as usize]);
8266        write_uvarint(&mut out, entries.len() as u64);
8267        let mut prev_run = 0usize;
8268        for e in &entries {
8269            let body = decompress(codec, &payload[e.start as usize..e.end as usize]).unwrap();
8270            let first = crate::dict::run_first_term(&body, 0).unwrap();
8271            write_uvarint(&mut out, (e.first_run - prev_run) as u64);
8272            write_uvarint(&mut out, first.len() as u64);
8273            out.extend_from_slice(&first);
8274            write_uvarint(&mut out, e.end - e.start);
8275            prev_run = e.first_run;
8276        }
8277        for e in &entries {
8278            out.extend_from_slice(&payload[e.start as usize..e.end as usize]);
8279        }
8280        out
8281    }
8282
8283    /// Each chunk's (first term, last term), decoded from the bodies.
8284    fn chunk_bounds_of(payload: &[u8], codec: u8) -> Vec<(Vec<u8>, Vec<u8>)> {
8285        let (_meta, entries) = parse_chunked_dict_dir(payload, payload.len() as u64).unwrap();
8286        entries
8287            .iter()
8288            .map(|e| {
8289                let body = decompress(codec, &payload[e.start as usize..e.end as usize]).unwrap();
8290                (
8291                    crate::dict::run_first_term(&body, 0).unwrap(),
8292                    crate::dict::run_last_term(&body, 0, body.len()).unwrap(),
8293                )
8294            })
8295            .collect()
8296    }
8297
8298    /// A term set built to hit every shape the corpus has: plain IRIs, literals
8299    /// sharing a long prefix with their neighbour (separators are LONG there —
8300    /// the WKT/`ohm-full` weak case), literals that diverge in their first bytes
8301    /// (separators are tiny — the `epfl-infoscience` case), literals larger than
8302    /// `DICT_CHUNK_BUDGET` on their own, and multibyte UTF-8.
8303    fn adversarial_terms() -> Vec<String> {
8304        let mut terms: Vec<String> = Vec::new();
8305        for i in 0..6_000 {
8306            terms.push(format!("<http://example.org/work/{i:06}>"));
8307        }
8308        let shared = "X".repeat(900);
8309        for i in 0..4_000 {
8310            terms.push(format!("\"{shared}-{i:05}-tail\""));
8311        }
8312        let deep = "Y".repeat(3_000); // shared prefix far past any short key
8313        for i in 0..120 {
8314            terms.push(format!("\"{deep}{i:03}\""));
8315        }
8316        for i in 0..6 {
8317            // one literal larger than the 64 KiB budget: a chunk of one run
8318            terms.push(format!("\"BIG-{i:02}-{}\"", "Z".repeat(90_000)));
8319        }
8320        for i in 0..800 {
8321            terms.push(format!(
8322                "\"h\u{e9}llo-{}-\u{1F600}{i:04}\"",
8323                "\u{e9}".repeat(40)
8324            ));
8325        }
8326        for i in 0..2_000 {
8327            terms.push(format!("\"{i:05}-{}\"", "T".repeat(1_800)));
8328        }
8329        terms.sort();
8330        terms.dedup();
8331        terms
8332    }
8333
8334    fn chunked_section_of(terms: &[String]) -> Vec<u8> {
8335        let mut b = crate::dict::DictSectionBuilder::new().with_restart_interval(16);
8336        for t in terms {
8337            b.push(t.clone());
8338        }
8339        b.build()
8340    }
8341
8342    /// The invariant, asserted against the real chunk contents: the stored key
8343    /// of chunk `i` is strictly above chunk `i-1`'s LAST term and at most chunk
8344    /// `i`'s own first term, chunk 0's key is empty, and the keys ascend — the
8345    /// three facts `ChunkedSection::id`'s `partition_point` relies on.
8346    ///
8347    /// Plus the anti-regression the rename exists for: on a section like this
8348    /// the key must NOT be the first term. A writer that quietly went back to
8349    /// storing terms would pass every other test in this file — dumps and
8350    /// `term(id)` route by `first_run` and never read the key.
8351    #[test]
8352    fn chunk_directory_keys_are_separators_not_terms() {
8353        let terms = adversarial_terms();
8354        let raw = chunked_section_of(&terms);
8355        let payload = encode_chunked_dict_section(&raw, CODEC_NONE);
8356        let (_meta, entries) = parse_chunked_dict_dir(&payload, payload.len() as u64).unwrap();
8357        let bounds = chunk_bounds_of(&payload, CODEC_NONE);
8358        assert!(
8359            entries.len() > 20,
8360            "need a many-chunk section, got {}",
8361            entries.len()
8362        );
8363
8364        assert!(entries[0].key.is_empty(), "chunk 0 needs no separator");
8365        let mut key_bytes = 0usize;
8366        let mut term_bytes = 0usize;
8367        let mut differ = 0usize;
8368        for (i, e) in entries.iter().enumerate() {
8369            key_bytes += e.key.len();
8370            term_bytes += bounds[i].0.len();
8371            if e.key != bounds[i].0 {
8372                differ += 1;
8373            }
8374            assert!(
8375                e.key <= bounds[i].0,
8376                "chunk {i}: key must be <= its own first term"
8377            );
8378            if i > 0 {
8379                assert!(
8380                    e.key.as_slice() > bounds[i - 1].1.as_slice(),
8381                    "chunk {i}: key must be > the previous chunk's last term"
8382                );
8383                assert!(
8384                    e.key > entries[i - 1].key,
8385                    "chunk {i}: keys must ascend for partition_point"
8386                );
8387                // shortest: one byte past the divergence from the predecessor
8388                assert_eq!(
8389                    e.key,
8390                    crate::dict::shortest_separator(&bounds[i - 1].1, &bounds[i].0),
8391                    "chunk {i}: key is not the SHORTEST separator"
8392                );
8393            }
8394        }
8395        assert!(
8396            differ > 0,
8397            "every key equalled its first term — the writer stores terms again"
8398        );
8399        assert!(
8400            key_bytes * 4 < term_bytes,
8401            "separators bought almost nothing: {key_bytes} B vs {term_bytes} B of first terms"
8402        );
8403        eprintln!(
8404            "chunks={} first-term keys={term_bytes} B  separator keys={key_bytes} B ({:.1}x)",
8405            entries.len(),
8406            term_bytes as f64 / key_bytes.max(1) as f64
8407        );
8408    }
8409
8410    /// A separator-keyed section answers **identically** to the verbatim-keyed
8411    /// one built over the same bodies, under the same unmodified reader: every
8412    /// id, every term, and every boundary probe — the first and last term of
8413    /// each chunk, one byte either side of each, strings that fall strictly
8414    /// *between* two chunks, truncations, the empty string, and probes below
8415    /// and above the whole section.
8416    ///
8417    /// Probing `id(term)` is the entire point. A broken key (a truncation, a
8418    /// hash) leaves `term(id)`, `dump` and `export` byte-perfect — they route by
8419    /// `first_run` — and only lookups lie.
8420    #[test]
8421    fn separator_keys_route_identically_to_verbatim_first_terms() {
8422        let terms = adversarial_terms();
8423        let raw = chunked_section_of(&terms);
8424        let sep_payload = encode_chunked_dict_section(&raw, CODEC_NONE);
8425        let ver_payload = verbatim_keyed_twin(&sep_payload, CODEC_NONE);
8426
8427        // Same chunking, byte-identical bodies — only the keys differ.
8428        let (_ms, es) = parse_chunked_dict_dir(&sep_payload, sep_payload.len() as u64).unwrap();
8429        let (_mv, ev) = parse_chunked_dict_dir(&ver_payload, ver_payload.len() as u64).unwrap();
8430        assert_eq!(es.len(), ev.len());
8431        for (a, b) in es.iter().zip(ev.iter()) {
8432            assert_eq!(a.first_run, b.first_run);
8433            assert_eq!(
8434                &sep_payload[a.start as usize..a.end as usize],
8435                &ver_payload[b.start as usize..b.end as usize],
8436                "chunk bodies must be identical"
8437            );
8438        }
8439        assert!(
8440            sep_payload.len() < ver_payload.len(),
8441            "separator payload is not smaller"
8442        );
8443
8444        let sec_s = decode_chunked_dict_section(
8445            &sep_payload,
8446            CODEC_NONE,
8447            crate::chunk_cache::ChunkCache::unlimited_arc(),
8448            0,
8449        )
8450        .unwrap();
8451        let sec_v = decode_chunked_dict_section(
8452            &ver_payload,
8453            CODEC_NONE,
8454            crate::chunk_cache::ChunkCache::unlimited_arc(),
8455            0,
8456        )
8457        .unwrap();
8458
8459        // 1. every present term → the same, correct id
8460        for (i, t) in terms.iter().enumerate() {
8461            let want = Some(i as u32 + 1);
8462            assert_eq!(sec_v.id(t), want, "verbatim id() lost a term");
8463            assert_eq!(sec_s.id(t), want, "separator id() lost a term");
8464        }
8465        // 2. every id → the same term
8466        for i in 0..terms.len() as u32 {
8467            assert_eq!(sec_s.term(i + 1), sec_v.term(i + 1));
8468            assert_eq!(
8469                sec_s.term(i + 1).as_deref(),
8470                Some(terms[i as usize].as_str())
8471            );
8472        }
8473        // 3. boundary probes, from the real chunk bounds
8474        let bounds = chunk_bounds_of(&sep_payload, CODEC_NONE);
8475        let mut probes: Vec<String> = Vec::new();
8476        let push = |bytes: &[u8], probes: &mut Vec<String>| {
8477            if let Ok(s) = std::str::from_utf8(bytes) {
8478                probes.push(s.to_string());
8479            }
8480        };
8481        for (first, last) in &bounds {
8482            for base in [first, last] {
8483                push(base, &mut probes); // present
8484                let mut after = base.clone();
8485                after.push(0x01); // immediately after — between chunks when base = last
8486                push(&after, &mut probes);
8487                let mut far = base.clone();
8488                far.push(b'~');
8489                push(&far, &mut probes);
8490                if !base.is_empty() {
8491                    push(&base[..base.len() - 1], &mut probes); // truncated
8492                    let mut down = base.clone();
8493                    *down.last_mut().unwrap() = down.last().unwrap().wrapping_sub(1);
8494                    push(&down, &mut probes);
8495                    let mut up = base.clone();
8496                    *up.last_mut().unwrap() = up.last().unwrap().wrapping_add(1);
8497                    push(&up, &mut probes);
8498                }
8499            }
8500        }
8501        // 4. the stored separators themselves, and the ends of the world
8502        for e in &es {
8503            push(&e.key, &mut probes);
8504            let mut plus = e.key.clone();
8505            plus.push(0);
8506            push(&plus, &mut probes);
8507        }
8508        probes.push(String::new()); // the empty key, i.e. chunk 0's
8509        probes.push("\u{1}".to_string()); // below every term
8510        probes.push("\u{10FFFF}\u{10FFFF}".to_string()); // above every term
8511        probes.push(terms.first().unwrap().clone());
8512        probes.push(terms.last().unwrap().clone());
8513
8514        let mut hits = 0usize;
8515        for p in &probes {
8516            let (a, b) = (sec_v.id(p), sec_s.id(p));
8517            assert_eq!(
8518                a,
8519                b,
8520                "probe diverged: {:?}",
8521                p.get(..p.len().min(48)).unwrap_or(p.as_str())
8522            );
8523            hits += usize::from(a.is_some());
8524        }
8525        assert!(
8526            probes.len() > 500 && hits > 50,
8527            "probe set is too thin: {} probes, {hits} of them present",
8528            probes.len()
8529        );
8530        eprintln!(
8531            "{} boundary probes, {hits} present, all identical",
8532            probes.len()
8533        );
8534    }
8535
8536    /// The same claim as a **property**: for arbitrary term sets and arbitrary
8537    /// probes, a separator-keyed section and the verbatim-keyed twin built from
8538    /// the same input are indistinguishable through the reader. The fixed test
8539    /// above picks the shapes I thought of; this one shrinks a counterexample
8540    /// out of the ones I did not.
8541    mod separator_props {
8542        use super::*;
8543        use proptest::prelude::*;
8544
8545        /// Sorted, deduplicated term sets mixing the three shapes that decide a
8546        /// separator's length: short IRIs, literals whose payload comes BEFORE
8547        /// the discriminating bytes (long shared prefixes ⇒ long separators),
8548        /// and literals that diverge immediately (⇒ one-byte separators). Sizes
8549        /// straddle `DICT_CHUNK_BUDGET`, so single- and many-chunk sections both
8550        /// occur.
8551        fn term_set() -> impl Strategy<Value = Vec<String>> {
8552            prop::collection::vec(("[a-c]{1,6}", 0u8..3, 300usize..1200), 60..250).prop_map(
8553                |specs| {
8554                    let mut terms: Vec<String> = specs
8555                        .into_iter()
8556                        .enumerate()
8557                        .map(|(i, (core, kind, pad))| {
8558                            let block = "z".repeat(pad);
8559                            match kind {
8560                                0 => format!("<http://ex/{core}/{i:05}>"),
8561                                1 => format!("\"{block}{core}{i:05}\""),
8562                                _ => format!("\"{core}{i:05}{block}\""),
8563                            }
8564                        })
8565                        .collect();
8566                    terms.sort();
8567                    terms.dedup();
8568                    terms
8569                },
8570            )
8571        }
8572
8573        proptest! {
8574            #[test]
8575            fn prop_separator_keyed_section_answers_like_verbatim(
8576                terms in term_set(),
8577                extra in prop::collection::vec(any::<String>(), 0..8),
8578            ) {
8579                let raw = chunked_section_of(&terms);
8580                let sep = encode_chunked_dict_section(&raw, CODEC_NONE);
8581                let ver = verbatim_keyed_twin(&sep, CODEC_NONE);
8582                prop_assert!(sep.len() <= ver.len());
8583
8584                // The separator invariant, against the real chunk contents.
8585                let (_m, es) = parse_chunked_dict_dir(&sep, sep.len() as u64).unwrap();
8586                let bounds = chunk_bounds_of(&sep, CODEC_NONE);
8587                prop_assert!(es[0].key.is_empty());
8588                for i in 1..es.len() {
8589                    prop_assert!(es[i].key.as_slice() > bounds[i - 1].1.as_slice());
8590                    prop_assert!(es[i].key <= bounds[i].0);
8591                    prop_assert!(es[i].key > es[i - 1].key);
8592                }
8593
8594                let sec_s = decode_chunked_dict_section(
8595                    &sep,
8596                    CODEC_NONE,
8597                    crate::chunk_cache::ChunkCache::unlimited_arc(),
8598                    0,
8599                )
8600                .unwrap();
8601                let sec_v = decode_chunked_dict_section(
8602                    &ver,
8603                    CODEC_NONE,
8604                    crate::chunk_cache::ChunkCache::unlimited_arc(),
8605                    0,
8606                )
8607                .unwrap();
8608
8609                for (i, t) in terms.iter().enumerate() {
8610                    prop_assert_eq!(sec_s.id(t), Some(i as u32 + 1));
8611                    prop_assert_eq!(sec_v.id(t), Some(i as u32 + 1));
8612                    prop_assert_eq!(sec_s.term(i as u32 + 1), sec_v.term(i as u32 + 1));
8613                }
8614
8615                // Probes: arbitrary strings, plus mutations of every boundary.
8616                let mut probes: Vec<String> = extra;
8617                probes.push(String::new());
8618                for (first, last) in &bounds {
8619                    for base in [first, last] {
8620                        if let Ok(s) = std::str::from_utf8(base) {
8621                            probes.push(s.to_string());
8622                            probes.push(format!("{s}\u{1}"));
8623                            probes.push(s[..s.len() - s.chars().next_back()
8624                                .map(char::len_utf8).unwrap_or(0)].to_string());
8625                        }
8626                    }
8627                }
8628                for e in &es {
8629                    if let Ok(s) = std::str::from_utf8(&e.key) {
8630                        probes.push(s.to_string());
8631                    }
8632                }
8633                for p in &probes {
8634                    prop_assert_eq!(sec_s.id(p), sec_v.id(p), "probe diverged: {:?}", p);
8635                }
8636            }
8637        }
8638    }
8639
8640    /// The TEXT_INDEX section is inside the content hash: a freshly built
8641    /// text-indexed file must pass `verify()`, and flipping a byte inside the
8642    /// section must break it. (Regression: `verify()` once rebuilt the hash
8643    /// without the text index, so every `--text-index` file failed as corrupt.)
8644    #[test]
8645    fn text_index_is_tamper_evident_and_verifies() {
8646        let triples: Vec<(String, String, String)> = vec![(
8647            "<http://ex/s0>".to_string(),
8648            "<http://ex/label>".to_string(),
8649            "\"alpha glucose phosphate\"".to_string(),
8650        )];
8651        let bytes = build_text_indexed(&triples);
8652        let header = Rete::open(&bytes).unwrap().header().clone();
8653        assert!(header.text_index_len > 0);
8654        assert!(verify(&bytes).unwrap(), "a text-indexed build must verify");
8655
8656        let mut tampered = bytes.clone();
8657        tampered[header.text_index_offset as usize] ^= 0xff;
8658        assert!(
8659            !verify(&tampered).unwrap(),
8660            "tampering with the text index must break verify()"
8661        );
8662    }
8663
8664    /// End-to-end win: on a remote (range-read) file, a lookup whose routed tile
8665    /// is ruled out by a bound secondary fetches **fewer bytes** with the synopsis
8666    /// than without it — and the answer is identical (empty) either way.
8667    #[test]
8668    fn synopsis_cuts_remote_fetch_bytes() {
8669        use crate::header::FLAG_TILE_SYNOPSIS;
8670        use crate::reader::{CountingReader, SliceReader};
8671
8672        // Zero-padded terms ⇒ dictionary ids are monotonic in i; subject s_i pairs
8673        // only with object o_i, so an OSP tile (routed by object) holds a
8674        // contiguous subject range — a subject from a far tile is provably absent.
8675        let triples: Vec<(String, String, String)> = (0..400u32)
8676            .map(|i| {
8677                (
8678                    format!("<http://ex/s/{i:04}>"),
8679                    "<http://ex/p>".to_string(),
8680                    format!("<http://ex/o/{i:04}>"),
8681                )
8682            })
8683            .collect();
8684        let mut db = DictionaryBuilder::new();
8685        for (s, p, o) in &triples {
8686            db.observe(s, p, o);
8687        }
8688        let dict = db.build();
8689        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
8690        for (s, p, o) in &triples {
8691            ib.push(dict.encode(s, p, o).unwrap());
8692        }
8693        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
8694
8695        // (s_0395, ?, o_0005): routes OSP by the (early) object, secondary = the
8696        // (late) subject — outside that tile's subject range, so the synopsis
8697        // prunes the one routed tile.
8698        let q = (Some("<http://ex/s/0395>"), None, Some("<http://ex/o/0005>"));
8699        // Measure the bytes the QUERY pulls (after open) — isolating the per-query
8700        // saving from the one-time synopsis trailer reads done at open, which a
8701        // persistent remote session amortizes over many queries.
8702        let query_bytes = |image: &[u8]| -> (u64, usize) {
8703            let leaked: &'static [u8] = Box::leak(image.to_vec().into_boxed_slice());
8704            let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
8705            let rete = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
8706            let before = reader.bytes_read(); // after open (incl. trailer reads)
8707            let n = rete.query(q.0, q.1, q.2).len();
8708            assert!(!rete.index_incomplete());
8709            (reader.bytes_read() - before, n)
8710        };
8711
8712        let (on_bytes, on_n) = query_bytes(&bytes);
8713        // Same file with the synopsis flag cleared = an older reader's behavior.
8714        let mut off = bytes.clone();
8715        off[5] &= !FLAG_TILE_SYNOPSIS;
8716        let (off_bytes, off_n) = query_bytes(&off);
8717
8718        assert_eq!(on_n, 0, "the pair never co-occurs");
8719        assert_eq!(off_n, 0, "same answer without the synopsis");
8720        // Both pay the same dictionary-resolution bytes; the difference is the one
8721        // routed index tile that the synopsis skips (and the no-synopsis path
8722        // fetches only to have its zone map reject it).
8723        assert!(
8724            on_bytes < off_bytes,
8725            "synopsis skips the routed tile fetch: {on_bytes} < {off_bytes}"
8726        );
8727    }
8728
8729    /// A double-bound-object intersection (`?p P o1 ; P o2 ; label ?l`) — the
8730    /// shape whose REMOTE join strategy changed (scan + hash-join instead of
8731    /// probing each prefix row) — must return the SAME rows opened eagerly (in
8732    /// memory) and lazily (remote-style, `is_remote()` true). Strategy is a
8733    /// performance choice; the result multiset is invariant.
8734    #[test]
8735    fn double_bound_object_join_eager_matches_lazy() {
8736        use crate::reader::SliceReader;
8737        let occ = "<http://ex/occ>";
8738        let phys = "<http://ex/physicist>";
8739        let phil = "<http://ex/philosopher>";
8740        let label = "<http://www.w3.org/2000/01/rdf-schema#label>";
8741        // p00..p19 are physicists; p00..p09 are also philosophers (the answer).
8742        let mut triples: Vec<(String, String, String)> = Vec::new();
8743        for i in 0..20u32 {
8744            triples.push((format!("<http://ex/p/{i:02}>"), occ.into(), phys.into()));
8745            if i < 10 {
8746                triples.push((format!("<http://ex/p/{i:02}>"), occ.into(), phil.into()));
8747            }
8748            triples.push((
8749                format!("<http://ex/p/{i:02}>"),
8750                label.into(),
8751                format!("\"Name {i:02}\""),
8752            ));
8753        }
8754        let mut db = DictionaryBuilder::new();
8755        for (s, p, o) in &triples {
8756            db.observe(s, p, o);
8757        }
8758        let dict = db.build();
8759        let mut ib = GraphIndexBuilder::new().with_tile_budget(16);
8760        for (s, p, o) in &triples {
8761            ib.push(dict.encode(s, p, o).unwrap());
8762        }
8763        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
8764
8765        let q = "SELECT ?l WHERE { \
8766            ?p <http://ex/occ> <http://ex/physicist> ; \
8767               <http://ex/occ> <http://ex/philosopher> ; \
8768               <http://www.w3.org/2000/01/rdf-schema#label> ?l }";
8769        let run = |rete: &Rete| -> Vec<String> {
8770            let (_, sols) = crate::eval_sparql(rete, q).unwrap();
8771            let mut v: Vec<String> = sols.iter().filter_map(|b| b.get("l").cloned()).collect();
8772            v.sort();
8773            v
8774        };
8775
8776        let eager_rows = run(&Rete::open(&bytes).unwrap());
8777        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
8778        let lazy = Rete::open_ranged_lazy_with(
8779            std::sync::Arc::new(SliceReader::new(leaked)),
8780            HEADER_LEN as u64,
8781        )
8782        .unwrap();
8783        let lazy_rows = run(&lazy);
8784        assert!(!lazy.index_incomplete());
8785
8786        assert_eq!(eager_rows.len(), 10, "the 10 physicist∩philosopher labels");
8787        assert_eq!(eager_rows, lazy_rows, "eager and lazy must agree exactly");
8788    }
8789
8790    /// A dictionary big enough to split into multiple chunks per section must
8791    /// round-trip every id↔term mapping through the chunked (v0.2) encoding —
8792    /// including terms at chunk boundaries and absent near-misses.
8793    #[test]
8794    fn multi_chunk_dictionary_round_trips() {
8795        let mut db = DictionaryBuilder::new();
8796        let term = |i: u32| format!("<http://example.org/some/long/prefix/entity/{i:06}>");
8797        for i in 0..6000u32 {
8798            db.observe(&term(i), "<http://ex/p>", &term(i + 1));
8799        }
8800        let dict = db.build();
8801        let mut ib = GraphIndexBuilder::new();
8802        for i in 0..6000u32 {
8803            ib.push(
8804                dict.encode(&term(i), "<http://ex/p>", &term(i + 1))
8805                    .unwrap(),
8806            );
8807        }
8808        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
8809        let rete = Rete::open(&bytes).unwrap();
8810        let d = rete.dictionary();
8811        assert_eq!(d.term_count(), dict.term_count());
8812        for i in (0..6000).step_by(97).chain([0, 1, 5999, 6000]) {
8813            let t = term(i);
8814            let sid = dict.subject_id(&t);
8815            assert_eq!(d.subject_id(&t), sid, "subject_id({t})");
8816            if let Some(id) = sid {
8817                assert_eq!(d.subject_term(id).as_deref(), Some(t.as_str()));
8818            }
8819            let oid = dict.object_id(&t);
8820            assert_eq!(d.object_id(&t), oid, "object_id({t})");
8821        }
8822        assert_eq!(d.subject_id("<http://example.org/absent>"), None);
8823        assert_eq!(d.predicate_id("<http://ex/p>"), Some(1));
8824        assert_eq!(d.predicate_term(1).as_deref(), Some("<http://ex/p>"));
8825    }
8826
8827    #[test]
8828    #[cfg(feature = "compression")]
8829    fn compression_shrinks_repetitive_data() {
8830        // Many triples sharing IRI prefixes — exactly what front-coding + zstd
8831        // should crush. The compressed file must be far smaller than the raw
8832        // term bytes, and still query correctly.
8833        let mut db = DictionaryBuilder::new();
8834        let triples: Vec<(String, String, String)> = (0..500)
8835            .map(|i| {
8836                (
8837                    format!("<http://example.org/entity/{i}>"),
8838                    "<http://example.org/p/relatedTo>".to_string(),
8839                    format!("<http://example.org/entity/{}>", (i + 1) % 500),
8840                )
8841            })
8842            .collect();
8843        for (s, p, o) in &triples {
8844            db.observe(s, p, o);
8845        }
8846        let dict = db.build();
8847        let mut ib = GraphIndexBuilder::new();
8848        for (s, p, o) in &triples {
8849            ib.push(dict.encode(s, p, o).unwrap());
8850        }
8851        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
8852
8853        let raw: usize = triples
8854            .iter()
8855            .map(|(s, p, o)| s.len() + p.len() + o.len())
8856            .sum();
8857        assert!(
8858            bytes.len() < raw / 2,
8859            "expected strong compression: file {} vs raw terms {raw}",
8860            bytes.len()
8861        );
8862
8863        // Still queryable after compression.
8864        let rete = Rete::open(&bytes).unwrap();
8865        let r = rete.query(Some("<http://example.org/entity/0>"), None, None);
8866        assert_eq!(r.len(), 1);
8867        assert_eq!(r[0].2, "<http://example.org/entity/1>");
8868    }
8869
8870    fn big_file_with_pyramid() -> Vec<u8> {
8871        // A ring of 300 entities -> index dwarfs dict+meta.
8872        let triples: Vec<(String, String, String)> = (0..300)
8873            .map(|i| {
8874                (
8875                    format!("<http://ex/e{i}>"),
8876                    "<http://ex/next>".to_string(),
8877                    format!("<http://ex/e{}>", (i + 1) % 300),
8878                )
8879            })
8880            .collect();
8881        let mut db = DictionaryBuilder::new();
8882        for (s, p, o) in &triples {
8883            db.observe(s, p, o);
8884        }
8885        let dict = db.build();
8886        let ids: Vec<_> = triples
8887            .iter()
8888            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
8889            .collect();
8890        let mut ib = GraphIndexBuilder::new();
8891        for &t in &ids {
8892            ib.push(t);
8893        }
8894        let (meta, levels) = build_pyramid_meta(&dict, &ids, DEFAULT_TILE_BUDGET);
8895        write_file(&dict, &ib.build(), false, &meta, levels)
8896    }
8897
8898    #[test]
8899    fn ranged_open_is_minimal_and_correct() {
8900        use crate::reader::{CountingReader, SliceReader};
8901        let bytes = big_file_with_pyramid();
8902
8903        let full = CountingReader::new(SliceReader::new(&bytes));
8904        let rete = Rete::open_ranged(&full).unwrap();
8905        // Full open touches at most 4 ranges (header, dict, index, meta).
8906        assert!(full.requests() <= 4, "requests = {}", full.requests());
8907        assert_eq!(
8908            rete.query(Some("<http://ex/e0>"), None, None)[0].2,
8909            "<http://ex/e1>"
8910        );
8911
8912        // Summary-only open skips the index → strictly fewer bytes than the file.
8913        let summ_reader = CountingReader::new(SliceReader::new(&bytes));
8914        let view = SummaryView::open_ranged(&summ_reader).unwrap().unwrap();
8915        assert!(!view.summary.is_empty());
8916        assert!(
8917            summ_reader.bytes_read() < bytes.len() as u64,
8918            "summary read {} of {} bytes",
8919            summ_reader.bytes_read(),
8920            bytes.len()
8921        );
8922        // And fewer than a full open, since it never fetched the index.
8923        assert!(summ_reader.bytes_read() < full.bytes_read());
8924    }
8925
8926    #[test]
8927    fn content_hash_is_set_and_verifies() {
8928        let bytes = build_image();
8929        let rete = Rete::open(&bytes).unwrap();
8930        assert_ne!(
8931            rete.header().content_hash,
8932            [0u8; 16],
8933            "hash must be populated"
8934        );
8935        assert!(verify(&bytes).unwrap(), "freshly built file verifies");
8936        assert!(
8937            verify_ranged(&crate::reader::SliceReader::new(&bytes)).unwrap(),
8938            "the streaming verify agrees"
8939        );
8940
8941        // Same data builds an identical hash (deterministic).
8942        assert_eq!(
8943            Rete::open(&build_image()).unwrap().header().content_hash,
8944            rete.header().content_hash
8945        );
8946
8947        // Corrupting a payload byte breaks verification.
8948        let mut tampered = bytes.clone();
8949        let last = tampered.len() - 5; // inside payload, before footer magic
8950        tampered[last] ^= 0xff;
8951        assert!(!verify(&tampered).unwrap());
8952        assert!(!verify_ranged(&crate::reader::SliceReader::new(&tampered)).unwrap());
8953        // A truncated file is an error, never a pass.
8954        let short = &bytes[..bytes.len() / 2];
8955        assert!(verify_ranged(&crate::reader::SliceReader::new(short)).is_err());
8956        assert!(verify_ranged(&crate::reader::SliceReader::new(&bytes[..10])).is_err());
8957    }
8958
8959    /// Build the standard 3-triple image with an opaque metadata payload.
8960    fn build_with_metadata(meta: &[u8]) -> Vec<u8> {
8961        let triples = [
8962            ("Alice", "knows", "Bob"),
8963            ("Bob", "knows", "Carol"),
8964            ("Alice", "age", "30"),
8965        ];
8966        let mut db = DictionaryBuilder::new();
8967        for (s, p, o) in triples {
8968            db.observe(s, p, o);
8969        }
8970        let dict = db.build();
8971        let ids: Vec<_> = triples
8972            .iter()
8973            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
8974            .collect();
8975        let mut ib = GraphIndexBuilder::new();
8976        for &t in &ids {
8977            ib.push(t);
8978        }
8979        let (pmeta, levels) = build_pyramid_meta(&dict, &ids, DEFAULT_TILE_BUDGET);
8980        write_dataset_with_metadata(&dict, &ib.build(), &[], false, &pmeta, levels, meta, &[])
8981    }
8982
8983    #[test]
8984    fn metadata_round_trips_and_shifts_offsets() {
8985        let card = br#"{"title":"My Dataset"}"#;
8986        let bytes = build_with_metadata(card);
8987        let rete = Rete::open(&bytes).unwrap();
8988
8989        // The opaque payload reads back verbatim.
8990        assert_eq!(rete.metadata(), Some(card.as_slice()));
8991        let h = rete.header();
8992        assert_eq!(h.metadata_offset, HEADER_LEN as u64);
8993        assert_eq!(h.metadata_len, card.len() as u64);
8994        // The dictionary (and everything after it) shifted forward by the card.
8995        assert_eq!(h.dictionary_offset, HEADER_LEN as u64 + card.len() as u64);
8996
8997        // The index still decodes correctly at its shifted offset.
8998        assert_eq!(
8999            rete.query(Some("Bob"), Some("knows"), Some("Carol")).len(),
9000            1
9001        );
9002        // The card is inside the content hash, so the file still verifies.
9003        assert!(verify(&bytes).unwrap());
9004    }
9005
9006    #[test]
9007    fn empty_metadata_is_byte_identical_to_plain_writer() {
9008        // The `&[]` path must produce exactly the bytes of the metadata-free
9009        // writer for identical inputs — old files and outputs are unchanged.
9010        assert_eq!(
9011            build_with_metadata(&[]),
9012            build_image(),
9013            "empty-metadata output must equal the plain writer byte-for-byte"
9014        );
9015    }
9016
9017    /// The contract `replace_metadata` has to meet to be usable at the end of a
9018    /// build: swapping the card must land on **exactly** the bytes the writer
9019    /// would have produced with that card in the first place — shorter, longer,
9020    /// and gone — so a spliced file and a rebuilt one are the same file.
9021    #[test]
9022    fn replace_metadata_matches_a_direct_write() {
9023        let first = br#"{"title":"My Dataset","queries":[1,2,3]}"#;
9024        let bytes = build_with_metadata(first);
9025        for target in [
9026            br#"{"title":"My Dataset","queries":[1,2]}"#.as_slice(), // shorter
9027            br#"{"title":"My Dataset","queries":[1,2,3,4,5,6,7]}"#.as_slice(), // longer
9028            b"".as_slice(),                                          // removed
9029            first.as_slice(),                                        // unchanged
9030        ] {
9031            let spliced = replace_metadata(&bytes, target).unwrap();
9032            assert_eq!(
9033                spliced,
9034                build_with_metadata(target),
9035                "splicing {} bytes of metadata must equal a direct write",
9036                target.len()
9037            );
9038            assert!(
9039                verify(&spliced).unwrap(),
9040                "the new hash covers the new card"
9041            );
9042            let rete = Rete::open(&spliced).unwrap();
9043            assert_eq!(
9044                rete.metadata(),
9045                (!target.is_empty()).then_some(target),
9046                "the new payload reads back verbatim"
9047            );
9048            // The data is untouched by the surgery.
9049            assert_eq!(
9050                rete.query(Some("Bob"), Some("knows"), Some("Carol")).len(),
9051                1
9052            );
9053        }
9054    }
9055
9056    /// A build-info section survives the splice, still adjacent to the card, and
9057    /// still outside the content hash.
9058    #[test]
9059    fn replace_metadata_keeps_build_info_adjacent() {
9060        let with_info =
9061            attach_build_info(&build_with_metadata(br#"{"a":1}"#), br#"{"builder":"x"}"#).unwrap();
9062        let spliced = replace_metadata(&with_info, br#"{"a":1,"b":2}"#).unwrap();
9063        let h = Header::from_bytes(&spliced).unwrap();
9064        assert_eq!(h.metadata_offset, HEADER_LEN as u64);
9065        assert_eq!(h.build_info_offset, HEADER_LEN as u64 + h.metadata_len);
9066        assert_eq!(
9067            read_build_info(&spliced).unwrap().as_deref(),
9068            Some(br#"{"builder":"x"}"#.as_slice())
9069        );
9070        assert!(verify(&spliced).unwrap());
9071        // Same card, same content hash, whatever the build-info says.
9072        assert_eq!(
9073            h.content_hash,
9074            Header::from_bytes(&build_with_metadata(br#"{"a":1,"b":2}"#))
9075                .unwrap()
9076                .content_hash
9077        );
9078    }
9079
9080    #[test]
9081    fn metadata_is_tamper_evident() {
9082        let card = br#"{"title":"x"}"#;
9083        let mut bytes = build_with_metadata(card);
9084        assert!(verify(&bytes).unwrap());
9085        // The card occupies [HEADER_LEN .. HEADER_LEN+card_len); flip a byte in it.
9086        bytes[HEADER_LEN + 2] ^= 0xff;
9087        assert!(
9088            !verify(&bytes).unwrap(),
9089            "tampering with the card must break verify()"
9090        );
9091    }
9092
9093    #[test]
9094    fn ranged_opens_do_not_fetch_metadata() {
9095        use crate::reader::{CountingReader, SliceReader};
9096        let card = vec![0xABu8; 512]; // distinctive and sizable
9097        let bytes = build_with_metadata(&card);
9098        let total = bytes.len() as u64;
9099
9100        // A full ranged open never loads the card and never reads its byte range.
9101        let r = CountingReader::new(SliceReader::new(&bytes));
9102        let rete = Rete::open_ranged(&r).unwrap();
9103        assert!(
9104            rete.metadata().is_none(),
9105            "open_ranged must not load the card"
9106        );
9107        assert!(r.requests() <= 4, "requests = {}", r.requests());
9108        assert!(
9109            r.bytes_read() <= total - card.len() as u64,
9110            "read {} of {} bytes; the {}-byte card must be skipped",
9111            r.bytes_read(),
9112            total,
9113            card.len()
9114        );
9115
9116        // Summary-only open likewise ignores the card and still summarizes.
9117        let rs = CountingReader::new(SliceReader::new(&bytes));
9118        let view = SummaryView::open_ranged(&rs).unwrap().unwrap();
9119        assert!(!view.summary.is_empty());
9120        assert!(rs.bytes_read() <= total - card.len() as u64);
9121    }
9122
9123    #[test]
9124    fn metadata_ranged_fetches_only_header_and_card() {
9125        use crate::reader::{CountingReader, SliceReader};
9126        // The CARD tier: fetch the self-description over a RangeReader touching
9127        // only the header + metadata range — never the dictionary/index/pyramid.
9128        let card = vec![0xCDu8; 384];
9129        let bytes = build_with_metadata(&card);
9130
9131        let r = CountingReader::new(SliceReader::new(&bytes));
9132        let got = read_metadata_ranged(&r).unwrap().unwrap();
9133        assert_eq!(got, card, "the card reads back verbatim");
9134        assert_eq!(r.requests(), 2, "exactly header + metadata ranges");
9135        assert_eq!(
9136            r.bytes_read(),
9137            HEADER_LEN as u64 + card.len() as u64,
9138            "no dictionary/index/pyramid bytes are touched"
9139        );
9140
9141        // A cardless file resolves to None after a single header read.
9142        let plain = build_image();
9143        let rp = CountingReader::new(SliceReader::new(&plain));
9144        assert!(read_metadata_ranged(&rp).unwrap().is_none());
9145        assert_eq!(rp.requests(), 1, "header only for a cardless file");
9146        assert_eq!(rp.bytes_read(), HEADER_LEN as u64);
9147    }
9148
9149    #[test]
9150    fn build_info_attaches_outside_the_hash() {
9151        let card = br#"{"title":"My Dataset"}"#;
9152        let base = build_with_metadata(card);
9153        let base_hash = Header::from_bytes(&base).unwrap().content_hash;
9154
9155        let info = br#"{"built_at":"2026-08-04T00:00:00Z","builder":"rete-cli 0.3.2"}"#;
9156        let with = attach_build_info(&base, info).unwrap();
9157
9158        // The section reads back verbatim, adjacent to the metadata.
9159        assert_eq!(read_build_info(&with).unwrap().unwrap(), info);
9160        let h = Header::from_bytes(&with).unwrap();
9161        assert_eq!(h.build_info_offset, HEADER_LEN as u64 + card.len() as u64);
9162        assert_eq!(h.build_info_len, info.len() as u64);
9163        assert_eq!(h.expected_file_len(), Some(with.len() as u64));
9164
9165        // OUTSIDE the hash: the content hash is unchanged and the file still
9166        // verifies — two builds of identical data stay hash-equal even though
9167        // their build-info differs.
9168        assert_eq!(h.content_hash, base_hash);
9169        assert!(verify(&with).unwrap());
9170        let other = attach_build_info(&base, br#"{"built_at":"1999-01-01T00:00:00Z"}"#).unwrap();
9171        assert_eq!(Header::from_bytes(&other).unwrap().content_hash, base_hash);
9172
9173        // The graph still opens and answers at its shifted offsets.
9174        let rete = Rete::open(&with).unwrap();
9175        assert_eq!(
9176            rete.query(Some("Bob"), Some("knows"), Some("Carol")).len(),
9177            1
9178        );
9179        assert_eq!(rete.metadata(), Some(card.as_slice()));
9180
9181        // Replacing an existing section works and removing it restores the
9182        // original image byte-for-byte (the strip-equality determinism proof).
9183        let replaced = attach_build_info(&with, br#"{"builder":"other"}"#).unwrap();
9184        assert_eq!(
9185            read_build_info(&replaced).unwrap().unwrap(),
9186            br#"{"builder":"other"}"#
9187        );
9188        assert!(verify(&replaced).unwrap());
9189        let stripped = attach_build_info(&replaced, &[]).unwrap();
9190        assert_eq!(stripped, base, "stripping build-info restores the image");
9191    }
9192
9193    #[test]
9194    fn build_info_attaches_on_a_metadata_free_file() {
9195        // No card: the section slots in right after the header.
9196        let base = build_image();
9197        let info = b"{\"builder\":\"x\"}";
9198        let with = attach_build_info(&base, info).unwrap();
9199        let h = Header::from_bytes(&with).unwrap();
9200        assert_eq!(h.build_info_offset, HEADER_LEN as u64);
9201        assert_eq!(h.metadata_len, 0);
9202        assert_eq!(read_build_info(&with).unwrap().unwrap(), info);
9203        assert!(verify(&with).unwrap());
9204        assert_eq!(
9205            h.content_hash,
9206            Header::from_bytes(&base).unwrap().content_hash
9207        );
9208        Rete::open(&with).unwrap();
9209    }
9210
9211    /// The contract a **streaming** rewriter needs: a plan is enough to rebuild
9212    /// the file from three spans — the header it hands back, the untouched
9213    /// `[HEADER_LEN, insert)` prefix, the new section, and the untouched
9214    /// `[tail_start, len)` tail — and the result is byte-identical to the
9215    /// in-memory splice. That is what lets a 17 GB file gain a build record
9216    /// with a 4 MiB buffer instead of 34 GB of RAM.
9217    #[test]
9218    fn a_plan_rebuilds_exactly_what_the_in_memory_splice_produces() {
9219        let card = br#"{"title":"My Dataset"}"#;
9220        let base = build_with_metadata(card);
9221        let base_hash = Header::from_bytes(&base).unwrap().content_hash;
9222        for start in [
9223            base.clone(),
9224            attach_build_info(&base, b"{\"a\":1}").unwrap(),
9225        ] {
9226            for info in [
9227                br#"{"builder":"rete-cli 0.3.2","query_costs":{}}"#.as_slice(), // longer
9228                b"{}".as_slice(),                                               // shorter
9229                b"".as_slice(),                                                 // removed
9230            ] {
9231                let plan = plan_build_info(&start, start.len() as u64, info.len() as u64).unwrap();
9232                let mut streamed = Vec::new();
9233                streamed.extend_from_slice(&plan.header);
9234                streamed.extend_from_slice(&start[HEADER_LEN..plan.insert as usize]);
9235                streamed.extend_from_slice(info);
9236                streamed.extend_from_slice(&start[plan.tail_start as usize..]);
9237                assert_eq!(streamed.len() as u64, plan.new_len);
9238                assert_eq!(
9239                    streamed,
9240                    attach_build_info(&start, info).unwrap(),
9241                    "a {}-byte section streamed must equal the same section spliced",
9242                    info.len()
9243                );
9244                // The point of the exercise: the file's identity is untouched.
9245                assert!(verify(&streamed).unwrap());
9246                let h = Header::from_bytes(&streamed).unwrap();
9247                assert_eq!(h.content_hash, base_hash);
9248                assert_eq!(h.expected_file_len(), Some(plan.new_len));
9249                let rete = Rete::open(&streamed).unwrap();
9250                assert_eq!(rete.metadata(), Some(card.as_slice()));
9251                assert_eq!(
9252                    rete.query(Some("Bob"), Some("knows"), Some("Carol")).len(),
9253                    1
9254                );
9255            }
9256        }
9257    }
9258
9259    /// A plan is refused rather than trusted when the header's own numbers put
9260    /// the section outside the file — the header is attacker-controlled input.
9261    #[test]
9262    fn a_plan_refuses_a_header_that_overruns_the_file() {
9263        let base = build_with_metadata(br#"{"title":"x"}"#);
9264        assert!(plan_build_info(&base, 8, 16).is_err());
9265        assert!(plan_build_info(&base[..HEADER_LEN], base.len() as u64, 16).is_ok());
9266    }
9267
9268    #[test]
9269    fn card_and_build_info_ranged_is_one_header_plus_one_range() {
9270        use crate::reader::{CountingReader, SliceReader};
9271        let card = vec![0xCDu8; 384];
9272        let info = vec![0xEFu8; 200];
9273        let bytes = attach_build_info(&build_with_metadata(&card), &info).unwrap();
9274
9275        // Both sections in ONE coalesced range after the header — the CARD
9276        // tier's 1 header + 1 range budget holds with build info included.
9277        let r = CountingReader::new(SliceReader::new(&bytes));
9278        let (m, b) = read_card_and_build_info_ranged(&r).unwrap();
9279        assert_eq!(m.unwrap(), card);
9280        assert_eq!(b.unwrap(), info);
9281        assert_eq!(r.requests(), 2, "header + one coalesced range");
9282        assert_eq!(
9283            r.bytes_read(),
9284            HEADER_LEN as u64 + card.len() as u64 + info.len() as u64
9285        );
9286
9287        // Card only: same two requests, no build info.
9288        let plain = build_with_metadata(&card);
9289        let rp = CountingReader::new(SliceReader::new(&plain));
9290        let (m, b) = read_card_and_build_info_ranged(&rp).unwrap();
9291        assert_eq!(m.unwrap(), card);
9292        assert!(b.is_none());
9293        assert_eq!(rp.requests(), 2);
9294        // Stated as the comparison every client depends on: carrying a build
9295        // record costs a reader NOTHING in requests. Any client that shows the
9296        // build record — the CLI, the browser card modal — can therefore do it
9297        // in the card's own budget, and does not need a second call.
9298        assert_eq!(
9299            r.requests(),
9300            rp.requests(),
9301            "build info must not cost an extra request",
9302        );
9303
9304        // Neither: the header read alone answers.
9305        let none = build_image();
9306        let rn = CountingReader::new(SliceReader::new(&none));
9307        let (m, b) = read_card_and_build_info_ranged(&rn).unwrap();
9308        assert!(m.is_none() && b.is_none());
9309        assert_eq!(rn.requests(), 1);
9310    }
9311
9312    #[test]
9313    fn schema_summary_groups_by_type() {
9314        let rt = RDF_TYPE;
9315        let bytes = build_from(&[
9316            ("Alice", rt, "Person"),
9317            ("Bob", rt, "Person"),
9318            ("NYC", rt, "City"),
9319            ("Alice", "knows", "Bob"),
9320            ("Alice", "livesIn", "NYC"),
9321            ("Alice", "name", "\"Alice\""),
9322        ]);
9323        let rete = Rete::open(&bytes).unwrap();
9324        let summary = schema_summary(&rete);
9325        // Expect class-level relations, rdf:type excluded.
9326        assert!(summary.contains(&("Person".into(), "knows".into(), "Person".into(), 1)));
9327        assert!(summary.contains(&("Person".into(), "livesIn".into(), "City".into(), 1)));
9328        assert!(summary.contains(&("Person".into(), "name".into(), "(literal)".into(), 1)));
9329        // No rdf:type relations in the summary.
9330        assert!(!summary.iter().any(|(_, p, _, _)| p == RDF_TYPE));
9331
9332        // Class populations: 2 People, 1 City, sorted by count desc.
9333        let classes = schema_classes(&rete);
9334        assert_eq!(
9335            classes,
9336            vec![("Person".into(), 2u32), ("City".into(), 1u32)]
9337        );
9338    }
9339
9340    fn build_from(triples: &[(&str, &str, &str)]) -> Vec<u8> {
9341        let mut db = DictionaryBuilder::new();
9342        for (s, p, o) in triples {
9343            db.observe(s, p, o);
9344        }
9345        let dict = db.build();
9346        let mut ib = GraphIndexBuilder::new();
9347        for (s, p, o) in triples {
9348            ib.push(dict.encode(s, p, o).unwrap());
9349        }
9350        write_file(&dict, &ib.build(), false, &[], 0)
9351    }
9352
9353    /// Build a file WITH a pyramid (so the schema pyramid + coherence axioms ship).
9354    fn build_with_pyramid(triples: &[(&str, &str, &str)]) -> Vec<u8> {
9355        let mut db = DictionaryBuilder::new();
9356        for (s, p, o) in triples {
9357            db.observe(s, p, o);
9358        }
9359        let dict = db.build();
9360        let encoded: Vec<_> = triples
9361            .iter()
9362            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
9363            .collect();
9364        let mut ib = GraphIndexBuilder::new();
9365        for t in &encoded {
9366            ib.push(*t);
9367        }
9368        let (meta, levels) = build_pyramid_meta(&dict, &encoded, DEFAULT_TILE_BUDGET);
9369        write_dataset(&dict, &ib.build(), &[], false, &meta, levels)
9370    }
9371
9372    #[test]
9373    fn tbox_coherence_flags_unsatisfiable_class_index_free() {
9374        use crate::reader::{CountingReader, SliceReader};
9375        let rt = RDF_TYPE;
9376        let sub = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";
9377        let disj = "<http://www.w3.org/2002/07/owl#disjointWith>";
9378        // C ⊑ D, C ⊑ E, D disjointWith E ⇒ C unsatisfiable — schema-only, with one
9379        // instance present just so the schema pyramid gets built.
9380        let bytes = build_with_pyramid(&[
9381            ("<http://ex/C>", sub, "<http://ex/D>"),
9382            ("<http://ex/C>", sub, "<http://ex/E>"),
9383            ("<http://ex/D>", disj, "<http://ex/E>"),
9384            ("<http://ex/x>", rt, "<http://ex/C>"),
9385        ]);
9386
9387        let r = CountingReader::new(SliceReader::new(&bytes));
9388        let view = SummaryView::open_ranged(&r).unwrap().unwrap();
9389        let points = view.tbox_coherence();
9390        assert!(
9391            points
9392                .iter()
9393                .any(|i| i.kind == "unsatisfiable-class" && i.detail.contains("http://ex/C>")),
9394            "expected C unsatisfiable from the schema alone, got {points:?}"
9395        );
9396
9397        // Proven index-free: bytes read never reach the (root_dir) index section,
9398        // mirroring `schema_pyramid_round_trips_through_file_index_free`.
9399        let header = Header::from_bytes(&bytes[..HEADER_LEN]).unwrap();
9400        assert!(
9401            r.bytes_read() <= bytes.len() as u64 - header.root_dir_len,
9402            "tbox_coherence must not read the triple index"
9403        );
9404    }
9405
9406    #[test]
9407    fn schema_coherence_reads_only_the_schema_block() {
9408        use crate::reader::{CountingReader, SliceReader};
9409        let rt = RDF_TYPE;
9410        let sub = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";
9411        let disj = "<http://www.w3.org/2002/07/owl#disjointWith>";
9412        // 500 instances with unique literals → a sizable dictionary + community
9413        // summary, so a whole-pyramid-meta read would be large; the schema block
9414        // (bounded by the tiny ontology) stays small.
9415        let mut triples: Vec<(String, String, String)> = vec![
9416            ("<http://ex/C>".into(), sub.into(), "<http://ex/D>".into()),
9417            ("<http://ex/C>".into(), sub.into(), "<http://ex/E>".into()),
9418            ("<http://ex/D>".into(), disj.into(), "<http://ex/E>".into()),
9419        ];
9420        for i in 0..500 {
9421            let s = format!("<http://ex/x{i}>");
9422            triples.push((s.clone(), rt.into(), "<http://ex/C>".into()));
9423            triples.push((
9424                s,
9425                "<http://ex/label>".into(),
9426                format!("\"unique label {i}\""),
9427            ));
9428        }
9429        let trefs: Vec<(&str, &str, &str)> = triples
9430            .iter()
9431            .map(|(s, p, o)| (s.as_str(), p.as_str(), o.as_str()))
9432            .collect();
9433        let bytes = build_with_pyramid(&trefs);
9434
9435        let header = Header::from_bytes(&bytes[..HEADER_LEN]).unwrap();
9436        assert!(
9437            header.schema_meta_len > 0,
9438            "the writer recorded a schema-block length"
9439        );
9440        assert!(
9441            (header.schema_meta_len as u64) < header.pyramid_meta_len,
9442            "schema block ({}) should be far smaller than the whole pyramid-meta ({})",
9443            header.schema_meta_len,
9444            header.pyramid_meta_len
9445        );
9446
9447        let r = CountingReader::new(SliceReader::new(&bytes));
9448        let points = read_schema_coherence_ranged(&r).unwrap().unwrap();
9449        assert!(points.iter().any(|i| i.kind == "unsatisfiable-class"));
9450        // It read only the header + the schema block — not the summary or dictionary.
9451        assert!(
9452            r.bytes_read() <= HEADER_LEN as u64 + header.schema_meta_len as u64,
9453            "read {} bytes; expected <= header + schema block ({})",
9454            r.bytes_read(),
9455            HEADER_LEN as u64 + header.schema_meta_len as u64
9456        );
9457    }
9458
9459    #[test]
9460    fn tbox_coherence_clean_schema_is_coherent() {
9461        use crate::reader::SliceReader;
9462        let rt = RDF_TYPE;
9463        let sub = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";
9464        let bytes = build_with_pyramid(&[
9465            ("<http://ex/Dog>", sub, "<http://ex/Animal>"),
9466            ("<http://ex/x>", rt, "<http://ex/Dog>"),
9467        ]);
9468        let view = SummaryView::open_ranged(&SliceReader::new(&bytes))
9469            .unwrap()
9470            .unwrap();
9471        assert!(view.tbox_is_coherent(), "a plain hierarchy is coherent");
9472    }
9473
9474    /// A quad-index file must be **refused** by every reader that predates it —
9475    /// an old reader ignores flag bit 3 and the unknown kind 10, so it would
9476    /// otherwise open the file as one without named graphs and answer `GRAPH`
9477    /// queries with nothing. The seal makes each of their open paths fail on the
9478    /// index container's section count, before any payload is read; their
9479    /// `verify` still passes, because the part list it hashes is unchanged.
9480    /// (Checked against real 0.3.x binaries too: QUAD-INDEX-DESIGN.md §9.)
9481    #[test]
9482    fn readers_before_the_quad_index_refuse_its_files() {
9483        for perms in [PermSet::ALL, PermSet::CORE] {
9484            let quads: Vec<crate::ingest::RawQuad> = (0..50)
9485                .map(|i| {
9486                    (
9487                        format!("<http://ex/s{i}>"),
9488                        "<http://ex/p>".to_string(),
9489                        format!("\"{i}\""),
9490                        Some(format!("<http://ex/g{}>", i % 7)),
9491                    )
9492                })
9493                .collect();
9494            let (bytes, _) = crate::ingest::assemble_dataset_with_layout(
9495                quads,
9496                false,
9497                false,
9498                None,
9499                crate::PyramidAlgo::Louvain,
9500                perms,
9501                crate::ingest::GraphLayout::Quad,
9502                |_, _| Vec::<u8>::new(),
9503            );
9504            let h = Header::from_bytes(&bytes).unwrap();
9505            assert!(h.has_quad_index());
9506            let root =
9507                &bytes[h.root_dir_offset as usize..(h.root_dir_offset + h.root_dir_len) as usize];
9508            // 0.3.4–0.3.7, resident (`Rete::open`, `open_ranged`): count != mask.
9509            assert!(decode_index_container(root, h.block_codec, h.perms).is_err());
9510            assert!(
9511                container_section_payload_ranges(root, h.root_dir_offset, h.perms.len()).is_err()
9512            );
9513            // 0.3.0–0.3.3 expected exactly six sections, whatever the mask.
9514            assert_ne!(decode_container(root, CODEC_NONE).unwrap().len(), NUM_PERMS);
9515            assert_ne!(
9516                decode_container(root, CODEC_NONE).unwrap().len(),
9517                h.perms.len()
9518            );
9519            // Every lazy / remote open (`open_ranged_lazy`, `query_ranged`).
9520            let r = crate::reader::SliceReader::new(&bytes);
9521            for expect in [h.perms.len(), NUM_PERMS] {
9522                assert!(locate_container_section_ranged(
9523                    &r,
9524                    h.root_dir_offset,
9525                    h.root_dir_len,
9526                    0,
9527                    expect as u64
9528                )
9529                .is_err());
9530            }
9531            // Their `verify` hashes the same parts the writer hashed.
9532            assert_eq!(hash_of_sections(&h, &bytes).unwrap(), h.content_hash);
9533            // And a current reader reads the file.
9534            let rete = Rete::open(&bytes).unwrap();
9535            assert_eq!(rete.named_graph_count(), 7);
9536            assert_eq!(rete.dump(Some("<http://ex/g3>")).len(), 7);
9537        }
9538    }
9539
9540    #[test]
9541    fn named_graphs_round_trip() {
9542        // One shared dictionary; default graph + a named graph "g1".
9543        let all = [
9544            ("Alice", "knows", "Bob"), // default
9545            ("Bob", "age", "30"),      // named g1
9546        ];
9547        let mut db = DictionaryBuilder::new();
9548        for (s, p, o) in all {
9549            db.observe(s, p, o);
9550        }
9551        let dict = db.build();
9552
9553        let mut def = GraphIndexBuilder::new();
9554        def.push(dict.encode("Alice", "knows", "Bob").unwrap());
9555        let mut g1 = GraphIndexBuilder::new();
9556        g1.push(dict.encode("Bob", "age", "30").unwrap());
9557
9558        let named = vec![("http://ex/g1".to_string(), g1.build())];
9559        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
9560
9561        assert!(verify(&bytes).unwrap());
9562        let rete = Rete::open(&bytes).unwrap();
9563        assert_eq!(rete.graph_names(), vec!["http://ex/g1"]);
9564        // The named graph contains Bob age 30, not the default-graph triple.
9565        let gi = rete.graph_index("http://ex/g1").unwrap();
9566        assert_eq!(gi.triple_count(), 1);
9567        assert!(rete.graph_index("http://ex/missing").is_none());
9568
9569        // quad_count counts ALL quads — default graph + named graphs (1 + 1),
9570        // not just the default index (which would report 1).
9571        assert_eq!(rete.header().quad_count, 2);
9572
9573        // Default-graph query path is unchanged.
9574        assert_eq!(rete.query(Some("Alice"), None, None).len(), 1);
9575
9576        // dump() round-trips each graph back to terms.
9577        assert_eq!(
9578            rete.dump(None),
9579            vec![("Alice".into(), "knows".into(), "Bob".into())]
9580        );
9581        assert_eq!(
9582            rete.dump(Some("http://ex/g1")),
9583            vec![("Bob".into(), "age".into(), "30".into())]
9584        );
9585    }
9586
9587    /// A named graph whose container exceeds [`NAMED_GRAPH_RESIDENT_MAX`]
9588    /// opens through [`open_index_container_lazy`] — the default graph's own
9589    /// machinery, at the container's offset. Drive that helper directly over a
9590    /// named graph's container range (the threshold itself is too large to
9591    /// cross cheaply in a unit test) and require identical matches.
9592    #[test]
9593    fn named_graph_container_opens_tile_lazily_like_the_root() {
9594        let mut db = DictionaryBuilder::new();
9595        let node = |n: u32| format!("<http://ex/n{n}>");
9596        let p = "<http://ex/p>".to_string();
9597        db.observe(&node(0), &p, &node(1));
9598        for i in 0..500u32 {
9599            db.observe(&node(1000 + i), &p, &node(2000 + i));
9600        }
9601        let dict = db.build();
9602        let mut def = GraphIndexBuilder::new();
9603        def.push(dict.encode(&node(0), &p, &node(1)).unwrap());
9604        let mut g = GraphIndexBuilder::new().with_tile_budget(256); // many tiles
9605        for i in 0..500u32 {
9606            g.push(dict.encode(&node(1000 + i), &p, &node(2000 + i)).unwrap());
9607        }
9608        let named = vec![("<http://ex/g>".to_string(), g.build())];
9609        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
9610        let rete = Rete::open(&bytes).unwrap();
9611        let header = rete.header();
9612
9613        // The single entry's container range: count varint, iri_len varint,
9614        // iri, container_len varint, then the container.
9615        let soff = header.named_graphs_offset as usize;
9616        let send = soff + header.named_graphs_len as usize;
9617        let sec = &bytes[soff..send];
9618        let (n, mut pos) = read_uvarint(sec).unwrap();
9619        assert_eq!(n, 1);
9620        let (ilen, u1) = read_uvarint(&sec[pos..]).unwrap();
9621        pos += u1 + ilen as usize;
9622        let (clen, u2) = read_uvarint(&sec[pos..]).unwrap();
9623        pos += u2;
9624        let container = ByteRange {
9625            offset: (soff + pos) as u64,
9626            len: clen,
9627        };
9628
9629        use crate::reader::SliceReader;
9630        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
9631        let reader: std::sync::Arc<dyn RangeReader + Send + Sync> =
9632            std::sync::Arc::new(SliceReader::new(leaked));
9633        let (lazy_idx, _, _) = open_index_container_lazy(
9634            &reader,
9635            container,
9636            header.block_codec,
9637            header.has_tile_synopsis(),
9638            1,
9639            header.perms,
9640            header.perms.len(),
9641        )
9642        .unwrap();
9643        let mut want = rete
9644            .graph_index("<http://ex/g>")
9645            .unwrap()
9646            .match_pattern((None, None, None));
9647        let mut got = lazy_idx.match_pattern((None, None, None));
9648        want.sort_unstable();
9649        got.sort_unstable();
9650        assert_eq!(got.len(), 500);
9651        assert_eq!(got, want);
9652        assert!(!lazy_idx.load_incomplete());
9653    }
9654
9655    /// `dump_iter` must agree with `dump` exactly — and must be *lazy*: taking
9656    /// the first triple of a lazily range-read file may not drag the whole
9657    /// index across the reader, or the JS client's streaming quad cursor (which
9658    /// is this iterator, suspended between wasm calls) would be a materializing
9659    /// dump wearing a cursor's clothes.
9660    #[test]
9661    fn dump_iter_matches_dump_and_stops_early() {
9662        use crate::reader::{CountingReader, SliceReader};
9663
9664        // Enough triples over tiny tiles to force many tiles per permutation.
9665        let triples: Vec<(String, String, String)> = (0..2000u32)
9666            .map(|i| {
9667                (
9668                    format!("<http://ex/s/{i:04}>"),
9669                    "<http://ex/p>".to_string(),
9670                    format!("<http://ex/o/{i:04}>"),
9671                )
9672            })
9673            .collect();
9674        let mut db = DictionaryBuilder::new();
9675        for (s, p, o) in &triples {
9676            db.observe(s, p, o);
9677        }
9678        let dict = db.build();
9679        let mut ib = GraphIndexBuilder::new().with_tile_budget(256);
9680        for (s, p, o) in &triples {
9681            ib.push(dict.encode(s, p, o).unwrap());
9682        }
9683        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
9684
9685        // Same triples, same order, as the eager dump.
9686        let rete = Rete::open(&bytes).unwrap();
9687        assert_eq!(rete.dump_iter(None).collect::<Vec<_>>(), rete.dump(None));
9688        // A missing named graph yields nothing rather than the default graph.
9689        assert_eq!(rete.dump_iter(Some("http://ex/nope")).count(), 0);
9690
9691        // Lazily: taking one triple must cost far fewer bytes than draining.
9692        let read_bytes = |take: usize| -> u64 {
9693            let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
9694            let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
9695            let rete = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
9696            let before = reader.bytes_read();
9697            let n = rete.dump_iter(None).take(take).count();
9698            assert_eq!(n, take.min(triples.len()));
9699            assert!(!rete.index_incomplete());
9700            reader.bytes_read() - before
9701        };
9702        let first_only = read_bytes(1);
9703        let everything = read_bytes(triples.len());
9704        // Always true: stopping after one triple cannot cost a full scan.
9705        assert!(
9706            first_only < everything,
9707            "dump_iter is not lazy: one triple read {first_only} B, a full scan {everything} B"
9708        );
9709        // The STRONG claim — under half — only holds once the payload dwarfs the
9710        // fixed open cost. This fixture is ~32 KB, and with `compression` off the
9711        // dictionary is essentially the whole file, so opening it at all already
9712        // pays most of `everything` (21 KB of 33 KB) and the ratio says nothing
9713        // about laziness. Asserting it unconditionally is how this test failed on
9714        // `--no-default-features` while passing everywhere else.
9715        #[cfg(feature = "compression")]
9716        assert!(
9717            first_only * 2 < everything,
9718            "dump_iter is not lazy: one triple read {first_only} B of the {everything} B a full scan reads"
9719        );
9720
9721        // A FRESH open: these assertions fault tiles and the dictionary in,
9722        // and the laziness measurement above counts bytes on a COLD reader —
9723        // sharing one `rete` makes first_only and everything both ~0, so the
9724        // comparison silently stops testing anything.
9725        let fresh = Rete::open(&bytes).unwrap();
9726        // `dump_batch` is the resumable form the language clients pull through.
9727        // Driven to exhaustion it must reproduce the eager dump EXACTLY — same
9728        // triples, same order — for any batch size, including one so small that
9729        // every call ends on a subject boundary before reaching it.
9730        let drain = |max_quads: usize| {
9731            let (mut out, mut cursor, mut calls) = (Vec::new(), 0u32, 0);
9732            loop {
9733                let (batch, next, done) = fresh.dump_batch(None, cursor, max_quads);
9734                out.extend(batch);
9735                cursor = next;
9736                calls += 1;
9737                assert!(calls < 10_000, "cursor failed to advance");
9738                if done {
9739                    break out;
9740                }
9741            }
9742        };
9743        let eager = fresh.dump(None);
9744        for max in [1usize, 7, 128, 100_000] {
9745            assert_eq!(drain(max), eager, "batch size {max} changed the dump");
9746        }
9747        // An unknown graph is an empty, already-finished dump — not an error and
9748        // not the default graph leaking through.
9749        let (t, _, done) = fresh.dump_batch(Some("http://ex/nope"), 0, 16);
9750        assert!(t.is_empty() && done);
9751    }
9752
9753    /// `query_batch` is `dump_batch` generalized to a PATTERN — the primitive the
9754    /// Java client's RDF4J cursor pulls through, one bounded batch per wasm call.
9755    /// Three things have to hold or the cursor is a materializing scan wearing a
9756    /// cursor's clothes: driven to exhaustion it must reproduce the eager
9757    /// `query_in_graph` for every pattern shape and every batch size; taking one
9758    /// row must cost far fewer bytes than draining; and every call must either
9759    /// yield a row or say `done`, so the caller can never spin.
9760    #[test]
9761    fn query_batch_matches_query_in_graph_and_stops_early() {
9762        use crate::reader::{CountingReader, SliceReader};
9763
9764        // Enough triples over tiny tiles that a scan spans many tiles, with two
9765        // predicates and a repeated subject so bound patterns have real fanout.
9766        let triples: Vec<(String, String, String)> = (0..2000u32)
9767            .map(|i| {
9768                (
9769                    format!("<http://ex/s/{:04}>", i / 2),
9770                    format!("<http://ex/p/{}>", i % 2),
9771                    format!("<http://ex/o/{i:04}>"),
9772                )
9773            })
9774            .collect();
9775        let mut db = DictionaryBuilder::new();
9776        for (s, p, o) in &triples {
9777            db.observe(s, p, o);
9778        }
9779        let dict = db.build();
9780        let mut ib = GraphIndexBuilder::new().with_tile_budget(256);
9781        for (s, p, o) in &triples {
9782            ib.push(dict.encode(s, p, o).unwrap());
9783        }
9784        let bytes = write_file(&dict, &ib.build(), false, &[], 0);
9785        let rete = Rete::open(&bytes).unwrap();
9786
9787        let drain = |s: Option<&str>, p: Option<&str>, o: Option<&str>, max: usize| {
9788            let (mut out, mut cursor, mut calls) = (Vec::new(), 0u64, 0);
9789            loop {
9790                let (batch, next, done) = rete.query_batch(None, s, p, o, cursor, max);
9791                // The contract the client relies on: rows, or done. Never
9792                // "nothing yet, call again" — that is an unbounded spin.
9793                assert!(!batch.is_empty() || done, "empty non-final batch");
9794                out.extend(batch);
9795                cursor = next;
9796                calls += 1;
9797                assert!(calls < 20_000, "cursor failed to advance");
9798                if done {
9799                    break out;
9800                }
9801            }
9802        };
9803        let shapes: [(Option<&str>, Option<&str>, Option<&str>); 6] = [
9804            (None, None, None),
9805            (Some("<http://ex/s/0007>"), None, None),
9806            (None, Some("<http://ex/p/1>"), None),
9807            (None, None, Some("<http://ex/o/0042>")),
9808            (Some("<http://ex/s/0007>"), Some("<http://ex/p/0>"), None),
9809            (
9810                Some("<http://ex/s/0007>"),
9811                Some("<http://ex/p/0>"),
9812                Some("<http://ex/o/0014>"),
9813            ),
9814        ];
9815        for (s, p, o) in shapes {
9816            let eager = rete.query_in_graph(None, s, p, o);
9817            assert!(
9818                !eager.is_empty(),
9819                "fixture shape {s:?} {p:?} {o:?} matches nothing"
9820            );
9821            // The lazy pull form agrees exactly, order included.
9822            assert_eq!(
9823                rete.query_iter(None, s, p, o).collect::<Vec<_>>(),
9824                eager,
9825                "query_iter disagrees for {s:?} {p:?} {o:?}"
9826            );
9827            for max in [1usize, 3, 64, 100_000] {
9828                let mut got = drain(s, p, o, max);
9829                let mut want = eager.clone();
9830                // A batch is re-sorted canonically per batch, not globally, so
9831                // a bound pattern's batches can arrive in permutation order.
9832                got.sort();
9833                want.sort();
9834                assert_eq!(got, want, "batch size {max} changed {s:?} {p:?} {o:?}");
9835            }
9836            // The fully unbound scan routes to SPO, so batching is order-exact.
9837            if s.is_none() && p.is_none() && o.is_none() {
9838                assert_eq!(drain(s, p, o, 7), eager, "unbound batching reordered rows");
9839            }
9840        }
9841
9842        // A bound term the dictionary has never seen, and an unknown graph, are
9843        // both finished-and-empty rather than errors or a leak of everything.
9844        let (t, _, done) = rete.query_batch(None, Some("<http://ex/nope>"), None, None, 0, 16);
9845        assert!(t.is_empty() && done);
9846        let (t, _, done) = rete.query_batch(Some("http://ex/nope"), None, None, None, 0, 16);
9847        assert!(t.is_empty() && done);
9848        assert_eq!(
9849            rete.query_iter(None, Some("<http://ex/nope>"), None, None)
9850                .count(),
9851            0
9852        );
9853
9854        // Lazily: one row must not drag the whole scan across the reader. This
9855        // is the property that makes `LIMIT 1` bounded on a 48 GiB file.
9856        let read_bytes = |take: usize| -> u64 {
9857            let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
9858            let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
9859            let rete = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
9860            let before = reader.bytes_read();
9861            let (mut n, mut cursor) = (0usize, 0u64);
9862            loop {
9863                let (batch, next, done) = rete.query_batch(None, None, None, None, cursor, 1);
9864                n += batch.len();
9865                cursor = next;
9866                if done || n >= take {
9867                    break;
9868                }
9869            }
9870            assert!(n >= take.min(triples.len()));
9871            assert!(!rete.index_incomplete());
9872            reader.bytes_read() - before
9873        };
9874        let first_only = read_bytes(1);
9875        let everything = read_bytes(triples.len());
9876        assert!(
9877            first_only < everything,
9878            "query_batch is not lazy: one row read {first_only} B, a full scan {everything} B"
9879        );
9880        #[cfg(feature = "compression")]
9881        assert!(
9882            first_only * 2 < everything,
9883            "query_batch is not lazy: one row read {first_only} B of the {everything} B a full scan reads"
9884        );
9885    }
9886
9887    /// A batched cursor over a QUADS file must stay inside the graph it was
9888    /// given: the default graph and each named graph resume independently.
9889    #[test]
9890    fn query_batch_is_graph_scoped() {
9891        let mut db = DictionaryBuilder::new();
9892        for (s, p, o) in [
9893            ("Alice", "knows", "Bob"),
9894            ("Alice", "knows", "Carol"),
9895            ("Alice", "knows", "Dave"),
9896            ("Erin", "knows", "Frank"),
9897            ("Alice", "knows", "Zoe"),
9898        ] {
9899            db.observe(s, p, o);
9900        }
9901        let dict = db.build();
9902        let mut def = GraphIndexBuilder::new();
9903        def.push(dict.encode("Alice", "knows", "Bob").unwrap());
9904        def.push(dict.encode("Alice", "knows", "Carol").unwrap());
9905        let mut g1 = GraphIndexBuilder::new();
9906        g1.push(dict.encode("Alice", "knows", "Dave").unwrap());
9907        g1.push(dict.encode("Erin", "knows", "Frank").unwrap());
9908        let mut g2 = GraphIndexBuilder::new();
9909        g2.push(dict.encode("Alice", "knows", "Zoe").unwrap());
9910        let named = vec![
9911            ("http://ex/g1".to_string(), g1.build()),
9912            ("http://ex/g2".to_string(), g2.build()),
9913        ];
9914        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
9915        let rete = Rete::open(&bytes).unwrap();
9916        for graph in [None, Some("http://ex/g1"), Some("http://ex/g2")] {
9917            let eager = rete.query_in_graph(graph, None, None, None);
9918            let (mut out, mut cursor) = (Vec::new(), 0u64);
9919            loop {
9920                let (batch, next, done) = rete.query_batch(graph, None, None, None, cursor, 1);
9921                out.extend(batch);
9922                cursor = next;
9923                if done {
9924                    break;
9925                }
9926            }
9927            assert_eq!(out, eager, "graph {graph:?} batched differently");
9928        }
9929    }
9930
9931    #[test]
9932    fn query_in_graph_is_graph_scoped() {
9933        // Default graph: Alice knows Bob, Alice knows Carol.
9934        // Named g1: Alice knows Dave (same predicate, different graph).
9935        let mut db = DictionaryBuilder::new();
9936        for (s, p, o) in [
9937            ("Alice", "knows", "Bob"),
9938            ("Alice", "knows", "Carol"),
9939            ("Alice", "knows", "Dave"),
9940        ] {
9941            db.observe(s, p, o);
9942        }
9943        let dict = db.build();
9944
9945        let mut def = GraphIndexBuilder::new();
9946        def.push(dict.encode("Alice", "knows", "Bob").unwrap());
9947        def.push(dict.encode("Alice", "knows", "Carol").unwrap());
9948        let mut g1 = GraphIndexBuilder::new();
9949        g1.push(dict.encode("Alice", "knows", "Dave").unwrap());
9950
9951        let named = vec![("http://ex/g1".to_string(), g1.build())];
9952        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
9953        let rete = Rete::open(&bytes).unwrap();
9954
9955        // Default graph only: the two default-graph objects, not Dave.
9956        let mut def_objs: Vec<String> = rete
9957            .query_in_graph(None, Some("Alice"), Some("knows"), None)
9958            .into_iter()
9959            .map(|(_, _, o)| o)
9960            .collect();
9961        def_objs.sort();
9962        assert_eq!(def_objs, vec!["Bob".to_string(), "Carol".to_string()]);
9963
9964        // Named graph only: just Dave.
9965        assert_eq!(
9966            rete.query_in_graph(Some("http://ex/g1"), Some("Alice"), None, None),
9967            vec![("Alice".into(), "knows".into(), "Dave".into())]
9968        );
9969
9970        // A wildcard-everything scan is scoped to its graph.
9971        assert_eq!(rete.query_in_graph(None, None, None, None).len(), 2);
9972        assert_eq!(
9973            rete.query_in_graph(Some("http://ex/g1"), None, None, None)
9974                .len(),
9975            1
9976        );
9977
9978        // An unknown graph IRI is empty, not an error.
9979        assert!(rete
9980            .query_in_graph(Some("http://ex/missing"), None, None, None)
9981            .is_empty());
9982    }
9983
9984    /// A **bounded** `query_in_graph` — and a dump of one named graph — must not
9985    /// fault the whole dictionary on a lazily range-read file.
9986    ///
9987    /// Both used to call `Dictionary::prefetch_all`, so the graph-scoped pattern
9988    /// primitive an RDF4J `Sail` calls once per named graph paid for every term
9989    /// in the file to answer a handful of rows. Measured on `cordis.rete`
9990    /// (801 MB, a 417 MB dictionary) before the fix: a 15-row answer read
9991    /// 416 MB and peaked at 2.5 GB RSS. The fixture below is the same shape in
9992    /// miniature — long literals so the dictionary dominates the file — and the
9993    /// assertion is the invariant that regressed: bytes read must stay well
9994    /// under the dictionary section. Lazy answers are compared against eager
9995    /// throughout: cheaper is only interesting if it is also correct.
9996    #[test]
9997    fn bounded_query_in_graph_does_not_fault_the_whole_dictionary() {
9998        use crate::reader::{CountingReader, SliceReader};
9999
10000        // Long, distinct, poorly-compressible object literals: the dictionary is
10001        // the payload here, exactly as on a graph that stores abstracts or
10002        // embedded media. Repeating one filler string would not do — front
10003        // coding plus zstd shrink 2000 near-identical literals to ~6 KB, which
10004        // is smaller than the index and makes the measurement meaningless.
10005        let payload = |i: u32| -> String {
10006            let mut s = String::with_capacity(800);
10007            let mut x = i as u64 ^ 0x9e37_79b9_7f4a_7c15;
10008            while s.len() < 800 {
10009                x = x
10010                    .wrapping_mul(6364136223846793005)
10011                    .wrapping_add(1442695040888963407);
10012                s.push_str(&format!("{:016x}", x ^ (x >> 29)));
10013            }
10014            s
10015        };
10016        // Half the quads go to the named graph, and the two halves are kept
10017        // lexically apart (`a…` / `b…`) so each lands in its own dictionary
10018        // chunks — the real layout on a quads file whose graphs use different
10019        // IRI namespaces, e.g. `cordis.rete`. Interleaving them instead would
10020        // put a term of both graphs in every chunk, and then no per-graph
10021        // access pattern could avoid faulting the whole dictionary.
10022        let quads: Vec<(String, String, String, bool)> = (0..2000u32)
10023            .map(|i| {
10024                let named = i % 2 == 0;
10025                let ns = if named { 'a' } else { 'b' };
10026                (
10027                    format!("<http://ex/{ns}/s/{i:04}>"),
10028                    "<http://ex/abstract>".to_string(),
10029                    format!("\"{ns}{} number {i}\"", payload(i)),
10030                    named,
10031                )
10032            })
10033            .collect();
10034        let mut db = DictionaryBuilder::new();
10035        for (s, p, o, _) in &quads {
10036            db.observe(s, p, o);
10037        }
10038        let dict = db.build();
10039        let mut def = GraphIndexBuilder::new().with_tile_budget(256);
10040        let mut g1 = GraphIndexBuilder::new().with_tile_budget(256);
10041        for (s, p, o, named) in &quads {
10042            let t = dict.encode(s, p, o).unwrap();
10043            if *named {
10044                g1.push(t);
10045            } else {
10046                def.push(t);
10047            }
10048        }
10049        let named = vec![("http://ex/g1".to_string(), g1.build())];
10050        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
10051
10052        let eager = Rete::open(&bytes).unwrap();
10053        let dict_len = eager.header().dictionary_len;
10054
10055        // `probe` runs one closure against a COLD lazy open and reports the
10056        // bytes it faulted beyond the open itself.
10057        let leaked: &'static [u8] = Box::leak(bytes.clone().into_boxed_slice());
10058        let probe = |f: &dyn Fn(&Rete) -> Vec<TermTriple>| -> (Vec<TermTriple>, u64) {
10059            let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
10060            let lazy = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
10061            let before = reader.bytes_read();
10062            let out = f(&lazy);
10063            assert!(!lazy.index_incomplete(), "a chunk fetch failed");
10064            (out, reader.bytes_read() - before)
10065        };
10066
10067        // 1. One bound subject in the named graph.
10068        let want = eager.query_in_graph(
10069            Some("http://ex/g1"),
10070            Some("<http://ex/a/s/0100>"),
10071            None,
10072            None,
10073        );
10074        assert_eq!(want.len(), 1, "fixture: expected exactly one match");
10075        let (got, pulled) = probe(&|r| {
10076            r.query_in_graph(
10077                Some("http://ex/g1"),
10078                Some("<http://ex/a/s/0100>"),
10079                None,
10080                None,
10081            )
10082        });
10083        assert_eq!(got, want, "lazy query_in_graph disagrees with eager");
10084        assert!(
10085            pulled * 4 < dict_len,
10086            "a 1-row query_in_graph faulted {pulled} B of a {dict_len} B dictionary — \
10087             it is prefetching the whole thing again"
10088        );
10089
10090        // 2. A graph with NOTHING to resolve must not fault the dictionary.
10091        //    Both shapes below used to pull every chunk before returning an
10092        //    empty answer, and `rete export` hits the second one first on a
10093        //    quads file whose default graph is empty — on `cordis.rete` that
10094        //    was 415 MB read and 2.5 GB of peak RSS to emit zero triples. What
10095        //    is left is the named-graph directory walk it takes to learn the
10096        //    graph is absent at all; that is index, not dictionary.
10097        let (empty, q_pulled) =
10098            probe(&|r| r.query_in_graph(Some("http://ex/nope"), None, None, None));
10099        assert!(empty.is_empty());
10100        let (_, d_pulled) = probe(&|r| {
10101            let mut n = 0usize;
10102            r.dump_each(Some("http://ex/nope"), |_, _, _| n += 1);
10103            assert_eq!(n, 0, "an absent graph yielded triples");
10104            Vec::new()
10105        });
10106        assert_eq!(
10107            q_pulled, d_pulled,
10108            "query_in_graph and dump_each disagree on what an absent graph costs"
10109        );
10110        assert!(
10111            q_pulled * 4 < dict_len,
10112            "an absent graph faulted {q_pulled} B of a {dict_len} B dictionary"
10113        );
10114
10115        // 3. Dumping ONE named graph costs that graph, not the whole file: the
10116        //    default graph's half of the dictionary must stay unfaulted.
10117        let want: Vec<TermTriple> = eager.dump(Some("http://ex/g1"));
10118        assert_eq!(want.len(), 1000);
10119        let (got, pulled) = probe(&|r| {
10120            let mut out = Vec::new();
10121            r.dump_each(Some("http://ex/g1"), |s, p, o| {
10122                out.push((s.to_string(), p.to_string(), o.to_string()))
10123            });
10124            out
10125        });
10126        assert_eq!(got, want, "lazy dump_each disagrees with eager dump");
10127        assert!(
10128            pulled * 3 < dict_len * 2,
10129            "dumping half the graph faulted {pulled} B of a {dict_len} B dictionary — \
10130             the other graph's chunks are being pulled in too"
10131        );
10132    }
10133
10134    /// Build a multi-chunk single-graph ranged file image for the eviction
10135    /// tests: 4000 distinct ~70-byte object literals overflow the 64 KiB
10136    /// dictionary chunk budget into several chunks, and one multi-KiB literal
10137    /// forces a large run/chunk boundary — the shape a tiny budget must survive.
10138    #[cfg(test)]
10139    fn build_multichunk_image() -> Vec<u8> {
10140        use crate::index::PermSet;
10141        let mut db = DictionaryBuilder::new();
10142        let mut triples: Vec<(String, String, String)> = (0..4000u32)
10143            .map(|i| {
10144                (
10145                    format!("<http://ex/s/{i:07}>"),
10146                    format!("<http://ex/p/{}>", i % 8),
10147                    format!("\"object literal number {i:07} with padding text to widen the term\""),
10148                )
10149            })
10150            .collect();
10151        triples.push((
10152            "<http://ex/s/big>".to_string(),
10153            "<http://ex/p/big>".to_string(),
10154            format!("\"{}\"", "Z".repeat(8000)),
10155        ));
10156        for (s, p, o) in &triples {
10157            db.observe(s, p, o);
10158        }
10159        let dict = db.build();
10160        let mut idx = GraphIndexBuilder::new()
10161            .with_tile_budget(64)
10162            .with_perms(PermSet::ALL);
10163        for (s, p, o) in &triples {
10164            idx.push(dict.encode(s, p, o).unwrap());
10165        }
10166        write_dataset(&dict, &idx.build(), &[], true, &[], 0)
10167    }
10168
10169    /// **Bounded-export phase 2: eviction is byte-transparent, and progress is
10170    /// guaranteed at any cap.** A cap far below the decompressed dictionary
10171    /// forces the chunk cache to evict during a full dump, yet the dump is
10172    /// byte-for-byte the unlimited-cap dump — the `Arc` handout keeps every
10173    /// chunk alive across its one decode, so eviction never drops bytes in use.
10174    /// A 1-byte cap (smaller than any single chunk, so every insert evicts
10175    /// itself immediately) still completes with identical output: it is the
10176    /// handout invariant, not residency, that makes a window make progress.
10177    #[test]
10178    fn tiny_dict_cache_evicts_yet_dump_is_byte_identical() {
10179        use crate::reader::SliceReader;
10180        let leaked: &'static [u8] = Box::leak(build_multichunk_image().into_boxed_slice());
10181        let dump = |cap: u64| -> (
10182            Vec<(String, String, String)>,
10183            crate::chunk_cache::CacheStats,
10184        ) {
10185            let rete =
10186                Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64).unwrap();
10187            rete.dict.set_cache_cap(cap);
10188            let mut out = Vec::new();
10189            rete.dump_filtered_each(None, None, None, None, |s, p, o| {
10190                out.push((s.to_string(), p.to_string(), o.to_string()))
10191            });
10192            assert!(
10193                !rete.index_incomplete(),
10194                "no lazy fault should fail at cap {cap}"
10195            );
10196            let stats = rete.dict_cache_stats();
10197            (out, stats)
10198        };
10199
10200        let (unlimited, us) = dump(u64::MAX);
10201        assert_eq!(unlimited.len(), 4001, "every triple dumped");
10202        assert_eq!(us.evictions, 0, "unlimited cap evicts nothing");
10203
10204        // A cap far below the decompressed dictionary: eviction happens, output
10205        // unchanged.
10206        let (tiny, ts) = dump(128 * 1024);
10207        assert_eq!(tiny, unlimited, "128 KiB-cap dump differs from unlimited");
10208        assert!(ts.evictions > 0, "a tiny cap must evict (stats {ts:?})");
10209
10210        // Progress guarantee: a 1-byte cap still completes, byte-identical.
10211        let (starved, ss) = dump(1);
10212        assert_eq!(starved, unlimited, "1-byte-cap dump differs from unlimited");
10213        assert!(
10214            ss.evictions > 0,
10215            "1-byte cap evicts every insert (stats {ss:?})"
10216        );
10217    }
10218
10219    /// **The budget pays for the open's fixed directories.** A ranged open holds
10220    /// one descriptor per dictionary chunk and per index tile for its whole
10221    /// life, outside every cache — 88 MB on the 14.75 GiB openaire-relation,
10222    /// 224 MB on epfl-infoscience. `set_memory_budget` used to give the caches
10223    /// the whole budget on top of that; it now measures the directories
10224    /// ([`Rete::fixed_overhead`]) and takes them out of the dictionary share, so
10225    /// caps + directories stay within the total. This checks the arithmetic on
10226    /// a many-chunk file, the 64 MiB floor, the unlimited pass-through, and that
10227    /// a dump under the capped budget keeps the dictionary cache within its
10228    /// (net) share.
10229    #[test]
10230    fn memory_budget_charges_the_fixed_directories() {
10231        use crate::reader::SliceReader;
10232        let leaked: &'static [u8] = Box::leak(build_multichunk_image().into_boxed_slice());
10233        let rete =
10234            Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64).unwrap();
10235
10236        let fixed = rete.fixed_overhead();
10237        // Every chunk and every tile has a descriptor; a multi-chunk dictionary
10238        // and a 64-triple tile budget make both directories non-trivial.
10239        assert!(
10240            fixed.dict_directory > 0 && fixed.index_directory > 0,
10241            "{fixed:?}"
10242        );
10243        assert_eq!(fixed.named_graphs, 0, "no named graphs in this file");
10244        assert_eq!(
10245            fixed.total(),
10246            fixed.dict_directory + fixed.index_directory + fixed.named_graphs
10247        );
10248
10249        // A budget well above the floor: the dictionary share is the plain
10250        // split less the directories, exactly.
10251        let total: u64 = 512 << 20;
10252        let applied = rete.set_memory_budget(Some(total));
10253        let plain = split_memory_budget(total);
10254        assert_eq!(applied.dict_cache, plain.dict_cache - fixed.total());
10255        assert_eq!(rete.dict_cache_cap(), applied.dict_cache);
10256        assert_eq!(applied.tile_cache, plain.tile_cache);
10257        assert_eq!(applied.block_cache, plain.block_cache);
10258        assert!(
10259            applied.dict_cache + applied.tile_cache + applied.block_cache + fixed.total() <= total,
10260            "caps + fixed directories exceed the budget: {applied:?} + {fixed:?} > {total}"
10261        );
10262
10263        // A budget whose share would fall under the floor keeps the floor.
10264        let applied = rete.set_memory_budget(Some(1 << 20));
10265        assert_eq!(applied.dict_cache, MIN_DICT_CACHE_CAP);
10266
10267        // Unlimited stays unlimited: nothing is subtracted from u64::MAX.
10268        let applied = rete.set_memory_budget(None);
10269        assert_eq!(applied.dict_cache, u64::MAX);
10270        assert_eq!(rete.dict_cache_cap(), u64::MAX);
10271
10272        // Under a cap smaller than the decompressed dictionary, a full dump
10273        // keeps the cache within it.
10274        let rete =
10275            Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64).unwrap();
10276        rete.dict.set_cache_cap(128 * 1024);
10277        let mut rows = 0usize;
10278        rete.dump_filtered_each(None, None, None, None, |_, _, _| rows += 1);
10279        assert_eq!(rows, 4001);
10280        let s = rete.dict_cache_stats();
10281        assert!(s.evictions > 0, "the cap must bind: {s:?}");
10282        // One oversized chunk may sit alone above the cap (the handout
10283        // invariant); otherwise residency is within it.
10284        assert!(
10285            s.entries <= 1 || s.resident_bytes <= 128 * 1024,
10286            "dictionary cache over its cap after the dump: {s:?}"
10287        );
10288    }
10289
10290    /// **Bounded-export phase 2: per-graph index release.** After a named
10291    /// graph's slot is exported, [`Rete::release_named_graph`] drops its decoded
10292    /// index (and its tiles) so a many-graph dump holds one graph's index at a
10293    /// time, not all of them. The graph re-opens lazily if touched again.
10294    #[test]
10295    fn releasing_a_named_graph_drops_and_reopens_its_index() {
10296        use crate::index::PermSet;
10297        use crate::reader::SliceReader;
10298        let mut db = DictionaryBuilder::new();
10299        let all: Vec<(String, String, String)> = (0..40u32)
10300            .map(|i| {
10301                (
10302                    format!("<http://ex/s{}>", i % 7),
10303                    format!("<http://ex/p{}>", i % 3),
10304                    format!("<http://ex/o{}>", i % 5),
10305                )
10306            })
10307            .collect();
10308        for (s, p, o) in &all {
10309            db.observe(s, p, o);
10310        }
10311        let dict = db.build();
10312        let mk = |take: fn(u32) -> bool| {
10313            let mut g = GraphIndexBuilder::new()
10314                .with_tile_budget(16)
10315                .with_perms(PermSet::ALL);
10316            for (i, (s, p, o)) in all.iter().enumerate() {
10317                if take(i as u32) {
10318                    g.push(dict.encode(s, p, o).unwrap());
10319                }
10320            }
10321            g.build()
10322        };
10323        let def = mk(|_| true);
10324        let g1 = mk(|i| i % 2 == 0);
10325        let g2 = mk(|i| i % 3 == 0);
10326        let image = write_dataset(
10327            &dict,
10328            &def,
10329            &[
10330                ("<http://ex/g1>".to_string(), g1),
10331                ("<http://ex/g2>".to_string(), g2),
10332            ],
10333            true,
10334            &[],
10335            0,
10336        );
10337        let leaked: &'static [u8] = Box::leak(image.into_boxed_slice());
10338        let mut rete =
10339            Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64).unwrap();
10340
10341        let opened = |r: &Rete| -> usize {
10342            match &r.named_graphs {
10343                NamedGraphsSlot::Lazy(l) => {
10344                    let mut n = 0;
10345                    l.for_each_opened(|_| n += 1);
10346                    n
10347                }
10348                _ => 0,
10349            }
10350        };
10351
10352        assert_eq!(opened(&rete), 0, "nothing opened before first touch");
10353        // Touching g1 opens its index.
10354        assert!(rete.graph_index("<http://ex/g1>").is_some());
10355        assert_eq!(opened(&rete), 1, "g1 opened");
10356        // Release it: the index is dropped.
10357        rete.release_named_graph("<http://ex/g1>");
10358        assert_eq!(opened(&rete), 0, "g1 released");
10359        // Releasing an unknown / already-released graph is a no-op.
10360        rete.release_named_graph("<http://ex/absent>");
10361        rete.release_named_graph("<http://ex/g1>");
10362        assert_eq!(opened(&rete), 0);
10363        // The graph re-opens lazily on the next access.
10364        assert!(rete.graph_index("<http://ex/g1>").is_some());
10365        assert_eq!(opened(&rete), 1, "g1 re-opened after release");
10366    }
10367
10368    /// **The filtered-dump correctness invariant** (#117 point 1): for every
10369    /// graph and every one of the eight bound/unbound shapes,
10370    /// `dump_filtered_each` must return *exactly* the quads `dump_each` returns,
10371    /// filtered — no more, and above all no fewer.
10372    ///
10373    /// "No fewer" is the whole risk. The speedup comes from *not fetching* tiles
10374    /// a synopsis says cannot match, and a pruning bug does not crash, it
10375    /// silently returns a shorter answer. So the oracle here is the unfiltered
10376    /// dump with a plain Rust `retain` over it — the thing the filter is meant
10377    /// to be a cheaper spelling of — and it is checked on every open path, since
10378    /// the eager path prunes with a different code path than the lazy one.
10379    ///
10380    /// It runs on a `PermSet::CORE` (`--permutations 3`) file as well as a
10381    /// six-permutation one. Every shape routes inside CORE — `perm_routing_never_leaves_core`
10382    /// proves the routing table does — so the two must agree row for row, and if
10383    /// a future shape ever routed outside CORE this test fails on the three-perm
10384    /// build rather than silently returning nothing from an absent section.
10385    #[test]
10386    fn filtered_dump_is_exactly_the_unfiltered_dump_filtered() {
10387        use crate::index::PermSet;
10388        use crate::reader::SliceReader;
10389
10390        let terms = |i: u32| {
10391            (
10392                format!("<http://ex/s{}>", i % 11),
10393                format!("<http://ex/p{}>", i % 3),
10394                // Mix IRIs and literals in the object column: a literal lives in
10395                // a different dictionary section than a node, and the object
10396                // filter has to resolve through the right one.
10397                if i.is_multiple_of(4) {
10398                    format!("\"lit {}\"@en", i % 5)
10399                } else {
10400                    format!("<http://ex/o{}>", i % 7)
10401                },
10402            )
10403        };
10404        let build = |perms: PermSet| {
10405            let mut db = DictionaryBuilder::new();
10406            let all: Vec<_> = (0..90u32).map(terms).collect();
10407            for (s, p, o) in &all {
10408                db.observe(s, p, o);
10409            }
10410            let dict = db.build();
10411            let mut def = GraphIndexBuilder::new()
10412                .with_tile_budget(16)
10413                .with_perms(perms);
10414            let mut g1 = GraphIndexBuilder::new()
10415                .with_tile_budget(16)
10416                .with_perms(perms);
10417            for (i, (s, p, o)) in all.iter().enumerate() {
10418                let t = dict.encode(s, p, o).unwrap();
10419                // Deliberately overlapping, not disjoint: every second triple is
10420                // in both graphs, so a filter that leaked across graphs would
10421                // still look plausible on counts alone.
10422                if i % 2 == 0 {
10423                    g1.push(t);
10424                }
10425                def.push(t);
10426            }
10427            write_dataset(
10428                &dict,
10429                &def.build(),
10430                &[("<http://ex/g1>".to_string(), g1.build())],
10431                true,
10432                &[],
10433                0,
10434            )
10435        };
10436
10437        let shapes: Vec<(Option<&str>, Option<&str>, Option<&str>)> = vec![
10438            (None, None, None),
10439            (Some("<http://ex/s3>"), None, None),
10440            (None, Some("<http://ex/p1>"), None),
10441            (None, None, Some("<http://ex/o5>")),
10442            (None, None, Some("\"lit 0\"@en")),
10443            (Some("<http://ex/s3>"), Some("<http://ex/p0>"), None),
10444            (Some("<http://ex/s4>"), None, Some("<http://ex/o4>")),
10445            (None, Some("<http://ex/p1>"), Some("<http://ex/o5>")),
10446            (
10447                Some("<http://ex/s3>"),
10448                Some("<http://ex/p0>"),
10449                Some("<http://ex/o3>"),
10450            ),
10451            // Terms the dictionary has never seen, in each position: these must
10452            // return nothing, not everything (an unresolvable bound term must
10453            // not silently degrade to "unbound").
10454            (Some("<http://ex/nope>"), None, None),
10455            (None, Some("<http://ex/nope>"), None),
10456            (None, None, Some("<http://ex/nope>")),
10457        ];
10458
10459        let mut any_pruned = false;
10460        for perms in [PermSet::ALL, PermSet::CORE] {
10461            let image = build(perms);
10462            let leaked: &'static [u8] = Box::leak(image.clone().into_boxed_slice());
10463            let opens: Vec<(&str, Rete)> = vec![
10464                ("resident", Rete::open(&image).unwrap()),
10465                (
10466                    "ranged",
10467                    Rete::open_ranged(&SliceReader::new(leaked)).unwrap(),
10468                ),
10469                (
10470                    "lazy",
10471                    Rete::open_ranged_lazy_with(SliceReader::new(leaked), HEADER_LEN as u64)
10472                        .unwrap(),
10473                ),
10474            ];
10475            for graph in [None, Some("<http://ex/g1>"), Some("<http://ex/absent>")] {
10476                // The oracle: the unfiltered dump, filtered in Rust.
10477                let full: Vec<TermTriple> = {
10478                    let mut v = Vec::new();
10479                    Rete::open(&image).unwrap().dump_each(graph, |s, p, o| {
10480                        v.push((s.to_string(), p.to_string(), o.to_string()))
10481                    });
10482                    v.sort();
10483                    v
10484                };
10485                for &(s, p, o) in &shapes {
10486                    let mut want = full.clone();
10487                    want.retain(|(ts, tp, to)| {
10488                        s.is_none_or(|x| x == ts)
10489                            && p.is_none_or(|x| x == tp)
10490                            && o.is_none_or(|x| x == to)
10491                    });
10492                    for (path, rete) in &opens {
10493                        let mut got = Vec::new();
10494                        rete.dump_filtered_each(graph, s, p, o, |s, p, o| {
10495                            got.push((s.to_string(), p.to_string(), o.to_string()))
10496                        });
10497                        got.sort();
10498                        assert_eq!(
10499                            got,
10500                            want,
10501                            "{path}/{perms:?} graph={graph:?} pattern={:?} — filtered dump \
10502                             is not the unfiltered dump filtered",
10503                            (s, p, o)
10504                        );
10505                        assert!(!rete.index_incomplete(), "{path}: a fetch failed");
10506                    }
10507                    // …and the plan must agree with what the dump actually did:
10508                    // an empty answer means either no scan at all, or a scan
10509                    // whose admitted tiles simply held nothing.
10510                    let plan = opens[2].1.dump_plan(graph, s, p, o);
10511                    if graph == Some("<http://ex/absent>")
10512                        || [s, p, o].contains(&Some("<http://ex/nope>"))
10513                    {
10514                        assert!(
10515                            plan.scan.is_none(),
10516                            "an unmatchable dump still planned a scan: {:?}",
10517                            (graph, s, p, o)
10518                        );
10519                    } else {
10520                        let scan = plan.scan.expect("a matchable dump plans a scan");
10521                        assert!(
10522                            PermSet::CORE.contains(scan.permutation),
10523                            "a dump routed to {:?}, outside PermSet::CORE — a \
10524                             --permutations 3 file has no such section",
10525                            scan.permutation
10526                        );
10527                        assert!(scan.tiles_admitted <= scan.tiles_routed);
10528                        assert!(scan.tiles_routed <= scan.tiles_total);
10529                        assert!(scan.tile_bytes <= scan.section_bytes);
10530                        if scan.tiles_admitted < scan.tiles_total {
10531                            any_pruned = true;
10532                        }
10533                    }
10534                }
10535            }
10536        }
10537        assert!(
10538            any_pruned,
10539            "no shape pruned a single tile — the fixture is too small to be \
10540             measuring anything"
10541        );
10542    }
10543
10544    /// The point of the filter: a predicate-scoped dump of a lazily range-read
10545    /// file must **fetch less** than the same graph's full dump, not merely
10546    /// return less.
10547    ///
10548    /// The fixture is the shape the win shows on — many tiles, and a predicate
10549    /// whose triples live in a minority of them — so the assertion can be about
10550    /// bytes rather than about the code path being taken on faith. Measured on
10551    /// the real thing (`cordis.rete`, 801 MB): one predicate of one named graph
10552    /// read 16.0 MB where the graph's full dump read 375.8 MB.
10553    #[test]
10554    fn a_predicate_scoped_dump_fetches_less_than_the_graph() {
10555        use crate::reader::{CountingReader, SliceReader};
10556
10557        // 200 subjects × 20 predicates, tiled small so there are many tiles, and
10558        // one distinct object per cell so that a predicate slice needs a
10559        // twentieth of the object dictionary rather than all of it — otherwise
10560        // the dictionary, which this change does not prune, swamps the index
10561        // saving the test is about.
10562        let mut db = DictionaryBuilder::new();
10563        let mut all = Vec::new();
10564        for s in 0..200u32 {
10565            for p in 0..20u32 {
10566                let t = (
10567                    format!("<http://ex/s/{s:04}>"),
10568                    format!("<http://ex/p/{p:02}>"),
10569                    format!("\"object {s:04}/{p:02}\""),
10570                );
10571                db.observe(&t.0, &t.1, &t.2);
10572                all.push(t);
10573            }
10574        }
10575        let dict = db.build();
10576        let mut ib = GraphIndexBuilder::new().with_tile_budget(64);
10577        for (s, p, o) in &all {
10578            ib.push(dict.encode(s, p, o).unwrap());
10579        }
10580        let image = write_dataset(&dict, &ib.build(), &[], false, &[], 0);
10581        let leaked: &'static [u8] = Box::leak(image.clone().into_boxed_slice());
10582
10583        // Bytes faulted past the open, for one dump.
10584        let probe = |s: Option<&str>, p: Option<&str>, o: Option<&str>| -> (usize, u64) {
10585            let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
10586            let lazy = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
10587            let before = reader.bytes_read();
10588            let mut n = 0usize;
10589            lazy.dump_filtered_each(None, s, p, o, |_, _, _| n += 1);
10590            assert!(!lazy.index_incomplete());
10591            (n, reader.bytes_read() - before)
10592        };
10593
10594        let (full_rows, full_bytes) = probe(None, None, None);
10595        let (pred_rows, pred_bytes) = probe(None, Some("<http://ex/p/07>"), None);
10596        let (subj_rows, subj_bytes) = probe(Some("<http://ex/s/0021>"), None, None);
10597        assert_eq!(full_rows, 4000);
10598        assert_eq!(pred_rows, 200, "one predicate covers every subject once");
10599        assert_eq!(subj_rows, 20);
10600        // True in any build: a slice fetches strictly less than the graph.
10601        assert!(
10602            pred_bytes < full_bytes && subj_bytes < full_bytes,
10603            "a slice fetched {pred_bytes} / {subj_bytes} B where the whole graph fetched \
10604             {full_bytes} B — the scan is not being pruned at all"
10605        );
10606        // The SIZE of the win is a property of the FILE, not of this change: it is
10607        // the index share of what a dump reads, and the dictionary — which no
10608        // filter prunes — is the rest. With the default `compression` feature the
10609        // dictionary chunk bodies are zstd-compressed and the slice costs about a
10610        // tenth of the graph (measured here: 1,565 B for the predicate and 864 B
10611        // for the subject, against 15,714 B). Built `--no-default-features` those
10612        // same chunks are stored RAW, so resolving 200 rows' terms dominates and
10613        // the identical, identically-pruned scan costs 22,225 B of 40,165 — 55%.
10614        // Asserting the strong ratio unconditionally would be asserting that zstd
10615        // is enabled; the index-side prune is checked below and holds either way.
10616        #[cfg(feature = "compression")]
10617        assert!(
10618            pred_bytes * 4 < full_bytes && subj_bytes * 4 < full_bytes,
10619            "a 1-in-20 predicate slice fetched {pred_bytes} B and one subject \
10620             {subj_bytes} B where the whole graph fetched {full_bytes} B"
10621        );
10622
10623        // And the plan predicted the index side of it without fetching a tile.
10624        let reader = std::sync::Arc::new(CountingReader::new(SliceReader::new(leaked)));
10625        let lazy = Rete::open_ranged_lazy_with(reader.clone(), HEADER_LEN as u64).unwrap();
10626        let before = reader.bytes_read();
10627        let plan = lazy
10628            .dump_plan(None, None, Some("<http://ex/p/07>"), None)
10629            .scan
10630            .expect("a known predicate plans a scan");
10631        assert!(
10632            reader.bytes_read() - before < 64 * 1024,
10633            "planning fetched {} B — it is supposed to read directories, not tiles",
10634            reader.bytes_read() - before
10635        );
10636        assert!(plan.tiles_admitted < plan.tiles_total);
10637        // The index side on its own, with the dictionary's share taken out: one
10638        // predicate of twenty routes to a handful of POS tiles. (Measured here:
10639        // the whole dump faults 15,714 B, the predicate slice 1,565 B and one
10640        // subject 864 B — a 10x and an 18x, most of what is left being the
10641        // dictionary this change does not prune.)
10642        assert!(
10643            plan.tile_bytes * 5 < plan.section_bytes,
10644            "one predicate of twenty planned {} B of a {} B section",
10645            plan.tile_bytes,
10646            plan.section_bytes
10647        );
10648    }
10649
10650    #[test]
10651    fn query_quads_tags_every_graph() {
10652        let mut db = DictionaryBuilder::new();
10653        for (s, p, o) in [("Alice", "knows", "Bob"), ("Alice", "knows", "Dave")] {
10654            db.observe(s, p, o);
10655        }
10656        let dict = db.build();
10657        let mut def = GraphIndexBuilder::new();
10658        def.push(dict.encode("Alice", "knows", "Bob").unwrap());
10659        let mut g1 = GraphIndexBuilder::new();
10660        g1.push(dict.encode("Alice", "knows", "Dave").unwrap());
10661        let named = vec![("http://ex/g1".to_string(), g1.build())];
10662        let bytes = write_dataset(&dict, &def.build(), &named, true, &[], 0);
10663        let rete = Rete::open(&bytes).unwrap();
10664
10665        // `Alice knows ?` spans both graphs; each match carries its graph tag.
10666        let quads = rete.query_quads(Some("Alice"), Some("knows"), None);
10667        assert_eq!(quads.len(), 2);
10668        assert_eq!(
10669            quads[0],
10670            (("Alice".into(), "knows".into(), "Bob".into()), None)
10671        );
10672        assert_eq!(
10673            quads[1],
10674            (
10675                ("Alice".into(), "knows".into(), "Dave".into()),
10676                Some("http://ex/g1".to_string())
10677            )
10678        );
10679
10680        // A bound term absent from the dictionary yields nothing, in any graph.
10681        assert!(rete.query_quads(Some("Nobody"), None, None).is_empty());
10682    }
10683
10684    #[test]
10685    fn pyramid_meta_round_trips_in_file() {
10686        let rete = Rete::open(&build_image()).unwrap();
10687        let pyr = rete.pyramid().expect("file has a pyramid");
10688        // Summary covers all 3 triples by count; tiles are not stored in v0.
10689        let total: u32 = pyr.summary.iter().map(|e| e.count).sum();
10690        assert_eq!(total, 3);
10691        assert!(!pyr.summary.is_empty());
10692        assert!(pyr.tiles.is_empty());
10693    }
10694
10695    #[test]
10696    fn schema_pyramid_round_trips_through_file_index_free() {
10697        use crate::reader::{CountingReader, SliceReader};
10698        let sub = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";
10699        let q = |s: &str, p: &str, o: &str| {
10700            (s.to_string(), p.to_string(), o.to_string(), None::<String>)
10701        };
10702        // Astronomer ⊑ Scientist ⊑ Person ⊑ Agent, instances at the leaves.
10703        let quads = vec![
10704            q("<a>", RDF_TYPE, "<Astronomer>"),
10705            q("<b>", RDF_TYPE, "<Astronomer>"),
10706            q("<c>", RDF_TYPE, "<Person>"),
10707            q("<Astronomer>", sub, "<Scientist>"),
10708            q("<Scientist>", sub, "<Person>"),
10709            q("<Person>", sub, "<Agent>"),
10710            q("<a>", "<knows>", "<b>"),
10711            q("<b>", "<knows>", "<c>"),
10712        ];
10713        let (bytes, _) =
10714            crate::ingest::assemble_dataset_with_opts(quads, true, false, None, |_, _| Vec::new());
10715
10716        // The v2 schema pyramid round-trips through the built file.
10717        let rete = Rete::open(&bytes).unwrap();
10718        let pyr = rete.pyramid().expect("pyramid present");
10719        assert!(!pyr.level_rollups.is_empty(), "schema pyramid shipped");
10720        assert!(pyr
10721            .class_hierarchy
10722            .iter()
10723            .any(|n| n.class == "<Agent>" && n.depth == 0));
10724
10725        // It reads index-free: a SummaryView open never touches the index section.
10726        let r = CountingReader::new(SliceReader::new(&bytes));
10727        let view = SummaryView::open_ranged(&r).unwrap().unwrap();
10728        assert!(view.level_count() >= 2, "multi-level pyramid");
10729        let coarse = view.level_rollup(0).unwrap();
10730        assert!(
10731            coarse.classes.iter().any(|(c, _)| c == "<Agent>"),
10732            "coarsest level rolls up to the root Agent"
10733        );
10734        let h = Header::from_bytes(&bytes).unwrap();
10735        assert!(
10736            r.bytes_read() <= bytes.len() as u64 - h.root_dir_len,
10737            "summary read {} bytes; the {}-byte index section must be skipped",
10738            r.bytes_read(),
10739            h.root_dir_len
10740        );
10741    }
10742
10743    #[test]
10744    fn predicate_totals_from_summary_only() {
10745        use crate::reader::SliceReader;
10746        // build_image: 2 `knows` triples + 1 `age` triple.
10747        let bytes = build_image();
10748        let reader = SliceReader::new(&bytes);
10749        let view = SummaryView::open_ranged(&reader).unwrap().unwrap();
10750        assert_eq!(view.predicate_total("knows"), 2);
10751        assert_eq!(view.predicate_total("age"), 1);
10752        assert_eq!(view.predicate_total("missing"), 0);
10753        let totals = view.predicate_totals();
10754        assert_eq!(totals[0], ("knows".to_string(), 2)); // sorted by count desc
10755    }
10756
10757    #[test]
10758    fn query_patterns_resolve_to_terms() {
10759        let rete = Rete::open(&build_image()).unwrap();
10760
10761        // All triples.
10762        assert_eq!(rete.query(None, None, None).len(), 3);
10763
10764        // Subject bound.
10765        let mut alice = rete.query(Some("Alice"), None, None);
10766        alice.sort();
10767        assert_eq!(
10768            alice,
10769            vec![
10770                ("Alice".into(), "age".into(), "30".into()),
10771                ("Alice".into(), "knows".into(), "Bob".into()),
10772            ]
10773        );
10774
10775        // Predicate bound.
10776        assert_eq!(rete.query(None, Some("knows"), None).len(), 2);
10777
10778        // Full triple, present and absent.
10779        assert_eq!(
10780            rete.query(Some("Bob"), Some("knows"), Some("Carol")),
10781            vec![("Bob".into(), "knows".into(), "Carol".into())]
10782        );
10783        assert!(rete.query(Some("Nobody"), None, None).is_empty());
10784        assert!(rete.query(None, Some("likes"), None).is_empty());
10785    }
10786
10787    #[test]
10788    fn query_provenance_reports_terms_ids_sections_and_index_choice() {
10789        let bytes = build_image();
10790        let rete = Rete::open(&bytes).unwrap();
10791
10792        let mut matches = rete.query_with_provenance(None, Some("knows"), None);
10793        matches.sort_by(|a, b| a.terms.cmp(&b.terms));
10794
10795        assert_eq!(matches.len(), 2);
10796        assert_eq!(
10797            matches[0].terms,
10798            ("Alice".into(), "knows".into(), "Bob".into())
10799        );
10800        assert_eq!(
10801            matches[0].ids,
10802            rete.dictionary().encode("Alice", "knows", "Bob").unwrap()
10803        );
10804        assert_eq!(matches[0].graph.as_deref(), None);
10805        assert_eq!(
10806            matches[0].matched_pattern,
10807            (None, Some(matches[0].ids.1), None)
10808        );
10809        assert_eq!(
10810            matches[0].index_permutation,
10811            crate::index::IndexPermutation::Pos
10812        );
10813
10814        let h = rete.header();
10815        assert_eq!(matches[0].dictionary_range.offset, h.dictionary_offset);
10816        assert_eq!(matches[0].dictionary_range.len, h.dictionary_len);
10817        assert_eq!(matches[0].index_range.offset, h.root_dir_offset);
10818        assert_eq!(matches[0].index_range.len, h.root_dir_len);
10819        assert!(
10820            matches[0].index_section_range.offset > h.root_dir_offset,
10821            "POS is section 1, so its payload starts after the container header and SPO payload"
10822        );
10823        assert!(matches[0].index_section_range.len > 0);
10824        assert!(matches[0].index_section_range.end() <= matches[0].index_range.end());
10825        assert!(matches[0].index_section_range.len < matches[0].index_range.len);
10826        assert_eq!(
10827            matches[0].pyramid_range.as_ref().map(|r| (r.offset, r.len)),
10828            Some((h.pyramid_meta_offset, h.pyramid_meta_len))
10829        );
10830        // Tiled (v0.2) files report the physical tile holding the match; its
10831        // compressed byte range nests inside the selected section payload.
10832        let tile_range = matches[0].tile_range.expect("tiled file reports a tile");
10833        assert!(matches[0]
10834            .tile
10835            .as_deref()
10836            .unwrap()
10837            .starts_with(matches[0].index_permutation.name()));
10838        assert!(matches[0].index_section_range.offset <= tile_range.offset);
10839        assert!(tile_range.end() <= matches[0].index_section_range.end());
10840    }
10841
10842    /// Build an in-memory `.rete` with `n` labeled subjects: each carries an
10843    /// `rdfs:label` literal drawn from `WORDS` (a word prefix selects ~1/|WORDS|
10844    /// of them) plus one extra edge so the subject has a degree to rank by.
10845    fn build_labeled(n: usize) -> Vec<u8> {
10846        const LABEL: &str = "<http://www.w3.org/2000/01/rdf-schema#label>";
10847        const WORDS: &[&str] = &[
10848            "alanine",
10849            "benzene",
10850            "glucose",
10851            "dextrose",
10852            "ethanol",
10853            "formate",
10854            "heptane",
10855            "isoleucine",
10856        ];
10857        let triples: Vec<(String, String, String)> = (0..n)
10858            .flat_map(|i| {
10859                let s = format!("<http://ex/e{i}>");
10860                let w = WORDS[i % WORDS.len()];
10861                [
10862                    (s.clone(), LABEL.to_string(), format!("\"{w}-{i:06}\"")),
10863                    (
10864                        s,
10865                        "<http://ex/p>".to_string(),
10866                        format!("<http://ex/c{}>", i % 64),
10867                    ),
10868                ]
10869            })
10870            .collect();
10871        let mut db = DictionaryBuilder::new();
10872        for (s, p, o) in &triples {
10873            db.observe(s, p, o);
10874        }
10875        let dict = db.build();
10876        let ids: Vec<(u32, u32, u32)> = triples
10877            .iter()
10878            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
10879            .collect();
10880        let mut ib = GraphIndexBuilder::new();
10881        for &t in &ids {
10882            ib.push(t);
10883        }
10884        let (meta, levels) = build_pyramid_meta(&dict, &ids, DEFAULT_TILE_BUDGET);
10885        write_file(&dict, &ib.build(), false, &meta, levels)
10886    }
10887
10888    #[test]
10889    fn prefix_search_matches_a_filter_scan() {
10890        // 800 < the 8192 label-index cap, so the index is COMPLETE — every label
10891        // is present and the two paths must return the exact same subject set.
10892        let bytes = build_labeled(800);
10893        let rete = Rete::open(&bytes).unwrap();
10894        let idx_subjects: std::collections::BTreeSet<String> = rete
10895            .prefix_search("glucose", 10_000)
10896            .into_iter()
10897            .map(|(_label, subject)| subject)
10898            .collect();
10899        // The same selection via a SPARQL FILTER scan over every label literal.
10900        let q = "SELECT ?s WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?l \
10901                 FILTER(STRSTARTS(LCASE(?l), \"glucose\")) }";
10902        let crate::QueryOutput::Select(_, rows) = crate::eval_query(&rete, q).unwrap() else {
10903            panic!("expected SELECT");
10904        };
10905        let scan_subjects: std::collections::BTreeSet<String> =
10906            rows.iter().map(|r| r.get("s").cloned().unwrap()).collect();
10907        assert_eq!(idx_subjects, scan_subjects, "index agrees with the scan");
10908        assert_eq!(
10909            idx_subjects.len(),
10910            100,
10911            "800/8 words = 100 glucose-* labels"
10912        );
10913    }
10914
10915    /// A legacy (pyramid-block) label search resolves its hits' subjects in one
10916    /// batched dictionary read on a lazy open — the label-search twin of the
10917    /// full-text hits' batch — instead of one dependent read per chunk.
10918    #[test]
10919    fn legacy_label_hits_resolve_in_one_batched_read() {
10920        use std::sync::{Arc, Mutex};
10921        type CallLog = Arc<Mutex<Vec<Vec<(u64, u64)>>>>;
10922        #[derive(Clone)]
10923        struct Calls(Arc<Vec<u8>>, CallLog);
10924        impl crate::RangeReader for Calls {
10925            fn len(&self) -> u64 {
10926                self.0.len() as u64
10927            }
10928            fn read_at(&self, offset: u64, len: u64) -> std::io::Result<Vec<u8>> {
10929                self.1.lock().unwrap().push(vec![(offset, len)]);
10930                crate::SliceReader::new(&self.0).read_at(offset, len)
10931            }
10932            fn read_many(&self, ranges: &[(u64, u64)]) -> std::io::Result<Vec<Vec<u8>>> {
10933                self.1.lock().unwrap().push(ranges.to_vec());
10934                let r = crate::SliceReader::new(&self.0);
10935                ranges.iter().map(|&(o, l)| r.read_at(o, l)).collect()
10936            }
10937        }
10938        let bytes = build_labeled(20_000);
10939        let h = Header::from_bytes(&bytes).unwrap();
10940        let (d0, dl) = (h.dictionary_offset, h.dictionary_len);
10941        let want = Rete::open(&bytes).unwrap().prefix_search("glucose", 1000);
10942        assert_eq!(want.len(), 1000);
10943        for search_view in [false, true] {
10944            let calls = Calls(Arc::new(bytes.clone()), Arc::new(Mutex::new(Vec::new())));
10945            let got = if search_view {
10946                let view = SearchView::open_ranged_with(calls.clone(), HEADER_LEN as u64).unwrap();
10947                calls.1.lock().unwrap().clear();
10948                view.prefix_search("glucose", 1000)
10949            } else {
10950                let rete = Rete::open_ranged_lazy_with(calls.clone(), HEADER_LEN as u64).unwrap();
10951                calls.1.lock().unwrap().clear();
10952                rete.prefix_search("glucose", 1000)
10953            };
10954            assert_eq!(got, want, "search view = {search_view}");
10955            let dict_reads = calls
10956                .1
10957                .lock()
10958                .unwrap()
10959                .iter()
10960                .filter(|c| c.iter().any(|&(o, l)| o < d0 + dl && d0 < o + l))
10961                .count();
10962            assert_eq!(dict_reads, 1, "search view = {search_view}");
10963        }
10964    }
10965
10966    /// Latency: the binary-search label index vs the FILTER scan it replaces.
10967    /// Ignored by default (timing-sensitive); run with
10968    /// `cargo test -p rete-core -- --ignored --nocapture bench_prefix_search`.
10969    #[test]
10970    #[ignore]
10971    fn bench_prefix_search_vs_filter_scan() {
10972        use std::time::Instant;
10973        let n = 6000; // < the 8192 cap, so both paths return identical sets
10974        let bytes = build_labeled(n);
10975        let rete = Rete::open(&bytes).unwrap();
10976        let q = "SELECT ?s WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?l \
10977                 FILTER(STRSTARTS(LCASE(?l), \"glucose\")) }";
10978        let reps = 200;
10979        let idx_n = rete.prefix_search("glucose", 100_000).len();
10980        let t = Instant::now();
10981        for _ in 0..reps {
10982            std::hint::black_box(rete.prefix_search("glucose", 100_000));
10983        }
10984        let idx_ms = t.elapsed().as_secs_f64() * 1000.0 / reps as f64;
10985        let t = Instant::now();
10986        for _ in 0..reps {
10987            let _ = std::hint::black_box(crate::eval_query(&rete, q).unwrap());
10988        }
10989        let scan_ms = t.elapsed().as_secs_f64() * 1000.0 / reps as f64;
10990        println!(
10991            "label prefix search over {n} labeled subjects ({idx_n} matches): \
10992             index {idx_ms:.4} ms vs FILTER scan {scan_ms:.3} ms ({:.0}× faster)",
10993            scan_ms / idx_ms
10994        );
10995    }
10996
10997    /// Latency: the TEXT_INDEX word search vs the `FILTER(CONTAINS(?l, …))` scan
10998    /// it replaces. Ignored by default (timing-sensitive); run with
10999    /// `cargo test -p rete-core -- --ignored --nocapture bench_text_search`.
11000    #[test]
11001    #[ignore]
11002    fn bench_text_search_vs_contains_scan() {
11003        use std::time::Instant;
11004        const LABEL: &str = "<http://www.w3.org/2000/01/rdf-schema#label>";
11005        const WORDS: &[&str] = &[
11006            "alanine",
11007            "benzene",
11008            "glucose",
11009            "dextrose",
11010            "ethanol",
11011            "formate",
11012            "heptane",
11013            "isoleucine",
11014        ];
11015        let n = 6000;
11016        let triples: Vec<(String, String, String)> = (0..n)
11017            .map(|i| {
11018                (
11019                    format!("<http://ex/e{i}>"),
11020                    LABEL.to_string(),
11021                    format!("\"{} sample number {i:06}\"", WORDS[i % WORDS.len()]),
11022                )
11023            })
11024            .collect();
11025        let mut db = DictionaryBuilder::new();
11026        for (s, p, o) in &triples {
11027            db.observe(s, p, o);
11028        }
11029        let dict = db.build();
11030        let ids: Vec<(u32, u32, u32)> = triples
11031            .iter()
11032            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
11033            .collect();
11034        let mut ib = GraphIndexBuilder::new();
11035        for &t in &ids {
11036            ib.push(t);
11037        }
11038        let ti = compute_text_index(&dict, &ids);
11039        let bytes = write_dataset_with_metadata(&dict, &ib.build(), &[], false, &[], 0, &[], &ti);
11040        let rete = Rete::open(&bytes).unwrap();
11041
11042        let q = "SELECT ?s WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?l \
11043                 FILTER(CONTAINS(LCASE(?l), \"glucose\")) }";
11044        let reps = 200;
11045        let idx_n = rete.text_search(&["glucose"], None, 100_000).len();
11046        let t = Instant::now();
11047        for _ in 0..reps {
11048            std::hint::black_box(rete.text_search(&["glucose"], None, 100_000));
11049        }
11050        let idx_ms = t.elapsed().as_secs_f64() * 1000.0 / reps as f64;
11051        let t = Instant::now();
11052        for _ in 0..reps {
11053            let _ = std::hint::black_box(crate::eval_query(&rete, q).unwrap());
11054        }
11055        let scan_ms = t.elapsed().as_secs_f64() * 1000.0 / reps as f64;
11056        println!(
11057            "text search over {n} literals ({idx_n} matches): \
11058             index {idx_ms:.4} ms vs FILTER(CONTAINS) scan {scan_ms:.3} ms ({:.0}× faster)",
11059            scan_ms / idx_ms
11060        );
11061    }
11062
11063    /// Operational debugging harness for a REAL on-disk file (ignored; driven
11064    /// by env vars): step-by-step dump of a bound (p, o) POS routing.
11065    ///   RETE_DEBUG_FILE=<path.rete> RETE_DEBUG_P=<iri> RETE_DEBUG_O=<iri>
11066    #[test]
11067    #[ignore = "operational tool, driven by RETE_DEBUG_* env vars"]
11068    fn debug_bound_po_routing() {
11069        struct FR(std::fs::File);
11070        impl crate::RangeReader for FR {
11071            fn len(&self) -> u64 {
11072                self.0.metadata().map(|m| m.len()).unwrap_or(0)
11073            }
11074            fn read_at(&self, offset: u64, len: u64) -> std::io::Result<Vec<u8>> {
11075                use std::os::unix::fs::FileExt;
11076                let mut buf = vec![0u8; len as usize];
11077                self.0.read_exact_at(&mut buf, offset)?;
11078                Ok(buf)
11079            }
11080        }
11081        let path = std::env::var("RETE_DEBUG_FILE").expect("RETE_DEBUG_FILE");
11082        let p_iri = std::env::var("RETE_DEBUG_P").expect("RETE_DEBUG_P");
11083        let o_iri = std::env::var("RETE_DEBUG_O").expect("RETE_DEBUG_O");
11084        let rete = Rete::open_ranged_lazy_with(
11085            std::sync::Arc::new(FR(std::fs::File::open(&path).unwrap())),
11086            HEADER_LEN as u64,
11087        )
11088        .unwrap();
11089        let pid = rete.dict.predicate_id(&p_iri).expect("p resolves");
11090        let oid = rete.dict.object_id(&o_iri).expect("o resolves");
11091        eprintln!("pid={pid} oid={oid}");
11092        let pattern = (None, Some(pid), Some(oid));
11093        let perm = GraphIndex::best_permutation(pattern);
11094        eprintln!("best_permutation = {}", perm.name());
11095        let si = perm.section_index();
11096        let tiles = &rete.index.sections[si];
11097        eprintln!("section {} tiles = {}", perm.name(), tiles.len());
11098        let [pa, pb, pc] = perm.order_pattern(pattern);
11099        eprintln!("permuted pattern pa={pa:?} pb={pb:?} pc={pc:?}");
11100        let (start, end) = rete.index.tile_span(si, pa);
11101        eprintln!("tile_span = [{start}, {end}) -> {} tiles", end - start);
11102        let mut admitted = 0usize;
11103        for (ti, t) in tiles.iter().enumerate().take(end).skip(start) {
11104            if t.syn_admits(pb, pc) {
11105                admitted += 1;
11106                if admitted <= 10 {
11107                    let (lo, hi) = t.leading_range();
11108                    eprintln!("  admit tile {ti}: a=[{lo},{hi}] syn={:?}", t.syn);
11109                }
11110            }
11111        }
11112        eprintln!("admitted {admitted} tile(s) by synopsis");
11113        let n = rete.index.scan_iter(pattern).count();
11114        eprintln!("scan_iter matches = {n}");
11115        let hi_res = rete.query(None, Some(&p_iri), Some(&o_iri));
11116        eprintln!("high-level query matches = {}", hi_res.len());
11117    }
11118
11119    /// The header segment's LABEL and the span it describes must agree. They
11120    /// did not from v0.3 (which grew the header to 1 KiB) until this test: the
11121    /// label still said "fixed 128 bytes", the pre-v0.3 size, while the segment
11122    /// covered `HEADER_LEN` = 1024 — and that label is exactly what the file
11123    /// explorer shows someone inspecting a real file. Reading the size back out
11124    /// of the string is the point: it fails if anyone retypes the number
11125    /// instead of deriving it from `HEADER_LEN`.
11126    #[test]
11127    fn layout_header_label_matches_its_span() {
11128        let mut db = DictionaryBuilder::new();
11129        let mut triples = Vec::new();
11130        for i in 0..24u32 {
11131            let (s, p, o) = (
11132                format!("<http://ex/s{}>", i % 5),
11133                format!("<http://ex/p{}>", i % 3),
11134                format!("<http://ex/o{i}>"),
11135            );
11136            db.observe(&s, &p, &o);
11137            triples.push((s, p, o));
11138        }
11139        let dict = db.build();
11140        let ids: Vec<(u32, u32, u32)> = triples
11141            .iter()
11142            .map(|(s, p, o)| dict.encode(s, p, o).unwrap())
11143            .collect();
11144        let index = GraphIndexBuilder::from_triples(ids).build();
11145        let image = write_dataset_with_metadata(
11146            &dict,
11147            &index,
11148            &[],
11149            false,
11150            &[],
11151            0,
11152            br#"{"name":"layout-label-fixture"}"#,
11153            &[],
11154        );
11155        let rete = Rete::open(&image).unwrap();
11156
11157        let layout = rete.file_layout();
11158        let header = layout
11159            .iter()
11160            .find(|s| s.kind == "header")
11161            .expect("every layout starts with a header segment");
11162
11163        assert_eq!(
11164            header.offset, 0,
11165            "the header is the first thing in the file"
11166        );
11167        assert_eq!(
11168            header.len, HEADER_LEN as u64,
11169            "the header segment must span exactly HEADER_LEN"
11170        );
11171
11172        // Pull the number back out of the human-readable label and hold it to
11173        // the span. A hard-coded literal that drifts from HEADER_LEN dies here.
11174        let digits: String = header
11175            .label
11176            .chars()
11177            .skip_while(|c| !c.is_ascii_digit())
11178            .take_while(|c| c.is_ascii_digit())
11179            .collect();
11180        assert!(
11181            !digits.is_empty(),
11182            "header label {:?} states no size at all",
11183            header.label
11184        );
11185        assert_eq!(
11186            digits.parse::<u64>().unwrap(),
11187            header.len,
11188            "header label {:?} disagrees with the {}-byte span it describes",
11189            header.label,
11190            header.len
11191        );
11192
11193        // The layout must also stay sorted and start at byte zero, which is
11194        // what makes "the first segment is the header" meaningful.
11195        assert!(
11196            layout.windows(2).all(|w| w[0].offset <= w[1].offset),
11197            "file_layout must return segments sorted by offset"
11198        );
11199    }
11200}