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

feat: Reimplement MapConcat operator without statefulMapConcat. #902

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import pekko.stream.Attributes._
val expand = name("expand")
val statefulMap = name("statefulMap")
val statefulMapConcat = name("statefulMapConcat")
val mapConcat = name("mapConcat")
val detacher = name("detacher")
val groupBy = name("groupBy")
val prefixAndTail = name("prefixAndTail")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.fusing

import scala.util.control.Exception.Catcher
import scala.util.control.NonFatal

import org.apache.pekko
import pekko.annotation.InternalApi
import pekko.stream.{ Attributes, FlowShape, Inlet, Outlet, Supervision }
import pekko.stream.ActorAttributes.SupervisionStrategy
import pekko.stream.Attributes.SourceLocation
import pekko.stream.Supervision.Decider
import pekko.stream.impl.Stages.DefaultAttributes
import pekko.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler }
import pekko.util.ccompat._

/**
* INTERNAL API
*/
@InternalApi
@ccompatUsedUntil213
private[pekko] final class MapConcat[In, Out](f: In => IterableOnce[Out])
extends GraphStage[FlowShape[In, Out]] {
require(f != null, "f function should not be null")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be defensive, is it nessnary check?
We should specify the parent of the function, like f function of MapConcat should not be null ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,better than a materilixation error.
As the stack trace can show the detail, that's information can be ommited

private val in = Inlet[In]("MapConcat.in")
private val out = Outlet[Out]("MapConcat.out")
override val shape: FlowShape[In, Out] = FlowShape(in, out)

override def initialAttributes: Attributes = DefaultAttributes.mapConcat and SourceLocation.forLambda(f)

override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) with InHandler with OutHandler {
private lazy val decider: Decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider

private var currentIterator: Iterator[Out] = _

private def hasNext = currentIterator != null && currentIterator.hasNext

override def onPush(): Unit =
try {
currentIterator = f(grab(in)).iterator
tryPushAndPull()
} catch handleException

private def tryPushAndPull(): Unit =
try {
if (hasNext) {
push(out, currentIterator.next())
if (!hasNext && isClosed(in)) {
completeStage()
}
} else if (isClosed(in)) completeStage()
else pull(in)
} catch handleException
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as both next and hasNext can continue failing, so just simply droping the current iterator.


private def handleException: Catcher[Unit] = {
case NonFatal(ex) =>
decider(ex) match {
case Supervision.Stop => failStage(ex)
case _ =>
if (isClosed(in)) completeStage()
else if (!hasBeenPulled(in)) pull(in)
}
}

override def onPull(): Unit = tryPushAndPull()

override def onUpstreamFinish(): Unit = if (!hasNext) completeStage()

setHandlers(in, out, this)
}

override def toString: String = "MapConcat"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,7 @@ trait FlowOps[+Out, +Mat] {
*
* '''Cancels when''' downstream cancels
*/
@nowarn("msg=deprecated")
def mapConcat[T](f: Out => IterableOnce[T]): Repr[T] = statefulMapConcat(() => f)
def mapConcat[T](f: Out => IterableOnce[T]): Repr[T] = via(new MapConcat(f))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not the correct opinion :
(1) Does javadsl require similar modifications? maybe refer to org.apache.pekko.stream.javadsl.Flow#statefulMapConcat
(2) can we use object or case object for MapConcat, it is refered to org.apache.pekko.stream.impl.fusing.MapAsync.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. the javadsl's mapconcat is using scaladsl's mapconcat
  2. case class will make the jar bigger and we have no pattern matching here.


/**
* Transform each stream element with the help of a state.
Expand Down
Loading