-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcommit_blobs.go
197 lines (156 loc) · 4.14 KB
/
commit_blobs.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
package gitbase
import (
"io"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-mysql-server.v0/sql"
)
type commitBlobsTable struct{}
// CommitBlobsSchema is the schema for the commit blobs table.
var CommitBlobsSchema = sql.Schema{
{Name: "repository_id", Type: sql.Text, Source: CommitBlobsTableName},
{Name: "commit_hash", Type: sql.Text, Source: CommitBlobsTableName},
{Name: "blob_hash", Type: sql.Text, Source: CommitBlobsTableName},
}
var _ sql.PushdownProjectionAndFiltersTable = (*commitBlobsTable)(nil)
func newCommitBlobsTable() sql.Table {
return new(commitBlobsTable)
}
func (commitBlobsTable) isGitbaseTable() {}
func (commitBlobsTable) String() string {
return printTable(CommitBlobsTableName, CommitBlobsSchema)
}
func (commitBlobsTable) Resolved() bool { return true }
func (commitBlobsTable) Name() string { return CommitBlobsTableName }
func (commitBlobsTable) Schema() sql.Schema { return CommitBlobsSchema }
func (t *commitBlobsTable) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
return f(t)
}
func (t *commitBlobsTable) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
return t, nil
}
func (commitBlobsTable) Children() []sql.Node { return nil }
func (commitBlobsTable) RowIter(ctx *sql.Context) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.CommitBlobsTable")
iter, err := NewRowRepoIter(ctx, &commitBlobsIter{})
if err != nil {
span.Finish()
return nil, err
}
return sql.NewSpanIter(span, iter), nil
}
func (commitBlobsTable) HandledFilters(filters []sql.Expression) []sql.Expression {
return handledFilters(CommitBlobsTableName, CommitBlobsSchema, filters)
}
func (commitBlobsTable) WithProjectAndFilters(
ctx *sql.Context,
_, filters []sql.Expression,
) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.CommitBlobsTable")
iter, err := rowIterWithSelectors(
ctx, CommitBlobsSchema, CommitBlobsTableName, filters,
[]string{"commit_hash", "repository_id"},
func(selectors selectors) (RowRepoIter, error) {
repos, err := selectors.textValues("repository_id")
if err != nil {
return nil, err
}
commits, err := selectors.textValues("commit_hash")
if err != nil {
return nil, err
}
s, ok := ctx.Session.(*Session)
if !ok {
return nil, ErrInvalidGitbaseSession.New(ctx.Session)
}
return &commitBlobsIter{
repos: repos,
commits: commits,
skipGitErrors: s.SkipGitErrors,
}, nil
},
)
if err != nil {
span.Finish()
return nil, err
}
return sql.NewSpanIter(span, iter), nil
}
type commitBlobsIter struct {
repoID string
iter object.CommitIter
currCommit *object.Commit
filesIter *object.FileIter
skipGitErrors bool
// selectors for faster filtering
repos []string
commits []string
}
func (i *commitBlobsIter) NewIterator(repo *Repository) (RowRepoIter, error) {
var iter object.CommitIter
if len(i.repos) == 0 || stringContains(i.repos, repo.ID) {
var err error
iter, err = repo.Repo.CommitObjects()
if err != nil {
return nil, err
}
}
return &commitBlobsIter{
repoID: repo.ID,
iter: iter,
repos: i.repos,
commits: i.commits,
}, nil
}
func (i *commitBlobsIter) Next() (sql.Row, error) {
for {
if i.iter == nil {
return nil, io.EOF
}
if i.currCommit == nil {
commit, err := i.iter.Next()
if err != nil {
if err != io.EOF && i.skipGitErrors {
continue
}
return nil, err
}
if len(i.commits) > 0 && !stringContains(i.commits, commit.Hash.String()) {
continue
}
filesIter, err := commit.Files()
if err != nil {
if i.skipGitErrors {
continue
}
return nil, err
}
i.currCommit = commit
i.filesIter = filesIter
}
file, err := i.filesIter.Next()
if err != nil {
if err == io.EOF {
i.currCommit = nil
i.filesIter.Close()
i.filesIter = nil
continue
}
if i.skipGitErrors {
continue
}
return nil, err
}
return sql.NewRow(
i.repoID, i.currCommit.Hash.String(), file.Blob.Hash.String(),
), nil
}
}
func (i *commitBlobsIter) Close() error {
if i.filesIter != nil {
i.filesIter.Close()
}
if i.iter != nil {
i.iter.Close()
}
return nil
}