Line data Source code
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 1 : func decodeZstd(decodedBuf, b []byte) ([]byte, error) { 20 1 : return zstd.Decompress(decodedBuf, b) 21 1 : } 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 1 : func encodeZstd(compressedBuf []byte, varIntLen int, b []byte) []byte { 29 1 : buf := bytes.NewBuffer(compressedBuf[:varIntLen]) 30 1 : writer := zstd.NewWriterLevel(buf, 3) 31 1 : writer.Write(b) 32 1 : writer.Close() 33 1 : return buf.Bytes() 34 1 : }