Skip to main content

jump_core/
content.rs

1// Copyright 2026 entro314-labs
2// SPDX-License-Identifier: MPL-2.0
3
4//! Full-text search inside files, on SQLite FTS5.
5//!
6//! ## Why this is a separate index from [`crate::files`]
7//!
8//! The two have opposite lifecycles. The path index is cheap to throw away and
9//! rebuild wholesale — 8.75 s for a home directory — so it is simply rebuilt on
10//! a timer. Content is the reverse: extracting text is expensive, so it must be
11//! *incremental*, touching only files whose `(mtime, size)` changed since last
12//! time. Sharing one store would force the expensive half to follow the cheap
13//! half's refresh strategy.
14//!
15//! ## Why FTS5 rather than a vector index
16//!
17//! Launcher queries are overwhelmingly known-item: the user knows roughly what
18//! the file is called or what phrase is in it. That is lexical retrieval, which
19//! BM25 does well and cheaply. Embeddings answer a different question —
20//! semantic recall — and would put a model inference on every keystroke, which
21//! is irreconcilable with a sub-millisecond result path. If semantic ranking is
22//! ever wanted, the shape is a reranker over the top ~50 rows *of this index*,
23//! not a replacement for it.
24//!
25//! ## Bounds
26//!
27//! Indexing user documents can pull in an unbounded amount of data, so every
28//! axis is capped: file size, extensions, and total documents. Extraction is
29//! plain-text only — binary formats need external tools, which are a
30//! configuration and failure surface this does not take on.
31
32use std::path::{Path, PathBuf};
33use std::time::SystemTime;
34
35use rusqlite::{Connection, OptionalExtension, params};
36
37use crate::model::{Icon, Item, ItemKey, Source};
38
39/// Extensions whose contents are indexed.
40///
41/// Text-like formats only. Anything requiring an external extractor is out of
42/// scope: a launcher that shells out to `pdftotext` inherits its failure modes.
43pub const DEFAULT_EXTENSIONS: &[&str] = &[
44    "md",
45    "txt",
46    "rst",
47    "org",
48    "adoc",
49    "tex",
50    "log",
51    "csv",
52    "tsv",
53    "json",
54    "toml",
55    "yaml",
56    "yml",
57    "ini",
58    "conf",
59    "cfg",
60    "xml",
61    "html",
62    "css",
63    "scss",
64    "js",
65    "ts",
66    "jsx",
67    "tsx",
68    "rs",
69    "py",
70    "go",
71    "rb",
72    "sh",
73    "bash",
74    "zsh",
75    "fish",
76    "c",
77    "h",
78    "cpp",
79    "hpp",
80    "java",
81    "kt",
82    "swift",
83    "sql",
84    "lua",
85    "vim",
86    "nix",
87    "dockerfile",
88    "gradle",
89    "properties",
90    "env",
91];
92
93/// Size limits for the content index.
94#[derive(Debug, Clone, Copy)]
95pub struct Limits {
96    /// Stop indexing once the database reaches this size.
97    pub max_index_bytes: u64,
98    /// Skip files larger than this.
99    ///
100    /// Beyond a couple of megabytes a text file is almost always generated — a
101    /// log, a dump, a minified bundle — and indexing it costs far more than it
102    /// returns.
103    pub max_file_bytes: u64,
104}
105
106impl Default for Limits {
107    fn default() -> Self {
108        Self {
109            max_index_bytes: 512 * 1024 * 1024,
110            max_file_bytes: 2 * 1024 * 1024,
111        }
112    }
113}
114
115/// Characters of extracted text stored per document.
116const MAX_TEXT_CHARS: usize = 200_000;
117
118/// Ceiling on indexed documents, so a pathological tree cannot grow the
119/// database without limit.
120const MAX_DOCUMENTS: usize = 200_000;
121
122/// Results returned per query.
123const RESULTS: usize = 8;
124
125/// Documents written per transaction. See [`Content::index`].
126const BATCH: usize = 400;
127
128/// Why a content-index operation failed.
129#[derive(Debug, thiserror::Error)]
130pub enum Error {
131    /// The database could not be opened or its schema not created.
132    #[error("could not open the content index")]
133    Open(#[source] rusqlite::Error),
134    /// A query against an open index failed.
135    #[error("content index query failed")]
136    Query(#[source] rusqlite::Error),
137}
138
139/// Full-text index over file contents.
140pub struct Content {
141    connection: Connection,
142    extensions: Vec<String>,
143    limits: Limits,
144    path: PathBuf,
145    /// Reused across documents so a full pass does not allocate a fresh buffer
146    /// per file. Sized to the largest file accepted, growing at most once.
147    buffer: Vec<u8>,
148}
149
150impl std::fmt::Debug for Content {
151    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152        f.debug_struct("Content").finish_non_exhaustive()
153    }
154}
155
156impl Content {
157    /// Open, creating the schema if needed.
158    ///
159    /// # Errors
160    ///
161    /// [`Error::Open`] if the database cannot be opened or the schema cannot
162    /// be created.
163    pub fn open(path: &Path, extensions: Vec<String>, limits: Limits) -> Result<Self, Error> {
164        if let Some(parent) = path.parent() {
165            let _ = std::fs::create_dir_all(parent);
166        }
167
168        let connection = Connection::open(path).map_err(Error::Open)?;
169
170        // WAL keeps a background reindex from blocking queries, which is the
171        // whole point of indexing off the interactive path.
172        let _ = connection.pragma_update(None, "journal_mode", "WAL");
173        let _ = connection.pragma_update(None, "synchronous", "NORMAL");
174
175        connection
176            .execute_batch(
177                "CREATE TABLE IF NOT EXISTS documents (
178                     path       TEXT PRIMARY KEY,
179                     mtime      INTEGER NOT NULL,
180                     size       INTEGER NOT NULL
181                 );
182                 -- `path` is UNINDEXED: it is carried for retrieval, and the
183                 -- path index already handles matching on names.
184                 CREATE VIRTUAL TABLE IF NOT EXISTS search USING fts5 (
185                     path UNINDEXED,
186                     body,
187                     tokenize = 'unicode61 remove_diacritics 2'
188                 );",
189            )
190            .map_err(Error::Open)?;
191
192        let extensions = if extensions.is_empty() {
193            DEFAULT_EXTENSIONS.iter().map(|e| (*e).to_owned()).collect()
194        } else {
195            extensions
196                .iter()
197                .map(|e| e.trim_start_matches('.').to_lowercase())
198                .collect()
199        };
200
201        Ok(Self {
202            connection,
203            extensions,
204            limits,
205            path: path.to_path_buf(),
206            buffer: Vec::new(),
207        })
208    }
209
210    /// Whether the index has reached its configured size ceiling.
211    ///
212    /// The write-ahead log is counted because it is real space the index is
213    /// using; ignoring it lets the database sit well over its cap between
214    /// checkpoints.
215    #[must_use]
216    pub fn is_full(&self) -> bool {
217        disk_usage(&self.path) >= self.limits.max_index_bytes
218    }
219
220    /// Whether this path is a candidate for content indexing.
221    #[must_use]
222    pub fn accepts(&self, path: &Path) -> bool {
223        accepts(&self.extensions, path)
224    }
225
226    /// Number of indexed documents.
227    #[must_use]
228    pub fn len(&self) -> usize {
229        self.connection
230            .query_row("SELECT count(*) FROM documents", [], |row| {
231                row.get::<_, i64>(0)
232            })
233            .map_or(0, |count| count as usize)
234    }
235
236    /// Whether the index holds no documents.
237    #[must_use]
238    pub fn is_empty(&self) -> bool {
239        self.len() == 0
240    }
241
242    /// Index `paths`, skipping anything unchanged since last time.
243    ///
244    /// Returns how many documents were newly extracted. Callers run this off the
245    /// interactive path.
246    pub fn index(&mut self, paths: &[PathBuf]) -> usize {
247        let mut indexed = 0;
248        let existing = self.len();
249
250        // Batched rather than one transaction per document. With WAL every
251        // commit is an fsync, so committing per file made a first-run index of
252        // ~24 000 documents take minutes; a few hundred documents per
253        // transaction turns that into one fsync per batch.
254        for chunk in paths.chunks(BATCH) {
255            if existing + indexed >= MAX_DOCUMENTS {
256                tracing::warn!(limit = MAX_DOCUMENTS, "content index full; stopping");
257                break;
258            }
259            // Checked per batch rather than per document: `stat` on the database
260            // is cheap but not free, and a batch cannot overshoot the cap by
261            // more than one batch of documents.
262            if self.is_full() {
263                tracing::info!(
264                    limit_mb = self.limits.max_index_bytes / (1024 * 1024),
265                    "content index reached its size limit; stopping"
266                );
267                break;
268            }
269
270            let Content {
271                connection,
272                buffer,
273                limits,
274                extensions,
275                ..
276            } = self;
277            let Ok(transaction) = connection.transaction() else {
278                break;
279            };
280
281            for path in chunk {
282                if !accepts(extensions, path) {
283                    continue;
284                }
285
286                let Ok(metadata) = std::fs::metadata(path) else {
287                    continue;
288                };
289                if !metadata.is_file() || metadata.len() > limits.max_file_bytes {
290                    continue;
291                }
292
293                let size = metadata.len() as i64;
294                let mtime = metadata
295                    .modified()
296                    .ok()
297                    .and_then(|time| time.duration_since(SystemTime::UNIX_EPOCH).ok())
298                    .map_or(0, |age| age.as_secs() as i64);
299
300                if is_current(&transaction, path, mtime, size) {
301                    continue;
302                }
303                if upsert(&transaction, buffer, path, mtime, size) {
304                    indexed += 1;
305                }
306            }
307
308            if transaction.commit().is_err() {
309                break;
310            }
311        }
312
313        if indexed > 0 {
314            tracing::info!(indexed, total = self.len(), "content index updated");
315        }
316        indexed
317    }
318
319    /// Drop documents whose files no longer exist.
320    pub fn prune_missing(&mut self) {
321        let Ok(mut statement) = self.connection.prepare("SELECT path FROM documents") else {
322            return;
323        };
324        let Ok(rows) = statement.query_map([], |row| row.get::<_, String>(0)) else {
325            return;
326        };
327
328        let gone: Vec<String> = rows
329            .flatten()
330            .filter(|path| !Path::new(path).exists())
331            .collect();
332        drop(statement);
333
334        for path in &gone {
335            let _ = self
336                .connection
337                .execute("DELETE FROM search WHERE path = ?1", params![path]);
338            let _ = self
339                .connection
340                .execute("DELETE FROM documents WHERE path = ?1", params![path]);
341        }
342        if !gone.is_empty() {
343            tracing::info!(removed = gone.len(), "pruned deleted documents");
344        }
345    }
346
347    /// Search document contents, ranked by BM25.
348    ///
349    /// # Errors
350    ///
351    /// [`Error::Query`] if the underlying FTS5 query fails.
352    pub fn search(&self, query: &str) -> Result<Vec<Item>, Error> {
353        let Some(expression) = fts_query(query) else {
354            return Ok(Vec::new());
355        };
356
357        let mut statement = self
358            .connection
359            .prepare(
360                // bm25() returns a *negative* score where more negative is more
361                // relevant, so ascending order puts the best match first.
362                "SELECT path,
363                        snippet(search, 1, '', '', '…', 12) AS excerpt,
364                        bm25(search) AS score
365                 FROM search
366                 WHERE search MATCH ?1
367                 ORDER BY score
368                 LIMIT ?2",
369            )
370            .map_err(Error::Query)?;
371
372        let rows = statement
373            .query_map(params![expression, RESULTS as i64], |row| {
374                Ok((
375                    row.get::<_, String>(0)?,
376                    row.get::<_, String>(1)?,
377                    row.get::<_, f64>(2)?,
378                ))
379            })
380            .map_err(Error::Query)?;
381
382        Ok(rows
383            .flatten()
384            .filter_map(|(path, excerpt, score)| {
385                let path = PathBuf::from(path);
386                // The index can lag the filesystem; never offer a file that is
387                // no longer there.
388                path.is_file().then(|| item(&path, &excerpt, score))
389            })
390            .collect())
391    }
392}
393
394/// The files an index at `path` is made of, in the order they are safe to
395/// delete: the write-ahead log and its shared-memory index before the
396/// database. A database left without its log is a valid, slightly older
397/// database; a log left without its database would be replayed into whatever
398/// database is created at that path next.
399fn files(path: &Path) -> [PathBuf; 3] {
400    let beside = |suffix: &str| {
401        let mut name = path.as_os_str().to_owned();
402        name.push(suffix);
403        PathBuf::from(name)
404    };
405    [beside("-wal"), beside("-shm"), path.to_path_buf()]
406}
407
408/// Bytes the content index at `path` occupies on disk, write-ahead log
409/// included. Zero when there is no index.
410#[must_use]
411pub fn disk_usage(path: &Path) -> u64 {
412    files(path)
413        .iter()
414        .filter_map(|file| std::fs::metadata(file).ok())
415        .map(|metadata| metadata.len())
416        .sum()
417}
418
419/// Delete the content index at `path`, returning the bytes that freed.
420///
421/// Nothing may have the index open. SQLite removes the write-ahead log *by
422/// name* when the last connection closes, so a [`Content`] still open across
423/// this call would later delete the log of whichever index had been created
424/// at `path` in the meantime. Drop every [`Content`] for `path` first.
425///
426/// # Errors
427///
428/// The first file that could not be removed. An index that does not exist is
429/// not an error: there is nothing to free.
430pub fn clear(path: &Path) -> std::io::Result<u64> {
431    let freed = disk_usage(path);
432    for file in files(path) {
433        match std::fs::remove_file(&file) {
434            Ok(()) => {}
435            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
436            Err(error) => return Err(error),
437        }
438    }
439    Ok(freed)
440}
441
442/// Whether `path`'s extension is one we index.
443fn accepts(extensions: &[String], path: &Path) -> bool {
444    path.extension()
445        .and_then(|extension| extension.to_str())
446        .is_some_and(|extension| {
447            let extension = extension.to_lowercase();
448            extensions.contains(&extension)
449        })
450}
451
452/// Whether the stored copy already matches what is on disk.
453fn is_current(connection: &Connection, path: &Path, mtime: i64, size: i64) -> bool {
454    let stored: Option<(i64, i64)> = connection
455        .query_row(
456            "SELECT mtime, size FROM documents WHERE path = ?1",
457            params![path.to_string_lossy()],
458            |row| Ok((row.get(0)?, row.get(1)?)),
459        )
460        .optional()
461        .ok()
462        .flatten();
463
464    stored == Some((mtime, size))
465}
466
467/// Extract and store one document, replacing any previous copy.
468fn upsert(
469    connection: &Connection,
470    buffer: &mut Vec<u8>,
471    path: &Path,
472    mtime: i64,
473    size: i64,
474) -> bool {
475    let Some(text) = extract(buffer, path, size as u64) else {
476        return false;
477    };
478    let path_text = path.to_string_lossy().into_owned();
479
480    // FTS5 has no upsert; the old row has to go first or the document is
481    // matched twice, once with stale content.
482    let removed = connection.execute("DELETE FROM search WHERE path = ?1", params![path_text]);
483    let inserted = connection.execute(
484        "INSERT INTO search (path, body) VALUES (?1, ?2)",
485        params![path_text, text],
486    );
487    let recorded = connection.execute(
488        "INSERT INTO documents (path, mtime, size) VALUES (?1, ?2, ?3)
489         ON CONFLICT(path) DO UPDATE SET mtime = excluded.mtime, size = excluded.size",
490        params![path_text, mtime, size],
491    );
492
493    removed.is_ok() && inserted.is_ok() && recorded.is_ok()
494}
495
496/// Build an FTS5 MATCH expression from user input.
497///
498/// Every token is quoted and the quotes inside are doubled, because FTS5 MATCH
499/// is a query language: a bare `AND`, `*`, `"` or `:` from the user would either
500/// change the meaning of the query or raise a syntax error mid-keystroke.
501fn fts_query(query: &str) -> Option<String> {
502    let tokens: Vec<String> = query
503        .split_whitespace()
504        .filter(|token| token.chars().any(char::is_alphanumeric))
505        .map(|token| format!("\"{}\"", token.replace('"', "\"\"")))
506        .collect();
507
508    if tokens.is_empty() {
509        return None;
510    }
511
512    // All terms required, and the last one is a prefix so results appear while
513    // the user is still typing it.
514    let last = tokens.len() - 1;
515    let mut expression = tokens;
516    expression[last].push('*');
517    Some(expression.join(" AND "))
518}
519
520/// Read a file as text, if it looks like text.
521///
522/// Reads into a caller-owned buffer that is reused across the whole pass, so a
523/// 24 000-document index performs one allocation growth rather than 24 000
524/// allocations. `std::fs::read` was doing the latter, plus a second allocation
525/// for the lossy conversion and a third for the truncating `collect`.
526fn extract(buffer: &mut Vec<u8>, path: &Path, size: u64) -> Option<String> {
527    use std::io::Read;
528
529    let file = std::fs::File::open(path).ok()?;
530    buffer.clear();
531    buffer.reserve(size as usize);
532    // Bounded at the reader so a file that grows between `stat` and `read`
533    // cannot pull in more than expected.
534    file.take(size).read_to_end(buffer).ok()?;
535
536    // A NUL byte in the first block is the standard heuristic for "not text",
537    // and catches files whose extension lies.
538    if buffer[..buffer.len().min(8192)].contains(&0) {
539        return None;
540    }
541
542    // Borrow rather than allocate when the bytes are already valid UTF-8, which
543    // is the overwhelmingly common case for the extensions indexed here. The
544    // binding keeps the lossy fallback alive without relying on temporary
545    // lifetime extension inside the match.
546    let lossy;
547    let text = match std::str::from_utf8(buffer) {
548        Ok(text) => text,
549        Err(_) => {
550            lossy = String::from_utf8_lossy(buffer);
551            &lossy
552        }
553    };
554
555    Some(match text.char_indices().nth(MAX_TEXT_CHARS) {
556        Some((cutoff, _)) => text[..cutoff].to_owned(),
557        None => text.to_owned(),
558    })
559}
560
561/// Build a result row from a content hit.
562fn item(path: &Path, excerpt: &str, score: f64) -> Item {
563    let title = path.file_name().map_or_else(
564        || path.display().to_string(),
565        |name| name.to_string_lossy().into_owned(),
566    );
567
568    let excerpt = excerpt.split_whitespace().collect::<Vec<_>>().join(" ");
569
570    // The same filename often exists in several projects — four copies of
571    // CLAUDE.md produced four identical-looking rows. The directory is what
572    // tells them apart, so it leads the subtitle and the excerpt follows.
573    let location = path
574        .parent()
575        .map(|parent| {
576            let text = parent.display().to_string();
577            dirs::home_dir()
578                .and_then(|home| {
579                    text.strip_prefix(home.to_str()?)
580                        .map(|rest| format!("~{rest}"))
581                })
582                .unwrap_or(text)
583        })
584        .unwrap_or_default();
585
586    Item {
587        key: ItemKey(format!("content:{}", path.display())),
588        id: 0,
589        title,
590        // Location plus the matching text: the excerpt explains why the result
591        // is here, the directory says which file it is.
592        subtitle: if location.is_empty() {
593            excerpt
594        } else {
595            format!("{location}  ·  {excerpt}")
596        },
597        icon: Some(Icon::Name("text-x-generic".to_owned())),
598        category_icon: Some(Icon::Name("edit-find-symbolic".to_owned())),
599        window: None,
600        source: Source::File {
601            path: path.to_path_buf(),
602        },
603        // bm25 is negative-better and unbounded; map it onto the positive scale
604        // the rest of the ranking uses. -10 or better is treated as excellent.
605        autocomplete: None,
606        score: (((-score) / 10.0).clamp(0.0, 1.0) as f32) * 3.5,
607    }
608}
609
610#[cfg(test)]
611mod tests {
612    use super::*;
613
614    fn index() -> Content {
615        // A file-backed database in a temporary directory, because FTS5 and WAL
616        // behave differently in memory and the tests should exercise the real
617        // configuration.
618        let dir = std::env::temp_dir().join(format!("jump-content-test-{}", std::process::id()));
619        let _ = std::fs::remove_dir_all(&dir);
620        Content::open(&dir.join("content.db"), Vec::new(), Limits::default()).expect("open index")
621    }
622
623    #[test]
624    fn quotes_tokens_so_fts_syntax_cannot_leak() {
625        // `AND` and `*` are FTS5 operators; unquoted they would change meaning.
626        let expression = fts_query("AND value").expect("expression");
627        assert!(expression.starts_with("\"AND\""));
628        assert!(expression.contains("AND \"value\"*"));
629    }
630
631    #[test]
632    fn embedded_quotes_are_escaped() {
633        let expression = fts_query("say \"hello\"").expect("expression");
634        assert!(expression.contains("\"\"hello\"\""));
635    }
636
637    #[test]
638    fn punctuation_only_queries_are_rejected() {
639        assert!(fts_query("!!! ???").is_none());
640        assert!(fts_query("   ").is_none());
641    }
642
643    #[test]
644    fn extension_filter_matches_case_insensitively() {
645        let content = index();
646        assert!(content.accepts(Path::new("/tmp/a.MD")));
647        assert!(content.accepts(Path::new("/tmp/a.rs")));
648        assert!(!content.accepts(Path::new("/tmp/a.png")));
649    }
650
651    #[test]
652    fn indexes_and_finds_by_content() {
653        let dir = std::env::temp_dir().join("jump-content-find");
654        let _ = std::fs::remove_dir_all(&dir);
655        std::fs::create_dir_all(&dir).expect("create dir");
656
657        let file = dir.join("notes.md");
658        std::fs::write(&file, "the quick brown fox jumps over the lazy dog").expect("write");
659
660        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
661            .expect("open index");
662        assert_eq!(content.index(std::slice::from_ref(&file)), 1);
663
664        let results = content.search("brown fox").expect("search");
665        assert_eq!(results.len(), 1);
666        assert!(results[0].subtitle.contains("brown"));
667
668        // Re-indexing an unchanged file must do nothing.
669        assert_eq!(content.index(std::slice::from_ref(&file)), 0);
670
671        let _ = std::fs::remove_dir_all(&dir);
672    }
673
674    #[test]
675    fn changed_files_are_reindexed_without_duplicating() {
676        let dir = std::env::temp_dir().join("jump-content-change");
677        let _ = std::fs::remove_dir_all(&dir);
678        std::fs::create_dir_all(&dir).expect("create dir");
679
680        let file = dir.join("doc.txt");
681        std::fs::write(&file, "original contents here").expect("write");
682
683        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
684            .expect("open index");
685        content.index(std::slice::from_ref(&file));
686
687        // A different size guarantees the change is detected regardless of
688        // filesystem timestamp granularity.
689        std::fs::write(&file, "replacement contents entirely different now").expect("rewrite");
690        assert_eq!(content.index(std::slice::from_ref(&file)), 1);
691        assert_eq!(content.len(), 1, "the document is replaced, not duplicated");
692
693        assert!(content.search("original").expect("search").is_empty());
694        assert_eq!(content.search("replacement").expect("search").len(), 1);
695
696        let _ = std::fs::remove_dir_all(&dir);
697    }
698
699    #[test]
700    fn binary_files_are_skipped() {
701        let dir = std::env::temp_dir().join("jump-content-binary");
702        let _ = std::fs::remove_dir_all(&dir);
703        std::fs::create_dir_all(&dir).expect("create dir");
704
705        // A .txt containing NUL bytes: the extension lies, the probe catches it.
706        let file = dir.join("fake.txt");
707        std::fs::write(&file, [0x00, 0x01, 0x02, b'h', b'i']).expect("write");
708
709        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
710            .expect("open index");
711        assert_eq!(content.index(&[file]), 0);
712
713        let _ = std::fs::remove_dir_all(&dir);
714    }
715
716    #[test]
717    fn clearing_deletes_the_database_and_its_log() {
718        let dir = tempfile::tempdir().expect("tempdir");
719        let path = dir.path().join("content.db");
720
721        // No index yet: nothing to free, and not an error.
722        assert_eq!(clear(&path).expect("clear nothing"), 0);
723
724        let file = dir.path().join("diary.md");
725        std::fs::write(&file, "words nobody else should find").expect("write");
726        let mut content = Content::open(&path, Vec::new(), Limits::default()).expect("open index");
727        assert_eq!(content.index(std::slice::from_ref(&file)), 1);
728
729        // While the index is open its write-ahead log is on disk beside it,
730        // and counts towards what the index occupies.
731        let [log, shared, database] = files(&path);
732        assert!(log.exists(), "WAL mode keeps a log beside the database");
733        let database_bytes = std::fs::metadata(&database).expect("database").len();
734        assert!(disk_usage(&path) > database_bytes);
735
736        // Closing checkpoints the log away; put both sidecars back, as a
737        // launcher that was killed mid-index would have left them.
738        drop(content);
739        std::fs::write(&log, b"left behind").expect("log");
740        std::fs::write(&shared, b"left behind").expect("shared memory");
741
742        let occupied = disk_usage(&path);
743        assert_eq!(clear(&path).expect("clear"), occupied);
744        for file in files(&path) {
745            assert!(!file.exists(), "{} survived", file.display());
746        }
747        assert_eq!(disk_usage(&path), 0);
748        // The document itself is not the index's to delete.
749        assert!(file.exists());
750
751        // Switching content search back on starts from nothing.
752        let content = Content::open(&path, Vec::new(), Limits::default()).expect("reopen");
753        assert!(content.is_empty());
754        assert!(content.search("nobody").expect("search").is_empty());
755    }
756
757    #[test]
758    fn deleted_files_are_pruned() {
759        let dir = std::env::temp_dir().join("jump-content-prune");
760        let _ = std::fs::remove_dir_all(&dir);
761        std::fs::create_dir_all(&dir).expect("create dir");
762
763        let file = dir.join("temp.md");
764        std::fs::write(&file, "some indexed words").expect("write");
765
766        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
767            .expect("open index");
768        content.index(std::slice::from_ref(&file));
769        assert_eq!(content.len(), 1);
770
771        std::fs::remove_file(&file).expect("remove");
772        content.prune_missing();
773        assert_eq!(content.len(), 0);
774
775        let _ = std::fs::remove_dir_all(&dir);
776    }
777}