Repository navigation
[BEAM-6365] Add ZStandard compression support for Java SDK #7416
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
Closed
+106
−7
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ | |
| import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; | ||
| import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream; | ||
| import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; | ||
| import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream; | ||
| import org.joda.time.Instant; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
@@ -113,6 +114,14 @@ public void testAutoSplittable() throws Exception { | |
| source = CompressedSource.from(new ByteSource("input.ZIP", 1)); | ||
| assertFalse(source.isSplittable()); | ||
|
|
||
| // ZSTD files are not splittable | ||
| source = CompressedSource.from(new ByteSource("input.zst", 1)); | ||
| assertFalse(source.isSplittable()); | ||
| source = CompressedSource.from(new ByteSource("input.ZST", 1)); | ||
| assertFalse(source.isSplittable()); | ||
| source = CompressedSource.from(new ByteSource("input.zstd", 1)); | ||
| assertFalse(source.isSplittable()); | ||
|
|
||
| // DEFLATE files are not splittable | ||
| source = CompressedSource.from(new ByteSource("input.deflate", 1)); | ||
| assertFalse(source.isSplittable()); | ||
|
|
@@ -180,6 +189,13 @@ public void testEmptyReadGzip() throws Exception { | |
| runReadTest(input, CompressionMode.GZIP); | ||
| } | ||
|
|
||
| /** Test reading empty input with zstd. */ | ||
| @Test | ||
| public void testEmptyReadZstd() throws Exception { | ||
| byte[] input = generateInput(0); | ||
| runReadTest(input, CompressionMode.ZSTD); | ||
| } | ||
|
|
||
| private static byte[] compressGzip(byte[] input) throws IOException { | ||
| ByteArrayOutputStream res = new ByteArrayOutputStream(); | ||
| try (GZIPOutputStream gzipStream = new GZIPOutputStream(res)) { | ||
|
|
@@ -258,7 +274,14 @@ public void testCompressedReadBzip2() throws Exception { | |
| runReadTest(input, CompressionMode.BZIP2); | ||
| } | ||
|
|
||
| /** Test reading according to filepattern when the file is bzipped. */ | ||
| /** Test reading empty input with zstd. */ | ||
| @Test | ||
| public void testCompressedReadZstd() throws Exception { | ||
| byte[] input = generateInput(0); | ||
| runReadTest(input, CompressionMode.ZSTD); | ||
| } | ||
|
|
||
| /** Test reading according to filepattern when the file is gzipped. */ | ||
| @Test | ||
| public void testCompressedAccordingToFilepatternGzip() throws Exception { | ||
| byte[] input = generateInput(100); | ||
|
|
@@ -267,7 +290,7 @@ public void testCompressedAccordingToFilepatternGzip() throws Exception { | |
| verifyReadContents(input, tmpFile, null /* default auto decompression factory */); | ||
| } | ||
|
|
||
| /** Test reading according to filepattern when the file is gzipped. */ | ||
| /** Test reading according to filepattern when the file is bzipped. */ | ||
|
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. other good catch good. |
||
| @Test | ||
| public void testCompressedAccordingToFilepatternBzip2() throws Exception { | ||
| byte[] input = generateInput(100); | ||
|
|
@@ -276,6 +299,15 @@ public void testCompressedAccordingToFilepatternBzip2() throws Exception { | |
| verifyReadContents(input, tmpFile, null /* default auto decompression factory */); | ||
| } | ||
|
|
||
| /** Test reading according to filepattern when the file is zstd compressed. */ | ||
| @Test | ||
| public void testCompressedAccordingToFilepatternZstd() throws Exception { | ||
| byte[] input = generateInput(100); | ||
| File tmpFile = tmpFolder.newFile("test.zst"); | ||
| writeFile(tmpFile, input, CompressionMode.ZSTD); | ||
| verifyReadContents(input, tmpFile, null /* default auto decompression factory */); | ||
| } | ||
|
|
||
| /** Test reading multiple files with different compression. */ | ||
| @Test | ||
| public void testHeterogeneousCompression() throws Exception { | ||
|
|
@@ -301,6 +333,11 @@ public void testHeterogeneousCompression() throws Exception { | |
| writeFile(bzip2File, generated, CompressionMode.BZIP2); | ||
| expected.addAll(Bytes.asList(generated)); | ||
|
|
||
| File zstdFile = tmpFolder.newFile(baseName + ".zst"); | ||
| generated = generateInput(1000, 4); | ||
| writeFile(zstdFile, generated, CompressionMode.ZSTD); | ||
| expected.addAll(Bytes.asList(generated)); | ||
|
|
||
| String filePattern = new File(tmpFolder.getRoot().toString(), baseName + ".*").toString(); | ||
|
|
||
| CompressedSource<Byte> source = CompressedSource.from(new ByteSource(filePattern, 1)); | ||
|
|
@@ -359,6 +396,18 @@ public void testBzip2FileIsNotSplittable() throws Exception { | |
| assertFalse(source.isSplittable()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testZstdFileIsNotSplittable() throws Exception { | ||
| String baseName = "test-input"; | ||
|
|
||
| File compressedFile = tmpFolder.newFile(baseName + ".zst"); | ||
| writeFile(compressedFile, generateInput(10), CompressionMode.ZSTD); | ||
|
|
||
| CompressedSource<Byte> source = | ||
| CompressedSource.from(new ByteSource(compressedFile.getPath(), 1)); | ||
| assertFalse(source.isSplittable()); | ||
| } | ||
|
|
||
| /** | ||
| * Test reading an uncompressed file with {@link CompressionMode#GZIP}, since we must support this | ||
| * due to properties of services that we read from. | ||
|
|
@@ -383,6 +432,16 @@ public void testFalseBzip2Stream() throws Exception { | |
| verifyReadContents(input, tmpFile, CompressionMode.BZIP2); | ||
| } | ||
|
|
||
| /** Test reading an uncompressed file with {@link Compression#ZSTD}, and show that we fail. */ | ||
| @Test | ||
| public void testFalseZstdStream() throws Exception { | ||
| byte[] input = generateInput(1000); | ||
| File tmpFile = tmpFolder.newFile("test.zst"); | ||
| Files.write(input, tmpFile); | ||
| thrown.expectMessage("Decompression error: Unknown frame descriptor"); | ||
| verifyReadContents(input, tmpFile, CompressionMode.ZSTD); | ||
| } | ||
|
|
||
| /** | ||
| * Test reading an empty input file with gzip; it must be interpreted as uncompressed because the | ||
| * gzip header is two bytes. | ||
|
|
@@ -476,6 +535,8 @@ private OutputStream getOutputStreamForMode(CompressionMode mode, OutputStream s | |
| return new BZip2CompressorOutputStream(stream); | ||
| case ZIP: | ||
| return new TestZipOutputStream(stream); | ||
| case ZSTD: | ||
| return new ZstdCompressorOutputStream(stream); | ||
| case DEFLATE: | ||
| return new DeflateCompressorOutputStream(stream); | ||
| default: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
good catch!