forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
185 lines (160 loc) · 3.96 KB
/
search.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
/*
* Copyright (C) 2013 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"os"
"path"
dutils "pkg.deepin.io/lib/utils"
"regexp"
)
type dataInfo struct {
Key string
Value string
}
type matchResInfo struct {
highestList []string
excellentList []string
veryGoodList []string
goodList []string
aboveAvgList []string
avgList []string
belowAvgList []string
poorList []string
}
func getCachePath() (string, bool) {
userCache := dutils.GetCacheDir()
if len(userCache) < 1 {
return "", false
}
cachePath := path.Join(userCache, "deepin", "dde-daemon", "search")
if !dutils.IsFileExist(cachePath) {
if err := os.MkdirAll(cachePath, 0755); err != nil {
logger.Warningf("MkdirAll '%s' failed: %v",
cachePath, err)
return "", false
}
}
return cachePath, true
}
func sortMatchResult(res *matchResInfo) []string {
ret := []string{}
if res == nil {
return ret
}
ret = append(ret, res.highestList...)
ret = append(ret, res.excellentList...)
ret = append(ret, res.veryGoodList...)
ret = append(ret, res.goodList...)
ret = append(ret, res.aboveAvgList...)
ret = append(ret, res.avgList...)
ret = append(ret, res.belowAvgList...)
ret = append(ret, res.poorList...)
return ret
}
func getMatchReuslt(info *dataInfo, matchers map[*regexp.Regexp]uint32,
res *matchResInfo) {
if info == nil || matchers == nil || res == nil {
return
}
curScore := uint32(0)
for matcher, score := range matchers {
if matcher == nil {
continue
}
if matcher.MatchString(info.Key) {
logger.Debugf("score %d, target str: %q", score, info.Key)
if score > curScore {
curScore = score
}
}
}
switch curScore {
case POOR:
res.poorList = append(res.poorList,
info.Value)
case BELOW_AVERAGE:
res.belowAvgList = append(res.belowAvgList,
info.Value)
case AVERAGE:
res.avgList = append(res.avgList,
info.Value)
case ABOVE_AVERAGE:
res.aboveAvgList = append(res.aboveAvgList,
info.Value)
case GOOD:
res.goodList = append(res.goodList,
info.Value)
case VERY_GOOD:
res.veryGoodList = append(res.veryGoodList,
info.Value)
case EXCELLENT:
res.excellentList = append(res.excellentList,
info.Value)
case HIGHEST:
res.highestList = append(res.highestList,
info.Value)
}
}
func searchString(key, md5 string) []string {
list := []string{}
cachePath, ok := getCachePath()
if !ok {
return list
}
filename := path.Join(cachePath, md5)
if !dutils.IsFileExist(filename) {
logger.Warningf("'%s' not exist", filename)
return list
}
datas := []dataInfo{}
if !readDatasFromFile(&datas, filename) {
return list
}
matchers := getMatchers(key)
var matchRes = matchResInfo{}
for _, v := range datas {
getMatchReuslt(&v, matchers, &matchRes)
}
list = sortMatchResult(&matchRes)
return list
}
func searchStartWithString(key, md5 string) []string {
list := []string{}
cachePath, ok := getCachePath()
if !ok {
return list
}
filename := path.Join(cachePath, md5)
if !dutils.IsFileExist(filename) {
logger.Warningf("'%s' not exist", filename)
return list
}
datas := []dataInfo{}
if !readDatasFromFile(&datas, filename) {
return list
}
match := regexp.MustCompile(fmt.Sprintf(`(?i)^(%s)`, key))
for _, v := range datas {
if match.MatchString(v.Key) {
list = append(list, v.Value)
}
}
return list
}