forked from dgraph-io/dgraph
/
stringfilter.go
117 lines (100 loc) · 2.77 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
115
116
117
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package worker
import (
"strings"
"gopkg.in/adibiarsotp/dgraph.v82/protos"
"gopkg.in/adibiarsotp/dgraph.v82/tok"
"gopkg.in/adibiarsotp/dgraph.v82/types"
"gopkg.in/adibiarsotp/dgraph.v82/x"
)
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 *protos.List, values [][]types.Val, filter stringFilter) *protos.List {
rv := &protos.List{}
for i := 0; i < len(values); i++ {
for j := 0; j < len(values[i]); j++ {
if len(values[i][j].Value.(string)) == 0 {
continue
}
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 = tok.FtsTokenizerName(filter.lang)
}
tokenizer, found := tok.GetTokenizer(tokName)
// tokenizer was used in previous stages of query proccessing, it has to be available
x.AssertTrue(found)
tokens, err := tokenizer.Tokens(value)
if err == nil {
return tokens
}
return []string{}
}