-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
344 lines (283 loc) · 6.64 KB
/
storage.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
//storage-------------------------------------------------------------
type storage struct {
Tries []*trie
to_update []bool
rewrite bool
signal_close chan bool
}
func new_storage() *storage {
if g_st != nil {
g_st.close()
}
tries := make([]*trie, 1, 3)
tries[0] = new_trie(filepath.Join(runtime.GOROOT(), "src/pkg"), goversion())
for _, v := range gopaths {
tries = append(tries, new_trie(v, "time"))
}
to_update := make([]bool, len(gopaths))
signal_close := make(chan bool)
st := &storage{Tries: tries, to_update: to_update, signal_close: signal_close}
//st.rewrite = g_st.rewrite
st.write()
st.query(c_main_query)
return st
}
func read_from_file(dir string) (*storage, error) {
if !is_exist(dir) {
st := new_storage()
st.rewrite = true
return st, nil
}
var this = &storage{}
b, err := ioutil.ReadFile(dir)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, this)
if err != nil {
return nil, err
}
err = this.convert_read()
if err != nil {
return nil, err
}
this.to_update = make([]bool, len(gopaths))
this.signal_close = make(chan bool)
return this, nil
}
func (this *storage) same_version() bool {
if this == nil {
panic("the storage is nil")
}
if this.Tries[0].Info == goversion() {
return true
}
return false
}
func (this *storage) convert_write() error {
if this == nil {
return errors.New("the storage is nil")
}
m := get_code()
for _, v := range this.Tries {
if _, err := strconv.Atoi(v.Root.Name); err == nil {
continue
}
sub, ok := m[v.Root.Name]
if ok {
v.Root.Name = sub
}
}
return nil
}
func (this *storage) convert_read() error {
if this == nil {
return errors.New("the storage is nil")
}
m := get_code()
for _, v := range this.Tries {
if _, err := strconv.Atoi(v.Root.Name); err != nil {
continue
}
sub, ok := m[v.Root.Name]
if ok {
v.Root.Name = sub
}
}
return nil
}
//add function
func (this *storage) recomand(query string) []*pointer {
var arr = make([]*pointer, 0, 4)
for _, v := range this.Tries {
v.similar(query, &arr)
}
if len(arr) == 0 {
return nil
}
sort.Sort(ps(arr))
return arr
}
//io
func (this *storage) write() {
if this == nil {
panic("the storage is nil")
}
go func() {
var filedir string = config_file()
for {
select {
case <-c_storage_rewrite:
//wg.Add(1)
for k, v := range this.to_update {
if v {
this.rewrite = true
this.Tries[k+1] = new_trie(gopaths[k], "time")
}
}
if !this.rewrite {
g_wg.Done()
continue
}
if !is_exist(filedir) {
os.MkdirAll(filepath.Dir(filedir), os.ModePerm)
}
file, err := os.Create(filedir)
if err != nil {
panic(err.Error())
}
err = this.convert_write()
if err != nil {
panic(err.Error())
}
b, err := json.Marshal(this)
if err != nil {
panic(err.Error())
}
_, err = file.Write(b)
if err != nil {
panic(err.Error())
}
log.Println("rewrite the config file")
g_wg.Done()
case <-this.signal_close:
return
}
}
}()
}
//new
func (this *storage) query(c_main_query chan string) {
if this == nil {
panic("the storage is nil")
}
c_pkg_gofile := make(chan string, g_numCPU*2+2)
//保证不会死锁,下面详细说明
a := make(chan bool)
//b的作用是,1 goroutine 处理完了,通知 2
b := make(chan bool)
var send_gofiles int32
var folds, tackled, folds_final int32 = 0, 0, -1
for i := 0; i < g_numCPU; i++ {
//1st goroutine
go func() {
var father string
var index int = -1
var last_update time.Time
var err error
for {
select {
case dir, ok := <-c_pkg_gofile:
if !ok {
break
}
if strings.HasPrefix(dir, father) || father == "" {
for k, v := range gopaths {
if strings.HasPrefix(dir, v) {
father = v
index = k
last_update, err = time.Parse("2006-01-02 15:04:05 -0700", this.Tries[k+1].Info)
if err != nil {
log.Println("the config file's time cannot parse,we will reconstruct it again.")
this.to_update[index] = true
//todo
os.Exit(2)
}
}
}
}
fis, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
var gofiles int32
for _, fi := range fis {
if fi.IsDir() {
continue
}
if index != -1 && !this.to_update[index] {
mod := fi.ModTime()
if mod.After(last_update) {
this.to_update[index] = true
}
}
if filter_gofile(fi) {
c_query_reader <- filepath.Join(dir, fi.Name())
gofiles++
}
}
atomic.AddInt32(&send_gofiles, gofiles)
atomic.AddInt32(&tackled, 1)
//2种情况
//
//1.路径先发完,folds_final转变。2.文件已处理完,路径还没找完(最后不符合的文件.js .html多)
//针对1情况基本不会有问题,最后一个路径处理好,那么两者的数字会相等
//第2种情况,文件处理好,folds_final还没有转变,就死锁,那么a可以保持在folds_final转变后
//再询问一次两者是否相等,如果相等那就返回
case <-a:
//log.Println("tackled", tackled)
//log.Println("folds_final", folds_final)
case <-this.signal_close:
return
}
if tackled == folds_final {
atomic.StoreInt32(&tackled, 0)
atomic.StoreInt32(&folds, 0)
atomic.StoreInt32(&folds_final, -1)
b <- true
}
}
}()
}
//2nd goroutine
go func() {
w := &sync.WaitGroup{}
for {
select {
case pkg, ok := <-c_main_query:
if !ok {
break
}
w.Add(len(this.Tries))
for _, v := range this.Tries {
v.query(pkg, &folds, w, c_pkg_gofile)
}
w.Wait()
atomic.StoreInt32(&folds_final, folds)
a <- true
//wait for 1 goroutine
<-b
//if did not send any gofile,goto the main goroutine immediately
if send_gofiles == 0 {
c_scan_main <- 0
} else {
//test
//log.Println("send gofiles:", send_gofiles)
f_query_reader <- send_gofiles
atomic.StoreInt32(&send_gofiles, 0)
}
case <-this.signal_close:
return
}
}
}()
}
func (this *storage) close() {
close(this.signal_close)
}