use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use std::sync::{Arc, RwLock};
use syn::File;
use anyhow::{Result, Context};
use serde::Serialize;
use std::fs;
#[derive(Debug, Clone)]
pub struct CachedAst {
pub ast: File,
pub file_size: u64,
pub modified_time: u64,
pub created_at: u64,
}
unsafe impl Send for CachedAst {}
unsafe impl Sync for CachedAst {}
#[derive(Debug, Clone, Serialize)]
pub struct CacheStats {
pub total_requests: u64,
pub cache_hits: u64,
pub cache_misses: u64,
pub invalidations: u64,
pub entries_count: usize,
pub hit_rate: f64,
}
impl CacheStats {
pub fn new() -> Self {
Self {
total_requests: 0,
cache_hits: 0,
cache_misses: 0,
invalidations: 0,
entries_count: 0,
hit_rate: 0.0,
}
}
pub fn update_hit_rate(&mut self) {
if self.total_requests > 0 {
self.hit_rate = (self.cache_hits as f64 / self.total_requests as f64) * 100.0;
}
}
}
#[derive(Debug)]
pub struct AstCache {
entries: Arc<RwLock<HashMap<PathBuf, CachedAst>>>,
access_order: Arc<RwLock<Vec<PathBuf>>>,
stats: Arc<RwLock<CacheStats>>,
max_entries: usize,
max_age_seconds: u64,
}
impl AstCache {
pub fn new(max_entries: usize, max_age_seconds: u64) -> Self {
Self {
entries: Arc::new(RwLock::new(HashMap::new())),
access_order: Arc::new(RwLock::new(Vec::new())),
stats: Arc::new(RwLock::new(CacheStats::new())),
max_entries,
max_age_seconds,
}
}
pub fn get_or_parse(&self, file_path: &Path) -> Result<File> {
let file_path_buf = file_path.to_path_buf();
{
let mut stats = self.stats.write().unwrap();
stats.total_requests += 1;
}
if let Some(cached_ast) = self.get_valid_cached_ast(&file_path_buf)? {
self.update_access_order(&file_path_buf);
let mut stats = self.stats.write().unwrap();
stats.cache_hits += 1;
stats.update_hit_rate();
return Ok(cached_ast.ast);
}
let ast = self.parse_and_cache(&file_path_buf)?;
let mut stats = self.stats.write().unwrap();
stats.cache_misses += 1;
stats.update_hit_rate();
Ok(ast)
}
fn get_valid_cached_ast(&self, file_path: &PathBuf) -> Result<Option<CachedAst>> {
let entries = self.entries.read().unwrap();
if let Some(cached) = entries.get(file_path) {
let metadata = match fs::metadata(file_path) {
Ok(meta) => meta,
Err(_) => {
drop(entries);
self.invalidate_entry(file_path);
return Ok(None);
}
};
let current_modified = metadata
.modified()
.unwrap_or(SystemTime::UNIX_EPOCH)
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let current_size = metadata.len();
if cached.modified_time == current_modified &&
cached.file_size == current_size &&
!self.is_entry_expired(cached) {
return Ok(Some(cached.clone()));
} else {
drop(entries);
self.invalidate_entry(file_path);
}
}
Ok(None)
}
fn is_entry_expired(&self, cached: &CachedAst) -> bool {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
(now - cached.created_at) > self.max_age_seconds
}
fn parse_and_cache(&self, file_path: &PathBuf) -> Result<File> {
let content = fs::read_to_string(file_path)
.with_context(|| format!("Failed to read file: {}", file_path.display()))?;
let ast: File = syn::parse_file(&content)
.with_context(|| format!("Failed to parse Rust file: {}", file_path.display()))?;
let metadata = fs::metadata(file_path)
.with_context(|| format!("Failed to get file metadata: {}", file_path.display()))?;
let modified_time = metadata
.modified()
.unwrap_or(SystemTime::UNIX_EPOCH)
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let file_size = metadata.len();
let created_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let cached_ast = CachedAst {
ast: ast.clone(),
file_size,
modified_time,
created_at,
};
self.insert_entry(file_path.clone(), cached_ast);
Ok(ast)
}
fn insert_entry(&self, file_path: PathBuf, cached_ast: CachedAst) {
let mut entries = self.entries.write().unwrap();
let mut access_order = self.access_order.write().unwrap();
if entries.len() >= self.max_entries && !entries.contains_key(&file_path) {
if let Some(lru_path) = access_order.first().cloned() {
entries.remove(&lru_path);
access_order.retain(|p| p != &lru_path);
let mut stats = self.stats.write().unwrap();
stats.invalidations += 1;
}
}
entries.insert(file_path.clone(), cached_ast);
access_order.retain(|p| p != &file_path);
access_order.push(file_path);
let mut stats = self.stats.write().unwrap();
stats.entries_count = entries.len();
}
fn update_access_order(&self, file_path: &PathBuf) {
let mut access_order = self.access_order.write().unwrap();
access_order.retain(|p| p != file_path);
access_order.push(file_path.clone());
}
fn invalidate_entry(&self, file_path: &PathBuf) {
let mut entries = self.entries.write().unwrap();
let mut access_order = self.access_order.write().unwrap();
if entries.remove(file_path).is_some() {
access_order.retain(|p| p != file_path);
let mut stats = self.stats.write().unwrap();
stats.invalidations += 1;
stats.entries_count = entries.len();
}
}
pub fn get_stats(&self) -> CacheStats {
let stats = self.stats.read().unwrap();
let entries = self.entries.read().unwrap();
let mut current_stats = stats.clone();
current_stats.entries_count = entries.len();
current_stats
}
}
impl Default for AstCache {
fn default() -> Self {
Self::new(1000, 3600)
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
use std::io::Write;
#[test]
fn test_ast_cache_basic_functionality() {
let cache = AstCache::new(10, 3600);
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.rs");
let mut file = fs::File::create(&file_path).unwrap();
writeln!(file, "fn main() {{ println!(\"Hello, world!\"); }}").unwrap();
let ast1 = cache.get_or_parse(&file_path).unwrap();
let stats1 = cache.get_stats();
assert_eq!(stats1.cache_misses, 1);
assert_eq!(stats1.cache_hits, 0);
let ast2 = cache.get_or_parse(&file_path).unwrap();
let stats2 = cache.get_stats();
assert_eq!(stats2.cache_misses, 1);
assert_eq!(stats2.cache_hits, 1);
assert_eq!(format!("{:?}", ast1), format!("{:?}", ast2));
}
#[test]
fn test_cache_invalidation_on_file_change() {
let cache = AstCache::new(10, 3600);
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.rs");
let mut file = fs::File::create(&file_path).unwrap();
writeln!(file, "fn main() {{ println!(\"Hello, world!\"); }}").unwrap();
let _ast1 = cache.get_or_parse(&file_path).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10)); let mut file = fs::File::create(&file_path).unwrap();
writeln!(file, "fn main() {{ println!(\"Hello, modified world!\"); }}").unwrap();
let _ast2 = cache.get_or_parse(&file_path).unwrap();
let stats = cache.get_stats();
assert_eq!(stats.cache_misses, 2); }
#[test]
fn test_lru_eviction() {
let cache = AstCache::new(2, 3600); let temp_dir = TempDir::new().unwrap();
let files: Vec<PathBuf> = (0..3).map(|i| {
let file_path = temp_dir.path().join(format!("test{}.rs", i));
let mut file = fs::File::create(&file_path).unwrap();
writeln!(file, "fn main{} () {{ println!(\"Hello {}!\"); }}", i, i).unwrap();
file_path
}).collect();
let _ast0 = cache.get_or_parse(&files[0]).unwrap();
let _ast1 = cache.get_or_parse(&files[1]).unwrap();
let _ast2 = cache.get_or_parse(&files[2]).unwrap();
let _ast0_again = cache.get_or_parse(&files[0]).unwrap();
let stats = cache.get_stats();
assert_eq!(stats.cache_misses, 4);
assert_eq!(stats.entries_count, 2); }
}