Skip to main content

ix/
reader.rs

1//! Index reader — the mmap-based query-time interface.
2//!
3//! Fast, zero-copy access to the index data.
4
5use crate::bloom::BloomFilter;
6use crate::error::{Error, Result};
7use crate::format::{FILE_ENTRY_SIZE, FileStatus, HEADER_SIZE, Header};
8use crate::posting::PostingList;
9use crate::string_pool::StringPoolReader;
10use crate::trigram::Trigram;
11use memmap2::Mmap;
12use std::fs::File;
13use std::path::{Path, PathBuf};
14use std::time::UNIX_EPOCH;
15
16#[cfg(unix)]
17use std::os::unix::fs::MetadataExt;
18
19/// Lightweight snapshot of shard-level metadata (no mmap needed).
20#[derive(Debug, Clone, Copy)]
21pub struct ShardMetadata {
22    /// Microsecond-precision Unix timestamp from the shard header.
23    pub shard_timestamp: u64,
24    /// Total number of files indexed in this shard.
25    pub file_count: u32,
26    /// Total number of unique trigrams in this shard.
27    pub trigram_count: u32,
28}
29
30/// One entry in the CDX block index: first trigram key + absolute block offset.
31#[derive(Debug, Clone, Copy)]
32pub struct CdxBlockEntry {
33    /// First trigram key in this block.
34    pub first_key: u32,
35    /// Absolute byte offset of the compressed block.
36    pub block_offset: u64,
37}
38
39/// Index reader — mmaps the shard file for zero-copy lookups.
40pub struct Reader {
41    mmap: Mmap,
42    /// Parsed shard header containing section offsets and sizes.
43    pub header: Header,
44    string_pool: StringPoolReader<'static>,
45    inode: Option<u64>,
46    cdx_blocks: Vec<CdxBlockEntry>,
47}
48
49/// Descriptor pointing into the trigram table for a single trigram.
50#[derive(Debug)]
51pub struct TrigramInfo {
52    /// Absolute file offset where the posting list begins.
53    pub posting_offset: u64,
54    /// Number of bytes in the encoded posting list.
55    pub posting_length: u32,
56    /// How many files contain this trigram (document frequency).
57    pub doc_frequency: u32,
58}
59
60/// Metadata about a single file known to the index.
61#[derive(Debug)]
62pub struct FileInfo {
63    /// Internal 0-based file identifier.
64    pub file_id: u32,
65    /// Absolute path to the file on disk.
66    pub path: PathBuf,
67    /// Whether the file is fresh, stale, or deleted.
68    pub status: FileStatus,
69    /// Last modification time in nanoseconds since the Unix epoch.
70    pub mtime_ns: u64,
71    /// File size in bytes at index time.
72    pub size_bytes: u64,
73    /// XXH64 content hash computed at index time.
74    pub content_hash: u64,
75}
76
77#[allow(clippy::as_conversions)] // binary format: usize/u32/u64 casts for index decoding
78#[allow(clippy::indexing_slicing)] // binary format: fixed-size buffer ops, length-checked
79impl Reader {
80    /// Open and memory-map an index file for reading.
81    ///
82    /// # Errors
83    ///
84    /// Returns an error if the file cannot be opened, memory-mapped, or its
85    /// header is invalid.
86    pub fn open(path: &Path) -> Result<Self> {
87        let file = File::open(path)?;
88
89        // SAFETY: Mmap::map wraps the mmap(2) syscall. The file handle is kept alive
90        // by Mmap's internal Arc<File>, ensuring the underlying data remains valid
91        // for the lifetime of the mmap.
92        let mmap = unsafe { Mmap::map(&file)? };
93
94        if mmap.len() < HEADER_SIZE {
95            return Err(Error::IndexTooSmall);
96        }
97
98        let header = Header::parse(&mmap[0..HEADER_SIZE])?;
99        header.validate_bounds(mmap.len() as u64)?;
100
101        #[cfg(unix)]
102        let inode = Some(file.metadata()?.ino());
103
104        #[cfg(not(unix))]
105        let inode = None;
106
107        // SAFETY: We transmute the slice lifetime to 'static. This is sound because:
108        // INVARIANT: Reader owns the Mmap, which owns the underlying memory.
109        // INVARIANT: Mmap's data remains valid for the entire lifetime of Reader.
110        // INVARIANT: No mutable access to mmap occurs after construction.
111        // INVARIANT: StringPoolReader<'static> cannot outlive Reader (it's a field).
112        // This is the standard pattern for self-referential mmap structs in Rust.
113        let string_pool_data: &'static [u8] = unsafe {
114            let start = header.string_pool_offset as usize;
115            let end = (header.string_pool_offset + header.string_pool_size) as usize;
116            std::mem::transmute::<&[u8], &'static [u8]>(&mmap[start..end])
117        };
118        let string_pool = StringPoolReader::new(string_pool_data)?;
119
120        let cdx_blocks = if header.has_cdx() && header.cdx_block_index_size > 0 {
121            let idx_start = header.cdx_block_index_offset as usize;
122            let idx_end = idx_start + header.cdx_block_index_size as usize;
123            let idx_data = mmap
124                .get(idx_start..idx_end)
125                .ok_or(Error::SectionOutOfBounds {
126                    section: "cdx_block_index",
127                    offset: header.cdx_block_index_offset,
128                    size: header.cdx_block_index_size,
129                    file_len: mmap.len() as u64,
130                })?;
131            let mut blocks = Vec::new();
132            let mut pos = 0;
133            while pos + 12 <= idx_data.len() {
134                let first_key = u32::from_le_bytes(
135                    idx_data[pos..pos + 4]
136                        .try_into()
137                        .map_err(|_| Error::Config("bad cdx key".into()))?,
138                );
139                if first_key == u32::MAX {
140                    break;
141                }
142                let block_offset = u64::from_le_bytes(
143                    idx_data[pos + 4..pos + 12]
144                        .try_into()
145                        .map_err(|_| Error::Config("bad cdx offset".into()))?,
146                );
147                blocks.push(CdxBlockEntry {
148                    first_key,
149                    block_offset,
150                });
151                pos += 12;
152            }
153            blocks
154        } else {
155            Vec::new()
156        };
157
158        Ok(Self {
159            mmap,
160            header,
161            string_pool,
162            inode,
163            cdx_blocks,
164        })
165    }
166
167    /// Get the last modification time among all source files in the tree.
168    ///
169    /// # Errors
170    ///
171    /// Returns an error if the directory walk fails or metadata cannot be read.
172    pub fn get_last_modified(root: &Path) -> Result<u64> {
173        let mut last_modified = 0u64;
174        let walker = ignore::WalkBuilder::new(root)
175            .hidden(false)
176            .git_ignore(true)
177            .require_git(false)
178            .add_custom_ignore_filename(".ixignore")
179            .filter_entry(move |entry| {
180                let path = entry.path();
181                let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
182
183                if entry.file_type().is_some_and(|t| t.is_dir())
184                    && matches!(
185                        name,
186                        "lost+found"
187                            | ".git"
188                            | "node_modules"
189                            | "target"
190                            | "__pycache__"
191                            | ".tox"
192                            | ".venv"
193                            | "venv"
194                            | ".ix"
195                    )
196                {
197                    return false;
198                }
199
200                if entry.file_type().is_some_and(|t| t.is_file()) {
201                    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
202                    if matches!(
203                        ext,
204                        "so" | "o"
205                            | "dylib"
206                            | "a"
207                            | "dll"
208                            | "exe"
209                            | "pyc"
210                            | "jpg"
211                            | "png"
212                            | "gif"
213                            | "mp4"
214                            | "mp3"
215                            | "pdf"
216                            | "zip"
217                            | "7z"
218                            | "rar"
219                            | "sqlite"
220                            | "db"
221                            | "bin"
222                    ) || name.ends_with(".tar.gz")
223                    {
224                        return false;
225                    }
226                }
227                true
228            })
229            .build();
230
231        for result in walker {
232            match result {
233                Ok(entry) => {
234                    if entry.file_type().is_some_and(|t| t.is_file()) {
235                        let metadata =
236                            entry.metadata().map_err(|e| Error::Config(e.to_string()))?;
237                        let mtime = metadata
238                            .modified()
239                            .and_then(|t| {
240                                t.duration_since(UNIX_EPOCH)
241                                    .map_err(|_| std::io::Error::other("time went backwards"))
242                            })
243                            .map_or(0, |d| d.as_micros() as u64);
244                        if mtime > last_modified {
245                            last_modified = mtime;
246                        }
247                    }
248                }
249                Err(e) => {
250                    eprintln!("ix: warning: stale check skipping path: {e}");
251                }
252            }
253        }
254        Ok(last_modified)
255    }
256
257    /// Binary search the trigram table. Returns `None` if the trigram
258    /// is unknown.
259    ///
260    /// When CDX compression is active, performs a two-level search:
261    /// first on the block index, then within the decompressed block.
262    pub fn get_trigram(&self, trigram: Trigram) -> Option<TrigramInfo> {
263        if self.header.has_cdx() && !self.cdx_blocks.is_empty() {
264            return self.get_trigram_cdx(trigram);
265        }
266
267        // Legacy fallback (no CDX)
268        let count = self.header.trigram_count as usize;
269        let table_start = self.header.trigram_table_offset as usize;
270        let entry_size = crate::format::TRIGRAM_ENTRY_SIZE;
271
272        let mut low = 0;
273        let mut high = count;
274
275        while low < high {
276            let mid = low + (high - low) / 2;
277            let entry_off = table_start + mid * entry_size;
278
279            let key_bytes = self.mmap.get(entry_off..entry_off + 4)?;
280            let key = u32::from_le_bytes(key_bytes.try_into().ok()?);
281
282            match key.cmp(&trigram) {
283                std::cmp::Ordering::Equal => {
284                    let entry = self.mmap.get(entry_off..entry_off + entry_size)?;
285
286                    let mut off_bytes = [0u8; 8];
287                    off_bytes[..6].copy_from_slice(&entry[4..10]);
288                    let posting_offset = u64::from_le_bytes(off_bytes);
289
290                    let posting_length = entry
291                        .get(10..14)
292                        .and_then(|s| s.try_into().ok())
293                        .map(u32::from_le_bytes)?;
294
295                    let doc_frequency = entry
296                        .get(14..18)
297                        .and_then(|s| s.try_into().ok())
298                        .map(u32::from_le_bytes)?;
299
300                    return Some(TrigramInfo {
301                        posting_offset,
302                        posting_length,
303                        doc_frequency,
304                    });
305                }
306                std::cmp::Ordering::Less => low = mid + 1,
307                std::cmp::Ordering::Greater => high = mid,
308            }
309        }
310
311        None
312    }
313
314    fn get_trigram_cdx(&self, trigram: Trigram) -> Option<TrigramInfo> {
315        let mut block_idx = 0;
316        for (i, entry) in self.cdx_blocks.iter().enumerate() {
317            if entry.first_key > trigram {
318                break;
319            }
320            block_idx = i;
321        }
322
323        let block_entry = self.cdx_blocks.get(block_idx)?;
324
325        let block_end = self.cdx_blocks.get(block_idx + 1).map_or_else(
326            || self.header.trigram_table_offset + self.header.trigram_table_size,
327            |next| next.block_offset,
328        );
329
330        let block_start = block_entry.block_offset as usize;
331        let block_end = block_end as usize;
332        let block_data = self.mmap.get(block_start..block_end)?;
333
334        let decompressed = zstd::decode_all(block_data).ok()?;
335
336        let mut pos = 0;
337        let num_entries =
338            usize::try_from(crate::varint::decode(&decompressed, &mut pos).ok()?).unwrap_or(0);
339
340        let mut last_key = 0u32;
341        for _ in 0..num_entries {
342            let key_delta =
343                u32::try_from(crate::varint::decode(&decompressed, &mut pos).ok()?).unwrap_or(0);
344            let key = last_key + key_delta;
345            last_key = key;
346
347            let posting_offset = crate::varint::decode(&decompressed, &mut pos).ok()?;
348            let posting_length =
349                u32::try_from(crate::varint::decode(&decompressed, &mut pos).ok()?).unwrap_or(0);
350            let doc_frequency =
351                u32::try_from(crate::varint::decode(&decompressed, &mut pos).ok()?).unwrap_or(0);
352
353            if key == trigram {
354                return Some(TrigramInfo {
355                    posting_offset,
356                    posting_length,
357                    doc_frequency,
358                });
359            }
360            if key > trigram {
361                break;
362            }
363        }
364
365        None
366    }
367
368    /// Decode the posting list for a given trigram info.
369    ///
370    /// # Errors
371    ///
372    /// Returns an error if the posting data is out of bounds or corrupted.
373    pub fn decode_postings(&self, info: &TrigramInfo) -> Result<PostingList> {
374        let start = info.posting_offset as usize;
375        let end = start + info.posting_length as usize;
376        if end > self.mmap.len() {
377            return Err(Error::PostingOutOfBounds);
378        }
379        PostingList::decode(&self.mmap[start..end])
380    }
381
382    /// Retrieve file metadata by its ID.
383    ///
384    /// # Errors
385    ///
386    /// Returns an error if the file ID is out of bounds or the file table entry
387    /// is malformed.
388    pub fn get_file(&self, file_id: u32) -> Result<FileInfo> {
389        if file_id >= self.header.file_count {
390            return Err(Error::FileIdOutOfBounds(file_id));
391        }
392
393        let entry_off = self.header.file_table_offset as usize + file_id as usize * FILE_ENTRY_SIZE;
394        let entry = self
395            .mmap
396            .get(entry_off..entry_off + FILE_ENTRY_SIZE)
397            .ok_or(Error::SectionOutOfBounds {
398                section: "file_entry",
399                offset: entry_off as u64,
400                size: FILE_ENTRY_SIZE as u64,
401                file_len: self.mmap.len() as u64,
402            })?;
403
404        let path_off = u32::from_le_bytes(
405            entry[4..8]
406                .try_into()
407                .map_err(|_| Error::Config("invalid path offset".into()))?,
408        );
409        let status = FileStatus::from_u8(entry[10]);
410        let mtime_ns = u64::from_le_bytes(
411            entry[12..20]
412                .try_into()
413                .map_err(|_| Error::Config("invalid mtime".into()))?,
414        );
415        let size_bytes = u64::from_le_bytes(
416            entry[20..28]
417                .try_into()
418                .map_err(|_| Error::Config("invalid size".into()))?,
419        );
420        let content_hash = u64::from_le_bytes(
421            entry[28..36]
422                .try_into()
423                .map_err(|_| Error::Config("invalid hash".into()))?,
424        );
425
426        let path = self.string_pool.resolve(path_off)?;
427
428        Ok(FileInfo {
429            file_id,
430            path: PathBuf::from(path),
431            status,
432            mtime_ns,
433            size_bytes,
434            content_hash,
435        })
436    }
437
438    /// Check if a bloom filter for a file may contain a trigram.
439    ///
440    /// # Panics
441    ///
442    /// Panics if the bloom filter bytes in the mmap are not exactly 2 or 4 bytes
443    /// as expected (this should never happen with a valid index file).
444    #[must_use]
445    pub fn bloom_may_contain(&self, file_id: u32, trigram: Trigram) -> bool {
446        if !self.header.has_bloom() {
447            return true;
448        }
449
450        let entry_off = self.header.file_table_offset as usize + file_id as usize * FILE_ENTRY_SIZE;
451        let Some(bloom_bytes) = self.mmap.get(entry_off + 40..entry_off + 44) else {
452            return true;
453        };
454
455        let bloom_rel_off = u32::from_le_bytes(
456            bloom_bytes
457                .try_into()
458                .expect("bloom_bytes is exactly 4 bytes"),
459        );
460        let bloom_abs_off = self.header.bloom_offset as usize + bloom_rel_off as usize;
461
462        let Some(size_bytes) = self.mmap.get(bloom_abs_off..bloom_abs_off + 2) else {
463            return true;
464        };
465        let size = u16::from_le_bytes(
466            size_bytes
467                .try_into()
468                .expect("size_bytes is exactly 2 bytes"),
469        ) as usize;
470
471        let num_hashes = self.mmap.get(bloom_abs_off + 2).copied().unwrap_or(0);
472        let Some(bits) = self.mmap.get(bloom_abs_off + 4..bloom_abs_off + 4 + size) else {
473            return true;
474        };
475
476        BloomFilter::slice_contains(bits, num_hashes, trigram)
477    }
478
479    /// Retrieve high-level shard metadata without parsing the full header.
480    #[must_use]
481    pub const fn metadata(&self) -> ShardMetadata {
482        ShardMetadata {
483            shard_timestamp: self.header.created_at,
484            file_count: self.header.file_count,
485            trigram_count: self.header.trigram_count,
486        }
487    }
488
489    /// Detect whether the shard file on disk has been rebuilt under this live mmap.
490    ///
491    /// Returns `true` if the inode or file size differs, or if the file no longer exists.
492    /// A stale reader should be dropped and reopened.
493    ///
494    /// On Unix: uses inode comparison (inode changes on atomic rename).
495    /// On non-Unix: uses file size comparison only (Windows file locking prevents
496    /// rebuild under live mmap, so size-only detection is sufficient).
497    #[must_use]
498    pub fn is_stale(&self, path: &Path) -> bool {
499        let Ok(current) = std::fs::metadata(path) else {
500            return true;
501        };
502
503        if current.len() as usize != self.mmap.len() {
504            return true;
505        }
506
507        #[cfg(unix)]
508        {
509            if let Some(stored_inode) = self.inode
510                && current.ino() != stored_inode
511            {
512                return true;
513            }
514        }
515
516        false
517    }
518}