/
compress.go
88 lines (73 loc) · 2.08 KB
/
compress.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
// Copyright 2023 The CortexTheseus Authors
// This file is part of the CortexTheseus library.
//
// The CortexTheseus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexTheseus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
package compress
import (
"bytes"
"compress/gzip"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/golang/snappy"
"io"
"time"
)
func UnzipData(data []byte) (resData []byte, err error) {
start := time.Now()
defer func() { log.Info("Unzip data", "cost", time.Since(start)) }()
b := bytes.NewBuffer(data)
var r io.Reader
r, err = gzip.NewReader(b)
if err != nil {
return
}
var resB bytes.Buffer
_, err = resB.ReadFrom(r)
if err != nil {
return
}
resData = resB.Bytes()
return
}
func ZipData(data []byte) (compressedData []byte, err error) {
start := time.Now()
defer func() { log.Info("Zip data", "cost", time.Since(start)) }()
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err = gz.Write(data)
if err != nil {
return
}
if err = gz.Flush(); err != nil {
return
}
if err = gz.Close(); err != nil {
return
}
compressedData = b.Bytes()
return
}
func SnappyEncode(data []byte) ([]byte, error) {
start := time.Now()
defer func() { log.Info("Snappy encode", "cost", time.Since(start)) }()
return snappy.Encode(nil, data), nil
}
func SnappyDecode(data []byte) ([]byte, error) {
start := time.Now()
defer func() { log.Info("Snappy decode", "cost", time.Since(start)) }()
res, err := snappy.Decode(nil, data)
if err != nil {
return nil, err
}
return res, nil
}