2019-05-22 21:16:55 +00:00
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
2020-08-15 11:44:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-07-23 16:26:39 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding/zstd"
|
2019-05-24 09:34:32 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2019-05-22 21:16:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CompressZSTDLevel appends compressed src to dst and returns
|
|
|
|
// the appended dst.
|
|
|
|
//
|
|
|
|
// The given compressLevel is used for the compression.
|
|
|
|
func CompressZSTDLevel(dst, src []byte, compressLevel int) []byte {
|
2019-05-24 10:01:02 +00:00
|
|
|
compressCalls.Inc()
|
2019-05-24 09:34:32 +00:00
|
|
|
originalBytes.Add(len(src))
|
|
|
|
dstLen := len(dst)
|
2019-07-23 16:26:39 +00:00
|
|
|
dst = zstd.CompressLevel(dst, src, compressLevel)
|
2019-05-24 09:34:32 +00:00
|
|
|
compressedBytes.Add(len(dst) - dstLen)
|
|
|
|
return dst
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecompressZSTD decompresses src, appends the result to dst and returns
|
|
|
|
// the appended dst.
|
|
|
|
func DecompressZSTD(dst, src []byte) ([]byte, error) {
|
2019-05-24 10:01:02 +00:00
|
|
|
decompressCalls.Inc()
|
2020-08-15 11:44:29 +00:00
|
|
|
b, err := zstd.Decompress(dst, src)
|
|
|
|
if err != nil {
|
|
|
|
return b, fmt.Errorf("cannot decompress zstd block with len=%d to a buffer with len=%d: %w; block data (hex): %X", len(src), len(dst), err, src)
|
|
|
|
}
|
|
|
|
return b, nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-05-24 09:34:32 +00:00
|
|
|
|
|
|
|
var (
|
2019-05-24 10:01:02 +00:00
|
|
|
compressCalls = metrics.NewCounter(`vm_zstd_block_compress_calls_total`)
|
|
|
|
decompressCalls = metrics.NewCounter(`vm_zstd_block_decompress_calls_total`)
|
|
|
|
|
2019-05-24 09:34:32 +00:00
|
|
|
originalBytes = metrics.NewCounter(`vm_zstd_block_original_bytes_total`)
|
|
|
|
compressedBytes = metrics.NewCounter(`vm_zstd_block_compressed_bytes_total`)
|
|
|
|
)
|