-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
251 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package compress | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// BenchmarkNew-24 55165 22851 ns/op 23884 B/op 2 allocs/op | ||
func BenchmarkNew(b *testing.B) { | ||
b.ReportAllocs() | ||
c, _ := New(CompressionAlgoZstd, CompressionLevelZstdBest) | ||
defer func() { _ = c.Close() }() | ||
|
||
for i := 0; i < b.N; i++ { | ||
r, _ := c.Compress(loremIpsumDolor) | ||
_, _ = c.Decompress(r) | ||
} | ||
} |
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,154 @@ | ||
package compress | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
// CompressionAlgorithm is the interface that wraps the compression algorithm method. | ||
type CompressionAlgorithm int | ||
|
||
func (c CompressionAlgorithm) String() string { | ||
switch c { | ||
case CompressionAlgoZstd: | ||
return "zstd" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (c CompressionAlgorithm) isValid() bool { | ||
return c == CompressionAlgoZstd | ||
} | ||
|
||
func NewCompressionAlgorithm(s string) (CompressionAlgorithm, error) { | ||
switch s { | ||
case "zstd": | ||
return CompressionAlgoZstd, nil | ||
default: | ||
return 0, fmt.Errorf("unknown compression algorithm: %s", s) | ||
} | ||
} | ||
|
||
// CompressionLevel is the interface that wraps the compression level method. | ||
type CompressionLevel int | ||
|
||
func (c CompressionLevel) String() string { | ||
switch c { | ||
case CompressionLevelZstdFastest: | ||
return "fastest" | ||
case CompressionLevelZstdDefault: | ||
return "default" | ||
case CompressionLevelZstdBetter: | ||
return "better" | ||
case CompressionLevelZstdBest: | ||
return "best" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (c CompressionLevel) isValid() bool { | ||
switch c { | ||
case CompressionLevelZstdFastest, | ||
CompressionLevelZstdDefault, | ||
CompressionLevelZstdBetter, | ||
CompressionLevelZstdBest: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func NewCompressionLevel(s string) (CompressionLevel, error) { | ||
switch s { | ||
case "fastest": | ||
return CompressionLevelZstdFastest, nil | ||
case "default": | ||
return CompressionLevelZstdDefault, nil | ||
case "better": | ||
return CompressionLevelZstdBetter, nil | ||
case "best": | ||
return CompressionLevelZstdBest, nil | ||
default: | ||
return 0, fmt.Errorf("unknown compression level: %s", s) | ||
} | ||
} | ||
|
||
var ( | ||
CompressionAlgoZstd = CompressionAlgorithm(1) | ||
|
||
CompressionLevelZstdFastest = CompressionLevel(zstd.SpeedFastest) | ||
CompressionLevelZstdDefault = CompressionLevel(zstd.SpeedDefault) // "pretty fast" compression | ||
CompressionLevelZstdBetter = CompressionLevel(zstd.SpeedBetterCompression) | ||
CompressionLevelZstdBest = CompressionLevel(zstd.SpeedBestCompression) | ||
) | ||
|
||
func New(algo CompressionAlgorithm, level CompressionLevel) (*Compressor, error) { | ||
if !algo.isValid() { | ||
return nil, fmt.Errorf("invalid compression algorithm: %d", algo) | ||
} | ||
if !level.isValid() { | ||
return nil, fmt.Errorf("invalid compression level: %d", level) | ||
} | ||
|
||
encoder, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.EncoderLevel(level))) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create zstd encoder: %w", err) | ||
} | ||
|
||
decoder, err := zstd.NewReader(nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create zstd decoder: %w", err) | ||
} | ||
|
||
return &Compressor{ | ||
encoder: encoder, | ||
decoder: decoder, | ||
}, nil | ||
} | ||
|
||
type Compressor struct { | ||
encoder *zstd.Encoder | ||
decoder *zstd.Decoder | ||
} | ||
|
||
func (c *Compressor) Compress(src []byte) ([]byte, error) { | ||
return c.encoder.EncodeAll(src, nil), nil | ||
} | ||
|
||
func (c *Compressor) Decompress(src []byte) ([]byte, error) { | ||
return c.decoder.DecodeAll(src, nil) | ||
} | ||
|
||
func (c *Compressor) Close() error { | ||
c.decoder.Close() | ||
return c.encoder.Close() | ||
} | ||
|
||
// SerializeSettings serializes the compression settings. | ||
func SerializeSettings(algo CompressionAlgorithm, level CompressionLevel) string { | ||
return fmt.Sprintf("%d:%d", algo, level) | ||
} | ||
|
||
// DeserializeSettings deserializes the compression settings. | ||
func DeserializeSettings(s string) (CompressionAlgorithm, CompressionLevel, error) { | ||
var algoInt, levelInt int | ||
_, err := fmt.Sscanf(s, "%d:%d", &algoInt, &levelInt) | ||
if err != nil { | ||
return 0, 0, fmt.Errorf("cannot deserialize settings: %w", err) | ||
} | ||
|
||
algo := CompressionAlgorithm(algoInt) | ||
if !algo.isValid() { | ||
return 0, 0, fmt.Errorf("invalid compression algorithm: %d", algoInt) | ||
} | ||
|
||
level := CompressionLevel(levelInt) | ||
if !level.isValid() { | ||
return 0, 0, fmt.Errorf("invalid compression level: %d", levelInt) | ||
} | ||
|
||
return algo, level, nil | ||
} |
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,75 @@ | ||
package compress | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var loremIpsumDolor = []byte(`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. | ||
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. | ||
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`) | ||
|
||
func TestCompress(t *testing.T) { | ||
compressionLevels := []CompressionLevel{ | ||
CompressionLevelZstdFastest, | ||
CompressionLevelZstdDefault, | ||
CompressionLevelZstdBetter, | ||
CompressionLevelZstdBest, | ||
} | ||
for _, level := range compressionLevels { | ||
c, err := New(CompressionAlgoZstd, level) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { _ = c.Close() }) | ||
|
||
compressed, err := c.Compress(loremIpsumDolor) | ||
require.NoError(t, err) | ||
require.Less(t, len(compressed), len(loremIpsumDolor)) | ||
|
||
decompressed, err := c.Decompress(compressed) | ||
require.NoError(t, err) | ||
require.Equal(t, string(loremIpsumDolor), string(decompressed)) | ||
} | ||
} | ||
|
||
func TestSerialization(t *testing.T) { | ||
algo, err := NewCompressionAlgorithm("zstd") | ||
require.NoError(t, err) | ||
require.Equal(t, CompressionAlgoZstd, algo) | ||
|
||
level, err := NewCompressionLevel("best") | ||
require.NoError(t, err) | ||
require.Equal(t, CompressionLevelZstdBest, level) | ||
|
||
serialized := SerializeSettings(algo, level) | ||
require.Equal(t, "1:4", serialized) | ||
|
||
algo, level, err = DeserializeSettings(serialized) | ||
require.NoError(t, err) | ||
require.Equal(t, CompressionAlgoZstd, algo) | ||
require.Equal(t, CompressionLevelZstdBest, level) | ||
} | ||
|
||
func TestDeserializationError(t *testing.T) { | ||
// valid algo is 1 | ||
// valid level is 1-4 | ||
testCases := []string{ | ||
"0:0", "0:1", "1:0", "2:1", "1:5", | ||
} | ||
for _, tc := range testCases { | ||
_, _, err := DeserializeSettings(tc) | ||
require.Error(t, err) | ||
} | ||
} | ||
|
||
func TestNewError(t *testing.T) { | ||
c, err := New(CompressionAlgorithm(0), CompressionLevelZstdDefault) | ||
require.Nil(t, c) | ||
require.Error(t, err) | ||
|
||
c, err = New(CompressionAlgoZstd, CompressionLevel(0)) | ||
require.Nil(t, c) | ||
require.Error(t, err) | ||
} |
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