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

Added the MOVE command to finagle-redis. #267

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ trait Keys { self: BaseClient =>
case EmptyMBulkReply() => Future.Nil
}

/**
* Move key from the currently selected database to the specified destination
* database. When key already exists in the destination database, or it does
* not exist in the source database, it does nothing.
*
* @param key, db
* @return true if key was moved.
* false if key was not moved for any reason.
*/
def move(key: ChannelBuffer, db: ChannelBuffer): Future[JBoolean] =
doRequest(Move(key, db)) {
case IntegerReply(n) => Future.value(n == 1)
}

/**
* Set a key's time to live in milliseconds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Commands {
val EXPIRE = "EXPIRE"
val EXPIREAT = "EXPIREAT"
val KEYS = "KEYS"
val MOVE = "MOVE"
val PERSIST = "PERSIST"
val PEXPIRE = "PEXPIRE"
val PEXPIREAT = "PEXPIREAT"
Expand Down Expand Up @@ -140,6 +141,7 @@ object Commands {
EXPIRE -> {Expire(_)},
EXPIREAT -> {ExpireAt(_)},
KEYS -> {Keys(_)},
MOVE -> {Move(_)},
PERSIST -> {Persist(_)},
PEXPIRE -> {PExpire(_)},
PEXPIREAT -> {PExpireAt(_)},
Expand Down Expand Up @@ -269,11 +271,13 @@ object Commands {
}

object CommandBytes {
// Key Commands
val DEL = StringToChannelBuffer("DEL")
val EXISTS = StringToChannelBuffer("EXISTS")
val EXPIRE = StringToChannelBuffer("EXPIRE")
val EXPIREAT = StringToChannelBuffer("EXPIREAT")
val KEYS = StringToChannelBuffer("KEYS")
val MOVE = StringToChannelBuffer("MOVE")
val PERSIST = StringToChannelBuffer("PERSIST")
val PEXPIRE = StringToChannelBuffer("PEXPIRE")
val PEXPIREAT = StringToChannelBuffer("PEXPIREAT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ object Keys {
def apply(args: Seq[Array[Byte]]) = new Keys(ChannelBuffers.wrappedBuffer(args.head))
}

case class Move(key: ChannelBuffer, db: ChannelBuffer) extends StrictKeyCommand {
def command = Commands.MOVE
RequireClientProtocol(db != null && db.readableBytes > 0, "Database must be specified")
def toChannelBuffer =
RedisCodec.toUnifiedFormat(Seq(CommandBytes.MOVE, key, db))
}
object Move {
def apply(args: Seq[Array[Byte]]) = {
val list = trimList(args, 2, "MOVE")
new Move(ChannelBuffers.wrappedBuffer(list(0)),
ChannelBuffers.wrappedBuffer(list(1)))
}
}

case class Persist(key: ChannelBuffer) extends StrictKeyCommand {
def command = Commands.PERSIST
def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.PERSIST, key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class NaggatiSpec extends SpecificationWithJUnit {
"KEYS" >> {
codec(wrap("KEYS h?llo\r\n")) mustEqual List(Keys("h?llo"))
}
"MOVE" >> {
codec(wrap("MOVE\r\n")) must throwA[ClientError]
codec(wrap("MOVE boo\r\n")) must throwA[ClientError]
codec(wrap("MOVE boo moo \r\n")) mustEqual List(Move(boo, moo))
}
"PERSIST" >> {
codec(wrap("PERSIST\r\n")) must throwA[ClientError]
codec(wrap("PERSIST foo\r\n")) mustEqual List(Persist("foo"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ class ClientServerIntegrationSpec extends SpecificationWithJUnit {
Await.result(client(Type(KEY))) mustEqual StatusReply("string")
Await.result(client(Type("nosuchkey"))) mustEqual StatusReply("none")
}
"MOVE" >> {
val fromDb = 14
val toDb = 15
Await.result(client(Select(toDb))) mustEqual StatusReply("OK")
Await.result(client(Del(List(KEY)))) // only ensuring clean slate, reply doesn't matter
Await.result(client(Select(fromDb))) mustEqual StatusReply("OK")
Await.result(client(Set("MOO", "BAR"))) mustEqual StatusReply("OK")

Await.result(client(Move("", StringToChannelBuffer(toDb.toString)))) must throwA[ClientError]
Await.result(client(Move("MOO", StringToChannelBuffer("")))) must throwA[ClientError]
Await.result(client(Move(StringToChannelBuffer("MOO"),
StringToChannelBuffer(toDb.toString)))) mustEqual IntegerReply(1)
Await.result(client(Move(StringToChannelBuffer("MOO"),
StringToChannelBuffer(toDb.toString)))) mustEqual IntegerReply(0)
}
}

"Handle Sorted Set Commands" >> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ class ClientSpec extends SpecificationWithJUnit {
Await.result(client.pTtl(foo)) map (_ must beLessThanOrEqualTo(20000L))
}

"move" in {
val fromDb = 14
val toDb = 15
Await.result(client.select(toDb))
Await.result(client.del(Seq(foo)))
Await.result(client.select(fromDb))

Await.result(client.move(foo, bar)) mustEqual false
Await.result(client.set(foo, bar))
Await.result(client.move(foo, StringToChannelBuffer(toDb.toString))) mustEqual true
Await.result(client.del(Seq(foo)))
}

// Once the scan/hscan pull request gets merged into Redis master,
// the tests can be uncommented.
// "scan" in {
Expand Down
1 change: 0 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ object Finagle extends Build {
util("logging")
),
testOptions in Test := Seq(Tests.Filter {
case "com.twitter.finagle.redis.protocol.integration.ClientServerIntegrationSpec" => false
case "com.twitter.finagle.redis.integration.ClientSpec" => false
case "com.twitter.finagle.redis.integration.BtreeClientSpec" => false
case "com.twitter.finagle.redis.integration.ClientServerIntegrationSpec" => false
Expand Down