Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implemented redis HMSET. Fixes issue #96 #126

Closed
wants to merge 2 commits into from

2 participants

@mairbek

No description provided.

@asrinivas asrinivas closed this
@kil9 kil9 referenced this pull request from a commit in kil9/finagle
@mairbek mairbek [split] Implemented redis HMSET. Fixes issue #96
Github-pull-request: twitter#126
Signed-off-by: Rachit Arora <rarora@twitter.com>

RB_ID=115820
08059f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 15, 2012
  1. @mairbek
Commits on Dec 16, 2012
  1. @mairbek

    Fixed preconditions

    mairbek authored
This page is out of date. Refresh to see the latest.
View
11 finagle-redis/src/main/scala/com/twitter/finagle/redis/HashCommands.scala
@@ -69,6 +69,17 @@ trait Hashes { self: BaseClient =>
}
/**
+ * Sets values for given fields in hash
+ * @param key hash key
+ * @param fv map of field to value
+ * @see http://redis.io/commands/hmset
+ */
+ def hMSet(key: ChannelBuffer, fv: Map[ChannelBuffer, ChannelBuffer]): Future[Unit] =
+ doRequest(HMSet(key, fv)) {
+ case StatusReply(msg) => Future.Unit
+ }
+
+ /**
* Returns keys in given hash, starting at cursor
* @param hash key, cursor, count, pattern
* @return cursor followed by matching keys
View
3  finagle-redis/src/main/scala/com/twitter/finagle/redis/protocol/Command.scala
@@ -82,6 +82,7 @@ object Commands {
val HGETALL = "HGETALL"
val HKEYS = "HKEYS"
val HMGET = "HMGET"
+ val HMSET = "HMSET"
val HSCAN = "HSCAN"
val HSET = "HSET"
@@ -181,6 +182,7 @@ object Commands {
HGETALL -> {HGetAll(_)},
HKEYS -> {HKeys(_)},
HMGET -> {HMGet(_)},
+ HMSET -> {HMSet(_)},
HSCAN -> {HScan(_)},
HSET -> {HSet(_)},
@@ -296,6 +298,7 @@ object CommandBytes {
val HGETALL = StringToChannelBuffer("HGETALL")
val HKEYS = StringToChannelBuffer("HKEYS")
val HMGET = StringToChannelBuffer("HMGET")
+ val HMSET = StringToChannelBuffer("HMSET")
val HSCAN = StringToChannelBuffer("HSCAN")
val HSET = StringToChannelBuffer("HSET")
View
30 finagle-redis/src/main/scala/com/twitter/finagle/redis/protocol/commands/Hashes.scala
@@ -8,7 +8,7 @@ import org.jboss.netty.buffer.{ChannelBuffer, ChannelBuffers}
object HDel {
def apply(args: Seq[Array[Byte]]) = {
- RequireClientProtocol(args.length > 2, "HDEL requires a hash key and at least one field")
+ RequireClientProtocol(args.length >= 2, "HDEL requires a hash key and at least one field")
new HDel(ChannelBuffers.wrappedBuffer(args(0)),
args.drop(1).map(ChannelBuffers.wrappedBuffer(_)))
}
@@ -52,7 +52,7 @@ case class HKeys(key: ChannelBuffer) extends StrictKeyCommand {
object HMGet {
def apply(args: Seq[Array[Byte]]) = {
- RequireClientProtocol(args.length > 2, "HMGET requires a hash key and at least one field")
+ RequireClientProtocol(args.length >= 2, "HMGET requires a hash key and at least one field")
new HMGet(ChannelBuffers.wrappedBuffer(args(0)),
args.drop(1).map(ChannelBuffers.wrappedBuffer(_)))
}
@@ -62,6 +62,32 @@ case class HMGet(key: ChannelBuffer, fields: Seq[ChannelBuffer]) extends StrictK
def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HMGET, key) ++ fields)
}
+case class HMSet(key: ChannelBuffer, fv: Map[ChannelBuffer, ChannelBuffer]) extends StrictKeyCommand {
+ def command = Commands.HMSET
+
+ val fvList: Seq[ChannelBuffer] = fv.flatMap { case(f,v) =>
+ f :: v :: Nil
+ }(collection.breakOut)
+
+ def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HMSET, key) ++ fvList)
+}
+
+object HMSet {
+ def apply(args: Seq[Array[Byte]]) = {
+ RequireClientProtocol(args.length >= 3, "HMSET requires a hash key and at least one field and value")
+
+ val key = ChannelBuffers.wrappedBuffer(args(0))
+ val fv = args.drop(1).grouped(2).map {
+ case field :: value :: Nil => (ChannelBuffers.wrappedBuffer(field),
+ ChannelBuffers.wrappedBuffer(value))
+ case _ => throw ClientError("Unexpected uneven pair of elements in HMSET")
+ }.toMap
+
+ new HMSet(key, fv)
+ }
+}
+
+
case class HScan(
key: ChannelBuffer,
cursor: Long,
View
6 finagle-redis/src/test/scala/com/twitter/finagle/redis/integration/ClientSpec.scala
@@ -230,6 +230,12 @@ class ClientSpec extends SpecificationWithJUnit {
client.hMGet(foo, Seq(bar, boo))().toList) mustEqual Seq("baz", "moo")
}
+ "set multiple values" in {
+ client.hMSet(foo, Map(baz -> bar, moo -> boo))()
+ CBToString.fromList(
+ client.hMGet(foo, Seq(baz, moo))().toList) mustEqual Seq("bar", "boo")
+ }
+
"get multiple values at once" in {
client.hSet(foo, bar, baz)()
client.hSet(foo, boo, moo)()
Something went wrong with that request. Please try again.