Skip to content

Commit

Permalink
Minor refactoring (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
barambani authored Aug 9, 2020
1 parent 084d974 commit aa70dac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.concurrent.{Executors, TimeUnit}
import cats.effect.{ContextShift, IO, Timer}
import cats.syntax.flatMap._
import laserdisc.auto._
import laserdisc.fs2.parallel.testcases
import laserdisc.fs2.parallel.testcases.TestCasesLaserdisc
import log.effect.fs2.SyncLogWriter.consoleLogUpToLevel
import log.effect.{LogLevels, LogWriter}

Expand All @@ -26,7 +26,7 @@ object CatsIoTestRunner {
val runForMinutes = 2
val task = timer.clock.monotonic(TimeUnit.MINUTES) >>= { start: Long =>
RedisClient.to("localhost", 6379).use { cl =>
val cases = testcases.TestCasesLaserdisc[IO](cl)
val cases = TestCasesLaserdisc[IO](cl)
def loop(count: Long): IO[Long] =
cases.case1 >> timer.clock.monotonic(TimeUnit.MINUTES) >>= { current =>
if (current - start >= runForMinutes) IO.pure(count)
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ val V = new {
val `scodec-stream` = "2.0.0"
val scredis = "2.3.3"
val shapeless = "2.3.3"
val zio = "1.0.0-RC21"
val `zio-interop-cats` = "2.0.0.0-RC14"
val zio = "1.0.0"
val `zio-interop-cats` = "2.1.4.0"
}

val `cats-core` = Def.setting("org.typelevel" %% "cats-core" % V.cats)
Expand Down
34 changes: 17 additions & 17 deletions core/src/main/scala/laserdisc/protocol/RESP.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,20 @@ sealed trait RESPCodecs extends BitVectorSyntax {
protected final val crlfBytes = crlf.bytes
private[this] final val crlfBytesSize = crlfBytes.size

private[this] final def crlfTerminatedCodec[A](baseCodec: Codec[A], from: Long = 0L): Codec[A] =
filtered(
baseCodec,
new Codec[BitVector] {
override final def sizeBound: SizeBound = SizeBound.unknown
override final def encode(bits: BitVector): Attempt[BitVector] = Attempt.successful(bits ++ crlf)
override final def decode(bits: BitVector): Attempt[DecodeResult[BitVector]] =
bits.bytes.indexOfSlice(crlfBytes, from) match {
case -1 =>
Attempt.failure(new MatchingDiscriminatorNotFound(s"Does not contain 'CRLF' termination bytes. Content: ${bits.tailToUtf8}"))
case i => Attempt.successful(DecodeResult(bits.take(i * BitsInByte), bits.drop(i * BitsInByte + crlfSize)))
}
private[this] final def crlfTerminatedFrom(from: Long) =
new Codec[BitVector] {
override final val sizeBound: SizeBound = SizeBound.unknown
override final def encode(bits: BitVector): Attempt[BitVector] = Attempt.successful(bits ++ crlf)
override final def decode(bits: BitVector): Attempt[DecodeResult[BitVector]] = {
val i = bits.bytes.indexOfSlice(crlfBytes, from)
if (i == -1)
Attempt.failure(new MatchingDiscriminatorNotFound(s"Does not contain 'CRLF' termination bytes. Content: ${bits.tailToUtf8}"))
else Attempt.successful(DecodeResult(bits.take(i * BitsInByte), bits.drop(i * BitsInByte + crlfSize)))
}
)
}

private[this] final def crlfTerminatedCodec[A](baseCodec: Codec[A], from: Long = 0L): Codec[A] =
filtered(baseCodec, crlfTerminatedFrom(from))

private[this] final val crlfTerminatedStringCodec: Codec[String] = crlfTerminatedCodec(utf8Codec)
private[this] final val crlfTerminatedLongCodec: Codec[Long] = crlfTerminatedStringCodec.narrow(
Expand Down Expand Up @@ -222,7 +222,7 @@ sealed trait RESPCodecs extends BitVectorSyntax {
private[this] final def failEnc(arr: GenArr, err: SErr) =
General(s"failed to encode size of [$arr]: ${err.messageWithContext}", List("size"))

override final def sizeBound: SizeBound = SizeBound.unknown
override final val sizeBound: SizeBound = SizeBound.unknown
override final def encode(arr: GenArr): Attempt[BitVector] =
arr match {
case NilArr => Attempt.successful(nilArrBits)
Expand All @@ -234,7 +234,7 @@ sealed trait RESPCodecs extends BitVectorSyntax {
}

implicit final val respCodec: Codec[RESP] = new Codec[RESP] {
override final def sizeBound: SizeBound = SizeBound.unknown
override final val sizeBound: SizeBound = SizeBound.unknown
override final def encode(value: RESP): Attempt[BitVector] =
value match {
case str: Str => strCodec.encode(str).map(plus ++ _)
Expand All @@ -255,12 +255,12 @@ sealed trait RESPCodecs extends BitVectorSyntax {
case (other, _) => Attempt.failure(SErr(s"unidentified RESP type (Hex: ${other.toHex})"))
}
)
override final def toString: String = "RESP"
override final val toString: String = "RESP"
}

protected final val crlfTerminatedReprOfLongDecoder: Decoder[Repr[Long]] = crlfTerminatedCodec(
new Codec[Repr[String]] {
override final def sizeBound: SizeBound = SizeBound.unknown
override final val sizeBound: SizeBound = SizeBound.unknown
override final def encode(bd: Repr[String]): Attempt[BitVector] = utf8Codec.encode(bd.decoded)
override final def decode(bits: BitVector): Attempt[DecodeResult[Repr[String]]] = utf8Codec.decode(bits).map(_.map(Repr(_, bits)))
}
Expand Down
6 changes: 3 additions & 3 deletions fs2/src/main/scala/laserdisc/fs2/RedisClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ object RedisClient {

def pop: F[Option[Request[F]]] =
inFlight
.modify {
case h +: t => t -> Some(h)
case other => other -> None
.modify { inFl =>
if (inFl.nonEmpty) inFl.tail -> Some(inFl.head)
else inFl -> None
}

def serverAvailable(address: RedisAddress): Stream[F, Unit] =
Expand Down

0 comments on commit aa70dac

Please sign in to comment.