mirror of
https://github.com/sreedevk/deduplicator.git
synced 2026-08-31 04:25:31 +00:00
minor improvements
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -51,9 +52,14 @@ impl App {
|
||||
Ok(Message::Exit) => {
|
||||
server_tx
|
||||
.send(Message::Exit)
|
||||
.expect("message passing to app from ui failed");
|
||||
.expect("message passing to app from ui failed.");
|
||||
break;
|
||||
}
|
||||
Ok(Message::AddScanDirectory(dir)) => {
|
||||
server_tx
|
||||
.send(Message::AddScanDirectory(dir))
|
||||
.expect("message passing to server failed.");
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -4,15 +4,13 @@ mod processor;
|
||||
mod scanner;
|
||||
mod store;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::collections::HashMap;
|
||||
use anyhow::Result;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use threadpool::ThreadPool;
|
||||
|
||||
use self::file::FileMeta;
|
||||
use self::processor::Processor;
|
||||
use self::scanner::Scanner;
|
||||
use self::store::Store;
|
||||
@@ -44,16 +42,20 @@ impl Server {
|
||||
let processor_fq = self.fq.clone();
|
||||
let processor_store = self.dupstore.clone();
|
||||
let (processor_tx, processor_rx) = channel::<Message>();
|
||||
let (server_tx, server_rx) = channel::<Message>();
|
||||
|
||||
self.tpool.execute(move || {
|
||||
Processor::new(processor_fq, processor_store, processor_rx)
|
||||
.process()
|
||||
.ok();
|
||||
.expect("processer execution interrupted.");
|
||||
});
|
||||
|
||||
let scanner_fq = self.fq.clone();
|
||||
let (scanner_tx, scanner_rx) = channel::<Message>();
|
||||
self.tpool.execute(move || {
|
||||
Scanner::new(scanner_fq, scanner_rx).index().ok();
|
||||
Scanner::new(scanner_fq, scanner_rx)
|
||||
.index()
|
||||
.expect("scanner indexing interrupted.");
|
||||
});
|
||||
|
||||
self.tpool.execute(move || loop {
|
||||
@@ -61,7 +63,7 @@ impl Server {
|
||||
Ok(Message::AddScanDirectory(path)) => {
|
||||
scanner_tx
|
||||
.send(Message::AddScanDirectory(path))
|
||||
.unwrap_or_default();
|
||||
.expect("scanner tx message passing failed.");
|
||||
}
|
||||
Ok(Message::None) => {}
|
||||
Ok(Message::Exit) | Err(_) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::file::FileMeta;
|
||||
@@ -41,7 +41,6 @@ impl Processor {
|
||||
Some(file_res) => match FileMeta::new(file_res) {
|
||||
Err(_) => continue,
|
||||
Ok(fm) => {
|
||||
// self.duplicates.add(Index::Partial(fm_arc.partial), fm_arc.clone());
|
||||
let fm_arc = Arc::new(fm);
|
||||
self.duplicates
|
||||
.add(Index::Size(fm_arc.size), fm_arc.clone());
|
||||
|
||||
@@ -57,6 +57,8 @@ impl Scanner {
|
||||
}
|
||||
}
|
||||
|
||||
self.threadpool.join();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -85,3 +87,7 @@ impl Scanner {
|
||||
Ok((files, dirs))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use super::file::FileMeta;
|
||||
use anyhow::Result;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use dashmap::DashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub enum Index {
|
||||
@@ -11,19 +10,23 @@ pub enum Index {
|
||||
}
|
||||
|
||||
pub struct Store {
|
||||
internal: Arc<Mutex<HashMap<Index, Vec<Arc<FileMeta>>>>>,
|
||||
internal: Arc<DashMap<Index, Vec<Arc<FileMeta>>>>,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
internal: Arc::new(Mutex::new(HashMap::new())),
|
||||
internal: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn entries(&self) -> Vec<Vec<Arc<FileMeta>>> {
|
||||
self.internal.iter().map(|k| k.value().clone()).collect()
|
||||
}
|
||||
|
||||
pub fn add(&self, index: Index, file: Arc<FileMeta>) {
|
||||
let mut imut = self.internal.lock().unwrap();
|
||||
imut.entry(index)
|
||||
self.internal
|
||||
.entry(index)
|
||||
.and_modify(|fg| fg.push(file.clone()))
|
||||
.or_insert(vec![file]);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
@@ -6,7 +8,6 @@ use ratatui::crossterm::event::{self, Event, KeyCode};
|
||||
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
|
||||
use crate::server::file::FileMeta;
|
||||
use crate::server::{Message, Server};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -54,17 +55,16 @@ impl Tui {
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
let items_clone = {
|
||||
let mut_fq = self.server.fq.lock().unwrap();
|
||||
mut_fq.clone()
|
||||
};
|
||||
|
||||
let list = List::from_iter(
|
||||
items_clone
|
||||
.iter()
|
||||
.map(|fpath| ListItem::new(format!("{}", fpath))),
|
||||
);
|
||||
let listitems = self
|
||||
.server
|
||||
.dupstore
|
||||
.entries()
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|f| ListItem::new(format!("{}", f.path)))
|
||||
.collect::<Vec<ListItem>>();
|
||||
|
||||
let list = List::new(listitems);
|
||||
frame.render_widget(list.block(Block::new().borders(Borders::ALL)), frame.area());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user