-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressor.go
66 lines (53 loc) · 1.13 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
package boltutils
import (
"bytes"
"io/ioutil"
"github.com/pierrec/lz4"
)
type Compressor interface {
Compress(data []byte) ([]byte, error)
Decompress(data []byte) ([]byte, error)
}
type CompressorType int
const (
NoopCompressor CompressorType = iota
GzipCompressor
Lz4Compressor
)
type gzipCompressor struct {
}
func newGzipCompressor() gzipCompressor {
return gzipCompressor{}
}
func (gc gzipCompressor) Compress(data []byte) ([]byte, error) {
return gzipData(data)
}
func (gc gzipCompressor) Decompress(data []byte) ([]byte, error) {
return ungzipData(data)
}
type lz4Compressor struct {
}
func newLz4Compressor() lz4Compressor {
return lz4Compressor{}
}
func (gc lz4Compressor) Compress(data []byte) ([]byte, error) {
var b bytes.Buffer
w := lz4.NewWriter(&b)
_, err := w.Write(data)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (gc lz4Compressor) Decompress(compressedData []byte) ([]byte, error) {
r := lz4.NewReader(bytes.NewReader(compressedData))
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return data, nil
}