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 src with the Zstandard algorithm. The destination 17 : // buffer must already be sufficiently sized, otherwise decodeZstd may error. 18 1 : func decodeZstd(dst, src []byte) ([]byte, error) { 19 1 : n, err := zstd.DecompressInto(dst, src) 20 1 : // NB: zstd.DecompressInto may return n < 0 if err != nil. 21 1 : if err != nil { 22 0 : return nil, err 23 0 : } 24 1 : return dst[:n], nil 25 : } 26 : 27 : // encodeZstd compresses b with the Zstandard algorithm at default compression 28 : // level (level 3). It reuses the preallocated capacity of compressedBuf if it 29 : // is sufficient. The subslice `compressedBuf[:varIntLen]` should already encode 30 : // the length of `b` before calling encodeZstd. It returns the encoded byte 31 : // slice, including the `compressedBuf[:varIntLen]` prefix. 32 1 : func encodeZstd(compressedBuf []byte, varIntLen int, b []byte) []byte { 33 1 : buf := bytes.NewBuffer(compressedBuf[:varIntLen]) 34 1 : writer := zstd.NewWriterLevel(buf, 3) 35 1 : writer.Write(b) 36 1 : writer.Close() 37 1 : return buf.Bytes() 38 1 : }