zstd.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2019 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package y
  17. import (
  18. "sync"
  19. "github.com/klauspost/compress/zstd"
  20. )
  21. var (
  22. decoder *zstd.Decoder
  23. encoder *zstd.Encoder
  24. encOnce, decOnce sync.Once
  25. )
  26. // ZSTDDecompress decompresses a block using ZSTD algorithm.
  27. func ZSTDDecompress(dst, src []byte) ([]byte, error) {
  28. decOnce.Do(func() {
  29. var err error
  30. decoder, err = zstd.NewReader(nil)
  31. Check(err)
  32. })
  33. return decoder.DecodeAll(src, dst[:0])
  34. }
  35. // ZSTDCompress compresses a block using ZSTD algorithm.
  36. func ZSTDCompress(dst, src []byte, compressionLevel int) ([]byte, error) {
  37. encOnce.Do(func() {
  38. var err error
  39. level := zstd.EncoderLevelFromZstd(compressionLevel)
  40. encoder, err = zstd.NewWriter(nil, zstd.WithEncoderLevel(level))
  41. Check(err)
  42. })
  43. return encoder.EncodeAll(src, dst[:0]), nil
  44. }
  45. // ZSTDCompressBound returns the worst case size needed for a destination buffer.
  46. // Klauspost ZSTD library does not provide any API for Compression Bound. This
  47. // calculation is based on the DataDog ZSTD library.
  48. // See https://pkg.go.dev/github.com/DataDog/zstd#CompressBound
  49. func ZSTDCompressBound(srcSize int) int {
  50. lowLimit := 128 << 10 // 128 kB
  51. var margin int
  52. if srcSize < lowLimit {
  53. margin = (lowLimit - srcSize) >> 11
  54. }
  55. return srcSize + (srcSize >> 8) + margin
  56. }