New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support compression for IPC #1855
Changes from 10 commits
e51eca3
ede5115
67e7de5
58488a3
932e381
fcc4f5f
5b0d711
0c61067
9ac4b01
49edfc8
5d974b3
a14e1e1
7f90fb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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. | ||
|
||
use crate::ipc::CompressionType; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq)] | ||
pub enum CompressionCodecType { | ||
NoCompression, | ||
Lz4Frame, | ||
Zstd, | ||
} | ||
|
||
impl From<CompressionType> for CompressionCodecType { | ||
fn from(compression_type: CompressionType) -> Self { | ||
match compression_type { | ||
CompressionType::ZSTD => CompressionCodecType::Zstd, | ||
CompressionType::LZ4_FRAME => CompressionCodecType::Lz4Frame, | ||
_ => CompressionCodecType::NoCompression, | ||
} | ||
} | ||
} | ||
|
||
impl From<CompressionCodecType> for Option<CompressionType> { | ||
fn from(codec: CompressionCodecType) -> Self { | ||
match codec { | ||
CompressionCodecType::NoCompression => None, | ||
CompressionCodecType::Lz4Frame => Some(CompressionType::LZ4_FRAME), | ||
CompressionCodecType::Zstd => Some(CompressionType::ZSTD), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "zstd,lz4", test))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you can put this guard on the entire |
||
mod compression_function { | ||
use crate::error::Result; | ||
use crate::ipc::compression::ipc_compression::CompressionCodecType; | ||
use std::io::{Read, Write}; | ||
|
||
impl CompressionCodecType { | ||
pub fn compress(&self, input: &[u8], output: &mut Vec<u8>) -> Result<()> { | ||
match self { | ||
CompressionCodecType::Lz4Frame => { | ||
let mut encoder = lz4::EncoderBuilder::new().build(output).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the doc seems to say, "This module provides access to the block mode functions of the lz4 C library". |
||
encoder.write_all(input).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest passing the errors back out of here directly (as |
||
encoder.finish().1.unwrap(); | ||
Ok(()) | ||
} | ||
CompressionCodecType::Zstd => { | ||
let mut encoder = zstd::Encoder::new(output, 0).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we pass the error out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix, and pass the error out. |
||
encoder.write_all(input).unwrap(); | ||
encoder.finish().unwrap(); | ||
Ok(()) | ||
} | ||
_ => Ok(()), | ||
} | ||
} | ||
|
||
pub fn decompress(&self, input: &[u8], output: &mut Vec<u8>) -> Result<usize> { | ||
let result: Result<usize> = match self { | ||
CompressionCodecType::Lz4Frame => { | ||
let mut decoder = lz4::Decoder::new(input)?; | ||
match decoder.read_to_end(output) { | ||
Ok(size) => Ok(size), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
CompressionCodecType::Zstd => { | ||
let mut decoder = zstd::Decoder::new(input)?; | ||
match decoder.read_to_end(output) { | ||
Ok(size) => Ok(size), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
_ => Ok(input.len()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just handle the LZ4 and ZSTD branch. |
||
}; | ||
result | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::ipc::compression::ipc_compression::CompressionCodecType; | ||
|
||
#[test] | ||
fn test_lz4_compression() { | ||
let input_bytes = "hello lz4".as_bytes(); | ||
let codec: CompressionCodecType = CompressionCodecType::Lz4Frame; | ||
let mut output_bytes: Vec<u8> = Vec::new(); | ||
codec.compress(input_bytes, &mut output_bytes).unwrap(); | ||
let mut result_output_bytes: Vec<u8> = Vec::new(); | ||
codec | ||
.decompress(output_bytes.as_slice(), &mut result_output_bytes) | ||
.unwrap(); | ||
assert_eq!(input_bytes, result_output_bytes.as_slice()); | ||
} | ||
|
||
#[test] | ||
fn test_zstd_compression() { | ||
let input_bytes = "hello zstd".as_bytes(); | ||
let codec: CompressionCodecType = CompressionCodecType::Zstd; | ||
let mut output_bytes: Vec<u8> = Vec::new(); | ||
codec.compress(input_bytes, &mut output_bytes).unwrap(); | ||
let mut result_output_bytes: Vec<u8> = Vec::new(); | ||
codec | ||
.decompress(output_bytes.as_slice(), &mut result_output_bytes) | ||
.unwrap(); | ||
assert_eq!(input_bytes, result_output_bytes.as_slice()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
|
||
pub(crate) mod ipc_compression; | ||
pub(crate) const LENGTH_EMPTY_COMPRESSED_DATA: i64 = 0; | ||
pub(crate) const LENGTH_NO_COMPRESSED_DATA: i64 = -1; | ||
pub(crate) const LENGTH_OF_PREFIX_DATA: i64 = 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So by default we will include zstd, lz4 dependencies? If we don't use ipc or don't use compressed ipc, seems the dependencies are not necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree -- I suggest something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, include the dependency by default.
buffer compression protocol is common protocol for all languages in the arrow ecosystem, if we use the compression/decompression as optional, we can read the file or stream from compressed side.
2.0.0 compression has been implemented in the Java, C++ and other languages, If your rust server receiver a message compressed by the protocol, we can read them by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be in favour of supporting adding the deps to
ipc
but not creatingipc_compression
separately. The inconvenience of a larger binary is a lesser problem than the runtime error of not supporting compression by omission.The latter would need a rebuild.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have encountered an issue on cross-compiling lz4 crate on some platform. We don't use ipc so we can choose not to include ipc feature at all. I'm wondering if ipc compression can't be excluded, there might be some issues like that. Although this sounds like corner case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have add the feature
ipc_compression = "lz4,zstd"
as an option, if you want to compile with the ipc compression feature.