Skip to content

Commit 54ba8a3

Browse files
authored
Merge pull request #279 from mcarmonaa/feature/add-indexable-interfaces
*: implement indexable interfaces on basic and relation tables
2 parents 7b2f4d6 + 62d5848 commit 54ba8a3

20 files changed

+1856
-210
lines changed

blobs.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ var BlobsSchema = sql.Schema{
3838

3939
var _ sql.PushdownProjectionAndFiltersTable = (*blobsTable)(nil)
4040

41-
func newBlobsTable() sql.Table {
42-
return new(blobsTable)
41+
func newBlobsTable() Indexable {
42+
return &indexableTable{
43+
PushdownTable: new(blobsTable),
44+
buildIterWithSelectors: blobsIterBuilder,
45+
}
4346
}
4447

4548
var _ Table = (*blobsTable)(nil)
@@ -91,29 +94,20 @@ func (blobsTable) HandledFilters(filters []sql.Expression) []sql.Expression {
9194
return handledFilters(BlobsTableName, BlobsSchema, filters)
9295
}
9396

97+
func (*blobsTable) handledColumns() []string {
98+
return []string{"blob_hash"}
99+
}
100+
94101
func (r *blobsTable) WithProjectAndFilters(
95102
ctx *sql.Context,
96103
columns, filters []sql.Expression,
97104
) (sql.RowIter, error) {
98105
span, ctx := ctx.Span("gitbase.BlobsTable")
99106
iter, err := rowIterWithSelectors(
100-
ctx, BlobsSchema, BlobsTableName, filters,
101-
[]string{"blob_hash"},
102-
func(selectors selectors) (RowRepoIter, error) {
103-
if len(selectors["blob_hash"]) == 0 {
104-
return &blobIter{readContent: shouldReadContent(columns)}, nil
105-
}
106-
107-
hashes, err := selectors.textValues("blob_hash")
108-
if err != nil {
109-
return nil, err
110-
}
111-
112-
return &blobsByHashIter{
113-
hashes: hashes,
114-
readContent: shouldReadContent(columns),
115-
}, nil
116-
},
107+
ctx, BlobsSchema, BlobsTableName,
108+
filters, columns,
109+
r.handledColumns(),
110+
blobsIterBuilder,
117111
)
118112

119113
if err != nil {
@@ -124,10 +118,27 @@ func (r *blobsTable) WithProjectAndFilters(
124118
return sql.NewSpanIter(span, iter), nil
125119
}
126120

121+
func blobsIterBuilder(_ *sql.Context, selectors selectors, columns []sql.Expression) (RowRepoIter, error) {
122+
if len(selectors["blob_hash"]) == 0 {
123+
return &blobIter{readContent: shouldReadContent(columns)}, nil
124+
}
125+
126+
hashes, err := selectors.textValues("blob_hash")
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
return &blobsByHashIter{
132+
hashes: hashes,
133+
readContent: shouldReadContent(columns),
134+
}, nil
135+
}
136+
127137
type blobIter struct {
128138
repoID string
129139
iter *object.BlobIter
130140
readContent bool
141+
lastHash string
131142
}
132143

133144
func (i *blobIter) NewIterator(repo *Repository) (RowRepoIter, error) {
@@ -139,12 +150,17 @@ func (i *blobIter) NewIterator(repo *Repository) (RowRepoIter, error) {
139150
return &blobIter{repoID: repo.ID, iter: iter, readContent: i.readContent}, nil
140151
}
141152

153+
func (i *blobIter) Repository() string { return i.repoID }
154+
155+
func (i *blobIter) LastObject() string { return i.lastHash }
156+
142157
func (i *blobIter) Next() (sql.Row, error) {
143158
o, err := i.iter.Next()
144159
if err != nil {
145160
return nil, err
146161
}
147162

163+
i.lastHash = o.Hash.String()
148164
return blobToRow(i.repoID, o, i.readContent)
149165
}
150166

@@ -161,12 +177,17 @@ type blobsByHashIter struct {
161177
pos int
162178
hashes []string
163179
readContent bool
180+
lastHash string
164181
}
165182

166183
func (i *blobsByHashIter) NewIterator(repo *Repository) (RowRepoIter, error) {
167-
return &blobsByHashIter{repo, 0, i.hashes, i.readContent}, nil
184+
return &blobsByHashIter{repo, 0, i.hashes, i.readContent, ""}, nil
168185
}
169186

187+
func (i *blobsByHashIter) Repository() string { return i.repo.ID }
188+
189+
func (i *blobsByHashIter) LastObject() string { return i.lastHash }
190+
170191
func (i *blobsByHashIter) Next() (sql.Row, error) {
171192
for {
172193
if i.pos >= len(i.hashes) {
@@ -184,6 +205,7 @@ func (i *blobsByHashIter) Next() (sql.Row, error) {
184205
return nil, err
185206
}
186207

208+
i.lastHash = hash.String()
187209
return blobToRow(i.repo.ID, blob, i.readContent)
188210
}
189211
}

commit_blobs.go

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ var CommitBlobsSchema = sql.Schema{
1818

1919
var _ sql.PushdownProjectionAndFiltersTable = (*commitBlobsTable)(nil)
2020

21-
func newCommitBlobsTable() sql.Table {
22-
return new(commitBlobsTable)
21+
func newCommitBlobsTable() Indexable {
22+
return &indexableTable{
23+
PushdownTable: new(commitBlobsTable),
24+
buildIterWithSelectors: commitBlobsIterBuilder,
25+
}
2326
}
2427

2528
func (commitBlobsTable) isGitbaseTable() {}
@@ -59,36 +62,18 @@ func (commitBlobsTable) HandledFilters(filters []sql.Expression) []sql.Expressio
5962
return handledFilters(CommitBlobsTableName, CommitBlobsSchema, filters)
6063
}
6164

62-
func (commitBlobsTable) WithProjectAndFilters(
65+
func (commitBlobsTable) handledColumns() []string { return []string{"commit_hash", "repository_id"} }
66+
67+
func (t *commitBlobsTable) WithProjectAndFilters(
6368
ctx *sql.Context,
6469
_, filters []sql.Expression,
6570
) (sql.RowIter, error) {
6671
span, ctx := ctx.Span("gitbase.CommitBlobsTable")
6772
iter, err := rowIterWithSelectors(
68-
ctx, CommitBlobsSchema, CommitBlobsTableName, filters,
69-
[]string{"commit_hash", "repository_id"},
70-
func(selectors selectors) (RowRepoIter, error) {
71-
repos, err := selectors.textValues("repository_id")
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
commits, err := selectors.textValues("commit_hash")
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
s, ok := ctx.Session.(*Session)
82-
if !ok {
83-
return nil, ErrInvalidGitbaseSession.New(ctx.Session)
84-
}
85-
86-
return &commitBlobsIter{
87-
repos: repos,
88-
commits: commits,
89-
skipGitErrors: s.SkipGitErrors,
90-
}, nil
91-
},
73+
ctx, CommitBlobsSchema, CommitBlobsTableName,
74+
filters, nil,
75+
t.handledColumns(),
76+
commitBlobsIterBuilder,
9277
)
9378

9479
if err != nil {
@@ -99,8 +84,31 @@ func (commitBlobsTable) WithProjectAndFilters(
9984
return sql.NewSpanIter(span, iter), nil
10085
}
10186

87+
func commitBlobsIterBuilder(ctx *sql.Context, selectors selectors, columns []sql.Expression) (RowRepoIter, error) {
88+
repos, err := selectors.textValues("repository_id")
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
commits, err := selectors.textValues("commit_hash")
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
s, ok := ctx.Session.(*Session)
99+
if !ok {
100+
return nil, ErrInvalidGitbaseSession.New(ctx.Session)
101+
}
102+
103+
return &commitBlobsIter{
104+
repos: repos,
105+
commits: commits,
106+
skipGitErrors: s.SkipGitErrors,
107+
}, nil
108+
}
109+
102110
type commitBlobsIter struct {
103-
repoID string
111+
repo *Repository
104112
iter object.CommitIter
105113
currCommit *object.Commit
106114
filesIter *object.FileIter
@@ -122,13 +130,17 @@ func (i *commitBlobsIter) NewIterator(repo *Repository) (RowRepoIter, error) {
122130
}
123131

124132
return &commitBlobsIter{
125-
repoID: repo.ID,
133+
repo: repo,
126134
iter: iter,
127135
repos: i.repos,
128136
commits: i.commits,
129137
}, nil
130138
}
131139

140+
func (i *commitBlobsIter) Repository() string { return i.repo.ID }
141+
142+
func (i *commitBlobsIter) LastObject() string { return i.currCommit.Hash.String() }
143+
132144
func (i *commitBlobsIter) Next() (sql.Row, error) {
133145
for {
134146
if i.iter == nil {
@@ -175,7 +187,7 @@ func (i *commitBlobsIter) Next() (sql.Row, error) {
175187
}
176188

177189
return sql.NewRow(
178-
i.repoID, i.currCommit.Hash.String(), file.Blob.Hash.String(),
190+
i.repo.ID, i.currCommit.Hash.String(), file.Blob.Hash.String(),
179191
), nil
180192
}
181193
}

commit_blobs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCommitBlobsTableRowIter(t *testing.T) {
1313
require := require.New(t)
1414

1515
table := newCommitBlobsTable()
16-
require.IsType(&commitBlobsTable{}, table)
16+
require.NotNil(table)
1717

1818
ctx, paths, cleanup := setupRepos(t)
1919
defer cleanup()

commit_trees.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ var CommitTreesSchema = sql.Schema{
2121

2222
var _ sql.PushdownProjectionAndFiltersTable = (*commitTreesTable)(nil)
2323

24-
func newCommitTreesTable() sql.Table {
25-
return new(commitTreesTable)
24+
func newCommitTreesTable() Indexable {
25+
return &indexableTable{
26+
PushdownTable: new(commitTreesTable),
27+
buildIterWithSelectors: commitTreesIterBuilder,
28+
}
2629
}
2730

2831
func (commitTreesTable) isGitbaseTable() {}
@@ -62,31 +65,18 @@ func (commitTreesTable) HandledFilters(filters []sql.Expression) []sql.Expressio
6265
return handledFilters(CommitTreesTableName, CommitTreesSchema, filters)
6366
}
6467

65-
func (commitTreesTable) WithProjectAndFilters(
68+
func (commitTreesTable) handledColumns() []string { return []string{"commit_hash", "repository_id"} }
69+
70+
func (t *commitTreesTable) WithProjectAndFilters(
6671
ctx *sql.Context,
6772
_, filters []sql.Expression,
6873
) (sql.RowIter, error) {
6974
span, ctx := ctx.Span("gitbase.CommitTreesTable")
7075
iter, err := rowIterWithSelectors(
71-
ctx, CommitTreesSchema, CommitTreesTableName, filters,
72-
[]string{"commit_hash", "repository_id"},
73-
func(selectors selectors) (RowRepoIter, error) {
74-
repos, err := selectors.textValues("repository_id")
75-
if err != nil {
76-
return nil, err
77-
}
78-
79-
hashes, err := selectors.textValues("commit_hash")
80-
if err != nil {
81-
return nil, err
82-
}
83-
84-
return &commitTreesIter{
85-
ctx: ctx,
86-
commitHashes: hashes,
87-
repos: repos,
88-
}, nil
89-
},
76+
ctx, CommitTreesSchema, CommitTreesTableName,
77+
filters, nil,
78+
t.handledColumns(),
79+
commitTreesIterBuilder,
9080
)
9181

9282
if err != nil {
@@ -97,6 +87,24 @@ func (commitTreesTable) WithProjectAndFilters(
9787
return sql.NewSpanIter(span, iter), nil
9888
}
9989

90+
func commitTreesIterBuilder(ctx *sql.Context, selectors selectors, columns []sql.Expression) (RowRepoIter, error) {
91+
repos, err := selectors.textValues("repository_id")
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
hashes, err := selectors.textValues("commit_hash")
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
return &commitTreesIter{
102+
ctx: ctx,
103+
commitHashes: hashes,
104+
repos: repos,
105+
}, nil
106+
}
107+
100108
type commitTreesIter struct {
101109
ctx *sql.Context
102110
repo *Repository
@@ -129,6 +137,10 @@ func (i *commitTreesIter) NewIterator(repo *Repository) (RowRepoIter, error) {
129137
}, nil
130138
}
131139

140+
func (i *commitTreesIter) Repository() string { return i.repo.ID }
141+
142+
func (i *commitTreesIter) LastObject() string { return i.commit.Hash.String() }
143+
132144
func (i *commitTreesIter) Next() (sql.Row, error) {
133145
s, ok := i.ctx.Session.(*Session)
134146
if !ok {

0 commit comments

Comments
 (0)