-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy patheval.go
130 lines (111 loc) · 3.03 KB
/
eval.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
package shards
import (
"context"
"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/internal/tenant"
"github.com/sourcegraph/zoekt/query"
"github.com/sourcegraph/zoekt/trace"
)
// typeRepoSearcher evaluates all type:repo sub-queries before sending the query
// to the underlying searcher. We need to evaluate type:repo queries first
// since they need to do cross shard operations.
type typeRepoSearcher struct {
zoekt.Streamer
}
func (s *typeRepoSearcher) Search(ctx context.Context, q query.Q, opts *zoekt.SearchOptions) (sr *zoekt.SearchResult, err error) {
tr, ctx := trace.New(ctx, "typeRepoSearcher.Search", "")
tr.LazyLog(q, true)
tr.LazyPrintf("opts: %+v", opts)
if tenant.EnforceTenant() {
tenant.Log(ctx, tr)
}
defer func() {
if sr != nil {
tr.LazyPrintf("num files: %d", len(sr.Files))
tr.LazyPrintf("stats: %+v", sr.Stats)
}
if err != nil {
tr.LazyPrintf("error: %v", err)
tr.SetError(err)
}
tr.Finish()
}()
q, err = s.eval(ctx, tr, q)
if err != nil {
return nil, err
}
return s.Streamer.Search(ctx, q, opts)
}
func (s *typeRepoSearcher) StreamSearch(ctx context.Context, q query.Q, opts *zoekt.SearchOptions, sender zoekt.Sender) (err error) {
tr, ctx := trace.New(ctx, "typeRepoSearcher.StreamSearch", "")
tr.LazyLog(q, true)
tr.LazyPrintf("opts: %+v", opts)
if tenant.EnforceTenant() {
tenant.Log(ctx, tr)
}
var stats zoekt.Stats
defer func() {
tr.LazyPrintf("stats: %+v", stats)
if err != nil {
tr.LazyPrintf("error: %v", err)
tr.SetError(err)
}
tr.Finish()
}()
q, err = s.eval(ctx, tr, q)
if err != nil {
return err
}
return s.Streamer.StreamSearch(ctx, q, opts, zoekt.SenderFunc(func(event *zoekt.SearchResult) {
stats.Add(event.Stats)
sender.Send(event)
}))
}
func (s *typeRepoSearcher) List(ctx context.Context, q query.Q, opts *zoekt.ListOptions) (rl *zoekt.RepoList, err error) {
tr, ctx := trace.New(ctx, "typeRepoSearcher.List", "")
tr.LazyLog(q, true)
tr.LazyPrintf("opts: %s", opts)
if tenant.EnforceTenant() {
tenant.Log(ctx, tr)
}
defer func() {
if rl != nil {
tr.LazyPrintf("repos.size=%d reposmap.size=%d crashes=%d stats=%+v", len(rl.Repos), len(rl.ReposMap), rl.Crashes, rl.Stats)
}
if err != nil {
tr.LazyPrintf("error: %v", err)
tr.SetError(err)
}
tr.Finish()
}()
q, err = s.eval(ctx, tr, q)
if err != nil {
return nil, err
}
return s.Streamer.List(ctx, q, opts)
}
func (s *typeRepoSearcher) eval(ctx context.Context, tr *trace.Trace, q query.Q) (query.Q, error) {
var err error
q = query.Map(q, func(q query.Q) query.Q {
if err != nil {
return nil
}
rq, ok := q.(*query.Type)
if !ok || rq.Type != query.TypeRepo {
return q
}
tr.LazyPrintf("evaluating sub-expression %s", rq)
var rl *zoekt.RepoList
rl, err = s.Streamer.List(ctx, rq.Child, nil)
if err != nil {
return nil
}
rs := &query.RepoSet{Set: make(map[string]bool, len(rl.Repos))}
for _, r := range rl.Repos {
rs.Set[r.Repository.Name] = true
}
tr.LazyPrintf("replaced sub-expression with %s", rs)
return rs
})
return q, err
}