-
Notifications
You must be signed in to change notification settings - Fork 38
/
compress.go
39 lines (31 loc) · 897 Bytes
/
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
package blobstor
import (
"github.com/klauspost/compress/zstd"
)
// zstdFrameMagic contains first 4 bytes of any compressed object
// https://github.com/klauspost/compress/blob/master/zstd/framedec.go#L58 .
var zstdFrameMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
func noOpCompressor(data []byte) []byte {
return data
}
func noOpDecompressor(data []byte) ([]byte, error) {
return data, nil
}
func zstdCompressor() (*zstd.Encoder, func([]byte) []byte, error) {
enc, err := zstd.NewWriter(nil)
if err != nil {
return nil, nil, err
}
return enc, func(data []byte) []byte {
return enc.EncodeAll(data, make([]byte, 0, len(data)))
}, nil
}
func zstdDecompressor() (*zstd.Decoder, func([]byte) ([]byte, error), error) {
dec, err := zstd.NewReader(nil)
if err != nil {
return nil, nil, err
}
return dec, func(data []byte) ([]byte, error) {
return dec.DecodeAll(data, nil)
}, nil
}