refactor: code readable.
This commit is contained in:
127
src/meta/tree.rs
127
src/meta/tree.rs
@@ -1,16 +1,14 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::file::{calc_xxh128_with_callback, FileMeta};
|
||||
use super::progress::ProgressTracker;
|
||||
use crate::constants::META_VERSION;
|
||||
use crate::utils::{basename, friendly_size, should_skip_dir, should_skip_file};
|
||||
use crate::utils::{basename, should_skip_dir, should_skip_file};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DirSnapshot {
|
||||
@@ -23,34 +21,13 @@ pub struct DirSnapshot {
|
||||
|
||||
impl DirSnapshot {
|
||||
pub fn build_root(path: &Path) -> Result<Self> {
|
||||
// 先统计总文件数
|
||||
let total_files = count_files(path)?;
|
||||
let tracker = ProgressTracker::new(total_files, "构建中...");
|
||||
|
||||
// 创建进度条
|
||||
let pb = if total_files > 0 {
|
||||
let pb = ProgressBar::new(total_files);
|
||||
pb.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({percent}%) {msg}")
|
||||
.unwrap()
|
||||
.progress_chars("#>-"),
|
||||
);
|
||||
pb.set_message("构建中...");
|
||||
Some(pb)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let total_bytes_read = Arc::new(Mutex::new(0u64));
|
||||
let total_iops = Arc::new(Mutex::new(0u64));
|
||||
let mut node = Self::build_node(path, &pb, &start, &total_bytes_read, &total_iops)?;
|
||||
let mut node = Self::build_node(path, &tracker)?;
|
||||
node.v = Some(META_VERSION.to_string());
|
||||
|
||||
if let Some(pb) = &pb {
|
||||
pb.finish_with_message("构建完成");
|
||||
}
|
||||
|
||||
tracker.finish("构建完成");
|
||||
Ok(node)
|
||||
}
|
||||
|
||||
@@ -58,13 +35,7 @@ impl DirSnapshot {
|
||||
Ok(serde_json::from_reader(reader)?)
|
||||
}
|
||||
|
||||
fn build_node(
|
||||
path: &Path,
|
||||
pb: &Option<ProgressBar>,
|
||||
start: &Instant,
|
||||
total_bytes_read: &Arc<Mutex<u64>>,
|
||||
total_iops: &Arc<Mutex<u64>>,
|
||||
) -> Result<Self> {
|
||||
fn build_node(path: &Path, tracker: &ProgressTracker) -> Result<Self> {
|
||||
let dir_name = path
|
||||
.file_name()
|
||||
.map(basename)
|
||||
@@ -92,7 +63,7 @@ impl DirSnapshot {
|
||||
if should_skip_dir(&name) {
|
||||
continue;
|
||||
}
|
||||
dirs.push(Self::build_node(&full_path, pb, start, total_bytes_read, total_iops)?);
|
||||
dirs.push(Self::build_node(&full_path, tracker)?);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -100,28 +71,11 @@ impl DirSnapshot {
|
||||
continue;
|
||||
}
|
||||
|
||||
let total_bytes_read_clone = total_bytes_read.clone();
|
||||
let total_iops_clone = total_iops.clone();
|
||||
let meta = FileMeta::from_path_with_callback(&full_path, move |bytes| {
|
||||
*total_bytes_read_clone.lock().unwrap() += bytes;
|
||||
}, move || {
|
||||
*total_iops_clone.lock().unwrap() += 1;
|
||||
})?;
|
||||
let on_bytes = tracker.bytes_callback();
|
||||
let on_iop = tracker.iop_callback();
|
||||
let meta = FileMeta::from_path_with_callback(&full_path, on_bytes, on_iop)?;
|
||||
files.push(meta);
|
||||
|
||||
// 更新进度条
|
||||
if let Some(pb) = pb {
|
||||
pb.inc(1);
|
||||
let elapsed = start.elapsed().as_secs_f64();
|
||||
let total_bytes = *total_bytes_read.lock().unwrap();
|
||||
let total_ops = *total_iops.lock().unwrap();
|
||||
if total_bytes > 0 && elapsed > 0.0 {
|
||||
let speed_bytes_per_sec = total_bytes as f64 / elapsed;
|
||||
let speed_str = friendly_size(speed_bytes_per_sec as u64);
|
||||
let iops = total_ops as f64 / elapsed;
|
||||
pb.set_message(format!("IO速度: {}/s | IOPS: {:.0}", speed_str, iops));
|
||||
}
|
||||
}
|
||||
tracker.finish_file();
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
@@ -151,34 +105,13 @@ impl DirSnapshot {
|
||||
}
|
||||
|
||||
pub fn scan_dir_xxh128(path: &Path) -> Result<BTreeMap<PathBuf, String>> {
|
||||
// 先统计总文件数
|
||||
let total_files = count_files(path)?;
|
||||
|
||||
// 创建进度条
|
||||
let pb = if total_files > 0 {
|
||||
let pb = ProgressBar::new(total_files);
|
||||
pb.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({percent}%) {msg}")
|
||||
.unwrap()
|
||||
.progress_chars("#>-"),
|
||||
);
|
||||
pb.set_message("扫描中...");
|
||||
Some(pb)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let tracker = ProgressTracker::new(total_files, "扫描中...");
|
||||
|
||||
let mut map = BTreeMap::new();
|
||||
let start = Instant::now();
|
||||
let total_bytes_read = Arc::new(Mutex::new(0u64));
|
||||
let total_iops = Arc::new(Mutex::new(0u64));
|
||||
walk_dir_with_progress(path, &mut map, &pb, &start, &total_bytes_read, &total_iops)?;
|
||||
|
||||
if let Some(pb) = &pb {
|
||||
pb.finish_with_message("扫描完成");
|
||||
}
|
||||
walk_dir_with_progress(path, &mut map, &tracker)?;
|
||||
|
||||
tracker.finish("扫描完成");
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
@@ -220,10 +153,7 @@ fn count_files_recursive(path: &Path, count: &mut u64) -> Result<()> {
|
||||
fn walk_dir_with_progress(
|
||||
path: &Path,
|
||||
map: &mut BTreeMap<PathBuf, String>,
|
||||
pb: &Option<ProgressBar>,
|
||||
start: &Instant,
|
||||
total_bytes_read: &Arc<Mutex<u64>>,
|
||||
total_iops: &Arc<Mutex<u64>>,
|
||||
tracker: &ProgressTracker,
|
||||
) -> Result<()> {
|
||||
let mut entries = fs::read_dir(path)
|
||||
.with_context(|| format!("无法遍历目录: {}", path.display()))?
|
||||
@@ -243,7 +173,7 @@ fn walk_dir_with_progress(
|
||||
if should_skip_dir(&name) {
|
||||
continue;
|
||||
}
|
||||
walk_dir_with_progress(&full_path, map, pb, start, total_bytes_read, total_iops)?;
|
||||
walk_dir_with_progress(&full_path, map, tracker)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -251,28 +181,11 @@ fn walk_dir_with_progress(
|
||||
continue;
|
||||
}
|
||||
|
||||
let total_bytes_read_clone = total_bytes_read.clone();
|
||||
let total_iops_clone = total_iops.clone();
|
||||
let hash = calc_xxh128_with_callback(&full_path, move |bytes| {
|
||||
*total_bytes_read_clone.lock().unwrap() += bytes;
|
||||
}, move || {
|
||||
*total_iops_clone.lock().unwrap() += 1;
|
||||
})?;
|
||||
let on_bytes = tracker.bytes_callback();
|
||||
let on_iop = tracker.iop_callback();
|
||||
let hash = calc_xxh128_with_callback(&full_path, on_bytes, on_iop)?;
|
||||
map.insert(full_path, hash);
|
||||
|
||||
// 更新进度条
|
||||
if let Some(pb) = pb {
|
||||
pb.inc(1);
|
||||
let elapsed = start.elapsed().as_secs_f64();
|
||||
let total_bytes = *total_bytes_read.lock().unwrap();
|
||||
let total_ops = *total_iops.lock().unwrap();
|
||||
if total_bytes > 0 && elapsed > 0.0 {
|
||||
let speed_bytes_per_sec = total_bytes as f64 / elapsed;
|
||||
let speed_str = friendly_size(speed_bytes_per_sec as u64);
|
||||
let iops = total_ops as f64 / elapsed;
|
||||
pb.set_message(format!("IO速度: {}/s | IOPS: {:.0}", speed_str, iops));
|
||||
}
|
||||
}
|
||||
tracker.finish_file();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user