From 0acee46e583efbac06a9c2a013f277733bbcfc1b Mon Sep 17 00:00:00 2001 From: liushao <505786909@qq.com> Date: Mon, 17 Oct 2022 12:07:57 +0800 Subject: [PATCH 1/4] feat(compressor): deflate compress 1. Optimize compressor type definition. (Don't start with package name). 2. Implement deflate compressor and ut. close #312 --- pkg/compressor/compressor.go | 16 +----- pkg/compressor/compresstype_string.go | 34 +++++++++++++ pkg/compressor/defalte_compress_test.go | 40 +++++++++++++++ pkg/compressor/deflate_compress.go | 51 +++++++++++++++++++ .../{deflater_compress.go => types.go} | 15 ++++++ 5 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 pkg/compressor/compresstype_string.go create mode 100644 pkg/compressor/defalte_compress_test.go create mode 100644 pkg/compressor/deflate_compress.go rename pkg/compressor/{deflater_compress.go => types.go} (79%) diff --git a/pkg/compressor/compressor.go b/pkg/compressor/compressor.go index dadd530b3..a08412b70 100644 --- a/pkg/compressor/compressor.go +++ b/pkg/compressor/compressor.go @@ -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 } diff --git a/pkg/compressor/compresstype_string.go b/pkg/compressor/compresstype_string.go new file mode 100644 index 000000000..b268cef52 --- /dev/null +++ b/pkg/compressor/compresstype_string.go @@ -0,0 +1,34 @@ +/* + * 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. + */ + +// Code generated by "stringer -type=CompressType"; DO NOT EDIT. + +package compressor + +import "strconv" + +const _CompressType_name = "CompressGzipCompressZipCompressSevenzCompressBzip2CompressLz4CompressDeflateCompressZstdCompressMax" + +var _CompressType_index = [...]uint8{0, 12, 23, 37, 50, 61, 76, 88, 99} + +func (i CompressType) String() string { + i -= 1 + if i < 0 || i >= CompressType(len(_CompressType_index)-1) { + return "CompressType(" + strconv.FormatInt(int64(i+1), 10) + ")" + } + return _CompressType_name[_CompressType_index[i]:_CompressType_index[i+1]] +} diff --git a/pkg/compressor/defalte_compress_test.go b/pkg/compressor/defalte_compress_test.go new file mode 100644 index 000000000..3158d6ac3 --- /dev/null +++ b/pkg/compressor/defalte_compress_test.go @@ -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)) + dataCompressed, _ := dc.Compress(data) + fmt.Println(len(dataCompressed)) + ret, _ := dc.Decompress(dataCompressed) + fmt.Println(len(ret)) + assert.EqualValues(t, s.text, string(ret)) + } +} diff --git a/pkg/compressor/deflate_compress.go b/pkg/compressor/deflate_compress.go new file mode 100644 index 000000000..98ca11785 --- /dev/null +++ b/pkg/compressor/deflate_compress.go @@ -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 +} diff --git a/pkg/compressor/deflater_compress.go b/pkg/compressor/types.go similarity index 79% rename from pkg/compressor/deflater_compress.go rename to pkg/compressor/types.go index d1eea9879..963353560 100644 --- a/pkg/compressor/deflater_compress.go +++ b/pkg/compressor/types.go @@ -16,3 +16,18 @@ */ package compressor + +//go:generate stringer -type=CompressType +type CompressType int8 + +const ( + _ CompressType = iota + CompressGzip + CompressZip + CompressSevenz + CompressBzip2 + CompressLz4 + CompressDeflate + CompressZstd + CompressMax +) From df9a78eb895247d6cd84110830e43485a32a7737 Mon Sep 17 00:00:00 2001 From: liushao <505786909@qq.com> Date: Tue, 18 Oct 2022 12:10:20 +0800 Subject: [PATCH 2/4] refactor(compressor): revert compressor type definition --- pkg/compressor/compressor.go | 17 ++++++++++++- pkg/compressor/compresstype_string.go | 34 ------------------------- pkg/compressor/defalte_compress_test.go | 2 +- pkg/compressor/deflate_compress.go | 4 +-- pkg/compressor/types.go | 33 ------------------------ 5 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 pkg/compressor/compresstype_string.go delete mode 100644 pkg/compressor/types.go diff --git a/pkg/compressor/compressor.go b/pkg/compressor/compressor.go index a08412b70..fea7a1f7e 100644 --- a/pkg/compressor/compressor.go +++ b/pkg/compressor/compressor.go @@ -17,8 +17,23 @@ package compressor +type CompressorType int8 + +const ( + CompressorNone CompressorType = iota + CompressorGzip + CompressorZip + CompressorSevenz + CompressorBzip2 + CompressorLz4 + CompressorDefault + CompressorZstd + CompressorMax + CompressorDeflate +) + type Compressor interface { Compress([]byte) ([]byte, error) Decompress([]byte) ([]byte, error) - GetCompressorType() CompressType + GetCompressorType() CompressorType } diff --git a/pkg/compressor/compresstype_string.go b/pkg/compressor/compresstype_string.go deleted file mode 100644 index b268cef52..000000000 --- a/pkg/compressor/compresstype_string.go +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ - -// Code generated by "stringer -type=CompressType"; DO NOT EDIT. - -package compressor - -import "strconv" - -const _CompressType_name = "CompressGzipCompressZipCompressSevenzCompressBzip2CompressLz4CompressDeflateCompressZstdCompressMax" - -var _CompressType_index = [...]uint8{0, 12, 23, 37, 50, 61, 76, 88, 99} - -func (i CompressType) String() string { - i -= 1 - if i < 0 || i >= CompressType(len(_CompressType_index)-1) { - return "CompressType(" + strconv.FormatInt(int64(i+1), 10) + ")" - } - return _CompressType_name[_CompressType_index[i]:_CompressType_index[i+1]] -} diff --git a/pkg/compressor/defalte_compress_test.go b/pkg/compressor/defalte_compress_test.go index 3158d6ac3..f8205931f 100644 --- a/pkg/compressor/defalte_compress_test.go +++ b/pkg/compressor/defalte_compress_test.go @@ -26,7 +26,7 @@ func TestDeflateCompress(t *testing.T) { } dc := &DeflateCompress{} - assert.EqualValues(t, CompressDeflate, dc.GetCompressorType()) + assert.EqualValues(t, CompressorDeflate, dc.GetCompressorType()) for _, s := range ts { var data []byte = []byte(s.text) diff --git a/pkg/compressor/deflate_compress.go b/pkg/compressor/deflate_compress.go index 98ca11785..ed53b4f66 100644 --- a/pkg/compressor/deflate_compress.go +++ b/pkg/compressor/deflate_compress.go @@ -46,6 +46,6 @@ func (*DeflateCompress) Decompress(data []byte) ([]byte, error) { return io.ReadAll(fr) } -func (*DeflateCompress) GetCompressorType() CompressType { - return CompressDeflate +func (*DeflateCompress) GetCompressorType() CompressorType { + return CompressorDeflate } diff --git a/pkg/compressor/types.go b/pkg/compressor/types.go deleted file mode 100644 index 963353560..000000000 --- a/pkg/compressor/types.go +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 - -//go:generate stringer -type=CompressType -type CompressType int8 - -const ( - _ CompressType = iota - CompressGzip - CompressZip - CompressSevenz - CompressBzip2 - CompressLz4 - CompressDeflate - CompressZstd - CompressMax -) From b324205393fd3670ece8f25a65e2c13c9ff1ecd3 Mon Sep 17 00:00:00 2001 From: liushao <505786909@qq.com> Date: Tue, 18 Oct 2022 12:12:04 +0800 Subject: [PATCH 3/4] refactor(compressor): remove fmt.Println in ut --- pkg/compressor/defalte_compress_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/compressor/defalte_compress_test.go b/pkg/compressor/defalte_compress_test.go index f8205931f..95757273d 100644 --- a/pkg/compressor/defalte_compress_test.go +++ b/pkg/compressor/defalte_compress_test.go @@ -1,7 +1,6 @@ package compressor import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -30,11 +29,8 @@ func TestDeflateCompress(t *testing.T) { for _, s := range ts { var data []byte = []byte(s.text) - fmt.Println(len(data)) dataCompressed, _ := dc.Compress(data) - fmt.Println(len(dataCompressed)) ret, _ := dc.Decompress(dataCompressed) - fmt.Println(len(ret)) assert.EqualValues(t, s.text, string(ret)) } } From cd5a5a3b2aa64bb899fdb1396de7d4045674d937 Mon Sep 17 00:00:00 2001 From: liushao <505786909@qq.com> Date: Sun, 23 Oct 2022 11:18:52 +0800 Subject: [PATCH 4/4] refactor(compressor): adjust the order of deflate compressor constants. --- pkg/compressor/compressor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/compressor/compressor.go b/pkg/compressor/compressor.go index fea7a1f7e..5b30e2a35 100644 --- a/pkg/compressor/compressor.go +++ b/pkg/compressor/compressor.go @@ -28,8 +28,8 @@ const ( CompressorLz4 CompressorDefault CompressorZstd - CompressorMax CompressorDeflate + CompressorMax ) type Compressor interface {