use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::remote::RepositoryConfiguration;
type CacheKey = String;
fn current_timestamp() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("SystemTime::now() should always be after UNIX_EPOCH")
.as_secs()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheMetadata {
pub repo_url: String,
pub branch: String,
pub commit_hash: String,
pub created_at: u64,
pub last_accessed: u64,
pub size_bytes: u64,
pub cache_path: String,
}
impl CacheMetadata {
pub fn new(config: &RepositoryConfiguration, cache_path: &str, commit_hash: &str) -> Self {
let now = current_timestamp();
let size = get_directory_size(cache_path);
Self {
repo_url: config.url.clone(),
branch: config.branch.clone(),
commit_hash: commit_hash.to_string(),
created_at: now,
last_accessed: now,
size_bytes: size,
cache_path: cache_path.to_string(),
}
}
pub fn update_access_time(&mut self) {
self.last_accessed = current_timestamp();
}
}
pub struct CacheMetadataManager {
metadata: HashMap<CacheKey, CacheMetadata>,
metadata_file_path: String,
}
impl CacheMetadataManager {
pub fn new(metadata_file_path: String) -> Self {
let mut manager = Self {
metadata: HashMap::new(),
metadata_file_path,
};
manager.load_from_disk().ok();
manager
}
pub fn store_metadata(&mut self, key: &str, metadata: CacheMetadata) -> Result<(), String> {
self.metadata.insert(key.to_string(), metadata);
self.save_to_disk()
}
pub fn get_metadata(&self, key: &str) -> Option<&CacheMetadata> {
self.metadata.get(key)
}
pub fn update_access_time(&mut self, key: &str) -> Result<(), String> {
if let Some(metadata) = self.metadata.get_mut(key) {
metadata.update_access_time();
self.save_to_disk()
} else {
Err(format!("Metadata not found for key: {key}"))
}
}
pub fn is_cache_valid(&self, key: &str) -> bool {
self.metadata.contains_key(key)
}
pub fn remove_metadata(&mut self, key: &str) -> Result<(), String> {
self.metadata.remove(key);
self.save_to_disk()
}
pub fn get_all_keys(&self) -> Vec<String> {
self.metadata.keys().cloned().collect()
}
fn save_to_disk(&self) -> Result<(), String> {
if let Some(parent) = Path::new(&self.metadata_file_path).parent() {
fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create metadata directory: {e}"))?;
}
let json = serde_json::to_string_pretty(&self.metadata)
.map_err(|e| format!("Failed to serialize metadata: {e}"))?;
fs::write(&self.metadata_file_path, json)
.map_err(|e| format!("Failed to write metadata file: {e}"))?;
Ok(())
}
fn load_from_disk(&mut self) -> Result<(), String> {
if !Path::new(&self.metadata_file_path).exists() {
return Ok(());
}
let json = fs::read_to_string(&self.metadata_file_path)
.map_err(|e| format!("Failed to read metadata file: {e}"))?;
self.metadata = serde_json::from_str(&json)
.map_err(|e| format!("Failed to deserialize metadata: {e}"))?;
Ok(())
}
pub fn cleanup_old_entries(&mut self, max_age_seconds: u64) -> Result<Vec<String>, String> {
let now = current_timestamp();
let mut to_remove = Vec::new();
for (key, metadata) in &self.metadata {
if now - metadata.last_accessed > max_age_seconds {
to_remove.push(key.clone());
}
}
for key in &to_remove {
self.metadata.remove(key);
}
if !to_remove.is_empty() {
self.save_to_disk()?;
}
Ok(to_remove)
}
}
fn get_directory_size(path: &str) -> u64 {
let mut size = 0;
if let Ok(entries) = fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry
&& let Ok(metadata) = entry.metadata()
{
if metadata.is_file() {
size += metadata.len();
} else if metadata.is_dir() {
size += 1024; }
}
}
}
size
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_cache_metadata_creation() {
let config = RepositoryConfiguration::new(
"https://github.com/example/repo.git".to_string(),
"main".to_string(),
"./src/module1".to_string(),
vec!["src/".to_string()],
None,
None,
);
let temp_dir = TempDir::new().expect("Failed to create temporary directory for test");
let cache_path = temp_dir
.path()
.to_str()
.expect("Failed to convert temporary directory path to string");
let metadata = CacheMetadata::new(&config, cache_path, "abc123");
assert_eq!(metadata.repo_url, "https://github.com/example/repo.git");
assert_eq!(metadata.branch, "main");
assert_eq!(metadata.commit_hash, "abc123");
assert_eq!(metadata.cache_path, cache_path);
}
#[test]
fn test_cache_metadata_manager() {
let temp_dir = TempDir::new().expect("Failed to create temporary directory for test");
let metadata_file = temp_dir
.path()
.join("metadata.json")
.to_str()
.expect("Failed to convert path to string")
.to_string();
let mut manager = CacheMetadataManager::new(metadata_file);
let config = RepositoryConfiguration::new(
"https://github.com/example/repo.git".to_string(),
"main".to_string(),
"./src/module1".to_string(),
vec!["src/".to_string()],
None,
None,
);
let cache_path_binding = temp_dir.path().join("cache");
let test_cache_path = cache_path_binding
.to_str()
.expect("Failed to convert cache path to string");
let metadata = CacheMetadata::new(&config, test_cache_path, "abc123");
let key = "test-key";
manager
.store_metadata(key, metadata.clone())
.expect("Failed to store metadata");
let retrieved = manager
.get_metadata(key)
.expect("Failed to retrieve stored metadata");
assert_eq!(retrieved.repo_url, metadata.repo_url);
manager
.update_access_time(key)
.expect("Failed to update access time");
assert!(manager.is_cache_valid(key));
let keys = manager.get_all_keys();
assert_eq!(keys, vec!["test-key".to_string()]);
}
}