rete-cli 0.4.2

CLI to build and inspect Rete graph files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! The inspect group: read-only introspection of a `.rete` file — header
//! (`info`), human overview (`stats`), named graphs (`graphs`), content-hash
//! check (`verify`), the pyramid summary (`summary`), per-predicate totals
//! (`predicates`), and the type-level schema (`schema`). Several of these answer
//! from the summary alone, never touching the triple index.

use crate::commands::range_source::{open_local, LocalRangeReader};
use rete_core::{SummaryView, CODEC_ZSTD};

/// Print the raw file header (section offsets, counts, codec), plus the embedded
/// Dataset Card catalog when the file carries one.
pub(crate) fn info(file: &str) -> anyhow::Result<()> {
    // Header + card are small addressable ranges — read exactly those instead
    // of slurping the file (on a 50 GB graph this is two range reads, not 50 GB).
    let reader = LocalRangeReader::open(file)?;
    let head = rete_core::RangeReader::read_at(&reader, 0, rete_core::HEADER_LEN as u64)?;
    let header = rete_core::Header::from_bytes(&head)?;
    println!("{header:#?}");
    // The section directory the header just printed is also where "can I
    // full-text search this?" is decided; fold that into the card the same way
    // `rete card` does, so both surfaces answer it identically.
    let text_index = crate::commands::card::TextIndexSignal::probe(&reader, &header);
    // Same for "can this file merge-join?": it is the header's permutation mask
    // (byte 50, `0` = all six), so it is free here and true of the bytes rather
    // than of anything the card claims.
    let permutations = crate::commands::card::PermutationsSignal::probe(&header);
    println!("permutations: {}", permutations.describe());
    let label_index = crate::commands::card::LabelIndexSignal::probe(&reader, &header);
    if let Some(mut card) = crate::commands::card::load_card_ranged(&reader)? {
        card.observe_text_index(text_index);
        card.observe_label_index(label_index);
        card.observe_permutations(permutations);
        println!();
        println!(
            "{}",
            crate::commands::card::format_card(
                &card,
                &crate::commands::card::hex16(&header.content_hash)
            )
        );
    }
    Ok(())
}

/// How a file's statements split between the default graph and its named
/// graphs, and where the split came from.
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct GraphSplit {
    /// Every statement in the file: the header's `quad_count`.
    pub(crate) statements: u64,
    pub(crate) named_graphs: u64,
    /// Default-graph triples; `None` when they could not be counted cheaply.
    pub(crate) default_triples: Option<u64>,
    pub(crate) source: &'static str,
}

impl GraphSplit {
    pub(crate) fn named_quads(&self) -> Option<u64> {
        self.default_triples
            .map(|d| self.statements.saturating_sub(d))
    }
}

/// Split the header's statement count (default graph + every named graph)
/// into its two parts without reading the named graphs: free when there are
/// none; from the Dataset Card's build-time counts when it carries them and
/// they agree with the header; else by counting the default graph's SPO
/// tiles, which a lazy open faults in (the default graph only).
pub(crate) fn graph_split(
    rete: &rete_core::Rete,
    card: Option<&rete_core::card::DatasetCard>,
) -> GraphSplit {
    let statements = rete.header().quad_count;
    let named_graphs = rete.named_graph_count() as u64;
    let (default_triples, source) = if named_graphs == 0 {
        (Some(statements), "header")
    } else if let Some(c) = card.filter(|c| {
        c.quad_count == statements
            && c.named_graph_count == named_graphs
            && c.triple_count <= statements
    }) {
        (Some(c.triple_count), "dataset card")
    } else if statements <= u64::from(u32::MAX) {
        (
            Some(u64::from(rete.default_index().triple_count())),
            "default-graph index",
        )
    } else {
        (None, "unknown")
    };
    GraphSplit {
        statements,
        named_graphs,
        default_triples,
        source,
    }
}

/// Human-friendly overview: size, counts, graphs, pyramid, top predicates. The
/// per-predicate totals + community count come from the summary alone (the
/// triple index is never read).
pub(crate) fn stats(file: &str) -> anyhow::Result<()> {
    let rete = open_local(file)?;
    let h = rete.header();
    let file_len = std::fs::metadata(file).map(|m| m.len()).unwrap_or(0);
    // The card is two small range reads; it carries the build-time split.
    let card = crate::commands::card::load_card_ranged(&LocalRangeReader::open(file)?)
        .ok()
        .flatten();
    let split = graph_split(&rete, card.as_ref());
    let show = |n: Option<u64>| n.map_or_else(|| "unknown".to_string(), |n| n.to_string());
    println!("{file} — {file_len} bytes");
    println!(
        "  statements            : {} (all graphs)",
        split.statements
    );
    println!("  default-graph triples : {}", show(split.default_triples));
    println!("  named graphs          : {}", split.named_graphs);
    if split.named_graphs > 0 {
        println!(
            "  named-graph quads     : {} (split from the {})",
            show(split.named_quads()),
            split.source
        );
    }
    println!("  distinct terms        : {}", h.term_count);
    if h.has_quad_index() {
        println!("  named-graph layout    : quad index (graph-leading permutations, kind 10)");
    }
    println!("  pyramid levels        : {}", h.pyramid_levels);
    if h.has_tile_synopsis() {
        println!("  tile synopsis         : yes (range readers prune tiles by a bound secondary)");
    }
    println!(
        "  index permutations    : {} ({}){}",
        h.perms.len(),
        h.perms.names().join("/"),
        if h.perms.has_merge_orders() {
            ""
        } else {
            " — no sort-merge join"
        }
    );
    println!(
        "  compression           : {}",
        if h.block_codec == CODEC_ZSTD {
            "zstd"
        } else {
            "none"
        }
    );

    // Per-predicate totals + community count come from the summary alone — read
    // through a range reader so this stays cheap on a multi-GB file.
    let reader = LocalRangeReader::open(file)?;
    if let Some(view) = SummaryView::open_ranged(&reader)? {
        println!("  communities           : {}", view.community_count());
        let totals = view.predicate_totals();
        println!("  predicates (top {}):", totals.len().min(10));
        for (pred, count) in totals.iter().take(10) {
            println!("    {count:>8}  {pred}");
        }
    }

    // Per-predicate planner stats (query_stats block) — distinct subjects/objects,
    // multiplicities, and the functional / inverse-functional hints the cost-based
    // planner uses. Absent on files built before the block existed.
    let pstats = rete.predicate_stats();
    if !pstats.is_empty() {
        let dict = rete.dictionary();
        let mut sorted: Vec<_> = pstats.iter().collect();
        sorted.sort_by(|a, b| b.count.cmp(&a.count));
        println!(
            "  planner stats (query_stats, {} predicates):",
            pstats.len()
        );
        for s in sorted.iter().take(10) {
            let term = dict
                .predicate_term(s.predicate)
                .unwrap_or_else(|| format!("#{}", s.predicate));
            let f = if s.max_objects_per_subject == 1 {
                " · functional"
            } else {
                ""
            };
            let inv = if s.max_subjects_per_object == 1 {
                " · inverse-functional"
            } else {
                ""
            };
            println!(
                "    {:>8}  {term}  ({} subj, {} obj; ≤{}/subj, ≤{}/obj{f}{inv})",
                s.count,
                s.distinct_subjects,
                s.distinct_objects,
                s.max_objects_per_subject,
                s.max_subjects_per_object,
            );
        }
    }

    // Entity shapes (characteristic sets): the most common predicate-combinations.
    let shapes = rete.char_sets();
    if !shapes.is_empty() {
        let dict = rete.dictionary();
        let local = |iri: &str| -> String {
            iri.trim_end_matches('>')
                .rsplit(['/', '#'])
                .next()
                .unwrap_or(iri)
                .to_string()
        };
        println!(
            "  entity shapes (characteristic sets, top {}):",
            shapes.len().min(8)
        );
        for c in shapes.iter().take(8) {
            let preds: Vec<String> = c
                .predicates
                .iter()
                .map(|&p| {
                    dict.predicate_term(p)
                        .map(|t| local(&t))
                        .unwrap_or_else(|| format!("#{p}"))
                })
                .collect();
            println!("    {:>8} subj  {{{}}}", c.subjects, preds.join(", "));
        }
    }

    // Label index: how many entries are searchable by prefix (`rete search`) —
    // the LABEL_INDEX section, else the legacy pyramid-meta block.
    if let Some(li) = rete.label_index_section() {
        println!(
            "  label index: {} labels, {} bytes (LABEL_INDEX — `rete search {} <prefix>`)",
            li.entry_count(),
            li.section_len(),
            file
        );
    }
    let labels = rete.label_index();
    if !labels.is_empty() {
        println!(
            "  label index: {} labels (prefix-searchable — `rete search {} <prefix>`)",
            labels.len(),
            file
        );
    }

    // Full-text index (TEXT_INDEX section): word/CONTAINS search over literals.
    if rete.has_text_index() {
        println!(
            "  text index: {} bytes (full-text — `rete search {} --contains <word>`){}",
            h.text_index_len,
            file,
            if h.text_index_partial {
                "; some predicates only (section kind 11)"
            } else {
                ""
            }
        );
    }
    Ok(())
}

/// Prefix-search the label index: print the subjects whose label starts with
/// `prefix` (case-insensitive), as `label  <iri>`. Reads the LABEL_INDEX section
/// (or, on an older file, the bounded label block of the pyramid-meta) — no
/// literal scan. `--json` emits a versioned
/// `{schemaVersion, matches:[{label, subject}]}` envelope.
pub(crate) fn search(file: &str, prefix: &str, limit: usize, json: bool) -> anyhow::Result<()> {
    let rete = open_local(file)?;
    let hits = rete.prefix_search(prefix, limit);
    if json {
        let items: Vec<String> = hits
            .iter()
            .map(|(label, iri)| {
                format!(
                    "{{\"label\":{},\"subject\":{}}}",
                    json_str(label),
                    json_str(iri)
                )
            })
            .collect();
        println!(
            "{{\"schemaVersion\":{},\"matches\":[{}]}}",
            crate::JSON_SCHEMA_VERSION,
            items.join(",")
        );
        return Ok(());
    }
    if hits.is_empty() {
        if !rete.has_label_index() && rete.label_index().is_empty() {
            eprintln!("(this file has no label index — rebuild with `rete build --label-index`)");
        } else {
            println!("(no labels match \"{prefix}\")");
        }
        return Ok(());
    }
    for (label, iri) in &hits {
        println!("{label}\t{iri}");
    }
    Ok(())
}

/// Full-text search (`rete search --contains <word>…`): subjects whose literals
/// contain every word (AND, whole-word, case-insensitive), optionally also a word
/// starting with `--contains-prefix`. Answers from the TEXT_INDEX section.
pub(crate) fn search_contains(
    file: &str,
    words: &[String],
    prefix: Option<&str>,
    limit: usize,
    json: bool,
) -> anyhow::Result<()> {
    let rete = open_local(file)?;
    if !rete.has_text_index() {
        if json {
            println!(
                "{{\"schemaVersion\":{},\"matches\":[]}}",
                crate::JSON_SCHEMA_VERSION
            );
        } else {
            eprintln!("(this file has no text index — rebuild with `rete build --text-index`)");
        }
        return Ok(());
    }
    let word_refs: Vec<&str> = words.iter().map(String::as_str).collect();
    let hits = rete.text_search(&word_refs, prefix, limit);
    if json {
        let items: Vec<String> = hits
            .iter()
            .map(|iri| format!("{{\"subject\":{}}}", json_str(iri)))
            .collect();
        println!(
            "{{\"schemaVersion\":{},\"matches\":[{}]}}",
            crate::JSON_SCHEMA_VERSION,
            items.join(",")
        );
        return Ok(());
    }
    if hits.is_empty() {
        let mut terms: Vec<String> = words.to_vec();
        if let Some(p) = prefix {
            terms.push(format!("{p}*"));
        }
        println!("(no entities contain {})", terms.join(" + "));
        // An index of some graphs or predicates finds only their words; say
        // which, so "nothing" is not read as "nowhere in the file".
        if let Some(scope) = rete.text_index_scope().filter(|s| !s.is_default()) {
            eprintln!("(the text index covers: {})", scope.describe());
        }
        return Ok(());
    }
    for iri in &hits {
        println!("{iri}");
    }
    Ok(())
}

/// Minimal JSON string escaping for the `--json` output.
pub(crate) fn json_str(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// List the named graphs in a dataset (or note that there are none).
pub(crate) fn graphs(file: &str) -> anyhow::Result<()> {
    let rete = open_local(file)?;
    let names = rete.graph_names();
    if names.is_empty() {
        println!("(default graph only — no named graphs)");
    } else {
        for n in names {
            println!("{n}");
        }
    }
    Ok(())
}

/// Verify a file's content hash (detects corruption/truncation). Exits non-zero
/// on mismatch.
///
/// Streams the hashed sections through a positional reader, so memory stays at
/// one read slice whatever the file size (it used to read the whole file).
pub(crate) fn verify_cmd(file: &str) -> anyhow::Result<()> {
    let reader = LocalRangeReader::open(file)?;
    if rete_core::verify_ranged(&reader)? {
        println!("OK — content hash matches");
        Ok(())
    } else {
        anyhow::bail!(
            "FAILED — content hash mismatch, or a section digest mismatch (file corrupted or truncated)"
        );
    }
}

/// Print the pyramid summary — the community super-edge graph, and (for v2 files)
/// the **schema pyramid**: a leveled type histogram (abstract classes at coarse
/// levels, leaves as you zoom in). All read **index-free** from the pyramid-meta
/// via [`SummaryView`]. With `--level k`, print just level `k`'s type histogram.
pub(crate) fn summary(file: &str, level: Option<usize>) -> anyhow::Result<()> {
    // Positional reads of the sections the view needs, not the whole file.
    let reader = LocalRangeReader::open(file)?;
    let Some(view) = SummaryView::open_ranged(&reader)? else {
        // No pyramid: the schema may still have its own section (kind 9).
        match SummaryView::open_schema_ranged(&reader)? {
            Some(view) => {
                if let Some(k) = level {
                    return render_schema_level(&view, k);
                }
                println!("file has no pyramid; its SCHEMA section:");
                render_schema_overview(&view);
            }
            None => eprintln!("file has no pyramid"),
        }
        return Ok(());
    };

    if let Some(k) = level {
        return render_schema_level(&view, k);
    }

    println!(
        "pyramid round {} — {} communities summarized as {} superedge(s):",
        view.round,
        view.community_count(),
        view.summary.len()
    );
    for e in &view.summary {
        let pred = view
            .predicate_term(e.predicate)
            .unwrap_or_else(|| format!("#{}", e.predicate));
        let arrow = if e.s_comm == e.o_comm {
            "(internal)"
        } else {
            "->"
        };
        println!(
            "  C{} {arrow} C{}  via {pred}  x{}",
            e.s_comm, e.o_comm, e.count
        );
    }

    render_schema_overview(&view);
    Ok(())
}

/// Render the whole schema pyramid: one line per semantic level (coarse →
/// abstract, fine → leaves) with the top classes at that level.
pub(crate) fn render_schema_overview(view: &SummaryView) {
    if view.level_count() == 0 {
        return;
    }
    println!(
        "schema pyramid — {} level(s), {} class(es) in the subClassOf hierarchy:",
        view.level_count(),
        view.class_hierarchy.len()
    );
    for (k, lvl) in view.level_rollups.iter().enumerate() {
        let top: Vec<String> = lvl
            .classes
            .iter()
            .take(6)
            .map(|(c, n)| format!("{c}×{n}"))
            .collect();
        let more = if lvl.classes.len() > 6 {
            format!(" … (+{})", lvl.classes.len() - 6)
        } else {
            String::new()
        };
        println!(
            "  level {k} (depth {}, round {}): {}{more}",
            lvl.depth,
            lvl.round,
            top.join(", ")
        );
    }
    let link_total: usize = view.level_links.iter().map(|l| l.links.len()).sum();
    if link_total > 0 {
        println!(
            "  + {link_total} lateral class-relation(s) across levels (use --level k to see them)"
        );
    }
    if !view.descriptors.is_empty() {
        println!(
            "  + {} per-community descriptor(s) for progressive zoom (use the JSON via the API)",
            view.descriptors.len()
        );
    }
}

/// Render one schema-pyramid level's full type histogram (index-free).
fn render_schema_level(view: &SummaryView, k: usize) -> anyhow::Result<()> {
    if view.level_count() == 0 {
        eprintln!("file has no schema pyramid (build it from data with rdf:type)");
        return Ok(());
    }
    let Some(lvl) = view.level_rollup(k) else {
        anyhow::bail!(
            "level {k} out of range — the schema pyramid has {} level(s) (0..{})",
            view.level_count(),
            view.level_count() - 1
        );
    };
    println!(
        "schema pyramid level {k} — depth {} (round {}), {} class(es):",
        lvl.depth,
        lvl.round,
        lvl.classes.len()
    );
    for (class, count) in &lvl.classes {
        println!("  {count:>10}  {class}");
    }
    // The lateral (non-is-a) relations rolled up to this same level, if any.
    if let Some(links) = view.level_links.get(k) {
        if !links.links.is_empty() {
            println!("  relations at this level ({}):", links.links.len());
            for r in &links.links {
                println!(
                    "  {:>10}  {} --{}-> {}",
                    r.count, r.s_class, r.predicate, r.o_class
                );
            }
        }
    }
    Ok(())
}

/// Ontology-aware coarse graph: relations between `rdf:type` classes with
/// instance counts (the dataset's effective schema).
pub(crate) fn schema(file: &str) -> anyhow::Result<()> {
    let rete = open_local(file)?;

    let classes = rete_core::schema_classes(&rete);
    if classes.is_empty() {
        println!("(no rdf:type assertions — the data is untyped)");
    } else {
        println!("classes ({} types):", classes.len());
        for (class, count) in &classes {
            println!("  {count:>8}  {class}");
        }
    }

    let summary = rete_core::schema_summary(&rete);
    println!("relations:");
    for (s_class, pred, o_class, count) in &summary {
        println!("  {s_class} --{pred}--> {o_class}  ×{count}");
    }
    Ok(())
}

/// Exact per-predicate triple counts, computed from the summary alone (the triple
/// index is never read).
pub(crate) fn predicates(file: &str) -> anyhow::Result<()> {
    let reader = LocalRangeReader::open(file)?;
    match SummaryView::open_ranged(&reader)? {
        Some(view) => {
            println!(
                "{} communities · per-predicate totals (from summary, index not read):",
                view.community_count()
            );
            for (pred, count) in view.predicate_totals() {
                println!("  {count}\t{pred}");
            }
        }
        None => eprintln!("file has no pyramid"),
    }
    Ok(())
}