2020-04-22 04:28:47 +00:00
|
|
|
package filer_pb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-04-22 04:28:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TraverseBfs(filerClient FilerClient, parentPath util.FullPath, fn func(parentPath util.FullPath, entry *Entry)) (err error) {
|
|
|
|
|
|
|
|
K := 5
|
|
|
|
|
|
|
|
var jobQueueWg sync.WaitGroup
|
|
|
|
queue := util.NewQueue()
|
|
|
|
jobQueueWg.Add(1)
|
|
|
|
queue.Enqueue(parentPath)
|
|
|
|
var isTerminating bool
|
|
|
|
|
|
|
|
for i := 0; i < K; i++ {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if isTerminating {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t := queue.Dequeue()
|
|
|
|
if t == nil {
|
|
|
|
time.Sleep(329 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dir := t.(util.FullPath)
|
|
|
|
processErr := processOneDirectory(filerClient, dir, queue, &jobQueueWg, fn)
|
|
|
|
if processErr != nil {
|
|
|
|
err = processErr
|
|
|
|
}
|
|
|
|
jobQueueWg.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
jobQueueWg.Wait()
|
|
|
|
isTerminating = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func processOneDirectory(filerClient FilerClient, parentPath util.FullPath, queue *util.Queue, jobQueueWg *sync.WaitGroup, fn func(parentPath util.FullPath, entry *Entry)) (err error) {
|
|
|
|
|
2020-04-30 00:40:08 +00:00
|
|
|
return ReadDirAllEntries(filerClient, parentPath, "", func(entry *Entry, isLast bool) error {
|
2020-04-22 04:28:47 +00:00
|
|
|
|
|
|
|
fn(parentPath, entry)
|
|
|
|
|
|
|
|
if entry.IsDirectory {
|
|
|
|
subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
|
|
|
|
if parentPath == "/" {
|
|
|
|
subDir = "/" + entry.Name
|
|
|
|
}
|
|
|
|
jobQueueWg.Add(1)
|
|
|
|
queue.Enqueue(util.FullPath(subDir))
|
|
|
|
}
|
2020-04-30 00:40:08 +00:00
|
|
|
return nil
|
2020-04-22 04:28:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|