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