Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compressor): deflate compress #321

Merged
merged 4 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions pkg/compressor/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,8 @@

package compressor

type CompressorType int8

const (
CompressorNone CompressorType = iota
CompressorGzip
CompressorZip
CompressorSevenz
CompressorBzip2
CompressorLz4
CompressorDefault
CompressorZstd
CompressorMax
)

type Compressor interface {
Compress([]byte) ([]byte, error)
Decompress([]byte) ([]byte, error)
GetCompressorType() CompressorType
GetCompressorType() CompressType
}
34 changes: 34 additions & 0 deletions pkg/compressor/compresstype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions pkg/compressor/defalte_compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package compressor

import (
"fmt"
"testing"

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

func TestDeflateCompress(t *testing.T) {
ts := []struct {
text string
}{
{
text: "Don't communicate by sharing memory, share memory by communicating.",
},
{
text: "Concurrency is not parallelism.",
},
{
text: "The bigger the interface, the weaker the abstraction.",
},
{
text: "Documentation is for users.",
},
}

dc := &DeflateCompress{}
assert.EqualValues(t, CompressDeflate, dc.GetCompressorType())

for _, s := range ts {
var data []byte = []byte(s.text)
fmt.Println(len(data))
iSuperCoder marked this conversation as resolved.
Show resolved Hide resolved
dataCompressed, _ := dc.Compress(data)
fmt.Println(len(dataCompressed))
ret, _ := dc.Decompress(dataCompressed)
fmt.Println(len(ret))
assert.EqualValues(t, s.text, string(ret))
}
}
51 changes: 51 additions & 0 deletions pkg/compressor/deflate_compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 (
"bytes"
"compress/flate"
"io"

"github.com/seata/seata-go/pkg/util/log"
)

type DeflateCompress struct{}

func (*DeflateCompress) Compress(data []byte) ([]byte, error) {
var buf bytes.Buffer
fw, err := flate.NewWriter(&buf, flate.BestCompression)
if err != nil {
log.Error(err)
return nil, err
}
defer fw.Close()
fw.Write(data)
fw.Flush()
return buf.Bytes(), nil
}

func (*DeflateCompress) Decompress(data []byte) ([]byte, error) {
fr := flate.NewReader(bytes.NewBuffer(data))
defer fr.Close()
return io.ReadAll(fr)
}

func (*DeflateCompress) GetCompressorType() CompressType {
return CompressDeflate
}
15 changes: 15 additions & 0 deletions pkg/compressor/deflater_compress.go → pkg/compressor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@
*/

package compressor

//go:generate stringer -type=CompressType
type CompressType int8

const (
_ CompressType = iota
CompressGzip
CompressZip
iSuperCoder marked this conversation as resolved.
Show resolved Hide resolved
CompressSevenz
CompressBzip2
CompressLz4
CompressDeflate
CompressZstd
CompressMax
)