summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-02-09 23:44:42 +0100
committerantirez <antirez@gmail.com>2015-02-09 23:44:42 +0100
commit54c27e079a2cb8137007baee8635ccabc13acbfb (patch)
tree40494714e75d23f2f58ec2ed3b296b913ba9926f
parent359113cc222fcd47e8dcd424f0fa086538b2457f (diff)
downloadredis-54c27e079a2cb8137007baee8635ccabc13acbfb.tar.gz
Change alsoPropagate() behavior to make it more usable.
Now the API automatically creates its argv copy and increment ref count of passed objects.
-rw-r--r--src/redis.c21
-rw-r--r--src/t_set.c12
2 files changed, 24 insertions, 9 deletions
diff --git a/src/redis.c b/src/redis.c
index 277d2e4b0..8bfcfdaad 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -2016,11 +2016,28 @@ void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
}
/* Used inside commands to schedule the propagation of additional commands
- * after the current command is propagated to AOF / Replication. */
+ * after the current command is propagated to AOF / Replication.
+ *
+ * 'cmd' must be a pointer to the Redis command to replicate, dbid is the
+ * database ID the command should be propagated into.
+ * Arguments of the command to propagte are passed as an array of redis
+ * objects pointers of len 'argc', using the 'argv' vector.
+ *
+ * The function does not take a reference to the passed 'argv' vector,
+ * so it is up to the caller to release the passed argv (but it is usually
+ * stack allocated). The function autoamtically increments ref count of
+ * passed objects, so the caller does not need to. */
void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
int target)
{
- redisOpArrayAppend(&server.also_propagate,cmd,dbid,argv,argc,target);
+ robj **argvcopy = zmalloc(sizeof(robj*)*argc);
+ int j;
+
+ for (j = 0; j < argc; j++) {
+ argvcopy[j] = argv[j];
+ incrRefCount(argv[j]);
+ }
+ redisOpArrayAppend(&server.also_propagate,cmd,dbid,argvcopy,argc,target);
}
/* It is possible to call the function forceCommandPropagation() inside a
diff --git a/src/t_set.c b/src/t_set.c
index 619b0f8a6..47aa173a1 100644
--- a/src/t_set.c
+++ b/src/t_set.c
@@ -556,10 +556,12 @@ void spopWithCountCommand(redisClient *c) {
{
setTypeIterator *si;
- robj *objele, **propargv;
+ robj *objele, *propargv[3];
int element_encoding;
addReplyMultiBulkLen(c, elements_returned);
+ propargv[0] = createStringObject("SREM",4);
+ propargv[1] = c->argv[1];
si = setTypeInitIterator(aux_set);
while ((element_encoding = setTypeNext(si, &objele, &llele)) != -1) {
@@ -574,17 +576,13 @@ void spopWithCountCommand(redisClient *c) {
addReplyBulk(c, objele);
/* Replicate/AOF this command as an SREM operation */
- propargv = zmalloc(sizeof(robj*)*3);
- propargv[0] = createStringObject("SREM",4);
- propargv[1] = c->argv[1];
- incrRefCount(c->argv[1]);
propargv[2] = objele;
- incrRefCount(objele);
-
alsoPropagate(server.sremCommand,c->db->id,propargv,3,REDIS_PROPAGATE_AOF|REDIS_PROPAGATE_REPL);
+
decrRefCount(objele);
server.dirty++;
}
+ decrRefCount(propargv[0]);
setTypeReleaseIterator(si);
}