Skip to content

Commit

Permalink
fix: repeated file size fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
shuntaka9576 committed Jun 5, 2022
1 parent 9301813 commit 8917b59
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ type BatchRequestGenerator struct {
unit int
pos int
table *Table
endpos int
}

func (b *BatchRequestGenerator) Init(opt *BatchRequestGeneratorOption) *BatchRequestGenerator {
func (b *BatchRequestGenerator) Init(opt *BatchRequestGeneratorOption) (*BatchRequestGenerator, error) {
b.file = opt.File
b.action = opt.Action
b.table = opt.Table

return b
info, err := b.file.Stat()
if err != nil {
return nil, err
}

b.endpos = int(info.Size())

return b, nil
}

const (
Expand All @@ -48,18 +56,12 @@ func (b *BatchRequestGenerator) generate(unit int) (BatchRequest, error) {
TableName: b.table.Name,
}

info, err := b.file.Stat()
if err != nil {
return req, err
}
endpos := int(info.Size())

for {
var readSize int
if endpos-b.pos-READ_BUF_SIZE >= 0 {
if b.endpos-b.pos-READ_BUF_SIZE >= 0 {
readSize = READ_BUF_SIZE
} else {
readSize = endpos - b.pos
readSize = b.endpos - b.pos
}

if readSize == 0 {
Expand Down Expand Up @@ -121,7 +123,7 @@ func (b *BatchRequestGenerator) generate(unit int) (BatchRequest, error) {
return req, err
}

if endpos == b.pos+readSize {
if b.endpos == b.pos+readSize {
b.pos += readSize

return req, ErrBatchEOF
Expand Down

0 comments on commit 8917b59

Please sign in to comment.