rete-core 0.7.7

Core format types for the Rete cloud-native RDF graph file.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Derived IRI families (format `0x08`, SPEC.md section 5.3).
//!
//! Some files hold millions of IRIs that are a function of terms already in the
//! file: the OpenCitations Index names a citation `…/ci/<citing>-<cited>`, and
//! its two OMIDs are the objects of the citation's `hasCitingEntity` and
//! `hasCitedEntity` triples. Stored as text they are 99.7 % of that file's
//! dictionary. A **family** says how such an IRI is built:
//!
//! - a **template**: literal pieces around `n` holes;
//! - `n` **inputs**, one per hole: a predicate, and an IRI prefix. The text of
//!   hole `k` is what follows the prefix in the object of the subject's triple
//!   with that predicate.
//!
//! Nobody writes a family: the builder finds them in the data (`v06::derived`)
//! and stores each one's template and inputs in the file.
//!
//! The file keeps every id, rank, group count and routing key of the
//! subject-only section, and drops the **bodies** of the chunks whose every
//! term the writer checked, byte for byte, against the template. A chunk with
//! a term that does not derive stays stored whole (the exceptions). The reader
//! rebuilds a term from the index and the object dictionary: [`DerivedTerms`].
//!
//! Only the subject-only section holds derived chunks, and the inputs are
//! objects, which live in the shared and object-only sections, so decoding a
//! derived term never needs another derived term.

use std::sync::{Arc, OnceLock};

use crate::dictionary::Dictionary;
use crate::index::{GraphIndex, Pattern};

/// The dictionary section that holds derived chunks: subject-only.
pub const SECTION: usize = 1;

/// What the writer's detection asks of a family before it takes it
/// (SPEC.md section 5.3): the bytes it removes must be worth the slower decode.
/// Not a user setting; the CLI has only an off switch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeriveOpts {
    /// A family is taken when the stored bytes it removes are at least this many
    /// thousandths of the file (default 50, 5 %): a derived term costs a lookup
    /// two more requests and `rete export` about twice the time.
    pub min_permille: u64,
    /// ... and at least this many bytes (default 256 KiB): under that the whole
    /// dictionary is a request or two.
    pub min_bytes: u64,
}

impl Default for DeriveOpts {
    fn default() -> Self {
        DeriveOpts {
            min_permille: 50,
            min_bytes: 256 << 10,
        }
    }
}

impl DeriveOpts {
    /// The bytes a family must remove from a file of `file_len` bytes.
    pub fn threshold(&self, file_len: u64) -> u64 {
        (file_len / 1000)
            .saturating_mul(self.min_permille)
            .max(self.min_bytes)
    }
}

/// Most families a file may carry.
pub const MAX_FAMILIES: usize = 16;
/// Most holes in one template.
pub const MAX_INPUTS: usize = 8;

/// A malformed family or root.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeriveError(pub String);

impl std::fmt::Display for DeriveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for DeriveError {}

fn err<T>(m: impl Into<String>) -> Result<T, DeriveError> {
    Err(DeriveError(m.into()))
}

/// One hole of a template.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Input {
    /// The predicate whose object fills the hole, as an IRI (no `<>`).
    pub predicate: String,
    /// The object is an IRI that starts with this; the rest is the hole's text.
    pub prefix: String,
}

/// An IRI family: template, inputs, and the chunks of the subject-only section
/// whose bodies are not stored.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Family {
    /// The literal pieces: `inputs.len() + 1` of them. The first starts with
    /// `<` and the last ends with `>` (a term is stored as its token); the ones
    /// between holes are not empty, so that a term can be split again.
    pub literals: Vec<String>,
    /// One per hole.
    pub inputs: Vec<Input>,
    /// Chunks of the subject-only section whose bodies are not stored, as
    /// `(first chunk, count)`, ascending and disjoint. Empty in a spec.
    pub runs: Vec<(usize, usize)>,
}

impl Family {
    /// A family from its literal pieces and inputs (no runs yet). The writer's
    /// detection builds families this way; there is no user-written template.
    pub fn new(literals: Vec<String>, inputs: Vec<Input>) -> Result<Family, DeriveError> {
        let f = Family {
            literals,
            inputs,
            runs: Vec::new(),
        };
        f.check()?;
        Ok(f)
    }

    /// The template as text, `https://x/{PREDICATE|PREFIX}-{…}`: for `rete info`
    /// and the build record (without the runs).
    pub fn spec(&self) -> String {
        let mut s = String::new();
        for (k, lit) in self.literals.iter().enumerate() {
            let mut t = lit.as_str();
            if k == 0 {
                t = t.strip_prefix('<').unwrap_or(t);
            }
            if k + 1 == self.literals.len() {
                t = t.strip_suffix('>').unwrap_or(t);
            }
            s.push_str(t);
            if let Some(i) = self.inputs.get(k) {
                s.push('{');
                s.push_str(&i.predicate);
                s.push('|');
                s.push_str(&i.prefix);
                s.push('}');
            }
        }
        s
    }

    /// Is the template well formed?
    pub fn check(&self) -> Result<(), DeriveError> {
        let n = self.inputs.len();
        if n == 0 || n > MAX_INPUTS {
            return err(format!("family: a template has 1 to {MAX_INPUTS} holes"));
        }
        if self.literals.len() != n + 1 {
            return err("family: the literals do not fit the holes");
        }
        if !self.literals[0].starts_with('<') || !self.literals[n].ends_with('>') {
            return err("family: a template is an IRI");
        }
        for (k, l) in self.literals.iter().enumerate() {
            if k > 0 && k < n && l.is_empty() {
                return err("family: two holes need a literal between them");
            }
        }
        if self.literals[0].len() < 2 {
            return err("family: the template has no IRI text before its first hole");
        }
        Ok(())
    }

    /// The term for the hole texts `values`.
    pub fn build(&self, values: &[&str]) -> String {
        let mut s = String::new();
        for (k, lit) in self.literals.iter().enumerate() {
            s.push_str(lit);
            if let Some(v) = values.get(k) {
                s.push_str(v);
            }
        }
        s
    }

    /// The hole texts of `term`, if it has the template's shape. A text runs to
    /// the first occurrence of the next literal, so a term whose text holds that
    /// literal does not split the way it was built: the writer checks that
    /// [`Self::build`] of the split gives the term back, and stores the chunks
    /// where it does not.
    pub fn split<'a>(&self, term: &'a str) -> Option<Vec<&'a str>> {
        let n = self.inputs.len();
        let mut rest = term.strip_prefix(self.literals[0].as_str())?;
        let mut out = Vec::with_capacity(n);
        for k in 0..n {
            let lit = self.literals[k + 1].as_str();
            if k + 1 < n {
                let at = rest.find(lit)?;
                out.push(&rest[..at]);
                rest = &rest[at + lit.len()..];
            } else {
                out.push(rest.strip_suffix(lit)?);
            }
        }
        Some(out)
    }

    /// The object token (`<iri>`) of hole `k` with text `value`.
    pub fn object_token(&self, k: usize, value: &str) -> String {
        format!("<{}{}>", self.inputs[k].prefix, value)
    }

    /// The text of hole `k` in the object token `object`, if it is an IRI under
    /// the input's prefix with something after it.
    pub fn value_of<'a>(&self, k: usize, object: &'a str) -> Option<&'a str> {
        let v = object
            .strip_prefix('<')?
            .strip_suffix('>')?
            .strip_prefix(self.inputs[k].prefix.as_str())?;
        (!v.is_empty()).then_some(v)
    }

    /// The term the hole objects `objects` (tokens) spell, if each is under its
    /// input's prefix.
    pub fn derive(&self, objects: &[&str]) -> Option<String> {
        if objects.len() != self.inputs.len() {
            return None;
        }
        let mut values = Vec::with_capacity(objects.len());
        for (k, o) in objects.iter().enumerate() {
            values.push(self.value_of(k, o)?);
        }
        Some(self.build(&values))
    }

    /// Does the family's run list include chunk `ci`?
    pub fn covers(&self, ci: usize) -> bool {
        let at = self.runs.partition_point(|&(first, _)| first <= ci);
        at > 0 && ci < self.runs[at - 1].0 + self.runs[at - 1].1
    }

    /// Chunks the family has no body for.
    pub fn chunk_count(&self) -> usize {
        self.runs.iter().map(|r| r.1).sum()
    }
}

/// Encode the families (the unit root of tag 7).
pub fn encode_families(fams: &[Family]) -> Vec<u8> {
    use crate::varint::write_uvarint;
    let lp = |o: &mut Vec<u8>, s: &str| {
        write_uvarint(o, s.len() as u64);
        o.extend_from_slice(s.as_bytes());
    };
    let mut o = Vec::new();
    write_uvarint(&mut o, fams.len() as u64);
    for f in fams {
        write_uvarint(&mut o, f.inputs.len() as u64);
        lp(&mut o, &f.literals[0]);
        for (k, i) in f.inputs.iter().enumerate() {
            lp(&mut o, &i.predicate);
            lp(&mut o, &i.prefix);
            lp(&mut o, &f.literals[k + 1]);
        }
        write_uvarint(&mut o, f.runs.len() as u64);
        for &(first, n) in &f.runs {
            write_uvarint(&mut o, first as u64);
            write_uvarint(&mut o, n as u64);
        }
    }
    o
}

/// Decode the families of a root; every count and length is untrusted.
/// `n_chunks` is the chunk count of the subject-only section.
pub fn decode_families(p: &[u8], n_chunks: usize) -> Result<Vec<Family>, DeriveError> {
    use crate::varint::read_uvarint;
    let uv = |pos: &mut usize| -> Result<u64, DeriveError> {
        let (v, n) = read_uvarint(p.get(*pos..).unwrap_or(&[]))
            .ok_or_else(|| DeriveError("derived-IRI root: truncated varint".into()))?;
        *pos += n;
        Ok(v)
    };
    let text = |pos: &mut usize, len: u64| -> Result<String, DeriveError> {
        let end = usize::try_from(len)
            .ok()
            .and_then(|l| pos.checked_add(l))
            .filter(|&e| e <= p.len())
            .ok_or_else(|| DeriveError("derived-IRI root: a string overruns".into()))?;
        let s = std::str::from_utf8(&p[*pos..end])
            .map_err(|_| DeriveError("derived-IRI root: a string is not UTF-8".into()))?
            .to_string();
        *pos = end;
        Ok(s)
    };
    let mut pos = 0usize;
    let nf = uv(&mut pos)?;
    if nf > MAX_FAMILIES as u64 {
        return err("derived-IRI root: too many families");
    }
    let mut out = Vec::new();
    for _ in 0..nf {
        let ni = uv(&mut pos)?;
        if ni == 0 || ni > MAX_INPUTS as u64 {
            return err("derived-IRI root: bad hole count");
        }
        let mut f = Family::default();
        let l = uv(&mut pos)?;
        f.literals.push(text(&mut pos, l)?);
        for _ in 0..ni {
            let l = uv(&mut pos)?;
            let predicate = text(&mut pos, l)?;
            let l = uv(&mut pos)?;
            let prefix = text(&mut pos, l)?;
            let l = uv(&mut pos)?;
            f.inputs.push(Input { predicate, prefix });
            f.literals.push(text(&mut pos, l)?);
        }
        f.check()?;
        let nr = uv(&mut pos)?;
        if nr > p.len() as u64 {
            return err("derived-IRI root: bad run count");
        }
        let mut prev_end = 0usize;
        for _ in 0..nr {
            let first = usize::try_from(uv(&mut pos)?).unwrap_or(usize::MAX);
            let n = usize::try_from(uv(&mut pos)?).unwrap_or(usize::MAX);
            let end = first.checked_add(n);
            if n == 0 || first < prev_end || end.is_none_or(|e| e > n_chunks) {
                return err("derived-IRI root: a run is out of order or past the chunks");
            }
            prev_end = first + n;
            f.runs.push((first, n));
        }
        out.push(f);
    }
    if pos != p.len() {
        return err("derived-IRI root: trailing bytes");
    }
    let all = elided_runs(&out);
    if all.windows(2).any(|w| w[0].0 + w[0].1 > w[1].0) {
        return err("derived-IRI root: two families claim a chunk");
    }
    Ok(out)
}

/// The chunk ranges of all families, sorted.
pub fn elided_runs(fams: &[Family]) -> Vec<(usize, usize)> {
    let mut all: Vec<(usize, usize)> = fams.iter().flat_map(|f| f.runs.iter().copied()).collect();
    all.sort_unstable();
    all
}

/// The candidate subjects a lookup looks at, at most, for one term.
const MAX_CANDIDATES: usize = 1 << 20;

/// Length, first id, last id and a hash of an id list.
type Signature = (usize, Option<u32>, Option<u32>, u64);

/// Objects decoded within one batch: the citing OMID of a run of citations is
/// the same object for each of them.
struct Memo(std::collections::HashMap<u32, Option<std::sync::Arc<str>>>);

impl Memo {
    fn new() -> Self {
        Memo(std::collections::HashMap::new())
    }

    fn object(&mut self, dict: &Dictionary, o: u32) -> Option<std::sync::Arc<str>> {
        if self.0.len() > 1 << 16 {
            self.0.clear();
        }
        self.0
            .entry(o)
            .or_insert_with(|| dict.object_term(o).map(Into::into))
            .clone()
    }
}

/// Rebuilds derived terms for a [`Dictionary`]: reads the inputs of an id from
/// the index, decodes them in the dictionary and fills the template.
pub struct DerivedTerms {
    families: Vec<Family>,
    index: OnceLock<Arc<GraphIndex>>,
    /// Per family, the predicate id of each input (`None` when the file has no
    /// such predicate, so nothing derives).
    preds: OnceLock<Vec<Vec<Option<u32>>>>,
    /// The last term rebuilt: a subject is decoded again for each row it is in.
    last: std::sync::Mutex<Option<(u32, String)>>,
    /// Signature of the last id list [`Self::prefetch`] warmed.
    prefetched: std::sync::Mutex<Option<Signature>>,
}

impl DerivedTerms {
    /// A resolver for `families`.
    pub fn new(families: Vec<Family>) -> Self {
        DerivedTerms {
            families,
            index: OnceLock::new(),
            preds: OnceLock::new(),
            last: std::sync::Mutex::new(None),
            prefetched: std::sync::Mutex::new(None),
        }
    }

    /// The families.
    pub fn families(&self) -> &[Family] {
        &self.families
    }

    /// Give the resolver the index its ids are read from (once).
    pub fn wire(&self, index: Arc<GraphIndex>) {
        let _ = self.index.set(index);
    }

    fn preds(&self, dict: &Dictionary) -> &[Vec<Option<u32>>] {
        self.preds.get_or_init(|| {
            self.families
                .iter()
                .map(|f| {
                    f.inputs
                        .iter()
                        .map(|i| dict.predicate_id(&format!("<{}>", i.predicate)))
                        .collect()
                })
                .collect()
        })
    }

    /// The family that owns subject-only chunk `ci`.
    fn family_of_chunk(&self, ci: usize) -> Option<usize> {
        self.families.iter().position(|f| f.covers(ci))
    }

    /// The object ids of subject `s`'s inputs in family `fi`; `None` unless each
    /// input has exactly one object.
    fn inputs_of(&self, dict: &Dictionary, fi: usize, s: u32) -> Option<Vec<u32>> {
        let index = self.index.get()?;
        let mut out = Vec::with_capacity(self.families[fi].inputs.len());
        // One lookup of the subject's rows (SPO, the tile the prefetch faulted).
        let rows: Vec<(u32, u32, u32)> = index.scan_iter((Some(s), None, None)).collect();
        for p in self.preds(dict)[fi].iter() {
            let p = (*p)?;
            let mut it = rows.iter().filter(|r| r.1 == p);
            match (it.next(), it.next()) {
                (Some(&(_, _, o)), None) => out.push(o),
                _ => return None,
            }
        }
        Some(out)
    }

    /// The term of subject-only id `local`, rebuilt.
    pub fn term(&self, dict: &Dictionary, local: u32) -> Option<String> {
        if let Ok(g) = self.last.lock() {
            if let Some((l, t)) = g.as_ref() {
                if *l == local {
                    return Some(t.clone());
                }
            }
        }
        let t = self.term_uncached(dict, local, &mut Memo::new())?;
        if let Ok(mut g) = self.last.lock() {
            *g = Some((local, t.clone()));
        }
        Some(t)
    }

    fn term_uncached(&self, dict: &Dictionary, local: u32, memo: &mut Memo) -> Option<String> {
        let ci = dict.subject_only_chunk(local)?;
        let fi = self.family_of_chunk(ci)?;
        let s = dict.shared_count().checked_add(local)?;
        let objects = self.inputs_of(dict, fi, s)?;
        let mut texts: Vec<std::sync::Arc<str>> = Vec::with_capacity(objects.len());
        for &o in &objects {
            texts.push(memo.object(dict, o)?);
        }
        let refs: Vec<&str> = texts.iter().map(|t| &**t).collect();
        self.families[fi].derive(&refs)
    }

    /// Fill `out[slot]` for each `(local id, slot)` of `jobs` that is derived.
    pub fn terms(&self, dict: &Dictionary, jobs: &[(u32, usize)], out: &mut [Option<String>]) {
        let mut jobs = jobs.to_vec();
        jobs.sort_unstable();
        let mut ids: Vec<u32> = jobs.iter().map(|j| j.0).collect();
        ids.dedup();
        self.prefetch(dict, &ids);
        // A subject repeats in every row it is in: rebuild each id once.
        let mut memo = Memo::new();
        let mut last: Option<(u32, Option<String>)> = None;
        for &(local, slot) in &jobs {
            if out[slot].is_some() {
                continue;
            }
            let t = match &last {
                Some((l, t)) if *l == local => t.clone(),
                _ => {
                    let t = self.term_uncached(dict, local, &mut memo);
                    last = Some((local, t.clone()));
                    t
                }
            };
            out[slot] = t;
        }
    }

    /// Warm what decoding these subject-only ids reads, in as few batched reads
    /// as the reader can make: the index tiles of their inputs, then the
    /// dictionary chunks of the objects those name. A no-op on a resident file.
    pub fn prefetch(&self, dict: &Dictionary, locals: &[u32]) {
        let Some(index) = self.index.get() else {
            return;
        };
        if !index.is_remote() {
            return;
        }
        // The same ids again (a caller prefetches, then decodes through `terms`):
        // nothing new to read.
        let mut locals = locals.to_vec();
        locals.sort_unstable();
        locals.dedup();
        let sig = (
            locals.len(),
            locals.first().copied(),
            locals.last().copied(),
            locals.iter().fold(0u64, |a, &l| {
                a.wrapping_mul(0x9E37_79B9_7F4A_7C15).wrapping_add(l as u64)
            }),
        );
        if let Ok(mut g) = self.prefetched.lock() {
            if *g == Some(sig) {
                return;
            }
            *g = Some(sig);
        }
        let locals = &locals[..];
        let mut pats: Vec<Pattern> = Vec::new();
        let mut wanted: Vec<u32> = Vec::new();
        let mut seen = std::collections::BTreeSet::new();
        for &l in locals {
            let Some(ci) = dict.subject_only_chunk(l) else {
                continue;
            };
            let Some(fi) = self.family_of_chunk(ci) else {
                continue;
            };
            let Some(s) = dict.shared_count().checked_add(l) else {
                continue;
            };
            if !seen.insert(s) {
                continue;
            }
            pats.push((Some(s), None, None));
            wanted.extend(self.preds(dict)[fi].iter().flatten());
        }
        wanted.sort_unstable();
        wanted.dedup();
        let mut at = 0;
        while at < pats.len() {
            at += index.prefetch_probe_tiles(&pats[at..]).max(1);
        }
        let mut nodes = Vec::new();
        for pat in &pats {
            for (_, p, o) in index.scan_iter(*pat) {
                if wanted.contains(&p) {
                    nodes.push(dict.object_node(o));
                }
            }
        }
        nodes.sort_unstable();
        nodes.dedup();
        dict.prefetch_terms(&nodes, &[]);
    }

    /// The object terms a lookup of `term` reads, if it is shaped like a member
    /// of a family: what a batched lookup prefetch adds to its terms.
    pub fn lookup_terms(&self, term: &str) -> Vec<String> {
        let mut out = Vec::new();
        for f in &self.families {
            if let Some(vals) = f.split(term) {
                for (k, v) in vals.iter().enumerate() {
                    out.push(f.object_token(k, v));
                }
            }
        }
        out
    }

    /// The section-local id of `term`, which routes to an elided chunk whose ids
    /// are `first..=last`: the subject in that range whose inputs are the ones
    /// `term` spells. `None` when there is none, which is when the term is not
    /// in the file.
    pub fn lookup(&self, dict: &Dictionary, term: &str, first: u32, last: u32) -> Option<u32> {
        let index = self.index.get()?;
        let ci = dict.subject_only_chunk(first)?;
        let fi = self.family_of_chunk(ci)?;
        let fam = &self.families[fi];
        let vals = fam.split(term)?;
        if fam.build(&vals) != term {
            return None;
        }
        let preds = self.preds(dict)[fi].clone();
        let mut objs = Vec::with_capacity(vals.len());
        for (k, v) in vals.iter().enumerate() {
            objs.push(dict.object_id_exact(&fam.object_token(k, v))?);
        }
        let shared = dict.shared_count();
        let (lo, hi) = (shared.checked_add(first)?, shared.checked_add(last)?);
        // The subjects of the first input within the chunk; the others check
        // each one (their rows sit next to it in the index).
        let mut cands: Vec<u32> = index
            .match_pattern((None, Some(preds[0]?), Some(objs[0])))
            .into_iter()
            .map(|t| t.0)
            .filter(|s| (lo..=hi).contains(s))
            .collect();
        cands.truncate(MAX_CANDIDATES);
        'cand: for s in cands {
            for k in 1..vals.len() {
                if index
                    .match_pattern((Some(s), Some(preds[k]?), Some(objs[k])))
                    .is_empty()
                {
                    continue 'cand;
                }
            }
            return Some(s - shared);
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const CITING: &str = "http://purl.org/spar/cito/hasCitingEntity";
    const CITED: &str = "http://purl.org/spar/cito/hasCitedEntity";
    const BR: &str = "https://w3id.org/oc/meta/br/";

    fn input(p: &str, prefix: &str) -> Input {
        Input {
            predicate: p.into(),
            prefix: prefix.into(),
        }
    }

    fn ci() -> Family {
        Family::new(
            vec![
                "<https://w3id.org/oc/index/ci/".into(),
                "-".into(),
                ">".into(),
            ],
            vec![input(CITING, BR), input(CITED, BR)],
        )
        .unwrap()
    }

    #[test]
    fn a_family_builds_splits_and_derives() {
        let f = ci();
        assert_eq!(f.inputs.len(), 2);
        assert_eq!(
            f.spec(),
            format!("https://w3id.org/oc/index/ci/{{{CITING}|{BR}}}-{{{CITED}|{BR}}}")
        );
        assert_eq!(
            f.build(&["0601785", "06902211389"]),
            "<https://w3id.org/oc/index/ci/0601785-06902211389>"
        );
        assert_eq!(
            f.split("<https://w3id.org/oc/index/ci/0601785-06902211389>"),
            Some(vec!["0601785", "06902211389"])
        );
        assert_eq!(f.split("<https://w3id.org/oc/index/ci/0601785>"), None);
        assert_eq!(f.split("<http://other/0601785-1>"), None);
        assert_eq!(
            f.value_of(0, "<https://w3id.org/oc/meta/br/0601785>"),
            Some("0601785")
        );
        assert_eq!(f.value_of(0, "<https://w3id.org/oc/meta/br/>"), None);
        assert_eq!(f.value_of(0, "<http://x/1>"), None);
        assert_eq!(
            f.derive(&[
                "<https://w3id.org/oc/meta/br/0601785>",
                "<https://w3id.org/oc/meta/br/06902211389>"
            ])
            .as_deref(),
            Some("<https://w3id.org/oc/index/ci/0601785-06902211389>")
        );
    }

    #[test]
    fn families_that_cannot_split_are_refused() {
        let lit = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        // Two holes with nothing between them.
        assert!(Family::new(
            lit(&["<http://x/", "", ">"]),
            vec![input("a", "b"), input("c", "d")]
        )
        .is_err());
        // No hole, no IRI text before the first hole, literals that do not fit.
        assert!(Family::new(lit(&["<http://x/>"]), vec![]).is_err());
        assert!(Family::new(lit(&["<", ">"]), vec![input("a", "b")]).is_err());
        assert!(Family::new(lit(&["<http://x/>"]), vec![input("a", "b")]).is_err());
    }

    #[test]
    fn root_round_trips_and_refuses_bad_runs() {
        let mut f = ci();
        f.runs = vec![(2, 3), (7, 1)];
        let bytes = encode_families(std::slice::from_ref(&f));
        assert_eq!(decode_families(&bytes, 10).unwrap(), vec![f.clone()]);
        assert!(decode_families(&bytes, 7).is_err(), "run past the chunks");
        assert!(decode_families(&bytes[..bytes.len() - 1], 10).is_err());
        let mut g = f.clone();
        g.runs = vec![(2, 3), (3, 1)];
        assert!(decode_families(&encode_families(&[g]), 10).is_err());
        assert!(f.covers(2) && f.covers(4) && !f.covers(5) && f.covers(7) && !f.covers(8));
    }
}