\Drupal\strata\Codec CompressionCodecInterface

One compression algorithm Strata can store a frame under.

A codec is identified by a short stable string recorded in every frame header, so a store written by one codec stays readable after the site's extensions change. That is the reason CompressionCodecInterface::id() exists separately from the class name: renaming a class must never orphan bytes already in the bucket.

Availability is a runtime property, not a build-time one. ext-zstd and ext-brotli are frequently absent, so a codec reports whether it can run instead of fataling on first use, and the registry falls back without losing the ability to READ what a better-equipped host wrote.

Measured on Drupal-shaped data in 8 KiB frames: gzip -9 reaches 3.52x, zstd -19 alone 3.71x, and zstd -19 with a trained dictionary 6.20x. The dictionary is worth more than the algorithm, which is why CompressionCodecInterface::supportsDictionary() is part of the contract rather than an implementation detail.

Summary

Methods
Constants
id()
isAvailable()
unavailableReason()
supportsDictionary()
levels()
compress()
decompress()
No public constants found

Methods

id()

id() : string

The stable identifier recorded in a frame header.

Never change this for an existing codec; add a new one instead.

Returns

string —

A short lowercase token such as "zstd", "gzip" or "none".

isAvailable()

isAvailable() : bool

Whether this codec can run on this host right now.

Returns

bool —

TRUE when every extension or binary the codec needs is present.

unavailableReason()

unavailableReason() : string|null

Why the codec is unavailable, for the settings form and hook_requirements().

Returns

string|null —

A short human-readable reason, or NULL when the codec is available.

supportsDictionary()

supportsDictionary() : bool

Whether the codec accepts a training dictionary.

Returns

bool —

TRUE when compress() and decompress() honour their $dictionary argument.

levels()

levels() : array{min: int, max: int, default: int, fast: int, dense: int}

The compression levels this codec accepts.

Returns

array{min: int, max: int, default: int, fast: int, dense: int} —

The usable range, the default, the level to use on the flush path where throughput matters, and the level to use during compaction where ratio matters.

compress()

compress(string  $data, int|null  $level = null, string|null  $dictionary = null) : string

Compresses a buffer.

Parameters

string $data

The bytes to compress. An empty string compresses to an empty string, so that an absent payload never becomes a non-empty frame.

int|null $level

A level within CompressionCodecInterface::levels(), or NULL for the default.

string|null $dictionary

Raw dictionary bytes, or NULL. Ignored by codecs that report no dictionary support, so a caller never has to branch on it.

Throws

\RuntimeException

When the codec is unavailable, or compression fails.

Returns

string —

The compressed bytes.

decompress()

decompress(string  $data, string|null  $dictionary = null) : string

Decompresses a buffer.

Must raise rather than return partial output. A truncated decompression is indistinguishable from correct output until much later.

Parameters

string $data

The compressed bytes.

string|null $dictionary

The same dictionary bytes used to compress, or NULL.

Throws

\RuntimeException

When the codec is unavailable, or the input is not valid for this codec.

Returns

string —

The original bytes.