Commit 53659404 authored by antirez's avatar antirez
Browse files

Improvements to PR #2425

1. Remove useless "cs" initialization.
2. Add a "select" var to capture a condition checked multiple times.
3. Avoid duplication of the same if (!copy) conditional.
4. Don't increment dirty if copy is given (no deletion is performed),
   otherwise we propagate MIGRATE when not needed.
parent 968ce964
...@@ -4484,7 +4484,6 @@ void migrateCommand(redisClient *c) { ...@@ -4484,7 +4484,6 @@ void migrateCommand(redisClient *c) {
try_again: try_again:
/* Initialization */ /* Initialization */
cs = NULL;
copy = 0; copy = 0;
replace = 0; replace = 0;
ttl = 0; ttl = 0;
...@@ -4522,13 +4521,15 @@ try_again: ...@@ -4522,13 +4521,15 @@ try_again:
rioInitWithBuffer(&cmd,sdsempty()); rioInitWithBuffer(&cmd,sdsempty());
/* Create RESTORE payload and generate the protocol to call the command. */ /* Send the SELECT command if the current DB is not already selected. */
if (cs->last_dbid != dbid) { int select = cs->last_dbid != dbid; /* Should we emit SELECT? */
if (select) {
redisAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',2)); redisAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',2));
redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"SELECT",6)); redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"SELECT",6));
redisAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,dbid)); redisAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,dbid));
} }
/* Create RESTORE payload and generate the protocol to call the command. */
expireat = getExpire(c->db,c->argv[3]); expireat = getExpire(c->db,c->argv[3]);
if (expireat != -1) { if (expireat != -1) {
ttl = expireat-mstime(); ttl = expireat-mstime();
...@@ -4578,12 +4579,12 @@ try_again: ...@@ -4578,12 +4579,12 @@ try_again:
char buf2[1024]; char buf2[1024];
/* Read the two replies */ /* Read the two replies */
if (cs->last_dbid != dbid && syncReadLine(cs->fd, buf1, sizeof(buf1), timeout) <= 0) if (select && syncReadLine(cs->fd, buf1, sizeof(buf1), timeout) <= 0)
goto socket_rd_err; goto socket_rd_err;
if (syncReadLine(cs->fd, buf2, sizeof(buf2), timeout) <= 0) if (syncReadLine(cs->fd, buf2, sizeof(buf2), timeout) <= 0)
goto socket_rd_err; goto socket_rd_err;
if ((cs->last_dbid != dbid && buf1[0] == '-') || buf2[0] == '-') { if ((select && buf1[0] == '-') || buf2[0] == '-') {
/* If we got an error at all, assume that the last_dbid is no longer valid */ /* On error assume that last_dbid is no longer valid. */
cs->last_dbid = -1; cs->last_dbid = -1;
addReplyErrorFormat(c,"Target instance replied with error: %s", addReplyErrorFormat(c,"Target instance replied with error: %s",
(cs->last_dbid != dbid && buf1[0] == '-') ? buf1+1 : buf2+1); (cs->last_dbid != dbid && buf1[0] == '-') ? buf1+1 : buf2+1);
...@@ -4592,15 +4593,14 @@ try_again: ...@@ -4592,15 +4593,14 @@ try_again:
cs->last_dbid = dbid; cs->last_dbid = dbid;
robj *aux; robj *aux;
addReply(c,shared.ok);
if (!copy) { if (!copy) {
/* No COPY option: remove the local key, signal the change. */ /* No COPY option: remove the local key, signal the change. */
dbDelete(c->db,c->argv[3]); dbDelete(c->db,c->argv[3]);
signalModifiedKey(c->db,c->argv[3]); signalModifiedKey(c->db,c->argv[3]);
} server.dirty++;
addReply(c,shared.ok);
server.dirty++;
if (!copy) {
/* Translate MIGRATE as DEL for replication/AOF. */ /* Translate MIGRATE as DEL for replication/AOF. */
aux = createStringObject("DEL",3); aux = createStringObject("DEL",3);
rewriteClientCommandVector(c,2,aux,c->argv[3]); rewriteClientCommandVector(c,2,aux,c->argv[3]);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment