-
Notifications
You must be signed in to change notification settings - Fork 123
/
remotes.go
336 lines (276 loc) · 6.57 KB
/
remotes.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
package gitbase
import (
"bytes"
"io"
"github.com/src-d/go-mysql-server/sql"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
)
type remotesTable struct {
checksumable
partitioned
filters []sql.Expression
index sql.IndexLookup
}
// RemotesSchema is the schema for the remotes table.
var RemotesSchema = sql.Schema{
{Name: "repository_id", Type: sql.Text, Nullable: false, Source: RemotesTableName},
{Name: "remote_name", Type: sql.Text, Nullable: false, Source: RemotesTableName},
{Name: "remote_push_url", Type: sql.Text, Nullable: false, Source: RemotesTableName},
{Name: "remote_fetch_url", Type: sql.Text, Nullable: false, Source: RemotesTableName},
{Name: "remote_push_refspec", Type: sql.Text, Nullable: false, Source: RemotesTableName},
{Name: "remote_fetch_refspec", Type: sql.Text, Nullable: false, Source: RemotesTableName},
}
func newRemotesTable(pool *RepositoryPool) *remotesTable {
return &remotesTable{checksumable: checksumable{pool}}
}
var _ Table = (*remotesTable)(nil)
var _ Squashable = (*remotesTable)(nil)
func (remotesTable) isSquashable() {}
func (remotesTable) isGitbaseTable() {}
func (remotesTable) Name() string {
return RemotesTableName
}
func (remotesTable) Schema() sql.Schema {
return RemotesSchema
}
func (r remotesTable) String() string {
return printTable(
RemotesTableName,
RemotesSchema,
nil,
r.filters,
r.index,
)
}
func (r *remotesTable) WithFilters(filters []sql.Expression) sql.Table {
nt := *r
nt.filters = filters
return &nt
}
func (r *remotesTable) WithIndexLookup(idx sql.IndexLookup) sql.Table {
nt := *r
nt.index = idx
return &nt
}
func (r *remotesTable) IndexLookup() sql.IndexLookup { return r.index }
func (r *remotesTable) Filters() []sql.Expression { return r.filters }
func (r *remotesTable) PartitionRows(
ctx *sql.Context,
p sql.Partition,
) (sql.RowIter, error) {
repo, err := getPartitionRepo(ctx, p)
if err != nil {
return nil, err
}
span, ctx := ctx.Span("gitbase.RemotesTable")
iter, err := rowIterWithSelectors(
ctx, RemotesSchema, RemotesTableName,
r.filters,
r.handledColumns(),
func(selectors) (sql.RowIter, error) {
remotes, err := repo.Remotes()
if err != nil {
return nil, err
}
if r.index != nil {
values, err := r.index.Values(p)
if err != nil {
return nil, err
}
return &remotesIndexIter{
index: values,
repo: repo,
remotes: remotes,
}, nil
}
return &remotesRowIter{
repo: repo,
remotes: remotes,
}, nil
},
)
if err != nil {
span.Finish()
return nil, errorWithRepo(repo, err)
}
return sql.NewSpanIter(span, newRepoRowIter(repo, iter)), nil
}
func (remotesTable) HandledFilters(filters []sql.Expression) []sql.Expression {
return handledFilters(RemotesTableName, RemotesSchema, filters)
}
func (remotesTable) handledColumns() []string { return nil }
// IndexKeyValues implements the sql.IndexableTable interface.
func (r *remotesTable) IndexKeyValues(
ctx *sql.Context,
colNames []string,
) (sql.PartitionIndexKeyValueIter, error) {
return newPartitionedIndexKeyValueIter(
ctx,
newRemotesTable(r.pool),
colNames,
newRemotesKeyValueIter,
)
}
type remotesRowIter struct {
repo *Repository
remotes []*git.Remote
remotePos int
urlPos int
}
func (i *remotesRowIter) Next() (sql.Row, error) {
for {
if i.remotePos >= len(i.remotes) {
return nil, io.EOF
}
remote := i.remotes[i.remotePos]
config := remote.Config()
if i.urlPos >= len(config.URLs) && i.urlPos >= len(config.Fetch) {
i.remotePos++
i.urlPos = 0
continue
}
row := remoteToRow(i.repo.ID(), config, i.urlPos)
i.urlPos++
return row, nil
}
}
func (i *remotesRowIter) Close() error {
if i.repo != nil {
i.repo.Close()
}
return nil
}
func remoteToRow(repoID string, config *config.RemoteConfig, pos int) sql.Row {
var url interface{}
if pos < len(config.URLs) {
url = config.URLs[pos]
}
fetch := remoteFetchURL(config, pos)
return sql.NewRow(
repoID,
config.Name,
url,
url,
fetch,
fetch,
)
}
type remoteIndexKey struct {
Repository string
Pos int
URLPos int
}
func (k *remoteIndexKey) encode() ([]byte, error) {
var buf bytes.Buffer
writeString(&buf, k.Repository)
writeInt64(&buf, int64(k.Pos))
writeInt64(&buf, int64(k.URLPos))
return buf.Bytes(), nil
}
func (k *remoteIndexKey) decode(data []byte) error {
var buf = bytes.NewBuffer(data)
var err error
if k.Repository, err = readString(buf); err != nil {
return err
}
pos, err := readInt64(buf)
if err != nil {
return err
}
urlPos, err := readInt64(buf)
if err != nil {
return err
}
k.Pos = int(pos)
k.URLPos = int(urlPos)
return nil
}
type remotesKeyValueIter struct {
repo *Repository
columns []string
remotes []*git.Remote
pos int
urlPos int
}
func newRemotesKeyValueIter(
_ *RepositoryPool,
repo *Repository,
columns []string,
) (sql.IndexKeyValueIter, error) {
remotes, err := repo.Remotes()
if err != nil {
return nil, err
}
return &remotesKeyValueIter{
repo: repo,
columns: columns,
remotes: remotes,
}, nil
}
func (i *remotesKeyValueIter) Next() ([]interface{}, []byte, error) {
for {
if i.pos >= len(i.remotes) {
return nil, nil, io.EOF
}
cfg := i.remotes[i.pos].Config()
if i.urlPos >= len(cfg.URLs) && i.urlPos >= len(cfg.Fetch) {
i.urlPos = 0
i.pos++
continue
}
i.urlPos++
key, err := encodeIndexKey(&remoteIndexKey{i.repo.ID(), i.pos, i.urlPos - 1})
if err != nil {
return nil, nil, err
}
row := remoteToRow(i.repo.ID(), cfg, i.urlPos-1)
values, err := rowIndexValues(row, i.columns, RemotesSchema)
if err != nil {
return nil, nil, err
}
return values, key, nil
}
}
func (i *remotesKeyValueIter) Close() error {
if i.repo != nil {
i.repo.Close()
}
return nil
}
type remotesIndexIter struct {
index sql.IndexValueIter
repo *Repository
remotes []*git.Remote
}
func (i *remotesIndexIter) Next() (sql.Row, error) {
var err error
var data []byte
defer closeIndexOnError(&err, i.index)
data, err = i.index.Next()
if err != nil {
return nil, err
}
var key remoteIndexKey
if err := decodeIndexKey(data, &key); err != nil {
return nil, err
}
config := i.remotes[key.Pos].Config()
return remoteToRow(key.Repository, config, key.URLPos), nil
}
func (i *remotesIndexIter) Close() error {
if i.repo != nil {
i.repo.Close()
}
return i.index.Close()
}
func remoteFetchURL(config *config.RemoteConfig, pos int) string {
if len(config.Fetch) > 0 {
var fpos = pos
if fpos >= len(config.Fetch) {
fpos = len(config.Fetch) - 1
}
return config.Fetch[fpos].String()
}
return config.URLs[pos]
}