Skip to content

Commit

Permalink
Updated parallel stream support to support ordered flag
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Dec 18, 2024
1 parent 70250b2 commit 38fe6a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
35 changes: 17 additions & 18 deletions core/shared/src/main/scala/rapid/ParallelStream.scala
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
package rapid

import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.DurationInt

case class ParallelStream[T, R](stream: Stream[T],
f: T => Task[R],
maxThreads: Int,
maxBuffer: Int) {
maxBuffer: Int,
ordered: Boolean) {
def drain: Task[Unit] = Task.unit.flatMap { _ =>
val completable = Task.completable[Unit]
ParallelUnorderedStreamProcessor(
stream = this,
handle = (_: R) => (),
complete = (_: Int) => completable.success(())
)
compile(_ => (), _ => completable.success(()))
completable
}

def count: Task[Int] = Task.unit.flatMap { _ =>
val completable = Task.completable[Int]
ParallelUnorderedStreamProcessor(
stream = this,
handle = (_: R) => (),
complete = completable.success
)
compile(_ => (), completable.success)
completable
}

def toList: Task[List[R]] = Task.unit.flatMap { _ =>
val list = ListBuffer.empty[R]
val completable = Task.completable[List[R]]
compile(list.addOne, _ => completable.success(list.toList))
completable
}

protected def compile(handle: R => Unit, complete: Int => Unit): Unit = if (ordered) {
ParallelStreamProcessor(
stream = this,
handle = handle,
complete = complete
)
} else {
ParallelUnorderedStreamProcessor(
stream = this,
handle = list.addOne,
complete = (_: Int) => completable.success(list.toList)
handle = handle,
complete = complete
)
completable
}
}

Expand Down
6 changes: 4 additions & 2 deletions core/shared/src/main/scala/rapid/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ class Stream[Return](private val task: Task[Iterator[Return]]) extends AnyVal {
def count: Task[Int] = task.map(_.size)

def par[R](maxThreads: Int = ParallelStream.DefaultMaxThreads,
maxBuffer: Int = ParallelStream.DefaultMaxBuffer)
maxBuffer: Int = ParallelStream.DefaultMaxBuffer,
ordered: Boolean = false)
(f: Return => Task[R]): ParallelStream[Return, R] = ParallelStream(
stream = this,
f = f,
maxThreads = maxThreads,
maxBuffer = maxBuffer
maxBuffer = maxBuffer,
ordered = ordered
)
}

Expand Down

0 comments on commit 38fe6a5

Please sign in to comment.