]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
2 | ||
3 | #include <signal.h> | |
7dcc10b6 | 4 | #include <ctype.h> |
e2641e09 | 5 | |
c772d9c6 | 6 | void SlotToKeyAdd(robj *key); |
7 | void SlotToKeyDel(robj *key); | |
8 | ||
e2641e09 | 9 | /*----------------------------------------------------------------------------- |
10 | * C-level DB API | |
11 | *----------------------------------------------------------------------------*/ | |
12 | ||
13 | robj *lookupKey(redisDb *db, robj *key) { | |
14 | dictEntry *de = dictFind(db->dict,key->ptr); | |
15 | if (de) { | |
c0ba9ebe | 16 | robj *val = dictGetVal(de); |
e2641e09 | 17 | |
7d0966a6 | 18 | /* Update the access time for the aging algorithm. |
19 | * Don't do it if we have a saving child, as this will trigger | |
20 | * a copy on write madness. */ | |
f48cd4b9 | 21 | if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) |
7d0966a6 | 22 | val->lru = server.lruclock; |
e2641e09 | 23 | return val; |
24 | } else { | |
25 | return NULL; | |
26 | } | |
27 | } | |
28 | ||
29 | robj *lookupKeyRead(redisDb *db, robj *key) { | |
b80b1c59 | 30 | robj *val; |
31 | ||
e2641e09 | 32 | expireIfNeeded(db,key); |
b80b1c59 | 33 | val = lookupKey(db,key); |
34 | if (val == NULL) | |
35 | server.stat_keyspace_misses++; | |
36 | else | |
37 | server.stat_keyspace_hits++; | |
38 | return val; | |
e2641e09 | 39 | } |
40 | ||
41 | robj *lookupKeyWrite(redisDb *db, robj *key) { | |
bcf2995c | 42 | expireIfNeeded(db,key); |
e2641e09 | 43 | return lookupKey(db,key); |
44 | } | |
45 | ||
46 | robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) { | |
47 | robj *o = lookupKeyRead(c->db, key); | |
48 | if (!o) addReply(c,reply); | |
49 | return o; | |
50 | } | |
51 | ||
52 | robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) { | |
53 | robj *o = lookupKeyWrite(c->db, key); | |
54 | if (!o) addReply(c,reply); | |
55 | return o; | |
56 | } | |
57 | ||
f85cd526 | 58 | /* Add the key to the DB. It's up to the caller to increment the reference |
59 | * counte of the value if needed. | |
60 | * | |
61 | * The program is aborted if the key already exists. */ | |
62 | void dbAdd(redisDb *db, robj *key, robj *val) { | |
63 | sds copy = sdsdup(key->ptr); | |
64 | int retval = dictAdd(db->dict, copy, val); | |
65 | ||
eab0e26e | 66 | redisAssertWithInfo(NULL,key,retval == REDIS_OK); |
f85cd526 | 67 | if (server.cluster_enabled) SlotToKeyAdd(key); |
68 | } | |
69 | ||
70 | /* Overwrite an existing key with a new value. Incrementing the reference | |
71 | * count of the new value is up to the caller. | |
72 | * This function does not modify the expire time of the existing key. | |
73 | * | |
74 | * The program is aborted if the key was not already present. */ | |
75 | void dbOverwrite(redisDb *db, robj *key, robj *val) { | |
76 | struct dictEntry *de = dictFind(db->dict,key->ptr); | |
77 | ||
eab0e26e | 78 | redisAssertWithInfo(NULL,key,de != NULL); |
f85cd526 | 79 | dictReplace(db->dict, key->ptr, val); |
e2641e09 | 80 | } |
81 | ||
f85cd526 | 82 | /* High level Set operation. This function can be used in order to set |
83 | * a key, whatever it was existing or not, to a new object. | |
e2641e09 | 84 | * |
f85cd526 | 85 | * 1) The ref count of the value object is incremented. |
86 | * 2) clients WATCHing for the destination key notified. | |
87 | * 3) The expire time of the key is reset (the key is made persistent). */ | |
88 | void setKey(redisDb *db, robj *key, robj *val) { | |
89 | if (lookupKeyWrite(db,key) == NULL) { | |
90 | dbAdd(db,key,val); | |
e2641e09 | 91 | } else { |
f85cd526 | 92 | dbOverwrite(db,key,val); |
e2641e09 | 93 | } |
f85cd526 | 94 | incrRefCount(val); |
95 | removeExpire(db,key); | |
89f6f6ab | 96 | signalModifiedKey(db,key); |
e2641e09 | 97 | } |
98 | ||
99 | int dbExists(redisDb *db, robj *key) { | |
100 | return dictFind(db->dict,key->ptr) != NULL; | |
101 | } | |
102 | ||
103 | /* Return a random key, in form of a Redis object. | |
104 | * If there are no keys, NULL is returned. | |
105 | * | |
106 | * The function makes sure to return keys not already expired. */ | |
107 | robj *dbRandomKey(redisDb *db) { | |
108 | struct dictEntry *de; | |
109 | ||
110 | while(1) { | |
111 | sds key; | |
112 | robj *keyobj; | |
113 | ||
114 | de = dictGetRandomKey(db->dict); | |
115 | if (de == NULL) return NULL; | |
116 | ||
c0ba9ebe | 117 | key = dictGetKey(de); |
e2641e09 | 118 | keyobj = createStringObject(key,sdslen(key)); |
119 | if (dictFind(db->expires,key)) { | |
120 | if (expireIfNeeded(db,keyobj)) { | |
121 | decrRefCount(keyobj); | |
122 | continue; /* search for another key. This expired. */ | |
123 | } | |
124 | } | |
125 | return keyobj; | |
126 | } | |
127 | } | |
128 | ||
129 | /* Delete a key, value, and associated expiration entry if any, from the DB */ | |
130 | int dbDelete(redisDb *db, robj *key) { | |
131 | /* Deleting an entry from the expires dict will not free the sds of | |
132 | * the key, because it is shared with the main dictionary. */ | |
133 | if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr); | |
c772d9c6 | 134 | if (dictDelete(db->dict,key->ptr) == DICT_OK) { |
135 | if (server.cluster_enabled) SlotToKeyDel(key); | |
136 | return 1; | |
137 | } else { | |
138 | return 0; | |
139 | } | |
e2641e09 | 140 | } |
141 | ||
e2641e09 | 142 | long long emptyDb() { |
143 | int j; | |
144 | long long removed = 0; | |
145 | ||
146 | for (j = 0; j < server.dbnum; j++) { | |
147 | removed += dictSize(server.db[j].dict); | |
148 | dictEmpty(server.db[j].dict); | |
149 | dictEmpty(server.db[j].expires); | |
150 | } | |
151 | return removed; | |
152 | } | |
153 | ||
154 | int selectDb(redisClient *c, int id) { | |
155 | if (id < 0 || id >= server.dbnum) | |
156 | return REDIS_ERR; | |
157 | c->db = &server.db[id]; | |
158 | return REDIS_OK; | |
159 | } | |
160 | ||
cea8c5cd | 161 | /*----------------------------------------------------------------------------- |
162 | * Hooks for key space changes. | |
163 | * | |
164 | * Every time a key in the database is modified the function | |
165 | * signalModifiedKey() is called. | |
166 | * | |
167 | * Every time a DB is flushed the function signalFlushDb() is called. | |
168 | *----------------------------------------------------------------------------*/ | |
169 | ||
170 | void signalModifiedKey(redisDb *db, robj *key) { | |
171 | touchWatchedKey(db,key); | |
cea8c5cd | 172 | } |
173 | ||
174 | void signalFlushedDb(int dbid) { | |
175 | touchWatchedKeysOnFlush(dbid); | |
cea8c5cd | 176 | } |
177 | ||
e2641e09 | 178 | /*----------------------------------------------------------------------------- |
179 | * Type agnostic commands operating on the key space | |
180 | *----------------------------------------------------------------------------*/ | |
181 | ||
182 | void flushdbCommand(redisClient *c) { | |
183 | server.dirty += dictSize(c->db->dict); | |
cea8c5cd | 184 | signalFlushedDb(c->db->id); |
e2641e09 | 185 | dictEmpty(c->db->dict); |
186 | dictEmpty(c->db->expires); | |
187 | addReply(c,shared.ok); | |
188 | } | |
189 | ||
190 | void flushallCommand(redisClient *c) { | |
cea8c5cd | 191 | signalFlushedDb(-1); |
e2641e09 | 192 | server.dirty += emptyDb(); |
193 | addReply(c,shared.ok); | |
f48cd4b9 | 194 | if (server.rdb_child_pid != -1) { |
195 | kill(server.rdb_child_pid,SIGKILL); | |
196 | rdbRemoveTempFile(server.rdb_child_pid); | |
e2641e09 | 197 | } |
13cd1515 | 198 | if (server.saveparamslen > 0) { |
199 | /* Normally rdbSave() will reset dirty, but we don't want this here | |
200 | * as otherwise FLUSHALL will not be replicated nor put into the AOF. */ | |
201 | int saved_dirty = server.dirty; | |
f48cd4b9 | 202 | rdbSave(server.rdb_filename); |
13cd1515 | 203 | server.dirty = saved_dirty; |
204 | } | |
e2641e09 | 205 | server.dirty++; |
206 | } | |
207 | ||
208 | void delCommand(redisClient *c) { | |
209 | int deleted = 0, j; | |
210 | ||
211 | for (j = 1; j < c->argc; j++) { | |
212 | if (dbDelete(c->db,c->argv[j])) { | |
cea8c5cd | 213 | signalModifiedKey(c->db,c->argv[j]); |
e2641e09 | 214 | server.dirty++; |
215 | deleted++; | |
216 | } | |
217 | } | |
218 | addReplyLongLong(c,deleted); | |
219 | } | |
220 | ||
221 | void existsCommand(redisClient *c) { | |
222 | expireIfNeeded(c->db,c->argv[1]); | |
223 | if (dbExists(c->db,c->argv[1])) { | |
224 | addReply(c, shared.cone); | |
225 | } else { | |
226 | addReply(c, shared.czero); | |
227 | } | |
228 | } | |
229 | ||
230 | void selectCommand(redisClient *c) { | |
bfc197c3 | 231 | long id; |
232 | ||
233 | if (getLongFromObjectOrReply(c, c->argv[1], &id, | |
234 | "invalid DB index") != REDIS_OK) | |
235 | return; | |
e2641e09 | 236 | |
a7b058da | 237 | if (server.cluster_enabled && id != 0) { |
ecc91094 | 238 | addReplyError(c,"SELECT is not allowed in cluster mode"); |
239 | return; | |
240 | } | |
e2641e09 | 241 | if (selectDb(c,id) == REDIS_ERR) { |
3ab20376 | 242 | addReplyError(c,"invalid DB index"); |
e2641e09 | 243 | } else { |
244 | addReply(c,shared.ok); | |
245 | } | |
246 | } | |
247 | ||
248 | void randomkeyCommand(redisClient *c) { | |
249 | robj *key; | |
250 | ||
251 | if ((key = dbRandomKey(c->db)) == NULL) { | |
252 | addReply(c,shared.nullbulk); | |
253 | return; | |
254 | } | |
255 | ||
256 | addReplyBulk(c,key); | |
257 | decrRefCount(key); | |
258 | } | |
259 | ||
260 | void keysCommand(redisClient *c) { | |
261 | dictIterator *di; | |
262 | dictEntry *de; | |
263 | sds pattern = c->argv[1]->ptr; | |
e0e1c195 | 264 | int plen = sdslen(pattern), allkeys; |
e2641e09 | 265 | unsigned long numkeys = 0; |
b301c1fc | 266 | void *replylen = addDeferredMultiBulkLength(c); |
e2641e09 | 267 | |
cc4f65fe | 268 | di = dictGetSafeIterator(c->db->dict); |
e0e1c195 | 269 | allkeys = (pattern[0] == '*' && pattern[1] == '\0'); |
e2641e09 | 270 | while((de = dictNext(di)) != NULL) { |
c0ba9ebe | 271 | sds key = dictGetKey(de); |
e2641e09 | 272 | robj *keyobj; |
273 | ||
e0e1c195 | 274 | if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) { |
e2641e09 | 275 | keyobj = createStringObject(key,sdslen(key)); |
276 | if (expireIfNeeded(c->db,keyobj) == 0) { | |
277 | addReplyBulk(c,keyobj); | |
278 | numkeys++; | |
279 | } | |
280 | decrRefCount(keyobj); | |
281 | } | |
282 | } | |
283 | dictReleaseIterator(di); | |
b301c1fc | 284 | setDeferredMultiBulkLength(c,replylen,numkeys); |
e2641e09 | 285 | } |
286 | ||
287 | void dbsizeCommand(redisClient *c) { | |
b70d3555 | 288 | addReplyLongLong(c,dictSize(c->db->dict)); |
e2641e09 | 289 | } |
290 | ||
291 | void lastsaveCommand(redisClient *c) { | |
b70d3555 | 292 | addReplyLongLong(c,server.lastsave); |
e2641e09 | 293 | } |
294 | ||
295 | void typeCommand(redisClient *c) { | |
296 | robj *o; | |
297 | char *type; | |
298 | ||
299 | o = lookupKeyRead(c->db,c->argv[1]); | |
300 | if (o == NULL) { | |
3ab20376 | 301 | type = "none"; |
e2641e09 | 302 | } else { |
303 | switch(o->type) { | |
3ab20376 PN |
304 | case REDIS_STRING: type = "string"; break; |
305 | case REDIS_LIST: type = "list"; break; | |
306 | case REDIS_SET: type = "set"; break; | |
307 | case REDIS_ZSET: type = "zset"; break; | |
308 | case REDIS_HASH: type = "hash"; break; | |
309 | default: type = "unknown"; break; | |
e2641e09 | 310 | } |
311 | } | |
3ab20376 | 312 | addReplyStatus(c,type); |
e2641e09 | 313 | } |
314 | ||
e2641e09 | 315 | void shutdownCommand(redisClient *c) { |
4ab8695d | 316 | int flags = 0; |
317 | ||
318 | if (c->argc > 2) { | |
319 | addReply(c,shared.syntaxerr); | |
320 | return; | |
321 | } else if (c->argc == 2) { | |
322 | if (!strcasecmp(c->argv[1]->ptr,"nosave")) { | |
323 | flags |= REDIS_SHUTDOWN_NOSAVE; | |
324 | } else if (!strcasecmp(c->argv[1]->ptr,"save")) { | |
325 | flags |= REDIS_SHUTDOWN_SAVE; | |
326 | } else { | |
327 | addReply(c,shared.syntaxerr); | |
328 | return; | |
329 | } | |
330 | } | |
331 | if (prepareForShutdown(flags) == REDIS_OK) exit(0); | |
3ab20376 | 332 | addReplyError(c,"Errors trying to SHUTDOWN. Check logs."); |
e2641e09 | 333 | } |
334 | ||
335 | void renameGenericCommand(redisClient *c, int nx) { | |
336 | robj *o; | |
7dcc10b6 | 337 | long long expire; |
e2641e09 | 338 | |
339 | /* To use the same key as src and dst is probably an error */ | |
340 | if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) { | |
341 | addReply(c,shared.sameobjecterr); | |
342 | return; | |
343 | } | |
344 | ||
345 | if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL) | |
346 | return; | |
347 | ||
348 | incrRefCount(o); | |
4ab18a33 | 349 | expire = getExpire(c->db,c->argv[1]); |
f85cd526 | 350 | if (lookupKeyWrite(c->db,c->argv[2]) != NULL) { |
e2641e09 | 351 | if (nx) { |
352 | decrRefCount(o); | |
353 | addReply(c,shared.czero); | |
354 | return; | |
355 | } | |
4ab18a33 | 356 | /* Overwrite: delete the old key before creating the new one with the same name. */ |
357 | dbDelete(c->db,c->argv[2]); | |
e2641e09 | 358 | } |
4ab18a33 | 359 | dbAdd(c->db,c->argv[2],o); |
360 | if (expire != -1) setExpire(c->db,c->argv[2],expire); | |
e2641e09 | 361 | dbDelete(c->db,c->argv[1]); |
cea8c5cd | 362 | signalModifiedKey(c->db,c->argv[1]); |
363 | signalModifiedKey(c->db,c->argv[2]); | |
e2641e09 | 364 | server.dirty++; |
365 | addReply(c,nx ? shared.cone : shared.ok); | |
366 | } | |
367 | ||
368 | void renameCommand(redisClient *c) { | |
369 | renameGenericCommand(c,0); | |
370 | } | |
371 | ||
372 | void renamenxCommand(redisClient *c) { | |
373 | renameGenericCommand(c,1); | |
374 | } | |
375 | ||
376 | void moveCommand(redisClient *c) { | |
377 | robj *o; | |
378 | redisDb *src, *dst; | |
379 | int srcid; | |
380 | ||
ecc91094 | 381 | if (server.cluster_enabled) { |
382 | addReplyError(c,"MOVE is not allowed in cluster mode"); | |
383 | return; | |
384 | } | |
385 | ||
e2641e09 | 386 | /* Obtain source and target DB pointers */ |
387 | src = c->db; | |
388 | srcid = c->db->id; | |
389 | if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) { | |
390 | addReply(c,shared.outofrangeerr); | |
391 | return; | |
392 | } | |
393 | dst = c->db; | |
394 | selectDb(c,srcid); /* Back to the source DB */ | |
395 | ||
396 | /* If the user is moving using as target the same | |
397 | * DB as the source DB it is probably an error. */ | |
398 | if (src == dst) { | |
399 | addReply(c,shared.sameobjecterr); | |
400 | return; | |
401 | } | |
402 | ||
403 | /* Check if the element exists and get a reference */ | |
404 | o = lookupKeyWrite(c->db,c->argv[1]); | |
405 | if (!o) { | |
406 | addReply(c,shared.czero); | |
407 | return; | |
408 | } | |
409 | ||
f85cd526 | 410 | /* Return zero if the key already exists in the target DB */ |
411 | if (lookupKeyWrite(dst,c->argv[1]) != NULL) { | |
e2641e09 | 412 | addReply(c,shared.czero); |
413 | return; | |
414 | } | |
f85cd526 | 415 | dbAdd(dst,c->argv[1],o); |
e2641e09 | 416 | incrRefCount(o); |
417 | ||
418 | /* OK! key moved, free the entry in the source DB */ | |
419 | dbDelete(src,c->argv[1]); | |
420 | server.dirty++; | |
421 | addReply(c,shared.cone); | |
422 | } | |
423 | ||
424 | /*----------------------------------------------------------------------------- | |
425 | * Expires API | |
426 | *----------------------------------------------------------------------------*/ | |
427 | ||
428 | int removeExpire(redisDb *db, robj *key) { | |
429 | /* An expire may only be removed if there is a corresponding entry in the | |
430 | * main dict. Otherwise, the key will never be freed. */ | |
eab0e26e | 431 | redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL); |
a539d29a | 432 | return dictDelete(db->expires,key->ptr) == DICT_OK; |
e2641e09 | 433 | } |
434 | ||
7dcc10b6 | 435 | void setExpire(redisDb *db, robj *key, long long when) { |
436 | dictEntry *kde, *de; | |
e2641e09 | 437 | |
438 | /* Reuse the sds from the main dict in the expire dict */ | |
7dcc10b6 | 439 | kde = dictFind(db->dict,key->ptr); |
440 | redisAssertWithInfo(NULL,key,kde != NULL); | |
441 | de = dictReplaceRaw(db->expires,dictGetKey(kde)); | |
442 | dictSetSignedIntegerVal(de,when); | |
e2641e09 | 443 | } |
444 | ||
445 | /* Return the expire time of the specified key, or -1 if no expire | |
446 | * is associated with this key (i.e. the key is non volatile) */ | |
7dcc10b6 | 447 | long long getExpire(redisDb *db, robj *key) { |
e2641e09 | 448 | dictEntry *de; |
449 | ||
450 | /* No expire? return ASAP */ | |
451 | if (dictSize(db->expires) == 0 || | |
452 | (de = dictFind(db->expires,key->ptr)) == NULL) return -1; | |
453 | ||
454 | /* The entry was found in the expire dict, this means it should also | |
455 | * be present in the main dict (safety check). */ | |
eab0e26e | 456 | redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL); |
7dcc10b6 | 457 | return dictGetSignedIntegerVal(de); |
e2641e09 | 458 | } |
459 | ||
bcf2995c | 460 | /* Propagate expires into slaves and the AOF file. |
461 | * When a key expires in the master, a DEL operation for this key is sent | |
462 | * to all the slaves and the AOF file if enabled. | |
463 | * | |
464 | * This way the key expiry is centralized in one place, and since both | |
465 | * AOF and the master->slave link guarantee operation ordering, everything | |
466 | * will be consistent even if we allow write operations against expiring | |
467 | * keys. */ | |
468 | void propagateExpire(redisDb *db, robj *key) { | |
bcf2995c | 469 | robj *argv[2]; |
470 | ||
355f8591 | 471 | argv[0] = shared.del; |
bcf2995c | 472 | argv[1] = key; |
355f8591 | 473 | incrRefCount(argv[0]); |
474 | incrRefCount(argv[1]); | |
bcf2995c | 475 | |
e394114d | 476 | if (server.aof_state != REDIS_AOF_OFF) |
1b1f47c9 | 477 | feedAppendOnlyFile(server.delCommand,db->id,argv,2); |
bcf2995c | 478 | if (listLength(server.slaves)) |
479 | replicationFeedSlaves(server.slaves,db->id,argv,2); | |
480 | ||
c25a5d3b | 481 | decrRefCount(argv[0]); |
482 | decrRefCount(argv[1]); | |
bcf2995c | 483 | } |
484 | ||
e2641e09 | 485 | int expireIfNeeded(redisDb *db, robj *key) { |
7dcc10b6 | 486 | long long when = getExpire(db,key); |
bcf2995c | 487 | |
3a73be75 | 488 | if (when < 0) return 0; /* No expire for this key */ |
489 | ||
040b0ade HW |
490 | /* Don't expire anything while loading. It will be done later. */ |
491 | if (server.loading) return 0; | |
492 | ||
bcf2995c | 493 | /* If we are running in the context of a slave, return ASAP: |
494 | * the slave key expiration is controlled by the master that will | |
495 | * send us synthesized DEL operations for expired keys. | |
496 | * | |
497 | * Still we try to return the right information to the caller, | |
498 | * that is, 0 if we think the key should be still valid, 1 if | |
499 | * we think the key is expired at this time. */ | |
500 | if (server.masterhost != NULL) { | |
024f213b | 501 | return mstime() > when; |
bcf2995c | 502 | } |
503 | ||
e2641e09 | 504 | /* Return when this key has not expired */ |
7dcc10b6 | 505 | if (mstime() <= when) return 0; |
e2641e09 | 506 | |
507 | /* Delete the key */ | |
508 | server.stat_expiredkeys++; | |
bcf2995c | 509 | propagateExpire(db,key); |
e2641e09 | 510 | return dbDelete(db,key); |
511 | } | |
512 | ||
513 | /*----------------------------------------------------------------------------- | |
514 | * Expires Commands | |
515 | *----------------------------------------------------------------------------*/ | |
516 | ||
70381bbf | 517 | /* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT |
518 | * and PEXPIREAT. Because the commad second argument may be relative or absolute | |
519 | * the "basetime" argument is used to signal what the base time is (either 0 | |
520 | * for *AT variants of the command, or the current time for relative expires). | |
521 | * | |
522 | * unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for | |
523 | * the argv[2] parameter. The basetime is always specified in milliesconds. */ | |
524 | void expireGenericCommand(redisClient *c, long long basetime, int unit) { | |
e2641e09 | 525 | dictEntry *de; |
7dcc10b6 | 526 | robj *key = c->argv[1], *param = c->argv[2]; |
70381bbf | 527 | long long when; /* unix time in milliseconds when the key will expire. */ |
e2641e09 | 528 | |
70381bbf | 529 | if (getLongLongFromObjectOrReply(c, param, &when, NULL) != REDIS_OK) |
7dcc10b6 | 530 | return; |
e2641e09 | 531 | |
70381bbf | 532 | if (unit == UNIT_SECONDS) when *= 1000; |
533 | when += basetime; | |
e2641e09 | 534 | |
535 | de = dictFind(c->db->dict,key->ptr); | |
536 | if (de == NULL) { | |
537 | addReply(c,shared.czero); | |
538 | return; | |
539 | } | |
812ecc8b | 540 | /* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past |
541 | * should never be executed as a DEL when load the AOF or in the context | |
542 | * of a slave instance. | |
543 | * | |
544 | * Instead we take the other branch of the IF statement setting an expire | |
545 | * (possibly in the past) and wait for an explicit DEL from the master. */ | |
70381bbf | 546 | if (when <= mstime() && !server.loading && !server.masterhost) { |
812ecc8b | 547 | robj *aux; |
548 | ||
eab0e26e | 549 | redisAssertWithInfo(c,key,dbDelete(c->db,key)); |
812ecc8b | 550 | server.dirty++; |
551 | ||
552 | /* Replicate/AOF this as an explicit DEL. */ | |
553 | aux = createStringObject("DEL",3); | |
554 | rewriteClientCommandVector(c,2,aux,key); | |
555 | decrRefCount(aux); | |
cea8c5cd | 556 | signalModifiedKey(c->db,key); |
812ecc8b | 557 | addReply(c, shared.cone); |
e2641e09 | 558 | return; |
559 | } else { | |
70381bbf | 560 | setExpire(c->db,key,when); |
0cf5b7b5 | 561 | addReply(c,shared.cone); |
cea8c5cd | 562 | signalModifiedKey(c->db,key); |
0cf5b7b5 | 563 | server.dirty++; |
e2641e09 | 564 | return; |
565 | } | |
566 | } | |
567 | ||
568 | void expireCommand(redisClient *c) { | |
c6bf4a00 | 569 | expireGenericCommand(c,mstime(),UNIT_SECONDS); |
e2641e09 | 570 | } |
571 | ||
572 | void expireatCommand(redisClient *c) { | |
c6bf4a00 | 573 | expireGenericCommand(c,0,UNIT_SECONDS); |
e2641e09 | 574 | } |
575 | ||
12d293ca | 576 | void pexpireCommand(redisClient *c) { |
c6bf4a00 | 577 | expireGenericCommand(c,mstime(),UNIT_MILLISECONDS); |
12d293ca | 578 | } |
52d46855 | 579 | |
12d293ca | 580 | void pexpireatCommand(redisClient *c) { |
c6bf4a00 | 581 | expireGenericCommand(c,0,UNIT_MILLISECONDS); |
12d293ca | 582 | } |
583 | ||
584 | void ttlGenericCommand(redisClient *c, int output_ms) { | |
585 | long long expire, ttl = -1; | |
e2641e09 | 586 | |
587 | expire = getExpire(c->db,c->argv[1]); | |
588 | if (expire != -1) { | |
7dcc10b6 | 589 | ttl = expire-mstime(); |
e2641e09 | 590 | if (ttl < 0) ttl = -1; |
591 | } | |
7dcc10b6 | 592 | if (ttl == -1) { |
593 | addReplyLongLong(c,-1); | |
594 | } else { | |
52d46855 | 595 | addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000)); |
7dcc10b6 | 596 | } |
e2641e09 | 597 | } |
a539d29a | 598 | |
12d293ca | 599 | void ttlCommand(redisClient *c) { |
600 | ttlGenericCommand(c, 0); | |
601 | } | |
602 | ||
603 | void pttlCommand(redisClient *c) { | |
604 | ttlGenericCommand(c, 1); | |
605 | } | |
606 | ||
a539d29a | 607 | void persistCommand(redisClient *c) { |
608 | dictEntry *de; | |
609 | ||
610 | de = dictFind(c->db->dict,c->argv[1]->ptr); | |
611 | if (de == NULL) { | |
612 | addReply(c,shared.czero); | |
613 | } else { | |
1fb4e8de | 614 | if (removeExpire(c->db,c->argv[1])) { |
a539d29a | 615 | addReply(c,shared.cone); |
1fb4e8de | 616 | server.dirty++; |
617 | } else { | |
a539d29a | 618 | addReply(c,shared.czero); |
1fb4e8de | 619 | } |
a539d29a | 620 | } |
621 | } | |
9791f0f8 | 622 | |
623 | /* ----------------------------------------------------------------------------- | |
624 | * API to get key arguments from commands | |
625 | * ---------------------------------------------------------------------------*/ | |
626 | ||
627 | int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) { | |
628 | int j, i = 0, last, *keys; | |
629 | REDIS_NOTUSED(argv); | |
630 | ||
631 | if (cmd->firstkey == 0) { | |
632 | *numkeys = 0; | |
633 | return NULL; | |
634 | } | |
635 | last = cmd->lastkey; | |
636 | if (last < 0) last = argc+last; | |
637 | keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1)); | |
638 | for (j = cmd->firstkey; j <= last; j += cmd->keystep) { | |
639 | redisAssert(j < argc); | |
b4b51446 | 640 | keys[i++] = j; |
9791f0f8 | 641 | } |
b4b51446 | 642 | *numkeys = i; |
9791f0f8 | 643 | return keys; |
644 | } | |
645 | ||
646 | int *getKeysFromCommand(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { | |
647 | if (cmd->getkeys_proc) { | |
648 | return cmd->getkeys_proc(cmd,argv,argc,numkeys,flags); | |
649 | } else { | |
650 | return getKeysUsingCommandTable(cmd,argv,argc,numkeys); | |
651 | } | |
652 | } | |
653 | ||
654 | void getKeysFreeResult(int *result) { | |
655 | zfree(result); | |
656 | } | |
657 | ||
658 | int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { | |
659 | if (flags & REDIS_GETKEYS_PRELOAD) { | |
660 | *numkeys = 0; | |
661 | return NULL; | |
662 | } else { | |
663 | return getKeysUsingCommandTable(cmd,argv,argc,numkeys); | |
664 | } | |
665 | } | |
666 | ||
667 | int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { | |
668 | if (flags & REDIS_GETKEYS_PRELOAD) { | |
669 | int *keys = zmalloc(sizeof(int)); | |
670 | *numkeys = 1; | |
671 | keys[0] = 1; | |
4b61ca46 | 672 | return keys; |
9791f0f8 | 673 | } else { |
674 | return getKeysUsingCommandTable(cmd,argv,argc,numkeys); | |
675 | } | |
676 | } | |
677 | ||
678 | int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { | |
679 | int i, num, *keys; | |
680 | REDIS_NOTUSED(cmd); | |
681 | REDIS_NOTUSED(flags); | |
682 | ||
683 | num = atoi(argv[2]->ptr); | |
684 | /* Sanity check. Don't return any key if the command is going to | |
685 | * reply with syntax error. */ | |
686 | if (num > (argc-3)) { | |
687 | *numkeys = 0; | |
688 | return NULL; | |
689 | } | |
6e1b9b58 | 690 | keys = zmalloc(sizeof(int)*num); |
9791f0f8 | 691 | for (i = 0; i < num; i++) keys[i] = 3+i; |
692 | *numkeys = num; | |
693 | return keys; | |
694 | } | |
c772d9c6 | 695 | |
696 | /* Slot to Key API. This is used by Redis Cluster in order to obtain in | |
697 | * a fast way a key that belongs to a specified hash slot. This is useful | |
698 | * while rehashing the cluster. */ | |
699 | void SlotToKeyAdd(robj *key) { | |
700 | unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); | |
701 | ||
702 | zslInsert(server.cluster.slots_to_keys,hashslot,key); | |
703 | incrRefCount(key); | |
704 | } | |
705 | ||
706 | void SlotToKeyDel(robj *key) { | |
707 | unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); | |
708 | ||
709 | zslDelete(server.cluster.slots_to_keys,hashslot,key); | |
710 | } | |
711 | ||
484354ff | 712 | unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) { |
c772d9c6 | 713 | zskiplistNode *n; |
714 | zrangespec range; | |
484354ff | 715 | int j = 0; |
c772d9c6 | 716 | |
717 | range.min = range.max = hashslot; | |
718 | range.minex = range.maxex = 0; | |
719 | ||
720 | n = zslFirstInRange(server.cluster.slots_to_keys, range); | |
484354ff | 721 | while(n && n->score == hashslot && count--) { |
722 | keys[j++] = n->obj; | |
723 | n = n->level[0].forward; | |
724 | } | |
725 | return j; | |
c772d9c6 | 726 | } |