Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolysergeev committed Feb 24, 2023
1 parent 49b920e commit c179d43
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 67 deletions.
2 changes: 1 addition & 1 deletion redis/src/main/scala/zio/redis/ClusterExecutor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final case class ClusterExecutor(

for {
keyOpt <- ZIO.succeed(command.respArgs.collectFirst { case key: RespArgument.Key => key })
keySlot = keyOpt.fold(Slot.Default)(key => Slot((key.asCRC16 % SlotsAmount).toLong))
keySlot = keyOpt.fold(Slot.Default)(key => Slot((key.asCRC16 & (SlotsAmount - 1)).toLong))
result <- executeSafe(keySlot)
} yield result
}
Expand Down
71 changes: 71 additions & 0 deletions redis/src/main/scala/zio/redis/RespArgument.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2021 John A. De Goes and the ZIO contributors
*
* Licensed 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 zio.redis

import zio.Chunk
import zio.redis.RespValue.BulkString
import zio.redis.codecs.CRC16
import zio.schema.Schema
import zio.schema.codec.BinaryCodec

import java.nio.charset.StandardCharsets

sealed trait RespArgument {
def respValue: RespValue.BulkString
}

object RespArgument {

final case class Unknown(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)
}

object Unknown {
def apply(str: String): Unknown = Unknown(Chunk.fromArray(str.getBytes(StandardCharsets.UTF_8)))
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Key = Key(codec.encode(schema)(data))
}

final case class CommandName(str: String) extends RespArgument {
lazy val respValue: BulkString = RespValue.bulkString(str)
}

final case class Literal(str: String) extends RespArgument {
lazy val respValue: BulkString = RespValue.bulkString(str)
}

final case class Key(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)

lazy val asCRC16: Int = {
val betweenBraces = bytes.dropWhile(b => b != '{').drop(1).takeWhile(b => b != '}')
val key = if (betweenBraces.isEmpty) bytes else betweenBraces
CRC16.get(key)
}
}

object Key {
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Key = Key(codec.encode(schema)(data))
}

final case class Value(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)
}

object Value {
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Value = Value(codec.encode(schema)(data))
}
}
36 changes: 36 additions & 0 deletions redis/src/main/scala/zio/redis/RespCommand.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 John A. De Goes and the ZIO contributors
*
* Licensed 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 zio.redis

import zio.Chunk

final case class RespCommand(respArgs: Chunk[RespArgument]) {
def ++(that: RespCommand): RespCommand = RespCommand(this.respArgs ++ that.respArgs)

def transformArgs(f: RespArgument => RespArgument): RespCommand = RespCommand(respArgs.map(f(_)))
}

object RespCommand {

def empty: RespCommand = new RespCommand(Chunk.empty)

def apply(respArgs: Chunk[RespArgument]): RespCommand = new RespCommand(respArgs)

def apply(respArgs: RespArgument*): RespCommand = new RespCommand(Chunk.fromIterable(respArgs))

def apply(respArg: RespArgument): RespCommand = new RespCommand(Chunk.single(respArg))
}
66 changes: 0 additions & 66 deletions redis/src/main/scala/zio/redis/RespValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,12 @@
package zio.redis

import zio._
import zio.redis.RespValue.BulkString
import zio.redis.codecs.CRC16
import zio.redis.options.Cluster.Slot
import zio.schema.Schema
import zio.schema.codec.BinaryCodec
import zio.stream._

import java.nio.charset.StandardCharsets

final case class RespCommand(respArgs: Chunk[RespArgument]) {
def ++(that: RespCommand): RespCommand = RespCommand(this.respArgs ++ that.respArgs)

def transformArgs(f: RespArgument => RespArgument): RespCommand = RespCommand(respArgs.map(f(_)))
}

object RespCommand {

def empty: RespCommand = new RespCommand(Chunk.empty)

def apply(respArgs: Chunk[RespArgument]): RespCommand = new RespCommand(respArgs)

def apply(respArgs: RespArgument*): RespCommand = new RespCommand(Chunk.fromIterable(respArgs))

def apply(respArg: RespArgument): RespCommand = new RespCommand(Chunk.single(respArg))
}

sealed trait RespArgument {
def respValue: RespValue.BulkString
}

object RespArgument {

case class Unknown(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)
}

object Unknown {
def apply(str: String): Unknown = Unknown(Chunk.fromArray(str.getBytes(StandardCharsets.UTF_8)))
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Key = Key(codec.encode(schema)(data))
}

case class CommandName(str: String) extends RespArgument {
lazy val respValue: BulkString = RespValue.bulkString(str)
}

case class Literal(str: String) extends RespArgument {
lazy val respValue: BulkString = RespValue.bulkString(str)
}

case class Key(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)

lazy val asCRC16: Int = {
val betweenBraces = bytes.dropWhile(b => b != '{').drop(1).takeWhile(b => b != '}')
val key = if (betweenBraces.isEmpty) bytes else betweenBraces
CRC16.get(key)
}
}

object Key {
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Key = Key(codec.encode(schema)(data))
}

case class Value(bytes: Chunk[Byte]) extends RespArgument {
lazy val respValue: BulkString = RespValue.BulkString(bytes)
}

object Value {
def apply[A](data: A)(implicit codec: BinaryCodec, schema: Schema[A]): Value = Value(codec.encode(schema)(data))
}
}

sealed trait RespValue extends Product with Serializable { self =>
import RespValue._
import RespValue.internal.{CrLf, Headers, NullArrayEncoded, NullStringEncoded}
Expand Down

0 comments on commit c179d43

Please sign in to comment.