forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzstd.go
50 lines (40 loc) · 1.14 KB
/
zstd.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
// +build cgo
package zstd
import (
"github.com/DataDog/zstd"
"github.com/segmentio/kafka-go"
)
func init() {
kafka.RegisterCompressionCodec(func() kafka.CompressionCodec {
return NewCompressionCodec()
})
}
type CompressionCodec struct {
// CompressionLevel is the level of compression to use on messages.
CompressionLevel int
}
const (
Code int8 = 4
// https://github.com/DataDog/zstd/blob/1e382f59b41eebd6f592c5db4fd1958ec38a0eba/zstd.go#L33
DefaultCompressionLevel int = 5
)
func NewCompressionCodec() CompressionCodec {
return NewCompressionCodecWith(DefaultCompressionLevel)
}
func NewCompressionCodecWith(level int) CompressionCodec {
return CompressionCodec{
CompressionLevel: level,
}
}
// Code implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Code() int8 {
return Code
}
// Encode implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Encode(src []byte) ([]byte, error) {
return zstd.CompressLevel(nil, src, c.CompressionLevel)
}
// Decode implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Decode(src []byte) ([]byte, error) {
return zstd.Decompress(nil, src)
}