From 18b6cb7643f23c3c3d8e44dc4584167fb2b32b58 Mon Sep 17 00:00:00 2001 From: Damian Janowski Date: Sat, 27 Feb 2010 23:36:19 -0300 Subject: [PATCH] Add DISCARD command to discard queued MULTI commands. --- redis.c | 16 +++++++++++++++- test-redis.tcl | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/redis.c b/redis.c index 52ada380..319a0aff 100644 --- a/redis.c +++ b/redis.c @@ -653,6 +653,7 @@ static void zscoreCommand(redisClient *c); static void zremrangebyscoreCommand(redisClient *c); static void multiCommand(redisClient *c); static void execCommand(redisClient *c); +static void discardCommand(redisClient *c); static void blpopCommand(redisClient *c); static void brpopCommand(redisClient *c); static void appendCommand(redisClient *c); @@ -733,6 +734,7 @@ static struct redisCommand cmdTable[] = { {"type",typeCommand,2,REDIS_CMD_INLINE,1,1,1}, {"multi",multiCommand,1,REDIS_CMD_INLINE,0,0,0}, {"exec",execCommand,1,REDIS_CMD_INLINE,0,0,0}, + {"discard",discardCommand,1,REDIS_CMD_INLINE,0,0,0}, {"sync",syncCommand,1,REDIS_CMD_INLINE,0,0,0}, {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE,0,0,0}, {"flushall",flushallCommand,1,REDIS_CMD_INLINE,0,0,0}, @@ -2141,7 +2143,7 @@ static int processCommand(redisClient *c) { } /* Exec the command */ - if (c->flags & REDIS_MULTI && cmd->proc != execCommand) { + if (c->flags & REDIS_MULTI && cmd->proc != execCommand && cmd->proc != discardCommand) { queueMultiCommand(c,cmd); addReply(c,shared.queued); } else { @@ -6051,6 +6053,18 @@ static void multiCommand(redisClient *c) { addReply(c,shared.ok); } +static void discardCommand(redisClient *c) { + if (!(c->flags & REDIS_MULTI)) { + addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n")); + return; + } + + freeClientMultiState(c); + initClientMultiState(c); + c->flags &= (~REDIS_MULTI); + addReply(c,shared.ok); +} + static void execCommand(redisClient *c) { int j; robj **orig_argv; diff --git a/test-redis.tcl b/test-redis.tcl index 36b82e38..83dfd0b4 100644 --- a/test-redis.tcl +++ b/test-redis.tcl @@ -1663,6 +1663,18 @@ proc main {server port} { list $v1 $v2 $v3 } {QUEUED QUEUED {{a b c} PONG}} + test {DISCARD} { + $r del mylist + $r rpush mylist a + $r rpush mylist b + $r rpush mylist c + $r multi + set v1 [$r del mylist] + set v2 [$r discard] + set v3 [$r lrange mylist 0 -1] + list $v1 $v2 $v3 + } {QUEUED OK {a b c}} + test {APPEND basics} { list [$r append foo bar] [$r get foo] \ [$r append foo 100] [$r get foo]