Skip to content

Commit bb73108

Browse files
committed
Use RepositoryPool instead of git.Repository
Signed-off-by: Javi Fontan <[email protected]>
1 parent 219f31d commit bb73108

File tree

9 files changed

+168
-74
lines changed

9 files changed

+168
-74
lines changed

blobs.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package gitquery
33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
55

6-
"gopkg.in/src-d/go-git.v4"
76
"gopkg.in/src-d/go-git.v4/plumbing/object"
87
)
98

109
type blobsTable struct {
11-
r *git.Repository
10+
pool *RepositoryPool
1211
}
1312

14-
func newBlobsTable(r *git.Repository) sql.Table {
15-
return &blobsTable{r: r}
13+
func newBlobsTable(pool *RepositoryPool) sql.Table {
14+
return &blobsTable{pool: pool}
1615
}
1716

1817
func (blobsTable) Resolved() bool {
@@ -39,33 +38,49 @@ func (r *blobsTable) TransformExpressionsUp(f func(sql.Expression) sql.Expressio
3938
}
4039

4140
func (r blobsTable) RowIter() (sql.RowIter, error) {
42-
bIter, err := r.r.BlobObjects()
41+
iter := &blobIter{}
42+
43+
rowRepoIter, err := NewRowRepoIter(r.pool, iter)
4344
if err != nil {
4445
return nil, err
4546
}
46-
iter := &blobIter{i: bIter}
47-
return iter, nil
47+
48+
return &rowRepoIter, nil
4849
}
4950

5051
func (blobsTable) Children() []sql.Node {
5152
return []sql.Node{}
5253
}
5354

5455
type blobIter struct {
55-
i *object.BlobIter
56+
iter *object.BlobIter
57+
}
58+
59+
func (i *blobIter) InitRepository(repo Repository) error {
60+
iter, err := repo.Repo.BlobObjects()
61+
if err != nil {
62+
return err
63+
}
64+
65+
i.iter = iter
66+
67+
return nil
5668
}
5769

5870
func (i *blobIter) Next() (sql.Row, error) {
59-
blob, err := i.i.Next()
71+
o, err := i.iter.Next()
6072
if err != nil {
6173
return nil, err
6274
}
6375

64-
return blobToRow(blob), nil
76+
return blobToRow(o), nil
6577
}
6678

6779
func (i *blobIter) Close() error {
68-
i.i.Close()
80+
if i.iter != nil {
81+
i.iter.Close()
82+
}
83+
6984
return nil
7085
}
7186

cmd/gitquery/query_base.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ func (c *cmdQueryBase) buildDatabase() error {
3636
return err
3737
}
3838

39+
pool := gitquery.NewRepositoryPool()
40+
pool.Add(c.Path, r)
41+
3942
c.name = filepath.Base(filepath.Join(c.Path, ".."))
40-
c.engine.AddDatabase(gitquery.NewDatabase(c.name, r))
43+
c.engine.AddDatabase(gitquery.NewDatabase(c.name, &pool))
4144
return err
4245
}
4346

commits.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package gitquery
33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
55

6-
"gopkg.in/src-d/go-git.v4"
76
"gopkg.in/src-d/go-git.v4/plumbing/object"
87
)
98

109
type commitsTable struct {
11-
r *git.Repository
10+
pool *RepositoryPool
1211
}
1312

14-
func newCommitsTable(r *git.Repository) sql.Table {
15-
return &commitsTable{r: r}
13+
func newCommitsTable(pool *RepositoryPool) sql.Table {
14+
return &commitsTable{pool: pool}
1615
}
1716

1817
func (commitsTable) Resolved() bool {
@@ -45,32 +44,49 @@ func (r *commitsTable) TransformExpressionsUp(f func(sql.Expression) sql.Express
4544
}
4645

4746
func (r commitsTable) RowIter() (sql.RowIter, error) {
48-
cIter, err := r.r.CommitObjects()
47+
iter := &commitIter{}
48+
49+
rowRepoIter, err := NewRowRepoIter(r.pool, iter)
4950
if err != nil {
5051
return nil, err
5152
}
52-
iter := &commitIter{i: cIter}
53-
return iter, nil
53+
54+
return &rowRepoIter, nil
5455
}
5556

5657
func (commitsTable) Children() []sql.Node {
5758
return []sql.Node{}
5859
}
5960

6061
type commitIter struct {
61-
i object.CommitIter
62+
iter object.CommitIter
63+
}
64+
65+
func (i *commitIter) InitRepository(repo Repository) error {
66+
iter, err := repo.Repo.CommitObjects()
67+
if err != nil {
68+
return err
69+
}
70+
71+
i.iter = iter
72+
73+
return nil
6274
}
6375

6476
func (i *commitIter) Next() (sql.Row, error) {
65-
commit, err := i.i.Next()
77+
o, err := i.iter.Next()
6678
if err != nil {
6779
return nil, err
6880
}
69-
return commitToRow(commit), nil
81+
82+
return commitToRow(o), nil
7083
}
7184

7285
func (i *commitIter) Close() error {
73-
i.i.Close()
86+
if i.iter != nil {
87+
i.iter.Close()
88+
}
89+
7490
return nil
7591
}
7692

database.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package gitquery
22

33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
5-
6-
"gopkg.in/src-d/go-git.v4"
75
)
86

97
const (
@@ -26,15 +24,15 @@ type Database struct {
2624
or sql.Table
2725
}
2826

29-
func NewDatabase(name string, r *git.Repository) sql.Database {
27+
func NewDatabase(name string, pool *RepositoryPool) sql.Database {
3028
return &Database{
3129
name: name,
32-
cr: newCommitsTable(r),
33-
rr: newReferencesTable(r),
34-
tr: newTagsTable(r),
35-
br: newBlobsTable(r),
36-
ter: newTreeEntriesTable(r),
37-
or: newObjectsTable(r),
30+
cr: newCommitsTable(pool),
31+
rr: newReferencesTable(pool),
32+
tr: newTagsTable(pool),
33+
br: newBlobsTable(pool),
34+
ter: newTreeEntriesTable(pool),
35+
or: newObjectsTable(pool),
3836
}
3937
}
4038

database_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func getDB(require *require.Assertions, fixture *fixtures.Fixture,
6464
r, err := git.Open(s, memfs.New())
6565
require.NoError(err)
6666

67-
db := NewDatabase(name, r)
67+
pool := NewRepositoryPool()
68+
pool.Add("repo", r)
69+
70+
db := NewDatabase(name, &pool)
6871
require.NotNil(db)
6972

7073
return db

objects.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package gitquery
33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
55

6-
"gopkg.in/src-d/go-git.v4"
76
"gopkg.in/src-d/go-git.v4/plumbing/object"
87
)
98

109
type objectsTable struct {
11-
r *git.Repository
10+
pool *RepositoryPool
1211
}
1312

14-
func newObjectsTable(r *git.Repository) sql.Table {
15-
return &objectsTable{r: r}
13+
func newObjectsTable(pool *RepositoryPool) sql.Table {
14+
return &objectsTable{pool: pool}
1615
}
1716

1817
func (objectsTable) Resolved() bool {
@@ -39,24 +38,37 @@ func (r *objectsTable) TransformExpressionsUp(f func(sql.Expression) sql.Express
3938
}
4039

4140
func (r objectsTable) RowIter() (sql.RowIter, error) {
42-
oIter, err := r.r.Objects()
41+
iter := &objectIter{}
42+
43+
rowRepoIter, err := NewRowRepoIter(r.pool, iter)
4344
if err != nil {
4445
return nil, err
4546
}
46-
iter := &objectIter{i: oIter}
47-
return iter, nil
47+
48+
return &rowRepoIter, nil
4849
}
4950

5051
func (objectsTable) Children() []sql.Node {
5152
return []sql.Node{}
5253
}
5354

5455
type objectIter struct {
55-
i *object.ObjectIter
56+
iter *object.ObjectIter
57+
}
58+
59+
func (i *objectIter) InitRepository(repo Repository) error {
60+
iter, err := repo.Repo.Objects()
61+
if err != nil {
62+
return err
63+
}
64+
65+
i.iter = iter
66+
67+
return nil
5668
}
5769

5870
func (i *objectIter) Next() (sql.Row, error) {
59-
o, err := i.i.Next()
71+
o, err := i.iter.Next()
6072
if err != nil {
6173
return nil, err
6274
}
@@ -65,10 +77,12 @@ func (i *objectIter) Next() (sql.Row, error) {
6577
}
6678

6779
func (i *objectIter) Close() error {
68-
i.i.Close()
80+
if i.iter != nil {
81+
i.iter.Close()
82+
}
83+
6984
return nil
7085
}
71-
7286
func objectToRow(o object.Object) sql.Row {
7387
return sql.NewRow(
7488
o.ID().String(),

references.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package gitquery
33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
55

6-
"gopkg.in/src-d/go-git.v4"
76
"gopkg.in/src-d/go-git.v4/plumbing"
87
"gopkg.in/src-d/go-git.v4/plumbing/storer"
98
)
109

1110
type referencesTable struct {
12-
r *git.Repository
11+
pool *RepositoryPool
1312
}
1413

15-
func newReferencesTable(r *git.Repository) sql.Table {
16-
return &referencesTable{r: r}
14+
func newReferencesTable(pool *RepositoryPool) sql.Table {
15+
return &referencesTable{pool: pool}
1716
}
1817

1918
func (referencesTable) Resolved() bool {
@@ -46,33 +45,49 @@ func (r *referencesTable) TransformExpressionsUp(f func(sql.Expression) sql.Expr
4645
}
4746

4847
func (r referencesTable) RowIter() (sql.RowIter, error) {
49-
rIter, err := r.r.References()
48+
iter := &referenceIter{}
49+
50+
rowRepoIter, err := NewRowRepoIter(r.pool, iter)
5051
if err != nil {
5152
return nil, err
5253
}
53-
iter := &referenceIter{i: rIter}
54-
return iter, nil
54+
55+
return &rowRepoIter, nil
5556
}
5657

5758
func (referencesTable) Children() []sql.Node {
5859
return []sql.Node{}
5960
}
6061

6162
type referenceIter struct {
62-
i storer.ReferenceIter
63+
iter storer.ReferenceIter
64+
}
65+
66+
func (i *referenceIter) InitRepository(repo Repository) error {
67+
iter, err := repo.Repo.References()
68+
if err != nil {
69+
return err
70+
}
71+
72+
i.iter = iter
73+
74+
return nil
6375
}
6476

6577
func (i *referenceIter) Next() (sql.Row, error) {
66-
reference, err := i.i.Next()
78+
o, err := i.iter.Next()
6779
if err != nil {
6880
return nil, err
6981
}
7082

71-
return referenceToRow(reference), nil
83+
return referenceToRow(o), nil
7284
}
7385

7486
func (i *referenceIter) Close() error {
75-
i.i.Close()
87+
if i.iter != nil {
88+
i.iter.Close()
89+
}
90+
7691
return nil
7792
}
7893

0 commit comments

Comments
 (0)