ddn.compressor.snappy
ddn.compressor.snappy
Snappy compression provider for ddn.api.compressor.
This module provides a complete Snappy compression and decompression implementation conforming to the Snappy raw format specification. It includes:
- Streaming
CompressorandDecompressorclasses implementing the
ddn.api.compressor interface.
- Hash-based match finding for LZ77-style compression.
- Full support for all Snappy element types (literals, 1/2/4-byte copies).
- Interoperability with the standard Snappy library format.
Snappy is designed for speed rather than maximum compression ratio. It achieves reasonable compression at very high speeds, making it suitable for real-time applications, database storage, and RPC systems.
Format specification: https://github.com/google/snappy/blob/main/format_description.txt
Copyright
Module Initializers 1
()Types 2
Snappy compressor implementing the Compressor interface.
Buffers all input until finish() is called, then compresses and emits the complete Snappy stream.
private CompressionOptions _optsprivate OutputSink _sinkprivate ubyte[] _bufferprivate ulong _bytesInprivate ulong _bytesOutprivate bool _finishedvoid setOutputSink(OutputSink sink)Set the output sink for compressed data.void setProgressCallback(ProgressCallback callback)Set an optional progress callback.void write(const(ubyte)[] data)Write data to be compressed.void finish()Finish compression and emit all output.void reset()Reset the compressor for reuse.bool setDictionary(const(ubyte)[] dict)Set a compression dictionary (not supported by Snappy).bool isFinished() @property constReturns true if finish() has been called and the stream is closed for further writes.this(CompressionOptions opts)Construct a new Snappy compressor.Snappy decompressor implementing the Decompressor interface.
Buffers all input until finish() is called, then decompresses and emits the complete uncompressed data.
private DecompressionOptions _optsprivate OutputSink _sinkprivate ubyte[] _bufferprivate ulong _bytesInprivate ulong _bytesOutprivate bool _finishedvoid setOutputSink(OutputSink sink)Set the output sink for decompressed data.void setProgressCallback(ProgressCallback callback)Set an optional progress callback.void write(const(ubyte)[] data)Write compressed data to be decompressed.void finish()Finish decompression and emit all output.void reset()Reset the decompressor for reuse.bool setDictionary(const(ubyte)[] dict)Set a decompression dictionary (not supported by Snappy).bool isFinished() @property constReturns true if finish() has been called and the stream is closed for further writes.this(DecompressionOptions opts)Construct a new Snappy decompressor.Functions 14
size_t decodeVarint(const(ubyte)[] input, out uint value)Decode a little-endian varint from input.size_t snappyHash(const(ubyte)[] data)Compute hash for a 4-byte sequence for match finding.uint snappyHashFast(const(ubyte) * p) pure nothrow @nogcFast inline hash using pointer cast for 32-bit LE read.auto findMatch(const(ubyte)[] data, size_t pos, ref size_t[SNAPPY_HASH_SIZE] hashTable)Find a match at the current position using the hash table.size_t encodeCopy(size_t offset, size_t length, ubyte[] output)Encode a copy (backreference) element.void emitLiteral(ref ubyte * op, const(ubyte) * src, size_t len) pure nothrow @nogcEmit a literal element using raw pointers.void emitCopy(ref ubyte * op, size_t offset, size_t length) pure nothrow @nogcEmit a copy (backreference) element using raw pointers.size_t compressBlockFast(const(ubyte)[] src, ubyte[] outBuf) @trusted @nogc nothrowCompress a block of data using Snappy algorithm.size_t compressBlock(const(ubyte)[] input, ubyte[] output)Compress a block of data using Snappy format.size_t decompressBlock(const(ubyte)[] input, size_t expectedLen, ubyte[] output)Decompress a Snappy-compressed block.Variables 8
SNAPPY_MIN_MATCH = 4Minimum match length for Snappy (4 bytes).
SNAPPY_MAX_OFFSET_1 = 2047Maximum offset for 1-byte copy (11 bits = 2047).
SNAPPY_MAX_OFFSET_2 = 65535Maximum offset for 2-byte copy (16 bits = 65535).
SNAPPY_MAX_LEN_1 = 11Maximum length for 1-byte copy encoding.
SNAPPY_MAX_LEN_2 = 64Maximum length for 2-byte and 4-byte copy encoding.
SNAPPY_HASH_BITS = 14Hash table size (64K entries for good match finding).
SNAPPY_HASH_SIZE = 1 << SNAPPY_HASH_BITSSNAPPY_BLOCK_SIZE = 32 * 1024Block size for compression (32KB, matching reference implementation).