Skip to content

Commit

Permalink
zstd: use pure go implementation
Browse files Browse the repository at this point in the history
use github.com/klauspost/compress/zstd package for zstd
compression and decompression

remove github.com/Datadog/zstd
  • Loading branch information
LeGEC committed Nov 11, 2022
1 parent d7d4b58 commit eff9c38
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.4.1
github.com/Azure/go-autorest/autorest v0.11.21
github.com/DataDog/zstd v1.5.3-0.20220606203749-fd035e54e312
github.com/RoaringBitmap/roaring v0.4.21
github.com/aws/aws-sdk-go v1.44.7
github.com/blang/semver v3.5.1+incompatible
Expand Down Expand Up @@ -59,6 +58,7 @@ require (
require (
github.com/cactus/go-statsd-client/v5 v5.0.0
github.com/google/brotli/go/cbrotli v0.0.0-20220110100810-f4153a09f87c
github.com/klauspost/compress v1.15.12
github.com/pkg/profile v1.6.0
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0
Expand Down Expand Up @@ -114,7 +114,6 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmoiron/sqlx v1.3.3 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/zstd v1.5.3-0.20220606203749-fd035e54e312 h1:MB4OhDISHzUx+9VIhkCinijzgGAsyrFqko4PjPjJmbw=
github.com/DataDog/zstd v1.5.3-0.20220606203749-fd035e54e312/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -379,8 +377,9 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
12 changes: 7 additions & 5 deletions internal/compression/zstd/compressor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
//go:build !(arm64 && darwin)
// +build !arm64 !darwin

package zstd

import (
"io"

"github.com/DataDog/zstd"
"github.com/klauspost/compress/zstd"
)

const (
Expand All @@ -17,7 +14,12 @@ const (
type Compressor struct{}

func (compressor Compressor) NewWriter(writer io.Writer) io.WriteCloser {
return zstd.NewWriterLevel(writer, 3)
zw, err := zstd.NewWriter(writer, zstd.WithEncoderLevel(zstd.SpeedDefault))
if err != nil {
panic(err)
}

return zw
}

func (compressor Compressor) FileExtension() string {
Expand Down
73 changes: 73 additions & 0 deletions internal/compression/zstd/compressor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package zstd

import (
"bytes"
"io"
"math/rand"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCompressDecompress(t *testing.T) {
type testCase struct {
name string
input string
}

seed := int64(0x1337c0deb357beef)
if rand, ok := os.LookupEnv("WALG_RANDOMIZE_TEST"); ok {
if rand != "" && rand != "0" {
seed = time.Now().UnixNano()
t.Logf("randomized seed: %x", seed)
}
}

var buff = make([]byte, 4096)
rand.New(rand.NewSource(seed)).Read(buff)

testcases := []*testCase{
{
name: "simple input",
input: "How much wood could a woodchuck chuck if a woodchuck could chuck wood ?",
},
{
name: "random data",
input: string(buff),
},
}

for _, tc := range testcases {
in := []byte(tc.input)

var comp bytes.Buffer
wc := Compressor{}.NewWriter(&comp)

var err error
_, err = wc.Write(in)
require.NoError(t, err, tc.name)

err = wc.Close()
require.NoError(t, err, tc.name)

rdr, err := Decompressor{}.Decompress(&comp)
require.NoError(t, err, tc.name)

var decomp bytes.Buffer
_, err = io.Copy(&decomp, rdr)
require.NoError(t, err)

err = rdr.Close()
require.NoError(t, err)

if !bytes.Equal(in, decomp.Bytes()) {
assert.Fail(t, "decompressed data doesn't match expected input", "testCase: %s", tc.name)
if tc.name == "random data" {
t.Log("random seed =", seed)
}
}
}
}
8 changes: 3 additions & 5 deletions internal/compression/zstd/decompressor.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//go:build !(arm64 && darwin)
// +build !arm64 !darwin

package zstd

import (
"io"

"github.com/DataDog/zstd"
"github.com/klauspost/compress/zstd"
"github.com/wal-g/wal-g/internal/compression/computils"
)

type Decompressor struct{}

func (decompressor Decompressor) Decompress(src io.Reader) (io.ReadCloser, error) {
return zstd.NewReader(computils.NewUntilEOFReader(src)), nil
zstdReader, err := zstd.NewReader(computils.NewUntilEOFReader(src))
return zstdReader.IOReadCloser(), err
}

func (decompressor Decompressor) FileExtension() string {
Expand Down
4 changes: 0 additions & 4 deletions internal/compression/zstd_enabled.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//go:build !windows && !(arm64 && darwin)
// +build !windows
// +build !arm64 !darwin

package compression

import (
Expand Down

0 comments on commit eff9c38

Please sign in to comment.