You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/08 20:05:31 UTC

[GitHub] [arrow] mqy commented on a change in pull request #9137: ARROW-8676: [Rust] IPC RecordBatch body compression

mqy commented on a change in pull request #9137:
URL: https://github.com/apache/arrow/pull/9137#discussion_r554081674



##########
File path: rust/arrow/src/ipc/compression.rs
##########
@@ -0,0 +1,122 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! IPC Compression Utilities
+
+use std::io::{Read, Write};
+
+use crate::error::{ArrowError, Result};
+use crate::ipc::gen::Message::CompressionType;
+
+pub trait IpcCompressionCodec {
+    fn compress(&self, input_buf: &[u8], output_buf: &mut Vec<u8>) -> Result<()>;
+    fn decompress(&self, input_buf: &[u8], output_buf: &mut Vec<u8>) -> Result<usize>;
+    fn get_compression_type(&self) -> CompressionType;
+}
+
+pub type Codec = Box<dyn IpcCompressionCodec>;
+
+#[inline]
+pub(crate) fn get_codec(
+    compression_type: Option<CompressionType>,
+) -> Result<Option<Codec>> {
+    match compression_type {
+        Some(CompressionType::LZ4_FRAME) => Ok(Some(Box::new(Lz4CompressionCodec {}))),
+        Some(ctype) => Err(ArrowError::InvalidArgumentError(format!(
+            "IPC CompresstionType {:?} not yet supported",
+            ctype
+        ))),
+        None => Ok(None),
+    }
+}
+
+pub struct Lz4CompressionCodec {}
+const LZ4_BUFFER_SIZE: usize = 4096;
+
+impl IpcCompressionCodec for Lz4CompressionCodec {
+    fn compress(&self, input_buf: &[u8], output_buf: &mut Vec<u8>) -> Result<()> {
+        if input_buf.is_empty() {
+            output_buf.write_all(&8i64.to_le_bytes())?;
+            return Ok(());
+        }
+        // write out the uncompressed length as a LE i64 value
+        output_buf.write_all(&(input_buf.len() as i64).to_le_bytes())?;
+        let mut encoder = lz4::EncoderBuilder::new().build(output_buf)?;
+        let mut from = 0;
+        loop {

Review comment:
       This loop can be omitted.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org