Skip to content
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

Make zstd compression level configurable #1121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private Compressor createCompressor(final MongoCompressor mongoCompressor) {
case "snappy":
return new SnappyCompressor();
case "zstd":
return new ZstdCompressor();
return new ZstdCompressor(mongoCompressor);
default:
throw new MongoClientException("Unsupported compressor " + mongoCompressor.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdInputStream;
import com.mongodb.MongoCompressor;
import com.mongodb.MongoInternalException;
import org.bson.ByteBuf;
import org.bson.io.BsonOutput;
Expand All @@ -27,6 +28,13 @@
import java.util.List;

class ZstdCompressor extends Compressor {
private final int level;

ZstdCompressor(final MongoCompressor mongoCompressor) {
int level = mongoCompressor.getPropertyNonNull(MongoCompressor.LEVEL, Zstd.defaultCompressionLevel());
this.level = Math.min(Math.max(level, Zstd.minCompressionLevel()), Zstd.maxCompressionLevel());
}

@Override
public String getName() {
return "zstd";
Expand All @@ -46,7 +54,7 @@ public void compress(final List<ByteBuf> source, final BsonOutput target) {

try {
byte[] out = new byte[(int) Zstd.compressBound(uncompressedSize)];
int compressedSize = (int) Zstd.compress(out, singleByteArraySource, Zstd.defaultCompressionLevel());
int compressedSize = (int) Zstd.compress(out, singleByteArraySource, this.level);
target.writeBytes(out, 0, compressedSize);
} catch (Exception e) {
throw new MongoInternalException("Unexpected exception", e);
Expand Down