M2.3: find in files backend
crates/search: parallel ignore walk + grep-searcher with the project's
ignore rules (vendor searched as library, node_modules and .git skipped),
literal or regex, case, whole word, include/exclude globs, binary and
> 8 MiB skips, match cap, cancel token, buffer overlay.
Engine: search.find returns {searchId} at once and runs off the lock;
pump_vfs drains results into searchResults (one per file, at most 500
per pump) and searchDone; search.cancel. Testkit: generated search tree.
Tests: library unit and property tests, engine tests, TCP control test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a6b2fd1300
commit
172693c8dd
21 changed files with 2231 additions and 5 deletions
|
|
@ -12,6 +12,17 @@ publish.workspace = true
|
|||
layer = 2
|
||||
|
||||
[dependencies]
|
||||
globset = "0.4.18"
|
||||
grep-matcher = "0.1.7"
|
||||
grep-regex = "0.1.13"
|
||||
grep-searcher = "0.1.14"
|
||||
ignore.workspace = true
|
||||
thiserror = "2.0.21"
|
||||
|
||||
[dev-dependencies]
|
||||
lutra-testkit.workspace = true
|
||||
proptest.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -2,3 +2,234 @@
|
|||
//!
|
||||
//! Streams matches from the project files off the UI thread, honouring the
|
||||
//! ignore rules of the project model.
|
||||
//!
|
||||
//! # Query
|
||||
//! A [`Query`] is compiled once into a [`Search`] ([`Search::new`]): a
|
||||
//! literal or a regular expression (Rust `regex` syntax), case sensitive or
|
||||
//! not, optionally whole words only, plus include and exclude globs. An
|
||||
//! invalid pattern or glob fails with [`SearchError`] before anything runs.
|
||||
//! Patterns never match across lines.
|
||||
//!
|
||||
//! # Which files are searched
|
||||
//! The walk follows the rules of `lutra-vfs`:
|
||||
//! - `.gitignore`, `.ignore`, `.git/info/exclude` and the global git excludes
|
||||
//! apply whether or not the folder is a git repository; ignored files and
|
||||
//! everything below an ignored directory are skipped. Ignore files above
|
||||
//! the project root are not read.
|
||||
//! - `.git` (directory or file) at any depth and every `node_modules`
|
||||
//! directory are skipped.
|
||||
//! - `vendor/` directly under the root is library code and is searched even
|
||||
//! when an ignore file lists it (Composer projects usually do); ignore
|
||||
//! files inside it still apply.
|
||||
//! - Hidden (dot) files are searched, as the project tree shows them.
|
||||
//! Symbolic links are not followed.
|
||||
//! - Include globs keep only files that match one of them, exclude globs
|
||||
//! drop files and whole directories. A glob without `/` matches the file
|
||||
//! or directory name (`*.php`), a glob with `/` the project-relative path
|
||||
//! (`src/**/*.php`).
|
||||
//! - Files larger than [`MAX_FILE_SIZE`], binary files (a NUL byte) and
|
||||
//! files that cannot be read are skipped.
|
||||
//!
|
||||
//! # Results
|
||||
//! Each file with matches is reported once as [`FileMatches`], in no
|
||||
//! particular order (the walk is parallel). Positions are 0-based; columns
|
||||
//! and lengths count UTF-16 code units. Lines end at `\n` (a `\r` before it
|
||||
//! is not part of the line). The preview is the line, or a window of at most
|
||||
//! [`MAX_PREVIEW_CHARS`] characters around the match for longer lines.
|
||||
//! At most [`Query::max_results`] matches are reported in total; the
|
||||
//! [`Summary`] says whether the cap cut the search short.
|
||||
//!
|
||||
//! # Threads and cancellation
|
||||
//! [`run`] blocks the calling thread and searches on a pool of up to
|
||||
//! [`MAX_THREADS`] walker threads; [`spawn`] runs it on a thread of its own
|
||||
//! and delivers [`SearchEvent`]s over a channel. A [`CancelToken`] stops a
|
||||
//! running search between files and between lines.
|
||||
//!
|
||||
//! # Open buffers
|
||||
//! An [`Overlay`] maps project-relative paths to text that replaces the file
|
||||
//! on disk, so unsaved edits are searched instead of the stale file. Only
|
||||
//! files the walk visits are looked up; text for a path that is not on disk
|
||||
//! (or is skipped by the rules) is not searched.
|
||||
|
||||
mod lines;
|
||||
mod query;
|
||||
mod walk;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{Receiver, channel};
|
||||
use std::time::Duration;
|
||||
|
||||
pub use query::Search;
|
||||
pub use walk::run;
|
||||
|
||||
/// Largest file searched, in bytes (8 MiB).
|
||||
pub const MAX_FILE_SIZE: u64 = 8 * 1024 * 1024;
|
||||
|
||||
/// Default cap on the number of matches of one search.
|
||||
pub const DEFAULT_MAX_RESULTS: usize = 10_000;
|
||||
|
||||
/// Highest cap a query may ask for.
|
||||
pub const MAX_RESULTS_LIMIT: usize = 100_000;
|
||||
|
||||
/// Longest preview, in characters (Unicode scalar values).
|
||||
pub const MAX_PREVIEW_CHARS: usize = 300;
|
||||
|
||||
/// Most walker threads of one search.
|
||||
pub const MAX_THREADS: usize = 8;
|
||||
|
||||
/// What to search for and where.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Query {
|
||||
/// The text or regular expression; must not be empty.
|
||||
pub pattern: String,
|
||||
/// Treat `pattern` as a regular expression instead of literal text.
|
||||
pub regex: bool,
|
||||
/// Match letter case exactly.
|
||||
pub case_sensitive: bool,
|
||||
/// Only matches that start and end at word boundaries.
|
||||
pub whole_word: bool,
|
||||
/// Only files matching one of these globs; all files when empty.
|
||||
pub include: Vec<String>,
|
||||
/// Skip files and directories matching one of these globs.
|
||||
pub exclude: Vec<String>,
|
||||
/// Stop after this many matches; clamped to `1..=`[`MAX_RESULTS_LIMIT`].
|
||||
pub max_results: usize,
|
||||
}
|
||||
|
||||
impl Query {
|
||||
/// A literal, case-insensitive query for `pattern` with the default cap.
|
||||
pub fn literal(pattern: impl Into<String>) -> Self {
|
||||
Self {
|
||||
pattern: pattern.into(),
|
||||
regex: false,
|
||||
case_sensitive: false,
|
||||
whole_word: false,
|
||||
include: Vec::new(),
|
||||
exclude: Vec::new(),
|
||||
max_results: DEFAULT_MAX_RESULTS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a query cannot run.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum SearchError {
|
||||
/// The pattern is empty.
|
||||
#[error("the search text is empty")]
|
||||
EmptyPattern,
|
||||
/// The pattern is not a valid regular expression, or matches a line
|
||||
/// break.
|
||||
#[error("invalid pattern: {0}")]
|
||||
InvalidPattern(String),
|
||||
/// An include or exclude glob is invalid.
|
||||
#[error("invalid glob: {0}")]
|
||||
InvalidGlob(String),
|
||||
}
|
||||
|
||||
/// One match.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Match {
|
||||
/// 0-based line.
|
||||
pub line: usize,
|
||||
/// Start in the line, in UTF-16 code units.
|
||||
pub column: usize,
|
||||
/// Length in UTF-16 code units; never 0.
|
||||
pub length: usize,
|
||||
/// The line, or a window of it around the match, without the line break.
|
||||
pub preview: String,
|
||||
/// Start of the match inside `preview`, in UTF-16 code units.
|
||||
pub preview_column: usize,
|
||||
}
|
||||
|
||||
/// The matches in one file, in line order.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FileMatches {
|
||||
/// Project-relative path with `/` separators.
|
||||
pub path: String,
|
||||
/// The matches; never empty.
|
||||
pub matches: Vec<Match>,
|
||||
}
|
||||
|
||||
/// How a search ended.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct Summary {
|
||||
/// Files with at least one reported match.
|
||||
pub files: usize,
|
||||
/// Matches reported.
|
||||
pub matches: usize,
|
||||
/// The match cap stopped the search; more matches may exist.
|
||||
pub truncated: bool,
|
||||
/// The search was cancelled before it finished.
|
||||
pub cancelled: bool,
|
||||
/// Wall time from start to end.
|
||||
pub elapsed: Duration,
|
||||
}
|
||||
|
||||
/// What a search started with [`spawn`] delivers, in order: any number of
|
||||
/// files, then exactly one summary.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SearchEvent {
|
||||
/// A file with matches.
|
||||
File(FileMatches),
|
||||
/// The search ended; nothing follows.
|
||||
Done(Summary),
|
||||
}
|
||||
|
||||
/// Text of open buffers by project-relative path (`/` separators), searched
|
||||
/// instead of the file on disk.
|
||||
pub type Overlay = HashMap<String, String>;
|
||||
|
||||
/// Stops a running search. Clones share the flag.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CancelToken(Arc<AtomicBool>);
|
||||
|
||||
impl CancelToken {
|
||||
/// A token that is not cancelled.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Asks the search to stop; it ends with [`Summary::cancelled`] set.
|
||||
pub fn cancel(&self) {
|
||||
self.0.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Whether [`CancelToken::cancel`] was called.
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
self.0.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs [`run`] on a new thread and returns the channel of its events. A
|
||||
/// receiver that is dropped does not stop the search; cancel it with
|
||||
/// `cancel`.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `root`: the project root (absolute).
|
||||
/// - `search`: the compiled query.
|
||||
/// - `overlay`: buffer texts that replace files on disk.
|
||||
/// - `cancel`: stops the search.
|
||||
///
|
||||
/// # Errors
|
||||
/// The OS refused to start the thread.
|
||||
pub fn spawn(
|
||||
root: PathBuf,
|
||||
search: Search,
|
||||
overlay: Overlay,
|
||||
cancel: CancelToken,
|
||||
) -> std::io::Result<Receiver<SearchEvent>> {
|
||||
let (tx, rx) = channel();
|
||||
std::thread::Builder::new()
|
||||
.name("lutra-search".into())
|
||||
.spawn(move || {
|
||||
let files = tx.clone();
|
||||
let summary = run(&root, &search, &overlay, &cancel, &move |file| {
|
||||
let _receiver_gone = files.send(SearchEvent::File(file));
|
||||
});
|
||||
let _receiver_gone = tx.send(SearchEvent::Done(summary));
|
||||
})?;
|
||||
Ok(rx)
|
||||
}
|
||||
|
|
|
|||
195
crates/search/src/lines.rs
Normal file
195
crates/search/src/lines.rs
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
//! Turning a matching line into [`Match`]es: UTF-16 columns and previews.
|
||||
|
||||
use grep_matcher::Matcher;
|
||||
use grep_regex::RegexMatcher;
|
||||
|
||||
use crate::{MAX_PREVIEW_CHARS, Match};
|
||||
|
||||
/// Characters shown before the match when a long line is cut to a window.
|
||||
const PREVIEW_LEAD: usize = 80;
|
||||
|
||||
/// Appends the matches of one line to `out`, at most `limit` in total.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `matcher`: the compiled pattern.
|
||||
/// - `line`: 0-based line number.
|
||||
/// - `bytes`: the line as read, with or without its line break. Invalid
|
||||
/// UTF-8 is replaced by U+FFFD before matching, so positions refer to the
|
||||
/// text an editor shows.
|
||||
/// - `out`: receives the matches; zero-length matches are skipped.
|
||||
/// - `limit`: stop once `out` holds this many matches.
|
||||
///
|
||||
/// A matcher error (none is expected for an in-memory regex) ends the line
|
||||
/// early; the matches found so far are kept.
|
||||
pub(crate) fn collect(
|
||||
matcher: &RegexMatcher,
|
||||
line: usize,
|
||||
bytes: &[u8],
|
||||
out: &mut Vec<Match>,
|
||||
limit: usize,
|
||||
) {
|
||||
let bytes = trim_line_break(bytes);
|
||||
let text = String::from_utf8_lossy(bytes);
|
||||
let mut spans = Vec::new();
|
||||
let _matcher_error = matcher.find_iter(text.as_bytes(), |m| {
|
||||
if m.start() < m.end() {
|
||||
spans.push((m.start(), m.end()));
|
||||
}
|
||||
out.len().saturating_add(spans.len()) < limit
|
||||
});
|
||||
if spans.is_empty() {
|
||||
return;
|
||||
}
|
||||
let window = Window::new(&text);
|
||||
let mut cursor = Cursor::default();
|
||||
for (start, end) in spans {
|
||||
let Some(matched) = text.get(start..end) else {
|
||||
continue;
|
||||
};
|
||||
if !cursor.advance(&text, start) {
|
||||
continue;
|
||||
}
|
||||
let (preview, preview_column) = window.preview(&text, &cursor, start);
|
||||
out.push(Match {
|
||||
line,
|
||||
column: cursor.utf16,
|
||||
length: utf16_len(matched),
|
||||
preview,
|
||||
preview_column,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// `bytes` without a trailing `\n` or `\r\n`.
|
||||
fn trim_line_break(bytes: &[u8]) -> &[u8] {
|
||||
let bytes = bytes.strip_suffix(b"\n").unwrap_or(bytes);
|
||||
bytes.strip_suffix(b"\r").unwrap_or(bytes)
|
||||
}
|
||||
|
||||
/// Number of UTF-16 code units of `s`.
|
||||
fn utf16_len(s: &str) -> usize {
|
||||
s.chars().map(char::len_utf16).sum()
|
||||
}
|
||||
|
||||
/// A position in the line, counted three ways; only moves forward.
|
||||
#[derive(Default)]
|
||||
struct Cursor {
|
||||
/// Byte offset.
|
||||
byte: usize,
|
||||
/// Characters before `byte`.
|
||||
chars: usize,
|
||||
/// UTF-16 code units before `byte`.
|
||||
utf16: usize,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
/// Moves to byte offset `to`; false if `to` lies behind the cursor or
|
||||
/// not on a character boundary.
|
||||
fn advance(&mut self, text: &str, to: usize) -> bool {
|
||||
let Some(between) = text.get(self.byte..to) else {
|
||||
return false;
|
||||
};
|
||||
for c in between.chars() {
|
||||
self.chars = self.chars.saturating_add(1);
|
||||
self.utf16 = self.utf16.saturating_add(c.len_utf16());
|
||||
}
|
||||
self.byte = to;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// How previews of one line are cut.
|
||||
enum Window {
|
||||
/// The line is short enough to be the preview of every match.
|
||||
Whole,
|
||||
/// The line is longer than [`MAX_PREVIEW_CHARS`]: the byte offset of
|
||||
/// every character, plus the text length at the end.
|
||||
Cut(Vec<usize>),
|
||||
}
|
||||
|
||||
impl Window {
|
||||
/// Prepares previews for `text`.
|
||||
fn new(text: &str) -> Self {
|
||||
if text.len() <= MAX_PREVIEW_CHARS || text.chars().count() <= MAX_PREVIEW_CHARS {
|
||||
return Self::Whole;
|
||||
}
|
||||
let mut offsets: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
|
||||
offsets.push(text.len());
|
||||
Self::Cut(offsets)
|
||||
}
|
||||
|
||||
/// The preview for a match at `cursor` (byte offset `start`), and the
|
||||
/// match's UTF-16 column inside it.
|
||||
fn preview(&self, text: &str, cursor: &Cursor, start: usize) -> (String, usize) {
|
||||
let Self::Cut(offsets) = self else {
|
||||
return (text.to_string(), cursor.utf16);
|
||||
};
|
||||
let total = offsets.len().saturating_sub(1);
|
||||
let first = cursor
|
||||
.chars
|
||||
.saturating_sub(PREVIEW_LEAD)
|
||||
.min(total.saturating_sub(MAX_PREVIEW_CHARS));
|
||||
let last = first.saturating_add(MAX_PREVIEW_CHARS).min(total);
|
||||
let from = offsets.get(first).copied().unwrap_or(0);
|
||||
let to = offsets.get(last).copied().unwrap_or(text.len());
|
||||
let preview = text.get(from..to).unwrap_or_default().to_string();
|
||||
let column = text.get(from..start).map(utf16_len).unwrap_or(0);
|
||||
(preview, column)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use grep_regex::RegexMatcherBuilder;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn matcher(pattern: &str) -> RegexMatcher {
|
||||
RegexMatcherBuilder::new()
|
||||
.fixed_strings(true)
|
||||
.line_terminator(Some(b'\n'))
|
||||
.build(pattern)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn columns_count_utf16_units() {
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("x"), 4, "ä😀x and x\r\n".as_bytes(), &mut out, 10);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!((out[0].line, out[0].column, out[0].length), (4, 3, 1));
|
||||
assert_eq!(out[0].preview, "ä😀x and x");
|
||||
assert_eq!(out[0].preview_column, 3);
|
||||
assert_eq!(out[1].column, 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_lines_are_cut_around_the_match() {
|
||||
let line = format!("{}needle{}", "a".repeat(1000), "b".repeat(1000));
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("needle"), 0, line.as_bytes(), &mut out, 10);
|
||||
let m = &out[0];
|
||||
assert_eq!(m.column, 1000);
|
||||
assert_eq!(m.preview.chars().count(), MAX_PREVIEW_CHARS);
|
||||
assert_eq!(
|
||||
m.preview.get(m.preview_column..m.preview_column + 6),
|
||||
Some("needle")
|
||||
);
|
||||
assert_eq!(m.preview_column, PREVIEW_LEAD);
|
||||
|
||||
let tail = format!("{}needle", "a".repeat(1000));
|
||||
out.clear();
|
||||
collect(&matcher("needle"), 0, tail.as_bytes(), &mut out, 10);
|
||||
assert!(out[0].preview.ends_with("needle"));
|
||||
assert_eq!(out[0].preview.chars().count(), MAX_PREVIEW_CHARS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_utf8_and_limits() {
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("ab"), 0, b"\xffab ab ab\n", &mut out, 2);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(out[0].column, 1);
|
||||
assert_eq!(out[0].preview, "\u{fffd}ab ab ab");
|
||||
}
|
||||
}
|
||||
139
crates/search/src/query.rs
Normal file
139
crates/search/src/query.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
//! Compiling a [`Query`]: the matcher and the path filters.
|
||||
|
||||
use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
|
||||
use grep_regex::{RegexMatcher, RegexMatcherBuilder};
|
||||
|
||||
use crate::{MAX_RESULTS_LIMIT, Query, SearchError};
|
||||
|
||||
/// Largest compiled regex, in bytes; bigger patterns are rejected instead of
|
||||
/// using unbounded memory.
|
||||
const REGEX_SIZE_LIMIT: usize = 16 * 1024 * 1024;
|
||||
|
||||
/// Most nesting a pattern may use.
|
||||
const REGEX_NEST_LIMIT: u32 = 100;
|
||||
|
||||
/// A compiled query, ready to run.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Search {
|
||||
/// Matches one line at a time.
|
||||
pub(crate) matcher: RegexMatcher,
|
||||
/// Include and exclude globs.
|
||||
pub(crate) filter: PathFilter,
|
||||
/// Cap on reported matches.
|
||||
pub(crate) max_results: usize,
|
||||
}
|
||||
|
||||
impl Search {
|
||||
/// Compiles `query`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`SearchError::EmptyPattern`], [`SearchError::InvalidPattern`] for a
|
||||
/// bad regular expression (or one that can match a line break),
|
||||
/// [`SearchError::InvalidGlob`] for a bad include or exclude glob.
|
||||
pub fn new(query: &Query) -> Result<Self, SearchError> {
|
||||
if query.pattern.is_empty() {
|
||||
return Err(SearchError::EmptyPattern);
|
||||
}
|
||||
let matcher = RegexMatcherBuilder::new()
|
||||
.fixed_strings(!query.regex)
|
||||
.case_insensitive(!query.case_sensitive)
|
||||
.word(query.whole_word)
|
||||
.line_terminator(Some(b'\n'))
|
||||
.crlf(true)
|
||||
.size_limit(REGEX_SIZE_LIMIT)
|
||||
.dfa_size_limit(REGEX_SIZE_LIMIT)
|
||||
.nest_limit(REGEX_NEST_LIMIT)
|
||||
.build(&query.pattern)
|
||||
.map_err(|e| SearchError::InvalidPattern(e.to_string()))?;
|
||||
Ok(Self {
|
||||
matcher,
|
||||
filter: PathFilter::new(&query.include, &query.exclude)?,
|
||||
max_results: query.max_results.clamp(1, MAX_RESULTS_LIMIT),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Include and exclude globs, split by whether they match a name or a path.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct PathFilter {
|
||||
/// Include globs; `None` when every file is included.
|
||||
include: Option<Globs>,
|
||||
/// Exclude globs; `None` when nothing is excluded.
|
||||
exclude: Option<Globs>,
|
||||
}
|
||||
|
||||
/// Globs matched against the file name and against the relative path.
|
||||
#[derive(Debug, Clone)]
|
||||
struct Globs {
|
||||
/// Globs without `/`.
|
||||
names: GlobSet,
|
||||
/// Globs with `/`.
|
||||
paths: GlobSet,
|
||||
}
|
||||
|
||||
impl PathFilter {
|
||||
/// Compiles the globs; blank ones are ignored.
|
||||
fn new(include: &[String], exclude: &[String]) -> Result<Self, SearchError> {
|
||||
Ok(Self {
|
||||
include: Globs::new(include)?,
|
||||
exclude: Globs::new(exclude)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the file at `rel` (project-relative, `/` separators) passes
|
||||
/// the include and exclude globs.
|
||||
pub(crate) fn file(&self, rel: &str) -> bool {
|
||||
let included = self.include.as_ref().is_none_or(|g| g.matches(rel));
|
||||
included && !self.excluded(rel)
|
||||
}
|
||||
|
||||
/// Whether an exclude glob matches the file or directory at `rel`.
|
||||
pub(crate) fn excluded(&self, rel: &str) -> bool {
|
||||
self.exclude.as_ref().is_some_and(|g| g.matches(rel))
|
||||
}
|
||||
}
|
||||
|
||||
impl Globs {
|
||||
/// Compiles `globs`; `None` if there is none. Leading and trailing `/`
|
||||
/// are dropped, so `tests/` excludes every directory named `tests`.
|
||||
fn new(globs: &[String]) -> Result<Option<Self>, SearchError> {
|
||||
let mut names = GlobSetBuilder::new();
|
||||
let mut paths = GlobSetBuilder::new();
|
||||
let mut any = false;
|
||||
for glob in globs.iter().map(|g| g.trim()).filter(|g| !g.is_empty()) {
|
||||
let pattern = glob.trim_matches('/');
|
||||
if pattern.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let compiled = GlobBuilder::new(pattern)
|
||||
.literal_separator(true)
|
||||
.backslash_escape(true)
|
||||
.build()
|
||||
.map_err(|e| SearchError::InvalidGlob(e.to_string()))?;
|
||||
if pattern.contains('/') {
|
||||
paths.add(compiled);
|
||||
} else {
|
||||
names.add(compiled);
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
if !any {
|
||||
return Ok(None);
|
||||
}
|
||||
let build = |b: GlobSetBuilder| {
|
||||
b.build()
|
||||
.map_err(|e| SearchError::InvalidGlob(e.to_string()))
|
||||
};
|
||||
Ok(Some(Self {
|
||||
names: build(names)?,
|
||||
paths: build(paths)?,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Whether `rel`'s last component matches a name glob or `rel` a path
|
||||
/// glob.
|
||||
fn matches(&self, rel: &str) -> bool {
|
||||
let name = rel.rsplit('/').next().unwrap_or(rel);
|
||||
self.names.is_match(name) || self.paths.is_match(rel)
|
||||
}
|
||||
}
|
||||
280
crates/search/src/walk.rs
Normal file
280
crates/search/src/walk.rs
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
//! The parallel walk over the project and the per-file search.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::time::Instant;
|
||||
|
||||
use grep_matcher::LineTerminator;
|
||||
use grep_regex::RegexMatcher;
|
||||
use grep_searcher::{BinaryDetection, Searcher, SearcherBuilder, Sink, SinkMatch};
|
||||
use ignore::{DirEntry, WalkBuilder, WalkState};
|
||||
|
||||
use crate::query::PathFilter;
|
||||
use crate::{
|
||||
CancelToken, FileMatches, MAX_FILE_SIZE, MAX_THREADS, Match, Overlay, Search, Summary, lines,
|
||||
};
|
||||
|
||||
/// Searches the project at `root` and calls `on_file` for every file with
|
||||
/// matches, from the walker threads, while the search runs. Blocks until
|
||||
/// the walk ends, the cap is reached or `cancel` fires.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `root`: the project root (absolute).
|
||||
/// - `search`: the compiled query.
|
||||
/// - `overlay`: buffer texts that replace files on disk.
|
||||
/// - `cancel`: stops the search; checked between files and lines.
|
||||
/// - `on_file`: receives each file's matches.
|
||||
///
|
||||
/// # Returns
|
||||
/// The totals. Files that cannot be read are skipped silently: a search
|
||||
/// reports what it can read, like the project tree lists what it can.
|
||||
pub fn run(
|
||||
root: &Path,
|
||||
search: &Search,
|
||||
overlay: &Overlay,
|
||||
cancel: &CancelToken,
|
||||
on_file: &(dyn Fn(FileMatches) + Sync),
|
||||
) -> Summary {
|
||||
let started = Instant::now();
|
||||
let totals = Totals::default();
|
||||
walker(root, &search.filter).build_parallel().run(|| {
|
||||
let mut searcher = SearcherBuilder::new()
|
||||
.line_terminator(LineTerminator::crlf())
|
||||
.line_number(true)
|
||||
.binary_detection(BinaryDetection::quit(0))
|
||||
.build();
|
||||
let totals = &totals;
|
||||
Box::new(move |entry| {
|
||||
if cancel.is_cancelled() || totals.full.load(Ordering::Relaxed) {
|
||||
return WalkState::Quit;
|
||||
}
|
||||
let Ok(entry) = entry else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let Some(rel) = file_path(root, &entry, &search.filter) else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let room = search
|
||||
.max_results
|
||||
.saturating_sub(totals.matches.load(Ordering::Relaxed));
|
||||
let Some(mut matches) = search_file(
|
||||
&mut searcher,
|
||||
&search.matcher,
|
||||
entry.path(),
|
||||
overlay.get(&rel),
|
||||
room,
|
||||
cancel,
|
||||
) else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let granted = totals.reserve(matches.len(), search.max_results);
|
||||
if granted < matches.len() {
|
||||
matches.truncate(granted);
|
||||
totals.full.store(true, Ordering::Relaxed);
|
||||
}
|
||||
if granted > 0 {
|
||||
totals.files.fetch_add(1, Ordering::Relaxed);
|
||||
on_file(FileMatches { path: rel, matches });
|
||||
}
|
||||
if totals.full.load(Ordering::Relaxed) {
|
||||
WalkState::Quit
|
||||
} else {
|
||||
WalkState::Continue
|
||||
}
|
||||
})
|
||||
});
|
||||
let cancelled = cancel.is_cancelled();
|
||||
Summary {
|
||||
files: totals.files.load(Ordering::Relaxed),
|
||||
matches: totals.matches.load(Ordering::Relaxed),
|
||||
truncated: totals.full.load(Ordering::Relaxed) && !cancelled,
|
||||
cancelled,
|
||||
elapsed: started.elapsed(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Counters shared by the walker threads.
|
||||
#[derive(Default)]
|
||||
struct Totals {
|
||||
/// Files reported.
|
||||
files: AtomicUsize,
|
||||
/// Matches reported.
|
||||
matches: AtomicUsize,
|
||||
/// The cap was reached, or a file had more matches than fit.
|
||||
full: AtomicBool,
|
||||
}
|
||||
|
||||
impl Totals {
|
||||
/// Takes up to `wanted` matches from what is left under `cap` and
|
||||
/// returns how many were granted. The caller marks the totals full when
|
||||
/// it got fewer than it wanted: only then is a match known to be left
|
||||
/// out.
|
||||
fn reserve(&self, wanted: usize, cap: usize) -> usize {
|
||||
let mut used = self.matches.load(Ordering::Relaxed);
|
||||
loop {
|
||||
let granted = wanted.min(cap.saturating_sub(used));
|
||||
match self.matches.compare_exchange_weak(
|
||||
used,
|
||||
used.saturating_add(granted),
|
||||
Ordering::Relaxed,
|
||||
Ordering::Relaxed,
|
||||
) {
|
||||
Ok(_) => return granted,
|
||||
Err(now) => used = now,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The walker over `root` with the project's rules (see the crate docs).
|
||||
fn walker(root: &Path, filter: &PathFilter) -> WalkBuilder {
|
||||
let threads = std::thread::available_parallelism()
|
||||
.map(|n| n.get())
|
||||
.unwrap_or(2)
|
||||
.clamp(1, MAX_THREADS);
|
||||
let mut builder = WalkBuilder::new(root);
|
||||
builder
|
||||
.hidden(false)
|
||||
.parents(false)
|
||||
.ignore(true)
|
||||
.git_ignore(true)
|
||||
.git_exclude(true)
|
||||
.git_global(true)
|
||||
.require_git(false)
|
||||
.follow_links(false)
|
||||
.threads(threads);
|
||||
let vendor = root.join("vendor");
|
||||
let vendor_is_dir = std::fs::symlink_metadata(&vendor).is_ok_and(|m| m.is_dir());
|
||||
if vendor_is_dir {
|
||||
builder.add(&vendor);
|
||||
}
|
||||
let root = root.to_path_buf();
|
||||
let filter = filter.clone();
|
||||
builder.filter_entry(move |entry| keep_entry(&root, &vendor, &filter, entry));
|
||||
builder
|
||||
}
|
||||
|
||||
/// Whether the walk enters (directory) or visits (file) `entry`: `.git` and
|
||||
/// `node_modules` are skipped, the root's `vendor` is reached through its
|
||||
/// own walk root instead of the project root's walk, and exclude globs drop
|
||||
/// whole directories.
|
||||
fn keep_entry(root: &Path, vendor: &Path, filter: &PathFilter, entry: &DirEntry) -> bool {
|
||||
let name = entry.file_name();
|
||||
if name == ".git" {
|
||||
return false;
|
||||
}
|
||||
let is_dir = entry.file_type().is_some_and(|t| t.is_dir());
|
||||
if is_dir && name == "node_modules" {
|
||||
return false;
|
||||
}
|
||||
if entry.depth() == 1 && entry.path() == vendor {
|
||||
return false;
|
||||
}
|
||||
if is_dir && let Some(rel) = relative(root, entry.path()) {
|
||||
return !filter.excluded(&rel);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The project-relative path of `entry` if it is a regular file that the
|
||||
/// globs keep.
|
||||
fn file_path(root: &Path, entry: &DirEntry, filter: &PathFilter) -> Option<String> {
|
||||
if !entry.file_type().is_some_and(|t| t.is_file()) {
|
||||
return None;
|
||||
}
|
||||
let rel = relative(root, entry.path())?;
|
||||
filter.file(&rel).then_some(rel)
|
||||
}
|
||||
|
||||
/// `path` relative to `root` with `/` separators; `None` outside the root or
|
||||
/// for a name that is not valid Unicode.
|
||||
fn relative(root: &Path, path: &Path) -> Option<String> {
|
||||
let rel: PathBuf = path.strip_prefix(root).ok()?.to_path_buf();
|
||||
let parts: Option<Vec<&str>> = rel.components().map(|c| c.as_os_str().to_str()).collect();
|
||||
Some(parts?.join("/"))
|
||||
}
|
||||
|
||||
/// Searches one file, or the buffer text that replaces it, and returns its
|
||||
/// matches; `None` without matches, for a binary, oversized or unreadable
|
||||
/// file, or when cancelled.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `room`: most matches to collect; one more is collected when possible,
|
||||
/// so the caller can tell that the cap cut the file short (with no room
|
||||
/// left, one match is enough to know that).
|
||||
fn search_file(
|
||||
searcher: &mut Searcher,
|
||||
matcher: &RegexMatcher,
|
||||
path: &Path,
|
||||
buffer: Option<&String>,
|
||||
room: usize,
|
||||
cancel: &CancelToken,
|
||||
) -> Option<Vec<Match>> {
|
||||
let mut sink = Collect {
|
||||
matcher,
|
||||
matches: Vec::new(),
|
||||
limit: room.saturating_add(1),
|
||||
binary: false,
|
||||
cancel,
|
||||
};
|
||||
let result = match buffer {
|
||||
Some(text) => {
|
||||
if text.len() as u64 > MAX_FILE_SIZE {
|
||||
return None;
|
||||
}
|
||||
searcher.search_slice(matcher, text.as_bytes(), &mut sink)
|
||||
}
|
||||
None => {
|
||||
let size = std::fs::metadata(path).ok()?.len();
|
||||
if size > MAX_FILE_SIZE {
|
||||
return None;
|
||||
}
|
||||
searcher.search_path(matcher, path, &mut sink)
|
||||
}
|
||||
};
|
||||
if result.is_err() || sink.binary || cancel.is_cancelled() || sink.matches.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(sink.matches)
|
||||
}
|
||||
|
||||
/// Collects the matches of one file.
|
||||
struct Collect<'a> {
|
||||
/// The pattern, run again on each matching line to find every match.
|
||||
matcher: &'a RegexMatcher,
|
||||
/// Matches so far.
|
||||
matches: Vec<Match>,
|
||||
/// Stop at this many matches.
|
||||
limit: usize,
|
||||
/// The file turned out to be binary.
|
||||
binary: bool,
|
||||
/// Stops the file early.
|
||||
cancel: &'a CancelToken,
|
||||
}
|
||||
|
||||
impl Sink for Collect<'_> {
|
||||
type Error = std::io::Error;
|
||||
|
||||
fn matched(&mut self, _searcher: &Searcher, mat: &SinkMatch<'_>) -> Result<bool, Self::Error> {
|
||||
if self.cancel.is_cancelled() {
|
||||
return Ok(false);
|
||||
}
|
||||
let line = mat
|
||||
.line_number()
|
||||
.and_then(|n| usize::try_from(n.saturating_sub(1)).ok())
|
||||
.unwrap_or(0);
|
||||
lines::collect(
|
||||
self.matcher,
|
||||
line,
|
||||
mat.bytes(),
|
||||
&mut self.matches,
|
||||
self.limit,
|
||||
);
|
||||
Ok(self.matches.len() < self.limit)
|
||||
}
|
||||
|
||||
fn binary_data(&mut self, _searcher: &Searcher, _offset: u64) -> Result<bool, Self::Error> {
|
||||
self.binary = true;
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
345
crates/search/tests/search.rs
Normal file
345
crates/search/tests/search.rs
Normal file
|
|
@ -0,0 +1,345 @@
|
|||
//! Find in files over real directories: matching, rules, caps, cancel.
|
||||
|
||||
#![allow(
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::indexing_slicing,
|
||||
clippy::panic
|
||||
)]
|
||||
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_search::{
|
||||
CancelToken, FileMatches, MAX_FILE_SIZE, Overlay, Query, Search, SearchError, SearchEvent,
|
||||
Summary, run, spawn,
|
||||
};
|
||||
use proptest::prelude::*;
|
||||
|
||||
/// Runs `query` over `root` and returns the files sorted by path.
|
||||
fn find(root: &Path, query: &Query, overlay: &Overlay) -> (Vec<FileMatches>, Summary) {
|
||||
let search = Search::new(query).unwrap();
|
||||
let found = Mutex::new(Vec::new());
|
||||
let summary = run(root, &search, overlay, &CancelToken::new(), &|f| {
|
||||
found.lock().unwrap().push(f)
|
||||
});
|
||||
let mut files = found.into_inner().unwrap();
|
||||
files.sort_by(|a, b| a.path.cmp(&b.path));
|
||||
(files, summary)
|
||||
}
|
||||
|
||||
fn paths(files: &[FileMatches]) -> Vec<&str> {
|
||||
files.iter().map(|f| f.path.as_str()).collect()
|
||||
}
|
||||
|
||||
fn put(root: &Path, rel: &str, text: &[u8]) {
|
||||
let path = root.join(rel);
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_case_and_whole_word() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(
|
||||
dir.path(),
|
||||
"a.php",
|
||||
b"<?php\n$user = new User();\n$users = [];\nUSER\n",
|
||||
);
|
||||
let mut q = Query::literal("user");
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 4);
|
||||
let m = &files[0].matches;
|
||||
assert_eq!((m[0].line, m[0].column, m[0].length), (1, 1, 4));
|
||||
assert_eq!(m[0].preview, "$user = new User();");
|
||||
assert_eq!((m[1].line, m[1].column), (1, 12));
|
||||
|
||||
q.case_sensitive = true;
|
||||
assert_eq!(find(dir.path(), &q, &Overlay::new()).1.matches, 2);
|
||||
|
||||
q.whole_word = true;
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(files[0].matches.len(), 1);
|
||||
assert_eq!(files[0].matches[0].line, 1);
|
||||
|
||||
let q = Query::literal("$users = [");
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(files[0].matches[0].line, 2, "regex characters are literal");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regex_and_invalid_input() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(dir.path(), "a.php", b"foo1 foo22 bar\r\nfoo333\r\n");
|
||||
let mut q = Query::literal(r"foo\d+");
|
||||
q.regex = true;
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 3);
|
||||
assert_eq!(files[0].matches[2].preview, "foo333", "no \\r in previews");
|
||||
assert_eq!(files[0].matches[1].length, 5);
|
||||
|
||||
q.pattern = "(".into();
|
||||
assert!(matches!(
|
||||
Search::new(&q),
|
||||
Err(SearchError::InvalidPattern(_))
|
||||
));
|
||||
q.pattern = String::new();
|
||||
assert_eq!(Search::new(&q).unwrap_err(), SearchError::EmptyPattern);
|
||||
let mut q = Query::literal("x");
|
||||
q.include = vec!["[".into()];
|
||||
assert!(matches!(Search::new(&q), Err(SearchError::InvalidGlob(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn include_and_exclude_globs() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for rel in ["src/a.php", "src/b.js", "tests/c.php", "src/deep/d.php"] {
|
||||
put(dir.path(), rel, b"needle\n");
|
||||
}
|
||||
let mut q = Query::literal("needle");
|
||||
q.include = vec!["*.php".into()];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(
|
||||
paths(&files),
|
||||
["src/a.php", "src/deep/d.php", "tests/c.php"]
|
||||
);
|
||||
|
||||
q.exclude = vec!["tests/".into()];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(paths(&files), ["src/a.php", "src/deep/d.php"]);
|
||||
|
||||
q.include = vec!["src/*.php".into()];
|
||||
q.exclude = vec![];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(
|
||||
paths(&files),
|
||||
["src/a.php"],
|
||||
"a path glob keeps to its level"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn honours_the_project_rules() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 400);
|
||||
put(dir.path(), "sub/.gitignore", b"skip.php\n");
|
||||
put(dir.path(), "sub/skip.php", b"lutraNeedle\n");
|
||||
put(dir.path(), "sub/.hidden.php", b"lutraNeedle\n");
|
||||
put(dir.path(), ".git/HEAD", b"lutraNeedle\n");
|
||||
let (files, summary) = find(
|
||||
dir.path(),
|
||||
&Query::literal(lutra_testkit::NEEDLE),
|
||||
&Overlay::new(),
|
||||
);
|
||||
let found = paths(&files);
|
||||
assert!(found.iter().all(|p| !p.starts_with("var/")), "{found:?}");
|
||||
assert!(found.iter().all(|p| !p.starts_with("node_modules/")));
|
||||
assert!(found.iter().all(|p| !p.starts_with(".git/")));
|
||||
assert!(!found.contains(&"bin/data.bin"), "binary skipped");
|
||||
assert!(!found.contains(&"debug.log"), "ignored file skipped");
|
||||
assert!(!found.contains(&"sub/skip.php"), "nested .gitignore");
|
||||
assert!(found.contains(&"sub/.hidden.php"), "dot files are searched");
|
||||
let vendor = found.iter().filter(|p| p.starts_with("vendor/")).count();
|
||||
assert_eq!(vendor, tree.needle_vendor, "vendor is searched");
|
||||
assert!(vendor > 0);
|
||||
assert_eq!(summary.files, tree.needle_files + 1);
|
||||
assert!(!summary.truncated && !summary.cancelled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_large_and_binary_files() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut big = b"needle\n".to_vec();
|
||||
big.resize(usize::try_from(MAX_FILE_SIZE).unwrap() + 1, b'a');
|
||||
put(dir.path(), "big.txt", &big);
|
||||
let mut late_nul = b"needle\n".to_vec();
|
||||
late_nul.extend(b"filler\n".repeat(50_000));
|
||||
late_nul.push(0);
|
||||
put(dir.path(), "late.bin", &late_nul);
|
||||
put(dir.path(), "ok.txt", b"needle\n");
|
||||
let (files, _) = find(dir.path(), &Query::literal("needle"), &Overlay::new());
|
||||
assert_eq!(paths(&files), ["ok.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_replaces_the_disk_text() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(dir.path(), "a.php", b"old text\n");
|
||||
put(dir.path(), "b.php", b"needle on disk\n");
|
||||
let mut overlay = Overlay::new();
|
||||
overlay.insert("a.php".into(), "line\n needle in buffer\n".into());
|
||||
overlay.insert("b.php".into(), "gone\n".into());
|
||||
overlay.insert("missing.php".into(), "needle\n".into());
|
||||
let (files, _) = find(dir.path(), &Query::literal("needle"), &overlay);
|
||||
assert_eq!(paths(&files), ["a.php"]);
|
||||
assert_eq!(files[0].matches[0].line, 1);
|
||||
assert_eq!(files[0].matches[0].column, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_the_number_of_matches() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for i in 0..20 {
|
||||
put(dir.path(), &format!("f{i}.txt"), b"x x x x x\n");
|
||||
}
|
||||
let mut q = Query::literal("x");
|
||||
q.max_results = 7;
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 7);
|
||||
assert!(summary.truncated);
|
||||
assert_eq!(files.iter().map(|f| f.matches.len()).sum::<usize>(), 7);
|
||||
assert_eq!(summary.files, files.len());
|
||||
|
||||
q.max_results = 100;
|
||||
let (_, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 100);
|
||||
assert!(
|
||||
!summary.truncated,
|
||||
"exactly at the cap with nothing left out"
|
||||
);
|
||||
q.max_results = 99;
|
||||
assert!(find(dir.path(), &q, &Overlay::new()).1.truncated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_streams_and_cancels() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 300);
|
||||
let search = Search::new(&Query::literal(lutra_testkit::NEEDLE)).unwrap();
|
||||
let rx = spawn(
|
||||
dir.path().into(),
|
||||
search.clone(),
|
||||
Overlay::new(),
|
||||
CancelToken::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let events: Vec<SearchEvent> = rx.iter().collect();
|
||||
let files = events
|
||||
.iter()
|
||||
.filter(|e| matches!(e, SearchEvent::File(_)))
|
||||
.count();
|
||||
assert_eq!(files, tree.needle_files);
|
||||
let Some(SearchEvent::Done(summary)) = events.last() else {
|
||||
panic!("no summary last: {events:?}");
|
||||
};
|
||||
assert_eq!(summary.files, files);
|
||||
|
||||
let cancel = CancelToken::new();
|
||||
cancel.cancel();
|
||||
let rx = spawn(dir.path().into(), search, Overlay::new(), cancel).unwrap();
|
||||
let events: Vec<SearchEvent> = rx.iter().collect();
|
||||
assert_eq!(events.len(), 1);
|
||||
let SearchEvent::Done(summary) = &events[0] else {
|
||||
panic!("{events:?}");
|
||||
};
|
||||
assert!(summary.cancelled);
|
||||
assert_eq!(summary.matches, 0);
|
||||
}
|
||||
|
||||
/// Measures the first result and the total on a generated tree of 20,000
|
||||
/// files. Run in release: `cargo test -p lutra-search --release --test
|
||||
/// search -- --ignored --nocapture measure`.
|
||||
#[test]
|
||||
#[ignore = "measurement; run explicitly in release"]
|
||||
fn measure_20k_tree() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let made = Instant::now();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 20_000);
|
||||
println!("generated {} files in {:?}", tree.files, made.elapsed());
|
||||
for (label, pattern) in [
|
||||
("common literal", "function"),
|
||||
("rare literal", lutra_testkit::NEEDLE),
|
||||
] {
|
||||
for round in 0..3 {
|
||||
let search = Search::new(&Query::literal(pattern)).unwrap();
|
||||
let started = Instant::now();
|
||||
let rx = spawn(
|
||||
dir.path().into(),
|
||||
search,
|
||||
Overlay::new(),
|
||||
CancelToken::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut first: Option<Duration> = None;
|
||||
let mut summary = None;
|
||||
for event in rx {
|
||||
match event {
|
||||
SearchEvent::File(_) => {
|
||||
first.get_or_insert_with(|| started.elapsed());
|
||||
}
|
||||
SearchEvent::Done(s) => summary = Some(s),
|
||||
}
|
||||
}
|
||||
let s = summary.unwrap();
|
||||
println!(
|
||||
"{label} round {round}: first {:?}, total {:?}, files {}, matches {}, truncated {}",
|
||||
first.unwrap(),
|
||||
s.elapsed,
|
||||
s.files,
|
||||
s.matches,
|
||||
s.truncated
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut q = Query::literal("function");
|
||||
q.max_results = lutra_search::MAX_RESULTS_LIMIT;
|
||||
let search = Search::new(&q).unwrap();
|
||||
let s = run(
|
||||
dir.path(),
|
||||
&search,
|
||||
&Overlay::new(),
|
||||
&CancelToken::new(),
|
||||
&|_| {},
|
||||
);
|
||||
println!(
|
||||
"common literal, cap 100k: total {:?}, files {}, matches {}, truncated {}",
|
||||
s.elapsed, s.files, s.matches, s.truncated
|
||||
);
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(64))]
|
||||
|
||||
/// Random patterns, flags and file contents never panic, and every
|
||||
/// reported match lies inside its preview.
|
||||
#[test]
|
||||
fn random_queries_never_panic(
|
||||
pattern in "\\PC{0,12}",
|
||||
regex in any::<bool>(),
|
||||
case_sensitive in any::<bool>(),
|
||||
whole_word in any::<bool>(),
|
||||
content in proptest::collection::vec(any::<u8>(), 0..400),
|
||||
text in "\\PC{0,200}",
|
||||
) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("bytes.txt"), &content).unwrap();
|
||||
fs::write(dir.path().join("text.php"), &text).unwrap();
|
||||
let query = Query {
|
||||
pattern,
|
||||
regex,
|
||||
case_sensitive,
|
||||
whole_word,
|
||||
include: vec![],
|
||||
exclude: vec![],
|
||||
max_results: 50,
|
||||
};
|
||||
let Ok(search) = Search::new(&query) else {
|
||||
return Ok(());
|
||||
};
|
||||
let found = Mutex::new(Vec::new());
|
||||
let summary = run(dir.path(), &search, &Overlay::new(), &CancelToken::new(), &|f| {
|
||||
found.lock().unwrap().push(f)
|
||||
});
|
||||
let found = found.into_inner().unwrap();
|
||||
prop_assert!(summary.matches <= 50);
|
||||
for file in found {
|
||||
for m in file.matches {
|
||||
prop_assert!(m.length > 0);
|
||||
let units = m.preview.encode_utf16().count();
|
||||
prop_assert!(m.preview_column + m.length <= units || m.preview_column <= units);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue