Documentation
¶
Overview ¶
Package compress wraps zstd/lz4 block compression and defines the composable codec-chain type (preprocessor → general compressor) used to reach 0.4–0.8 bytes/point (DESIGN.md §3.3, §14 M0).
A [Chain] is a sequence of a column-specific preprocessor (from [chunk]) followed by a general compressor (ZSTD or LZ4). Cold-tier recompression is just a different chain over the same parts. The wrappers are append-style (Compress(dst, src) []byte / Decompress(dst, src) []byte) and use pooled encoders/ decoders to avoid per-call allocation.
Index ¶
- Constants
- Variables
- func OutputSlack(alg Algorithm) int
- type Algorithm
- type Compressor
- func (c *Compressor) Algorithm() Algorithm
- func (c *Compressor) Compress(dst, src []byte) []byte
- func (c *Compressor) CompressBound(n int) int
- func (c *Compressor) CompressTransient(n int, presized bool) int64
- func (c *Compressor) Decompress(dst, src []byte) ([]byte, error)
- func (c *Compressor) DecompressLimit(buf, src []byte, limit int) ([]byte, error)
- func (c *Compressor) EncodeWorkspace() int64
- func (c *Compressor) Level() Level
- func (c *Compressor) OutputSlack() int
- type Level
- type ZSTDFramer
Constants ¶
const ( // FlagRaw means the payload is stored uncompressed. FlagRaw byte = 0x00 // FlagCompressed means the payload is algorithm-compressed. FlagCompressed byte = 0x01 )
Flag bytes for the compressed-block format.
const DecodeWorkspace = 512 << 10
DecodeWorkspace bounds what one Compressor.DecompressLimit call allocates beyond the decompressed size: the zstd output slack plus a freshly built block decoder's buffers and tables. LZ4 and raw blocks need none of it.
Variables ¶
var ErrLimit = errors.New("compress: decompressed size exceeds the limit")
ErrLimit is returned by Compressor.DecompressLimit for input that decompresses past its limit.
var ErrMalformed = errors.New("compress: malformed block")
ErrMalformed is returned by Compressor.DecompressLimit for input no writer of this package produces.
Functions ¶
func OutputSlack ¶ added in v0.49.0
OutputSlack is the spare capacity past the decompressed size that a Compressor.DecompressLimit destination of alg needs to decode without allocating. A decoded buffer keeps it for its life.
Types ¶
type Compressor ¶
type Compressor struct {
// contains filtered or unexported fields
}
Compressor is a pooled, append-style compressor. It is safe for concurrent use (each Compress/Decompress call borrows a pooled encoder/decoder).
A zstd encoder or decoder hands work between its goroutines over channels made when it is built, so one built inside a testing/synctest bubble must not be used outside it. The pools are therefore per Compressor, never package-level: a Compressor's coders are only ever used where it is.
func NewCompressor ¶
func NewCompressor(alg Algorithm, level Level) *Compressor
NewCompressor returns a Compressor for the given algorithm and level. Level is only meaningful for ZSTD; LZ4 ignores it.
func NewFrameCompressor ¶ added in v0.49.0
func NewFrameCompressor(alg Algorithm, level Level, maxInput int) *Compressor
NewFrameCompressor is NewCompressor for inputs of at most maxInput bytes: its zstd encoders match within a window of maxInput rounded up to a power of two, which is what their state is sized by. An input that fits the window compresses to the same bytes NewCompressor's would; a larger one still compresses, with matches no further back than the window.
func (*Compressor) Algorithm ¶
func (c *Compressor) Algorithm() Algorithm
Algorithm returns the compressor's algorithm.
func (*Compressor) Compress ¶
func (c *Compressor) Compress(dst, src []byte) []byte
Compress appends the compressed form of src to dst and returns the extended slice. If compression does not reduce the size (rare for small inputs), the raw bytes are stored with a 1-byte prefix FlagRaw.
func (*Compressor) CompressBound ¶ added in v0.49.0
func (c *Compressor) CompressBound(n int) int
CompressBound is the most Compressor.Compress appends for n bytes of input, compressed or stored raw: a destination with that much spare capacity takes the output without growing.
func (*Compressor) CompressTransient ¶ added in v0.49.0
func (c *Compressor) CompressTransient(n int, presized bool) int64
CompressTransient bounds what one Compressor.Compress call over n bytes allocates: into a fresh destination, the bound twice over for the append growth that builds it — into a presized one, nothing — and for lz4 the block it compresses into first.
func (*Compressor) Decompress ¶
func (c *Compressor) Decompress(dst, src []byte) ([]byte, error)
Decompress appends the decompressed form of src to dst and returns the extended slice. src must start with a flag byte (FlagRaw or FlagCompressed).
func (*Compressor) DecompressLimit ¶ added in v0.49.0
func (c *Compressor) DecompressLimit(buf, src []byte, limit int) ([]byte, error)
DecompressLimit is Compressor.Decompress that fails with ErrLimit rather than produce more than limit bytes, and with ErrMalformed on input it cannot bound. Unlike Decompress it does not append: it decodes into buf's capacity, discarding buf's contents, when that covers the bound and otherwise into a buffer of exactly the bound. It allocates at most the bound plus DecodeWorkspace.
func (*Compressor) EncodeWorkspace ¶ added in v0.49.0
func (c *Compressor) EncodeWorkspace() int64
EncodeWorkspace is what one encoder of c holds, borrowed by a Compressor.Compress call and kept by the pool after it: for zstd the hash tables of the level's preset and the history its window needs. Calls made one at a time borrow one encoder, so it is what a serial user of c holds in encoders.
func (*Compressor) Level ¶ added in v0.37.0
func (c *Compressor) Level() Level
Level returns the compressor's level (0 ⇒ the algorithm default).
func (*Compressor) OutputSlack ¶ added in v0.49.0
func (c *Compressor) OutputSlack() int
OutputSlack is OutputSlack for c's algorithm.
type Level ¶
type Level uint8
Level is a compression level. ZSTD levels: 1–22 (default 3, higher = better ratio but slower). LZ4 levels are not exposed (LZ4 is always fastest).
type ZSTDFramer ¶ added in v0.46.0
type ZSTDFramer struct {
// contains filtered or unexported fields
}
ZSTDFramer is a pooled codec for bare zstd frames, without the FlagRaw/FlagCompressed prefix Compressor puts in front of a block. Two properties follow from that and neither is available from Compressor:
- the bytes are a standard zstd frame, so any zstd implementation reads them — an HTTP `Content-Encoding: zstd` body, for one;
- ZSTDFramer.Reader decompresses as a stream, so a caller can bound the *decompressed* size. A whole-buffer decode cannot: it has already allocated the output by the time the size is known, which is exactly what a decompression bomb exploits.
A ZSTDFramer is safe for concurrent use.
func NewZSTDFramer ¶ added in v0.46.0
func NewZSTDFramer(level Level) *ZSTDFramer
NewZSTDFramer returns a ZSTDFramer compressing at the given level.
func (*ZSTDFramer) Compress ¶ added in v0.46.0
func (f *ZSTDFramer) Compress(dst, src []byte) []byte
Compress appends the zstd frame of src to dst and returns the extended slice. Unlike Compressor.Compress it always compresses, even when that grows the input; a caller that cares compares the lengths itself.
func (*ZSTDFramer) Reader ¶ added in v0.46.0
func (f *ZSTDFramer) Reader(r io.Reader) (io.ReadCloser, error)
Reader returns a streaming decompressor over the zstd frame in r. Close returns the decoder to the pool and must be called; it does not close r.