compress

package
v0.53.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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

View Source
var ErrLimit = errors.New("compress: decompressed size exceeds the limit")

ErrLimit is returned by Compressor.DecompressLimit for input that decompresses past its limit.

View Source
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

func OutputSlack(alg Algorithm) int

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 Algorithm

type Algorithm uint8

Algorithm identifies a general-purpose compression algorithm.

const (
	// AlgorithmNone is the identity (no compression).
	AlgorithmNone Algorithm = iota
	// AlgorithmZSTD is Zstandard (high ratio, good speed; default for cold data).
	AlgorithmZSTD
	// AlgorithmLZ4 is LZ4 (fast, lower ratio; default for hot data).
	AlgorithmLZ4
)

func (Algorithm) String

func (a Algorithm) String() string

String returns a stable lower-case algorithm name.

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).

const (
	// LevelDefault is the default compression level per algorithm.
	LevelDefault Level = 0
	// LevelFast is the fastest compression (ZSTD 1, LZ4 default).
	LevelFast Level = 1
	// LevelBest is the best ratio (ZSTD 19, LZ4 default).
	LevelBest Level = 19
)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL