lazy_static cleanup
general code cleanup in filecache
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
@@ -16,13 +14,6 @@ use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
|||||||
|
|
||||||
type CacheGuard<T> = Arc<RwLock<T>>;
|
type CacheGuard<T> = Arc<RwLock<T>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum FileEntryError {
|
|
||||||
NeedsUpdate,
|
|
||||||
EmptyFile,
|
|
||||||
FileOpenError,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FileEntry {
|
pub struct FileEntry {
|
||||||
path: CacheGuard<std::path::PathBuf>,
|
path: CacheGuard<std::path::PathBuf>,
|
||||||
@@ -57,7 +48,6 @@ impl FileEntry {
|
|||||||
Arc::new(RwLock::new(String::from(&*path)))
|
Arc::new(RwLock::new(String::from(&*path)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: this whole function has been hacked up trying to fix dead locks.
|
|
||||||
fn do_hash(&mut self) {
|
fn do_hash(&mut self) {
|
||||||
let path_ptr = self.path.clone();
|
let path_ptr = self.path.clone();
|
||||||
let path_lock = path_ptr.read().unwrap();
|
let path_lock = path_ptr.read().unwrap();
|
||||||
@@ -75,7 +65,6 @@ impl FileEntry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut hasher = blake3::Hasher::new();
|
let mut hasher = blake3::Hasher::new();
|
||||||
// HACK: this feels bad...
|
|
||||||
hasher.update_rayon(
|
hasher.update_rayon(
|
||||||
&temp_content
|
&temp_content
|
||||||
.bytes()
|
.bytes()
|
||||||
@@ -98,36 +87,6 @@ impl FileEntry {
|
|||||||
|
|
||||||
*self.hash.write().unwrap() = Some(hasher.finalize());
|
*self.hash.write().unwrap() = Some(hasher.finalize());
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let has_content = ptr.read().unwrap().is_some();
|
|
||||||
if has_content {
|
|
||||||
let mut hasher = blake3::Hasher::new();
|
|
||||||
hasher.update_rayon(content.get_ref());
|
|
||||||
|
|
||||||
|
|
||||||
let hash_ptr = self.hash.clone();
|
|
||||||
let hash_ptr = hash_ptr.write().unwrap();
|
|
||||||
let hash_ptr = Some(hasher.finalize());
|
|
||||||
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
|
|
||||||
let content_lock = self.content.clone();
|
|
||||||
let mut content = content_lock.write().unwrap();
|
|
||||||
|
|
||||||
if content.is_none() {
|
|
||||||
let path_lock = self.path.clone();
|
|
||||||
let file = std::fs::File::open(path_lock.read().unwrap().as_path());
|
|
||||||
let file = file.unwrap_or_else(|_| panic!("issue opening file for {:#?}", &self.path));
|
|
||||||
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +101,7 @@ pub struct FileCache {
|
|||||||
cache: CacheGuard<HashMap<String, CacheGuard<FileEntry>>>,
|
cache: CacheGuard<HashMap<String, CacheGuard<FileEntry>>>,
|
||||||
cache_dir: std::path::PathBuf,
|
cache_dir: std::path::PathBuf,
|
||||||
notify_watcher: RecommendedWatcher,
|
notify_watcher: RecommendedWatcher,
|
||||||
notify_thread: std::thread::JoinHandle<()>,
|
_notify_thread: std::thread::JoinHandle<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileCache {
|
impl FileCache {
|
||||||
@@ -171,7 +130,7 @@ impl FileCache {
|
|||||||
cache: Arc::new(RwLock::new(HashMap::new())),
|
cache: Arc::new(RwLock::new(HashMap::new())),
|
||||||
cache_dir: cache_dir.to_path_buf(),
|
cache_dir: cache_dir.to_path_buf(),
|
||||||
notify_watcher: notify::Watcher::new(tx, Duration::from_secs(1)).unwrap(),
|
notify_watcher: notify::Watcher::new(tx, Duration::from_secs(1)).unwrap(),
|
||||||
notify_thread: thread::Builder::new()
|
_notify_thread: thread::Builder::new()
|
||||||
.name("notify-thread".to_string())
|
.name("notify-thread".to_string())
|
||||||
.spawn(move || FileCache::notify_loop(rx))
|
.spawn(move || FileCache::notify_loop(rx))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
@@ -222,10 +181,9 @@ impl FileCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn notify_loop(rx: Receiver<DebouncedEvent>) {
|
fn notify_loop(rx: Receiver<DebouncedEvent>) {
|
||||||
let this_thread = std::thread::current();
|
log::info!("notify loop starting on thread-{:?}", std::thread::current());
|
||||||
log::info!("notify loop starting on thread-{:?}", &this_thread);
|
|
||||||
|
|
||||||
let thread_name: String = match &this_thread.name() {
|
let thread_name: String = match std::thread::current().name() {
|
||||||
Some(s) => s.to_string(),
|
Some(s) => s.to_string(),
|
||||||
None => "notify-thread".to_string(),
|
None => "notify-thread".to_string(),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ extern crate notify;
|
|||||||
|
|
||||||
/* general utility crates */
|
/* general utility crates */
|
||||||
//extern crate chrono;
|
//extern crate chrono;
|
||||||
extern crate lazy_static;
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
mod filecache;
|
mod filecache;
|
||||||
|
|||||||
Reference in New Issue
Block a user