forked from sourcegraph/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
180 lines (150 loc) · 4.63 KB
/
json.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
package json
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/query"
)
// defaultTimeout is the maximum amount of time a search request should
// take. This is the same default used by Sourcegraph.
const defaultTimeout = 20 * time.Second
func JSONServer(searcher zoekt.Searcher) http.Handler {
s := jsonSearcher{searcher}
mux := http.NewServeMux()
mux.HandleFunc("/search", s.jsonSearch)
mux.HandleFunc("/list", s.jsonList)
return mux
}
type jsonSearcher struct {
Searcher zoekt.Searcher
}
type jsonSearchArgs struct {
Q string
RepoIDs *[]uint32
Opts *zoekt.SearchOptions
}
type jsonSearchReply struct {
Result *zoekt.SearchResult
}
type jsonListArgs struct {
Q string
Opts *zoekt.ListOptions
}
type jsonListReply struct {
List *zoekt.RepoList
}
func (s *jsonSearcher) jsonSearch(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
w.Header().Add("Content-Type", "application/json")
if req.Method != "POST" {
jsonError(w, http.StatusMethodNotAllowed, "Only POST is supported")
return
}
searchArgs := jsonSearchArgs{}
err := json.NewDecoder(req.Body).Decode(&searchArgs)
if err != nil {
jsonError(w, http.StatusBadRequest, err.Error())
return
}
if searchArgs.Q == "" {
jsonError(w, http.StatusBadRequest, "missing query")
return
}
if searchArgs.Opts == nil {
searchArgs.Opts = &zoekt.SearchOptions{}
}
q, err := query.Parse(searchArgs.Q)
if err != nil {
jsonError(w, http.StatusBadRequest, err.Error())
return
}
if searchArgs.RepoIDs != nil {
q = query.NewAnd(q, query.NewRepoIDs(*searchArgs.RepoIDs...))
}
// Set a timeout if the user hasn't specified one.
if searchArgs.Opts.MaxWallTime == 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
defer cancel()
}
if err := CalculateDefaultSearchLimits(ctx, q, s.Searcher, searchArgs.Opts); err != nil {
jsonError(w, http.StatusInternalServerError, err.Error())
return
}
searchResult, err := s.Searcher.Search(ctx, q, searchArgs.Opts)
if err != nil {
jsonError(w, http.StatusInternalServerError, err.Error())
return
}
err = json.NewEncoder(w).Encode(jsonSearchReply{searchResult})
if err != nil {
jsonError(w, http.StatusInternalServerError, err.Error())
return
}
}
func jsonError(w http.ResponseWriter, statusCode int, err string) {
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(struct{ Error string }{Error: err})
}
// Calculates and sets heuristic defaults on opts for various upper bounds on
// the number of matches when searching, if none are already specified. The
// defaults are derived from opts.MaxDocDisplayCount, so if none is set, there
// is no calculation to do.
func CalculateDefaultSearchLimits(ctx context.Context,
q query.Q,
searcher zoekt.Searcher,
opts *zoekt.SearchOptions) error {
if opts.MaxDocDisplayCount == 0 || opts.ShardMaxMatchCount != 0 {
return nil
}
maxResultDocs := opts.MaxDocDisplayCount
// This is a special mode of Search that _only_ calculates ShardFilesConsidered and bails ASAP.
if result, err := searcher.Search(ctx, q, &zoekt.SearchOptions{EstimateDocCount: true}); err != nil {
return err
} else if numdocs := result.ShardFilesConsidered; numdocs > 10000 {
// If the search touches many shards and many files, we
// have to limit the number of matches. This setting
// is based on the number of documents eligible after
// considering reponames, so large repos (both
// android, chromium are about 500k files) aren't
// covered fairly.
// 10k docs, 50 maxResultDocs -> max match = (250 + 250 / 10)
opts.ShardMaxMatchCount = maxResultDocs*5 + (5*maxResultDocs)/(numdocs/1000)
} else {
// Virtually no limits for a small corpus.
n := numdocs + maxResultDocs*100
opts.ShardMaxMatchCount = n
opts.TotalMaxMatchCount = n
}
return nil
}
func (s *jsonSearcher) jsonList(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json")
if req.Method != "POST" {
jsonError(w, http.StatusMethodNotAllowed, "Only POST is supported")
return
}
listArgs := jsonListArgs{}
err := json.NewDecoder(req.Body).Decode(&listArgs)
if err != nil {
jsonError(w, http.StatusBadRequest, err.Error())
return
}
query, err := query.Parse(listArgs.Q)
if err != nil {
jsonError(w, http.StatusBadRequest, err.Error())
return
}
listResult, err := s.Searcher.List(req.Context(), query, listArgs.Opts)
if err != nil {
jsonError(w, http.StatusInternalServerError, err.Error())
return
}
err = json.NewEncoder(w).Encode(jsonListReply{listResult})
if err != nil {
jsonError(w, http.StatusInternalServerError, err.Error())
return
}
}