/
query.go
227 lines (212 loc) · 5 KB
/
query.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
package index
import (
"log"
"sort"
)
func (i *Index) PostingQuery(q *Query) (docids []uint32) {
return i.postingQuery(q, nil)
}
// Implements sort.Interface
type trigramCnt struct {
trigram uint32
count int
listcnt int
}
type trigramCnts []trigramCnt
func (t trigramCnts) Len() int {
return len(t)
}
func (t trigramCnts) Less(i, j int) bool {
return t[i].count < t[j].count
}
func (t trigramCnts) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (ix *Index) postingQuery(qry *Query, restrict []uint32) (ret []uint32) {
var list []uint32
switch qry.Op {
case QNone:
// nothing
case QAll:
if restrict != nil {
return restrict
}
list = make([]uint32, ix.DocidMap.Count)
for i := range list {
list[i] = uint32(i)
}
return list
case QAnd:
// "Query planner": we first sort the posting lists by their
// length (ascending)
withCount := make(trigramCnts, 0, len(qry.Trigram))
for _, t := range qry.Trigram {
tri := uint32(t[0])<<16 | uint32(t[1])<<8 | uint32(t[2])
meta, _, err := ix.Docid.metaEntry(Trigram(tri))
if err != nil {
log.Printf("numEntries: (probably okay) tri %d (%v): %v", tri, t, err)
withCount = append(withCount, trigramCnt{tri, 0, 0})
continue
}
//withCount[idx] = trigramCnt{tri, int(count), 0}
withCount = append(withCount, trigramCnt{tri, int(meta.Entries), 0})
}
sort.Sort(withCount)
if len(withCount) > 0 {
//q.pfdocid.growBuffer(withCount[len(withCount)-1].count)
}
stoppedAt := 0
for idx, t := range withCount {
previous := len(list)
if list == nil {
list = ix.postingList(t.trigram, restrict)
} else {
list = ix.postingAnd(list, t.trigram, restrict)
}
if len(list) == 0 {
return nil
}
//continue
withCount[idx].listcnt = len(list)
if previous > 0 {
minIdx := 0.70 * float32(len(withCount))
if (previous-len(list)) < 10 && stoppedAt == 0 && float32(idx) > minIdx {
stoppedAt = len(list)
}
}
if previous > 0 && (previous-len(list)) < 10 {
//fmt.Printf("difference is %d, break!\n", previous - len(list))
break
}
}
for _, sub := range qry.Sub {
if list == nil {
list = restrict
}
list = ix.postingQuery(sub, list)
if len(list) == 0 {
return nil
}
}
case QOr:
for _, t := range qry.Trigram {
tri := uint32(t[0])<<16 | uint32(t[1])<<8 | uint32(t[2])
if list == nil {
list = ix.postingList(tri, restrict)
} else {
list = ix.postingOr(list, tri, restrict)
}
}
for _, sub := range qry.Sub {
list1 := ix.postingQuery(sub, restrict)
list = mergeOr(list, list1)
}
}
return list
}
func mergeOr(l1, l2 []uint32) []uint32 {
var l []uint32
i := 0
j := 0
for i < len(l1) || j < len(l2) {
switch {
case j == len(l2) || (i < len(l1) && l1[i] < l2[j]):
l = append(l, l1[i])
i++
case i == len(l1) || (j < len(l2) && l1[i] > l2[j]):
l = append(l, l2[j])
j++
case l1[i] == l2[j]:
l = append(l, l1[i])
i++
j++
}
}
return l
}
func (ix *Index) readDocids(t Trigram, restrict []uint32) ([]uint32, error) {
docids, err := ix.Docid.Deltas(t)
if err != nil {
return nil, err
}
var prev uint32
entries := make([]uint32, len(docids))
//var entries []uint32
if len(docids) == 0 {
return nil, nil
}
var oidx int
for _, docid := range docids {
docid += prev
prev = docid
if restrict != nil {
i := 0
for i < len(restrict) && restrict[i] < docid {
i++
}
restrict = restrict[i:]
if len(restrict) == 0 || restrict[0] != docid {
continue
}
}
entries[oidx] = docid
oidx++
}
return entries[:oidx], nil
}
func (ix *Index) postingList(tri uint32, restrict []uint32) []uint32 {
x, err := ix.readDocids(Trigram(tri), restrict)
if err != nil {
log.Printf("(probably okay?) tri %d: %v", tri, err)
}
//log.Printf("len(postingList(%d, restrict %d)) = %d", tri, len(restrict), len(x))
return x
}
func (ix *Index) postingAnd(list []uint32, tri uint32, restrict []uint32) []uint32 {
l, err := ix.readDocids(Trigram(tri), restrict)
if err != nil {
log.Printf("(probably okay?) tri %d: %v", tri, err)
}
x := make([]uint32, 0, len(l))
i := 0
for _, fileid := range l {
for i < len(list) && list[i] < fileid {
i++
}
if i < len(list) && list[i] == fileid {
x = append(x, fileid)
i++
}
}
//log.Printf("len(postingAnd(%d, restrict %d)) = %d", tri, len(restrict), len(x))
return x
}
func (ix *Index) postingOr(list []uint32, tri uint32, restrict []uint32) []uint32 {
l, err := ix.readDocids(Trigram(tri), restrict)
if err != nil {
log.Printf("(probably okay?) tri %d: %v", tri, err)
}
//x := make([]uint32, 0, len(l))
x := make([]uint32, len(l)+len(list))
i := 0
xn := 0
for _, fileid := range l {
for i < len(list) && list[i] < fileid {
x[xn] = list[i]
xn++
//x = append(x, list[i])
i++
}
x[xn] = fileid
xn++
//x = append(x, fileid)
if i < len(list) && list[i] == fileid {
i++
}
}
copy(x[xn:], list[i:])
xn += len(list[i:])
//x = append(x, list[i:]...)
//log.Printf("len(postingOr(%d, retrict %d)) = %d", tri, len(restrict), len(x))
return x[:xn]
}