filematcher/main.v.bk
2026-01-09 13:25:44 +08:00

83 lines
1.3 KiB
Plaintext

module main
import os
import time
fn main() {
current_dir := 'C:\\'
mut scanner :=new_scanner(12)
scanner.do_scan(current_dir)
}
fn new_scanner(max_worker_count int) &Scanner{
return &Scanner{
max_worker: max_worker_count
}
}
struct Scanner{
mut:
max_worker int = 32
worker_count int
search_request chan string
work_done chan bool
}
fn (mut s Scanner)scanner(path string,is_master bool){
items := os.ls(path) or {
println('列出目录失败: $err\n$path')
s.work_done <- true
return
}
for item in items {
full_path := os.join_path(path, item)
if os.is_dir(full_path) {
if s.worker_count < s.max_worker{
s.search_request <- full_path
}else{
s.scanner(full_path,false)
}
} else {
println('$full_path')
}
}
if is_master{
s.work_done <- true
}
}
fn (mut s Scanner)do_scan(path string){
start := time.now()
s.worker_count = 1
go s.scanner(path,true)
s.watting_for_workers()
end := time.now()
duration := end - start
println('耗时: ${duration.seconds()} 秒')
}
fn (mut s Scanner)watting_for_workers(){
for {
select {
path := <- s.search_request {
s.worker_count++
go s.scanner(path,true)
}
_ :=<- s.work_done{
s.worker_count--
if s.worker_count == 0{
return
}
}
}
}
}