-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
44 lines (32 loc) · 970 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
40
41
42
43
44
package zstd_marshal
import (
"encoding/json"
"github.com/klauspost/compress/zstd"
)
type zstdEncoder struct {
*zstd.Encoder
}
// MarshalWithConcurrency - convert struct to zstd encoded bytes with concurrency
func MarshalWithConcurrency(v interface{}, concurrency int) ([]byte, error) {
// init zstd encoder with set concurrency
encoder := newZstdEncoderWithConcurrency(concurrency)
defer encoder.Close()
dataBytes, err := json.Marshal(v)
if err != nil {
return nil, err
}
encodedData := encoder.EncodeAll(dataBytes, make([]byte, 0, len(dataBytes)))
return encodedData, nil
}
// Marshal - convert struct to zstd encoded bytes
func Marshal(v interface{}) ([]byte, error) {
// init zstd encoder with set concurrency
encoder := newZstdEncoder()
defer encoder.Close()
dataBytes, err := json.Marshal(v)
if err != nil {
return nil, err
}
encodedData := encoder.EncodeAll(dataBytes, make([]byte, 0, len(dataBytes)))
return encodedData, nil
}