-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-17125. Using snappy-java in SnappyCodec #2201
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
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,9 @@ | |
|
|
||
| import org.apache.hadoop.io.compress.Decompressor; | ||
| import org.apache.hadoop.io.compress.DirectDecompressor; | ||
| import org.apache.hadoop.util.NativeCodeLoader; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.xerial.snappy.Snappy; | ||
|
|
||
| /** | ||
| * A {@link Decompressor} based on the snappy compression algorithm. | ||
|
|
@@ -45,24 +45,6 @@ public class SnappyDecompressor implements Decompressor { | |
| private int userBufOff = 0, userBufLen = 0; | ||
| private boolean finished; | ||
|
|
||
| private static boolean nativeSnappyLoaded = false; | ||
|
|
||
| static { | ||
| if (NativeCodeLoader.isNativeCodeLoaded() && | ||
| NativeCodeLoader.buildSupportsSnappy()) { | ||
| try { | ||
| initIDs(); | ||
| nativeSnappyLoaded = true; | ||
| } catch (Throwable t) { | ||
| LOG.error("failed to load SnappyDecompressor", t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean isNativeCodeLoaded() { | ||
| return nativeSnappyLoaded; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new compressor. | ||
| * | ||
|
|
@@ -201,7 +183,7 @@ public boolean finished() { | |
| * {@link #needsInput()} should be called in order to determine if more | ||
| * input data is required. | ||
| * | ||
| * @param b Buffer for the compressed data | ||
| * @param b Buffer for the uncompressed data | ||
| * @param off Start offset of the data | ||
| * @param len Size of the buffer | ||
| * @return The actual number of bytes of compressed data. | ||
|
|
@@ -276,13 +258,27 @@ public void end() { | |
| // do nothing | ||
| } | ||
|
|
||
| private native static void initIDs(); | ||
| private int decompressBytesDirect() throws IOException { | ||
| if (compressedDirectBufLen == 0) { | ||
| return 0; | ||
| } else { | ||
| // Set the position and limit of `compressedDirectBuf` for reading | ||
| compressedDirectBuf.position(0).limit(compressedDirectBufLen); | ||
| // There is compressed input, decompress it now. | ||
| int size = Snappy.uncompressedLength((ByteBuffer) compressedDirectBuf); | ||
| if (size > uncompressedDirectBuf.capacity()) { | ||
|
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. Should we check with
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. Before calling
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.
|
||
| throw new IOException("Could not decompress data. " + | ||
| "uncompressedDirectBuf length is too small."); | ||
| } | ||
| size = Snappy.uncompress((ByteBuffer) compressedDirectBuf, | ||
| (ByteBuffer) uncompressedDirectBuf); | ||
| return size; | ||
| } | ||
| } | ||
|
|
||
| private native int decompressBytesDirect(); | ||
|
|
||
| int decompressDirect(ByteBuffer src, ByteBuffer dst) throws IOException { | ||
| assert (this instanceof SnappyDirectDecompressor); | ||
|
|
||
| ByteBuffer presliced = dst; | ||
| if (dst.position() > 0) { | ||
| presliced = dst; | ||
|
|
@@ -311,10 +307,10 @@ int decompressDirect(ByteBuffer src, ByteBuffer dst) throws IOException { | |
| } | ||
| return n; | ||
| } | ||
|
|
||
| public static class SnappyDirectDecompressor extends SnappyDecompressor implements | ||
| DirectDecompressor { | ||
|
|
||
| @Override | ||
| public boolean finished() { | ||
| return (endOfInput && super.finished()); | ||
|
|
||
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.
I'm not sure we need to set position and limit here? If
compressedDirectBufalready has been set with position and limit before callingdecompressBytesDirect? Won't we read wrong data from this buffer?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.
decompressBytesDirectis the only call that reads the data fromcompressedDirectBuf, so I think we should read it from the beginning. IncompressedDirectBuf, we should fully decompress thecompressedDirectBuf.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.
For
decompressDirect,compressedDirectBufis set up correctly before callingdecompressBytesDirect. So we don't need to do it here again. Fordecompress, I'm not sure, but looks likesetInputFromSavedDataalso takes care aboutcompressedDirectBufreseting. I guess we don't need to do it indecompressBytesDirect.