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

Add ZSink.splitLines #1384

Merged
merged 2 commits into from
Aug 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/zio/Chunk.scala
Expand Up @@ -121,6 +121,12 @@ sealed trait Chunk[@specialized +A] { self =>
else Chunk.Slice(Chunk.Arr(dest), 0, j)
}

/**
* Flattens a chunk of chunks into a single chunk by concatenating all chunks.
*/
def flatten[B](implicit ev: A <:< Chunk[B]): Chunk[B] =
flatMap(ev(_))

/**
* Returns the concatenation of mapping every element into a new chunk using
* the specified function.
Expand Down
78 changes: 77 additions & 1 deletion streams-tests/jvm/src/test/scala/zio/stream/SinkSpec.scala
@@ -1,6 +1,6 @@
package zio.stream

import org.scalacheck.Arbitrary
import org.scalacheck.{ Arbitrary, Gen }
import org.specs2.ScalaCheck
import scala.{ Stream => _ }
import zio._
Expand Down Expand Up @@ -228,6 +228,14 @@ class SinkSpec(implicit ee: org.specs2.concurrent.ExecutionEnv) extends TestRunt

fromOutputStream $fromOutputStream

splitLines
preserves data $splitLines
handles leftovers $splitLinesLeftovers
transduces $splitLinesTransduce
single newline edgecase $splitLinesEdgecase
no newlines in data $splitLinesNoNewlines
\r\n on the boundary $splitLinesBoundary

throttleEnforce $throttleEnforce
with burst $throttleEnforceWithBurst

Expand Down Expand Up @@ -1228,6 +1236,74 @@ class SinkSpec(implicit ee: org.specs2.concurrent.ExecutionEnv) extends TestRunt
}
}

private def splitLines =
prop { (lines: List[String]) =>
val data = lines.mkString("\n")

unsafeRun {
for {
initial <- ZSink.splitLines.initial.map(Step.state(_))
middle <- ZSink.splitLines.step(initial, data)
result <- ZSink.splitLines.extract(Step.state(middle))
sinkLeftover = Step.leftover(middle)
} yield ((result ++ sinkLeftover).toArray[String].mkString("\n") must_=== lines.mkString("\n"))
}
}.setGen(
Gen
.listOf(Gen.asciiStr.map(_.filterNot(c => c == '\n' || c == '\r')))
.map(l => if (l.nonEmpty && l.last == "") l ++ List("a") else l)
)

private def splitLinesLeftovers = unsafeRun {
for {
initial <- ZSink.splitLines.initial.map(Step.state(_))
middle <- ZSink.splitLines.step(initial, "abc\nbc")
result <- ZSink.splitLines.extract(Step.state(middle))
sinkLeftover = Step.leftover(middle)
} yield (result.toArray[String].mkString("\n") must_=== "abc") and (sinkLeftover
.toArray[String]
.mkString must_=== "bc")
}

private def splitLinesTransduce = unsafeRun {
Stream("abc", "\n", "bc", "\n", "bcd", "bcd")
.transduce(ZSink.splitLines)
.runCollect
.map {
_ must_=== List(Chunk("abc"), Chunk("bc"), Chunk("bcdbcd"))
}
}

private def splitLinesEdgecase = unsafeRun {
Stream("\n")
.transduce(ZSink.splitLines)
.mapConcat(identity)
.runCollect
.map {
_ must_=== List("")
}
}

private def splitLinesNoNewlines = unsafeRun {
Stream("abc", "abc", "abc")
.transduce(ZSink.splitLines)
.mapConcat(identity)
.runCollect
.map {
_ must_=== List("abcabcabc")
}
}

private def splitLinesBoundary = unsafeRun {
Stream("abc\r", "\nabc")
.transduce(ZSink.splitLines)
.mapConcat(identity)
.runCollect
.map {
_ must_=== List("abc", "abc")
}
}

private def throttleEnforce = {

def sinkTest(sink: ZSink[Clock, Nothing, Nothing, Int, Option[Int]]) =
Expand Down
10 changes: 10 additions & 0 deletions streams/shared/src/main/scala/zio/stream/Sink.scala
Expand Up @@ -196,6 +196,16 @@ object Sink {
final def read1[E, A](e: Option[A] => E)(p: A => Boolean): Sink[E, A, A, A] =
ZSink.read1(e)(p)

/**
* see [[ZSink.splitLines]]
*/
final val splitLines: Sink[Nothing, String, String, Chunk[String]] = ZSink.splitLines

/**
* see [[ZSink.splitLinesChunk]]
*/
final val splitLinesChunk: Sink[Nothing, Chunk[String], Chunk[String], Chunk[String]] = ZSink.splitLinesChunk

/**
* see [[ZSink.succeed]]
*/
Expand Down
74 changes: 74 additions & 0 deletions streams/shared/src/main/scala/zio/stream/ZSink.scala
Expand Up @@ -19,6 +19,8 @@ package zio.stream
import zio._
import zio.clock.Clock
import zio.duration.Duration

import scala.collection.mutable
import scala.language.postfixOps

/**
Expand Down Expand Up @@ -1333,6 +1335,78 @@ object ZSink extends ZSinkPlatformSpecific {
}
}

/**
* Splits strings on newlines. Handles both `\r\n` and `\n`.
*/
final val splitLines: ZSink[Any, Nothing, String, String, Chunk[String]] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Not to nitpick here, but I see this as a deviation from the previous sinks, which have all been defined with def. AFAIK val creates objects eagerly, and they stay in memory for the duration of the program which references this code. Is that something desirable in this case? I may be completely off base here, I just want to hear the reasoning. Same for every other val, including the ones in Sink.

new SinkPure[Nothing, String, String, Chunk[String]] {
type State = (Chunk[String], Option[String], Boolean)
Copy link
Contributor

Choose a reason for hiding this comment

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

On an urelated note, this is probably what the stepless sinks' code will look like after the refactoring. :)


override val initialPure: Step[State, Nothing] = Step.more((Chunk.empty, None, false))

override def stepPure(s: State, a: String): Step[State, String] = {
val accumulatedLines = s._1
val concat = s._2.getOrElse("") + a
val wasSplitCRLF = s._3

if (concat.isEmpty) Step.more(s)
else {
val buf = mutable.ArrayBuffer[String]()

var i =
// If we had a split CRLF, we start reading from the last character of the
// leftover (which was the '\r')
if (wasSplitCRLF) s._2.map(_.length).getOrElse(1) - 1
// Otherwise we just skip over the entire previous leftover as it doesn't
// contain a newline.
else s._2.map(_.length).getOrElse(0)

var sliceStart = 0
var splitCRLF = false

while (i < concat.length) {
if (concat(i) == '\n') {
buf += concat.substring(sliceStart, i)
i += 1
sliceStart = i
} else if (concat(i) == '\r' && (i + 1 < concat.length) && (concat(i + 1) == '\n')) {
buf += concat.substring(sliceStart, i)
i += 2
sliceStart = i
} else if (concat(i) == '\r' && (i == concat.length - 1)) {
splitCRLF = true
i += 1
} else {
i += 1
}
}

if (buf.isEmpty) Step.more((accumulatedLines, Some(concat), splitCRLF))
else {
val newLines = Chunk.fromArray(buf.toArray[String])
val leftover = concat.substring(sliceStart, concat.length)

if (splitCRLF) Step.more((accumulatedLines ++ newLines, Some(leftover), splitCRLF))
else
Step.done(
(accumulatedLines ++ newLines, None, splitCRLF),
if (leftover.nonEmpty) Chunk.single(leftover) else Chunk.empty
)
}
}
}

override def extractPure(s: State): Either[Nothing, Chunk[String]] =
Right(s._1 ++ s._2.map(Chunk.single(_)).getOrElse(Chunk.empty))
}

/**
* Merges chunks of strings and splits them on newlines. Handles both
* `\r\n` and `\n`.
*/
final val splitLinesChunk: ZSink[Any, Nothing, Chunk[String], Chunk[String], Chunk[String]] =
splitLines.contramap[Chunk[String]](_.mkString).mapRemainder(Chunk.single)

/**
* Creates a single-value sink from a value.
*/
Expand Down