|
1 | 1 | use crate::actions::Action; |
2 | | -use walkdir::WalkDir; |
| 2 | +use std::collections::HashSet; |
| 3 | +use std::fs; |
| 4 | +use std::path::PathBuf; |
| 5 | +use walkdir::{IntoIter as WalkDirIter, WalkDir}; |
3 | 6 |
|
4 | | -/// Index the provided filesystem paths and return a list of [`Action`]s. |
| 7 | +const DEFAULT_BATCH_SIZE: usize = 512; |
| 8 | +const DEFAULT_MAX_ITEMS: usize = 100_000; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Copy)] |
| 11 | +pub struct IndexOptions { |
| 12 | + pub batch_size: usize, |
| 13 | + pub max_items: usize, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for IndexOptions { |
| 17 | + fn default() -> Self { |
| 18 | + Self { |
| 19 | + batch_size: DEFAULT_BATCH_SIZE, |
| 20 | + max_items: DEFAULT_MAX_ITEMS, |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl IndexOptions { |
| 26 | + pub fn with_max_items(max_items: Option<usize>) -> Self { |
| 27 | + Self { |
| 28 | + max_items: max_items.unwrap_or(DEFAULT_MAX_ITEMS), |
| 29 | + ..Self::default() |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Lazily indexes files from one or more roots and yields actions in batches. |
5 | 35 | /// |
6 | | -/// Any errors encountered while traversing the directory tree are logged and |
7 | | -/// returned to the caller. |
8 | | -pub fn index_paths(paths: &[String]) -> anyhow::Result<Vec<Action>> { |
9 | | - let mut results = Vec::new(); |
10 | | - for p in paths { |
11 | | - for entry in WalkDir::new(p).into_iter() { |
12 | | - let entry = match entry { |
13 | | - Ok(e) => e, |
14 | | - Err(e) => { |
15 | | - tracing::error!(path = %p, error = %e, "failed to read directory entry"); |
16 | | - return Err(e.into()); |
| 36 | +/// Duplicate files are skipped by canonical path. Traversal errors stop |
| 37 | +/// iteration and are returned to the caller. |
| 38 | +pub struct IndexBatchIter { |
| 39 | + roots: Vec<String>, |
| 40 | + root_idx: usize, |
| 41 | + current: Option<WalkDirIter>, |
| 42 | + seen: HashSet<PathBuf>, |
| 43 | + options: IndexOptions, |
| 44 | + produced: usize, |
| 45 | +} |
| 46 | + |
| 47 | +impl IndexBatchIter { |
| 48 | + fn new(paths: &[String], options: IndexOptions) -> Self { |
| 49 | + let options = IndexOptions { |
| 50 | + batch_size: options.batch_size.max(1), |
| 51 | + max_items: options.max_items.max(1), |
| 52 | + }; |
| 53 | + Self { |
| 54 | + roots: paths.to_vec(), |
| 55 | + root_idx: 0, |
| 56 | + current: None, |
| 57 | + seen: HashSet::new(), |
| 58 | + options, |
| 59 | + produced: 0, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn next_root(&mut self) -> Option<String> { |
| 64 | + let root = self.roots.get(self.root_idx).cloned(); |
| 65 | + if root.is_some() { |
| 66 | + self.root_idx += 1; |
| 67 | + } |
| 68 | + root |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl Iterator for IndexBatchIter { |
| 73 | + type Item = anyhow::Result<Vec<Action>>; |
| 74 | + |
| 75 | + fn next(&mut self) -> Option<Self::Item> { |
| 76 | + if self.produced >= self.options.max_items { |
| 77 | + return None; |
| 78 | + } |
| 79 | + |
| 80 | + let mut batch = Vec::with_capacity(self.options.batch_size); |
| 81 | + while self.produced < self.options.max_items && batch.len() < self.options.batch_size { |
| 82 | + if self.current.is_none() { |
| 83 | + if let Some(root) = self.next_root() { |
| 84 | + self.current = Some(WalkDir::new(root).into_iter()); |
| 85 | + } else { |
| 86 | + break; |
17 | 87 | } |
| 88 | + } |
| 89 | + |
| 90 | + let Some(iter) = self.current.as_mut() else { |
| 91 | + continue; |
18 | 92 | }; |
19 | | - if entry.file_type().is_file() { |
20 | | - if let Some(name) = entry.path().file_name().and_then(|n| n.to_str()) { |
21 | | - results.push(Action { |
| 93 | + |
| 94 | + match iter.next() { |
| 95 | + Some(Ok(entry)) => { |
| 96 | + if !entry.file_type().is_file() { |
| 97 | + continue; |
| 98 | + } |
| 99 | + let canonical = match fs::canonicalize(entry.path()) { |
| 100 | + Ok(path) => path, |
| 101 | + Err(err) => { |
| 102 | + tracing::error!( |
| 103 | + path = %entry.path().display(), |
| 104 | + error = %err, |
| 105 | + "failed to canonicalize indexed path" |
| 106 | + ); |
| 107 | + return Some(Err(err.into())); |
| 108 | + } |
| 109 | + }; |
| 110 | + if !self.seen.insert(canonical.clone()) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + let Some(name) = canonical.file_name().and_then(|n| n.to_str()) else { |
| 114 | + continue; |
| 115 | + }; |
| 116 | + let display = canonical.display().to_string(); |
| 117 | + batch.push(Action { |
22 | 118 | label: name.to_string(), |
23 | | - desc: entry.path().display().to_string(), |
24 | | - action: entry.path().display().to_string(), |
| 119 | + desc: display.clone(), |
| 120 | + action: display, |
25 | 121 | args: None, |
26 | 122 | }); |
| 123 | + self.produced += 1; |
| 124 | + } |
| 125 | + Some(Err(err)) => { |
| 126 | + tracing::error!(error = %err, "failed to read directory entry"); |
| 127 | + return Some(Err(err.into())); |
| 128 | + } |
| 129 | + None => { |
| 130 | + self.current = None; |
27 | 131 | } |
28 | 132 | } |
29 | 133 | } |
| 134 | + |
| 135 | + if batch.is_empty() { |
| 136 | + None |
| 137 | + } else { |
| 138 | + Some(Ok(batch)) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +pub fn index_paths_batched(paths: &[String], options: IndexOptions) -> IndexBatchIter { |
| 144 | + IndexBatchIter::new(paths, options) |
| 145 | +} |
| 146 | + |
| 147 | +/// Index the provided filesystem paths and return a list of [`Action`]s. |
| 148 | +/// |
| 149 | +/// This compatibility helper exhausts the batched iterator into a single |
| 150 | +/// vector; prefer [`index_paths_batched`] when possible. |
| 151 | +pub fn index_paths(paths: &[String]) -> anyhow::Result<Vec<Action>> { |
| 152 | + let mut results = Vec::new(); |
| 153 | + for batch in index_paths_batched(paths, IndexOptions::default()) { |
| 154 | + results.extend(batch?); |
30 | 155 | } |
31 | 156 | Ok(results) |
32 | 157 | } |
0 commit comments