forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
231 lines (196 loc) · 5.76 KB
/
compress.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
package processor
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"fmt"
"time"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/tracing"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/response"
"github.com/dafanshu/benthos/v3/lib/types"
"github.com/golang/snappy"
"github.com/pierrec/lz4/v4"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeCompress] = TypeSpec{
constructor: NewCompress,
Categories: []Category{
CategoryParsing,
},
Summary: `
Compresses messages according to the selected algorithm. Supported compression
algorithms are: gzip, zlib, flate, snappy, lz4.`,
Description: `
The 'level' field might not apply to all algorithms.`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("algorithm", "The compression algorithm to use.").HasOptions("gzip", "zlib", "flate", "snappy", "lz4"),
docs.FieldCommon("level", "The level of compression to use. May not be applicable to all algorithms."),
PartsFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// CompressConfig contains configuration fields for the Compress processor.
type CompressConfig struct {
Algorithm string `json:"algorithm" yaml:"algorithm"`
Level int `json:"level" yaml:"level"`
Parts []int `json:"parts" yaml:"parts"`
}
// NewCompressConfig returns a CompressConfig with default values.
func NewCompressConfig() CompressConfig {
return CompressConfig{
Algorithm: "gzip",
Level: gzip.DefaultCompression,
Parts: []int{},
}
}
//------------------------------------------------------------------------------
type compressFunc func(level int, bytes []byte) ([]byte, error)
func gzipCompress(level int, b []byte) ([]byte, error) {
buf := &bytes.Buffer{}
w, err := gzip.NewWriterLevel(buf, level)
if err != nil {
return nil, err
}
if _, err = w.Write(b); err != nil {
w.Close()
return nil, err
}
// Must flush writer before calling buf.Bytes()
w.Close()
return buf.Bytes(), nil
}
func zlibCompress(level int, b []byte) ([]byte, error) {
buf := &bytes.Buffer{}
w, err := zlib.NewWriterLevel(buf, level)
if err != nil {
return nil, err
}
if _, err = w.Write(b); err != nil {
w.Close()
return nil, err
}
// Must flush writer before calling buf.Bytes()
w.Close()
return buf.Bytes(), nil
}
func flateCompress(level int, b []byte) ([]byte, error) {
buf := &bytes.Buffer{}
w, err := flate.NewWriter(buf, level)
if err != nil {
return nil, err
}
if _, err = w.Write(b); err != nil {
w.Close()
return nil, err
}
// Must flush writer before calling buf.Bytes()
w.Close()
return buf.Bytes(), nil
}
func snappyCompress(level int, b []byte) ([]byte, error) {
return snappy.Encode(nil, b), nil
}
func lz4Compress(level int, b []byte) ([]byte, error) {
buf := &bytes.Buffer{}
w := lz4.NewWriter(buf)
if level > 0 {
// The default compression level is 0 (lz4.Fast)
if err := w.Apply(lz4.CompressionLevelOption(lz4.CompressionLevel(1 << (8 + level)))); err != nil {
return nil, err
}
}
if _, err := w.Write(b); err != nil {
w.Close()
return nil, err
}
// Must flush writer before calling buf.Bytes()
w.Close()
return buf.Bytes(), nil
}
func strToCompressor(str string) (compressFunc, error) {
switch str {
case "gzip":
return gzipCompress, nil
case "zlib":
return zlibCompress, nil
case "flate":
return flateCompress, nil
case "snappy":
return snappyCompress, nil
case "lz4":
return lz4Compress, nil
}
return nil, fmt.Errorf("compression type not recognised: %v", str)
}
//------------------------------------------------------------------------------
// Compress is a processor that can selectively compress parts of a message as a
// chosen compression algorithm.
type Compress struct {
conf CompressConfig
comp compressFunc
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mErr metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewCompress returns a Compress processor.
func NewCompress(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
cor, err := strToCompressor(conf.Compress.Algorithm)
if err != nil {
return nil, err
}
return &Compress{
conf: conf.Compress,
comp: cor,
log: log,
stats: stats,
mCount: stats.GetCounter("count"),
mErr: stats.GetCounter("error"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
}, nil
}
//------------------------------------------------------------------------------
// ProcessMessage applies the processor to a message, either creating >0
// resulting messages or a response to be sent back to the message source.
func (c *Compress) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
c.mCount.Incr(1)
newMsg := msg.Copy()
proc := func(i int, span *tracing.Span, part types.Part) error {
newBytes, err := c.comp(c.conf.Level, part.Get())
if err == nil {
part.Set(newBytes)
} else {
c.log.Errorf("Failed to compress message part: %v\n", err)
c.mErr.Incr(1)
return err
}
return nil
}
if newMsg.Len() == 0 {
return nil, response.NewAck()
}
IteratePartsWithSpanV2(TypeCompress, c.conf.Parts, newMsg, proc)
c.mBatchSent.Incr(1)
c.mSent.Incr(int64(newMsg.Len()))
msgs := [1]types.Message{newMsg}
return msgs[:], nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (c *Compress) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (c *Compress) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------