Skip to content

Commit

Permalink
Change alsoPropagate() behavior to make it more usable.
Browse files Browse the repository at this point in the history
Now the API automatically creates its argv copy and increment ref count
of passed objects.
  • Loading branch information
antirez committed Feb 11, 2015
1 parent 6b5922d commit cc7f043
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
21 changes: 19 additions & 2 deletions src/redis.c
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/t_set.c
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit cc7f043

Please sign in to comment.