This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
adding gzip compression support to the schema #4220
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c7718e
adding gzip compression support to the schema
ca5c873
bugfix in stream renderer.
b464f4c
bugfix in stream renderer.
6b4bd28
Merge branch 'gzip_stream_support' of https://github.com/mmigdiso/atl…
195cf95
Merge branch 'gzip_stream_support' of https://github.com/mmigdiso/atl…
e64b0b3
refactor gzip logic
f8aa774
Merge branch 'gzip_stream_support' of https://github.com/mmigdiso/atl…
e918eaf
adding changelog
bc6fe85
adding automatic compressor detection
002e9d6
review fix
1b42bf6
review fix
7acd139
fixed a potential bug in compression detection
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 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 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 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
95 changes: 95 additions & 0 deletions
95
atlasdb-commons/src/main/java/com/palantir/common/compression/ClientCompressor.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.common.compression; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.UnaryOperator; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import com.google.common.collect.MoreCollectors; | ||
import com.google.common.io.ByteStreams; | ||
import com.palantir.common.base.Throwables; | ||
|
||
import net.jpountz.lz4.LZ4BlockInputStream; | ||
|
||
public enum ClientCompressor { | ||
GZIP(in -> { | ||
try { | ||
return new GzipCompressingInputStream(in); | ||
} catch (Exception exc) { | ||
throw new RuntimeException(exc); | ||
} | ||
}, in -> { | ||
try { | ||
return new GZIPInputStream(in); | ||
} catch (Exception exc) { | ||
throw new RuntimeException(exc); | ||
} | ||
}, GzipCompressingInputStream.GZIP_HEADER), | ||
LZ4(LZ4CompressingInputStream::new, LZ4BlockInputStream::new, | ||
new byte[] {'L', 'Z', '4', 'B', 'l', 'o', 'c', 'k'}), | ||
NONE(null, UnaryOperator.identity(), new byte[] {}); | ||
|
||
private final UnaryOperator<InputStream> compressorCreator; | ||
private UnaryOperator<InputStream> decompressorCreator; | ||
public final byte[] magic; | ||
mmigdiso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ClientCompressor(UnaryOperator<InputStream> compressorCreator, UnaryOperator<InputStream> decompressorCreator, | ||
byte[] magic) { | ||
this.compressorCreator = compressorCreator; | ||
this.decompressorCreator = decompressorCreator; | ||
this.magic = magic; | ||
} | ||
|
||
public InputStream getCompressor(InputStream stream) { | ||
return compressorCreator.apply(stream); | ||
} | ||
|
||
private boolean matchMagic(byte[] buffer, int bufferLen) { | ||
int i = 0; | ||
while (i < magic.length && i < bufferLen && magic[i] == buffer[i++]); | ||
return i >= magic.length && magic.length > 0; | ||
} | ||
|
||
/** | ||
* Method that takes a compressed stream and returns a decompressor stream. It will throw {@code | ||
* IllegalArgumentException} if more than one decompressor is detected, a {@code NoSuchElementException} if no | ||
* compressor detected. | ||
*/ | ||
static InputStream getDecompressorStream(InputStream stream) throws IOException { | ||
BufferedInputStream buff = new BufferedInputStream(stream); | ||
List<ClientCompressor> compressors = Arrays.stream(ClientCompressor.values()).sorted( | ||
Comparator.comparingInt((ClientCompressor t) -> t.magic.length).reversed() | ||
).collect( | ||
Collectors.toList()); | ||
int maxLen = compressors.get(0).magic.length; | ||
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.
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. can avoid all of the sorting and reversing stuff :) 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. hey @j-baker , but i need to start from the longest prefix, otherwise a shorter magic char which is a substring of another magic char would cause a bug. the purpose of this code was not only to find the maxlen. |
||
buff.mark(maxLen); | ||
byte[] headerBuffer = new byte[maxLen]; | ||
int len = ByteStreams.read(buff, headerBuffer, 0, maxLen); | ||
buff.reset(); | ||
ClientCompressor compressor = compressors.stream().filter( | ||
t -> t.magic.length <= len && t.matchMagic(headerBuffer, len)).collect(MoreCollectors.onlyElement()); | ||
return compressor.decompressorCreator.apply(buff); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ommons/src/main/java/com/palantir/common/compression/CompressorForwardingInputStream.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.common.compression; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class CompressorForwardingInputStream extends InputStream { | ||
mmigdiso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private InputStream compressedStream; | ||
private InputStream delegate; | ||
|
||
public CompressorForwardingInputStream(InputStream stream) { | ||
compressedStream = stream; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
initializeDelegate(); | ||
return delegate.read(); | ||
} | ||
|
||
@Override | ||
public int read(byte b[], int off, int len) throws IOException { | ||
initializeDelegate(); | ||
return delegate.read(b, off, len); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (delegate != null) { | ||
delegate.close(); | ||
} | ||
} | ||
|
||
private void initializeDelegate() throws IOException { | ||
if (delegate == null) { | ||
delegate = ClientCompressor.getDecompressorStream(compressedStream); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
"LZ4Block".getBytes(StandardCharsets.UTF_8)
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.
this looks nicer but different than the original implementation. Unfortunately the access modifier does not allow to use it.
https://github.com/lz4/lz4-java/blob/d43546e24388533eebd40fccb4be5468f0411788/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java#L37