Class CompressionHelper
Provides transparent compression and decompression utilities for model serialization.
public static class CompressionHelper
- Inheritance
-
CompressionHelper
- Inherited Members
Remarks
CompressionHelper handles the application of compression during model serialization and decompression during deserialization. It works transparently with the facade pattern, so users never directly interact with compression algorithms.
For Beginners: This helper automatically compresses your model when you save it and decompresses it when you load it. You don't need to do anything special - just configure compression when building your model, and the rest happens automatically.
Benefits:
- Smaller model files (50-90% size reduction)
- Faster model loading over networks
- Lower storage costs
- Seamless integration with existing serialize/deserialize methods
Methods
Compress(byte[], CompressionConfig)
Compresses the serialized model data based on the compression configuration.
public static byte[] Compress(byte[] data, CompressionConfig config)
Parameters
databyte[]The uncompressed serialized model data.
configCompressionConfigThe compression configuration.
Returns
- byte[]
The compressed data with header information.
Remarks
For Beginners: This method takes your model data and makes it smaller. It adds a small header so we can decompress it later. The header contains: - Magic bytes (to identify this as compressed data) - Version number (for future compatibility) - Compression type (which algorithm was used) - Original size (so we know how much memory to allocate)
Exceptions
- ArgumentNullException
Thrown when data or config is null.
DecompressIfNeeded(byte[])
Decompresses model data if it was compressed, otherwise returns unchanged data.
public static byte[] DecompressIfNeeded(byte[] data)
Parameters
databyte[]The potentially compressed model data.
Returns
- byte[]
The decompressed data.
Remarks
For Beginners: This method checks if the data was compressed by us. If yes, it decompresses it. If no, it returns the data unchanged. This allows loading both compressed and uncompressed models seamlessly.
Exceptions
- ArgumentNullException
Thrown when data is null.
- InvalidOperationException
Thrown when decompression fails.
GetCompressionStats(byte[], byte[])
Gets compression statistics for the last compression operation.
public static (long originalSize, long compressedSize, double ratio, double savedPercent) GetCompressionStats(byte[] originalData, byte[] compressedData)
Parameters
Returns
- (long originalSize, long compressedSize, double ratio, double savedPercent)
A tuple containing (original size, compressed size, compression ratio, space saved percentage).
IsCompressedData(byte[])
Checks if the data was compressed by AiDotNet.
public static bool IsCompressedData(byte[] data)
Parameters
databyte[]The data to check.
Returns
- bool
True if the data appears to be compressed AiDotNet model data.