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 block 9 : 10 : import ( 11 : "bytes" 12 : 13 : "github.com/DataDog/zstd" 14 : ) 15 : 16 : // UseStandardZstdLib indicates whether the zstd implementation is a port of the 17 : // official one in the facebook/zstd repository. 18 : // 19 : // This constant is only used in tests. Some tests rely on reproducibility of 20 : // SST files, but a custom implementation of zstd will produce different 21 : // compression result. So those tests have to be disabled in such cases. 22 : // 23 : // We cannot always use the official facebook/zstd implementation since it 24 : // relies on CGo. 25 : const UseStandardZstdLib = true 26 : 27 : // decodeZstd decompresses src with the Zstandard algorithm. The destination 28 : // buffer must already be sufficiently sized, otherwise decodeZstd may error. 29 1 : func decodeZstd(dst, src []byte) ([]byte, error) { 30 1 : n, err := zstd.DecompressInto(dst, src) 31 1 : // NB: zstd.DecompressInto may return n < 0 if err != nil. 32 1 : if err != nil { 33 1 : return nil, err 34 1 : } 35 1 : return dst[:n], nil 36 : } 37 : 38 : // encodeZstd compresses b with the Zstandard algorithm at default compression 39 : // level (level 3). It reuses the preallocated capacity of compressedBuf if it 40 : // is sufficient. The subslice `compressedBuf[:varIntLen]` should already encode 41 : // the length of `b` before calling encodeZstd. It returns the encoded byte 42 : // slice, including the `compressedBuf[:varIntLen]` prefix. 43 1 : func encodeZstd(compressedBuf []byte, varIntLen int, b []byte) []byte { 44 1 : buf := bytes.NewBuffer(compressedBuf[:varIntLen]) 45 1 : writer := zstd.NewWriterLevel(buf, 3) 46 1 : writer.Write(b) 47 1 : writer.Close() 48 1 : return buf.Bytes() 49 1 : }