-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-17292. Using lz4-java in Lz4Codec #2350
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
f40b965
5571c25
9ed789e
1edddbd
ffda7e8
d1fa992
c35a8ce
41e59b1
c02e8ca
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 |
|---|---|---|
|
|
@@ -22,9 +22,11 @@ | |
| import java.nio.Buffer; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import net.jpountz.lz4.LZ4Factory; | ||
| import net.jpountz.lz4.LZ4Compressor; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.io.compress.Compressor; | ||
| import org.apache.hadoop.util.NativeCodeLoader; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -49,22 +51,7 @@ public class Lz4Compressor implements Compressor { | |
| private long bytesRead = 0L; | ||
| private long bytesWritten = 0L; | ||
|
|
||
| private final boolean useLz4HC; | ||
|
|
||
| static { | ||
| if (NativeCodeLoader.isNativeCodeLoaded()) { | ||
| // Initialize the native library | ||
| try { | ||
| initIDs(); | ||
| } catch (Throwable t) { | ||
| // Ignore failure to load/initialize lz4 | ||
| LOG.warn(t.toString()); | ||
| } | ||
| } else { | ||
| LOG.error("Cannot load " + Lz4Compressor.class.getName() + | ||
| " without native hadoop library!"); | ||
| } | ||
| } | ||
| private final LZ4Compressor lz4Compressor; | ||
|
|
||
| /** | ||
| * Creates a new compressor. | ||
|
|
@@ -74,9 +61,21 @@ public class Lz4Compressor implements Compressor { | |
| * which trades CPU for compression ratio. | ||
| */ | ||
| public Lz4Compressor(int directBufferSize, boolean useLz4HC) { | ||
| this.useLz4HC = useLz4HC; | ||
| this.directBufferSize = directBufferSize; | ||
|
|
||
| try { | ||
| LZ4Factory lz4Factory = LZ4Factory.fastestInstance(); | ||
| if (useLz4HC) { | ||
|
||
| lz4Compressor = lz4Factory.highCompressor(); | ||
|
||
| } else { | ||
| lz4Compressor = lz4Factory.fastCompressor(); | ||
| } | ||
| } catch (AssertionError t) { | ||
| throw new RuntimeException("lz4-java library is not available: " + | ||
| "Lz4Compressor has not been loaded. You need to add " + | ||
| "lz4-java.jar to your CLASSPATH. " + t, t); | ||
| } | ||
|
|
||
| uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); | ||
|
|
||
| // Compression is guaranteed to succeed if 'dstCapacity' >= | ||
|
|
@@ -243,7 +242,7 @@ public synchronized int compress(byte[] b, int off, int len) | |
| } | ||
|
|
||
| // Compress data | ||
| n = useLz4HC ? compressBytesDirectHC() : compressBytesDirect(); | ||
| n = compressDirectBuf(); | ||
| compressedDirectBuf.limit(n); | ||
| uncompressedDirectBuf.clear(); // lz4 consumes all buffer input | ||
|
|
||
|
|
@@ -309,11 +308,20 @@ public synchronized long getBytesWritten() { | |
| public synchronized void end() { | ||
| } | ||
|
|
||
| private native static void initIDs(); | ||
|
|
||
| private native int compressBytesDirect(); | ||
|
|
||
| private native int compressBytesDirectHC(); | ||
|
|
||
| public native static String getLibraryName(); | ||
| private int compressDirectBuf() { | ||
|
||
| if (uncompressedDirectBufLen == 0) { | ||
| return 0; | ||
| } else { | ||
| // Set the position and limit of `uncompressedDirectBuf` for reading | ||
| uncompressedDirectBuf.limit(uncompressedDirectBufLen).position(0); | ||
| compressedDirectBuf.clear(); | ||
|
||
| lz4Compressor.compress((ByteBuffer) uncompressedDirectBuf, | ||
| (ByteBuffer) compressedDirectBuf); | ||
| uncompressedDirectBufLen = 0; | ||
| uncompressedDirectBuf.limit(directBufferSize).position(0); | ||
| int size = compressedDirectBuf.position(); | ||
| compressedDirectBuf.position(0); | ||
| return size; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,10 @@ | |
| import java.nio.Buffer; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import net.jpountz.lz4.LZ4Factory; | ||
| import net.jpountz.lz4.LZ4SafeDecompressor; | ||
|
||
|
|
||
| import org.apache.hadoop.io.compress.Decompressor; | ||
| import org.apache.hadoop.util.NativeCodeLoader; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -44,20 +46,7 @@ public class Lz4Decompressor implements Decompressor { | |
| private int userBufOff = 0, userBufLen = 0; | ||
| private boolean finished; | ||
|
|
||
| static { | ||
| if (NativeCodeLoader.isNativeCodeLoaded()) { | ||
| // Initialize the native library | ||
| try { | ||
| initIDs(); | ||
| } catch (Throwable t) { | ||
| // Ignore failure to load/initialize lz4 | ||
| LOG.warn(t.toString()); | ||
| } | ||
| } else { | ||
| LOG.error("Cannot load " + Lz4Compressor.class.getName() + | ||
| " without native hadoop library!"); | ||
| } | ||
| } | ||
| private LZ4SafeDecompressor lz4Decompressor; | ||
|
|
||
| /** | ||
| * Creates a new compressor. | ||
|
|
@@ -67,6 +56,15 @@ public class Lz4Decompressor implements Decompressor { | |
| public Lz4Decompressor(int directBufferSize) { | ||
| this.directBufferSize = directBufferSize; | ||
|
|
||
| try { | ||
| LZ4Factory lz4Factory = LZ4Factory.fastestInstance(); | ||
| lz4Decompressor = lz4Factory.safeDecompressor(); | ||
| } catch (AssertionError t) { | ||
| throw new RuntimeException("lz4-java library is not available: " + | ||
| "Lz4Decompressor has not been loaded. You need to add " + | ||
| "lz4-java.jar to your CLASSPATH. " + t, t); | ||
| } | ||
|
|
||
| compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); | ||
| uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); | ||
| uncompressedDirectBuf.position(directBufferSize); | ||
|
|
@@ -200,7 +198,7 @@ public synchronized boolean finished() { | |
| * @param b Buffer for the compressed data | ||
| * @param off Start offset of the data | ||
| * @param len Size of the buffer | ||
| * @return The actual number of bytes of compressed data. | ||
| * @return The actual number of bytes of uncompressed data. | ||
| * @throws IOException | ||
| */ | ||
| @Override | ||
|
|
@@ -228,7 +226,7 @@ public synchronized int decompress(byte[] b, int off, int len) | |
| uncompressedDirectBuf.limit(directBufferSize); | ||
|
|
||
| // Decompress data | ||
| n = decompressBytesDirect(); | ||
| n = decompressDirectBuf(); | ||
| uncompressedDirectBuf.limit(n); | ||
|
|
||
| if (userBufLen <= 0) { | ||
|
|
@@ -272,7 +270,18 @@ public synchronized void end() { | |
| // do nothing | ||
| } | ||
|
|
||
| private native static void initIDs(); | ||
|
|
||
| private native int decompressBytesDirect(); | ||
| private int decompressDirectBuf() { | ||
| if (compressedDirectBufLen == 0) { | ||
|
||
| return 0; | ||
| } else { | ||
| compressedDirectBuf.limit(compressedDirectBufLen).position(0); | ||
| lz4Decompressor.decompress((ByteBuffer) compressedDirectBuf, | ||
| (ByteBuffer) uncompressedDirectBuf); | ||
| compressedDirectBufLen = 0; | ||
| compressedDirectBuf.clear(); | ||
| int size = uncompressedDirectBuf.position(); | ||
| uncompressedDirectBuf.position(0); | ||
| return size; | ||
| } | ||
| } | ||
| } | ||
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.
put into the same import block as org.sjf4j
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.
They are already in same block, no?