forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
572 lines (458 loc) · 16.1 KB
/
api.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
// Copyright 2016 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 "github.com/google/zoekt"
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"time"
"github.com/google/zoekt/query"
)
// FileMatch contains all the matches within a file.
type FileMatch struct {
// Ranking; the higher, the better.
Score float64 // TODO - hide this field?
// For debugging. Needs DebugScore set, but public so tests in
// other packages can print some diagnostics.
Debug string
FileName string
// Repository is the globally unique name of the repo of the
// match
Repository string
Branches []string
LineMatches []LineMatch
// RepositoryID is a Sourcegraph extension. This is the ID of Repository in
// Sourcegraph.
RepositoryID uint32
// RepositoryPriority is a Sourcegraph extension. It is used by Sourcegraph to
// order results from different repositories relative to each other.
RepositoryPriority float64
// Only set if requested
Content []byte
// Checksum of the content.
Checksum []byte
// Detected language of the result.
Language string
// SubRepositoryName is the globally unique name of the repo,
// if it came from a subrepository
SubRepositoryName string
// SubRepositoryPath holds the prefix where the subrepository
// was mounted.
SubRepositoryPath string
// Commit SHA1 (hex) of the (sub)repo holding the file.
Version string
}
// LineMatch holds the matches within a single line in a file.
type LineMatch struct {
// The line in which a match was found.
Line []byte
LineStart int
LineEnd int
LineNumber int
// Before and After are only set when SearchOptions.NumContextLines is > 0
Before []byte
After []byte
// If set, this was a match on the filename.
FileName bool
// The higher the better. Only ranks the quality of the match
// within the file, does not take rank of file into account
Score float64
DebugScore string
LineFragments []LineFragmentMatch
}
type Symbol struct {
Sym string
Kind string
Parent string
ParentKind string
}
// LineFragmentMatch a segment of matching text within a line.
type LineFragmentMatch struct {
// Offset within the line, in bytes.
LineOffset int
// Offset from file start, in bytes.
Offset uint32
// Number bytes that match.
MatchLength int
SymbolInfo *Symbol
}
// Stats contains interesting numbers on the search
type Stats struct {
// Amount of I/O for reading contents.
ContentBytesLoaded int64
// Amount of I/O for reading from index.
IndexBytesLoaded int64
// Number of search shards that had a crash.
Crashes int
// Wall clock time for this search
Duration time.Duration
// Number of files containing a match.
FileCount int
// Number of files in shards that we considered.
ShardFilesConsidered int
// Files that we evaluated. Equivalent to files for which all
// atom matches (including negations) evaluated to true.
FilesConsidered int
// Files for which we loaded file content to verify substring matches
FilesLoaded int
// Candidate files whose contents weren't examined because we
// gathered enough matches.
FilesSkipped int
// Shards that we scanned to find matches.
ShardsScanned int
// Shards that we did not process because a query was canceled.
ShardsSkipped int
// Shards that we did not process because the query was rejected
// by the bloom or ngram filter indicating it had no matches.
ShardsSkippedFilter int
// Number of non-overlapping matches
MatchCount int
// Number of candidate matches as a result of searching ngrams.
NgramMatches int
// Wall clock time for queued search.
Wait time.Duration
// Number of times regexp was called on files that we evaluated.
RegexpsConsidered int
}
func (s *Stats) Add(o Stats) {
s.ContentBytesLoaded += o.ContentBytesLoaded
s.IndexBytesLoaded += o.IndexBytesLoaded
s.Crashes += o.Crashes
s.FileCount += o.FileCount
s.FilesConsidered += o.FilesConsidered
s.FilesLoaded += o.FilesLoaded
s.FilesSkipped += o.FilesSkipped
s.MatchCount += o.MatchCount
s.NgramMatches += o.NgramMatches
s.ShardFilesConsidered += o.ShardFilesConsidered
s.ShardsScanned += o.ShardsScanned
s.ShardsSkipped += o.ShardsSkipped
s.ShardsSkippedFilter += o.ShardsSkippedFilter
s.Wait += o.Wait
s.RegexpsConsidered += o.RegexpsConsidered
}
// Zero returns true if stats is empty.
func (s *Stats) Zero() bool {
if s == nil {
return true
}
return !(s.ContentBytesLoaded > 0 ||
s.IndexBytesLoaded > 0 ||
s.Crashes > 0 ||
s.FileCount > 0 ||
s.FilesConsidered > 0 ||
s.FilesLoaded > 0 ||
s.FilesSkipped > 0 ||
s.MatchCount > 0 ||
s.NgramMatches > 0 ||
s.ShardFilesConsidered > 0 ||
s.ShardsScanned > 0 ||
s.ShardsSkipped > 0 ||
s.ShardsSkippedFilter > 0 ||
s.Wait > 0 ||
s.RegexpsConsidered > 0)
}
// Progress contains information about the global progress of the running search query.
// This is used by the frontend to reorder results and emit them when stable.
// Sourcegraph specific: this is used when querying multiple zoekt-webserver instances.
type Progress struct {
// Priority of the shard that was searched.
Priority float64
// MaxPendingPriority is the maximum priority of pending result that is being searched in parallel.
// This is used to reorder results when the result set is known to be stable-- that is, when a result's
// Priority is greater than the max(MaxPendingPriority) from the latest results of each backend, it can be returned to the user.
//
// MaxPendingPriority decreases monotonically in each SearchResult.
MaxPendingPriority float64
}
// SearchResult contains search matches and extra data
type SearchResult struct {
Stats
Progress
Files []FileMatch
// RepoURLs holds a repo => template string map.
RepoURLs map[string]string
// FragmentNames holds a repo => template string map, for
// the line number fragment.
LineFragments map[string]string
}
// RepositoryBranch describes an indexed branch, which is a name
// combined with a version.
type RepositoryBranch struct {
Name string
Version string
}
func (r RepositoryBranch) String() string {
return fmt.Sprintf("%s@%s", r.Name, r.Version)
}
// Repository holds repository metadata.
type Repository struct {
// Sourcergaph's repository ID
ID uint32
// The repository name
Name string
// The repository URL.
URL string
// The physical source where this repo came from, eg. full
// path to the zip filename or git repository directory. This
// will not be exposed in the UI, but can be used to detect
// orphaned index shards.
Source string
// The branches indexed in this repo.
Branches []RepositoryBranch
// Nil if this is not the super project.
SubRepoMap map[string]*Repository
// URL template to link to the commit of a branch
CommitURLTemplate string
// The repository URL for getting to a file. Has access to
// {{Branch}}, {{Path}}
FileURLTemplate string
// The URL fragment to add to a file URL for line numbers. has
// access to {{LineNumber}}. The fragment should include the
// separator, generally '#' or ';'.
LineFragmentTemplate string
// Perf optimization: priority is set when we load the shard. It corresponds to
// the value of "priority" stored in RawConfig.
priority float64
// All zoekt.* configuration settings.
RawConfig map[string]string
// Importance of the repository, bigger is more important
Rank uint16
// IndexOptions is a hash of the options used to create the index for the
// repo.
IndexOptions string
// HasSymbols is true if this repository has indexed ctags
// output. Sourcegraph specific: This field is more appropriate for
// IndexMetadata. However, we store it here since the Sourcegraph frontend
// can read this structure but not IndexMetadata.
HasSymbols bool
// Tombstone is true if we are not allowed to search this repo.
Tombstone bool
// LatestCommitDate is the date of the latest commit among all indexed Branches.
// The date might be time.Time's 0-value if the repository was last indexed
// before this field was added.
LatestCommitDate time.Time
// FileTombstones is a set of file paths that should be ignored across all branches
// in this shard.
FileTombstones map[string]struct{} `json:",omitempty"`
}
func (r *Repository) UnmarshalJSON(data []byte) error {
// We define a new type so that we can use json.Unmarhsal
// without recursing into this same method.
type repository *Repository
repo := repository(r)
err := json.Unmarshal(data, repo)
if err != nil {
return err
}
if v, ok := repo.RawConfig["repoid"]; ok {
id, _ := strconv.ParseUint(v, 10, 32)
r.ID = uint32(id)
}
if v, ok := repo.RawConfig["priority"]; ok {
r.priority, err = strconv.ParseFloat(v, 64)
if err != nil {
r.priority = 0
}
}
return nil
}
// MergeMutable will merge x into r. mutated will be true if it made any
// changes. err is non-nil if we needed to mutate an immutable field.
//
// Note: SubRepoMap, IndexOptions and HasSymbol fields are ignored. They are
// computed while indexing so can't be synthesized from x.
//
// Note: We ignore RawConfig fields which are duplicated into Repository:
// name and id.
//
// Note: URL, *Template fields are ignored. They are not used by Sourcegraph.
func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
if r.ID != x.ID {
// Sourcegraph: strange behaviour may occur if ID changes but names don't.
return mutated, errors.New("ID is immutable")
}
if r.Name != x.Name {
// Name is encoded into the shard name on disk. We need to re-index if it
// changes.
return mutated, errors.New("Name is immutable")
}
if !reflect.DeepEqual(r.Branches, x.Branches) {
// Need a reindex if content changing.
return mutated, errors.New("Branches is immutable")
}
for k, v := range x.RawConfig {
// We ignore name and id since they are encoded into the repository.
if k == "name" || k == "id" {
continue
}
if r.RawConfig == nil {
mutated = true
r.RawConfig = make(map[string]string)
}
if r.RawConfig[k] != v {
mutated = true
r.RawConfig[k] = v
}
}
return mutated, nil
}
// IndexMetadata holds metadata stored in the index file. It contains
// data generated by the core indexing library.
type IndexMetadata struct {
IndexFormatVersion int
IndexFeatureVersion int
IndexMinReaderVersion int
IndexTime time.Time
PlainASCII bool
LanguageMap map[string]uint16
ZoektVersion string
ID string
}
// Statistics of a (collection of) repositories.
type RepoStats struct {
// Repos is used for aggregrating the number of repositories.
Repos int
// Shards is the total number of search shards.
Shards int
// Documents holds the number of documents or files.
Documents int
// IndexBytes is the amount of RAM used for index overhead.
IndexBytes int64
// ContentBytes is the amount of RAM used for raw content.
ContentBytes int64
// Sourcegraph specific stats below. These are not as efficient to calculate
// as the above statistics. We experimentally measured about a 10% slower
// shard load time. However, we find these values very useful to track and
// computing them outside of load time introduces a lot of complexity.
// NewLinesCount is the number of newlines "\n" that appear in the zoekt
// indexed documents. This is not exactly the same as line count, since it
// will not include lines not terminated by "\n" (eg a file with no "\n", or
// a final line without "\n"). Note: Zoekt deduplicates documents across
// branches, so if a path has the same contents on multiple branches, there
// is only one document for it. As such that document's newlines is only
// counted once. See DefaultBranchNewLinesCount and AllBranchesNewLinesCount
// for counts which do not deduplicate.
NewLinesCount uint64
// DefaultBranchNewLinesCount is the number of newlines "\n" in the default
// branch.
DefaultBranchNewLinesCount uint64
// OtherBranchesNewLinesCount is the number of newlines "\n" in all branches
// except the default branch.
OtherBranchesNewLinesCount uint64
}
func (s *RepoStats) Add(o *RepoStats) {
// can't update Repos, since one repo may have multiple
// shards.
s.Shards += o.Shards
s.IndexBytes += o.IndexBytes
s.Documents += o.Documents
s.ContentBytes += o.ContentBytes
// Sourcegraph specific
s.NewLinesCount += o.NewLinesCount
s.DefaultBranchNewLinesCount += o.DefaultBranchNewLinesCount
s.OtherBranchesNewLinesCount += o.OtherBranchesNewLinesCount
}
type RepoListEntry struct {
Repository Repository
IndexMetadata IndexMetadata
Stats RepoStats
}
type MinimalRepoListEntry struct {
HasSymbols bool
Branches []RepositoryBranch
}
// RepoList holds a set of Repository metadata.
type RepoList struct {
// Full response to a List request. Returned when ListOptions.Minimal is false.
Repos []*RepoListEntry
Crashes int
// Minimal response to a List request. Returned when ListOptions.Minimal is true.
Minimal map[uint32]*MinimalRepoListEntry
// Stats response to a List request.
// This is the aggregate RepoStats of all repos matching the input query.
Stats RepoStats
}
type Searcher interface {
Search(ctx context.Context, q query.Q, opts *SearchOptions) (*SearchResult, error)
// List lists repositories. The query `q` can only contain
// query.Repo atoms.
List(ctx context.Context, q query.Q, opts *ListOptions) (*RepoList, error)
Close()
// Describe the searcher for debug messages.
String() string
}
type ListOptions struct {
// Return only Minimal data per repo that Sourcegraph frontend needs.
Minimal bool
}
func (o *ListOptions) String() string {
return fmt.Sprintf("%#v", o)
}
type SearchOptions struct {
// Return an upper-bound estimate of eligible documents in
// stats.ShardFilesConsidered.
EstimateDocCount bool
// Return the whole file.
Whole bool
// Maximum number of matches: skip all processing an index
// shard after we found this many non-overlapping matches.
ShardMaxMatchCount int
// Maximum number of matches: stop looking for more matches
// once we have this many matches across shards.
TotalMaxMatchCount int
// Maximum number of matches: skip processing documents for a repository in
// a shard once we have found ShardRepoMaxMatchCount.
//
// A compound shard may contain multiple repositories. This will most often
// be set to 1 to find all repositories containing a result.
ShardRepoMaxMatchCount int
// Maximum number of important matches: skip processing
// shard after we found this many important matches.
ShardMaxImportantMatch int
// Maximum number of important matches across shards.
TotalMaxImportantMatch int
// Abort the search after this much time has passed.
MaxWallTime time.Duration
// Trim the number of results after collating and sorting the
// results
MaxDocDisplayCount int
// If set to a number greater than zero then up to this many number
// of context lines will be added before and after each matched line.
// Note that the included context lines might contain matches and
// it's up to the consumer of the result to remove those lines.
NumContextLines int
// Trace turns on opentracing for this request if true and if the Jaeger address was provided as
// a command-line flag
Trace bool
// If set, the search results will contain debug information for scoring.
DebugScore bool
// SpanContext is the opentracing span context, if it exists, from the zoekt client
SpanContext map[string]string
}
func (s *SearchOptions) String() string {
return fmt.Sprintf("%#v", s)
}
// Sender is the interface that wraps the basic Send method.
type Sender interface {
Send(*SearchResult)
}
// Streamer adds the method StreamSearch to the Searcher interface.
type Streamer interface {
Searcher
StreamSearch(ctx context.Context, q query.Q, opts *SearchOptions, sender Sender) (err error)
}