From 4098469c226bca298c00d1bef1f7566789452c8c Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Thu, 27 Jun 2024 18:26:14 +0200 Subject: [PATCH] Add factory to create a (de)compressor based on native availability --- .../java/io/airlift/compress/v2/lz4/Lz4Compressor.java | 8 ++++++++ .../java/io/airlift/compress/v2/lz4/Lz4Decompressor.java | 8 ++++++++ .../io/airlift/compress/v2/snappy/SnappyCompressor.java | 8 ++++++++ .../io/airlift/compress/v2/snappy/SnappyDecompressor.java | 8 ++++++++ .../java/io/airlift/compress/v2/zstd/ZstdCompressor.java | 8 ++++++++ .../io/airlift/compress/v2/zstd/ZstdDecompressor.java | 8 ++++++++ 6 files changed, 48 insertions(+) diff --git a/src/main/java/io/airlift/compress/v2/lz4/Lz4Compressor.java b/src/main/java/io/airlift/compress/v2/lz4/Lz4Compressor.java index 0b9f0f6d..fc8c41fa 100644 --- a/src/main/java/io/airlift/compress/v2/lz4/Lz4Compressor.java +++ b/src/main/java/io/airlift/compress/v2/lz4/Lz4Compressor.java @@ -22,4 +22,12 @@ public sealed interface Lz4Compressor permits Lz4JavaCompressor, Lz4NativeCompressor { int compress(MemorySegment input, MemorySegment output); + + static Lz4Compressor create() + { + if (Lz4NativeCompressor.isEnabled()) { + return new Lz4NativeCompressor(); + } + return new Lz4JavaCompressor(); + } } diff --git a/src/main/java/io/airlift/compress/v2/lz4/Lz4Decompressor.java b/src/main/java/io/airlift/compress/v2/lz4/Lz4Decompressor.java index 233d642f..0dcd1ebc 100644 --- a/src/main/java/io/airlift/compress/v2/lz4/Lz4Decompressor.java +++ b/src/main/java/io/airlift/compress/v2/lz4/Lz4Decompressor.java @@ -22,4 +22,12 @@ public sealed interface Lz4Decompressor permits Lz4JavaDecompressor, Lz4NativeDecompressor { int decompress(MemorySegment input, MemorySegment output); + + static Lz4Decompressor create() + { + if (Lz4NativeDecompressor.isEnabled()) { + return new Lz4NativeDecompressor(); + } + return new Lz4JavaDecompressor(); + } } diff --git a/src/main/java/io/airlift/compress/v2/snappy/SnappyCompressor.java b/src/main/java/io/airlift/compress/v2/snappy/SnappyCompressor.java index 18423953..8d544257 100644 --- a/src/main/java/io/airlift/compress/v2/snappy/SnappyCompressor.java +++ b/src/main/java/io/airlift/compress/v2/snappy/SnappyCompressor.java @@ -22,4 +22,12 @@ public sealed interface SnappyCompressor permits SnappyJavaCompressor, SnappyNativeCompressor { int compress(MemorySegment input, MemorySegment output); + + static SnappyCompressor create() + { + if (SnappyNativeCompressor.isEnabled()) { + return new SnappyNativeCompressor(); + } + return new SnappyJavaCompressor(); + } } diff --git a/src/main/java/io/airlift/compress/v2/snappy/SnappyDecompressor.java b/src/main/java/io/airlift/compress/v2/snappy/SnappyDecompressor.java index f9ed1e37..6a20ec94 100644 --- a/src/main/java/io/airlift/compress/v2/snappy/SnappyDecompressor.java +++ b/src/main/java/io/airlift/compress/v2/snappy/SnappyDecompressor.java @@ -20,4 +20,12 @@ public sealed interface SnappyDecompressor permits SnappyJavaDecompressor, SnappyNativeDecompressor { int getUncompressedLength(byte[] compressed, int compressedOffset); + + static SnappyDecompressor create() + { + if (SnappyNativeDecompressor.isEnabled()) { + return new SnappyNativeDecompressor(); + } + return new SnappyJavaDecompressor(); + } } diff --git a/src/main/java/io/airlift/compress/v2/zstd/ZstdCompressor.java b/src/main/java/io/airlift/compress/v2/zstd/ZstdCompressor.java index 8c2a725c..74e8deac 100644 --- a/src/main/java/io/airlift/compress/v2/zstd/ZstdCompressor.java +++ b/src/main/java/io/airlift/compress/v2/zstd/ZstdCompressor.java @@ -21,4 +21,12 @@ public interface ZstdCompressor extends Compressor { int compress(MemorySegment input, MemorySegment output); + + static ZstdCompressor create() + { + if (ZstdNativeCompressor.isEnabled()) { + return new ZstdNativeCompressor(); + } + return new ZstdJavaCompressor(); + } } diff --git a/src/main/java/io/airlift/compress/v2/zstd/ZstdDecompressor.java b/src/main/java/io/airlift/compress/v2/zstd/ZstdDecompressor.java index 44a164ca..e54420f3 100644 --- a/src/main/java/io/airlift/compress/v2/zstd/ZstdDecompressor.java +++ b/src/main/java/io/airlift/compress/v2/zstd/ZstdDecompressor.java @@ -19,4 +19,12 @@ public interface ZstdDecompressor extends Decompressor { long getDecompressedSize(byte[] input, int offset, int length); + + static ZstdDecompressor create() + { + if (ZstdNativeDecompressor.isEnabled()) { + return new ZstdNativeDecompressor(); + } + return new ZstdJavaDecompressor(); + } }