Skip to content

Commit 664ae06

Browse files
committed
Use a batch interface for gitrepo cat file
1 parent 04607f7 commit 664ae06

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

modules/git/batch.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
type Batch struct {
1212
cancel context.CancelFunc
13-
Reader *bufio.Reader
14-
Writer WriteCloserError
13+
reader *bufio.Reader
14+
writer WriteCloserError
1515
}
1616

1717
// NewBatch creates a new batch for the given repository, the Close must be invoked before release the batch
@@ -22,7 +22,7 @@ func NewBatch(ctx context.Context, repoPath string) (*Batch, error) {
2222
}
2323

2424
var batch Batch
25-
batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repoPath)
25+
batch.writer, batch.reader, batch.cancel = catFileBatch(ctx, repoPath)
2626
return &batch, nil
2727
}
2828

@@ -33,15 +33,23 @@ func NewBatchCheck(ctx context.Context, repoPath string) (*Batch, error) {
3333
}
3434

3535
var check Batch
36-
check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repoPath)
36+
check.writer, check.reader, check.cancel = catFileBatchCheck(ctx, repoPath)
3737
return &check, nil
3838
}
3939

40+
func (b *Batch) Reader() *bufio.Reader {
41+
return b.reader
42+
}
43+
44+
func (b *Batch) Writer() WriteCloserError {
45+
return b.writer
46+
}
47+
4048
func (b *Batch) Close() {
4149
if b.cancel != nil {
4250
b.cancel()
43-
b.Reader = nil
44-
b.Writer = nil
51+
b.reader = nil
52+
b.writer = nil
4553
b.cancel = nil
4654
}
4755
}

modules/git/repo_base_nogogit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
6868

6969
if !repo.batchInUse {
7070
repo.batchInUse = true
71-
return repo.batch.Writer, repo.batch.Reader, func() {
71+
return repo.batch.writer, repo.batch.reader, func() {
7272
repo.batchInUse = false
7373
}, nil
7474
}
@@ -78,7 +78,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
7878
if err != nil {
7979
return nil, nil, nil, err
8080
}
81-
return tempBatch.Writer, tempBatch.Reader, tempBatch.Close, nil
81+
return tempBatch.writer, tempBatch.reader, tempBatch.Close, nil
8282
}
8383

8484
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
@@ -93,7 +93,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
9393

9494
if !repo.checkInUse {
9595
repo.checkInUse = true
96-
return repo.check.Writer, repo.check.Reader, func() {
96+
return repo.check.writer, repo.check.reader, func() {
9797
repo.checkInUse = false
9898
}, nil
9999
}
@@ -103,7 +103,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
103103
if err != nil {
104104
return nil, nil, nil, err
105105
}
106-
return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil
106+
return tempBatchCheck.writer, tempBatchCheck.reader, tempBatchCheck.Close, nil
107107
}
108108

109109
func (repo *Repository) Close() error {

modules/gitrepo/cat_file.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
package gitrepo
55

66
import (
7+
"bufio"
78
"context"
89

910
"code.gitea.io/gitea/modules/git"
1011
)
1112

12-
func NewBatch(ctx context.Context, repo Repository) (*git.Batch, error) {
13+
type Batch interface {
14+
Reader() *bufio.Reader
15+
Writer() git.WriteCloserError
16+
Close()
17+
}
18+
19+
func NewBatch(ctx context.Context, repo Repository) (Batch, error) {
1320
return git.NewBatch(ctx, repoPath(repo))
1421
}

modules/indexer/code/bleve/bleve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha st
225225
defer gitBatch.Close()
226226

227227
for _, update := range changes.Updates {
228-
if err := b.addUpdate(ctx, gitBatch.Writer, gitBatch.Reader, sha, update, repo, batch); err != nil {
228+
if err := b.addUpdate(ctx, gitBatch.Writer(), gitBatch.Reader(), sha, update, repo, batch); err != nil {
229229
return err
230230
}
231231
}

modules/indexer/code/elasticsearch/elasticsearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha st
217217
defer batch.Close()
218218

219219
for _, update := range changes.Updates {
220-
updateReqs, err := b.addUpdate(ctx, batch.Writer, batch.Reader, sha, update, repo)
220+
updateReqs, err := b.addUpdate(ctx, batch.Writer(), batch.Reader(), sha, update, repo)
221221
if err != nil {
222222
return err
223223
}

0 commit comments

Comments
 (0)