Add buffered disk writes

This commit is contained in:
nxshock 2024-03-05 21:41:27 +05:00
parent 9b9eadfe92
commit 7c6ca4c81b
2 changed files with 20 additions and 2 deletions

View File

@ -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

View File

@ -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