use std::{sync::Mutex, thread};
use crate::{
Cache, FileKey, FileKind, MarkerCheck, Model, RepoPath, RepoRoot, Stat,
graph::{EdgeKind, NodeKey, RawEdge},
markers::{self, MarkerRow, RowKind},
model::KeyIndex,
source,
};
type Read = (FileKey, Vec<MarkerRow>);
pub(super) fn scan_outside(
model: &mut Model,
cache: &Cache,
root: &RepoRoot,
full: bool,
) -> Vec<RepoPath> {
if model.marker_check == MarkerCheck::Off {
return Vec::new();
}
let paths: Vec<RepoPath> = if full {
outside_files(model)
} else {
cache.outside_to_read()
};
let fresh = cache.is_fresh();
let mut added = Vec::new();
for (path, read) in read_all(root, paths) {
let Some((key, rows)) = read else {
cache.unread(&path);
continue;
};
let state = cache.observe(&path, &key);
if !full && state != crate::cache::FileState::Changed {
continue;
}
if rows.is_empty() && (fresh || !cache.has_markers(&path)) {
continue;
}
cache.record_markers(&path, rows.clone());
model.markers.insert(path.clone(), rows);
added.push(path);
}
added
}
fn outside_files(model: &Model) -> Vec<RepoPath> {
model
.tree
.files
.iter()
.filter(|f| f.root.is_none() && f.kind == FileKind::Candidate)
.map(|f| f.path.clone())
.collect()
}
pub(super) fn marker_edges(model: &Model, keys: &KeyIndex, root: &RepoRoot) -> Vec<RawEdge> {
if model.marker_check == MarkerCheck::Off {
return Vec::new();
}
let mut out = Vec::new();
for (path, read) in read_all(root, outside_files(model)) {
let Some((_, rows)) = read else {
continue;
};
for row in rows {
let RowKind::Marker { rel, target } = row.kind else {
continue;
};
if let Ok(r) = markers::resolve(model, keys, &target) {
out.push(RawEdge {
from: path.clone(),
kind: EdgeKind::Marker,
written: r.path.as_str().to_string(),
to: NodeKey::Path(r.path),
rel,
region: r.slug,
line: Some(row.line),
note: None,
});
}
}
}
out
}
pub(super) fn is_checked_unit(model: &Model, cache: &Cache, path: &RepoPath) -> bool {
model.is_unit(path)
|| model.markers.contains_key(path)
|| (model.is_marker_file(path) && cache.has_markers(path))
}
fn read_all(root: &RepoRoot, paths: Vec<RepoPath>) -> Vec<(RepoPath, Option<Read>)> {
let threads = thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
.min(paths.len().div_ceil(64).max(1));
let chunk = paths.len().div_ceil(threads).max(1);
let results: Mutex<Vec<(RepoPath, Option<Read>)>> = Mutex::new(Vec::with_capacity(paths.len()));
thread::scope(|scope| {
for part in paths.chunks(chunk) {
let results = &results;
scope.spawn(move || {
let found: Vec<(RepoPath, Option<Read>)> = part
.iter()
.map(|p| (p.clone(), read_one(root, p)))
.collect();
results
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.extend(found);
});
}
});
let mut results = results
.into_inner()
.unwrap_or_else(std::sync::PoisonError::into_inner);
results.sort_by(|a, b| a.0.cmp(&b.0));
results
}
fn read_one(root: &RepoRoot, path: &RepoPath) -> Option<Read> {
let fs_path = path.to_fs(root);
let bytes = std::fs::read(&fs_path).ok()?;
let mtime = std::fs::metadata(&fs_path)
.ok()
.and_then(|m| Stat::of(&m).mtime);
let key = FileKey {
len: bytes.len() as u64,
mtime,
hash: source::fnv1a64(&bytes),
};
let rows = markers::scan(&bytes).unwrap_or_default();
Some((key, rows))
}