mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-06 19:45:23 +00:00
caching for git repos and max_lines_in_memory for text
This commit is contained in:
parent
9d6d1f47ba
commit
14357f2c96
@ -30,9 +30,9 @@ pub struct Channel {
|
|||||||
running: bool,
|
running: bool,
|
||||||
icon: FileIcon,
|
icon: FileIcon,
|
||||||
crawl_handle: JoinHandle<()>,
|
crawl_handle: JoinHandle<()>,
|
||||||
entry_cache: Arc<Mutex<HashSet<String>>>,
|
git_dirs_cache: Arc<Mutex<HashSet<String>>>,
|
||||||
// TODO: implement cache validation/invalidation
|
// TODO: implement cache validation/invalidation
|
||||||
cache_valid: bool,
|
cache_valid: Arc<Mutex<bool>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Channel {
|
impl Channel {
|
||||||
@ -44,12 +44,14 @@ impl Channel {
|
|||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
let entry_cache = Arc::new(Mutex::new(HashSet::new()));
|
let entry_cache = Arc::new(Mutex::new(HashSet::new()));
|
||||||
|
let cache_valid = Arc::new(Mutex::new(false));
|
||||||
// start loading files in the background
|
// start loading files in the background
|
||||||
// PERF: store the results somewhere in a cache
|
// PERF: store the results somewhere in a cache
|
||||||
let crawl_handle = tokio::spawn(crawl_for_repos(
|
let crawl_handle = tokio::spawn(crawl_for_repos(
|
||||||
std::env::home_dir().expect("Could not get home directory"),
|
std::env::home_dir().expect("Could not get home directory"),
|
||||||
matcher.injector(),
|
matcher.injector(),
|
||||||
entry_cache.clone(),
|
entry_cache.clone(),
|
||||||
|
cache_valid.clone(),
|
||||||
));
|
));
|
||||||
Channel {
|
Channel {
|
||||||
matcher,
|
matcher,
|
||||||
@ -59,8 +61,8 @@ impl Channel {
|
|||||||
running: false,
|
running: false,
|
||||||
icon: FileIcon::from("git"),
|
icon: FileIcon::from("git"),
|
||||||
crawl_handle,
|
crawl_handle,
|
||||||
entry_cache,
|
git_dirs_cache: entry_cache,
|
||||||
cache_valid: false,
|
cache_valid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +159,7 @@ async fn crawl_for_repos(
|
|||||||
starting_point: std::path::PathBuf,
|
starting_point: std::path::PathBuf,
|
||||||
injector: nucleo::Injector<DirEntry>,
|
injector: nucleo::Injector<DirEntry>,
|
||||||
entry_cache: Arc<Mutex<HashSet<String>>>,
|
entry_cache: Arc<Mutex<HashSet<String>>>,
|
||||||
|
cache_valid: Arc<Mutex<bool>>,
|
||||||
) {
|
) {
|
||||||
let mut walker_overrides_builder = OverrideBuilder::new(&starting_point);
|
let mut walker_overrides_builder = OverrideBuilder::new(&starting_point);
|
||||||
walker_overrides_builder.add(".git").unwrap();
|
walker_overrides_builder.add(".git").unwrap();
|
||||||
@ -199,4 +202,6 @@ async fn crawl_for_repos(
|
|||||||
ignore::WalkState::Continue
|
ignore::WalkState::Continue
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
*cache_valid.lock() = true;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use std::{
|
|||||||
fs::File,
|
fs::File,
|
||||||
io::{BufRead, Read, Seek},
|
io::{BufRead, Read, Seek},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::{atomic::AtomicUsize, Arc},
|
||||||
};
|
};
|
||||||
|
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
@ -172,16 +172,24 @@ impl OnAir for Channel {
|
|||||||
/// a lot of files (e.g. starting tv in $HOME).
|
/// a lot of files (e.g. starting tv in $HOME).
|
||||||
const MAX_FILE_SIZE: u64 = 4 * 1024 * 1024;
|
const MAX_FILE_SIZE: u64 = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
const MAX_IN_MEMORY_LINES: usize = 5_000_000;
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[allow(clippy::unused_async)]
|
||||||
async fn load_candidates(path: PathBuf, injector: Injector<CandidateLine>) {
|
async fn load_candidates(path: PathBuf, injector: Injector<CandidateLine>) {
|
||||||
let current_dir = std::env::current_dir().unwrap();
|
let current_dir = std::env::current_dir().unwrap();
|
||||||
let walker =
|
let walker =
|
||||||
walk_builder(&path, *DEFAULT_NUM_THREADS, None).build_parallel();
|
walk_builder(&path, *DEFAULT_NUM_THREADS, None).build_parallel();
|
||||||
|
|
||||||
|
let lines_in_mem = Arc::new(AtomicUsize::new(0));
|
||||||
|
|
||||||
walker.run(|| {
|
walker.run(|| {
|
||||||
let injector = injector.clone();
|
let injector = injector.clone();
|
||||||
let current_dir = current_dir.clone();
|
let current_dir = current_dir.clone();
|
||||||
|
let lines_in_mem = lines_in_mem.clone();
|
||||||
Box::new(move |result| {
|
Box::new(move |result| {
|
||||||
|
if lines_in_mem.load(std::sync::atomic::Ordering::Relaxed) > MAX_IN_MEMORY_LINES {
|
||||||
|
return ignore::WalkState::Quit;
|
||||||
|
}
|
||||||
if let Ok(entry) = result {
|
if let Ok(entry) = result {
|
||||||
if entry.file_type().unwrap().is_file() {
|
if entry.file_type().unwrap().is_file() {
|
||||||
if let Ok(m) = entry.metadata() {
|
if let Ok(m) = entry.metadata() {
|
||||||
@ -192,6 +200,7 @@ async fn load_candidates(path: PathBuf, injector: Injector<CandidateLine>) {
|
|||||||
// iterate over the lines of the file
|
// iterate over the lines of the file
|
||||||
match File::open(entry.path()) {
|
match File::open(entry.path()) {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
// is the file a text-based file?
|
||||||
let mut reader = std::io::BufReader::new(&file);
|
let mut reader = std::io::BufReader::new(&file);
|
||||||
let mut buffer = [0u8; 128];
|
let mut buffer = [0u8; 128];
|
||||||
match reader.read(&mut buffer) {
|
match reader.read(&mut buffer) {
|
||||||
@ -212,6 +221,7 @@ async fn load_candidates(path: PathBuf, injector: Injector<CandidateLine>) {
|
|||||||
return ignore::WalkState::Continue;
|
return ignore::WalkState::Continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// read the lines of the file
|
||||||
let mut line_number = 0;
|
let mut line_number = 0;
|
||||||
for maybe_line in reader.lines() {
|
for maybe_line in reader.lines() {
|
||||||
match maybe_line {
|
match maybe_line {
|
||||||
@ -238,6 +248,7 @@ async fn load_candidates(path: PathBuf, injector: Injector<CandidateLine>) {
|
|||||||
c.line.clone().into();
|
c.line.clone().into();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
lines_in_mem.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("Error reading line: {:?}", e);
|
info!("Error reading line: {:?}", e);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user