-
Notifications
You must be signed in to change notification settings - Fork 0
/
downraws.go
154 lines (129 loc) · 3.08 KB
/
downraws.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package downraws
import (
"archive/zip"
"bytes"
"context"
"io"
"strconv"
"github.com/pkg/errors"
)
type DownRaws struct {
downFunc func(ctx context.Context, limit, offset int) ([][]interface{}, error)
// TODO write fields to file
fields []string
// Prohibit limit greater than maxRaw
limit int
offset int
maxRaw int
driver Driver
idx int
writeCount int
writer Writer
filename string
zipBuffer *bytes.Buffer
zipWriter *zip.Writer
}
var (
ErrDriverIsNotRegister = errors.New("driver is not register")
ErrProhibitLimitGreaterThanMaxRaw = errors.New("Prohibit limit greater than maxRaw")
)
func New(driverName, filename string, fn func(ctx context.Context, limit, offset int) ([][]interface{}, error), options ...Option) (*DownRaws, error) {
driver, ok := Drivers[driverName]
if !ok {
return nil, errors.Wrapf(ErrDriverIsNotRegister, "driver name: %s", driverName)
}
zipBuffer := bytes.NewBuffer(nil)
dr := &DownRaws{
downFunc: fn,
fields: nil,
limit: 3000,
offset: 0,
maxRaw: 3000,
driver: driver,
writer: driver.NewWriter(),
filename: filename,
zipBuffer: zipBuffer,
zipWriter: zip.NewWriter(zipBuffer),
}
for _, option := range options {
option(dr)
}
if dr.limit > dr.maxRaw {
_ = dr.zipWriter.Close()
return nil, errors.Wrapf(ErrProhibitLimitGreaterThanMaxRaw, "limit: %d; maxRaw: %d", dr.limit, dr.maxRaw)
}
return dr, nil
}
func (r *DownRaws) LoadData(ctx context.Context) error {
for ; ; r.offset += r.limit {
if ctx.Err() != nil {
return errors.WithStack(ctx.Err())
}
data, err := r.downFunc(ctx, r.limit, r.offset)
if err != nil {
return err
}
if len(data) == 0 {
break
}
for i := 0; i < len(data); i++ {
if r.writeCount >= r.maxRaw {
r.idx++
f, err := r.zipWriter.Create(r.filename + "_" + strconv.Itoa(r.idx) + r.driver.Suffix())
if err != nil {
return errors.WithStack(err)
}
if _, err := r.writer.WriteTo(f); err != nil {
return err
}
r.writer = r.driver.NewWriter()
r.writeCount = 0
}
if err := r.writer.Write(data[i]...); err != nil {
return err
}
r.writeCount++
}
}
if r.writeCount != 0 {
r.idx++
f, err := r.zipWriter.Create(r.filename + "_" + strconv.Itoa(r.idx) + r.driver.Suffix())
if err != nil {
return errors.WithStack(err)
}
if _, err := r.writer.WriteTo(f); err != nil {
return err
}
}
if err := r.zipWriter.Flush(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(r.zipWriter.Close())
}
func (r *DownRaws) Close() error {
return errors.WithStack(r.zipWriter.Close())
}
func (r *DownRaws) WriteTo(writer io.Writer) (int64, error) {
return r.zipBuffer.WriteTo(writer)
}
type Option func(r *DownRaws)
func SetFields(fields ...string) Option {
return func(r *DownRaws) {
r.fields = fields
}
}
func SetLimit(limit int) Option {
return func(r *DownRaws) {
r.limit = limit
}
}
func SetOffset(offset int) Option {
return func(r *DownRaws) {
r.offset = offset
}
}
func SetMaxRaw(maxRaw int) Option {
return func(r *DownRaws) {
r.maxRaw = maxRaw
}
}