github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/compression_cgo.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 ( 11 "bytes" 12 13 "github.com/DataDog/zstd" 14 ) 15 16 // decodeZstd decompresses b with the Zstandard algorithm. 17 // It reuses the preallocated capacity of decodedBuf if it is sufficient. 18 // On success, it returns the decoded byte slice. 19 func decodeZstd(decodedBuf, b []byte) ([]byte, error) { 20 return zstd.Decompress(decodedBuf, b) 21 } 22 23 // encodeZstd compresses b with the Zstandard algorithm at default compression 24 // level (level 3). It reuses the preallocated capacity of compressedBuf if it 25 // is sufficient. The subslice `compressedBuf[:varIntLen]` should already encode 26 // the length of `b` before calling encodeZstd. It returns the encoded byte 27 // slice, including the `compressedBuf[:varIntLen]` prefix. 28 func encodeZstd(compressedBuf []byte, varIntLen int, b []byte) []byte { 29 buf := bytes.NewBuffer(compressedBuf[:varIntLen]) 30 writer := zstd.NewWriterLevel(buf, 3) 31 writer.Write(b) 32 writer.Close() 33 return buf.Bytes() 34 }