forked from IBM/sarama
-
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.
Merge pull request IBM#2045 from honeycombio/lizf.zstd-compress-options
[zstd] pass level param through to compress/zstd encoder
- Loading branch information
Showing
3 changed files
with
42 additions
and
10 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
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 |
---|---|---|
@@ -1,18 +1,50 @@ | ||
package sarama | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
var ( | ||
zstdDec, _ = zstd.NewReader(nil) | ||
zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true)) | ||
) | ||
type ZstdEncoderParams struct { | ||
Level int | ||
} | ||
type ZstdDecoderParams struct { | ||
} | ||
|
||
var zstdEncMap, zstdDecMap sync.Map | ||
|
||
func getEncoder(params ZstdEncoderParams) *zstd.Encoder { | ||
if ret, ok := zstdEncMap.Load(params); ok { | ||
return ret.(*zstd.Encoder) | ||
} | ||
// It's possible to race and create multiple new writers. | ||
// Only one will survive GC after use. | ||
encoderLevel := zstd.SpeedDefault | ||
if params.Level != CompressionLevelDefault { | ||
encoderLevel = zstd.EncoderLevelFromZstd(params.Level) | ||
} | ||
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true), | ||
zstd.WithEncoderLevel(encoderLevel)) | ||
zstdEncMap.Store(params, zstdEnc) | ||
return zstdEnc | ||
} | ||
|
||
func getDecoder(params ZstdDecoderParams) *zstd.Decoder { | ||
if ret, ok := zstdDecMap.Load(params); ok { | ||
return ret.(*zstd.Decoder) | ||
} | ||
// It's possible to race and create multiple new readers. | ||
// Only one will survive GC after use. | ||
zstdDec, _ := zstd.NewReader(nil) | ||
zstdDecMap.Store(params, zstdDec) | ||
return zstdDec | ||
} | ||
|
||
func zstdDecompress(dst, src []byte) ([]byte, error) { | ||
return zstdDec.DecodeAll(src, dst) | ||
func zstdDecompress(params ZstdDecoderParams, dst, src []byte) ([]byte, error) { | ||
return getDecoder(params).DecodeAll(src, dst) | ||
} | ||
|
||
func zstdCompress(dst, src []byte) ([]byte, error) { | ||
return zstdEnc.EncodeAll(src, dst), nil | ||
func zstdCompress(params ZstdEncoderParams, dst, src []byte) ([]byte, error) { | ||
return getEncoder(params).EncodeAll(src, dst), nil | ||
} |