use crate::constants::capacity;
use crate::models::{
CodeAnalysis, CodeAnalysisApplyDiffDetail, CodeAnalysisReadDetail, CodeAnalysisRecord,
CodeAnalysisRunCommandDetail, CodeAnalysisWriteDetail,
};
use anyhow::Result;
use lru::LruCache;
use std::fs;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::SystemTime;
#[derive(Debug, Clone)]
struct CachedFile {
modified: SystemTime,
analysis: Arc<CodeAnalysis>,
size_bytes: usize,
}
pub struct FileParseCache {
cache: RwLock<LruCache<PathBuf, CachedFile>>,
}
impl FileParseCache {
pub fn new() -> Self {
let cache_size = NonZeroUsize::new(capacity::FILE_CACHE_SIZE).unwrap();
Self {
cache: RwLock::new(LruCache::new(cache_size)),
}
}
pub fn get_or_parse<P: AsRef<Path>>(&self, path: P) -> Result<Arc<CodeAnalysis>> {
let path = path.as_ref();
let path_buf = path.to_path_buf();
let metadata = fs::metadata(path)?;
let modified = metadata.modified()?;
{
if let Ok(cache_read) = self.cache.read() {
if let Some(cached) = cache_read.peek(&path_buf) {
if cached.modified >= modified {
log::trace!("LRU cache hit for {}", path.display());
let result = Arc::clone(&cached.analysis);
drop(cache_read);
if let Ok(mut cache_write) = self.cache.write() {
cache_write.get(&path_buf); }
return Ok(result);
}
}
}
}
log::debug!("LRU cache miss for {}, parsing...", path.display());
let analysis = crate::analysis::analyze_jsonl_file_typed(path)?;
let arc_analysis = Arc::new(analysis);
let size_bytes = estimate_analysis_bytes(arc_analysis.as_ref());
if let Ok(mut cache_write) = self.cache.write() {
cache_write.put(
path_buf,
CachedFile {
modified,
analysis: Arc::clone(&arc_analysis),
size_bytes,
},
);
}
Ok(arc_analysis)
}
pub fn clear(&self) {
if let Ok(mut cache) = self.cache.write() {
cache.clear();
}
}
pub fn cleanup_stale(&self) {
if let Ok(mut cache) = self.cache.write() {
let stale_keys: Vec<PathBuf> = cache
.iter()
.filter(|(path, _)| !path.exists())
.map(|(path, _)| path.clone())
.collect();
for key in stale_keys {
cache.pop(&key);
}
}
}
pub fn stats(&self) -> CacheStats {
if let Ok(cache) = self.cache.write() {
let total_bytes: usize = cache.iter().map(|(_, c)| c.size_bytes).sum();
CacheStats {
entry_count: cache.len(),
estimated_memory_kb: total_bytes / 1024,
}
} else {
CacheStats::default()
}
}
pub fn invalidate<P: AsRef<Path>>(&self, path: P) {
if let Ok(mut cache) = self.cache.write() {
cache.pop(&path.as_ref().to_path_buf());
}
}
pub fn get_cached_paths(&self) -> Vec<PathBuf> {
if let Ok(cache) = self.cache.write() {
cache.iter().map(|(path, _)| path.clone()).collect()
} else {
Vec::new()
}
}
}
impl Default for FileParseCache {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Default, Clone)]
pub struct CacheStats {
pub entry_count: usize,
pub estimated_memory_kb: usize,
}
fn estimate_analysis_bytes(analysis: &CodeAnalysis) -> usize {
use std::mem::size_of;
let mut bytes = size_of::<CodeAnalysis>();
bytes += analysis.user.capacity();
bytes += analysis.extension_name.capacity();
bytes += analysis.insights_version.capacity();
bytes += analysis.machine_id.capacity();
bytes += analysis.records.capacity() * size_of::<CodeAnalysisRecord>();
for record in &analysis.records {
bytes += record.task_id.capacity();
bytes += record.folder_path.capacity();
bytes += record.git_remote_url.capacity();
bytes += record.write_file_details.capacity() * size_of::<CodeAnalysisWriteDetail>();
for detail in &record.write_file_details {
bytes += detail.base.file_path.capacity();
bytes += detail.content.capacity();
}
bytes += record.read_file_details.capacity() * size_of::<CodeAnalysisReadDetail>();
for detail in &record.read_file_details {
bytes += detail.base.file_path.capacity();
}
bytes += record.edit_file_details.capacity() * size_of::<CodeAnalysisApplyDiffDetail>();
for detail in &record.edit_file_details {
bytes += detail.base.file_path.capacity();
bytes += detail.old_string.capacity();
bytes += detail.new_string.capacity();
}
bytes += record.run_command_details.capacity() * size_of::<CodeAnalysisRunCommandDetail>();
for detail in &record.run_command_details {
bytes += detail.base.file_path.capacity();
bytes += detail.command.capacity();
bytes += detail.description.capacity();
}
for (k, _) in &record.conversation_usage {
bytes += k.capacity();
bytes += 256;
}
}
bytes
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_basic() {
let cache = FileParseCache::new();
let stats = cache.stats();
assert_eq!(stats.entry_count, 0);
}
#[test]
fn test_cache_clear() {
let cache = FileParseCache::new();
cache.clear();
let stats = cache.stats();
assert_eq!(stats.entry_count, 0);
}
}