quick_search/crates/quicksearch-gui/src/backend.rs

86 lines
3.4 KiB
Rust

//! Wiring between the egui thread and the core services.
//!
//! All communication is non-blocking from the UI's point of view:
//! searches stream over an mpsc receiver drained each frame, indexing
//! state is polled, and the duplicates query runs on a throwaway worker
//! thread. Core threads wake the UI through `ctx.request_repaint()` — every
//! one of them, which is what makes polling enough: the frame that starts the
//! UI's own cadence is the one the waking thread asks for.
//!
//! The duplicates scan is the *only* throwaway thread left here, and it fires
//! on a user action rather than a timer. The status bar's file count used to
//! spawn one every five seconds, each opening its own connection — a page
//! cache and a fresh allocator arena per refresh, neither of which glibc gives
//! back. It now rides on `IndexerState`, published by the coordinator off the
//! connection it already holds.
use std::sync::{mpsc, Arc};
use quicksearch_core::config::Config;
use quicksearch_core::coordinator::IndexCoordinator;
use quicksearch_core::search::{DuplicateGroup, SearchService, SearchUpdate};
use quicksearch_core::shutdown;
pub struct Backend {
pub coordinator: Arc<IndexCoordinator>,
pub search: Option<SearchService>,
pub search_rx: mpsc::Receiver<SearchUpdate>,
pub dup_job: Option<mpsc::Receiver<Result<Vec<DuplicateGroup>, String>>>,
}
impl Backend {
pub fn start(config: &Config, ctx: egui::Context) -> Result<Backend, String> {
// The coordinator gets the same repaint hook the search worker does,
// and needs it for the same reason: eframe is reactive, and a run it
// schedules on its own — the periodic reindex a launch is already due —
// would otherwise sit unseen behind a settled window until the pointer
// moved. It calls this on the edge into work, not on a cadence.
let coord_ctx = ctx.clone();
let coordinator = Arc::new(IndexCoordinator::start(
config.clone(),
Arc::new(move || coord_ctx.request_repaint()),
)?);
if let Err(e) = shutdown::install_signal_handler(coordinator.clone()) {
quicksearch_core::log_warn!("failed to install signal handler: {}", e);
}
let repaint_ctx = ctx.clone();
let (search, search_rx) = SearchService::new(
config.resolved_database_path(),
Arc::new(move || repaint_ctx.request_repaint()),
);
Ok(Backend {
coordinator,
search: Some(search),
search_rx,
dup_job: None,
})
}
pub fn search(&self) -> &SearchService {
self.search.as_ref().expect("search service alive")
}
/// Kick off (or restart) the duplicates listing on a worker thread.
pub fn start_duplicates(&mut self, config: &Config, ctx: egui::Context) {
let (tx, rx) = mpsc::channel();
let db = config.resolved_database_path();
std::thread::spawn(move || {
let result =
quicksearch_core::search::find_duplicate_groups(&db.to_string_lossy(), 500, 0);
let _ = tx.send(result);
ctx.request_repaint();
});
self.dup_job = Some(rx);
}
/// Join the search worker and stop the coordinator. Called once from
/// `on_exit`.
pub fn shutdown(&mut self) {
if let Some(search) = self.search.take() {
search.shutdown();
}
self.coordinator.shutdown();
}
}