infino 0.1.0

A fast retrieval engine that stores data on object storage and runs SQL, full-text search, and vector search over it from a single system — search-on-Parquet.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! FST-backed term dictionary.
//!
//! The unified FTS blob stores every `(column, term)` pair as a single FST
//! keyed by `<column_name>\x1F<term>` (ASCII Unit Separator). Values are
//! `u64` — the per-`(column, term)` metadata offset directly. At the
//! dict level it's just an opaque integer keyed by bytes.
//!
//! The FST is **prefix-compressed** (FST tail-merging makes shared
//! suffixes near-free) and **prefix-iterable** — a range scan over keys
//! starting with `<column>\x1F` returns exactly the terms in that
//! column, in sorted order, in O(matching keys).
//!
//! # Build / read split
//!
//! `DictBuilder` stages inserts in a `BTreeMap` to absorb the FST's
//! "must insert in sorted order" requirement; `finish()` drains the
//! map in order into `fst::MapBuilder` and returns the serialized FST
//! bytes. `DictReader` opens those bytes (zero-copy via `Bytes`) and
//! exposes exact lookup + prefix iteration.

use std::{collections::BTreeMap, io::Write};

use fst::{IntoStreamer, Map, MapBuilder, Streamer};

use crate::superfile::format::FST_SEPARATOR;

/// Build a canonical FST key from `(column_name, term)`.
///
/// Encoding: `<column_name_utf8> | 0x1F | <term_utf8>`. The separator
/// byte (`FST_SEPARATOR`, ASCII Unit Separator) is below every printable
/// ASCII byte, so prefix iteration `column_name\x1F` cleanly captures
/// every term in that column.
///
/// Callers must ensure `column_name` does not itself contain the
/// separator byte — see [`validate_column_name`]. `term` may be any
/// bytes the tokenizer produced; v1's `AsciiLowerTokenizer` produces
/// only `[a-z0-9]+`, so the separator can never appear in a term.
pub fn make_key(column_name: &str, term: &str) -> Vec<u8> {
    let mut k = Vec::with_capacity(column_name.len() + 1 + term.len());
    k.extend_from_slice(column_name.as_bytes());
    k.push(FST_SEPARATOR);
    k.extend_from_slice(term.as_bytes());
    k
}

/// Returns `true` if `column_name` is safe to use as the column part of
/// an FST key: it must not contain the FST separator byte (otherwise
/// prefix iteration could return cross-column matches).
///
/// All other bytes are allowed; format-level naming rules (no `inf.`
/// prefix, etc.) are enforced elsewhere.
#[inline]
pub fn validate_column_name(column_name: &str) -> bool {
    !column_name.as_bytes().contains(&FST_SEPARATOR)
}

/// Stages keys for FST construction.
///
/// `fst::MapBuilder` requires keys to be inserted in sorted order;
/// `BTreeMap` absorbs that constraint while also deduplicating on
/// repeated key inserts (last write wins, matching the FST invariant
/// of one value per key).
#[derive(Debug, Default)]
pub struct DictBuilder {
    sorted_buffer: BTreeMap<Vec<u8>, u64>,
}

impl DictBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    /// Stage a `(key, value)` pair. Inserts can arrive in any order;
    /// repeated inserts of the same key keep the most-recent value.
    pub fn insert(&mut self, key: &[u8], value: u64) {
        self.sorted_buffer.insert(key.to_vec(), value);
    }

    /// Number of distinct keys staged so far.
    pub fn len(&self) -> usize {
        self.sorted_buffer.len()
    }

    pub fn is_empty(&self) -> bool {
        self.sorted_buffer.is_empty()
    }

    /// Finalize the FST and return its serialized bytes.
    ///
    /// Panics on internal FST errors — these can only occur if the
    /// `BTreeMap` invariant (sorted, unique keys) is broken, which is
    /// impossible without `unsafe`.
    pub fn finish(self) -> Vec<u8> {
        let mut builder = MapBuilder::memory();
        for (k, v) in self.sorted_buffer {
            builder
                .insert(&k, v)
                .expect("BTreeMap guarantees sorted, unique keys");
        }
        builder
            .into_inner()
            .expect("in-memory FST writer cannot fail at finalize")
    }
}

/// Streaming FST builder: wraps `fst::MapBuilder<W>` for callers that
/// can supply keys in already-sorted order and want the FST bytes
/// written progressively to a `Write` sink (e.g. a scratch file) so
/// the dictionary never has to be materialised in RAM.
///
/// Use this when build-time memory matters and the producer already
/// emits keys in lex order — for example after a k-way merge over
/// pre-sorted partition spills. Use [`DictBuilder`] when the caller
/// inserts in arbitrary order and the dictionary fits in RAM.
///
/// Mirrors the `fst::MapBuilder<W>` contract: keys MUST be strictly
/// greater than the previous key. Out-of-order inserts return an
/// `fst::Error`.
pub struct StreamingDictBuilder<W: Write> {
    inner: MapBuilder<W>,
    n_keys: u64,
}

impl<W: Write> StreamingDictBuilder<W> {
    /// Wrap a `Write` sink. The FST format header is written
    /// immediately so the sink position advances on construction.
    ///
    /// **Buffering**: `StreamingDictBuilder` does not interpose its
    /// own buffer; callers writing to a `File` (or any sink with
    /// per-call syscall cost) should wrap in a
    /// `std::io::BufWriter` first. The internal `fst::MapBuilder`
    /// writes in small chunks per key, so passing a raw `File`
    /// makes each `insert_sorted` call trigger multiple `write`
    /// syscalls. The FTS builder follows this contract — see
    /// `FtsBuilder::finish_to`, which wraps the scratch FST file in
    /// a `BufWriter` before constructing the streaming builder.
    pub fn new(w: W) -> Result<Self, fst::Error> {
        Ok(Self {
            inner: MapBuilder::new(w)?,
            n_keys: 0,
        })
    }

    /// Insert a `(key, value)` pair. `key` must be strictly greater
    /// than the previously inserted key. Returns an error otherwise.
    pub fn insert_sorted(&mut self, key: &[u8], value: u64) -> Result<(), fst::Error> {
        self.inner.insert(key, value)?;
        self.n_keys += 1;
        Ok(())
    }

    /// Number of keys inserted so far.
    #[inline]
    pub fn n_keys(&self) -> u64 {
        self.n_keys
    }

    /// Finalise the FST (writes the trailer) and return the inner
    /// writer. The writer is left at the byte just past the FST.
    pub fn finish(self) -> Result<W, fst::Error> {
        self.inner.into_inner()
    }
}

/// Reads a serialized FST. Borrows its bytes — zero-copy when the
/// caller has the blob mmap'd or held in a `Bytes`.
pub struct DictReader<'a> {
    fst: Map<&'a [u8]>,
}

impl<'a> DictReader<'a> {
    /// Open from already-serialized FST bytes (the output of
    /// `DictBuilder::finish`).
    pub fn open(bytes: &'a [u8]) -> Result<Self, fst::Error> {
        Ok(Self {
            fst: Map::new(bytes)?,
        })
    }

    /// Exact lookup. Returns `None` if `key` is not in the FST.
    #[inline]
    pub fn lookup(&self, key: &[u8]) -> Option<u64> {
        self.fst.get(key)
    }

    /// Number of `(key, value)` pairs in the FST.
    #[inline]
    pub fn len(&self) -> usize {
        self.fst.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.fst.is_empty()
    }

    /// Collect every `(key, value)` pair whose key starts with `prefix`,
    /// in lexicographic order. Returns owned data so the caller doesn't
    /// have to manage stream lifetimes.
    ///
    /// Uses an FST range scan that starts at `prefix` and stops as soon
    /// as a key without `prefix` appears — O(matching keys), not O(N).
    /// Suitable for "list every term in column X" diagnostics; for hot
    /// query paths use [`Self::lookup`].
    pub fn iter_prefix(&self, prefix: &[u8]) -> Vec<(Vec<u8>, u64)> {
        let mut out = Vec::new();
        let mut stream = self.fst.range().ge(prefix).into_stream();
        while let Some((key, value)) = stream.next() {
            if !key.starts_with(prefix) {
                break;
            }
            out.push((key.to_vec(), value));
        }
        out
    }
}

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

    // --- make_key -------------------------------------------------------

    #[test]
    fn make_key_encodes_with_unit_separator() {
        assert_eq!(make_key("title", "rust"), b"title\x1Frust");
    }

    #[test]
    fn make_key_handles_empty_term() {
        // The bare prefix used by iter_prefix to "list all terms in column".
        assert_eq!(make_key("title", ""), b"title\x1F");
    }

    #[test]
    fn make_key_handles_empty_column() {
        // Edge case; not produced in practice but must not panic.
        assert_eq!(make_key("", "rust"), b"\x1Frust");
    }

    #[test]
    fn make_key_handles_both_empty() {
        assert_eq!(make_key("", ""), b"\x1F");
    }

    #[test]
    fn make_key_preserves_term_bytes() {
        // The tokenizer drops non-ASCII tokens, but make_key itself is
        // byte-transparent. Multi-byte UTF-8 in the term comes through
        // exactly.
        let key = make_key("body", "café");
        assert_eq!(&key[0..4], b"body");
        assert_eq!(key[4], FST_SEPARATOR);
        assert_eq!(&key[5..], "café".as_bytes());
    }

    #[test]
    fn make_key_capacity_avoids_realloc() {
        // Sanity: the Vec is sized exactly. Not a behaviour requirement
        // but a perf one — protects the hot-path call against
        // accidental realloc.
        let key = make_key("title", "rust");
        assert_eq!(key.len(), key.capacity());
    }

    // --- validate_column_name -------------------------------------------

    #[test]
    fn validate_column_name_rejects_separator_byte() {
        // A column name containing 0x1F would let prefix iteration leak
        // across columns. Format-level validation rejects this at the
        // builder boundary.
        let bad = "ti\x1Ftle";
        assert!(!validate_column_name(bad));
    }

    #[test]
    fn validate_column_name_accepts_normal_names() {
        for name in [
            "title",
            "body",
            "headline",
            "field_1",
            "field-2",
            "MyField",
            "snake_case",
            "camelCase",
            "PascalCase",
            "with spaces",
            "with.dots",
        ] {
            assert!(validate_column_name(name), "expected {name:?} to be valid");
        }
    }

    #[test]
    fn validate_column_name_accepts_empty_string() {
        // Length restrictions are enforced at the builder level, not here.
        assert!(validate_column_name(""));
    }

    #[test]
    fn validate_column_name_accepts_high_unicode() {
        // No restriction on non-ASCII bytes. Format-level rules can add
        // restrictions if needed; the dict layer is byte-transparent.
        assert!(validate_column_name("café"));
        assert!(validate_column_name("日本語"));
    }

    // --- DictBuilder + DictReader roundtrip -----------------------------

    #[test]
    fn lookup_roundtrip_basic() {
        let mut b = DictBuilder::new();
        b.insert(&make_key("title", "rust"), 100);
        b.insert(&make_key("title", "async"), 200);
        b.insert(&make_key("body", "tokio"), 300);
        let bytes = b.finish();

        let r = DictReader::open(&bytes).expect("open DictReader");
        assert_eq!(r.lookup(&make_key("title", "rust")), Some(100));
        assert_eq!(r.lookup(&make_key("title", "async")), Some(200));
        assert_eq!(r.lookup(&make_key("body", "tokio")), Some(300));
        assert_eq!(r.len(), 3);
    }

    #[test]
    fn lookup_missing_returns_none() {
        let mut b = DictBuilder::new();
        b.insert(&make_key("title", "rust"), 1);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        assert_eq!(r.lookup(&make_key("title", "java")), None);
        assert_eq!(r.lookup(&make_key("body", "rust")), None);
        assert_eq!(r.lookup(b""), None);
    }

    #[test]
    fn out_of_order_inserts_work() {
        // Direct fst::MapBuilder rejects out-of-order keys; our
        // DictBuilder absorbs the sort via BTreeMap.
        let mut b = DictBuilder::new();
        let keys = ["z", "a", "m", "b", "y", "c"];
        for (i, k) in keys.iter().enumerate() {
            b.insert(&make_key("col", k), i as u64);
        }
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        for (i, k) in keys.iter().enumerate() {
            assert_eq!(r.lookup(&make_key("col", k)), Some(i as u64));
        }
    }

    #[test]
    fn duplicate_inserts_keep_last_value() {
        // BTreeMap dedups; the FST gets the latest value. Documents
        // the "last write wins" semantic for DictBuilder::insert.
        let mut b = DictBuilder::new();
        b.insert(&make_key("col", "key"), 1);
        b.insert(&make_key("col", "key"), 2);
        b.insert(&make_key("col", "key"), 999);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        assert_eq!(r.lookup(&make_key("col", "key")), Some(999));
        assert_eq!(r.len(), 1);
    }

    // --- Prefix iteration ----------------------------------------------

    #[test]
    fn iter_prefix_returns_matching_keys_only() {
        let mut b = DictBuilder::new();
        // Three columns, distinct vocabularies.
        b.insert(&make_key("title", "alpha"), 1);
        b.insert(&make_key("title", "beta"), 2);
        b.insert(&make_key("title", "gamma"), 3);
        b.insert(&make_key("body", "alpha"), 10);
        b.insert(&make_key("body", "delta"), 20);
        b.insert(&make_key("tag", "epsilon"), 100);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        // Listing all terms in `title`.
        let title_terms = r.iter_prefix(b"title\x1F");
        let title_only: Vec<&[u8]> = title_terms.iter().map(|(k, _)| k.as_slice()).collect();
        assert_eq!(
            title_only,
            vec![
                b"title\x1Falpha".as_slice(),
                b"title\x1Fbeta".as_slice(),
                b"title\x1Fgamma".as_slice(),
            ],
            "iter_prefix on title\\x1F returns only title-prefixed keys"
        );
        assert_eq!(
            title_terms.iter().map(|(_, v)| *v).collect::<Vec<_>>(),
            vec![1, 2, 3]
        );

        // body has different vocabulary.
        let body_terms = r.iter_prefix(b"body\x1F");
        assert_eq!(body_terms.len(), 2);

        // tag has just one.
        let tag_terms = r.iter_prefix(b"tag\x1F");
        assert_eq!(tag_terms.len(), 1);
    }

    #[test]
    fn iter_prefix_lexicographic_order() {
        // Insert in arbitrary order, expect lex order out.
        let mut b = DictBuilder::new();
        let terms = ["zebra", "ant", "monkey", "apple", "banana", "aardvark"];
        for (i, t) in terms.iter().enumerate() {
            b.insert(&make_key("col", t), i as u64);
        }
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        let listed: Vec<Vec<u8>> = r
            .iter_prefix(b"col\x1F")
            .into_iter()
            .map(|(k, _)| k[4..].to_vec()) // strip "col\x1F"
            .collect();
        let mut expected: Vec<&str> = terms.to_vec();
        expected.sort();
        let expected: Vec<Vec<u8>> = expected.iter().map(|s| s.as_bytes().to_vec()).collect();
        assert_eq!(listed, expected);
    }

    #[test]
    fn iter_prefix_no_match_returns_empty() {
        let mut b = DictBuilder::new();
        b.insert(&make_key("title", "rust"), 1);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        // Column that doesn't exist in the FST.
        assert_eq!(r.iter_prefix(b"missing\x1F"), Vec::new());
        // Empty prefix on non-empty FST returns everything (defining
        // behavior — useful for a "dump dict" diagnostic).
        assert_eq!(r.iter_prefix(b"").len(), 1);
    }

    #[test]
    fn iter_prefix_stops_at_first_non_match() {
        // Implementation correctness: iter_prefix must stop scanning
        // as soon as a non-matching key appears. We can't observe the
        // stopping directly, but we can confirm the result is correct
        // when many post-prefix keys exist.
        let mut b = DictBuilder::new();
        // Lots of `body\x1F*` keys after the (alphabetically earlier)
        // `title\x1F*` block.
        for i in 0..1000 {
            b.insert(&make_key("body", &format!("term{i:04}")), i as u64);
        }
        b.insert(&make_key("title", "rust"), 9999);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        // Title scan must yield exactly one entry, regardless of how
        // many body entries follow (`body` < `title` alphabetically,
        // so by the time the scan reaches `title\x1F` it has already
        // skipped past every `body\x1F*` key).
        let title = r.iter_prefix(b"title\x1F");
        assert_eq!(title.len(), 1);
        assert_eq!(title[0].1, 9999);
    }

    // --- Empty-FST handling --------------------------------------------

    #[test]
    fn empty_builder_produces_valid_empty_fst() {
        let bytes = DictBuilder::new().finish();
        let r = DictReader::open(&bytes).expect("open DictReader");
        assert!(r.is_empty());
        assert_eq!(r.len(), 0);
        assert_eq!(r.lookup(b"anything"), None);
        assert_eq!(r.iter_prefix(b""), Vec::new());
        assert_eq!(r.iter_prefix(b"prefix"), Vec::new());
    }

    // --- Stress / scale -------------------------------------------------

    #[test]
    fn handles_thousand_keys_roundtrip() {
        let mut b = DictBuilder::new();
        for i in 0..1000 {
            b.insert(&make_key("body", &format!("term{i:04}")), i as u64);
        }
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");
        assert_eq!(r.len(), 1000);

        for i in 0..1000 {
            let k = make_key("body", &format!("term{i:04}"));
            assert_eq!(r.lookup(&k), Some(i as u64));
        }

        // Prefix iter returns all 1000 in lex order.
        let listed = r.iter_prefix(b"body\x1F");
        assert_eq!(listed.len(), 1000);
        for w in listed.windows(2) {
            assert!(w[0].0 < w[1].0, "prefix iter not in lex order");
        }
    }

    #[test]
    fn serialization_is_deterministic() {
        // Same inputs (regardless of insertion order) produce
        // byte-identical FST output. Important for reproducible builds
        // and for content-addressed superfile hashing.
        let mut b1 = DictBuilder::new();
        b1.insert(&make_key("a", "x"), 1);
        b1.insert(&make_key("b", "y"), 2);
        b1.insert(&make_key("c", "z"), 3);
        let bytes1 = b1.finish();

        let mut b2 = DictBuilder::new();
        b2.insert(&make_key("c", "z"), 3);
        b2.insert(&make_key("a", "x"), 1);
        b2.insert(&make_key("b", "y"), 2);
        let bytes2 = b2.finish();

        assert_eq!(bytes1, bytes2);
    }

    #[test]
    fn keys_with_same_prefix_share_state() {
        // FST tail-merging makes shared prefixes effectively free.
        // We don't probe FST internals; just verify lookups are still
        // correct when many keys share a long common prefix.
        let mut b = DictBuilder::new();
        for i in 0..100 {
            b.insert(
                &make_key("very_long_column_name", &format!("term{i}")),
                i as u64,
            );
        }
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");
        assert_eq!(r.len(), 100);
        for i in 0..100 {
            let k = make_key("very_long_column_name", &format!("term{i}"));
            assert_eq!(r.lookup(&k), Some(i as u64));
        }
    }

    #[test]
    fn lookup_distinguishes_columns_with_same_term() {
        // The whole point of `<col>\x1F<term>` keying: same term in
        // different columns must look up to different values.
        let mut b = DictBuilder::new();
        b.insert(&make_key("title", "rust"), 1);
        b.insert(&make_key("body", "rust"), 2);
        b.insert(&make_key("tag", "rust"), 3);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open DictReader");

        assert_eq!(r.lookup(&make_key("title", "rust")), Some(1));
        assert_eq!(r.lookup(&make_key("body", "rust")), Some(2));
        assert_eq!(r.lookup(&make_key("tag", "rust")), Some(3));
    }

    /// `DictBuilder` tracks key counts + emptiness, which round-trip
    /// through `DictReader::{len, is_empty}`; the streaming builder
    /// tracks `n_keys` as sorted keys arrive.
    #[test]
    fn builder_and_reader_track_key_counts() {
        let mut b = DictBuilder::new();
        assert!(b.is_empty(), "fresh builder is empty");
        assert_eq!(b.len(), 0);
        b.insert(b"alpha", 1);
        b.insert(b"beta", 2);
        assert!(!b.is_empty());
        assert_eq!(b.len(), 2);
        let bytes = b.finish();
        let r = DictReader::open(&bytes).expect("open dict");
        assert_eq!(r.len(), 2);
        assert!(!r.is_empty());

        // Streaming builder counts keys fed in strictly-sorted order.
        let mut sb = StreamingDictBuilder::new(Vec::new()).expect("streaming builder");
        assert_eq!(sb.n_keys(), 0);
        sb.insert_sorted(b"a", 1).expect("sorted insert");
        sb.insert_sorted(b"b", 2).expect("sorted insert");
        assert_eq!(sb.n_keys(), 2);
        let _ = sb.finish().expect("finish streaming builder");
    }
}