/
gzip.go
46 lines (40 loc) · 953 Bytes
/
gzip.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
//go:build !no_gzip
// +build !no_gzip
package compress
import (
"bytes"
"io/ioutil"
"sync"
"github.com/klauspost/compress/gzip"
"github.com/pkg/errors"
"github.com/sabey/parquet-go/parquet"
)
var gzipWriterPool sync.Pool
func init() {
gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}
compressors[parquet.CompressionCodec_GZIP] = &Compressor{
Compress: func(buf []byte) []byte {
res := new(bytes.Buffer)
gzipWriter := gzipWriterPool.Get().(*gzip.Writer)
gzipWriter.Reset(res)
gzipWriter.Write(buf)
gzipWriter.Close()
gzipWriter.Reset(nil)
gzipWriterPool.Put(gzipWriter)
return res.Bytes()
},
Uncompress: func(buf []byte) (i []byte, err error) {
rbuf := bytes.NewReader(buf)
gzipReader, _ := gzip.NewReader(rbuf)
res, err := ioutil.ReadAll(gzipReader)
if err != nil {
return res, errors.Wrap(err, "ioutil.ReadAll")
}
return res, nil
},
}
}