refactor(compress): extract 128 bytes threshold to MinCompressSize constant
This commit is contained in:
@@ -268,7 +268,7 @@ func (l *TinyLog) CompressAndCleanup() (string, error) {
|
||||
size := stat.Size()
|
||||
|
||||
// 如果日志极短,免去压缩和 Base64 编码,直接以 raw: 明文形式返回
|
||||
if size <= 128 {
|
||||
if size <= int64(utils.MinCompressSize) {
|
||||
content, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -12,8 +12,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
zstdPrefix = "zstd:"
|
||||
rawPrefix = "raw:"
|
||||
zstdPrefix = "zstd:"
|
||||
rawPrefix = "raw:"
|
||||
MinCompressSize = 128
|
||||
)
|
||||
|
||||
var zlibWriterPool = sync.Pool{
|
||||
@@ -74,14 +75,14 @@ func initZstd() {
|
||||
})
|
||||
}
|
||||
|
||||
// CompressToBase64 compresses data using zstd and encodes to base64 with a prefix (uses raw for data under 128 bytes)
|
||||
// CompressToBase64 compresses data using zstd and encodes to base64 with a prefix (uses raw for data under MinCompressSize bytes)
|
||||
func CompressToBase64(data string) (string, error) {
|
||||
if data == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// 小于等于 128 字节,不需要压缩,直接前缀明文保存
|
||||
if len(data) <= 128 {
|
||||
// 小于等于阈值,不需要压缩,直接前缀明文保存
|
||||
if len(data) <= MinCompressSize {
|
||||
return rawPrefix + data, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user