Skip to content

*: implement indexable interfaces on basic and relation tables #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 42 additions & 20 deletions blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ var BlobsSchema = sql.Schema{

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

func newBlobsTable() sql.Table {
return new(blobsTable)
func newBlobsTable() Indexable {
return &indexableTable{
PushdownTable: new(blobsTable),
buildIterWithSelectors: blobsIterBuilder,
}
}

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

func (*blobsTable) handledColumns() []string {
return []string{"blob_hash"}
}

func (r *blobsTable) WithProjectAndFilters(
ctx *sql.Context,
columns, filters []sql.Expression,
) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.BlobsTable")
iter, err := rowIterWithSelectors(
ctx, BlobsSchema, BlobsTableName, filters,
[]string{"blob_hash"},
func(selectors selectors) (RowRepoIter, error) {
if len(selectors["blob_hash"]) == 0 {
return &blobIter{readContent: shouldReadContent(columns)}, nil
}

hashes, err := selectors.textValues("blob_hash")
if err != nil {
return nil, err
}

return &blobsByHashIter{
hashes: hashes,
readContent: shouldReadContent(columns),
}, nil
},
ctx, BlobsSchema, BlobsTableName,
filters, columns,
r.handledColumns(),
blobsIterBuilder,
)

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

func blobsIterBuilder(_ *sql.Context, selectors selectors, columns []sql.Expression) (RowRepoIter, error) {
if len(selectors["blob_hash"]) == 0 {
return &blobIter{readContent: shouldReadContent(columns)}, nil
}

hashes, err := selectors.textValues("blob_hash")
if err != nil {
return nil, err
}

return &blobsByHashIter{
hashes: hashes,
readContent: shouldReadContent(columns),
}, nil
}

type blobIter struct {
repoID string
iter *object.BlobIter
readContent bool
lastHash string
}

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

func (i *blobIter) Repository() string { return i.repoID }

func (i *blobIter) LastObject() string { return i.lastHash }

func (i *blobIter) Next() (sql.Row, error) {
o, err := i.iter.Next()
if err != nil {
return nil, err
}

i.lastHash = o.Hash.String()
return blobToRow(i.repoID, o, i.readContent)
}

Expand All @@ -161,12 +177,17 @@ type blobsByHashIter struct {
pos int
hashes []string
readContent bool
lastHash string
}

func (i *blobsByHashIter) NewIterator(repo *Repository) (RowRepoIter, error) {
return &blobsByHashIter{repo, 0, i.hashes, i.readContent}, nil
return &blobsByHashIter{repo, 0, i.hashes, i.readContent, ""}, nil
}

func (i *blobsByHashIter) Repository() string { return i.repo.ID }

func (i *blobsByHashIter) LastObject() string { return i.lastHash }

func (i *blobsByHashIter) Next() (sql.Row, error) {
for {
if i.pos >= len(i.hashes) {
Expand All @@ -184,6 +205,7 @@ func (i *blobsByHashIter) Next() (sql.Row, error) {
return nil, err
}

i.lastHash = hash.String()
return blobToRow(i.repo.ID, blob, i.readContent)
}
}
Expand Down
72 changes: 42 additions & 30 deletions commit_blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ var CommitBlobsSchema = sql.Schema{

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

func newCommitBlobsTable() sql.Table {
return new(commitBlobsTable)
func newCommitBlobsTable() Indexable {
return &indexableTable{
PushdownTable: new(commitBlobsTable),
buildIterWithSelectors: commitBlobsIterBuilder,
}
}

func (commitBlobsTable) isGitbaseTable() {}
Expand Down Expand Up @@ -59,36 +62,18 @@ func (commitBlobsTable) HandledFilters(filters []sql.Expression) []sql.Expressio
return handledFilters(CommitBlobsTableName, CommitBlobsSchema, filters)
}

func (commitBlobsTable) WithProjectAndFilters(
func (commitBlobsTable) handledColumns() []string { return []string{"commit_hash", "repository_id"} }

func (t *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
},
ctx, CommitBlobsSchema, CommitBlobsTableName,
filters, nil,
t.handledColumns(),
commitBlobsIterBuilder,
)

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

func commitBlobsIterBuilder(ctx *sql.Context, selectors selectors, columns []sql.Expression) (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
}

type commitBlobsIter struct {
repoID string
repo *Repository
iter object.CommitIter
currCommit *object.Commit
filesIter *object.FileIter
Expand All @@ -122,13 +130,17 @@ func (i *commitBlobsIter) NewIterator(repo *Repository) (RowRepoIter, error) {
}

return &commitBlobsIter{
repoID: repo.ID,
repo: repo,
iter: iter,
repos: i.repos,
commits: i.commits,
}, nil
}

func (i *commitBlobsIter) Repository() string { return i.repo.ID }

func (i *commitBlobsIter) LastObject() string { return i.currCommit.Hash.String() }

func (i *commitBlobsIter) Next() (sql.Row, error) {
for {
if i.iter == nil {
Expand Down Expand Up @@ -175,7 +187,7 @@ func (i *commitBlobsIter) Next() (sql.Row, error) {
}

return sql.NewRow(
i.repoID, i.currCommit.Hash.String(), file.Blob.Hash.String(),
i.repo.ID, i.currCommit.Hash.String(), file.Blob.Hash.String(),
), nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion commit_blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestCommitBlobsTableRowIter(t *testing.T) {
require := require.New(t)

table := newCommitBlobsTable()
require.IsType(&commitBlobsTable{}, table)
require.NotNil(table)

ctx, paths, cleanup := setupRepos(t)
defer cleanup()
Expand Down
56 changes: 34 additions & 22 deletions commit_trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ var CommitTreesSchema = sql.Schema{

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

func newCommitTreesTable() sql.Table {
return new(commitTreesTable)
func newCommitTreesTable() Indexable {
return &indexableTable{
PushdownTable: new(commitTreesTable),
buildIterWithSelectors: commitTreesIterBuilder,
}
}

func (commitTreesTable) isGitbaseTable() {}
Expand Down Expand Up @@ -62,31 +65,18 @@ func (commitTreesTable) HandledFilters(filters []sql.Expression) []sql.Expressio
return handledFilters(CommitTreesTableName, CommitTreesSchema, filters)
}

func (commitTreesTable) WithProjectAndFilters(
func (commitTreesTable) handledColumns() []string { return []string{"commit_hash", "repository_id"} }

func (t *commitTreesTable) WithProjectAndFilters(
ctx *sql.Context,
_, filters []sql.Expression,
) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.CommitTreesTable")
iter, err := rowIterWithSelectors(
ctx, CommitTreesSchema, CommitTreesTableName, filters,
[]string{"commit_hash", "repository_id"},
func(selectors selectors) (RowRepoIter, error) {
repos, err := selectors.textValues("repository_id")
if err != nil {
return nil, err
}

hashes, err := selectors.textValues("commit_hash")
if err != nil {
return nil, err
}

return &commitTreesIter{
ctx: ctx,
commitHashes: hashes,
repos: repos,
}, nil
},
ctx, CommitTreesSchema, CommitTreesTableName,
filters, nil,
t.handledColumns(),
commitTreesIterBuilder,
)

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

func commitTreesIterBuilder(ctx *sql.Context, selectors selectors, columns []sql.Expression) (RowRepoIter, error) {
repos, err := selectors.textValues("repository_id")
if err != nil {
return nil, err
}

hashes, err := selectors.textValues("commit_hash")
if err != nil {
return nil, err
}

return &commitTreesIter{
ctx: ctx,
commitHashes: hashes,
repos: repos,
}, nil
}

type commitTreesIter struct {
ctx *sql.Context
repo *Repository
Expand Down Expand Up @@ -129,6 +137,10 @@ func (i *commitTreesIter) NewIterator(repo *Repository) (RowRepoIter, error) {
}, nil
}

func (i *commitTreesIter) Repository() string { return i.repo.ID }

func (i *commitTreesIter) LastObject() string { return i.commit.Hash.String() }

func (i *commitTreesIter) Next() (sql.Row, error) {
s, ok := i.ctx.Session.(*Session)
if !ok {
Expand Down
Loading