From 95a5501a07cb0132615d6b127f85f91143fc2a6a Mon Sep 17 00:00:00 2001 From: Monius Date: Tue, 5 Mar 2024 03:12:31 +0800 Subject: [PATCH] replace rayon with tokio --- ato-cli/Cargo.toml | 4 ++-- ato-cli/src/cli.rs | 8 ++++--- ato/Cargo.toml | 3 ++- ato/src/lib.rs | 3 ++- ato/src/tk.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 ato/src/tk.rs diff --git a/ato-cli/Cargo.toml b/ato-cli/Cargo.toml index b18c588..4487999 100644 --- a/ato-cli/Cargo.toml +++ b/ato-cli/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "ato-cli" -version = "1.0.1" +version = "1.0.2" edition = "2021" [dependencies] ato = { path = "../ato" } clap = { version= "4.5.1", features=["derive"] } - +tokio = { version = "1.36.0", features = ["full"] } [[bin]] name = "ato-cli" diff --git a/ato-cli/src/cli.rs b/ato-cli/src/cli.rs index ef431a5..aeb6ab1 100644 --- a/ato-cli/src/cli.rs +++ b/ato-cli/src/cli.rs @@ -1,5 +1,6 @@ use clap::Parser; -use libato::ry::process; +// use libato::ry::process; +use libato::tk::process; #[derive(Parser, Debug)] struct Args { @@ -9,10 +10,11 @@ struct Args { external: String, } -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let _args = Args::parse(); let start = std::time::Instant::now(); - let _ = process(&_args.dataset, &_args.external); + let _ = process(&_args.dataset, &_args.external).await?; let duration = start.elapsed(); println!("Time elapsed in process() is: {:?}", duration); Ok(()) diff --git a/ato/Cargo.toml b/ato/Cargo.toml index d2916b6..d20c20b 100644 --- a/ato/Cargo.toml +++ b/ato/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -rayon = "1.9.0" +tokio = { version = "1.36.0", features = ["full"] } +# rayon = "1.9.0" walkdir="2.5.0" md5 = "0.7.0" diff --git a/ato/src/lib.rs b/ato/src/lib.rs index 4fd30c7..d04bf03 100644 --- a/ato/src/lib.rs +++ b/ato/src/lib.rs @@ -1 +1,2 @@ -pub mod ry; \ No newline at end of file +// pub mod ry; +pub mod tk; \ No newline at end of file diff --git a/ato/src/tk.rs b/ato/src/tk.rs new file mode 100644 index 0000000..8c82de1 --- /dev/null +++ b/ato/src/tk.rs @@ -0,0 +1,55 @@ + +use walkdir::WalkDir; +use tokio::{fs, task}; +use std::path; + +pub async fn async_hash(r: &str, p: &path::Path, e: &str) -> Result<(), Box>{ + let _dir = p.with_file_name(""); + let _num = _dir.file_name().unwrap().to_str().unwrap(); + let _in = p.with_file_name(format!("{}_{}_{}", _num, "total", e)); + let _up = p.with_file_name(format!("{}_{}_{}", _num, "up", e)); + let _low = p.with_file_name(format!("{}_{}_{}", _num, "low", e)); + if _in.is_file() && _up.is_file() && _low.is_file() { + let contents = fs::read(&_in).await?; + let digest = md5::compute(&contents); + let md5_dir = format!("{}/{:x}", r, digest); + + let _ = fs::create_dir_all(&md5_dir).await?; + let _ = fs::copy(_in, format!("{}/in.stl", &md5_dir)).await?; + let _ = fs::copy(_up, format!("{}/up.stl", &md5_dir)).await?; + let _ = fs::copy(_low, format!("{}/low.stl", &md5_dir)).await?; + let _ = fs::copy(p, format!("{}/gt.stl", &md5_dir)).await?; + } + else { + println!("{:#?}", p); + } + Ok(()) +} + +pub async fn process(_pth: &str, e:&str) -> Result<(), Box> { + let root = format!("{}_sha", _pth.to_owned()); + let _ = fs::create_dir_all(&root).await?; + let mut pool = vec![]; + + let pdd: Vec = + WalkDir::new(_pth).into_iter() + .map(|e| e.unwrap().into_path()) + .filter(|c| { + c.display().to_string().contains("gt") + }) + .collect(); + + for p in pdd { + let _root = root.clone(); + let _e = e.to_string(); + pool.push(task::spawn(async move { + let _ = async_hash(&_root, &p, &_e).await; + })); + } + + for task in pool { + task.await?; + } + + Ok(()) +} \ No newline at end of file