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