Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix setting lz4 compression levels #781

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pkg/kgo/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
// for that batch.
type CompressionCodec struct {
codec codecType
level int8
level int
}

// NoCompression is a compression option that avoids compression. This can
Expand All @@ -68,10 +68,7 @@ func ZstdCompression() CompressionCodec { return CompressionCodec{codecZstd, 0}
//
// If the level is invalid, compressors just use a default level.
func (c CompressionCodec) WithLevel(level int) CompressionCodec {
if level > 127 {
level = 127 // lz4 could theoretically be large, I guess
}
c.level = int8(level)
c.level = level
return c
}

Expand Down Expand Up @@ -116,8 +113,8 @@ out:
case codecGzip:
level := gzip.DefaultCompression
if codec.level != 0 {
if _, err := gzip.NewWriterLevel(nil, int(codec.level)); err != nil {
level = int(codec.level)
if _, err := gzip.NewWriterLevel(nil, codec.level); err != nil {
level = codec.level
}
}
c.gzPool = sync.Pool{New: func() any { c, _ := gzip.NewWriterLevel(nil, level); return c }}
Expand Down
14 changes: 14 additions & 0 deletions pkg/kgo/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ import (
"reflect"
"sync"
"testing"

"github.com/pierrec/lz4/v4"
)

// Regression test for #778.
func TestCompressionCodecLZ4WithSpecifiedLevel(t *testing.T) {
t.Parallel()

codec := Lz4Compression().WithLevel(512)
w := lz4.NewWriter(new(bytes.Buffer))
err := w.Apply(lz4.CompressionLevelOption(lz4.CompressionLevel(codec.level)))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}

func TestNewCompressor(t *testing.T) {
t.Parallel()
for i, test := range []struct {
Expand Down