From 2aaeb27eda71dce5f19fdd05d2f15461ccc301cd Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Wed, 18 Mar 2020 22:22:26 -0400 Subject: [PATCH] fix csv batch flush (#32) * fix csv batch flush Signed-off-by: yeya24 --- pkg/load/batch_loader.go | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/pkg/load/batch_loader.go b/pkg/load/batch_loader.go index c60d834..908d59b 100644 --- a/pkg/load/batch_loader.go +++ b/pkg/load/batch_loader.go @@ -68,7 +68,6 @@ func (b *SQLBatchLoader) Flush(ctx context.Context) error { // CSVBatchLoader helps us insert in batch type CSVBatchLoader struct { - buf [][]string f *os.File writer *csv.Writer } @@ -76,7 +75,6 @@ type CSVBatchLoader struct { // NewCSVBatchLoader creates a batch loader for csv format func NewCSVBatchLoader(f *os.File) *CSVBatchLoader { return &CSVBatchLoader{ - buf: make([][]string, 0, maxBatchCount), f: f, writer: csv.NewWriter(f), } @@ -84,26 +82,13 @@ func NewCSVBatchLoader(f *os.File) *CSVBatchLoader { // InsertValue inserts a value, the loader may flush all pending values. func (b *CSVBatchLoader) InsertValue(ctx context.Context, columns []string) error { - b.buf = append(b.buf, columns) - - if len(b.buf) >= maxBatchCount { - return b.Flush(ctx) - } - - return nil + return b.writer.Write(columns) } // Flush inserts all pending values func (b *CSVBatchLoader) Flush(ctx context.Context) error { - if len(b.buf) == 0 { - return nil - } - - err := b.writer.WriteAll(b.buf) - b.buf = b.buf[:0] b.writer.Flush() - - return err + return nil } // Close closes the file.