sift-core 0.2.0

Indexed regex search over codebases (library + grep-like CLI)
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! Corpus walk, file stat, and posting assembly helpers.
//!
//! These are private helpers used by [`TrigramIndex::build`] and
//! [`TrigramIndex::update`] — there is no separate builder type.

use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use ignore::{
    DirEntry, Error as IgnoreError, ParallelVisitor, ParallelVisitorBuilder, WalkBuilder, WalkState,
};

use super::Trigram;
use super::file_table::FileFingerprint;
use super::storage::lexicon::LexiconEntry;
use super::storage::trigram_sets::TrigramSet;

use crate::index::{CorpusKind, IndexConfig};
use crate::search::filter::{HiddenMode, IgnoreSources};

/// Collected index data ready for persistence.
pub struct IndexTables {
    pub fingerprints: Vec<FileFingerprint>,
    pub file_trigrams: Vec<TrigramSet>,
    pub lexicon: Vec<LexiconEntry>,
    pub postings: Vec<u8>,
}

// ---------------------------------------------------------------------------
// Corpus walk
// ---------------------------------------------------------------------------

/// Per-thread collector for [`WalkParallel::visit`].
struct CorpusPathCollector<'a> {
    root: PathBuf,
    exclude_paths: &'a [PathBuf],
    include_paths: &'a [PathBuf],
    thread_paths: Vec<PathBuf>,
    walk_error: Arc<Mutex<Option<crate::Error>>>,
    consolidated_paths: Arc<Mutex<Vec<PathBuf>>>,
}

impl Drop for CorpusPathCollector<'_> {
    fn drop(&mut self) {
        if self.thread_paths.is_empty() {
            return;
        }
        let mut guard = self
            .consolidated_paths
            .lock()
            .expect("consolidate corpus paths lock");
        guard.append(&mut self.thread_paths);
    }
}

impl ParallelVisitor for CorpusPathCollector<'_> {
    fn visit(&mut self, entry: Result<DirEntry, IgnoreError>) -> WalkState {
        let entry = match entry {
            Ok(entry) => entry,
            Err(err) => {
                let mut guard = self.walk_error.lock().expect("walk error lock");
                if guard.is_none() {
                    *guard = Some(crate::Error::Ignore(err));
                }
                drop(guard);
                return WalkState::Quit;
            }
        };
        let path = entry.path();
        let rel = path.strip_prefix(&self.root).unwrap_or(path);

        // Skip excluded directories before descent.
        if entry.file_type().is_some_and(|ft| ft.is_dir()) {
            if self.is_excluded(rel) {
                return WalkState::Skip;
            }
            return WalkState::Continue;
        }

        if entry.file_type().is_some_and(|ft| ft.is_file()) && !self.is_excluded(rel) {
            self.thread_paths.push(rel.to_path_buf());
        }
        WalkState::Continue
    }
}

impl CorpusPathCollector<'_> {
    /// True when the path matches an explicit exclude rule or falls outside
    /// the explicit include set.
    ///
    /// For directories this accepts the path if *any* included path lies
    /// under it, so that directory pruning does not skip an ancestor of a
    /// wanted file.
    fn is_excluded(&self, rel: &Path) -> bool {
        if self
            .exclude_paths
            .iter()
            .any(|excluded| rel.starts_with(excluded))
        {
            return true;
        }
        if !self.include_paths.is_empty()
            && !self.include_paths.iter().any(|included| {
                rel == *included || included.starts_with(rel) || rel.starts_with(included)
            })
        {
            return true;
        }
        false
    }
}

struct CorpusPathCollectorBuilder<'a> {
    root: PathBuf,
    exclude_paths: &'a [PathBuf],
    include_paths: &'a [PathBuf],
    walk_error: Arc<Mutex<Option<crate::Error>>>,
    consolidated_paths: Arc<Mutex<Vec<PathBuf>>>,
}

impl<'a> ParallelVisitorBuilder<'a> for CorpusPathCollectorBuilder<'a> {
    fn build(&mut self) -> Box<dyn ParallelVisitor + 'a> {
        Box::new(CorpusPathCollector {
            root: self.root.clone(),
            exclude_paths: self.exclude_paths,
            include_paths: self.include_paths,
            thread_paths: Vec::new(),
            walk_error: Arc::clone(&self.walk_error),
            consolidated_paths: Arc::clone(&self.consolidated_paths),
        })
    }
}

/// Walks the corpus directory and collects sorted relative file paths.
pub struct CorpusWalker<'a> {
    config: &'a IndexConfig<'a>,
}

impl<'a> CorpusWalker<'a> {
    pub const fn new(config: &'a IndexConfig<'a>) -> Self {
        Self { config }
    }

    fn walk_builder(&self) -> WalkBuilder {
        let sources = self.config.visibility.ignore.sources;
        let mut wb = WalkBuilder::new(self.config.corpus.root);
        wb.follow_links(self.config.corpus.follow_links)
            .hidden(matches!(self.config.visibility.hidden, HiddenMode::Respect))
            .parents(sources.contains(IgnoreSources::PARENT))
            .ignore(sources.contains(IgnoreSources::DOT))
            .git_ignore(sources.contains(IgnoreSources::VCS))
            .git_exclude(sources.contains(IgnoreSources::EXCLUDE))
            .git_global(sources.contains(IgnoreSources::GLOBAL))
            .require_git(self.config.visibility.ignore.require_git);
        wb
    }

    pub fn collect(&self) -> crate::Result<Vec<PathBuf>> {
        let walk_error = Arc::new(Mutex::new(None));
        let consolidated_paths = Arc::new(Mutex::new(Vec::new()));

        let mut builder = CorpusPathCollectorBuilder {
            root: self.config.corpus.root.to_path_buf(),
            exclude_paths: self.config.corpus.exclude_paths,
            include_paths: self.config.corpus.include_paths,
            walk_error: Arc::clone(&walk_error),
            consolidated_paths: Arc::clone(&consolidated_paths),
        };

        self.walk_builder().build_parallel().visit(&mut builder);

        {
            let mut err_slot = walk_error.lock().expect("walk error lock");
            if let Some(err) = err_slot.take() {
                return Err(err);
            }
        }

        let merged_paths = {
            let mut guard = consolidated_paths.lock().expect("paths lock");
            guard.sort_unstable();
            std::mem::take(&mut *guard)
        };
        Ok(merged_paths)
    }
}

// ---------------------------------------------------------------------------
// Fingerprint collection
// ---------------------------------------------------------------------------

/// Stats each file to produce fingerprints with mtime and size.
pub struct FingerprintCollector<'a> {
    root: &'a Path,
    paths: &'a [PathBuf],
}

impl<'a> FingerprintCollector<'a> {
    pub const fn new(root: &'a Path, paths: &'a [PathBuf]) -> Self {
        Self { root, paths }
    }

    pub fn collect(&self) -> crate::Result<Vec<FileFingerprint>> {
        use rayon::prelude::*;
        self.paths
            .par_iter()
            .map(|rel| {
                let abs = self.root.join(rel);
                let meta = std::fs::metadata(&abs)?;
                let mtime_secs = meta
                    .modified()
                    .ok()
                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                    .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(0));
                let size = meta.len();
                Ok(FileFingerprint {
                    path: rel.clone(),
                    mtime_secs,
                    size,
                })
            })
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Posting assembly
// ---------------------------------------------------------------------------

/// A packed (`trigram_key` << 32) | `file_id`, enabling sort-by-trigram-then-file-id on raw u64 ordering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct PackedPosting(u64);

impl PackedPosting {
    fn new(trigram: Trigram, file_id: u32) -> Self {
        Self((u64::from(trigram.as_u24()) << 32) | u64::from(file_id))
    }

    const fn trigram_key(self) -> u32 {
        (self.0 >> 32) as u32
    }

    const fn file_id(self) -> u32 {
        (self.0 & 0xFFFF_FFFF) as u32
    }
}

/// A single contiguous posting list for one trigram, yielded by [`PostingRuns`].
struct PostingRun<'a> {
    trigram_key: u32,
    pairs: &'a [PackedPosting],
}

impl PostingRun<'_> {
    const fn trigram_bytes(&self) -> [u8; 3] {
        Trigram::from_u24(self.trigram_key).to_bytes()
    }

    const fn len(&self) -> usize {
        self.pairs.len()
    }

    fn file_ids(&self) -> impl Iterator<Item = u32> + '_ {
        self.pairs.iter().map(|p| p.file_id())
    }
}

/// Iterator over contiguous trigram runs in sorted packed-posting order.
struct PostingRuns<'a> {
    pairs: &'a [PackedPosting],
    pos: usize,
}

impl<'a> PostingRuns<'a> {
    const fn new(pairs: &'a [PackedPosting]) -> Self {
        Self { pairs, pos: 0 }
    }
}

impl<'a> Iterator for PostingRuns<'a> {
    type Item = PostingRun<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.pairs.len() {
            return None;
        }
        let trigram_key = self.pairs[self.pos].trigram_key();
        let start = self.pos;
        while self.pos < self.pairs.len() && self.pairs[self.pos].trigram_key() == trigram_key {
            self.pos += 1;
        }
        Some(PostingRun {
            trigram_key,
            pairs: &self.pairs[start..self.pos],
        })
    }
}

/// Assembles trigram → file ID posting lists from per-file trigram sets.
pub struct PostingAssembler<'a> {
    file_trigrams: &'a [TrigramSet],
}

impl<'a> PostingAssembler<'a> {
    pub const fn new(file_trigrams: &'a [TrigramSet]) -> Self {
        Self { file_trigrams }
    }

    pub fn assemble(&self) -> crate::Result<(Vec<LexiconEntry>, Vec<u8>)> {
        let mut pairs = self.collect_pairs()?;
        Self::sort_pairs(&mut pairs);
        Self::encode_runs(&pairs)
    }

    fn collect_pairs(&self) -> crate::Result<Vec<PackedPosting>> {
        let total: usize = self.file_trigrams.iter().map(|s| s.as_slice().len()).sum();
        let mut pairs = Vec::with_capacity(total);
        for (id, set) in self.file_trigrams.iter().enumerate() {
            let id_u32: u32 = id.try_into().map_err(|_| {
                crate::Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "too many indexed files",
                ))
            })?;
            for tri in set.as_slice() {
                pairs.push(PackedPosting::new(*tri, id_u32));
            }
        }
        Ok(pairs)
    }

    fn sort_pairs(pairs: &mut Vec<PackedPosting>) {
        let len = pairs.len();
        if len < 2 {
            return;
        }

        let mut scratch = vec![PackedPosting(0); len];
        let mut count = vec![0usize; 65_536];

        for pass in 0..4 {
            let shift = pass * 16;

            for &p in pairs.iter() {
                count[((p.0 >> shift) & 0xFFFF) as usize] += 1;
            }

            let mut total = 0usize;
            for c in &mut count {
                let tmp = *c + total;
                *c = total;
                total = tmp;
            }

            for &p in pairs.iter() {
                let byte = ((p.0 >> shift) & 0xFFFF) as usize;
                let pos = &mut count[byte];
                scratch[*pos] = p;
                *pos += 1;
            }

            std::mem::swap(pairs, &mut scratch);

            count.fill(0);
        }
    }

    fn encode_runs(pairs: &[PackedPosting]) -> crate::Result<(Vec<LexiconEntry>, Vec<u8>)> {
        let mut posting_bytes = Vec::with_capacity(pairs.len() * 3);
        let mut lex_entries = Vec::new();

        for run in PostingRuns::new(pairs) {
            let offset: u64 = posting_bytes.len().try_into().map_err(|_| {
                crate::Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "postings offset overflow",
                ))
            })?;

            let len = u32::try_from(run.len()).map_err(|_| {
                crate::Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "posting list too long",
                ))
            })?;

            let tri_bytes = run.trigram_bytes();
            Self::encode_run(&run, &mut posting_bytes)?;

            lex_entries.push(LexiconEntry {
                trigram: tri_bytes,
                offset,
                len,
            });
        }

        Ok((lex_entries, posting_bytes))
    }

    fn encode_run(run: &PostingRun<'_>, out: &mut Vec<u8>) -> crate::Result<()> {
        let mut prev = 0u64;
        for (j, id) in run.file_ids().enumerate() {
            let raw = if j == 0 {
                u64::from(id)
            } else {
                u64::from(id).checked_sub(prev).ok_or_else(|| {
                    crate::Error::Io(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "non-monotonic posting list",
                    ))
                })?
            };
            let mut buf = unsigned_varint::encode::u64_buffer();
            let encoded = unsigned_varint::encode::u64(raw, &mut buf);
            out.extend_from_slice(encoded);
            prev = u64::from(id);
        }
        Ok(())
    }
}

/// Build in-memory index tables from a corpus configuration.
///
/// Orchestrates: corpus walk → fingerprint → trigram extraction → posting assembly.
pub fn build_tables(config: &IndexConfig<'_>) -> crate::Result<IndexTables> {
    use rayon::prelude::*;

    let (paths, root) = match config.corpus.kind {
        CorpusKind::SingleFile => {
            if config.corpus.include_paths.is_empty() {
                return Err(crate::Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "SingleFile corpus must specify the file in include_paths",
                )));
            }
            let paths = config.corpus.include_paths.to_vec();
            (paths, config.corpus.root)
        }
        CorpusKind::Directory => {
            let paths = CorpusWalker::new(config).collect()?;
            (paths, config.corpus.root)
        }
    };

    let fingerprints = FingerprintCollector::new(root, &paths).collect()?;

    let file_trigrams: Vec<TrigramSet> = fingerprints
        .par_iter()
        .map(|fp| {
            let abs = root.join(&fp.path);
            TrigramSet::from_file(&abs)
        })
        .collect::<std::io::Result<_>>()
        .map_err(crate::Error::Io)?;

    let (lexicon, postings) = PostingAssembler::new(&file_trigrams).assemble()?;

    Ok(IndexTables {
        fingerprints,
        file_trigrams,
        lexicon,
        postings,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{CorpusKind, CorpusSpec, IndexConfig};
    use crate::search::filter::IgnoreConfig;
    use crate::{CandidateFilter, CandidateFilterConfig, VisibilityConfig};
    use std::fs;
    use std::path::PathBuf;
    use tempfile::TempDir;

    struct TablesFixture;

    impl TablesFixture {
        fn no_ignore_config(root: &Path) -> IndexConfig<'_> {
            IndexConfig {
                corpus: CorpusSpec {
                    root,
                    kind: CorpusKind::Directory,
                    follow_links: false,
                    include_paths: &[],
                    exclude_paths: &[],
                },
                visibility: VisibilityConfig {
                    ignore: IgnoreConfig::disabled(),
                    ..Default::default()
                },
            }
        }

        fn build(root: &Path) -> IndexTables {
            let config = Self::no_ignore_config(root);
            build_tables(&config).expect("build tables")
        }
    }

    struct PostingCounts;

    impl PostingCounts {
        fn file_occurrences(postings: &[u8], lexicon: &[LexiconEntry], file_id: u32) -> usize {
            lexicon
                .iter()
                .map(|entry| {
                    let start = usize::try_from(entry.offset).unwrap_or(usize::MAX);
                    let end = lexicon
                        .iter()
                        .find(|e| e.offset > entry.offset)
                        .map_or(postings.len(), |e| {
                            usize::try_from(e.offset).unwrap_or(usize::MAX)
                        });
                    let slice = &postings[start..end];
                    crate::index::trigram::storage::postings::Postings::decode_sorted(slice)
                        .unwrap_or_default()
                        .into_iter()
                        .filter(|&id| id == file_id)
                        .count()
                })
                .sum()
        }
    }

    struct FilterCorpus;

    impl FilterCorpus {
        fn write(root: &Path) {
            fs::create_dir_all(root.join("skip")).expect("mkdir skip");
            fs::create_dir_all(root.join("also_skip")).expect("mkdir also_skip");
            fs::write(root.join("keep.txt"), "beta\n").expect("write keep");
            fs::write(root.join("skip/ignored.txt"), "beta\n").expect("write skip");
            fs::write(root.join("also_skip/omit.txt"), "beta\n").expect("write omit");
            fs::write(root.join(".gitignore"), "skip/**\n").expect("write gitignore");
            fs::write(root.join(".ignore"), "also_skip/**\n").expect("write ignore");
        }
    }

    struct FilterParity;

    impl FilterParity {
        fn filter_config(build: &IndexConfig<'_>) -> CandidateFilterConfig {
            CandidateFilterConfig {
                exclude_paths: build.corpus.exclude_paths.to_vec(),
                visibility: build.visibility.clone(),
                follow_links: build.corpus.follow_links,
                ..CandidateFilterConfig::default()
            }
        }

        fn all_corpus_files(root: &Path) -> Vec<PathBuf> {
            let mut files = Vec::new();
            Self::collect_files_recursive(root, root, &mut files);
            files.sort_unstable();
            files
        }

        fn collect_files_recursive(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) {
            let entries = fs::read_dir(dir).expect("read dir");
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    Self::collect_files_recursive(root, &path, out);
                } else if path.is_file() {
                    let rel = path.strip_prefix(root).expect("under root").to_path_buf();
                    out.push(rel);
                }
            }
        }
    }

    #[test]
    fn build_index_tables_sorts_file_paths_deterministically() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join("z.txt"), "hello\n").expect("write z");
        fs::write(tmp.path().join("a.txt"), "world\n").expect("write a");
        fs::write(tmp.path().join("m.txt"), "test\n").expect("write m");

        let tables = TablesFixture::build(tmp.path());
        let paths: Vec<PathBuf> = tables
            .fingerprints
            .iter()
            .map(|fp| fp.path.clone())
            .collect();
        let expected = vec![
            PathBuf::from("a.txt"),
            PathBuf::from("m.txt"),
            PathBuf::from("z.txt"),
        ];
        assert_eq!(paths, expected);
    }

    #[test]
    fn build_index_tables_exclude_paths_prevents_indexing() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join("keep.txt"), "hello\n").expect("write keep");
        let excluded_dir = tmp.path().join("excluded");
        fs::create_dir_all(&excluded_dir).expect("create excluded dir");
        fs::write(excluded_dir.join("skip.txt"), "world\n").expect("write skip");

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[PathBuf::from("excluded")],
            },
            visibility: VisibilityConfig {
                ignore: IgnoreConfig::disabled(),
                ..Default::default()
            },
        };
        let tables = build_tables(&config).expect("build tables");
        assert_eq!(tables.fingerprints.len(), 1);
        assert_eq!(tables.fingerprints[0].path, PathBuf::from("keep.txt"));
    }

    #[test]
    fn build_index_tables_duplicate_trigrams_deduplicated_in_postings() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join("a.txt"), "ababab\n").expect("write file");
        fs::write(tmp.path().join("b.txt"), "ababab\n").expect("write file");

        let tables = TablesFixture::build(tmp.path());
        assert_eq!(tables.fingerprints.len(), 2);

        let unique_trigrams = 3;
        assert_eq!(
            PostingCounts::file_occurrences(&tables.postings, &tables.lexicon, 0),
            unique_trigrams
        );
        assert_eq!(
            PostingCounts::file_occurrences(&tables.postings, &tables.lexicon, 1),
            unique_trigrams
        );
    }

    #[test]
    fn corpus_walk_excludes_gitignored_paths_without_git_repo() {
        let tmp = TempDir::new().expect("create temp dir");
        FilterCorpus::write(tmp.path());

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig::default(),
        };
        let paths = CorpusWalker::new(&config).collect().expect("walk corpus");
        assert!(paths.iter().any(|p| p == Path::new("keep.txt")));
        assert!(!paths.iter().any(|p| p.starts_with("skip")));
        assert!(!paths.iter().any(|p| p.starts_with("also_skip")));
    }

    #[test]
    fn corpus_walk_with_empty_ignore_sources_includes_gitignored() {
        let tmp = TempDir::new().expect("create temp dir");
        FilterCorpus::write(tmp.path());

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig {
                ignore: IgnoreConfig::disabled(),
                ..Default::default()
            },
        };
        let paths = CorpusWalker::new(&config).collect().expect("walk corpus");
        assert!(paths.iter().any(|p| p.starts_with("skip")));
        assert!(paths.iter().any(|p| p.starts_with("also_skip")));
    }

    #[test]
    fn corpus_walk_agrees_with_candidate_filter() {
        let tmp = TempDir::new().expect("create temp dir");
        FilterCorpus::write(tmp.path());

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig::default(),
        };
        let indexed = CorpusWalker::new(&config).collect().expect("walk corpus");
        let filter = CandidateFilter::new(&FilterParity::filter_config(&config), tmp.path())
            .expect("filter");

        for rel in FilterParity::all_corpus_files(tmp.path()) {
            let should_index = filter.matches_path(&rel);
            let is_indexed = indexed.iter().any(|p| p == &rel);
            assert_eq!(
                is_indexed, should_index,
                "path {rel:?}: indexed={is_indexed} filter={should_index}"
            );
        }
    }

    #[test]
    fn build_index_tables_include_paths_filters_to_single_file() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join("keep.txt"), "hello\n").expect("write keep");
        fs::write(tmp.path().join("skip.txt"), "world\n").expect("write skip");

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[PathBuf::from("keep.txt")],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig {
                ignore: IgnoreConfig::disabled(),
                ..Default::default()
            },
        };
        let tables = build_tables(&config).expect("build tables");
        assert_eq!(tables.fingerprints.len(), 1);
        assert_eq!(tables.fingerprints[0].path, PathBuf::from("keep.txt"));
    }

    #[test]
    fn build_single_file_corpus_ignores_siblings() {
        let tmp = TempDir::new().expect("create temp dir");
        let file = tmp.path().join("only.txt");
        fs::write(&file, "needle\n").expect("write file");
        fs::write(tmp.path().join("other.txt"), "haystack\n").expect("write other");

        let only_txt = PathBuf::from("only.txt");
        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::SingleFile,
                follow_links: false,
                include_paths: &[only_txt],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig::default(),
        };
        let tables = build_tables(&config).expect("build tables");
        assert_eq!(tables.fingerprints.len(), 1);
        assert_eq!(
            tables.fingerprints[0].path,
            PathBuf::from("only.txt"),
            "should only index the specified file, not siblings"
        );
    }

    #[test]
    fn fingerprints_have_nonzero_size() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join("a.txt"), "hello world\n").expect("write file");

        let tables = TablesFixture::build(tmp.path());
        assert_eq!(tables.fingerprints.len(), 1);
        assert!(
            tables.fingerprints[0].size > 0,
            "fingerprint should capture file size"
        );
    }

    #[test]
    fn build_respects_gitignore_by_default() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::write(tmp.path().join(".gitignore"), "*.ignored\n").expect("write gitignore");
        fs::write(tmp.path().join("keep.txt"), "hello\n").expect("write keep");
        fs::write(tmp.path().join("skip.ignored"), "secret\n").expect("write ignored");

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig::default(),
        };
        let tables = build_tables(&config).expect("build tables");
        let paths: Vec<_> = tables.fingerprints.iter().map(|f| f.path.clone()).collect();
        assert!(
            !paths.iter().any(|p| p == "skip.ignored"),
            "gitignored file must not be indexed, got {paths:?}"
        );
        assert!(
            paths.iter().any(|p| p == "keep.txt"),
            "keep.txt must be indexed, got {paths:?}"
        );
    }

    #[test]
    fn build_prunes_gitignored_directory() {
        let tmp = TempDir::new().expect("create temp dir");
        fs::create_dir_all(tmp.path().join("src")).expect("mkdir src");
        fs::create_dir_all(tmp.path().join("target")).expect("mkdir target");
        fs::write(tmp.path().join(".gitignore"), "/target\n").expect("write gitignore");
        fs::write(tmp.path().join("src/keep.txt"), "needle\n").expect("write keep");
        fs::write(tmp.path().join("target/ignored.txt"), "secret\n").expect("write ignored");

        let config = IndexConfig {
            corpus: CorpusSpec {
                root: tmp.path(),
                kind: CorpusKind::Directory,
                follow_links: false,
                include_paths: &[],
                exclude_paths: &[],
            },
            visibility: VisibilityConfig::default(),
        };
        let tables = build_tables(&config).expect("build tables");
        let paths: Vec<_> = tables.fingerprints.iter().map(|f| f.path.clone()).collect();
        assert!(
            paths.iter().any(|p| p == Path::new("src/keep.txt")),
            "keep must be indexed, got {paths:?}"
        );
        assert!(
            !paths.iter().any(|p| p.starts_with("target")),
            "target/ must not be indexed, got {paths:?}"
        );
    }

    #[test]
    fn sort_pairs_matches_std_order() {
        let cases: &[(u32, u32)] = &[
            (0x00_0000, 0),
            (0x00_0001, 0),
            (0x00_0000, 1),
            (0x00_FFFF, 0),
            (0x01_0000, 0),
            (0x00_FFFF, 0xFFFF),
            (0x01_0000, 0x0000),
            (0x00_FEFF, 0xFFFF),
            (0x00_FF00, 0x1_0000),
            (0x00_4567, 0x89AB),
            (0x00_ABCD, 0x0123),
            (0x00_FFFF, 0xABCD),
            (0x00_89AB, 0xCDEF),
            (0x00_0000, 0xFFFF_FFFE),
            (0x00_0000, 0xFFFF_FFFF),
            (0x00_FFFF, 0xFFFF_FFFF),
            (0x00_ABCD, 0xFFFF_FFFE),
        ];

        let mut sorted_a: Vec<PackedPosting> = cases
            .iter()
            .map(|&(tri, fid)| PackedPosting::new(Trigram::from_u24(tri), fid))
            .collect();
        let mut sorted_b = sorted_a.clone();

        PostingAssembler::sort_pairs(&mut sorted_a);
        sorted_b.sort_unstable();

        assert_eq!(sorted_a, sorted_b);
        assert!(sorted_a.windows(2).all(|w| w[0] <= w[1]));
    }
}