diff --git a/go.mod b/go.mod index 744538a732..43452c16d8 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/cockroachdb/pebble require ( - github.com/DataDog/zstd v1.5.6-0.20230622172052-ea68dcab66c0 + github.com/DataDog/zstd v1.4.5 github.com/HdrHistogram/hdrhistogram-go v1.1.2 github.com/cespare/xxhash/v2 v2.2.0 github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f diff --git a/go.sum b/go.sum index 92aae6dab0..c5c1dcf926 100644 --- a/go.sum +++ b/go.sum @@ -38,8 +38,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= -github.com/DataDog/zstd v1.5.6-0.20230622172052-ea68dcab66c0 h1:ye3LRgDs6Og7SKC1wBQH8oMaGczhCRpPpnU74l4rma8= -github.com/DataDog/zstd v1.5.6-0.20230622172052-ea68dcab66c0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= +github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= +github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20190129172621-c8b1d7a94ddf/go.mod h1:aJ4qN3TfrelA6NZ6AXsXRfmEVaYin3EDbSPJrKS8OXo= github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= diff --git a/sstable/compression.go b/sstable/compression.go index 47b02b8322..4dd5bea4bc 100644 --- a/sstable/compression.go +++ b/sstable/compression.go @@ -33,24 +33,23 @@ func decompressedLen(blockType blockType, b []byte) (int, int, error) { } } -func decompressInto(blockType blockType, compressed []byte, buf []byte) error { +func decompressInto(blockType blockType, compressed []byte, buf []byte) ([]byte, error) { var result []byte var err error switch blockType { case snappyCompressionBlockType: result, err = snappy.Decode(buf, compressed) case zstdCompressionBlockType: - err = decodeZstd(buf, compressed) - result = buf + result, err = decodeZstd(buf, compressed) } if err != nil { - return base.MarkCorruptionError(err) + return nil, base.MarkCorruptionError(err) } if len(result) != 0 && (len(result) != len(buf) || &result[0] != &buf[0]) { - return base.CorruptionErrorf("pebble/table: decompressed into unexpected buffer: %p != %p", + return nil, base.CorruptionErrorf("pebble/table: decompressed into unexpected buffer: %p != %p", errors.Safe(result), errors.Safe(buf)) } - return nil + return result, nil } // decompressBlock decompresses an SST block, with space allocated from a cache. @@ -69,7 +68,7 @@ func decompressBlock(cache *cache.Cache, blockType blockType, b []byte) (*cache. // Allocate sufficient space from the cache. decoded := cache.Alloc(decodedLen) decodedBuf := decoded.Buf() - if err := decompressInto(blockType, b, decodedBuf); err != nil { + if _, err := decompressInto(blockType, b, decodedBuf); err != nil { cache.Free(decoded) return nil, err } diff --git a/sstable/compression_cgo.go b/sstable/compression_cgo.go index 40e9c7e006..ad7d844201 100644 --- a/sstable/compression_cgo.go +++ b/sstable/compression_cgo.go @@ -13,11 +13,11 @@ import ( "github.com/DataDog/zstd" ) -// decodeZstd decompresses src with the Zstandard algorithm. The destination -// buffer must already be sufficiently sized, otherwise decodeZstd may error. -func decodeZstd(dst, src []byte) error { - _, err := zstd.DecompressInto(dst, src) - return err +// decodeZstd decompresses b with the Zstandard algorithm. +// It reuses the preallocated capacity of decodedBuf if it is sufficient. +// On success, it returns the decoded byte slice. +func decodeZstd(decodedBuf, b []byte) ([]byte, error) { + return zstd.Decompress(decodedBuf, b) } // encodeZstd compresses b with the Zstandard algorithm at default compression diff --git a/sstable/compression_nocgo.go b/sstable/compression_nocgo.go index 889ef7d591..42c34fb7f0 100644 --- a/sstable/compression_nocgo.go +++ b/sstable/compression_nocgo.go @@ -9,13 +9,13 @@ package sstable import "github.com/klauspost/compress/zstd" -// decodeZstd decompresses src with the Zstandard algorithm. The destination -// buffer must already be sufficiently sized, otherwise decodeZstd may error. -func decodeZstd(dst, src []byte) error { +// decodeZstd decompresses b with the Zstandard algorithm. +// It reuses the preallocated capacity of decodedBuf if it is sufficient. +// On success, it returns the decoded byte slice. +func decodeZstd(decodedBuf, b []byte) ([]byte, error) { decoder, _ := zstd.NewReader(nil) defer decoder.Close() - _, err := decoder.DecodeAll(src, dst[:0]) - return err + return decoder.DecodeAll(b, decodedBuf[:0]) } // encodeZstd compresses b with the Zstandard algorithm at default compression diff --git a/sstable/suffix_rewriter.go b/sstable/suffix_rewriter.go index 4228e557b6..9672deda46 100644 --- a/sstable/suffix_rewriter.go +++ b/sstable/suffix_rewriter.go @@ -540,9 +540,8 @@ func readBlockBuf(r *Reader, bh BlockHandle, buf []byte) ([]byte, []byte, error) if cap(buf) < decompressedLen { buf = make([]byte, decompressedLen) } - dst := buf[:decompressedLen] - err = decompressInto(typ, raw[prefix:], dst) - return dst, buf, err + res, err := decompressInto(typ, raw[prefix:], buf[:decompressedLen]) + return res, buf, err } // memReader is a thin wrapper around a []byte such that it can be passed to diff --git a/sstable/testdata/h.zstd-compression.sst b/sstable/testdata/h.zstd-compression.sst index 698d80d2b4..37de2506ad 100644 Binary files a/sstable/testdata/h.zstd-compression.sst and b/sstable/testdata/h.zstd-compression.sst differ