Browse Source

add zstd compress (#327)

zstd compress

Co-authored-by: xubaisheng <>
tags/1.0.2-RC1
Bison Xu GitHub 2 years ago
parent
commit
bb4c31e97f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 0 deletions
  1. +1
    -0
      go.mod
  2. +2
    -0
      go.sum
  3. +26
    -0
      pkg/compressor/zstd_compress.go
  4. +48
    -0
      pkg/compressor/zstd_compress_test.go

+ 1
- 0
go.mod View File

@@ -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


+ 2
- 0
go.sum View File

@@ -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=


+ 26
- 0
pkg/compressor/zstd_compress.go View File

@@ -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
- 0
pkg/compressor/zstd_compress_test.go View File

@@ -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))
}
}

Loading…
Cancel
Save