-
Notifications
You must be signed in to change notification settings - Fork 1
/
compressor.go
143 lines (129 loc) · 3.58 KB
/
compressor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2021 tree xie
//
// Licensed 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 cache
import (
"github.com/golang/snappy"
"github.com/klauspost/compress/zstd"
)
// Compressor is the interface that support
// encode and decode function for compression.
//
// Match returns the size is matched for compress
type Compressor interface {
// Match tests the data should be compressed
Match(size int) (matched bool)
// Encode encodes the data
Encode(data []byte) ([]byte, error)
// Decode decodes the data
Decode(data []byte) ([]byte, error)
}
type CompressorOption struct {
// MinCompressLength min compress length
MinCompressLength int
// Encode encode function
Encode func(data []byte) ([]byte, error)
// Decode decode function
Decode func(data []byte) ([]byte, error)
}
func snappyEncode(data []byte) ([]byte, error) {
dst := []byte{}
dst = snappy.Encode(dst, data)
return dst, nil
}
func snappyDecode(data []byte) ([]byte, error) {
var dst []byte
return snappy.Decode(dst, data)
}
func zstdEncode(data []byte, level int) ([]byte, error) {
encoder, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.EncoderLevel(level)))
if err != nil {
return nil, err
}
data = encoder.EncodeAll(data, make([]byte, 0, len(data)))
return data, nil
}
func zstdDecode(data []byte) ([]byte, error) {
decoder, err := zstd.NewReader(nil)
if err != nil {
return nil, err
}
return decoder.DecodeAll(data, nil)
}
type compressor struct {
minCompressLength int
encode func(data []byte) ([]byte, error)
decode func(data []byte) ([]byte, error)
}
func (c *compressor) Encode(data []byte) ([]byte, error) {
size := len(data)
// 不做压缩
compressType := CompressNone
if c.Match(size) {
compressType = Compressed
buf, err := c.encode(data)
if err != nil {
return nil, err
}
data = buf
size = len(data)
}
newData := make([]byte, size+1)
newData[0] = compressType
copy(newData[1:], data)
return newData, nil
}
func (c *compressor) Decode(data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, nil
}
compressType := data[0]
data = data[1:]
if compressType != CompressNone {
buf, err := c.decode(data)
if err != nil {
return nil, err
}
data = buf
}
return data, nil
}
func (c *compressor) Match(size int) bool {
return size > c.minCompressLength
}
// NewCompressor creates a new compressor
func NewCompressor(opt CompressorOption) Compressor {
return &compressor{
minCompressLength: opt.MinCompressLength,
encode: opt.Encode,
decode: opt.Decode,
}
}
// NewZSTDCompressor creates a zstd compressor
func NewZSTDCompressor(minCompressLength, level int) Compressor {
return NewCompressor(CompressorOption{
MinCompressLength: minCompressLength,
Encode: func(data []byte) ([]byte, error) {
return zstdEncode(data, level)
},
Decode: zstdDecode,
})
}
// NewSnappyCompressor creates a snappy compressor
func NewSnappyCompressor(minCompressLength int) Compressor {
return NewCompressor(CompressorOption{
MinCompressLength: minCompressLength,
Encode: snappyEncode,
Decode: snappyDecode,
})
}