Skip to content

Commit

Permalink
add support for the COPY command
Browse files Browse the repository at this point in the history
  • Loading branch information
mberndt123 committed Oct 20, 2023
1 parent 7a4f376 commit 16ec577
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/redis/src/main/scala/zio/redis/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ object Input {
RespCommand(RespCommandArgument.Literal("BLOCK"), RespCommandArgument.Value(data.toMillis.toString))
}

case object DbInput extends Input[Long] {
def encode(db: Long): RespCommand =
RespCommand(RespCommandArgument.Literal("DB"), RespCommandArgument.Value(db.toString()))
}

case object BoolInput extends Input[Boolean] {
def encode(data: Boolean): RespCommand =
RespCommand(RespCommandArgument.Literal(if (data) "1" else "0"))
Expand Down
31 changes: 31 additions & 0 deletions modules/redis/src/main/scala/zio/redis/api/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@ import java.time.Instant
trait Keys extends RedisEnvironment {
import Keys.{Keys => _, _}

/**
* Copies the value stored at the source key to the destination key.
*
* @param source
* source key
* @param destination
* destination key
* @param database
* optional database index
* @param replace
* if this is non-empty, the destination will be removed before copying the value to it
* @return
* true if source was copied, false otherwise.
*/

final def copy[S: Schema, D: Schema](
source: S,
destination: D,
database: Option[Long] = None,
replace: Option[Replace] = None
): IO[RedisError, Boolean] = {
val command = RedisCommand(
Copy,
Tuple4(ArbitraryKeyInput[S](), ArbitraryKeyInput[D](), OptionalInput(DbInput), OptionalInput(ReplaceInput)),
BoolOutput,
executor
)
command.run((source, destination, database, replace))
}

/**
* Removes the specified keys. A key is ignored if it does not exist.
*
Expand Down Expand Up @@ -561,6 +591,7 @@ trait Keys extends RedisEnvironment {
}

private[redis] object Keys {
final val Copy = "COPY"
final val Del = "DEL"
final val Dump = "DUMP"
final val Exists = "EXISTS"
Expand Down

0 comments on commit 16ec577

Please sign in to comment.