Skip to content

Commit

Permalink
Add bridge between aircompressor v1 and v2
Browse files Browse the repository at this point in the history
Additionally: seal interfaces and add factory methods
  • Loading branch information
wendigo committed Jul 3, 2024
1 parent 3b4a4b1 commit 129f0da
Show file tree
Hide file tree
Showing 37 changed files with 879 additions and 191 deletions.
28 changes: 28 additions & 0 deletions src/main/java/io/airlift/compress/Compressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

import java.nio.ByteBuffer;

public interface Compressor
{
int maxCompressedLength(int uncompressedSize);

/**
* @return number of bytes written to the output
*/
int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength);

void compress(ByteBuffer input, ByteBuffer output);
}
28 changes: 28 additions & 0 deletions src/main/java/io/airlift/compress/Decompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

import java.nio.ByteBuffer;

public interface Decompressor
{
/**
* @return number of bytes written to the output
*/
int decompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
throws MalformedInputException;

void decompress(ByteBuffer input, ByteBuffer output)
throws MalformedInputException;
}
36 changes: 36 additions & 0 deletions src/main/java/io/airlift/compress/MalformedInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

public class MalformedInputException
extends RuntimeException
{
private final long offset;

public MalformedInputException(long offset)
{
this(offset, "Malformed input");
}

public MalformedInputException(long offset, String reason)
{
super(reason + ": offset=" + offset);
this.offset = offset;
}

public long getOffset()
{
return offset;
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/bzip2/BZip2Codec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.bzip2;

public class BZip2Codec
extends io.airlift.compress.v2.bzip2.BZip2Codec
{
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/deflate/JdkDeflateCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.deflate;

public class JdkDeflateCodec
extends io.airlift.compress.v2.deflate.JdkDeflateCodec
{
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/gzip/JdkGzipCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.gzip;

public class JdkGzipCodec
extends io.airlift.compress.v2.gzip.JdkGzipCodec
{
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/hadoop/HadoopStreams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.hadoop;

public interface HadoopStreams
extends io.airlift.compress.v2.hadoop.HadoopStreams
{
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/lz4/Lz4Codec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

public class Lz4Codec
extends io.airlift.compress.v2.lz4.Lz4Codec
{
}
60 changes: 60 additions & 0 deletions src/main/java/io/airlift/compress/lz4/Lz4Compressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 io.airlift.compress.v2.lz4.Lz4JavaCompressor;
import io.airlift.compress.v2.lz4.Lz4NativeCompressor;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;

import static java.lang.Math.toIntExact;

public class Lz4Compressor
implements Compressor
{
private static final boolean NATIVE_ENABLED = Lz4NativeCompressor.isEnabled();
private final io.airlift.compress.v2.lz4.Lz4Compressor compressor;

public Lz4Compressor()
{
this.compressor = NATIVE_ENABLED ? new Lz4NativeCompressor() : new Lz4JavaCompressor();
}

@Override
public int maxCompressedLength(int uncompressedSize)
{
return compressor.maxCompressedLength(uncompressedSize);
}

@Override
public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
{
MemorySegment inputSegment = MemorySegment.ofArray(input).asSlice(inputOffset, inputLength);
MemorySegment outputSegment = MemorySegment.ofArray(output).asSlice(outputOffset, maxOutputLength);

return toIntExact(compressor.compress(inputSegment, outputSegment));
}

@Override
public void compress(ByteBuffer input, ByteBuffer output)
{
MemorySegment inputSegment = MemorySegment.ofBuffer(input);
MemorySegment outputSegment = MemorySegment.ofBuffer(output);

int written = compressor.compress(inputSegment, outputSegment);
output.position(output.position() + written);
}
}
57 changes: 57 additions & 0 deletions src/main/java/io/airlift/compress/lz4/Lz4Decompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 io.airlift.compress.v2.lz4.Lz4JavaDecompressor;
import io.airlift.compress.v2.lz4.Lz4NativeDecompressor;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;

import static java.lang.Math.toIntExact;

public class Lz4Decompressor
implements Decompressor
{
private static final boolean NATIVE_ENABLED = Lz4NativeDecompressor.isEnabled();
private final io.airlift.compress.v2.lz4.Lz4Decompressor decompressor;

public Lz4Decompressor()
{
this.decompressor = NATIVE_ENABLED ? new Lz4NativeDecompressor() : new Lz4JavaDecompressor();
}

@Override
public int decompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
throws MalformedInputException
{
MemorySegment inputSegment = MemorySegment.ofArray(input).asSlice(inputOffset, inputLength);
MemorySegment outputSegment = MemorySegment.ofArray(output).asSlice(outputOffset, maxOutputLength);

return toIntExact(decompressor.decompress(inputSegment, outputSegment));
}

@Override
public void decompress(ByteBuffer input, ByteBuffer output)
throws MalformedInputException
{
MemorySegment inputSegment = MemorySegment.ofBuffer(input);
MemorySegment outputSegment = MemorySegment.ofBuffer(output);

int written = decompressor.decompress(inputSegment, outputSegment);
output.position(output.position() + written);
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/airlift/compress/lz4/Lz4RawCompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

public class Lz4RawCompressor
{
private static final int HASH_LOG = 12;

public static final int MAX_TABLE_SIZE = (1 << HASH_LOG);

private Lz4RawCompressor() {}

public static int maxCompressedLength(int sourceLength)
{
return sourceLength + sourceLength / 255 + 16;
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/airlift/compress/lzo/LzoCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.lzo;

public class LzoCodec
extends io.airlift.compress.v2.lzo.LzoCodec
{
}
Loading

0 comments on commit 129f0da

Please sign in to comment.