Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2bf27c33
Commit
2bf27c33
authored
Mar 06, 2015
by
antirez
Browse files
Transactions: Implementation of the IF command
parent
509a6cc1
Changes
4
Show whitespace changes
Inline
Side-by-side
src/db.c
View file @
2bf27c33
...
@@ -62,7 +62,7 @@ robj *lookupKeyRead(redisDb *db, robj *key) {
...
@@ -62,7 +62,7 @@ robj *lookupKeyRead(redisDb *db, robj *key) {
if
(
expireIfNeeded
(
db
,
key
)
==
1
)
{
if
(
expireIfNeeded
(
db
,
key
)
==
1
)
{
/* Key expired. If we are in the context of a master, expireIfNeeded()
/* Key expired. If we are in the context of a master, expireIfNeeded()
* returns 0 only when the key does not exist at all, so it's sa
v
e
* returns 0 only when the key does not exist at all, so it's sa
f
e
* to return NULL ASAP. */
* to return NULL ASAP. */
if
(
server
.
masterhost
==
NULL
)
return
NULL
;
if
(
server
.
masterhost
==
NULL
)
return
NULL
;
...
...
src/multi.c
View file @
2bf27c33
...
@@ -304,6 +304,9 @@ void touchWatchedKeysOnFlush(int dbid) {
...
@@ -304,6 +304,9 @@ void touchWatchedKeysOnFlush(int dbid) {
}
}
}
}
/* WATCH key key key ...
* Add keys to the set of watched keys. If those keys are modified
* before the next transaction is executed, the transaction aborts. */
void
watchCommand
(
redisClient
*
c
)
{
void
watchCommand
(
redisClient
*
c
)
{
int
j
;
int
j
;
...
@@ -316,8 +319,47 @@ void watchCommand(redisClient *c) {
...
@@ -316,8 +319,47 @@ void watchCommand(redisClient *c) {
addReply
(
c
,
shared
.
ok
);
addReply
(
c
,
shared
.
ok
);
}
}
/* UNWATCH
* Flush the set of watched keys. */
void
unwatchCommand
(
redisClient
*
c
)
{
void
unwatchCommand
(
redisClient
*
c
)
{
unwatchAllKeys
(
c
);
unwatchAllKeys
(
c
);
c
->
flags
&=
(
~
REDIS_DIRTY_CAS
);
c
->
flags
&=
(
~
REDIS_DIRTY_CAS
);
addReply
(
c
,
shared
.
ok
);
addReply
(
c
,
shared
.
ok
);
}
}
/* IF XX key key key ...
* IF NX key key key ...
* Abort transaction if condition is not met. */
void
ifCommand
(
redisClient
*
c
)
{
int
j
;
int
xx
=
0
,
nx
=
0
;
if
(
!
(
c
->
flags
&
REDIS_MULTI
))
{
addReplyError
(
c
,
"IF outside MULTI is not allowed"
);
return
;
}
/* Check if it's xx or nx option, trying to do it reasonably fast. */
char
*
a
=
c
->
argv
[
1
]
->
ptr
;
if
((
a
[
0
]
==
'n'
||
a
[
0
]
==
'N'
)
&&
(
a
[
1
]
==
'x'
||
a
[
1
]
==
'X'
)
&&
a
[
2
]
==
'\0'
)
{
nx
=
1
;
}
else
if
((
a
[
0
]
==
'x'
||
a
[
0
]
==
'X'
)
&&
(
a
[
1
]
==
'x'
||
a
[
1
]
==
'X'
)
&&
a
[
2
]
==
'\0'
)
{
xx
=
1
;
}
else
{
addReply
(
c
,
shared
.
syntaxerr
);
return
;
}
/* Check if keys exist / don't exist, and flag the transaction if
* at least one key fails the test according to NX / XX option. */
for
(
j
=
2
;
j
<
c
->
argc
;
j
++
)
{
robj
*
o
=
lookupKeyRead
(
c
->
db
,
c
->
argv
[
j
]);
if
((
o
!=
NULL
&&
nx
)
||
(
o
==
NULL
&&
xx
))
c
->
flags
|=
REDIS_DIRTY_CAS
;
}
addReply
(
c
,
shared
.
ok
);
}
src/redis.c
View file @
2bf27c33
...
@@ -261,6 +261,7 @@ struct redisCommand redisCommandTable[] = {
...
@@ -261,6 +261,7 @@ struct redisCommand redisCommandTable[] = {
{
"pubsub"
,
pubsubCommand
,
-
2
,
"pltrR"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"pubsub"
,
pubsubCommand
,
-
2
,
"pltrR"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"watch"
,
watchCommand
,
-
2
,
"rsF"
,
0
,
NULL
,
1
,
-
1
,
1
,
0
,
0
},
{
"watch"
,
watchCommand
,
-
2
,
"rsF"
,
0
,
NULL
,
1
,
-
1
,
1
,
0
,
0
},
{
"unwatch"
,
unwatchCommand
,
1
,
"rsF"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"unwatch"
,
unwatchCommand
,
1
,
"rsF"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"if"
,
ifCommand
,
-
2
,
"rsF"
,
0
,
NULL
,
2
,
-
1
,
1
,
0
,
0
},
{
"cluster"
,
clusterCommand
,
-
2
,
"ar"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"cluster"
,
clusterCommand
,
-
2
,
"ar"
,
0
,
NULL
,
0
,
0
,
0
,
0
,
0
},
{
"restore"
,
restoreCommand
,
-
4
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"restore"
,
restoreCommand
,
-
4
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"restore-asking"
,
restoreCommand
,
-
4
,
"wmk"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"restore-asking"
,
restoreCommand
,
-
4
,
"wmk"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
...
@@ -2336,7 +2337,7 @@ int processCommand(redisClient *c) {
...
@@ -2336,7 +2337,7 @@ int processCommand(redisClient *c) {
/* Exec the command */
/* Exec the command */
if
(
c
->
flags
&
REDIS_MULTI
&&
if
(
c
->
flags
&
REDIS_MULTI
&&
c
->
cmd
->
proc
!=
execCommand
&&
c
->
cmd
->
proc
!=
discard
Command
&&
c
->
cmd
->
proc
!=
execCommand
&&
c
->
cmd
->
proc
!=
if
Command
&&
c
->
cmd
->
proc
!=
multiCommand
&&
c
->
cmd
->
proc
!=
watchCommand
)
c
->
cmd
->
proc
!=
multiCommand
&&
c
->
cmd
->
proc
!=
watchCommand
)
{
{
queueMultiCommand
(
c
);
queueMultiCommand
(
c
);
...
...
src/redis.h
View file @
2bf27c33
...
@@ -1561,6 +1561,7 @@ void pfcountCommand(redisClient *c);
...
@@ -1561,6 +1561,7 @@ void pfcountCommand(redisClient *c);
void
pfmergeCommand
(
redisClient
*
c
);
void
pfmergeCommand
(
redisClient
*
c
);
void
pfdebugCommand
(
redisClient
*
c
);
void
pfdebugCommand
(
redisClient
*
c
);
void
latencyCommand
(
redisClient
*
c
);
void
latencyCommand
(
redisClient
*
c
);
void
ifCommand
(
redisClient
*
c
);
#if defined(__GNUC__)
#if defined(__GNUC__)
void
*
calloc
(
size_t
count
,
size_t
size
)
__attribute__
((
deprecated
));
void
*
calloc
(
size_t
count
,
size_t
size
)
__attribute__
((
deprecated
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment