mimaison commented on code in PR #15516:
URL: https://github.com/apache/kafka/pull/15516#discussion_r1600267022


##########
clients/src/main/java/org/apache/kafka/common/compress/ZstdCompression.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.kafka.common.compress;
+
+import com.github.luben.zstd.BufferPool;
+import com.github.luben.zstd.RecyclingBufferPool;
+import com.github.luben.zstd.Zstd;
+import com.github.luben.zstd.ZstdInputStreamNoFinalizer;
+import com.github.luben.zstd.ZstdOutputStreamNoFinalizer;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.record.CompressionType;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.ByteBufferInputStream;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.apache.kafka.common.utils.ChunkedBytesStream;
+
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Objects;
+
+public class ZstdCompression implements Compression {
+
+    public static final int MIN_LEVEL = Zstd.minCompressionLevel();
+    public static final int MAX_LEVEL = Zstd.maxCompressionLevel();
+    public static final int DEFAULT_LEVEL = Zstd.defaultCompressionLevel();
+
+    private final int level;
+
+    private ZstdCompression(int level) {
+        this.level = level;
+    }
+
+    @Override
+    public CompressionType type() {
+        return CompressionType.ZSTD;
+    }
+
+    @Override
+    public OutputStream wrapForOutput(ByteBufferOutputStream bufferStream, 
byte messageVersion) {
+        try {
+            // Set input buffer (uncompressed) to 16 KB (none by default) to 
ensure reasonable performance
+            // in cases where the caller passes a small number of bytes to 
write (potentially a single byte).
+            return new BufferedOutputStream(new 
ZstdOutputStreamNoFinalizer(bufferStream, RecyclingBufferPool.INSTANCE, level), 
16 * 1024);
+        } catch (Throwable e) {
+            throw new KafkaException(e);
+        }
+    }
+
+    @Override
+    public InputStream wrapForInput(ByteBuffer buffer, byte messageVersion, 
BufferSupplier decompressionBufferSupplier) {
+        try {
+            return new ChunkedBytesStream(wrapForZstdInput(buffer, 
decompressionBufferSupplier),
+                    decompressionBufferSupplier,
+                    decompressionOutputSize(),
+                    false);
+        } catch (Throwable e) {
+            throw new KafkaException(e);
+        }
+    }
+
+    // visible for testing
+    public ZstdInputStreamNoFinalizer wrapForZstdInput(ByteBuffer buffer, 
BufferSupplier decompressionBufferSupplier) throws IOException {

Review Comment:
   I considered inlining this method as it's only used in a test which kind of 
test in `DefaultRecordBatchTest` the internal behavior of 
`ZstdInputStreamNoFinalizer`.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to