-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
stringfilter.go
114 lines (98 loc) · 2.67 KB
/
stringfilter.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
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* 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
*
* http://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 worker
import (
"strings"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)
type matchFn func(types.Val, stringFilter) bool
type stringFilter struct {
funcName string
funcType FuncType
lang string
tokens []string
match matchFn
ineqValue types.Val
eqVals []types.Val
}
func matchStrings(uids *pb.List, values [][]types.Val, filter stringFilter) *pb.List {
rv := &pb.List{}
for i := 0; i < len(values); i++ {
for j := 0; j < len(values[i]); j++ {
if filter.match(values[i][j], filter) {
rv.Uids = append(rv.Uids, uids.Uids[i])
break
}
}
}
return rv
}
func defaultMatch(value types.Val, filter stringFilter) bool {
tokenMap := map[string]bool{}
for _, t := range filter.tokens {
tokenMap[t] = false
}
tokens := tokenizeValue(value, filter)
cnt := 0
for _, token := range tokens {
previous, ok := tokenMap[token]
if ok {
tokenMap[token] = true
if previous == false { // count only once
cnt++
}
}
}
all := strings.HasPrefix(filter.funcName, "allof") // anyofterms or anyoftext
if all {
return cnt == len(filter.tokens)
} else {
return cnt > 0
}
}
func ineqMatch(value types.Val, filter stringFilter) bool {
if len(filter.eqVals) == 0 {
return types.CompareVals(filter.funcName, value, filter.ineqValue)
}
for _, v := range filter.eqVals {
if types.CompareVals(filter.funcName, value, v) {
return true
}
}
return false
}
func tokenizeValue(value types.Val, filter stringFilter) []string {
var tokName string
switch filter.funcType {
case StandardFn:
tokName = "term"
case FullTextSearchFn:
tokName = "fulltext"
}
tokenizer, found := tok.GetTokenizer(tokName)
// tokenizer was used in previous stages of query proccessing, it has to be available
x.AssertTrue(found)
tokens, err := tok.BuildTokens(value.Value, tokenizer.SetLang(filter.lang))
if err != nil {
glog.V(3).Infof("error while building tokens: %s", err)
return []string{}
}
return tokens
}