-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
75 lines (59 loc) · 1.29 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"fmt"
"github.com/en-vee/alog"
"os"
"runtime"
"sync"
"time"
)
func createFile(directory string, filename string) (*os.File, error, string) {
if err := os.MkdirAll(directory, 0755); err != nil {
return nil, err, ""
}
path := fmt.Sprintf("%s/%s_%d.csv",
directory,
filename,
time.Now().Unix(),
)
f, err := os.Create(path)
return f, err, path
}
func writeFileByChunks(f *os.File, s *storage) error {
wg := sync.WaitGroup{}
maxWg := runtime.GOMAXPROCS(0)
c, err := s.getCount()
if err != nil {
return err
}
linesPerChunk := c / maxWg
// for small exports we don't need more than one goroutines
if c < 100 {
linesPerChunk = c
}
// we want to force the first chunk processed first to add headers
wg.Add(1)
writeChunk(f, 0, linesPerChunk, s, &wg)
if linesPerChunk != c {
for i := 1; i <= maxWg; i++ {
wg.Add(1)
go writeChunk(f, i*linesPerChunk, linesPerChunk, s, &wg)
}
}
wg.Wait()
alog.Info("Count records exported: %d", c)
return nil
}
func writeChunk(f *os.File, start, size int, s *storage, wg *sync.WaitGroup) {
defer wg.Done()
w := bufio.NewWriter(f)
if err := s.writeData(w, start, size); err != nil {
alog.Error(err.Error())
return
}
if err := w.Flush(); err != nil {
alog.Error(err.Error())
return
}
}