-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
writer.go
285 lines (243 loc) · 6.33 KB
/
writer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package storage
import (
"bytes"
"context"
"io"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/snappy"
"github.com/klauspost/compress/zstd"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"go.uber.org/zap"
)
// CompressType represents the type of compression.
type CompressType uint8
const (
// NoCompression won't compress given bytes.
NoCompression CompressType = iota
// Gzip will compress given bytes in gzip format.
Gzip
// Snappy will compress given bytes in snappy format.
Snappy
// Zstd will compress given bytes in zstd format.
Zstd
)
// DecompressConfig is the config used for decompression.
type DecompressConfig struct {
// ZStdDecodeConcurrency only used for ZStd decompress, see WithDecoderConcurrency.
// if not 1, ZStd will decode file asynchronously.
ZStdDecodeConcurrency int
}
type flusher interface {
Flush() error
}
type emptyFlusher struct{}
func (*emptyFlusher) Flush() error {
return nil
}
type interceptBuffer interface {
io.WriteCloser
flusher
Len() int
Cap() int
Bytes() []byte
Reset()
Compressed() bool
}
func createSuffixString(compressType CompressType) string {
txtSuffix := ".txt"
switch compressType {
case Gzip:
txtSuffix += ".gz"
case Snappy:
txtSuffix += ".snappy"
case Zstd:
txtSuffix += ".zst"
default:
return ""
}
return txtSuffix
}
func newInterceptBuffer(chunkSize int, compressType CompressType) interceptBuffer {
if compressType == NoCompression {
return newNoCompressionBuffer(chunkSize)
}
return newSimpleCompressBuffer(chunkSize, compressType)
}
func newCompressWriter(compressType CompressType, w io.Writer) simpleCompressWriter {
switch compressType {
case Gzip:
return gzip.NewWriter(w)
case Snappy:
return snappy.NewBufferedWriter(w)
case Zstd:
newWriter, err := zstd.NewWriter(w)
if err != nil {
log.Warn("Met error when creating new writer for Zstd type file", zap.Error(err))
}
return newWriter
default:
return nil
}
}
func newCompressReader(compressType CompressType, cfg DecompressConfig, r io.Reader) (io.Reader, error) {
switch compressType {
case Gzip:
return gzip.NewReader(r)
case Snappy:
return snappy.NewReader(r), nil
case Zstd:
options := []zstd.DOption{}
if cfg.ZStdDecodeConcurrency > 0 {
options = append(options, zstd.WithDecoderConcurrency(cfg.ZStdDecodeConcurrency))
}
return zstd.NewReader(r, options...)
default:
return nil, nil
}
}
type noCompressionBuffer struct {
*bytes.Buffer
}
func (*noCompressionBuffer) Flush() error {
return nil
}
func (*noCompressionBuffer) Close() error {
return nil
}
func (*noCompressionBuffer) Compressed() bool {
return false
}
func newNoCompressionBuffer(chunkSize int) *noCompressionBuffer {
return &noCompressionBuffer{bytes.NewBuffer(make([]byte, 0, chunkSize))}
}
type simpleCompressWriter interface {
io.WriteCloser
flusher
}
type simpleCompressBuffer struct {
*bytes.Buffer
compressWriter simpleCompressWriter
cap int
}
func (b *simpleCompressBuffer) Write(p []byte) (int, error) {
written, err := b.compressWriter.Write(p)
return written, errors.Trace(err)
}
func (b *simpleCompressBuffer) Len() int {
return b.Buffer.Len()
}
func (b *simpleCompressBuffer) Cap() int {
return b.cap
}
func (b *simpleCompressBuffer) Reset() {
b.Buffer.Reset()
}
func (b *simpleCompressBuffer) Flush() error {
return b.compressWriter.Flush()
}
func (b *simpleCompressBuffer) Close() error {
return b.compressWriter.Close()
}
func (*simpleCompressBuffer) Compressed() bool {
return true
}
func newSimpleCompressBuffer(chunkSize int, compressType CompressType) *simpleCompressBuffer {
bf := bytes.NewBuffer(make([]byte, 0, chunkSize))
return &simpleCompressBuffer{
Buffer: bf,
cap: chunkSize,
compressWriter: newCompressWriter(compressType, bf),
}
}
type bufferedWriter struct {
buf interceptBuffer
writer ExternalFileWriter
}
func (u *bufferedWriter) Write(ctx context.Context, p []byte) (int, error) {
bytesWritten := 0
for u.buf.Len()+len(p) > u.buf.Cap() {
// We won't fit p in this chunk
// Is this chunk full?
chunkToFill := u.buf.Cap() - u.buf.Len()
if chunkToFill > 0 {
// It's not full so we write enough of p to fill it
prewrite := p[0:chunkToFill]
w, err := u.buf.Write(prewrite)
bytesWritten += w
if err != nil {
return bytesWritten, errors.Trace(err)
}
p = p[w:]
// continue buf because compressed data size may be less than Cap - Len
if u.buf.Compressed() {
continue
}
}
_ = u.buf.Flush()
err := u.uploadChunk(ctx)
if err != nil {
return 0, errors.Trace(err)
}
}
w, err := u.buf.Write(p)
bytesWritten += w
return bytesWritten, errors.Trace(err)
}
func (u *bufferedWriter) uploadChunk(ctx context.Context) error {
if u.buf.Len() == 0 {
return nil
}
b := u.buf.Bytes()
u.buf.Reset()
_, err := u.writer.Write(ctx, b)
return errors.Trace(err)
}
func (u *bufferedWriter) Close(ctx context.Context) error {
u.buf.Close()
err := u.uploadChunk(ctx)
if err != nil {
return errors.Trace(err)
}
return u.writer.Close(ctx)
}
// NewUploaderWriter wraps the Writer interface over an uploader.
func NewUploaderWriter(writer ExternalFileWriter, chunkSize int, compressType CompressType) ExternalFileWriter {
return newBufferedWriter(writer, chunkSize, compressType)
}
// newBufferedWriter is used to build a buffered writer.
func newBufferedWriter(writer ExternalFileWriter, chunkSize int, compressType CompressType) *bufferedWriter {
return &bufferedWriter{
writer: writer,
buf: newInterceptBuffer(chunkSize, compressType),
}
}
// BytesWriter is a Writer implementation on top of bytes.Buffer that is useful for testing.
type BytesWriter struct {
buf *bytes.Buffer
}
// Write delegates to bytes.Buffer.
func (u *BytesWriter) Write(_ context.Context, p []byte) (int, error) {
return u.buf.Write(p)
}
// Close delegates to bytes.Buffer.
func (*BytesWriter) Close(_ context.Context) error {
// noop
return nil
}
// Bytes delegates to bytes.Buffer.
func (u *BytesWriter) Bytes() []byte {
return u.buf.Bytes()
}
// String delegates to bytes.Buffer.
func (u *BytesWriter) String() string {
return u.buf.String()
}
// Reset delegates to bytes.Buffer.
func (u *BytesWriter) Reset() {
u.buf.Reset()
}
// NewBufferWriter creates a Writer that simply writes to a buffer (useful for testing).
func NewBufferWriter() *BytesWriter {
return &BytesWriter{buf: &bytes.Buffer{}}
}