-
Notifications
You must be signed in to change notification settings - Fork 0
/
work_sheet.go
137 lines (119 loc) · 2.4 KB
/
work_sheet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package traverse
import (
"github.com/cheggaaa/pb/v3"
"sync"
"time"
)
// the map[string]bool, "" item is self
// the name like path,name must unique
type Sheet map[string]bool
const (
ItemAddStat = iota
ItemOverStat
)
type itemStat struct {
Name string
Stat int
}
type WorkSheet struct {
Sheet Sheet
Over bool
overChan chan struct{}
lock *sync.RWMutex
traverseOverFlag bool
overLock *sync.RWMutex
WithProgressBarOut bool
pBar *pb.ProgressBar
Count int64
OverCount int64
statChan chan itemStat
}
func NewWorkSheet() *WorkSheet {
ws := &WorkSheet{
Sheet: make(map[string]bool),
overChan: make(chan struct{}),
lock: &sync.RWMutex{},
overLock: &sync.RWMutex{},
statChan: make(chan itemStat, 2048),
}
go ws.runStatChanHandle()
return ws
}
func (w *WorkSheet) ItemAdd(path string) {
w.statChan <- itemStat{
Name: path,
Stat: ItemAddStat,
}
}
func (w *WorkSheet) ItemOver(path string) {
w.statChan <- itemStat{
Name: path,
Stat: ItemOverStat,
}
}
func (w *WorkSheet) runStatChanHandle() {
for stat := range w.statChan {
if stat.Stat == ItemAddStat {
w.Count += 1
if _, ok := w.Sheet[stat.Name]; !ok {
w.Sheet[stat.Name] = false
}
} else {
w.OverCount += 1
w.Sheet[stat.Name] = true
}
}
}
func (w *WorkSheet) TraverseOver() {
w.traverseOverFlag = true
if w.Over {
return
}
// todo it's bad
go func() {
for {
if w.Count == w.OverCount {
w.Over = true
w.overChan <- struct{}{}
return
} else {
time.Sleep(100 * time.Millisecond)
}
}
}()
}
func (w *WorkSheet) IsOver() bool {
return w.Count == w.OverCount
}
func (w *WorkSheet) Wait() {
if w.Over {
return
}
<-w.overChan
}
// ---------------------------------------------------------------------------------------------------------------------
//func (w *WorkSheet) AddSub(name string, sub string) {
// w.lock.Lock()
// defer w.lock.Unlock()
//
// if _, ok := w.Sheet[name]; ok {
// if _, ok := w.Sheet[name][sub]; !ok {
// w.Sheet[name][sub] = false
// }
// }
// w.Add(sub)
//}
//
//func (w *WorkSheet) AddSubList(name string, subs []string) {
// w.lock.Lock()
// defer w.lock.Unlock()
//
// if _, ok := w.Sheet[name]; ok {
// for _, sub := range subs {
// if _, ok := w.Sheet[name][sub]; !ok {
// w.Sheet[name][sub] = false
// }
// w.Add(sub)
// }
// }
//}