github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/compression_nocgo.go (about)

     1  // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  //go:build !cgo
     6  // +build !cgo
     7  
     8  package sstable
     9  
    10  import "github.com/klauspost/compress/zstd"
    11  
    12  // decodeZstd decompresses b with the Zstandard algorithm.
    13  // It reuses the preallocated capacity of decodedBuf if it is sufficient.
    14  // On success, it returns the decoded byte slice.
    15  func decodeZstd(decodedBuf, b []byte) ([]byte, error) {
    16  	decoder, _ := zstd.NewReader(nil)
    17  	defer decoder.Close()
    18  	return decoder.DecodeAll(b, decodedBuf[:0])
    19  }
    20  
    21  // encodeZstd compresses b with the Zstandard algorithm at default compression
    22  // level (level 3). It reuses the preallocated capacity of compressedBuf if it
    23  // is sufficient. The subslice `compressedBuf[:varIntLen]` should already encode
    24  // the length of `b` before calling encodeZstd. It returns the encoded byte
    25  // slice, including the `compressedBuf[:varIntLen]` prefix.
    26  func encodeZstd(compressedBuf []byte, varIntLen int, b []byte) []byte {
    27  	encoder, _ := zstd.NewWriter(nil)
    28  	defer encoder.Close()
    29  	return encoder.EncodeAll(b, compressedBuf[:varIntLen])
    30  }