mdedetrich commented on code in PR #2409: URL: https://github.com/apache/pekko/pull/2409#discussion_r2472114664
########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdDecompressor.scala: ########## @@ -0,0 +1,99 @@ +/* + * 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 org.apache.pekko +import pekko.annotation.InternalApi +import pekko.stream.Attributes +import pekko.stream.impl.fusing.GraphStages.SimpleLinearGraphStage +import pekko.stream.stage.{ GraphStageLogic, InHandler, OutHandler } +import pekko.util.ByteString + +import com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdDecompressor(maxBytesPerChunk: Int) extends SimpleLinearGraphStage[ByteString] { + + override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = { + new GraphStageLogic(shape) with InHandler with OutHandler { + + private val sourceBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + + // This is initialized here to avoid an allocation per onPush, remember to clear before using + private val outputBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + private val decompressingStream = new ZstdDirectBufferDecompressingStreamNoFinalizer(sourceBuffer) + + override def onPush(): Unit = { + sourceBuffer.clear() + val inputArray = grab(in).toArrayUnsafe() + sourceBuffer.put(inputArray) + sourceBuffer.flip() + if (sourceBuffer.hasRemaining) { + var result = ByteString.empty + while (sourceBuffer.hasRemaining) { + outputBuffer.clear() + decompressingStream.read(outputBuffer) + outputBuffer.flip() + val outputArray = new Array[Byte](outputBuffer.limit()) + outputBuffer.get(outputArray) + result = result.concat(ByteString.fromArrayUnsafe(outputArray)) + } + + // Fencepost case, it's possible to still have sourceBuffer.hasRemaining and yet compression result + // not output anything + if (result.nonEmpty) + push(out, result) + else + pull(in) + } else pull(in) + sourceBuffer.flip() + } + + override def onPull(): Unit = pull(in) + + override def onUpstreamFinish(): Unit = { + sourceBuffer.flip() + if (sourceBuffer.hasRemaining) { + var result = ByteString.empty + while (sourceBuffer.hasRemaining) { + outputBuffer.clear() + decompressingStream.read(outputBuffer) + outputBuffer.flip() + val outputArray = new Array[Byte](outputBuffer.limit()) + outputBuffer.get(outputArray) + result = result.concat(ByteString.fromArrayUnsafe(outputArray)) + } + decompressingStream.close() + + // Fencepost case, it's possible to still have sourceBuffer.hasRemaining and yet compression result Review Comment: Done ########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdDecompressor.scala: ########## @@ -0,0 +1,99 @@ +/* + * 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 org.apache.pekko +import pekko.annotation.InternalApi +import pekko.stream.Attributes +import pekko.stream.impl.fusing.GraphStages.SimpleLinearGraphStage +import pekko.stream.stage.{ GraphStageLogic, InHandler, OutHandler } +import pekko.util.ByteString + +import com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdDecompressor(maxBytesPerChunk: Int) extends SimpleLinearGraphStage[ByteString] { + + override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = { + new GraphStageLogic(shape) with InHandler with OutHandler { + + private val sourceBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + + // This is initialized here to avoid an allocation per onPush, remember to clear before using + private val outputBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + private val decompressingStream = new ZstdDirectBufferDecompressingStreamNoFinalizer(sourceBuffer) + + override def onPush(): Unit = { + sourceBuffer.clear() + val inputArray = grab(in).toArrayUnsafe() + sourceBuffer.put(inputArray) + sourceBuffer.flip() + if (sourceBuffer.hasRemaining) { + var result = ByteString.empty + while (sourceBuffer.hasRemaining) { + outputBuffer.clear() + decompressingStream.read(outputBuffer) + outputBuffer.flip() + val outputArray = new Array[Byte](outputBuffer.limit()) + outputBuffer.get(outputArray) + result = result.concat(ByteString.fromArrayUnsafe(outputArray)) + } + + // Fencepost case, it's possible to still have sourceBuffer.hasRemaining and yet compression result Review Comment: Done -- 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]
