forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snappy.go
92 lines (77 loc) · 1.8 KB
/
snappy.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
package snappy
import (
"bytes"
"encoding/binary"
"github.com/golang/snappy"
kafka "github.com/segmentio/kafka-go"
)
func init() {
kafka.RegisterCompressionCodec(func() kafka.CompressionCodec {
return NewCompressionCodec()
})
}
type CompressionCodec struct {
writer *snappy.Writer
}
const Code = 2
func NewCompressionCodec() CompressionCodec {
return CompressionCodec{}
}
// Code implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Code() int8 {
return Code
}
// Encode implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Encode(dst, src []byte) (int, error) {
if n := snappy.MaxEncodedLen(len(src)); n > len(dst) {
buf := buffer{data: dst}
return buf.Write(snappy.Encode(nil, src))
}
return len(snappy.Encode(dst, src)), nil
}
// Decode implements the kafka.CompressionCodec interface.
func (c CompressionCodec) Decode(dst, src []byte) (int, error) {
buf := buffer{data: dst}
data, err := decode(src)
if err != nil {
return 0, err
}
return buf.Write(data)
}
var xerialHeader = []byte{130, 83, 78, 65, 80, 80, 89, 0}
// From github.com/eapache/go-xerial-snappy
func decode(src []byte) ([]byte, error) {
if !bytes.Equal(src[:8], xerialHeader) {
return snappy.Decode(nil, src)
}
var (
pos = uint32(16)
max = uint32(len(src))
dst = make([]byte, 0, len(src))
chunk []byte
err error
)
for pos < max {
size := binary.BigEndian.Uint32(src[pos : pos+4])
pos += 4
chunk, err = snappy.Decode(chunk, src[pos:pos+size])
if err != nil {
return nil, err
}
pos += size
dst = append(dst, chunk...)
}
return dst, nil
}
type buffer struct {
data []byte
size int
}
func (buf *buffer) Write(b []byte) (int, error) {
n := copy(buf.data[buf.size:], b)
buf.size += n
if n != len(b) {
return n, bytes.ErrTooLarge
}
return n, nil
}