| 1088 | } |
| 1089 | |
| 1090 | void renameGenericCommand(client *c, int nx) { |
| 1091 | robj *o; |
| 1092 | long long expire; |
| 1093 | int samekey = 0; |
| 1094 | |
| 1095 | /* When source and dest key is the same, no operation is performed, |
| 1096 | * if the key exists, however we still return an error on unexisting key. */ |
| 1097 | if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) samekey = 1; |
| 1098 | |
| 1099 | if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL) |
| 1100 | return; |
| 1101 | |
| 1102 | if (samekey) { |
| 1103 | addReply(c,nx ? shared.czero : shared.ok); |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | incrRefCount(o); |
| 1108 | expire = getExpire(c->db,c->argv[1]); |
| 1109 | if (lookupKeyWrite(c->db,c->argv[2]) != NULL) { |
| 1110 | if (nx) { |
| 1111 | decrRefCount(o); |
| 1112 | addReply(c,shared.czero); |
| 1113 | return; |
| 1114 | } |
| 1115 | /* Overwrite: delete the old key before creating the new one |
| 1116 | * with the same name. */ |
| 1117 | dbDelete(c->db,c->argv[2]); |
| 1118 | } |
| 1119 | dbAdd(c->db,c->argv[2],o); |
| 1120 | if (expire != -1) setExpire(c,c->db,c->argv[2],expire); |
| 1121 | dbDelete(c->db,c->argv[1]); |
| 1122 | signalModifiedKey(c,c->db,c->argv[1]); |
| 1123 | signalModifiedKey(c,c->db,c->argv[2]); |
| 1124 | notifyKeyspaceEvent(NOTIFY_GENERIC,"rename_from", |
| 1125 | c->argv[1],c->db->id); |
| 1126 | notifyKeyspaceEvent(NOTIFY_GENERIC,"rename_to", |
| 1127 | c->argv[2],c->db->id); |
| 1128 | server.dirty++; |
| 1129 | addReply(c,nx ? shared.cone : shared.ok); |
| 1130 | } |
| 1131 | |
| 1132 | void renameCommand(client *c) { |
| 1133 | renameGenericCommand(c,0); |
no test coverage detected