fork of https://github.com/sourcegraph/zoekt
1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package zoekt // import "github.com/sourcegraph/zoekt"
16
17import (
18 "math/rand"
19 "reflect"
20
21 proto "github.com/sourcegraph/zoekt/grpc/protos/zoekt/webserver/v1"
22 "google.golang.org/protobuf/types/known/durationpb"
23 "google.golang.org/protobuf/types/known/timestamppb"
24)
25
26func FileMatchFromProto(p *proto.FileMatch) FileMatch {
27 lineMatches := make([]LineMatch, len(p.GetLineMatches()))
28 for i, lineMatch := range p.GetLineMatches() {
29 lineMatches[i] = LineMatchFromProto(lineMatch)
30 }
31
32 chunkMatches := make([]ChunkMatch, len(p.GetChunkMatches()))
33 for i, chunkMatch := range p.GetChunkMatches() {
34 chunkMatches[i] = ChunkMatchFromProto(chunkMatch)
35 }
36
37 return FileMatch{
38 Score: p.GetScore(),
39 Debug: p.GetDebug(),
40 FileName: string(p.GetFileName()), // Note: 🚨Warning, this filename may be a non-UTF8 string.
41 Repository: p.GetRepository(),
42 Branches: p.GetBranches(),
43 LineMatches: lineMatches,
44 ChunkMatches: chunkMatches,
45 RepositoryID: p.GetRepositoryId(),
46 RepositoryPriority: p.GetRepositoryPriority(),
47 Content: p.GetContent(),
48 Checksum: p.GetChecksum(),
49 Language: p.GetLanguage(),
50 SubRepositoryName: p.GetSubRepositoryName(),
51 SubRepositoryPath: p.GetSubRepositoryPath(),
52 Version: p.GetVersion(),
53 }
54}
55
56func (m *FileMatch) ToProto() *proto.FileMatch {
57 lineMatches := make([]*proto.LineMatch, len(m.LineMatches))
58 for i, lm := range m.LineMatches {
59 lineMatches[i] = lm.ToProto()
60 }
61
62 chunkMatches := make([]*proto.ChunkMatch, len(m.ChunkMatches))
63 for i, cm := range m.ChunkMatches {
64 chunkMatches[i] = cm.ToProto()
65 }
66
67 return &proto.FileMatch{
68 Score: m.Score,
69 Debug: m.Debug,
70 FileName: []byte(m.FileName),
71 Repository: m.Repository,
72 Branches: m.Branches,
73 LineMatches: lineMatches,
74 ChunkMatches: chunkMatches,
75 RepositoryId: m.RepositoryID,
76 RepositoryPriority: m.RepositoryPriority,
77 Content: m.Content,
78 Checksum: m.Checksum,
79 Language: m.Language,
80 SubRepositoryName: m.SubRepositoryName,
81 SubRepositoryPath: m.SubRepositoryPath,
82 Version: m.Version,
83 }
84}
85
86func ChunkMatchFromProto(p *proto.ChunkMatch) ChunkMatch {
87 ranges := make([]Range, len(p.GetRanges()))
88 for i, r := range p.GetRanges() {
89 ranges[i] = RangeFromProto(r)
90 }
91
92 symbols := make([]*Symbol, len(p.GetSymbolInfo()))
93 for i, r := range p.GetSymbolInfo() {
94 symbols[i] = SymbolFromProto(r)
95 }
96
97 return ChunkMatch{
98 Content: p.GetContent(),
99 ContentStart: LocationFromProto(p.GetContentStart()),
100 FileName: p.GetFileName(),
101 Ranges: ranges,
102 SymbolInfo: symbols,
103 Score: p.GetScore(),
104 DebugScore: p.GetDebugScore(),
105 }
106}
107
108func (cm *ChunkMatch) ToProto() *proto.ChunkMatch {
109 ranges := make([]*proto.Range, len(cm.Ranges))
110 for i, r := range cm.Ranges {
111 ranges[i] = r.ToProto()
112 }
113
114 symbolInfo := make([]*proto.SymbolInfo, len(cm.SymbolInfo))
115 for i, si := range cm.SymbolInfo {
116 symbolInfo[i] = si.ToProto()
117 }
118
119 return &proto.ChunkMatch{
120 Content: cm.Content,
121 ContentStart: cm.ContentStart.ToProto(),
122 FileName: cm.FileName,
123 Ranges: ranges,
124 SymbolInfo: symbolInfo,
125 Score: cm.Score,
126 DebugScore: cm.DebugScore,
127 }
128}
129
130func RangeFromProto(p *proto.Range) Range {
131 return Range{
132 Start: LocationFromProto(p.GetStart()),
133 End: LocationFromProto(p.GetEnd()),
134 }
135}
136
137func (r *Range) ToProto() *proto.Range {
138 return &proto.Range{
139 Start: r.Start.ToProto(),
140 End: r.End.ToProto(),
141 }
142}
143
144func LocationFromProto(p *proto.Location) Location {
145 return Location{
146 ByteOffset: p.GetByteOffset(),
147 LineNumber: p.GetLineNumber(),
148 Column: p.GetColumn(),
149 }
150}
151
152func (l *Location) ToProto() *proto.Location {
153 return &proto.Location{
154 ByteOffset: l.ByteOffset,
155 LineNumber: l.LineNumber,
156 Column: l.Column,
157 }
158}
159
160func LineMatchFromProto(p *proto.LineMatch) LineMatch {
161 lineFragments := make([]LineFragmentMatch, len(p.GetLineFragments()))
162 for i, lineFragment := range p.GetLineFragments() {
163 lineFragments[i] = LineFragmentMatchFromProto(lineFragment)
164 }
165
166 return LineMatch{
167 Line: p.GetLine(),
168 LineStart: int(p.GetLineStart()),
169 LineEnd: int(p.GetLineEnd()),
170 LineNumber: int(p.GetLineNumber()),
171 Before: p.GetBefore(),
172 After: p.GetAfter(),
173 FileName: p.GetFileName(),
174 Score: p.GetScore(),
175 DebugScore: p.GetDebugScore(),
176 LineFragments: lineFragments,
177 }
178}
179
180func (lm *LineMatch) ToProto() *proto.LineMatch {
181 fragments := make([]*proto.LineFragmentMatch, len(lm.LineFragments))
182 for i, fragment := range lm.LineFragments {
183 fragments[i] = fragment.ToProto()
184 }
185
186 return &proto.LineMatch{
187 Line: lm.Line,
188 LineStart: int64(lm.LineStart),
189 LineEnd: int64(lm.LineEnd),
190 LineNumber: int64(lm.LineNumber),
191 Before: lm.Before,
192 After: lm.After,
193 FileName: lm.FileName,
194 Score: lm.Score,
195 DebugScore: lm.DebugScore,
196 LineFragments: fragments,
197 }
198}
199
200func SymbolFromProto(p *proto.SymbolInfo) *Symbol {
201 if p == nil {
202 return nil
203 }
204
205 return &Symbol{
206 Sym: p.GetSym(),
207 Kind: p.GetKind(),
208 Parent: p.GetParent(),
209 ParentKind: p.GetParentKind(),
210 }
211}
212
213func (s *Symbol) ToProto() *proto.SymbolInfo {
214 if s == nil {
215 return nil
216 }
217
218 return &proto.SymbolInfo{
219 Sym: s.Sym,
220 Kind: s.Kind,
221 Parent: s.Parent,
222 ParentKind: s.ParentKind,
223 }
224}
225
226func LineFragmentMatchFromProto(p *proto.LineFragmentMatch) LineFragmentMatch {
227 return LineFragmentMatch{
228 LineOffset: int(p.GetLineOffset()),
229 Offset: p.GetOffset(),
230 MatchLength: int(p.GetMatchLength()),
231 SymbolInfo: SymbolFromProto(p.GetSymbolInfo()),
232 }
233}
234
235func (lfm *LineFragmentMatch) ToProto() *proto.LineFragmentMatch {
236 return &proto.LineFragmentMatch{
237 LineOffset: int64(lfm.LineOffset),
238 Offset: lfm.Offset,
239 MatchLength: int64(lfm.MatchLength),
240 SymbolInfo: lfm.SymbolInfo.ToProto(),
241 }
242}
243
244func FlushReasonFromProto(p proto.FlushReason) FlushReason {
245 switch p {
246 case proto.FlushReason_FLUSH_REASON_TIMER_EXPIRED:
247 return FlushReasonTimerExpired
248 case proto.FlushReason_FLUSH_REASON_FINAL_FLUSH:
249 return FlushReasonFinalFlush
250 case proto.FlushReason_FLUSH_REASON_MAX_SIZE:
251 return FlushReasonMaxSize
252 default:
253 return FlushReason(0)
254 }
255}
256
257func (fr FlushReason) ToProto() proto.FlushReason {
258 switch fr {
259 case FlushReasonTimerExpired:
260 return proto.FlushReason_FLUSH_REASON_TIMER_EXPIRED
261 case FlushReasonFinalFlush:
262 return proto.FlushReason_FLUSH_REASON_FINAL_FLUSH
263 case FlushReasonMaxSize:
264 return proto.FlushReason_FLUSH_REASON_MAX_SIZE
265 default:
266 return proto.FlushReason_FLUSH_REASON_UNKNOWN_UNSPECIFIED
267 }
268}
269
270// Generate valid reasons for quickchecks
271func (fr FlushReason) Generate(rand *rand.Rand, size int) reflect.Value {
272 switch rand.Int() % 4 {
273 case 1:
274 return reflect.ValueOf(FlushReasonMaxSize)
275 case 2:
276 return reflect.ValueOf(FlushReasonFinalFlush)
277 case 3:
278 return reflect.ValueOf(FlushReasonTimerExpired)
279 default:
280 return reflect.ValueOf(FlushReason(0))
281 }
282}
283
284func StatsFromProto(p *proto.Stats) Stats {
285 return Stats{
286 ContentBytesLoaded: p.GetContentBytesLoaded(),
287 IndexBytesLoaded: p.GetIndexBytesLoaded(),
288 Crashes: int(p.GetCrashes()),
289 Duration: p.GetDuration().AsDuration(),
290 FileCount: int(p.GetFileCount()),
291 ShardFilesConsidered: int(p.GetShardFilesConsidered()),
292 FilesConsidered: int(p.GetFilesConsidered()),
293 FilesLoaded: int(p.GetFilesLoaded()),
294 FilesSkipped: int(p.GetFilesSkipped()),
295 ShardsScanned: int(p.GetShardsScanned()),
296 ShardsSkipped: int(p.GetShardsSkipped()),
297 ShardsSkippedFilter: int(p.GetShardsSkippedFilter()),
298 MatchCount: int(p.GetMatchCount()),
299 NgramMatches: int(p.GetNgramMatches()),
300 NgramLookups: int(p.GetNgramLookups()),
301 Wait: p.GetWait().AsDuration(),
302 MatchTreeConstruction: p.GetMatchTreeConstruction().AsDuration(),
303 MatchTreeSearch: p.GetMatchTreeSearch().AsDuration(),
304 RegexpsConsidered: int(p.GetRegexpsConsidered()),
305 FlushReason: FlushReasonFromProto(p.GetFlushReason()),
306 }
307}
308
309func (s *Stats) ToProto() *proto.Stats {
310 return &proto.Stats{
311 ContentBytesLoaded: s.ContentBytesLoaded,
312 IndexBytesLoaded: s.IndexBytesLoaded,
313 Crashes: int64(s.Crashes),
314 Duration: durationpb.New(s.Duration),
315 FileCount: int64(s.FileCount),
316 ShardFilesConsidered: int64(s.ShardFilesConsidered),
317 FilesConsidered: int64(s.FilesConsidered),
318 FilesLoaded: int64(s.FilesLoaded),
319 FilesSkipped: int64(s.FilesSkipped),
320 ShardsScanned: int64(s.ShardsScanned),
321 ShardsSkipped: int64(s.ShardsSkipped),
322 ShardsSkippedFilter: int64(s.ShardsSkippedFilter),
323 MatchCount: int64(s.MatchCount),
324 NgramMatches: int64(s.NgramMatches),
325 NgramLookups: int64(s.NgramLookups),
326 Wait: durationpb.New(s.Wait),
327 MatchTreeConstruction: durationpb.New(s.MatchTreeConstruction),
328 MatchTreeSearch: durationpb.New(s.MatchTreeSearch),
329 RegexpsConsidered: int64(s.RegexpsConsidered),
330 FlushReason: s.FlushReason.ToProto(),
331 }
332}
333
334func ProgressFromProto(p *proto.Progress) Progress {
335 return Progress{
336 Priority: p.GetPriority(),
337 MaxPendingPriority: p.GetMaxPendingPriority(),
338 }
339}
340
341func (p *Progress) ToProto() *proto.Progress {
342 return &proto.Progress{
343 Priority: p.Priority,
344 MaxPendingPriority: p.MaxPendingPriority,
345 }
346}
347
348func SearchResultFromStreamProto(p *proto.StreamSearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
349 if p == nil {
350 return nil
351 }
352
353 return SearchResultFromProto(p.GetResponseChunk(), repoURLs, lineFragments)
354}
355
356func SearchResultFromProto(p *proto.SearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
357 if p == nil {
358 return nil
359 }
360
361 files := make([]FileMatch, len(p.GetFiles()))
362 for i, file := range p.GetFiles() {
363 files[i] = FileMatchFromProto(file)
364 }
365
366 return &SearchResult{
367 Stats: StatsFromProto(p.GetStats()),
368 Progress: ProgressFromProto(p.GetProgress()),
369
370 Files: files,
371
372 RepoURLs: repoURLs,
373 LineFragments: lineFragments,
374 }
375}
376
377func (sr *SearchResult) ToProto() *proto.SearchResponse {
378 if sr == nil {
379 return nil
380 }
381
382 files := make([]*proto.FileMatch, len(sr.Files))
383 for i, file := range sr.Files {
384 files[i] = file.ToProto()
385 }
386
387 return &proto.SearchResponse{
388 Stats: sr.Stats.ToProto(),
389 Progress: sr.Progress.ToProto(),
390
391 Files: files,
392 }
393}
394
395func (sr *SearchResult) ToStreamProto() *proto.StreamSearchResponse {
396 if sr == nil {
397 return nil
398 }
399
400 return &proto.StreamSearchResponse{ResponseChunk: sr.ToProto()}
401}
402
403func RepositoryBranchFromProto(p *proto.RepositoryBranch) RepositoryBranch {
404 return RepositoryBranch{
405 Name: p.GetName(),
406 Version: p.GetVersion(),
407 }
408}
409
410func (r *RepositoryBranch) ToProto() *proto.RepositoryBranch {
411 return &proto.RepositoryBranch{
412 Name: r.Name,
413 Version: r.Version,
414 }
415}
416
417func RepositoryFromProto(p *proto.Repository) Repository {
418 branches := make([]RepositoryBranch, len(p.GetBranches()))
419 for i, branch := range p.GetBranches() {
420 branches[i] = RepositoryBranchFromProto(branch)
421 }
422
423 subRepoMap := make(map[string]*Repository, len(p.GetSubRepoMap()))
424 for name, repo := range p.GetSubRepoMap() {
425 r := RepositoryFromProto(repo)
426 subRepoMap[name] = &r
427 }
428
429 fileTombstones := make(map[string]struct{}, len(p.GetFileTombstones()))
430 for _, file := range p.GetFileTombstones() {
431 fileTombstones[file] = struct{}{}
432 }
433
434 return Repository{
435 ID: p.GetId(),
436 Name: p.GetName(),
437 URL: p.GetUrl(),
438 Source: p.GetSource(),
439 Branches: branches,
440 SubRepoMap: subRepoMap,
441 CommitURLTemplate: p.GetCommitUrlTemplate(),
442 FileURLTemplate: p.GetFileUrlTemplate(),
443 LineFragmentTemplate: p.GetLineFragmentTemplate(),
444 priority: p.GetPriority(),
445 RawConfig: p.GetRawConfig(),
446 Rank: uint16(p.GetRank()),
447 IndexOptions: p.GetIndexOptions(),
448 HasSymbols: p.GetHasSymbols(),
449 Tombstone: p.GetTombstone(),
450 LatestCommitDate: p.GetLatestCommitDate().AsTime(),
451 FileTombstones: fileTombstones,
452 }
453}
454
455func (r *Repository) ToProto() *proto.Repository {
456 if r == nil {
457 return nil
458 }
459
460 branches := make([]*proto.RepositoryBranch, len(r.Branches))
461 for i, branch := range r.Branches {
462 branches[i] = branch.ToProto()
463 }
464
465 subRepoMap := make(map[string]*proto.Repository, len(r.SubRepoMap))
466 for name, repo := range r.SubRepoMap {
467 subRepoMap[name] = repo.ToProto()
468 }
469
470 fileTombstones := make([]string, 0, len(r.FileTombstones))
471 for file := range r.FileTombstones {
472 fileTombstones = append(fileTombstones, file)
473 }
474
475 return &proto.Repository{
476 Id: r.ID,
477 Name: r.Name,
478 Url: r.URL,
479 Source: r.Source,
480 Branches: branches,
481 SubRepoMap: subRepoMap,
482 CommitUrlTemplate: r.CommitURLTemplate,
483 FileUrlTemplate: r.FileURLTemplate,
484 LineFragmentTemplate: r.LineFragmentTemplate,
485 Priority: r.priority,
486 RawConfig: r.RawConfig,
487 Rank: uint32(r.Rank),
488 IndexOptions: r.IndexOptions,
489 HasSymbols: r.HasSymbols,
490 Tombstone: r.Tombstone,
491 LatestCommitDate: timestamppb.New(r.LatestCommitDate),
492 FileTombstones: fileTombstones,
493 }
494}
495
496func IndexMetadataFromProto(p *proto.IndexMetadata) IndexMetadata {
497 languageMap := make(map[string]uint16, len(p.GetLanguageMap()))
498 for language, id := range p.GetLanguageMap() {
499 languageMap[language] = uint16(id)
500 }
501
502 return IndexMetadata{
503 IndexFormatVersion: int(p.GetIndexFormatVersion()),
504 IndexFeatureVersion: int(p.GetIndexFeatureVersion()),
505 IndexMinReaderVersion: int(p.GetIndexMinReaderVersion()),
506 IndexTime: p.GetIndexTime().AsTime(),
507 PlainASCII: p.GetPlainAscii(),
508 LanguageMap: languageMap,
509 ZoektVersion: p.GetZoektVersion(),
510 ID: p.GetId(),
511 }
512}
513
514func (m *IndexMetadata) ToProto() *proto.IndexMetadata {
515 if m == nil {
516 return nil
517 }
518
519 languageMap := make(map[string]uint32, len(m.LanguageMap))
520 for language, id := range m.LanguageMap {
521 languageMap[language] = uint32(id)
522 }
523
524 return &proto.IndexMetadata{
525 IndexFormatVersion: int64(m.IndexFormatVersion),
526 IndexFeatureVersion: int64(m.IndexFeatureVersion),
527 IndexMinReaderVersion: int64(m.IndexMinReaderVersion),
528 IndexTime: timestamppb.New(m.IndexTime),
529 PlainAscii: m.PlainASCII,
530 LanguageMap: languageMap,
531 ZoektVersion: m.ZoektVersion,
532 Id: m.ID,
533 }
534}
535
536func RepoStatsFromProto(p *proto.RepoStats) RepoStats {
537 return RepoStats{
538 Repos: int(p.GetRepos()),
539 Shards: int(p.GetShards()),
540 Documents: int(p.GetDocuments()),
541 IndexBytes: p.GetIndexBytes(),
542 ContentBytes: p.GetContentBytes(),
543 NewLinesCount: p.GetNewLinesCount(),
544 DefaultBranchNewLinesCount: p.GetDefaultBranchNewLinesCount(),
545 OtherBranchesNewLinesCount: p.GetOtherBranchesNewLinesCount(),
546 }
547}
548
549func (s *RepoStats) ToProto() *proto.RepoStats {
550 return &proto.RepoStats{
551 Repos: int64(s.Repos),
552 Shards: int64(s.Shards),
553 Documents: int64(s.Documents),
554 IndexBytes: s.IndexBytes,
555 ContentBytes: s.ContentBytes,
556 NewLinesCount: s.NewLinesCount,
557 DefaultBranchNewLinesCount: s.DefaultBranchNewLinesCount,
558 OtherBranchesNewLinesCount: s.OtherBranchesNewLinesCount,
559 }
560}
561
562func RepoListEntryFromProto(p *proto.RepoListEntry) *RepoListEntry {
563 if p == nil {
564 return nil
565 }
566
567 return &RepoListEntry{
568 Repository: RepositoryFromProto(p.GetRepository()),
569 IndexMetadata: IndexMetadataFromProto(p.GetIndexMetadata()),
570 Stats: RepoStatsFromProto(p.GetStats()),
571 }
572}
573
574func (r *RepoListEntry) ToProto() *proto.RepoListEntry {
575 if r == nil {
576 return nil
577 }
578
579 return &proto.RepoListEntry{
580 Repository: r.Repository.ToProto(),
581 IndexMetadata: r.IndexMetadata.ToProto(),
582 Stats: r.Stats.ToProto(),
583 }
584}
585
586func MinimalRepoListEntryFromProto(p *proto.MinimalRepoListEntry) MinimalRepoListEntry {
587 branches := make([]RepositoryBranch, len(p.GetBranches()))
588 for i, branch := range p.GetBranches() {
589 branches[i] = RepositoryBranchFromProto(branch)
590 }
591
592 return MinimalRepoListEntry{
593 HasSymbols: p.GetHasSymbols(),
594 Branches: branches,
595 IndexTimeUnix: p.GetIndexTimeUnix(),
596 }
597}
598
599func (m *MinimalRepoListEntry) ToProto() *proto.MinimalRepoListEntry {
600 branches := make([]*proto.RepositoryBranch, len(m.Branches))
601 for i, branch := range m.Branches {
602 branches[i] = branch.ToProto()
603 }
604 return &proto.MinimalRepoListEntry{
605 HasSymbols: m.HasSymbols,
606 Branches: branches,
607 IndexTimeUnix: m.IndexTimeUnix,
608 }
609}
610
611func RepoListFromProto(p *proto.ListResponse) *RepoList {
612 repos := make([]*RepoListEntry, len(p.GetRepos()))
613 for i, repo := range p.GetRepos() {
614 repos[i] = RepoListEntryFromProto(repo)
615 }
616
617 reposMap := make(map[uint32]MinimalRepoListEntry, len(p.GetReposMap()))
618 for id, mle := range p.GetReposMap() {
619 reposMap[id] = MinimalRepoListEntryFromProto(mle)
620 }
621
622 return &RepoList{
623 Repos: repos,
624 ReposMap: reposMap,
625 Crashes: int(p.GetCrashes()),
626 Stats: RepoStatsFromProto(p.GetStats()),
627 }
628}
629
630func (r *RepoList) ToProto() *proto.ListResponse {
631 repos := make([]*proto.RepoListEntry, len(r.Repos))
632 for i, repo := range r.Repos {
633 repos[i] = repo.ToProto()
634 }
635
636 reposMap := make(map[uint32]*proto.MinimalRepoListEntry, len(r.ReposMap))
637 for id, repo := range r.ReposMap {
638 reposMap[id] = repo.ToProto()
639 }
640
641 return &proto.ListResponse{
642 Repos: repos,
643 ReposMap: reposMap,
644 Crashes: int64(r.Crashes),
645 Stats: r.Stats.ToProto(),
646 }
647}
648
649func (l *ListOptions) ToProto() *proto.ListOptions {
650 if l == nil {
651 return nil
652 }
653 var field proto.ListOptions_RepoListField
654 switch l.Field {
655 case RepoListFieldRepos:
656 field = proto.ListOptions_REPO_LIST_FIELD_REPOS
657 case RepoListFieldReposMap:
658 field = proto.ListOptions_REPO_LIST_FIELD_REPOS_MAP
659 }
660
661 return &proto.ListOptions{
662 Field: field,
663 }
664}
665
666func ListOptionsFromProto(p *proto.ListOptions) *ListOptions {
667 if p == nil {
668 return nil
669 }
670 var field RepoListField
671 switch p.GetField() {
672 case proto.ListOptions_REPO_LIST_FIELD_REPOS:
673 field = RepoListFieldRepos
674 case proto.ListOptions_REPO_LIST_FIELD_REPOS_MAP:
675 field = RepoListFieldReposMap
676 }
677 return &ListOptions{
678 Field: field,
679 }
680}
681
682func SearchOptionsFromProto(p *proto.SearchOptions) *SearchOptions {
683 if p == nil {
684 return nil
685 }
686
687 return &SearchOptions{
688 EstimateDocCount: p.GetEstimateDocCount(),
689 Whole: p.GetWhole(),
690 ShardMaxMatchCount: int(p.GetShardMaxMatchCount()),
691 TotalMaxMatchCount: int(p.GetTotalMaxMatchCount()),
692 ShardRepoMaxMatchCount: int(p.GetShardRepoMaxMatchCount()),
693 MaxWallTime: p.GetMaxWallTime().AsDuration(),
694 FlushWallTime: p.GetFlushWallTime().AsDuration(),
695 MaxDocDisplayCount: int(p.GetMaxDocDisplayCount()),
696 MaxMatchDisplayCount: int(p.GetMaxMatchDisplayCount()),
697 NumContextLines: int(p.GetNumContextLines()),
698 ChunkMatches: p.GetChunkMatches(),
699 UseDocumentRanks: p.GetUseDocumentRanks(),
700 DocumentRanksWeight: p.GetDocumentRanksWeight(),
701 Trace: p.GetTrace(),
702 DebugScore: p.GetDebugScore(),
703 UseBM25Scoring: p.GetUseBm25Scoring(),
704 }
705}
706
707func (s *SearchOptions) ToProto() *proto.SearchOptions {
708 if s == nil {
709 return nil
710 }
711
712 return &proto.SearchOptions{
713 EstimateDocCount: s.EstimateDocCount,
714 Whole: s.Whole,
715 ShardMaxMatchCount: int64(s.ShardMaxMatchCount),
716 TotalMaxMatchCount: int64(s.TotalMaxMatchCount),
717 ShardRepoMaxMatchCount: int64(s.ShardRepoMaxMatchCount),
718 MaxWallTime: durationpb.New(s.MaxWallTime),
719 FlushWallTime: durationpb.New(s.FlushWallTime),
720 MaxDocDisplayCount: int64(s.MaxDocDisplayCount),
721 MaxMatchDisplayCount: int64(s.MaxMatchDisplayCount),
722 NumContextLines: int64(s.NumContextLines),
723 ChunkMatches: s.ChunkMatches,
724 UseDocumentRanks: s.UseDocumentRanks,
725 DocumentRanksWeight: s.DocumentRanksWeight,
726 Trace: s.Trace,
727 DebugScore: s.DebugScore,
728 UseBm25Scoring: s.UseBM25Scoring,
729 }
730}