forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchtree.go
743 lines (643 loc) · 16.3 KB
/
matchtree.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright 2018 Google Inc. All rights reserved.
//
// 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 zoekt
import (
"fmt"
"log"
"regexp"
"strings"
"unicode/utf8"
"github.com/google/zoekt/query"
)
// A docIterator iterates over documents in order.
type docIterator interface {
// provide the next document where we can may find something
// interesting.
nextDoc() uint32
// clears any per-document state of the docIterator, and
// prepares for evaluating the given doc. The argument is
// strictly increasing over time.
prepare(nextDoc uint32)
}
const (
costConst = 0
costMemory = 1
costContent = 2
costRegexp = 3
)
const (
costMin = costConst
costMax = costRegexp
)
// An expression tree coupled with matches. The matchtree has two
// functions:
//
// * it implements boolean combinations (and, or, not)
//
// * it implements shortcuts, where we skip documents (for example: if
// there are no trigram matches, we can be sure there are no substring
// matches). The matchtree iterates over the documents as they are
// ordered in the shard.
//
// The general process for a given (shard, query) is
//
// - construct matchTree for the query
//
// - find all different leaf matchTrees (substring, regexp, etc.)
//
// in a loop:
//
// - find next doc to process using nextDoc
//
// - evaluate atoms (leaf expressions that match text)
//
// - evaluate the tree using matches(), storing the result in map.
//
// - if the complete tree returns (matches() == true) for the document,
// collect all text matches by looking at leaf matchTrees
//
type matchTree interface {
docIterator
// returns whether this matches, and if we are sure.
matches(cp *contentProvider, cost int, known map[matchTree]bool) (match bool, sure bool)
}
// docMatchTree iterates over documents for which predicate(docID) returns true.
type docMatchTree struct {
// the number of documents in a shard.
numDocs uint32
predicate func(docID uint32) bool
// provides additional information about the reason why the docMatchTree was
// created.
reason string
// mutable
firstDone bool
docID uint32
}
type bruteForceMatchTree struct {
// mutable
firstDone bool
docID uint32
}
type andLineMatchTree struct {
andMatchTree
}
type andMatchTree struct {
children []matchTree
}
type orMatchTree struct {
children []matchTree
}
type notMatchTree struct {
child matchTree
}
// Don't visit this subtree for collecting matches.
type noVisitMatchTree struct {
matchTree
}
type regexpMatchTree struct {
regexp *regexp.Regexp
fileName bool
// mutable
reEvaluated bool
found []*candidateMatch
// nextDoc, prepare.
bruteForceMatchTree
}
type substrMatchTree struct {
matchIterator
query *query.Substring
caseSensitive bool
fileName bool
// mutable
current []*candidateMatch
contEvaluated bool
}
type branchQueryMatchTree struct {
fileMasks []uint64
mask uint64
// mutable
firstDone bool
docID uint32
}
// all prepare methods
func (t *bruteForceMatchTree) prepare(doc uint32) {
t.docID = doc
t.firstDone = true
}
func (t *docMatchTree) prepare(doc uint32) {
t.docID = doc
t.firstDone = true
}
func (t *andMatchTree) prepare(doc uint32) {
for _, c := range t.children {
c.prepare(doc)
}
}
func (t *regexpMatchTree) prepare(doc uint32) {
t.found = t.found[:0]
t.reEvaluated = false
t.bruteForceMatchTree.prepare(doc)
}
func (t *orMatchTree) prepare(doc uint32) {
for _, c := range t.children {
c.prepare(doc)
}
}
func (t *notMatchTree) prepare(doc uint32) {
t.child.prepare(doc)
}
func (t *substrMatchTree) prepare(nextDoc uint32) {
t.matchIterator.prepare(nextDoc)
t.current = t.matchIterator.candidates()
t.contEvaluated = false
}
func (t *branchQueryMatchTree) prepare(doc uint32) {
t.firstDone = true
t.docID = doc
}
// nextDoc
func (t *docMatchTree) nextDoc() uint32 {
var start uint32
if t.firstDone {
start = t.docID + 1
}
for i := start; i < t.numDocs; i++ {
if t.predicate(i) {
return i
}
}
return maxUInt32
}
func (t *bruteForceMatchTree) nextDoc() uint32 {
if !t.firstDone {
return 0
}
return t.docID + 1
}
func (t *andMatchTree) nextDoc() uint32 {
var max uint32
for _, c := range t.children {
m := c.nextDoc()
if m > max {
max = m
}
}
return max
}
func (t *orMatchTree) nextDoc() uint32 {
min := uint32(maxUInt32)
for _, c := range t.children {
m := c.nextDoc()
if m < min {
min = m
}
}
return min
}
func (t *notMatchTree) nextDoc() uint32 {
return 0
}
func (t *branchQueryMatchTree) nextDoc() uint32 {
var start uint32
if t.firstDone {
start = t.docID + 1
}
for i := start; i < uint32(len(t.fileMasks)); i++ {
if (t.mask & t.fileMasks[i]) != 0 {
return i
}
}
return maxUInt32
}
// all String methods
func (t *bruteForceMatchTree) String() string {
return "all"
}
func (t *docMatchTree) String() string {
return fmt.Sprintf("doc(%s)", t.reason)
}
func (t *andMatchTree) String() string {
return fmt.Sprintf("and%v", t.children)
}
func (t *regexpMatchTree) String() string {
return fmt.Sprintf("re(%s)", t.regexp)
}
func (t *orMatchTree) String() string {
return fmt.Sprintf("or%v", t.children)
}
func (t *notMatchTree) String() string {
return fmt.Sprintf("not(%v)", t.child)
}
func (t *substrMatchTree) String() string {
f := ""
if t.fileName {
f = "f"
}
return fmt.Sprintf("%ssubstr(%q, %v, %v)", f, t.query.Pattern, t.current, t.matchIterator)
}
func (t *branchQueryMatchTree) String() string {
return fmt.Sprintf("branch(%x)", t.mask)
}
// visitMatches visits all atoms in matchTree. Note: This visits
// noVisitMatchTree. For collecting matches use visitMatches.
func visitMatchTree(t matchTree, f func(matchTree)) {
switch s := t.(type) {
case *andMatchTree:
for _, ch := range s.children {
visitMatchTree(ch, f)
}
case *orMatchTree:
for _, ch := range s.children {
visitMatchTree(ch, f)
}
case *andLineMatchTree:
visitMatchTree(&s.andMatchTree, f)
case *noVisitMatchTree:
visitMatchTree(s.matchTree, f)
case *notMatchTree:
visitMatchTree(s.child, f)
default:
f(t)
}
}
// visitMatches visits all atoms which can contribute matches. Note: This
// skips noVisitMatchTree.
func visitMatches(t matchTree, known map[matchTree]bool, f func(matchTree)) {
switch s := t.(type) {
case *andMatchTree:
for _, ch := range s.children {
if known[ch] {
visitMatches(ch, known, f)
}
}
case *andLineMatchTree:
visitMatches(&s.andMatchTree, known, f)
case *orMatchTree:
for _, ch := range s.children {
if known[ch] {
visitMatches(ch, known, f)
}
}
case *notMatchTree:
case *noVisitMatchTree:
// don't collect into negative trees.
default:
f(s)
}
}
// all matches() methods.
func (t *docMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
return t.predicate(cp.idx), true
}
func (t *bruteForceMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
return true, true
}
// andLineMatchTree is a performance optimization of andMatchTree. For content
// searches we don't want to run the regex engine if there is no line that
// contains matches from all terms.
func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
matches, sure := t.andMatchTree.matches(cp, cost, known)
if !(sure && matches) {
return matches, sure
}
// find child with fewest candidates
min := maxUInt32
fewestChildren := 0
for ix, child := range t.children {
v, ok := child.(*substrMatchTree)
// make sure we are running a content search and that all candidates are a
// substrMatchTree
if !ok || v.fileName {
return matches, sure
}
if len(v.current) < min {
min = len(v.current)
fewestChildren = ix
}
}
type lineRange struct {
start int
end int
}
lines := make([]lineRange, 0, len(t.children[fewestChildren].(*substrMatchTree).current))
prev := -1
for _, candidate := range t.children[fewestChildren].(*substrMatchTree).current {
line, byteStart, byteEnd := candidate.line(cp.newlines(), cp.fileSize)
if line == prev {
continue
}
prev = line
lines = append(lines, lineRange{byteStart, byteEnd})
}
// children keeps track of the children's candidates we have already seen.
children := make([][]*candidateMatch, 0, len(t.children)-1)
for j, child := range t.children {
if j == fewestChildren {
continue
}
children = append(children, child.(*substrMatchTree).current)
}
nextLine:
for i := 0; i < len(lines); i++ {
hits := 1
nextChild:
for j := range children {
nextCandidate:
for len(children[j]) > 0 {
candidate := children[j][0]
bo := int(cp.findOffset(false, candidate.runeOffset))
if bo < lines[i].start {
children[j] = children[j][1:]
continue nextCandidate
}
if bo <= lines[i].end {
hits++
continue nextChild
}
// move the `lines` iterator forward until bo <= line.end
for i < len(lines) && bo > lines[i].end {
i++
}
i--
continue nextLine
}
}
// return early once we found any line that contains matches from all children
if hits == len(t.children) {
return matches, true
}
}
return false, true
}
func (t *andMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
sure := true
for _, ch := range t.children {
v, ok := evalMatchTree(cp, cost, known, ch)
if ok && !v {
return false, true
}
if !ok {
sure = false
}
}
return true, sure
}
func (t *orMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
matches := false
sure := true
for _, ch := range t.children {
v, ok := evalMatchTree(cp, cost, known, ch)
if ok {
// we could short-circuit, but we want to use
// the other possibilities as a ranking
// signal.
matches = matches || v
} else {
sure = false
}
}
return matches, sure
}
func (t *branchQueryMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
return t.fileMasks[t.docID]&t.mask != 0, true
}
func (t *regexpMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
if t.reEvaluated {
return len(t.found) > 0, true
}
if cost < costRegexp {
return false, false
}
cp.stats.RegexpsConsidered++
idxs := t.regexp.FindAllIndex(cp.data(t.fileName), -1)
found := t.found[:0]
for _, idx := range idxs {
cm := &candidateMatch{
byteOffset: uint32(idx[0]),
byteMatchSz: uint32(idx[1] - idx[0]),
fileName: t.fileName,
}
found = append(found, cm)
}
t.found = found
t.reEvaluated = true
return len(t.found) > 0, true
}
// breakMatchesOnNewlines returns matches resulting from breaking each element
// of cms on newlines within text.
func breakMatchesOnNewlines(cms []*candidateMatch, text []byte) []*candidateMatch {
var lineCMs []*candidateMatch
for _, cm := range cms {
lineCMs = append(lineCMs, breakOnNewlines(cm, text)...)
}
return lineCMs
}
// breakOnNewlines returns matches resulting from breaking cm on newlines
// within text.
func breakOnNewlines(cm *candidateMatch, text []byte) []*candidateMatch {
var cms []*candidateMatch
addMe := &candidateMatch{}
*addMe = *cm
for i := uint32(cm.byteOffset); i < cm.byteOffset+cm.byteMatchSz; i++ {
if text[i] == '\n' {
addMe.byteMatchSz = i - addMe.byteOffset
if addMe.byteMatchSz != 0 {
cms = append(cms, addMe)
}
addMe = &candidateMatch{}
*addMe = *cm
addMe.byteOffset = i + 1
}
}
addMe.byteMatchSz = cm.byteOffset + cm.byteMatchSz - addMe.byteOffset
if addMe.byteMatchSz != 0 {
cms = append(cms, addMe)
}
return cms
}
func evalMatchTree(cp *contentProvider, cost int, known map[matchTree]bool, mt matchTree) (bool, bool) {
if v, ok := known[mt]; ok {
return v, true
}
v, ok := mt.matches(cp, cost, known)
if ok {
known[mt] = v
}
return v, ok
}
func (t *notMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
v, ok := evalMatchTree(cp, cost, known, t.child)
return !v, ok
}
func (t *substrMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {
if t.contEvaluated {
return len(t.current) > 0, true
}
if len(t.current) == 0 {
return false, true
}
if t.fileName && cost < costMemory {
return false, false
}
if !t.fileName && cost < costContent {
return false, false
}
pruned := t.current[:0]
for _, m := range t.current {
if m.byteOffset == 0 && m.runeOffset > 0 {
m.byteOffset = cp.findOffset(m.fileName, m.runeOffset)
}
if m.matchContent(cp.data(m.fileName)) {
pruned = append(pruned, m)
}
}
t.current = pruned
t.contEvaluated = true
return len(t.current) > 0, true
}
func (d *indexData) newMatchTree(q query.Q) (matchTree, error) {
if q == nil {
return nil, fmt.Errorf("got nil (sub)query")
}
switch s := q.(type) {
case *query.Regexp:
// RegexpToMatchTreeRecursive tries to distill a matchTree that matches a
// superset of the regexp. If the returned matchTree is equivalent to the
// original regexp, it returns true. An equivalent matchTree has the same
// behaviour as the original regexp and can be used instead.
//
subMT, isEq, _, err := d.regexpToMatchTreeRecursive(s.Regexp, ngramSize, s.FileName, s.CaseSensitive)
if err != nil {
return nil, err
}
// if the query can be used in place of the regexp
// return the subtree
if isEq {
return subMT, nil
}
prefix := ""
if !s.CaseSensitive {
prefix = "(?i)"
}
tr := ®expMatchTree{
regexp: regexp.MustCompile(prefix + s.Regexp.String()),
fileName: s.FileName,
}
return &andMatchTree{
children: []matchTree{
tr, &noVisitMatchTree{subMT},
},
}, nil
case *query.And:
var r []matchTree
for _, ch := range s.Children {
ct, err := d.newMatchTree(ch)
if err != nil {
return nil, err
}
r = append(r, ct)
}
return &andMatchTree{r}, nil
case *query.Or:
var r []matchTree
for _, ch := range s.Children {
ct, err := d.newMatchTree(ch)
if err != nil {
return nil, err
}
r = append(r, ct)
}
return &orMatchTree{r}, nil
case *query.Not:
ct, err := d.newMatchTree(s.Child)
return ¬MatchTree{
child: ct,
}, err
case *query.Substring:
return d.newSubstringMatchTree(s)
case *query.Branch:
mask := uint64(0)
if s.Pattern == "HEAD" {
mask = 1
} else {
for nm, m := range d.branchIDs {
if strings.Contains(nm, s.Pattern) {
mask |= uint64(m)
}
}
}
return &branchQueryMatchTree{
mask: mask,
fileMasks: d.fileBranchMasks,
}, nil
case *query.Const:
if s.Value {
return &bruteForceMatchTree{}, nil
} else {
return &noMatchTree{"const"}, nil
}
case *query.Language:
code, ok := d.metaData.LanguageMap[s.Language]
if !ok {
return &noMatchTree{"lang"}, nil
}
return &docMatchTree{
reason: "language",
numDocs: uint32(len(d.languages)),
predicate: func(docID uint32) bool {
return d.languages[docID] == code
},
}, nil
case *query.Symbol:
mt, err := d.newSubstringMatchTree(s.Atom)
if err != nil {
return nil, err
}
if _, ok := mt.(*regexpMatchTree); ok {
return nil, fmt.Errorf("regexps and short queries not implemented for symbol search")
}
subMT, ok := mt.(*substrMatchTree)
if !ok {
return nil, fmt.Errorf("found %T inside query.Symbol", mt)
}
subMT.matchIterator = d.newTrimByDocSectionIter(s.Atom, subMT.matchIterator)
return subMT, nil
}
log.Panicf("type %T", q)
return nil, nil
}
func (d *indexData) newSubstringMatchTree(s *query.Substring) (matchTree, error) {
st := &substrMatchTree{
query: s,
caseSensitive: s.CaseSensitive,
fileName: s.FileName,
}
if utf8.RuneCountInString(s.Pattern) < ngramSize {
prefix := ""
if !s.CaseSensitive {
prefix = "(?i)"
}
t := ®expMatchTree{
regexp: regexp.MustCompile(prefix + regexp.QuoteMeta(s.Pattern)),
fileName: s.FileName,
}
return t, nil
}
result, err := d.iterateNgrams(s)
if err != nil {
return nil, err
}
st.matchIterator = result
return st, nil
}