deshanxiao commented on code in PR #1714: URL: https://github.com/apache/orc/pull/1714#discussion_r1437568857
########## java/core/src/java/org/apache/orc/impl/BrotliCodec.java: ########## @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.orc.impl; + +import com.aayushatharva.brotli4j.Brotli4jLoader; +import com.aayushatharva.brotli4j.decoder.DecoderJNI; +import com.aayushatharva.brotli4j.encoder.Encoder; +import org.apache.orc.CompressionCodec; +import org.apache.orc.CompressionKind; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public class BrotliCodec implements CompressionCodec, DirectDecompressionCodec { + // load jni library. + static { + Brotli4jLoader.ensureAvailability(); + } + + public BrotliCodec() { + } + + static class BrotliOptions implements Options { + + private Encoder.Mode mode = Encoder.Mode.GENERIC; + private int quality = -1; + private int lgwin = -1; + + BrotliOptions() { + + } + + BrotliOptions(int quality, int lgwin, Encoder.Mode mode) { + this.quality = quality; + this.lgwin = lgwin; + this.mode = mode; + } + + @Override + public Options copy() { + return new BrotliOptions(quality, lgwin, mode); + } + + @Override + public Options setSpeed(SpeedModifier newValue) { + switch (newValue) { + case FAST: + // best speed + 1. + quality = 1; + break; + case DEFAULT: + // best quality. Keep default with default value. + quality = -1; + break; + case FASTEST: + // best speed. + quality = 0; + break; + default: + break; + } + return this; + } + + @Override + public Options setData(DataKind newValue) { + switch (newValue) { + case BINARY: + mode = Encoder.Mode.GENERIC; + break; + case TEXT: + mode = Encoder.Mode.TEXT; + break; + default: + break; + } + return this; + } + + public Encoder.Parameters brotli4jParameter() { + return new Encoder.Parameters() + .setQuality(quality).setWindow(lgwin).setMode(mode); + } + } + + private static final BrotliCodec.BrotliOptions DEFAULT_OPTIONS = new BrotliOptions(); + + @Override + public Options getDefaultOptions() { + return DEFAULT_OPTIONS; + } + + @Override + public boolean compress( + ByteBuffer in, + ByteBuffer out, + ByteBuffer overflow, + Options options) throws IOException { + BrotliOptions brotliOptions = (BrotliOptions) options; Review Comment: After a lot of testing I found out there is a problem that if the input data does not start from 0, this will cause the decompression to fail. So here is an extra copy when dealing with this situation. I create a issue to Brotli4j: https://github.com/hyperxpro/Brotli4j/issues/126 Since this PR has been merged, it is easy for us to re-implement it if new version of Brotli4j is released. https://github.com/hyperxpro/Brotli4j/pull/124 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
