-
Notifications
You must be signed in to change notification settings - Fork 4.1k
ARROW-12110: [Java] Implement ZSTD compression #9822
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
Changes from all commits
b5bcf68
c324527
b97e83f
cd0f4ba
c9216a8
944f655
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,74 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.arrow.compression; | ||
|
|
||
|
|
||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.vector.compression.AbstractCompressionCodec; | ||
| import org.apache.arrow.vector.compression.CompressionUtil; | ||
|
|
||
| import com.github.luben.zstd.Zstd; | ||
|
|
||
| /** | ||
| * Compression codec for the ZSTD algorithm. | ||
| */ | ||
| public class ZstdCompressionCodec extends AbstractCompressionCodec { | ||
|
|
||
| @Override | ||
| protected ArrowBuf doCompress(BufferAllocator allocator, ArrowBuf uncompressedBuffer) { | ||
| long maxSize = Zstd.compressBound(uncompressedBuffer.writerIndex()); | ||
|
Contributor
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 it possible to cause too much waste of memroy in the worst case?
Contributor
Author
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 think this is just a fact of life with compression algorithms. There are potentially ways of mitigating memory waste if changed the APIs around a little bit (trying to pack more data into a single ArrowBuf) or consolidating buffers into a larger buffer if compression level is high. But this is the guidance the documented methods give for sizing buffers. |
||
| long dstSize = CompressionUtil.SIZE_OF_UNCOMPRESSED_LENGTH + maxSize; | ||
| ArrowBuf compressedBuffer = allocator.buffer(dstSize); | ||
| long bytesWritten = Zstd.compressUnsafe( | ||
| compressedBuffer.memoryAddress() + CompressionUtil.SIZE_OF_UNCOMPRESSED_LENGTH, dstSize, | ||
| /*src*/uncompressedBuffer.memoryAddress(), /*srcSize=*/uncompressedBuffer.writerIndex(), | ||
| /*level=*/3); | ||
|
Contributor
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 make the level configurable?
Contributor
Author
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. maybe at some point, the default is 3. LZ4 also has a compression level which I don't think we are setting.
Contributor
Author
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'll open up a JIRA to track configuration. |
||
| if (Zstd.isError(bytesWritten)) { | ||
| compressedBuffer.close(); | ||
| throw new RuntimeException("Error compressing: " + Zstd.getErrorName(bytesWritten)); | ||
| } | ||
| compressedBuffer.writerIndex(CompressionUtil.SIZE_OF_UNCOMPRESSED_LENGTH + bytesWritten); | ||
| return compressedBuffer; | ||
| } | ||
|
|
||
| @Override | ||
| protected ArrowBuf doDecompress(BufferAllocator allocator, ArrowBuf compressedBuffer) { | ||
| long decompressedLength = readUncompressedLength(compressedBuffer); | ||
| ArrowBuf uncompressedBuffer = allocator.buffer(decompressedLength); | ||
| long decompressedSize = Zstd.decompressUnsafe(uncompressedBuffer.memoryAddress(), decompressedLength, | ||
| /*src=*/compressedBuffer.memoryAddress() + CompressionUtil.SIZE_OF_UNCOMPRESSED_LENGTH, | ||
| compressedBuffer.writerIndex() - CompressionUtil.SIZE_OF_UNCOMPRESSED_LENGTH); | ||
| if (Zstd.isError(decompressedSize)) { | ||
| uncompressedBuffer.close(); | ||
| throw new RuntimeException("Error decompressing: " + Zstd.getErrorName(decompressedLength)); | ||
| } | ||
| if (decompressedLength != decompressedSize) { | ||
| uncompressedBuffer.close(); | ||
| throw new RuntimeException("Expected != actual decompressed length: " + | ||
| decompressedLength + " != " + decompressedSize); | ||
| } | ||
| uncompressedBuffer.writerIndex(decompressedLength); | ||
| return uncompressedBuffer; | ||
| } | ||
|
|
||
| @Override | ||
| public CompressionUtil.CodecType getCodecType() { | ||
| return CompressionUtil.CodecType.ZSTD; | ||
| } | ||
| } | ||
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.
A downside to removing this comment completely is that someone might not easily figure out how to skip tests based on name (when working on another implementation).
Do you mind adding a comment to the effect that if an implementer is skipping one of the compression formats, they could use
if name == 'format'?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.
Added a link back to this PR>