Skip to content

Commit

Permalink
fix setting lz4 compression levels
Browse files Browse the repository at this point in the history
  • Loading branch information
asg0451 committed Jul 17, 2024
1 parent a5f2b71 commit d7ea2c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 2 additions & 5 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
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

0 comments on commit d7ea2c3

Please sign in to comment.