-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce_find.go
67 lines (57 loc) · 1.63 KB
/
force_find.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
package main
import (
"strings"
"sync"
)
const threshold = 1000
type ForceFind struct {
wg sync.WaitGroup
}
func (f *ForceFind) Match(entries []*Entry, text string) []*Entry {
patterns := strings.Split(text, "&")
if len(patterns) == 0 {
return make([]*Entry, 0)
}
if len(entries) < threshold {
return f.match(entries, patterns)
}
unitLen := len(entries) / 4
result := make([]*Entry, 0, len(entries))
locker := sync.Mutex{}
matchFunc := func(startIndex int, endIndex int) {
matchResult := f.match(entries[startIndex:endIndex], patterns)
locker.Lock()
result = append(result, matchResult...)
locker.Unlock()
f.wg.Done()
}
f.wg.Add(4)
go matchFunc(0, unitLen * 1)
go matchFunc(unitLen * 1, unitLen * 2)
go matchFunc(unitLen * 2, unitLen * 3)
go matchFunc(unitLen * 3, len(entries))
f.wg.Wait()
return result
}
func (f *ForceFind) match(entries []*Entry, patterns []string) []*Entry {
result := make([]*Entry, 0, len(entries))
// 先筛选出第一个pattern的全部数据
pattern := strings.TrimSpace(patterns[0])
for _, entry := range entries {
if len(pattern) == 0 || strings.Contains(entry.command.String(), pattern) || strings.Contains(entry.explain.String(), pattern) {
result = append(result, entry)
}
}
// 再根据剩余的pattern,进行筛选
for _, pattern := range patterns[1:] {
pattern = strings.TrimSpace(pattern)
tmp := make([]*Entry, 0, len(result))
for _, entry := range result {
if len(pattern) == 0 || strings.Contains(entry.command.String(), pattern) || strings.Contains(entry.explain.String(), pattern) {
tmp = append(tmp, entry)
}
}
result = tmp
}
return result
}