Skip to content

Commit e5f6593

Browse files
author
Stephen Gutekanst
committed
doctree/sourcegraph: batch multiple-files-and-locations per LSIF request
Signed-off-by: Stephen Gutekanst <[email protected]>
1 parent bc597bf commit e5f6593

File tree

3 files changed

+107
-77
lines changed

3 files changed

+107
-77
lines changed

doctree/sourcegraph/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ func New(opt Options) Client {
2828
Dial: func(network, addr string) (net.Conn, error) {
2929
return net.DialTimeout(network, addr, 3*time.Second)
3030
},
31+
ResponseHeaderTimeout: 0,
3132
}
3233
return &graphQLClient{
3334
opt: opt,
3435
client: &http.Client{
3536
Transport: tr,
36-
Timeout: 3 * time.Second,
37+
Timeout: 60 * time.Second,
3738
},
3839
}
3940
}

doctree/sourcegraph/query_def_ref_impl.go

Lines changed: 102 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,49 @@ import (
99
"github.com/pkg/errors"
1010
)
1111

12-
func defRefImplQuery(n int) string {
12+
func defRefImplQuery(args DefRefImplArgs) string {
1313
var aliased []string
1414
var params []string
15-
for i := 0; i < n; i++ {
16-
params = append(params, fmt.Sprintf("$line%v: Int!, $character%v: Int!", i, i))
17-
aliased = append(aliased, fmt.Sprintf(`
18-
references%v: references(
19-
line: $line%v
20-
character: $character%v
15+
16+
for fileIndex, file := range args.Files {
17+
params = append(params, fmt.Sprintf("$file%vpath: String!", fileIndex))
18+
var aliasedFile []string
19+
for i := range file.Positions {
20+
params = append(params, fmt.Sprintf("$file%vline%v: Int!", fileIndex, i))
21+
params = append(params, fmt.Sprintf("$file%vcharacter%v: Int!", fileIndex, i))
22+
aliasedFile = append(aliasedFile, strings.ReplaceAll(strings.ReplaceAll(`
23+
references#P: references(
24+
line: $file#Fline#P
25+
character: $file#Fcharacter#P
2126
first: $firstReferences
2227
after: $afterReferences
2328
filter: $filter
2429
) {
2530
...LocationConnectionFields
2631
}
27-
implementations%v: implementations(
28-
line: $line%v
29-
character: $character%v
30-
first: $firstImplementations
31-
after: $afterImplementations
32-
filter: $filter
33-
) {
32+
# TODO: query implementations once that API does not take down prod:
33+
# https://github.com/sourcegraph/sourcegraph/issues/36882
34+
#implementations#P: implementations(
35+
# line: $file#Fline#P
36+
# character: $file#Fcharacter#P
37+
# first: $firstImplementations
38+
# after: $afterImplementations
39+
# filter: $filter
40+
#) {
41+
# ...LocationConnectionFields
42+
#}
43+
definitions#P: definitions(line: $file#Fline#P, character: $file#Fcharacter#P, filter: $filter) {
3444
...LocationConnectionFields
3545
}
36-
definitions%v: definitions(line: $line%v, character: $character%v, filter: $filter) {
37-
...LocationConnectionFields
38-
}
39-
`, i, i, i, i, i, i, i, i, i))
46+
`, "#F", fmt.Sprint(fileIndex)), "#P", fmt.Sprint(i)))
47+
}
48+
aliased = append(aliased, fmt.Sprintf(`
49+
blob%v: blob(path: $file%vpath) {
50+
lsif {
51+
%s
52+
}
53+
}
54+
`, fileIndex, fileIndex, strings.Join(aliasedFile, "\n")))
4055
}
4156

4257
return fmt.Sprintf(`
@@ -69,7 +84,16 @@ func defRefImplQuery(n int) string {
6984
}
7085
}
7186
72-
query UsePreciseCodeIntelForPosition($repositoryCloneUrl: String!, $commit: String!, $path: String!, $afterReferences: String, $firstReferences: Int, $afterImplementations: String, $firstImplementations: Int, $filter: String, %s) {
87+
query UsePreciseCodeIntelForPosition(
88+
$repositoryCloneUrl: String!,
89+
$commit: String!,
90+
$afterReferences: String,
91+
$firstReferences: Int,
92+
# TODO: query implementations once that API does not take down prod:
93+
# https://github.com/sourcegraph/sourcegraph/issues/36882
94+
#$afterImplementations: String,
95+
#$firstImplementations: Int,
96+
$filter: String, %s) {
7397
repository(cloneURL: $repositoryCloneUrl) {
7498
id
7599
name
@@ -78,11 +102,7 @@ func defRefImplQuery(n int) string {
78102
isArchived
79103
commit(rev: $commit) {
80104
id
81-
blob(path: $path) {
82-
lsif {
83-
%s
84-
}
85-
}
105+
%s
86106
}
87107
}
88108
}
@@ -94,12 +114,16 @@ type Position struct {
94114
Character uint `json:"character"`
95115
}
96116

117+
type File struct {
118+
Path string `json:"path"`
119+
Positions []Position
120+
}
121+
97122
type DefRefImplArgs struct {
98123
AfterImplementations *string `json:"afterImplementations"`
99124
AfterReferences *string `json:"afterReferences"`
100125
RepositoryCloneURL string `json:"repositoryCloneUrl"`
101-
Path string `json:"path"`
102-
Positions []Position
126+
Files []File
103127
Commit string `json:"commit"`
104128
Filter *string `json:"filter"`
105129
FirstImplementations uint `json:"firstImplementations"`
@@ -111,18 +135,20 @@ func (c *graphQLClient) DefRefImpl(ctx context.Context, args DefRefImplArgs) (*R
111135
"afterImplementations": args.AfterImplementations,
112136
"afterReferences": args.AfterReferences,
113137
"repositoryCloneUrl": args.RepositoryCloneURL,
114-
"path": args.Path,
115138
"commit": args.Commit,
116139
"filter": args.Filter,
117140
"firstImplementations": args.FirstImplementations,
118141
"firstReferences": args.FirstReferences,
119142
}
120-
for i, pos := range args.Positions {
121-
vars[fmt.Sprintf("line%v", i)] = pos.Line
122-
vars[fmt.Sprintf("character%v", i)] = pos.Character
143+
for fileIndex, file := range args.Files {
144+
vars[fmt.Sprintf("file%vpath", fileIndex)] = file.Path
145+
for i, pos := range file.Positions {
146+
vars[fmt.Sprintf("file%vline%v", fileIndex, i)] = pos.Line
147+
vars[fmt.Sprintf("file%vcharacter%v", fileIndex, i)] = pos.Character
148+
}
123149
}
124150

125-
resp, err := c.requestGraphQL(ctx, "DefRefImpl", defRefImplQuery(len(args.Positions)), vars)
151+
resp, err := c.requestGraphQL(ctx, "DefRefImpl", defRefImplQuery(args), vars)
126152
if err != nil {
127153
return nil, errors.Wrap(err, "graphql")
128154
}
@@ -135,71 +161,74 @@ func (c *graphQLClient) DefRefImpl(ctx context.Context, args DefRefImplArgs) (*R
135161
return nil, errors.Wrap(err, "Unmarshal")
136162
}
137163
var (
138-
r = raw.Data.Repository
139-
references []Location
140-
implementations []Location
141-
definitions []Location
164+
r = raw.Data.Repository
165+
blobs []Blob
142166
)
143-
decodeLocation := func(name string, dst *[]Location) error {
144-
raw, ok := r.Commit.Blob.LSIF[name]
167+
for fileIndex, file := range args.Files {
168+
rawBlob, ok := r.Commit[fmt.Sprintf("blob%v", fileIndex)]
145169
if !ok {
146-
return nil
147-
}
148-
var result *Location
149-
if err := json.Unmarshal(raw, &result); err != nil {
150-
return errors.Wrap(err, "Unmarshal")
170+
continue
151171
}
152-
if result != nil {
153-
*dst = append(*dst, *result)
172+
var info struct {
173+
LSIF map[string]json.RawMessage
154174
}
155-
return nil
156-
}
157-
for i := range args.Positions {
158-
if err := decodeLocation(fmt.Sprintf("references%v", i), &references); err != nil {
159-
return nil, errors.Wrap(err, "decodeLocation(references)")
175+
if err := json.Unmarshal(rawBlob, &info); err != nil {
176+
return nil, errors.Wrap(err, "Unmarshal")
160177
}
161-
if err := decodeLocation(fmt.Sprintf("implementations%v", i), &implementations); err != nil {
162-
return nil, errors.Wrap(err, "decodeLocation(implementations)")
178+
179+
decodeLocation := func(name string, dst *[]Location) error {
180+
raw, ok := info.LSIF[name]
181+
if !ok {
182+
return nil
183+
}
184+
var result *Location
185+
if err := json.Unmarshal(raw, &result); err != nil {
186+
return errors.Wrap(err, "Unmarshal")
187+
}
188+
if result != nil {
189+
*dst = append(*dst, *result)
190+
}
191+
return nil
163192
}
164-
if err := decodeLocation(fmt.Sprintf("definitions%v", i), &definitions); err != nil {
165-
return nil, errors.Wrap(err, "decodeLocation(definitions)")
193+
blob := Blob{LSIF: &LSIFBlob{}}
194+
for i := range file.Positions {
195+
if err := decodeLocation(fmt.Sprintf("references%v", i), &blob.LSIF.References); err != nil {
196+
return nil, errors.Wrap(err, "decodeLocation(references)")
197+
}
198+
// TODO: query implementations once that API does not take down prod:
199+
// https://github.com/sourcegraph/sourcegraph/issues/36882
200+
// if err := decodeLocation(fmt.Sprintf("implementations%v", i), &blob.LSIF.Implementations); err != nil {
201+
// return nil, errors.Wrap(err, "decodeLocation(implementations)")
202+
// }
203+
if err := decodeLocation(fmt.Sprintf("definitions%v", i), &blob.LSIF.Definitions); err != nil {
204+
return nil, errors.Wrap(err, "decodeLocation(definitions)")
205+
}
166206
}
207+
blobs = append(blobs, blob)
167208
}
209+
var commitID string
210+
_ = json.Unmarshal(r.Commit["id"], &commitID)
211+
var commitOID string
212+
_ = json.Unmarshal(r.Commit["oid"], &commitOID)
168213
return &Repository{
169214
ID: r.ID,
170215
Name: r.Name,
171216
Stars: r.Stars,
172217
IsFork: r.IsFork,
173218
IsArchived: r.IsArchived,
174219
Commit: &Commit{
175-
ID: r.Commit.ID,
176-
OID: r.Commit.OID,
177-
Blob: &Blob{
178-
LSIF: &LSIFBlob{
179-
References: references,
180-
Implementations: implementations,
181-
Definitions: definitions,
182-
},
183-
},
220+
ID: commitID,
221+
OID: commitOID,
222+
Blobs: blobs,
184223
},
185224
}, nil
186225
}
187226

188-
type DefRefImplBlob struct {
189-
LSIF map[string]json.RawMessage
190-
}
191-
192-
type DefRefImplCommit struct {
193-
ID string
194-
OID string
195-
Blob *DefRefImplBlob
196-
}
197-
198227
type DefRefImplRepository struct {
199228
ID string
200229
Name string
201230
Stars uint64
202231
IsFork bool
203232
IsArchived bool
204-
Commit *DefRefImplCommit `json:"commit"`
233+
Commit map[string]json.RawMessage
205234
}

doctree/sourcegraph/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ type Blob struct {
4848
}
4949

5050
type Commit struct {
51-
ID string
52-
OID string
53-
Blob *Blob
51+
ID string
52+
OID string
53+
Blobs []Blob
5454
}
5555

5656
type Repository struct {

0 commit comments

Comments
 (0)