raboof commented on code in PR #2409:
URL: https://github.com/apache/pekko/pull/2409#discussion_r2556136982


##########
actor/src/main/java/org/apache/pekko/io/ByteBufferCleaner.java:
##########
@@ -36,7 +37,8 @@
  *
  * <p>See <a 
href=https://bugs.openjdk.java.net/browse/JDK-4724038>JDK-4724038</a>
  */
-final class ByteBufferCleaner {
+@InternalStableApi

Review Comment:
   This annotation is for APIs that need to remain binary compatible because 
other, independently-released Pekko components depend on them.
   
   As you're using this API within Pekko Core, and we expect all Pekko Core 
artifact versions to be consistent, the annotation is not needed for this use.
   
   Are you planning to use this API in other Pekko modules? If not then we can 
remove this annotation here. If so then we should add a note to the scaladoc 
pointing to where it's (going to be) used.



##########
stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.pekko.stream.impl.io.compression
+
+import java.nio.ByteBuffer
+
+import com.github.luben.zstd.{ Zstd, ZstdDictCompress, 
ZstdDirectBufferCompressingStreamNoFinalizer }
+
+import org.apache.pekko
+import pekko.annotation.InternalApi
+import pekko.io.ByteBufferCleaner
+import pekko.util.ByteString
+
+/** INTERNAL API */
+@InternalApi private[pekko] class ZstdCompressor(
+    compressionLevel: Int =
+      Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = 
None) extends Compressor {
+
+  private val targetBuffer = ByteBuffer.allocateDirect(65536)
+  private val compressingStream = new 
ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel)
+
+  dictionary.foreach(compressingStream.setDict)
+
+  override def compress(input: ByteString): ByteString = {
+    val inputBB = ByteBuffer.allocateDirect(input.size)
+    inputBB.put(input.toArrayUnsafe())
+    inputBB.flip()
+    compressingStream.compress(inputBB)
+    val result = ByteString.fromByteBuffer(targetBuffer)

Review Comment:
   Are you still planning on this?



##########
.scala-steward.conf:
##########
@@ -5,6 +5,8 @@ updates.pin = [
   { groupId = "org.scala-lang", artifactId = "scala3-library", version = 
"3.3." }
   # sbt-assembly 2.3 causes build issues 
(https://github.com/apache/pekko/pull/1744)
   { groupId = "com.eed3si9n", artifactId = "sbt-assembly", version = "2.2." }
+  # zstd-jni upgrades are fine as long as they are backwards compatible
+  { groupId = "com.github.luben", artifactId = "zstd-jni", version = "1."}

Review Comment:
   Shouldn't we even allow major version updates? Then we can decide on a 
case-by-case basis whether the incompatibility that caused them to bump the 
major version is a reason for us to avoid the update.



##########
stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.pekko.stream.impl.io.compression
+
+import java.nio.ByteBuffer
+
+import com.github.luben.zstd.{ Zstd, ZstdDictCompress, 
ZstdDirectBufferCompressingStreamNoFinalizer }
+
+import org.apache.pekko
+import pekko.annotation.InternalApi
+import pekko.io.ByteBufferCleaner
+import pekko.util.ByteString
+
+/** INTERNAL API */
+@InternalApi private[pekko] class ZstdCompressor(
+    compressionLevel: Int =
+      Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = 
None) extends Compressor {
+
+  private val targetBuffer = ByteBuffer.allocateDirect(65536)
+  private val compressingStream = new 
ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel)
+
+  dictionary.foreach(compressingStream.setDict)
+
+  override def compress(input: ByteString): ByteString = {
+    val inputBB = ByteBuffer.allocateDirect(input.size)
+    inputBB.put(input.toArrayUnsafe())
+    inputBB.flip()
+    compressingStream.compress(inputBB)
+    val result = ByteString.fromByteBuffer(targetBuffer)
+    targetBuffer.flip()
+    if (ByteBufferCleaner.isSupported)
+      ByteBufferCleaner.clean(inputBB)
+    result
+  }
+
+  override def flush(): ByteString = {
+    targetBuffer.flip()
+    val result = ByteString.fromByteBuffer(targetBuffer)
+    targetBuffer.clear()
+    compressingStream.flush()
+    result
+  }
+
+  override def finish(): ByteString = {
+    compressingStream.close()
+    targetBuffer.flip()
+    val arr = Array.ofDim[Byte](targetBuffer.limit())
+    targetBuffer.get(arr)
+    val result = ByteString.fromArrayUnsafe(arr)
+    if (ByteBufferCleaner.isSupported)
+      ByteBufferCleaner.clean(targetBuffer)
+    result
+  }
+
+  override def compressAndFlush(input: ByteString): ByteString = {
+    val inputBB = ByteBuffer.allocateDirect(input.size)
+    inputBB.put(input.toArray)

Review Comment:
   the recoding is now complete, right? This line still seems to be here, 
should it still be changed to `Unsafe`?



##########
docs/src/main/paradox/stream/operators/Compression/zstd.md:
##########
@@ -0,0 +1,30 @@
+# Compression.zstd
+
+Creates a flow that zstd-compresses a stream of ByteStrings.
+
+@ref[Compression operators](../index.md#compression-operators)
+
+## Signature
+
+@apidoc[Compression.zstd](stream.*.Compression$) { 
scala="#zstd:org.apache.pekko.stream.scaladsl.Flow[org.apache.pekko.util.ByteString,org.apache.pekko.util.ByteString,org.apache.pekko.NotUsed]"
 java="#zstd()" }
+
+## Description
+
+Creates a flow that zstd-compresses a stream of ByteStrings. Note that the 
compressor
+will SYNC_FLUSH after every @apidoc[util.ByteString] so that it is guaranteed 
that every @apidoc[util.ByteString]

Review Comment:
   This also means that if the way the incoming data is cut up into 
`ByteString`s is not deterministic, the compressed stream will not be 
deterministic, right? I think that is acceptable, but would be worth calling 
out.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to