From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ronja.mits.lan by ronja.mits.lan with LMTP id UJk0N4MsfGaTaAAAxxbTJA (envelope-from ); Wed, 26 Jun 2024 16:58:11 +0200 Received: from proxmox-new.maurer-it.com (unknown [192.168.2.33]) by ronja.mits.lan (Postfix) with ESMTPS id C444BF6463A; Wed, 26 Jun 2024 16:58:11 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A79AF4873B; Wed, 26 Jun 2024 16:58:11 +0200 (CEST) Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by proxmox-new.maurer-it.com (Proxmox) with ESMTPS; Wed, 26 Jun 2024 16:58:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1F005538B; Wed, 26 Jun 2024 16:58:08 +0200 (CEST) From: Maximiliano Sandoval To: pbs-devel@lists.proxmox.com Date: Wed, 26 Jun 2024 16:57:58 +0200 Message-Id: <20240626145801.751326-3-m.sandoval@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240626145801.751326-1-m.sandoval@proxmox.com> References: <20240626145801.751326-1-m.sandoval@proxmox.com> MIME-Version: 1.0 Subject: [pbs-devel] [PATCH promox v4 2/5] compression: deflate: add builder pattern X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" X-SPAM-LEVEL: Spam detection results: 0 DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods MAILING_LIST_MULTI -2 Multiple indicators imply a widely-seen list manager RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record This allows creating a encoder in a more general way and allows to specify whether we want to set zlib headers. This is useful to compress HTTP traffic, as per [1]. [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives Signed-off-by: Maximiliano Sandoval --- .../src/deflate/compression.rs | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/proxmox-compression/src/deflate/compression.rs b/proxmox-compression/src/deflate/compression.rs index 6e6a151d..24d2bc99 100644 --- a/proxmox-compression/src/deflate/compression.rs +++ b/proxmox-compression/src/deflate/compression.rs @@ -35,27 +35,64 @@ pub struct DeflateEncoder { state: EncoderState, } -impl DeflateEncoder { - pub fn new(inner: T) -> Self { - Self::with_quality(inner, Level::Default) +pub struct DeflateEncoderBuilder { + inner: T, + is_zlib: bool, + buffer_size: usize, + level: Level, +} + +impl DeflateEncoderBuilder { + pub fn zlib(mut self, is_zlib: bool) -> Self { + self.is_zlib = is_zlib; + self } - pub fn with_quality(inner: T, level: Level) -> Self { - let level = match level { + pub fn level(mut self, level: Level) -> Self { + self.level = level; + self + } + + pub fn buffer_size(mut self, buffer_size: usize) -> Self { + self.buffer_size = buffer_size; + self + } + + pub fn build(self) -> DeflateEncoder { + let level = match self.level { Level::Fastest => Compression::fast(), Level::Best => Compression::best(), Level::Default => Compression::new(3), Level::Precise(val) => Compression::new(val), }; - Self { - inner, - compressor: Compress::new(level, false), - buffer: ByteBuffer::with_capacity(super::BUFFER_SIZE), + DeflateEncoder { + inner: self.inner, + compressor: Compress::new(level, self.is_zlib), + buffer: ByteBuffer::with_capacity(self.buffer_size), input_buffer: Bytes::new(), state: EncoderState::Reading, } } +} + +impl DeflateEncoder { + pub fn new(inner: T) -> Self { + Self::builder(inner).build() + } + + pub fn builder(inner: T) -> DeflateEncoderBuilder { + DeflateEncoderBuilder { + inner, + is_zlib: false, + buffer_size: super::BUFFER_SIZE, + level: Level::Default, + } + } + + pub fn with_quality(inner: T, level: Level) -> Self { + Self::builder(inner).level(level).build() + } pub fn total_in(&self) -> u64 { self.compressor.total_in() -- 2.39.2 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel