-
Notifications
You must be signed in to change notification settings - Fork 32
/
statvar_search.go
315 lines (295 loc) · 9.29 KB
/
statvar_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
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package statvar
import (
"context"
"sort"
"strings"
pb "github.com/datacommonsorg/mixer/internal/proto"
"github.com/datacommonsorg/mixer/internal/server/resource"
"github.com/datacommonsorg/mixer/internal/store"
)
const (
maxFilteredIds = 3000 // Twice of maxResult to give buffer for place filter.
maxResult = 1000
)
// SearchStatVar implements API for Mixer.SearchStatVar.
func SearchStatVar(
ctx context.Context,
in *pb.SearchStatVarRequest,
store *store.Store,
cache *resource.Cache,
) (
*pb.SearchStatVarResponse, error,
) {
query := in.GetQuery()
places := in.GetPlaces()
svOnly := in.GetSvOnly()
result := &pb.SearchStatVarResponse{
StatVars: []*pb.EntityInfo{},
StatVarGroups: []*pb.SearchResultSVG{},
Matches: []string{},
}
if query == "" {
return result, nil
}
tokens := strings.Fields(
strings.Replace(strings.ToLower(query), ",", " ", -1))
searchIndex := cache.SvgSearchIndex
svList, svgList, matches := searchTokens(tokens, searchIndex, svOnly)
// Filter the stat var and stat var group by places.
if len(places) > 0 {
// Read from stat existence cache, which can take several seconds when
// there are a lot of ids. So pre-prune the ids, as the result will be
// filtered anyway.
ids := []string{}
if len(svList) > maxFilteredIds {
svList = svList[0:maxFilteredIds]
}
if len(svgList) > maxFilteredIds {
svgList = svgList[0:maxFilteredIds]
}
for _, item := range append(svList, svgList...) {
ids = append(ids, item.Dcid)
}
statVarCount, err := Count(ctx, store, ids, places)
if err != nil {
return nil, err
}
svList = filter(svList, statVarCount, len(places))
svgList = filter(svgList, statVarCount, len(places))
}
svResult, svgResult := groupStatVars(svList, svgList, cache.ParentSvg, cache.SvgSearchIndex.Ranking)
// TODO(shifucun): return the total number of result for client to consume.
if len(svResult) > maxResult {
svResult = svResult[0:maxResult]
}
if len(svgResult) > maxResult {
svgResult = svgResult[0:maxResult]
}
result.StatVars = svResult
result.StatVarGroups = svgResult
result.Matches = matches
return result, nil
}
func filter(
nodes []*pb.EntityInfo,
countMap map[string]map[string]int32,
numPlaces int) []*pb.EntityInfo {
result := []*pb.EntityInfo{}
for _, node := range nodes {
if existence, ok := countMap[node.Dcid]; ok && len(existence) > 0 {
result = append(result, node)
}
}
return result
}
// Return whether r1 should be ranked ahead of r2
func compareRankingInfo(
r1 *resource.RankingInfo,
dcid1 string,
r2 *resource.RankingInfo,
dcid2 string,
) bool {
if r1.ApproxNumPv != r2.ApproxNumPv {
return r1.ApproxNumPv < r2.ApproxNumPv
}
if r1.NumKnownPv != r2.NumKnownPv {
return r1.NumKnownPv < r2.NumKnownPv
}
if r1.RankingName != r2.RankingName {
return r1.RankingName < r2.RankingName
}
return dcid1 < dcid2
}
func groupStatVars(
svList []*pb.EntityInfo,
svgList []*pb.EntityInfo,
parentMap map[string][]string,
ranking map[string]*resource.RankingInfo,
) ([]*pb.EntityInfo, []*pb.SearchResultSVG) {
// Create a map of svg id to svg search result
svgMap := map[string]*pb.SearchResultSVG{}
for _, svg := range svgList {
svgMap[svg.Dcid] = &pb.SearchResultSVG{
Dcid: svg.Dcid,
Name: svg.Name,
StatVars: []*pb.EntityInfo{},
}
}
resultSv := []*pb.EntityInfo{}
// Iterate through the list of stat vars. If a stat var has a parent svg that
// is a result, add the stat var to that svg result. If not, add the stat var
// to the result list of stat vars
for _, sv := range svList {
isGrouped := false
if parents, ok := parentMap[sv.Dcid]; ok {
// filter parents list to only those that are results
possibleParents := []string{}
for _, parent := range parents {
if _, ok := svgMap[parent]; ok {
possibleParents = append(possibleParents, parent)
}
}
// sort parents by their ranking info so that stat var will be added to
// highest ranked parent
sort.SliceStable(possibleParents, func(i, j int) bool {
ri := ranking[possibleParents[i]]
rj := ranking[possibleParents[j]]
return compareRankingInfo(ri, possibleParents[i], rj, possibleParents[j])
})
// add stat var to the svg result of the highest ranked parent
if len(possibleParents) > 0 {
svgMap[possibleParents[0]].StatVars = append(svgMap[possibleParents[0]].StatVars, sv)
isGrouped = true
}
}
if !isGrouped {
resultSv = append(resultSv, sv)
}
}
// Convert the svgMap to a list of svg results
resultSvg := []*pb.SearchResultSVG{}
for _, svg := range svgMap {
resultSvg = append(resultSvg, svg)
}
// Sort the stat var group results
sort.SliceStable(resultSvg, func(i, j int) bool {
svgi := resultSvg[i]
svgj := resultSvg[j]
if len(svgi.StatVars) != len(svgj.StatVars) {
return len(svgi.StatVars) > len(svgj.StatVars)
}
ri := ranking[resultSvg[i].Dcid]
rj := ranking[resultSvg[j].Dcid]
return compareRankingInfo(ri, resultSvg[i].Dcid, rj, resultSvg[j].Dcid)
})
return resultSv, resultSvg
}
// TokenToMatches is a map of token to a set of strings that match the token
type TokenToMatches map[string]map[string]struct{}
func searchTokens(
tokens []string, index *resource.SearchIndex, svOnly bool,
) ([]*pb.EntityInfo, []*pb.EntityInfo, []string) {
// svMatches and svgMatches are maps of sv/svg id to TokenToMatches of tokens
// that match each sv/svg
svMatches := map[string]TokenToMatches{}
svgMatches := map[string]TokenToMatches{}
// Get all matching sv and svg from the trie for each token
for _, token := range tokens {
currNode := index.RootTrieNode
// Traverse the Trie following the order of the characters in the token
// until we either reach the end of the token or we reach a node that
// doesn't have the next character as a child node.
// eg. Trie: Root - a
// / \
// b c
//
// If token is "ab", currNode will go Root -> a -> b
// If token is "bc", currNode will go Root -> nil
// If token is "abc", currNode will go Root -> a -> b -> nil
for _, c := range token {
if _, ok := currNode.ChildrenNodes[c]; !ok {
currNode = nil
break
}
currNode = currNode.ChildrenNodes[c]
}
// The token is not a prefix or word in the Trie.
if currNode == nil {
continue
}
// Traverse the entire subTrie rooted at the node corresponding to the
// last character in the token and add all SvIds and SvgIds seen.
nodesToCheck := []resource.TrieNode{*currNode}
for len(nodesToCheck) > 0 {
node := nodesToCheck[0]
nodesToCheck = nodesToCheck[1:]
for _, node := range node.ChildrenNodes {
nodesToCheck = append(nodesToCheck, *node)
}
for sv := range node.SvIds {
if _, ok := svMatches[sv]; !ok {
svMatches[sv] = TokenToMatches{}
}
svMatches[sv][token] = node.Matches
}
if svOnly {
continue
}
for svg := range node.SvgIds {
if _, ok := svgMatches[svg]; !ok {
svgMatches[svg] = TokenToMatches{}
}
svgMatches[svg][token] = node.Matches
}
}
}
// matchingStrings is a set where matches will be keys and those keys will be
// mapped to an empty struct
matchingStrings := map[string]struct{}{}
exists := struct{}{}
// Only select sv and svg that matches all the tokens
svList := []*pb.EntityInfo{}
for sv, tokenMatches := range svMatches {
if len(tokenMatches) == len(tokens) {
svList = append(svList, &pb.EntityInfo{
Dcid: sv,
Name: index.Ranking[sv].RankingName,
})
for _, matchList := range tokenMatches {
for match := range matchList {
matchingStrings[match] = exists
}
}
}
}
// Sort stat vars by number of PV; If two stat vars have same number of PV,
// then order by the stat var (group) name.
sort.SliceStable(svList, func(i, j int) bool {
ranking := index.Ranking
ri := ranking[svList[i].Dcid]
rj := ranking[svList[j].Dcid]
return compareRankingInfo(ri, svList[i].Dcid, rj, svList[j].Dcid)
})
// Stat Var Groups will be sorted later.
svgList := []*pb.EntityInfo{}
for svg, tokenMatches := range svgMatches {
if len(tokenMatches) == len(tokens) {
svgList = append(svgList, &pb.EntityInfo{
Dcid: svg,
Name: index.Ranking[svg].RankingName,
})
for _, matchList := range tokenMatches {
for match := range matchList {
matchingStrings[match] = exists
}
}
}
}
sort.SliceStable(svgList, func(i, j int) bool {
ranking := index.Ranking
ri := ranking[svgList[i].Dcid]
rj := ranking[svgList[j].Dcid]
return compareRankingInfo(ri, svgList[i].Dcid, rj, svgList[j].Dcid)
})
matchingStringsList := []string{}
for match := range matchingStrings {
matchingStringsList = append(matchingStringsList, match)
}
sort.SliceStable(matchingStringsList, func(i, j int) bool {
return matchingStringsList[i] < matchingStringsList[j]
})
return svList, svgList, matchingStringsList
}