-
Notifications
You must be signed in to change notification settings - Fork 124
/
repositories.go
158 lines (128 loc) · 3.4 KB
/
repositories.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
package gitbase
import (
"io"
"github.com/src-d/go-mysql-server/sql"
)
type repositoriesTable struct {
checksumable
partitioned
filters []sql.Expression
index sql.IndexLookup
}
// RepositoriesSchema is the schema for the repositories table.
var RepositoriesSchema = sql.Schema{
{Name: "repository_id", Type: sql.Text, Nullable: false, Source: RepositoriesTableName},
}
func newRepositoriesTable(pool *RepositoryPool) *repositoriesTable {
return &repositoriesTable{checksumable: checksumable{pool}}
}
var _ Table = (*repositoriesTable)(nil)
var _ Squashable = (*repositoriesTable)(nil)
func (repositoriesTable) isSquashable() {}
func (repositoriesTable) isGitbaseTable() {}
func (repositoriesTable) Name() string {
return RepositoriesTableName
}
func (repositoriesTable) Schema() sql.Schema {
return RepositoriesSchema
}
func (r repositoriesTable) String() string {
return printTable(
RepositoriesTableName,
RepositoriesSchema,
nil,
r.filters,
r.index,
)
}
func (repositoriesTable) HandledFilters(filters []sql.Expression) []sql.Expression {
return handledFilters(RepositoriesTableName, RepositoriesSchema, filters)
}
func (r *repositoriesTable) WithFilters(filters []sql.Expression) sql.Table {
nt := *r
nt.filters = filters
return &nt
}
func (r *repositoriesTable) WithIndexLookup(idx sql.IndexLookup) sql.Table {
nt := *r
nt.index = idx
return &nt
}
func (r *repositoriesTable) 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.RepositoriesTable")
iter, err := rowIterWithSelectors(
ctx, RepositoriesSchema, RepositoriesTableName,
r.filters,
r.handledColumns(),
func(_ selectors) (sql.RowIter, error) {
if r.index != nil {
values, err := r.index.Values(p)
if err != nil {
return nil, err
}
return &rowIndexIter{new(repoRowKeyMapper), values}, nil
}
return &repositoriesRowIter{repo: repo}, nil
},
)
if err != nil {
span.Finish()
return nil, errorWithRepo(repo, err)
}
return sql.NewSpanIter(span, newRepoRowIter(repo, iter)), nil
}
func (repositoriesTable) handledColumns() []string { return nil }
func (r *repositoriesTable) IndexLookup() sql.IndexLookup { return r.index }
func (r *repositoriesTable) Filters() []sql.Expression { return r.filters }
// IndexKeyValues implements the sql.IndexableTable interface.
func (r *repositoriesTable) IndexKeyValues(
ctx *sql.Context,
colNames []string,
) (sql.PartitionIndexKeyValueIter, error) {
return newTablePartitionIndexKeyValueIter(
ctx,
newRepositoriesTable(r.pool),
RepositoriesTableName,
colNames,
new(repoRowKeyMapper),
)
}
type repoRowKeyMapper struct{}
func (repoRowKeyMapper) fromRow(row sql.Row) ([]byte, error) {
if len(row) != 1 {
return nil, errRowKeyMapperRowLength.New(1, len(row))
}
repo, ok := row[0].(string)
if !ok {
return nil, errRowKeyMapperColType.New(0, repo, row[0])
}
return []byte(repo), nil
}
func (repoRowKeyMapper) toRow(data []byte) (sql.Row, error) {
return sql.Row{string(data)}, nil
}
type repositoriesRowIter struct {
repo *Repository
visited bool
}
func (i *repositoriesRowIter) Next() (sql.Row, error) {
if i.visited {
return nil, io.EOF
}
i.visited = true
return sql.NewRow(i.repo.ID()), nil
}
func (i *repositoriesRowIter) Close() error {
i.visited = true
if i.repo != nil {
i.repo.Close()
}
return nil
}