Skip to content

Commit

Permalink
add zstd compress (#327)
Browse files Browse the repository at this point in the history
zstd compress

Co-authored-by: xubaisheng <>
  • Loading branch information
betterwinsone authored Oct 28, 2022
1 parent 5eb6073 commit bb4c31e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/klauspost/compress v1.15.11
github.com/knadh/koanf v1.4.3 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/knadh/koanf v1.4.3 h1:rSJcSH5LSFhvzBRsAYfT3k7eLP0I4UxeZqjtAatk+wc=
github.com/knadh/koanf v1.4.3/go.mod h1:5FAkuykKXZvLqhAbP4peWgM5CTcZmn7L1d27k/a+kfg=
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7/go.mod h1:Y2SaZf2Rzd0pXkLVhLlCiAXFCLSXAIbTKDivVgff/AM=
Expand Down
26 changes: 26 additions & 0 deletions pkg/compressor/zstd_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@
*/

package compressor

import (
"github.com/klauspost/compress/zstd"
)

type Zstd struct{}

func (z Zstd) Compress(data []byte) ([]byte, error) {
var encoder, err = zstd.NewWriter(nil)
if err != nil {
return nil, err
}
return encoder.EncodeAll(data, make([]byte, 0, len(data))), nil
}

func (z Zstd) Decompress(data []byte) ([]byte, error) {
var decoder, err = zstd.NewReader(nil)
if err != nil {
return nil, err
}
return decoder.DecodeAll(data, nil)
}

func (z Zstd) GetCompressorType() CompressorType {
return CompressorZstd
}
48 changes: 48 additions & 0 deletions pkg/compressor/zstd_compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compressor

import (
"strings"
"testing"

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

func TestZstdCompress(t *testing.T) {
ts := []struct {
text string
}{
{
text: strings.Repeat("Don't communicate by sharing memory, share memory by communicating.", 1000),
},
{
text: "88888msj0*&^^%$$#$@!~jjdjfjdlfjkhhdh//><|}{{|\"",
},
}

dc := &Zstd{}
assert.EqualValues(t, CompressorZstd, dc.GetCompressorType())

for _, s := range ts {
var data = []byte(s.text)
dataCompressed, _ := dc.Compress(data)
ret, _ := dc.Decompress(dataCompressed)
assert.EqualValues(t, s.text, string(ret))
}
}

0 comments on commit bb4c31e

Please sign in to comment.