Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
matrix:
java-version:
- 22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, we should remove 22, since it's already EOL .

- 24
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/io/airlift/compress/v3/lz4/Lz4Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@

import java.lang.foreign.MemorySegment;

import static io.airlift.compress.v3.lz4.Lz4Native.DEFAULT_ACCELERATION;

public sealed interface Lz4Compressor
extends Compressor
permits Lz4JavaCompressor, Lz4NativeCompressor
{
int compress(MemorySegment input, MemorySegment output);

static Lz4Compressor create()
{
return create(DEFAULT_ACCELERATION);
}

static Lz4Compressor create(int acceleration)
{
if (Lz4NativeCompressor.isEnabled()) {
return new Lz4NativeCompressor();
return new Lz4NativeCompressor(acceleration);
}

if (acceleration != DEFAULT_ACCELERATION) {
throw new IllegalArgumentException("Acceleration different from default cannot be used for non-native LZ4 compression");
}
return new Lz4JavaCompressor();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/airlift/compress/v3/lz4/Lz4Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private record MethodHandles(

// Defined in lz4.h: https://github.com/lz4/lz4/blob/v1.9.4/lib/lz4.c#L51
public static final int DEFAULT_ACCELERATION = 1;
public static final int MAX_ACCELERATION = 65537;
public static final int STATE_SIZE;

static {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/io/airlift/compress/v3/lz4/Lz4NativeCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@
import java.lang.foreign.MemorySegment;

import static io.airlift.compress.v3.lz4.Lz4Native.DEFAULT_ACCELERATION;
import static io.airlift.compress.v3.lz4.Lz4Native.MAX_ACCELERATION;
import static java.lang.Math.toIntExact;

public final class Lz4NativeCompressor
implements Lz4Compressor
{
private final MemorySegment state = Arena.ofAuto().allocate(Lz4Native.STATE_SIZE);
private final int acceleration;

public Lz4NativeCompressor()
{
this(DEFAULT_ACCELERATION);
}

public Lz4NativeCompressor(int acceleration)
{
if (acceleration < DEFAULT_ACCELERATION || acceleration > MAX_ACCELERATION) {
throw new IllegalArgumentException("LZ4 acceleration should be in the [%d, %d] range but got %d".formatted(DEFAULT_ACCELERATION, MAX_ACCELERATION, acceleration));
}
Lz4Native.verifyEnabled();
this.acceleration = acceleration;
}

public static boolean isEnabled()
Expand All @@ -45,12 +56,12 @@ public int compress(byte[] input, int inputOffset, int inputLength, byte[] outpu
{
MemorySegment inputSegment = MemorySegment.ofArray(input).asSlice(inputOffset, inputLength);
MemorySegment outputSegment = MemorySegment.ofArray(output).asSlice(outputOffset, maxOutputLength);
return Lz4Native.compress(inputSegment, inputLength, outputSegment, maxOutputLength, DEFAULT_ACCELERATION, state);
return Lz4Native.compress(inputSegment, inputLength, outputSegment, maxOutputLength, acceleration, state);
}

@Override
public int compress(MemorySegment input, MemorySegment output)
{
return Lz4Native.compress(input, toIntExact(input.byteSize()), output, toIntExact(output.byteSize()), DEFAULT_ACCELERATION, state);
return Lz4Native.compress(input, toIntExact(input.byteSize()), output, toIntExact(output.byteSize()), acceleration, state);
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/airlift/compress/v3/zstd/ZstdCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ static ZstdCompressor create()
}
return new ZstdJavaCompressor();
}

static ZstdCompressor create(int compressionLevel)
{
if (ZstdNativeCompressor.isEnabled()) {
return new ZstdNativeCompressor(compressionLevel);
}
if (compressionLevel != CompressionParameters.DEFAULT_COMPRESSION_LEVEL) {
throw new IllegalArgumentException("Compression level different from default cannot be used for non-native Zstd compressor");
}
return new ZstdJavaCompressor();
}
}
50 changes: 50 additions & 0 deletions src/test/java/io/airlift/compress/v3/lz4/TestLz4NativeFastest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.v3.lz4;

import io.airlift.compress.v3.Compressor;
import io.airlift.compress.v3.Decompressor;
import io.airlift.compress.v3.thirdparty.JPountzLz4Compressor;
import io.airlift.compress.v3.thirdparty.JPountzLz4Decompressor;
import net.jpountz.lz4.LZ4Factory;

import static io.airlift.compress.v3.lz4.Lz4Native.MAX_ACCELERATION;

class TestLz4NativeFastest
extends AbstractTestLz4
{
@Override
protected Compressor getCompressor()
{
return new Lz4NativeCompressor(MAX_ACCELERATION);
}

@Override
protected Decompressor getDecompressor()
{
return new Lz4NativeDecompressor();
}

@Override
protected Compressor getVerifyCompressor()
{
return new JPountzLz4Compressor(LZ4Factory.fastestInstance());
}

@Override
protected Decompressor getVerifyDecompressor()
{
return new JPountzLz4Decompressor(LZ4Factory.fastestInstance());
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/airlift/compress/v3/zstd/TestZstdFast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.v3.zstd;

import io.airlift.compress.v3.Compressor;
import io.airlift.compress.v3.Decompressor;
import io.airlift.compress.v3.thirdparty.ZstdJniCompressor;
import io.airlift.compress.v3.thirdparty.ZstdJniDecompressor;

public class TestZstdFast
extends AbstractTestZstd
{
@Override
protected ZstdCompressor getCompressor()
{
return new ZstdNativeCompressor(-7);
}

@Override
protected ZstdDecompressor getDecompressor()
{
return new ZstdNativeDecompressor();
}

@Override
protected Compressor getVerifyCompressor()
{
return new ZstdJniCompressor(-7);
}

@Override
protected Decompressor getVerifyDecompressor()
{
return new ZstdJniDecompressor();
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/airlift/compress/v3/zstd/TestZstdHigh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.v3.zstd;

import io.airlift.compress.v3.Compressor;
import io.airlift.compress.v3.Decompressor;
import io.airlift.compress.v3.thirdparty.ZstdJniCompressor;
import io.airlift.compress.v3.thirdparty.ZstdJniDecompressor;

public class TestZstdHigh
extends AbstractTestZstd
{
@Override
protected ZstdCompressor getCompressor()
{
return new ZstdNativeCompressor(8);
}

@Override
protected ZstdDecompressor getDecompressor()
{
return new ZstdNativeDecompressor();
}

@Override
protected Compressor getVerifyCompressor()
{
return new ZstdJniCompressor(8);
}

@Override
protected Decompressor getVerifyDecompressor()
{
return new ZstdJniDecompressor();
}
}