|
| 1 | +package codenav |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + |
| 7 | + genslices "github.com/life4/genesis/slices" |
| 8 | + "github.com/sourcegraph/scip/bindings/go/scip" |
| 9 | + "google.golang.org/protobuf/proto" |
| 10 | + |
| 11 | + "github.com/sourcegraph/sourcegraph/internal/api" |
| 12 | + "github.com/sourcegraph/sourcegraph/internal/codeintel/codenav/internal/lsifstore" |
| 13 | + "github.com/sourcegraph/sourcegraph/internal/codeintel/codenav/shared" |
| 14 | + "github.com/sourcegraph/sourcegraph/internal/codeintel/core" |
| 15 | +) |
| 16 | + |
| 17 | +type MappedIndex interface { |
| 18 | + // GetDocument returns None if the index does not contain a document at the given path. |
| 19 | + // There is no caching here, every call to GetDocument re-fetches the full document from the database. |
| 20 | + GetDocument(context.Context, core.RepoRelPath) (core.Option[MappedDocument], error) |
| 21 | + GetUploadSummary() core.UploadSummary |
| 22 | + // TODO: Should there be a bulk-API for getting multiple documents? |
| 23 | +} |
| 24 | + |
| 25 | +var _ MappedIndex = mappedIndex{} |
| 26 | + |
| 27 | +// MappedDocument wraps a SCIP document from an uploaded index. |
| 28 | +// All methods accept and return ranges in the context of the target commit. |
| 29 | +type MappedDocument interface { |
| 30 | + // GetOccurrences returns shared slices. Do not modify the returned slice or Occurrences without copying them first |
| 31 | + GetOccurrences(context.Context) ([]*scip.Occurrence, error) |
| 32 | + // GetOccurrencesAtRange returns shared slices. Do not modify the returned slice or Occurrences without copying them first |
| 33 | + GetOccurrencesAtRange(context.Context, scip.Range) ([]*scip.Occurrence, error) |
| 34 | +} |
| 35 | + |
| 36 | +var _ MappedDocument = &mappedDocument{} |
| 37 | + |
| 38 | +// NewMappedIndexFromTranslator creates a MappedIndex using the given GitTreeTranslator. |
| 39 | +// The translators SourceCommit has to be the targetCommit, which will be mapped to upload.GetCommit() |
| 40 | +func NewMappedIndexFromTranslator( |
| 41 | + lsifStore lsifstore.LsifStore, |
| 42 | + gitTreeTranslator GitTreeTranslator, |
| 43 | + upload core.UploadLike, |
| 44 | +) MappedIndex { |
| 45 | + return mappedIndex{ |
| 46 | + lsifStore: lsifStore, |
| 47 | + gitTreeTranslator: gitTreeTranslator, |
| 48 | + upload: upload, |
| 49 | + targetCommit: gitTreeTranslator.GetSourceCommit(), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type mappedIndex struct { |
| 54 | + lsifStore lsifstore.LsifStore |
| 55 | + gitTreeTranslator GitTreeTranslator |
| 56 | + upload core.UploadLike |
| 57 | + targetCommit api.CommitID |
| 58 | +} |
| 59 | + |
| 60 | +func (i mappedIndex) GetUploadSummary() core.UploadSummary { |
| 61 | + return core.UploadSummary{ |
| 62 | + ID: i.upload.GetID(), |
| 63 | + Root: i.upload.GetRoot(), |
| 64 | + Commit: i.upload.GetCommit(), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (i mappedIndex) GetDocument(ctx context.Context, path core.RepoRelPath) (core.Option[MappedDocument], error) { |
| 69 | + optDocument, err := i.lsifStore.SCIPDocument(ctx, i.upload.GetID(), core.NewUploadRelPath(i.upload, path)) |
| 70 | + if err != nil { |
| 71 | + return core.None[MappedDocument](), err |
| 72 | + } |
| 73 | + if document, ok := optDocument.Get(); ok { |
| 74 | + return core.Some[MappedDocument](&mappedDocument{ |
| 75 | + gitTreeTranslator: i.gitTreeTranslator, |
| 76 | + indexCommit: i.upload.GetCommit(), |
| 77 | + targetCommit: i.targetCommit, |
| 78 | + path: path, |
| 79 | + document: &lockedDocument{ |
| 80 | + inner: document, |
| 81 | + isMapped: false, |
| 82 | + mapErrored: nil, |
| 83 | + lock: sync.RWMutex{}, |
| 84 | + }, |
| 85 | + mapOnce: sync.Once{}, |
| 86 | + }), nil |
| 87 | + } else { |
| 88 | + return core.None[MappedDocument](), nil |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +type mappedDocument struct { |
| 93 | + gitTreeTranslator GitTreeTranslator |
| 94 | + indexCommit api.CommitID |
| 95 | + targetCommit api.CommitID |
| 96 | + path core.RepoRelPath |
| 97 | + |
| 98 | + document *lockedDocument |
| 99 | + mapOnce sync.Once |
| 100 | +} |
| 101 | + |
| 102 | +type lockedDocument struct { |
| 103 | + inner *scip.Document |
| 104 | + isMapped bool |
| 105 | + mapErrored error |
| 106 | + lock sync.RWMutex |
| 107 | +} |
| 108 | + |
| 109 | +func cloneOccurrence(occ *scip.Occurrence) *scip.Occurrence { |
| 110 | + occCopy, ok := proto.Clone(occ).(*scip.Occurrence) |
| 111 | + if !ok { |
| 112 | + panic("impossible! proto.Clone changed the type of message") |
| 113 | + } |
| 114 | + return occCopy |
| 115 | +} |
| 116 | + |
| 117 | +// Concurrency: Only call this while you're holding a read lock on the document |
| 118 | +func (d *mappedDocument) mapAllOccurrences(ctx context.Context) ([]*scip.Occurrence, error) { |
| 119 | + newOccurrences := make([]*scip.Occurrence, 0) |
| 120 | + for _, occ := range d.document.inner.Occurrences { |
| 121 | + sharedRange := shared.TranslateRange(scip.NewRangeUnchecked(occ.Range)) |
| 122 | + mappedRange, ok, err := d.gitTreeTranslator.GetTargetCommitRangeFromSourceRange(ctx, string(d.indexCommit), d.path.RawValue(), sharedRange, true) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + if !ok { |
| 127 | + continue |
| 128 | + } |
| 129 | + newOccurrence := cloneOccurrence(occ) |
| 130 | + newOccurrence.Range = mappedRange.ToSCIPRange().SCIPRange() |
| 131 | + newOccurrences = append(newOccurrences, newOccurrence) |
| 132 | + } |
| 133 | + return newOccurrences, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (d *mappedDocument) GetOccurrences(ctx context.Context) ([]*scip.Occurrence, error) { |
| 137 | + // It's important we don't remap the occurrences twice |
| 138 | + d.mapOnce.Do(func() { |
| 139 | + d.document.lock.RLock() |
| 140 | + newOccurrences, err := d.mapAllOccurrences(ctx) |
| 141 | + d.document.lock.RUnlock() |
| 142 | + |
| 143 | + d.document.lock.Lock() |
| 144 | + defer d.document.lock.Unlock() |
| 145 | + if err != nil { |
| 146 | + d.document.mapErrored = err |
| 147 | + return |
| 148 | + } |
| 149 | + d.document.inner.Occurrences = newOccurrences |
| 150 | + d.document.isMapped = true |
| 151 | + }) |
| 152 | + |
| 153 | + if d.document.mapErrored != nil { |
| 154 | + return nil, d.document.mapErrored |
| 155 | + } |
| 156 | + return d.document.inner.Occurrences, nil |
| 157 | +} |
| 158 | + |
| 159 | +func (d *mappedDocument) GetOccurrencesAtRange(ctx context.Context, range_ scip.Range) ([]*scip.Occurrence, error) { |
| 160 | + d.document.lock.RLock() |
| 161 | + occurrences := d.document.inner.Occurrences |
| 162 | + if d.document.isMapped { |
| 163 | + d.document.lock.RUnlock() |
| 164 | + return FindOccurrencesWithEqualRange(occurrences, range_), nil |
| 165 | + } |
| 166 | + d.document.lock.RUnlock() |
| 167 | + |
| 168 | + mappedRg, ok, err := d.gitTreeTranslator.GetTargetCommitRangeFromSourceRange( |
| 169 | + ctx, string(d.indexCommit), d.path.RawValue(), shared.TranslateRange(range_), false, |
| 170 | + ) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + if !ok { |
| 175 | + // The range was changed/removed in the target commit, so return no occurrences |
| 176 | + return nil, nil |
| 177 | + } |
| 178 | + pastMatchingOccurrences := FindOccurrencesWithEqualRange(occurrences, mappedRg.ToSCIPRange()) |
| 179 | + scipRange := range_.SCIPRange() |
| 180 | + return genslices.Map(pastMatchingOccurrences, func(occ *scip.Occurrence) *scip.Occurrence { |
| 181 | + newOccurrence := cloneOccurrence(occ) |
| 182 | + newOccurrence.Range = scipRange |
| 183 | + return newOccurrence |
| 184 | + }), nil |
| 185 | +} |
0 commit comments