-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-17125. Using snappy-java in SnappyCodec #2297
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 15 commits
23da513
3aa3f55
65fd9f4
9c0f08b
f52dd20
87903a9
5adcf53
40cc18a
0e44d4b
666a37b
9de4712
0ed518d
712749c
2b5a212
1cb398b
aa6a6d5
19edba2
beec931
fc525fa
562b80d
0600169
fd71a20
7dc320d
5685b0b
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 |
|---|---|---|
|
|
@@ -24,9 +24,10 @@ | |
|
|
||
| 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; | ||
| import org.xerial.snappy.Snappy; | ||
| import org.xerial.snappy.SnappyLoader; | ||
|
|
||
| /** | ||
| * A {@link Compressor} based on the snappy compression algorithm. | ||
|
|
@@ -48,30 +49,21 @@ public class SnappyCompressor implements Compressor { | |
| private long bytesRead = 0L; | ||
| private long bytesWritten = 0L; | ||
|
|
||
| private static boolean nativeSnappyLoaded = false; | ||
|
|
||
| static { | ||
| if (NativeCodeLoader.isNativeCodeLoaded() && | ||
| NativeCodeLoader.buildSupportsSnappy()) { | ||
| try { | ||
| initIDs(); | ||
| nativeSnappyLoaded = true; | ||
| } catch (Throwable t) { | ||
| LOG.error("failed to load SnappyCompressor", t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean isNativeCodeLoaded() { | ||
| return nativeSnappyLoaded; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to check if the snappy class is available for SnappyCompressor too.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. |
||
| } | ||
|
|
||
| /** | ||
| * Creates a new compressor. | ||
| * | ||
| * @param directBufferSize size of the direct buffer to be used. | ||
| */ | ||
| public SnappyCompressor(int directBufferSize) { | ||
| // `snappy-java` is provided scope. We need to check if it is available. | ||
| try { | ||
| SnappyLoader.getVersion(); | ||
| } catch (Throwable t) { | ||
| throw new RuntimeException("snappy-java library is not available: " + | ||
| "SnappyCompressor has not been loaded. You need to add " + | ||
| "snappy-java.jar to your CLASSPATH", t); | ||
| } | ||
|
|
||
| this.directBufferSize = directBufferSize; | ||
|
|
||
| uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); | ||
|
|
@@ -225,7 +217,7 @@ public int compress(byte[] b, int off, int len) | |
| } | ||
|
|
||
| // Compress data | ||
| n = compressBytesDirect(); | ||
| n = compressDirectBuf(); | ||
| compressedDirectBuf.limit(n); | ||
| uncompressedDirectBuf.clear(); // snappy consumes all buffer input | ||
|
|
||
|
|
@@ -291,9 +283,17 @@ public long getBytesWritten() { | |
| public void end() { | ||
| } | ||
|
|
||
| private native static void initIDs(); | ||
|
|
||
| private native int compressBytesDirect(); | ||
|
|
||
| public native static String getLibraryName(); | ||
| private int compressDirectBuf() throws IOException { | ||
| if (uncompressedDirectBufLen == 0) { | ||
| return 0; | ||
| } else { | ||
| // Set the position and limit of `uncompressedDirectBuf` for reading | ||
| uncompressedDirectBuf.limit(uncompressedDirectBufLen).position(0); | ||
| int size = Snappy.compress((ByteBuffer) uncompressedDirectBuf, | ||
| (ByteBuffer) compressedDirectBuf); | ||
| uncompressedDirectBufLen = 0; | ||
| uncompressedDirectBuf.limit(uncompressedDirectBuf.capacity()).position(0); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this seems unnecessary as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems so, I remember I added this to fix test failure. It might be |
||
| return size; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.