-
Notifications
You must be signed in to change notification settings - Fork 1
/
webNodeList.go
165 lines (150 loc) · 3.32 KB
/
webNodeList.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"sort"
"sync"
"unicode"
)
func CreateWebNodeList() (result WebNodeList) {
result.list = make([]WebNode, 0)
return
}
type WebNodeList struct {
mux sync.Mutex
list []WebNode
pendingCount int
busyCount int
doneCount int
failCount int
busyPointer int
}
func compareStrings(lhs, rhs string) (validOrder bool) {
// Create left and right rune slices
lrs, rrs := []rune(lhs), []rune(rhs)
for i := 0; i < len(lrs) && i < len(lrs); i++ {
if lhs != rhs {
lhsLower := unicode.ToLower(lrs[i])
rhsLower := unicode.ToLower(rrs[i])
if lhsLower == rhsLower {
return lrs[i] < rrs[i]
} else {
return lhsLower < rhsLower
}
}
}
return len(lhs) < len(rhs)
}
func compareWebNodes(lhs, rhs *WebNode) (validOrder bool) {
switch {
case lhs.nodeType == rhs.nodeType:
return compareStrings(lhs.name, rhs.name)
case lhs.nodeType == file:
return true
default:
return false
}
}
func (l *WebNodeList) insertAtIndex(n []WebNode, index int) {
insertLen := len(n)
l.list = append(l.list, make([]WebNode, insertLen)...)
copy(l.list[index+insertLen:], l.list[index:])
for i := 0; i < insertLen; i++ {
l.list[index+i] = n[i]
}
}
func (l *WebNodeList) InsertSorted(nodes []WebNode, parentPath string, sortNodes bool) {
if len(nodes) == 0 {
return
}
l.mux.Lock()
defer l.mux.Unlock()
if sortNodes {
sort.Slice(nodes, func(i, j int) bool { return compareWebNodes(&nodes[i], &nodes[j]) })
}
for i := 0; i < len(nodes); i++ {
if nodes[i].nodeStatus == pending {
l.pendingCount++
}
}
for l.busyPointer < len(l.list) && l.list[l.busyPointer].nodeStatus == done {
l.busyPointer++
}
for i := l.busyPointer; i < len(l.list); i++ {
if l.list[i].nodeStatus == busy && l.list[i].path == parentPath {
for k := range nodes {
nodes[k].nodeDepth = l.list[i].nodeDepth + 1
}
nodes[len(nodes)-1].nodeLastSibling = true
l.insertAtIndex(nodes, i+1)
return
}
}
l.list = append(l.list, nodes...)
}
func (l *WebNodeList) IsDone() bool {
l.mux.Lock()
defer l.mux.Unlock()
return l.pendingCount == 0 && l.busyCount == 0
}
func (l *WebNodeList) GetPending() (result WebNode, wait bool) {
l.mux.Lock()
defer l.mux.Unlock()
if l.pendingCount == 0 {
wait = true
return
}
for i := range l.list {
if l.list[i].nodeStatus == pending {
l.setStatusByIndex(i, busy)
result = l.list[i]
return
}
}
wait = true
return
}
func (l *WebNodeList) SetFailed(path string) {
l.mux.Lock()
defer l.mux.Unlock()
for i := range l.list {
if l.list[i].path == path {
if !l.list[i].nodeFail {
l.failCount++
l.list[i].nodeFail = true
}
return
}
}
}
func (l *WebNodeList) SetStatus(path string, status NodeStatus) {
l.mux.Lock()
defer l.mux.Unlock()
for i := range l.list {
if l.list[i].path == path {
l.setStatusByIndex(i, status)
return
}
}
}
func (l *WebNodeList) setStatusByIndex(index int, status NodeStatus) {
switch l.list[index].nodeStatus {
case pending:
l.pendingCount--
case busy:
l.busyCount--
case done:
l.doneCount--
}
switch status {
case pending:
l.pendingCount++
case busy:
l.busyCount++
case done:
l.doneCount++
}
l.list[index].nodeStatus = status
return
}
func (l *WebNodeList) GetStats() (done, fail, total int) {
return l.doneCount + l.failCount, l.failCount, l.doneCount + l.pendingCount + l.busyCount + l.failCount
}