haubur commented on code in PR #2529:
URL: https://github.com/apache/iggy/pull/2529#discussion_r2673467091


##########
core/common/src/types/compression/compression_algorithm.rs:
##########
@@ -45,24 +49,110 @@ impl FromStr for CompressionAlgorithm {
     fn from_str(s: &str) -> Result<Self, Self::Err> {
         match s.to_lowercase().as_str() {
             "gzip" => Ok(CompressionAlgorithm::Gzip),
+            "zstd" => Ok(CompressionAlgorithm::Zstd),
+            "lz4" => Ok(CompressionAlgorithm::Lz4),
+            "snappy" => Ok(CompressionAlgorithm::Snappy),
             "none" => Ok(CompressionAlgorithm::None),
             _ => Err(format!("Unknown compression type: {s}")),
         }
     }
 }
 
 impl CompressionAlgorithm {
+    pub fn compress(&self, data: &[u8]) -> Result<Vec<u8>, IggyError> {
+        match self {
+            CompressionAlgorithm::None => Ok(data.to_vec()),
+            CompressionAlgorithm::Gzip => {
+                let mut compressed_data = Vec::new();
+                let mut encoder = GzEncoder::new(&mut compressed_data, 
Compression::default());
+                encoder
+                    .write_all(data)
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                encoder
+                    .finish()
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                Ok(compressed_data)
+            }
+            CompressionAlgorithm::Zstd => {
+                let compressed_data = zstd::encode_all(data, 0)
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                Ok(compressed_data)
+            }
+            CompressionAlgorithm::Lz4 => {
+                let compressed_data = Vec::new();
+                let mut encoder = EncoderBuilder::new()
+                    .level(4)
+                    .build(compressed_data)
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                encoder
+                    .write_all(data)
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                let (compressed_data, result) = encoder.finish();
+                result.map_err(|e| 
IggyError::CompressionError(e.to_string()))?;
+                Ok(compressed_data)
+            }
+            CompressionAlgorithm::Snappy => {
+                let compressed_data = snap::raw::Encoder::new()
+                    .compress_vec(data)
+                    .map_err(|e| IggyError::CompressionError(e.to_string()))?;
+                Ok(compressed_data)
+            }
+        }
+    }
+
+    pub fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, IggyError> {

Review Comment:
   good point, done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to