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

Add LZ4 compressor support. #324

Merged
merged 4 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.3.0
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/parnurzeal/gorequest v0.2.16
github.com/pierrec/lz4/v4 v4.1.17
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/common v0.32.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4 v2.5.2+incompatible h1:WCjObylUIOlKy/+7Abdn34TLIkXiA4UWUMhxq9m9ZXI=
github.com/pierrec/lz4 v2.5.2+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 h1:+FZIDR/D97YOPik4N4lPDaUcLDF/EQPogxtlHB2ZZRM=
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
Expand Down
39 changes: 39 additions & 0 deletions pkg/compressor/lz4_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,42 @@
*/

package compressor

import (
"fmt"

"github.com/pierrec/lz4/v4"
WyattJia marked this conversation as resolved.
Show resolved Hide resolved
)

type Lz4 struct {
}

func (l *Lz4) Compress(data []byte) ([]byte, error) {

buffer := make([]byte, lz4.CompressBlockBound(len(data)))

var compressor lz4.Compressor

n, err := compressor.CompressBlock(data, buffer)
if err != nil {
return nil, err
}
if n >= len(data) {
return nil, fmt.Errorf("`%s` is not compressible", string(data))
}

return buffer[:n], nil
}

func (l *Lz4) Decompress(in []byte) ([]byte, error) {
out := make([]byte, 100*len(in))
n, err := lz4.UncompressBlock(in, out)
if err != nil {
return nil, err
}
return out[:n], nil
}

func (l *Lz4) GetCompressorType() CompressorType {
return CompressorLz4
}
40 changes: 40 additions & 0 deletions pkg/compressor/lz4_compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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"
WyattJia marked this conversation as resolved.
Show resolved Hide resolved

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

func TestLz4Compress(t *testing.T) {
sample := strings.Repeat("hello world", 100)

lz4 := Lz4{}

compressResult, err := lz4.Compress([]byte(sample))
assert.NoError(t, err)
t.Logf("Compressed result: %v", string(compressResult))

decompressResult, err := lz4.Decompress(compressResult)
WyattJia marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
assert.Equal(t, sample, string(decompressResult))
t.Logf("Decompressed result: %v", string(decompressResult))
}
3 changes: 2 additions & 1 deletion pkg/datasource/sql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ func (c *Conn) Begin() (driver.Tx, error) {
}

// BeginTx Open a transaction and judge whether the current transaction needs to open a
// global transaction according to ctx. If so, it needs to be included in the transaction management of seata
//
// global transaction according to ctx. If so, it needs to be included in the transaction management of seata
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if conn, ok := c.targetConn.(driver.ConnBeginTx); ok {
tx, err := conn.BeginTx(ctx, opts)
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/datasource/datasource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (dm *BasicSourceManager) BranchRegister(ctx context.Context, clientId strin
return 0, nil
}

// Branch report
// Branch report
func (dm *BasicSourceManager) BranchReport(ctx context.Context, req message.BranchReportRequest) error {
return nil
}
Expand All @@ -138,7 +138,7 @@ func (dm *BasicSourceManager) RegisterResource(resource rm.Resource) error {
return nil
}

// Unregister a model.Resource from the model.Resource Manager
// Unregister a model.Resource from the model.Resource Manager
func (dm *BasicSourceManager) UnregisterResource(resource rm.Resource) error {
return errors.New("unsupport unregister resource")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/datasource/sql/types/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package types
import "github.com/arana-db/parser/ast"

// ExecutorType
//
//go:generate stringer -type=ExecutorType
type ExecutorType int32

Expand Down