|
| 1 | +use crate::project::Error; |
| 2 | +use error_stack::{Result, ResultExt}; |
| 3 | +use std::{ |
| 4 | + collections::HashMap, |
| 5 | + fs::{self, File, OpenOptions}, |
| 6 | + io::{BufReader, BufWriter}, |
| 7 | + path::{Path, PathBuf}, |
| 8 | + sync::Mutex, |
| 9 | +}; |
| 10 | + |
| 11 | +pub trait Cache { |
| 12 | + fn get_file_owner(&self, path: &Path) -> Result<Option<FileOwnerCacheEntry>, Error>; |
| 13 | + fn write_file_owner(&self, path: &Path, owner: Option<String>); |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct GlobalCache<'a> { |
| 18 | + base_path: &'a PathBuf, |
| 19 | + cache_directory: &'a String, |
| 20 | + file_owner_cache: Option<Box<Mutex<HashMap<PathBuf, FileOwnerCacheEntry>>>>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] |
| 24 | +pub struct FileOwnerCacheEntry { |
| 25 | + timestamp: u64, |
| 26 | + pub owner: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +const DEFAULT_CACHE_CAPACITY: usize = 10000; |
| 30 | + |
| 31 | +impl<'a> GlobalCache<'a> { |
| 32 | + pub fn new(base_path: &'a PathBuf, cache_directory: &'a String) -> Self { |
| 33 | + Self { |
| 34 | + base_path, |
| 35 | + cache_directory, |
| 36 | + file_owner_cache: None, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + pub fn persist_cache(&self) -> Result<(), Error> { |
| 41 | + let cache_path = self.get_cache_path(); |
| 42 | + let file = OpenOptions::new() |
| 43 | + .write(true) |
| 44 | + .create(true) |
| 45 | + .truncate(true) |
| 46 | + .open(cache_path) |
| 47 | + .change_context(Error::Io)?; |
| 48 | + |
| 49 | + let writer = BufWriter::new(file); |
| 50 | + let cache = self.file_owner_cache.as_ref().unwrap().lock().map_err(|_| Error::Io)?; |
| 51 | + serde_json::to_writer(writer, &*cache).change_context(Error::SerdeJson) |
| 52 | + } |
| 53 | + |
| 54 | + pub fn load_cache(&mut self) -> Result<(), Error> { |
| 55 | + let cache_path = self.get_cache_path(); |
| 56 | + if !cache_path.exists() { |
| 57 | + self.file_owner_cache = Some(Box::new(Mutex::new(HashMap::with_capacity(DEFAULT_CACHE_CAPACITY)))); |
| 58 | + return Ok(()); |
| 59 | + } |
| 60 | + |
| 61 | + let file = File::open(cache_path).change_context(Error::Io)?; |
| 62 | + let reader = BufReader::new(file); |
| 63 | + let json = serde_json::from_reader(reader); |
| 64 | + self.file_owner_cache = match json { |
| 65 | + Ok(cache) => Some(Box::new(Mutex::new(cache))), |
| 66 | + _ => Some(Box::new(Mutex::new(HashMap::with_capacity(DEFAULT_CACHE_CAPACITY)))), |
| 67 | + }; |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn get_file_owner(&self, path: &Path) -> Result<Option<FileOwnerCacheEntry>, Error> { |
| 72 | + if let Ok(cache) = self.file_owner_cache.as_ref().unwrap().lock() { |
| 73 | + if let Some(cached_entry) = cache.get(path) { |
| 74 | + let timestamp = Self::get_file_timestamp(path)?; |
| 75 | + if cached_entry.timestamp == timestamp { |
| 76 | + return Ok(Some(cached_entry.clone())); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + Ok(None) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn write_file_owner(&self, path: &Path, owner: Option<String>) { |
| 84 | + if let Ok(mut cache) = self.file_owner_cache.as_ref().unwrap().lock() { |
| 85 | + if let Ok(timestamp) = Self::get_file_timestamp(path) { |
| 86 | + cache.insert(path.to_path_buf(), FileOwnerCacheEntry { timestamp, owner }); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn get_cache_path(&self) -> PathBuf { |
| 92 | + let cache_dir = self.base_path.join(PathBuf::from(&self.cache_directory)); |
| 93 | + fs::create_dir_all(&cache_dir).unwrap(); |
| 94 | + |
| 95 | + cache_dir.join("project-file-cache.json") |
| 96 | + } |
| 97 | + |
| 98 | + pub fn delete_cache(&self) -> Result<(), Error> { |
| 99 | + let cache_path = self.get_cache_path(); |
| 100 | + dbg!("deleting", &cache_path); |
| 101 | + fs::remove_file(cache_path).change_context(Error::Io) |
| 102 | + } |
| 103 | + |
| 104 | + fn get_file_timestamp(path: &Path) -> Result<u64, Error> { |
| 105 | + let metadata = fs::metadata(path).change_context(Error::Io)?; |
| 106 | + metadata |
| 107 | + .modified() |
| 108 | + .change_context(Error::Io)? |
| 109 | + .duration_since(std::time::UNIX_EPOCH) |
| 110 | + .change_context(Error::Io) |
| 111 | + .map(|duration| duration.as_secs()) |
| 112 | + } |
| 113 | +} |
0 commit comments