using System.IO.Compression; namespace ZB.MOM.WW.CBDD.Core.Compression; /// /// Compression configuration for document payload processing. /// public sealed class CompressionOptions { /// /// Default compression options (compression disabled). /// public static CompressionOptions Default { get; } = new(); /// /// Enables payload compression for new writes. /// public bool EnableCompression { get; init; } = false; /// /// Minimum payload size (bytes) required before compression is attempted. /// public int MinSizeBytes { get; init; } = 1024; /// /// Minimum percentage of size reduction required to keep compressed output. /// public int MinSavingsPercent { get; init; } = 10; /// /// Preferred default codec for new writes. /// public CompressionCodec Codec { get; init; } = CompressionCodec.Brotli; /// /// Compression level passed to codec implementations. /// public CompressionLevel Level { get; init; } = CompressionLevel.Fastest; /// /// Maximum allowed decompressed payload size. /// public int MaxDecompressedSizeBytes { get; init; } = 16 * 1024 * 1024; /// /// Optional maximum input size allowed for compression attempts. /// public int? MaxCompressionInputBytes { get; init; } internal static CompressionOptions Normalize(CompressionOptions? options) { var candidate = options ?? Default; if (candidate.MinSizeBytes < 0) throw new ArgumentOutOfRangeException(nameof(MinSizeBytes), "MinSizeBytes must be non-negative."); if (candidate.MinSavingsPercent is < 0 or > 100) throw new ArgumentOutOfRangeException(nameof(MinSavingsPercent), "MinSavingsPercent must be between 0 and 100."); if (!Enum.IsDefined(candidate.Codec)) throw new ArgumentOutOfRangeException(nameof(Codec), $"Unsupported codec: {candidate.Codec}."); if (candidate.MaxDecompressedSizeBytes <= 0) throw new ArgumentOutOfRangeException(nameof(MaxDecompressedSizeBytes), "MaxDecompressedSizeBytes must be greater than 0."); if (candidate.MaxCompressionInputBytes is <= 0) throw new ArgumentOutOfRangeException(nameof(MaxCompressionInputBytes), "MaxCompressionInputBytes must be greater than 0 when provided."); return candidate; } }