-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add support for LZ4 frame format #142
Open
findepi
wants to merge
2
commits into
airlift:master
Choose a base branch
from
findepi:findepi/lz4frame
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/main/java/io/airlift/compress/lz4/Lz4FrameCompressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Licensed 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 io.airlift.compress.lz4; | ||
|
||
import io.airlift.compress.Compressor; | ||
|
||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
|
||
import static io.airlift.compress.lz4.Lz4RawCompressor.MAX_TABLE_SIZE; | ||
import static io.airlift.compress.lz4.UnsafeUtil.getAddress; | ||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; | ||
|
||
/** | ||
* This class is not thread-safe | ||
*/ | ||
public class Lz4FrameCompressor | ||
implements Compressor | ||
{ | ||
private final int[] table = new int[MAX_TABLE_SIZE]; | ||
|
||
@Override | ||
public int maxCompressedLength(int uncompressedSize) | ||
{ | ||
return Lz4FrameRawCompressor.maxCompressedLength(uncompressedSize); | ||
} | ||
|
||
@Override | ||
public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) | ||
{ | ||
verifyRange(input, inputOffset, inputLength); | ||
verifyRange(output, outputOffset, maxOutputLength); | ||
|
||
long inputAddress = ARRAY_BYTE_BASE_OFFSET + inputOffset; | ||
long outputAddress = ARRAY_BYTE_BASE_OFFSET + outputOffset; | ||
|
||
return Lz4FrameRawCompressor.compress( | ||
input, | ||
inputAddress, | ||
inputLength, | ||
output, | ||
outputAddress, | ||
maxOutputLength, | ||
table); | ||
} | ||
|
||
@Override | ||
public void compress(ByteBuffer inputBuffer, ByteBuffer outputBuffer) | ||
{ | ||
if (true) { | ||
// TODO support byte buffers, see disabled tests | ||
throw new UnsupportedOperationException("This is disabled, does not work with direct buffers yet"); | ||
} | ||
|
||
// Java 9+ added an overload of various methods in ByteBuffer. When compiling with Java 11+ and targeting Java 8 bytecode | ||
// the resulting signatures are invalid for JDK 8, so accesses below result in NoSuchMethodError. Accessing the | ||
// methods through the interface class works around the problem | ||
// Sidenote: we can't target "javac --release 8" because Unsafe is not available in the signature data for that profile | ||
Buffer input = inputBuffer; | ||
Buffer output = outputBuffer; | ||
|
||
Object inputBase; | ||
long inputAddress; | ||
int inputLimit; | ||
if (input.isDirect()) { | ||
inputBase = null; | ||
long address = getAddress(input); | ||
inputAddress = address + input.position(); | ||
inputLimit = input.limit(); | ||
} | ||
else if (input.hasArray()) { | ||
inputBase = input.array(); | ||
inputAddress = ARRAY_BYTE_BASE_OFFSET + input.arrayOffset() + input.position(); | ||
inputLimit = input.limit(); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unsupported input ByteBuffer implementation " + input.getClass().getName()); | ||
} | ||
|
||
Object outputBase; | ||
long outputAddress; | ||
int outputLimit; | ||
if (output.isDirect()) { | ||
outputBase = null; | ||
long address = getAddress(output); | ||
outputAddress = address + output.position(); | ||
outputLimit = output.limit(); | ||
} | ||
else if (output.hasArray()) { | ||
outputBase = output.array(); | ||
outputAddress = ARRAY_BYTE_BASE_OFFSET + output.arrayOffset() + output.position(); | ||
outputLimit = output.limit(); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unsupported output ByteBuffer implementation " + output.getClass().getName()); | ||
} | ||
|
||
// HACK: Assure JVM does not collect Slice wrappers while compressing, since the | ||
// collection may trigger freeing of the underlying memory resulting in a segfault | ||
// There is no other known way to signal to the JVM that an object should not be | ||
// collected in a block, and technically, the JVM is allowed to eliminate these locks. | ||
synchronized (input) { | ||
synchronized (output) { | ||
int written = Lz4FrameRawCompressor.compress( | ||
inputBase, | ||
inputAddress, | ||
inputLimit, | ||
outputBase, | ||
outputAddress, | ||
outputLimit, | ||
table); | ||
output.position(output.position() + written); | ||
} | ||
} | ||
} | ||
|
||
private static void verifyRange(byte[] data, int offset, int length) | ||
{ | ||
requireNonNull(data, "data is null"); | ||
if (offset < 0 || length < 0 || offset + length > data.length) { | ||
throw new IllegalArgumentException(format("Invalid offset or length (%s, %s) in array of length %s", offset, length, data.length)); | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/main/java/io/airlift/compress/lz4/Lz4FrameDecompressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed 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 io.airlift.compress.lz4; | ||
|
||
import io.airlift.compress.Decompressor; | ||
import io.airlift.compress.MalformedInputException; | ||
|
||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
|
||
import static io.airlift.compress.lz4.UnsafeUtil.getAddress; | ||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; | ||
|
||
public class Lz4FrameDecompressor | ||
implements Decompressor | ||
{ | ||
@Override | ||
public int decompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) | ||
throws MalformedInputException | ||
{ | ||
verifyRange(input, inputOffset, inputLength); | ||
verifyRange(output, outputOffset, maxOutputLength); | ||
|
||
return Lz4FrameRawDecompressor.decompress( | ||
input, | ||
ARRAY_BYTE_BASE_OFFSET + inputOffset, | ||
inputLength, | ||
output, | ||
ARRAY_BYTE_BASE_OFFSET + outputOffset, | ||
maxOutputLength); | ||
} | ||
|
||
@Override | ||
public void decompress(ByteBuffer inputBuffer, ByteBuffer outputBuffer) | ||
throws MalformedInputException | ||
{ | ||
if (true) { | ||
// TODO support byte buffers, see disabled tests | ||
throw new UnsupportedOperationException("This is disabled, does not work with direct buffers yet"); | ||
} | ||
|
||
// Java 9+ added an overload of various methods in ByteBuffer. When compiling with Java 11+ and targeting Java 8 bytecode | ||
// the resulting signatures are invalid for JDK 8, so accesses below result in NoSuchMethodError. Accessing the | ||
// methods through the interface class works around the problem | ||
// Sidenote: we can't target "javac --release 8" because Unsafe is not available in the signature data for that profile | ||
Buffer input = inputBuffer; | ||
Buffer output = outputBuffer; | ||
|
||
Object inputBase; | ||
long inputAddress; | ||
int inputLimit; | ||
if (input.isDirect()) { | ||
inputBase = null; | ||
long address = getAddress(input); | ||
inputAddress = address + input.position(); | ||
inputLimit = input.limit(); | ||
} | ||
else if (input.hasArray()) { | ||
inputBase = input.array(); | ||
inputAddress = ARRAY_BYTE_BASE_OFFSET + input.arrayOffset() + input.position(); | ||
inputLimit = input.limit(); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unsupported input ByteBuffer implementation " + input.getClass().getName()); | ||
} | ||
|
||
Object outputBase; | ||
long outputAddress; | ||
int outputLimit; | ||
if (output.isDirect()) { | ||
outputBase = null; | ||
long address = getAddress(output); | ||
outputAddress = address + output.position(); | ||
outputLimit = output.limit(); | ||
} | ||
else if (output.hasArray()) { | ||
outputBase = output.array(); | ||
outputAddress = ARRAY_BYTE_BASE_OFFSET + output.arrayOffset() + output.position(); | ||
outputLimit = output.limit(); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unsupported output ByteBuffer implementation " + output.getClass().getName()); | ||
} | ||
|
||
// HACK: Assure JVM does not collect Slice wrappers while decompressing, since the | ||
// collection may trigger freeing of the underlying memory resulting in a segfault | ||
// There is no other known way to signal to the JVM that an object should not be | ||
// collected in a block, and technically, the JVM is allowed to eliminate these locks. | ||
synchronized (input) { | ||
synchronized (output) { | ||
int written = Lz4FrameRawDecompressor.decompress(inputBase, inputAddress, inputLimit, outputBase, outputAddress, outputLimit); | ||
output.position(output.position() + written); | ||
} | ||
} | ||
} | ||
|
||
public static long getDecompressedSize(byte[] input, int offset, int length) | ||
findepi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
int baseAddress = ARRAY_BYTE_BASE_OFFSET + offset; | ||
return Lz4FrameRawDecompressor.getDecompressedSize(input, baseAddress, length); | ||
} | ||
|
||
private static void verifyRange(byte[] data, int offset, int length) | ||
{ | ||
requireNonNull(data, "data is null"); | ||
if (offset < 0 || length < 0 || offset + length > data.length) { | ||
throw new IllegalArgumentException(format("Invalid offset or length (%s, %s) in array of length %s", offset, length, data.length)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TODO