use fallow_types::discover::FileId;
use fallow_types::source_fingerprint::SourceFingerprint;
use crate::{cache, discover, extract};
pub struct CacheUpdate<'a> {
pub modules: &'a [extract::ModuleInfo],
pub files: &'a [discover::DiscoveredFile],
pub metadata_hits: &'a [FileId],
pub needs: extract::ParseNeeds,
pub read_started_ns: u64,
}
pub fn parse_needs(
config: &fallow_config::ResolvedConfig,
need_complexity: bool,
) -> extract::ParseNeeds {
extract::ParseNeeds {
complexity: need_complexity,
call_facts: config.reads_call_facts(),
flags: config.reads_feature_flags(),
component_contracts: config.reads_component_contracts(),
}
}
pub fn update_cache(store: &mut cache::CacheStore, update: &CacheUpdate<'_>) -> bool {
let mut dirty = false;
for module in update.modules {
if fallow_extract::is_metadata_hit(update.metadata_hits, module.file_id) {
continue;
}
if let Some(file) = update.files.get(module.file_id.0 as usize) {
let fingerprint =
file_fingerprint(&file.path).for_content_read_at(update.read_started_ns);
dirty |= store.record_module(&file.path, module, fingerprint, update.needs);
}
}
let removed_stale_paths = store.retain_paths(update.files);
dirty || removed_stale_paths
}
fn file_fingerprint(path: &std::path::Path) -> SourceFingerprint {
std::fs::metadata(path).map_or(SourceFingerprint::new(0, 0), |metadata| {
SourceFingerprint::from_metadata(&metadata)
})
}
pub fn collect_file_hashes(
modules: &[extract::ModuleInfo],
files: &[discover::DiscoveredFile],
) -> rustc_hash::FxHashMap<std::path::PathBuf, u64> {
modules
.iter()
.filter_map(|module| {
files
.get(module.file_id.0 as usize)
.map(|file| (file.path.clone(), module.content_hash))
})
.collect()
}
#[cfg(test)]
mod tests {
use fallow_types::discover::{DiscoveredFile, FileId};
use fallow_types::source_fingerprint::SourceFingerprint;
use super::{CacheUpdate, update_cache};
use crate::cache::CacheStore;
const SOURCE: &str = "export const alpha = 1;\n";
const STORED: SourceFingerprint = SourceFingerprint::with_ctime(10, 20, 24);
fn rewrite_missing_file(metadata_hits: &[FileId]) -> (bool, SourceFingerprint) {
let root = std::env::temp_dir().join("fallow-parse-cache-no-such-project");
let path = root.join("api.ts");
let module = fallow_extract::parse_source_to_module(FileId(0), &path, SOURCE, 0, false);
let mut store = CacheStore::new(&root);
store.record_module(
&path,
&module,
STORED,
fallow_extract::ParseNeeds::default(),
);
let files = [DiscoveredFile {
id: FileId(0),
path: path.clone(),
size_bytes: STORED.file_size,
}];
let changed = update_cache(
&mut store,
&CacheUpdate {
modules: std::slice::from_ref(&module),
files: &files,
metadata_hits,
needs: fallow_extract::ParseNeeds::default(),
read_started_ns: fallow_types::source_fingerprint::now_ns(),
},
);
let stored = store
.get_by_path_only(&path)
.expect("entry stays in the store")
.source_fingerprint();
(changed, stored)
}
#[test]
#[cfg_attr(miri, ignore = "the metadata read is blocked by Miri isolation")]
fn update_cache_skips_the_stat_of_a_metadata_hit() {
assert_eq!(rewrite_missing_file(&[FileId(0)]), (false, STORED));
}
#[test]
#[cfg_attr(miri, ignore = "the metadata read is blocked by Miri isolation")]
fn update_cache_reads_the_metadata_of_any_other_module() {
let (changed, stored) = rewrite_missing_file(&[]);
assert!(changed);
assert_eq!(stored, SourceFingerprint::new(0, 0));
}
}