forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
217 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package kafka | ||
|
||
const ( | ||
CompressionNone int8 = iota | ||
CompressionGZIP | ||
CompressionSnappy | ||
CompressionLZ4 | ||
) | ||
|
||
type CompressionCodec struct { | ||
str func() string | ||
encode func(src []byte) ([]byte, error) | ||
decode func(src []byte) ([]byte, error) | ||
} | ||
|
||
const compressionCodecMask int8 = 0x03 | ||
const defaultCompressionLevel int = -1 | ||
|
||
func init() { | ||
RegisterCompressionCodec(0, | ||
func() string { return "none" }, | ||
func(src []byte) ([]byte, error) { return src, nil }, | ||
func(src []byte) ([]byte, error) { return src, nil }, | ||
) | ||
} | ||
|
||
func codecToStr(codec int8) string { | ||
switch codec { | ||
case CompressionNone: | ||
return "none" | ||
case CompressionGZIP: | ||
return "gzip" | ||
case CompressionSnappy: | ||
return "snappy" | ||
case CompressionLZ4: | ||
return "lz4" | ||
default: | ||
return "unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package kafka_test | ||
|
||
import ( | ||
"testing" | ||
|
||
kafka "github.com/segmentio/kafka-go" | ||
_ "github.com/segmentio/kafka-go/gzip" | ||
) | ||
|
||
func TestCompression(t *testing.T) { | ||
msg := kafka.Message{ | ||
Value: []byte("message"), | ||
} | ||
|
||
testEncodeDecode(t, msg, kafka.CompressionNone) | ||
testEncodeDecode(t, msg, kafka.CompressionGZIP) | ||
} | ||
|
||
func testEncodeDecode(t *testing.T, m kafka.Message, codec int8) { | ||
var r1, r2 kafka.Message | ||
var err error | ||
|
||
t.Run("encode with "+codecToStr(codec), func(t *testing.T) { | ||
m.CompressionCodec = codec | ||
r1, err = m.Encode() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
|
||
t.Run("encode with "+codecToStr(codec), func(t *testing.T) { | ||
r2, err = r1.Decode() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if string(r2.Value) != "message" { | ||
t.Error("bad message") | ||
t.Log("got: ", string(m.Value)) | ||
t.Log("expected: message") | ||
} | ||
}) | ||
} | ||
|
||
func codecToStr(codec int8) string { | ||
switch codec { | ||
case kafka.CompressionNone: | ||
return "none" | ||
case kafka.CompressionGZIP: | ||
return "gzip" | ||
case kafka.CompressionSnappy: | ||
return "snappy" | ||
case kafka.CompressionLZ4: | ||
return "lz4" | ||
default: | ||
return "unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package gzip | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io/ioutil" | ||
|
||
kafka "github.com/segmentio/kafka-go" | ||
) | ||
|
||
func init() { | ||
kafka.RegisterCompressionCodec(1, String, Encode, Decode) | ||
} | ||
|
||
func Code() int8 { | ||
return 1 | ||
} | ||
|
||
func String() string { | ||
return "gzip" | ||
} | ||
|
||
//TODO: compression level | ||
func Encode(src []byte) ([]byte, error) { | ||
var buf bytes.Buffer | ||
writer := gzip.NewWriter(&buf) | ||
_, err := writer.Write(src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = writer.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func Decode(src []byte) ([]byte, error) { | ||
reader, err := gzip.NewReader(bytes.NewReader(src)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ioutil.ReadAll(reader) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package gzip | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestGzip(t *testing.T) { | ||
var r1, r2 []byte | ||
var err error | ||
payload := []byte("message") | ||
|
||
t.Run("encode", func(t *testing.T) { | ||
r1, err = Encode(payload) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if bytes.Equal(payload, r1) { | ||
t.Error("failed to encode payload") | ||
t.Log("got: ", r1) | ||
} | ||
}) | ||
|
||
t.Run("decode", func(t *testing.T) { | ||
r2, err = Decode(r1) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if !bytes.Equal(payload, r2) { | ||
t.Error("failed to decode payload") | ||
t.Log("expected: ", payload) | ||
t.Log("got: ", r2) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.