From 7c6ca4c81b03f735be781b5c314d3beaa0a8aff6 Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 5 Mar 2024 21:41:27 +0500 Subject: [PATCH] Add buffered disk writes --- export_csv.go | 11 ++++++++++- export_csvzip.go | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/export_csv.go b/export_csv.go index 6a097d6..76604ee 100644 --- a/export_csv.go +++ b/export_csv.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "encoding/csv" "fmt" "os" @@ -22,12 +23,14 @@ func (c *CsvExporter) Convert(filePath string, rows chan []any) error { return err } + buf := bufio.NewWriterSize(f, 4*1024*1024) + enc, err := c.Encoding.Encoder() if err != nil { return err } - w := csv.NewWriter(enc.Writer(f)) + w := csv.NewWriter(enc.Writer(buf)) w.Comma = ';' rowNum := 0 @@ -51,6 +54,12 @@ func (c *CsvExporter) Convert(filePath string, rows chan []any) error { return err } + err = buf.Flush() + if err != nil { + f.Close() + return err + } + err = f.Close() if err != nil { return err diff --git a/export_csvzip.go b/export_csvzip.go index 1acbad8..e81d4dc 100644 --- a/export_csvzip.go +++ b/export_csvzip.go @@ -2,6 +2,7 @@ package main import ( "archive/zip" + "bufio" "encoding/csv" "os" "path/filepath" @@ -22,7 +23,9 @@ func (c *CsvZipExporter) Convert(filePath string, rows chan []any) error { return err } - z := zip.NewWriter(f) + buf := bufio.NewWriterSize(f, 4*1024*1024) + + z := zip.NewWriter(buf) zw, err := z.Create(filepath.Base(filePath) + ".csv") if err != nil { @@ -65,6 +68,12 @@ func (c *CsvZipExporter) Convert(filePath string, rows chan []any) error { return err } + err = buf.Flush() + if err != nil { + f.Close() + return err + } + err = f.Close() if err != nil { return err