]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
2 | ||
3 | #include <signal.h> | |
4 | ||
c772d9c6 | 5 | void SlotToKeyAdd(robj *key); |
6 | void SlotToKeyDel(robj *key); | |
7 | ||
e2641e09 | 8 | /*----------------------------------------------------------------------------- |
9 | * C-level DB API | |
10 | *----------------------------------------------------------------------------*/ | |
11 | ||
5d46e370 | 12 | /* Important notes on lookup and disk store. |
13 | * | |
14 | * When disk store is enabled on lookup we can have different cases. | |
15 | * | |
16 | * a) The key is in memory: | |
17 | * - If the key is not in IO_SAVEINPROG state we can access it. | |
18 | * As if it's just IO_SAVE this means we have the key in the IO queue | |
19 | * but can't be accessed by the IO thread (it requires to be | |
20 | * translated into an IO Job by the cache cron function.) | |
21 | * - If the key is in IO_SAVEINPROG we can't touch the key and have | |
22 | * to blocking wait completion of operations. | |
23 | * b) The key is not in memory: | |
24 | * - If it's marked as non existing on disk as well (negative cache) | |
25 | * we don't need to perform the disk access. | |
26 | * - if the key MAY EXIST, but is not in memory, and it is marked as IO_SAVE | |
27 | * then the key can only be a deleted one. As IO_SAVE keys are never | |
28 | * evicted (dirty state), so the only possibility is that key was deleted. | |
29 | * - if the key MAY EXIST we need to blocking load it. | |
30 | * We check that the key is not in IO_SAVEINPROG state before accessing | |
31 | * the disk object. If it is in this state, we wait. | |
32 | */ | |
33 | ||
34 | void lookupWaitBusyKey(redisDb *db, robj *key) { | |
35 | /* FIXME: wait just for this key, not everything */ | |
36 | waitEmptyIOJobsQueue(); | |
37 | processAllPendingIOJobs(); | |
38 | redisAssert((cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG) == 0); | |
39 | } | |
40 | ||
e2641e09 | 41 | robj *lookupKey(redisDb *db, robj *key) { |
42 | dictEntry *de = dictFind(db->dict,key->ptr); | |
43 | if (de) { | |
44 | robj *val = dictGetEntryVal(de); | |
45 | ||
7d0966a6 | 46 | /* Update the access time for the aging algorithm. |
47 | * Don't do it if we have a saving child, as this will trigger | |
48 | * a copy on write madness. */ | |
49 | if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1) | |
50 | val->lru = server.lruclock; | |
ef59a8bc | 51 | |
3be00d7e | 52 | if (server.ds_enabled && |
53 | cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG) | |
54 | { | |
5d46e370 | 55 | /* Need to wait for the key to get unbusy */ |
5ab7bbfc | 56 | redisLog(REDIS_DEBUG,"Lookup found a key in SAVEINPROG state. Waiting. (Key was in the cache)"); |
5d46e370 | 57 | lookupWaitBusyKey(db,key); |
e2641e09 | 58 | } |
53eeeaff | 59 | server.stat_keyspace_hits++; |
e2641e09 | 60 | return val; |
61 | } else { | |
ad01a255 | 62 | time_t expire; |
63 | robj *val; | |
64 | ||
65 | /* Key not found in the in memory hash table, but if disk store is | |
66 | * enabled we may have this key on disk. If so load it in memory | |
9a373028 | 67 | * in a blocking way. */ |
ad01a255 | 68 | if (server.ds_enabled && cacheKeyMayExist(db,key)) { |
5d46e370 | 69 | long flags = cacheScheduleIOGetFlags(db,key); |
70 | ||
71 | /* They key is not in cache, but it has a SAVE op in queue? | |
72 | * The only possibility is that the key was deleted, since | |
73 | * dirty keys are not evicted. */ | |
74 | if (flags & REDIS_IO_SAVE) { | |
75 | server.stat_keyspace_misses++; | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | /* At this point we need to blocking load the key in memory. | |
80 | * The first thing we do is waiting here if the key is busy. */ | |
81 | if (flags & REDIS_IO_SAVEINPROG) { | |
5ab7bbfc | 82 | redisLog(REDIS_DEBUG,"Lookup found a key in SAVEINPROG state. Waiting (while force loading)."); |
5d46e370 | 83 | lookupWaitBusyKey(db,key); |
9a373028 | 84 | } |
85 | ||
5d46e370 | 86 | redisLog(REDIS_DEBUG,"Force loading key %s via lookup", key->ptr); |
ad01a255 | 87 | val = dsGet(db,key,&expire); |
88 | if (val) { | |
89 | int retval = dbAdd(db,key,val); | |
90 | redisAssert(retval == REDIS_OK); | |
91 | if (expire != -1) setExpire(db,key,expire); | |
92 | server.stat_keyspace_hits++; | |
93 | return val; | |
5d46e370 | 94 | } else { |
95 | cacheSetKeyDoesNotExist(db,key); | |
ad01a255 | 96 | } |
97 | } | |
53eeeaff | 98 | server.stat_keyspace_misses++; |
e2641e09 | 99 | return NULL; |
100 | } | |
101 | } | |
102 | ||
103 | robj *lookupKeyRead(redisDb *db, robj *key) { | |
104 | expireIfNeeded(db,key); | |
105 | return lookupKey(db,key); | |
106 | } | |
107 | ||
108 | robj *lookupKeyWrite(redisDb *db, robj *key) { | |
bcf2995c | 109 | expireIfNeeded(db,key); |
e2641e09 | 110 | return lookupKey(db,key); |
111 | } | |
112 | ||
113 | robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) { | |
114 | robj *o = lookupKeyRead(c->db, key); | |
115 | if (!o) addReply(c,reply); | |
116 | return o; | |
117 | } | |
118 | ||
119 | robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) { | |
120 | robj *o = lookupKeyWrite(c->db, key); | |
121 | if (!o) addReply(c,reply); | |
122 | return o; | |
123 | } | |
124 | ||
125 | /* Add the key to the DB. If the key already exists REDIS_ERR is returned, | |
126 | * otherwise REDIS_OK is returned, and the caller should increment the | |
127 | * refcount of 'val'. */ | |
128 | int dbAdd(redisDb *db, robj *key, robj *val) { | |
129 | /* Perform a lookup before adding the key, as we need to copy the | |
130 | * key value. */ | |
131 | if (dictFind(db->dict, key->ptr) != NULL) { | |
132 | return REDIS_ERR; | |
133 | } else { | |
134 | sds copy = sdsdup(key->ptr); | |
135 | dictAdd(db->dict, copy, val); | |
c15a3887 | 136 | if (server.ds_enabled) cacheSetKeyMayExist(db,key); |
c772d9c6 | 137 | if (server.cluster_enabled) SlotToKeyAdd(key); |
e2641e09 | 138 | return REDIS_OK; |
139 | } | |
140 | } | |
141 | ||
142 | /* If the key does not exist, this is just like dbAdd(). Otherwise | |
143 | * the value associated to the key is replaced with the new one. | |
144 | * | |
145 | * On update (key already existed) 0 is returned. Otherwise 1. */ | |
146 | int dbReplace(redisDb *db, robj *key, robj *val) { | |
a440ecf0 | 147 | robj *oldval; |
c15a3887 | 148 | int retval; |
a440ecf0 | 149 | |
150 | if ((oldval = dictFetchValue(db->dict,key->ptr)) == NULL) { | |
e2641e09 | 151 | sds copy = sdsdup(key->ptr); |
152 | dictAdd(db->dict, copy, val); | |
c772d9c6 | 153 | if (server.cluster_enabled) SlotToKeyAdd(key); |
c15a3887 | 154 | retval = 1; |
e2641e09 | 155 | } else { |
156 | dictReplace(db->dict, key->ptr, val); | |
c15a3887 | 157 | retval = 0; |
e2641e09 | 158 | } |
c15a3887 | 159 | if (server.ds_enabled) cacheSetKeyMayExist(db,key); |
160 | return retval; | |
e2641e09 | 161 | } |
162 | ||
163 | int dbExists(redisDb *db, robj *key) { | |
164 | return dictFind(db->dict,key->ptr) != NULL; | |
165 | } | |
166 | ||
167 | /* Return a random key, in form of a Redis object. | |
168 | * If there are no keys, NULL is returned. | |
169 | * | |
170 | * The function makes sure to return keys not already expired. */ | |
171 | robj *dbRandomKey(redisDb *db) { | |
172 | struct dictEntry *de; | |
173 | ||
174 | while(1) { | |
175 | sds key; | |
176 | robj *keyobj; | |
177 | ||
178 | de = dictGetRandomKey(db->dict); | |
179 | if (de == NULL) return NULL; | |
180 | ||
181 | key = dictGetEntryKey(de); | |
182 | keyobj = createStringObject(key,sdslen(key)); | |
183 | if (dictFind(db->expires,key)) { | |
184 | if (expireIfNeeded(db,keyobj)) { | |
185 | decrRefCount(keyobj); | |
186 | continue; /* search for another key. This expired. */ | |
187 | } | |
188 | } | |
189 | return keyobj; | |
190 | } | |
191 | } | |
192 | ||
193 | /* Delete a key, value, and associated expiration entry if any, from the DB */ | |
194 | int dbDelete(redisDb *db, robj *key) { | |
d934e1e8 | 195 | /* If diskstore is enabled make sure to awake waiting clients for this key |
196 | * as it is not really useful to wait for a key already deleted to be | |
197 | * loaded from disk. */ | |
c15a3887 | 198 | if (server.ds_enabled) { |
199 | handleClientsBlockedOnSwappedKey(db,key); | |
200 | cacheSetKeyDoesNotExist(db,key); | |
201 | } | |
16d77878 | 202 | |
e2641e09 | 203 | /* Deleting an entry from the expires dict will not free the sds of |
204 | * the key, because it is shared with the main dictionary. */ | |
205 | if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr); | |
c772d9c6 | 206 | if (dictDelete(db->dict,key->ptr) == DICT_OK) { |
207 | if (server.cluster_enabled) SlotToKeyDel(key); | |
208 | return 1; | |
209 | } else { | |
210 | return 0; | |
211 | } | |
e2641e09 | 212 | } |
213 | ||
69bfffb4 | 214 | /* Empty the whole database. |
215 | * If diskstore is enabled this function will just flush the in-memory cache. */ | |
e2641e09 | 216 | long long emptyDb() { |
217 | int j; | |
218 | long long removed = 0; | |
219 | ||
220 | for (j = 0; j < server.dbnum; j++) { | |
221 | removed += dictSize(server.db[j].dict); | |
222 | dictEmpty(server.db[j].dict); | |
223 | dictEmpty(server.db[j].expires); | |
69bfffb4 | 224 | if (server.ds_enabled) dictEmpty(server.db[j].io_negcache); |
e2641e09 | 225 | } |
226 | return removed; | |
227 | } | |
228 | ||
229 | int selectDb(redisClient *c, int id) { | |
230 | if (id < 0 || id >= server.dbnum) | |
231 | return REDIS_ERR; | |
232 | c->db = &server.db[id]; | |
233 | return REDIS_OK; | |
234 | } | |
235 | ||
cea8c5cd | 236 | /*----------------------------------------------------------------------------- |
237 | * Hooks for key space changes. | |
238 | * | |
239 | * Every time a key in the database is modified the function | |
240 | * signalModifiedKey() is called. | |
241 | * | |
242 | * Every time a DB is flushed the function signalFlushDb() is called. | |
243 | *----------------------------------------------------------------------------*/ | |
244 | ||
245 | void signalModifiedKey(redisDb *db, robj *key) { | |
246 | touchWatchedKey(db,key); | |
247 | if (server.ds_enabled) | |
3be00d7e | 248 | cacheScheduleIO(db,key,REDIS_IO_SAVE); |
cea8c5cd | 249 | } |
250 | ||
251 | void signalFlushedDb(int dbid) { | |
252 | touchWatchedKeysOnFlush(dbid); | |
cea8c5cd | 253 | } |
254 | ||
e2641e09 | 255 | /*----------------------------------------------------------------------------- |
256 | * Type agnostic commands operating on the key space | |
257 | *----------------------------------------------------------------------------*/ | |
258 | ||
259 | void flushdbCommand(redisClient *c) { | |
260 | server.dirty += dictSize(c->db->dict); | |
cea8c5cd | 261 | signalFlushedDb(c->db->id); |
e2641e09 | 262 | dictEmpty(c->db->dict); |
263 | dictEmpty(c->db->expires); | |
120b9ba8 | 264 | if (server.ds_enabled) dsFlushDb(c->db->id); |
e2641e09 | 265 | addReply(c,shared.ok); |
266 | } | |
267 | ||
268 | void flushallCommand(redisClient *c) { | |
cea8c5cd | 269 | signalFlushedDb(-1); |
e2641e09 | 270 | server.dirty += emptyDb(); |
271 | addReply(c,shared.ok); | |
272 | if (server.bgsavechildpid != -1) { | |
273 | kill(server.bgsavechildpid,SIGKILL); | |
274 | rdbRemoveTempFile(server.bgsavechildpid); | |
275 | } | |
120b9ba8 | 276 | if (server.ds_enabled) |
277 | dsFlushDb(-1); | |
278 | else | |
279 | rdbSave(server.dbfilename); | |
e2641e09 | 280 | server.dirty++; |
281 | } | |
282 | ||
283 | void delCommand(redisClient *c) { | |
284 | int deleted = 0, j; | |
285 | ||
286 | for (j = 1; j < c->argc; j++) { | |
31222292 | 287 | if (server.ds_enabled) { |
288 | lookupKeyRead(c->db,c->argv[j]); | |
289 | /* FIXME: this can be optimized a lot, no real need to load | |
290 | * a possibly huge value. */ | |
291 | } | |
e2641e09 | 292 | if (dbDelete(c->db,c->argv[j])) { |
cea8c5cd | 293 | signalModifiedKey(c->db,c->argv[j]); |
e2641e09 | 294 | server.dirty++; |
295 | deleted++; | |
31222292 | 296 | } else if (server.ds_enabled) { |
297 | if (cacheKeyMayExist(c->db,c->argv[j]) && | |
298 | dsExists(c->db,c->argv[j])) | |
299 | { | |
3be00d7e | 300 | cacheScheduleIO(c->db,c->argv[j],REDIS_IO_SAVE); |
31222292 | 301 | deleted = 1; |
302 | } | |
e2641e09 | 303 | } |
304 | } | |
305 | addReplyLongLong(c,deleted); | |
306 | } | |
307 | ||
308 | void existsCommand(redisClient *c) { | |
309 | expireIfNeeded(c->db,c->argv[1]); | |
310 | if (dbExists(c->db,c->argv[1])) { | |
311 | addReply(c, shared.cone); | |
312 | } else { | |
313 | addReply(c, shared.czero); | |
314 | } | |
315 | } | |
316 | ||
317 | void selectCommand(redisClient *c) { | |
318 | int id = atoi(c->argv[1]->ptr); | |
319 | ||
ecc91094 | 320 | if (server.cluster_enabled) { |
321 | addReplyError(c,"SELECT is not allowed in cluster mode"); | |
322 | return; | |
323 | } | |
e2641e09 | 324 | if (selectDb(c,id) == REDIS_ERR) { |
3ab20376 | 325 | addReplyError(c,"invalid DB index"); |
e2641e09 | 326 | } else { |
327 | addReply(c,shared.ok); | |
328 | } | |
329 | } | |
330 | ||
331 | void randomkeyCommand(redisClient *c) { | |
332 | robj *key; | |
333 | ||
334 | if ((key = dbRandomKey(c->db)) == NULL) { | |
335 | addReply(c,shared.nullbulk); | |
336 | return; | |
337 | } | |
338 | ||
339 | addReplyBulk(c,key); | |
340 | decrRefCount(key); | |
341 | } | |
342 | ||
343 | void keysCommand(redisClient *c) { | |
344 | dictIterator *di; | |
345 | dictEntry *de; | |
346 | sds pattern = c->argv[1]->ptr; | |
e0e1c195 | 347 | int plen = sdslen(pattern), allkeys; |
e2641e09 | 348 | unsigned long numkeys = 0; |
b301c1fc | 349 | void *replylen = addDeferredMultiBulkLength(c); |
e2641e09 | 350 | |
351 | di = dictGetIterator(c->db->dict); | |
e0e1c195 | 352 | allkeys = (pattern[0] == '*' && pattern[1] == '\0'); |
e2641e09 | 353 | while((de = dictNext(di)) != NULL) { |
354 | sds key = dictGetEntryKey(de); | |
355 | robj *keyobj; | |
356 | ||
e0e1c195 | 357 | if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) { |
e2641e09 | 358 | keyobj = createStringObject(key,sdslen(key)); |
359 | if (expireIfNeeded(c->db,keyobj) == 0) { | |
360 | addReplyBulk(c,keyobj); | |
361 | numkeys++; | |
362 | } | |
363 | decrRefCount(keyobj); | |
364 | } | |
365 | } | |
366 | dictReleaseIterator(di); | |
b301c1fc | 367 | setDeferredMultiBulkLength(c,replylen,numkeys); |
e2641e09 | 368 | } |
369 | ||
370 | void dbsizeCommand(redisClient *c) { | |
b70d3555 | 371 | addReplyLongLong(c,dictSize(c->db->dict)); |
e2641e09 | 372 | } |
373 | ||
374 | void lastsaveCommand(redisClient *c) { | |
b70d3555 | 375 | addReplyLongLong(c,server.lastsave); |
e2641e09 | 376 | } |
377 | ||
378 | void typeCommand(redisClient *c) { | |
379 | robj *o; | |
380 | char *type; | |
381 | ||
382 | o = lookupKeyRead(c->db,c->argv[1]); | |
383 | if (o == NULL) { | |
3ab20376 | 384 | type = "none"; |
e2641e09 | 385 | } else { |
386 | switch(o->type) { | |
3ab20376 PN |
387 | case REDIS_STRING: type = "string"; break; |
388 | case REDIS_LIST: type = "list"; break; | |
389 | case REDIS_SET: type = "set"; break; | |
390 | case REDIS_ZSET: type = "zset"; break; | |
391 | case REDIS_HASH: type = "hash"; break; | |
392 | default: type = "unknown"; break; | |
e2641e09 | 393 | } |
394 | } | |
3ab20376 | 395 | addReplyStatus(c,type); |
e2641e09 | 396 | } |
397 | ||
e2641e09 | 398 | void shutdownCommand(redisClient *c) { |
399 | if (prepareForShutdown() == REDIS_OK) | |
400 | exit(0); | |
3ab20376 | 401 | addReplyError(c,"Errors trying to SHUTDOWN. Check logs."); |
e2641e09 | 402 | } |
403 | ||
404 | void renameGenericCommand(redisClient *c, int nx) { | |
405 | robj *o; | |
406 | ||
407 | /* To use the same key as src and dst is probably an error */ | |
408 | if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) { | |
409 | addReply(c,shared.sameobjecterr); | |
410 | return; | |
411 | } | |
412 | ||
413 | if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL) | |
414 | return; | |
415 | ||
416 | incrRefCount(o); | |
e2641e09 | 417 | if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) { |
418 | if (nx) { | |
419 | decrRefCount(o); | |
420 | addReply(c,shared.czero); | |
421 | return; | |
422 | } | |
423 | dbReplace(c->db,c->argv[2],o); | |
424 | } | |
425 | dbDelete(c->db,c->argv[1]); | |
cea8c5cd | 426 | signalModifiedKey(c->db,c->argv[1]); |
427 | signalModifiedKey(c->db,c->argv[2]); | |
e2641e09 | 428 | server.dirty++; |
429 | addReply(c,nx ? shared.cone : shared.ok); | |
430 | } | |
431 | ||
432 | void renameCommand(redisClient *c) { | |
433 | renameGenericCommand(c,0); | |
434 | } | |
435 | ||
436 | void renamenxCommand(redisClient *c) { | |
437 | renameGenericCommand(c,1); | |
438 | } | |
439 | ||
440 | void moveCommand(redisClient *c) { | |
441 | robj *o; | |
442 | redisDb *src, *dst; | |
443 | int srcid; | |
444 | ||
ecc91094 | 445 | if (server.cluster_enabled) { |
446 | addReplyError(c,"MOVE is not allowed in cluster mode"); | |
447 | return; | |
448 | } | |
449 | ||
e2641e09 | 450 | /* Obtain source and target DB pointers */ |
451 | src = c->db; | |
452 | srcid = c->db->id; | |
453 | if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) { | |
454 | addReply(c,shared.outofrangeerr); | |
455 | return; | |
456 | } | |
457 | dst = c->db; | |
458 | selectDb(c,srcid); /* Back to the source DB */ | |
459 | ||
460 | /* If the user is moving using as target the same | |
461 | * DB as the source DB it is probably an error. */ | |
462 | if (src == dst) { | |
463 | addReply(c,shared.sameobjecterr); | |
464 | return; | |
465 | } | |
466 | ||
467 | /* Check if the element exists and get a reference */ | |
468 | o = lookupKeyWrite(c->db,c->argv[1]); | |
469 | if (!o) { | |
470 | addReply(c,shared.czero); | |
471 | return; | |
472 | } | |
473 | ||
474 | /* Try to add the element to the target DB */ | |
e2641e09 | 475 | if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) { |
476 | addReply(c,shared.czero); | |
477 | return; | |
478 | } | |
479 | incrRefCount(o); | |
480 | ||
481 | /* OK! key moved, free the entry in the source DB */ | |
482 | dbDelete(src,c->argv[1]); | |
483 | server.dirty++; | |
484 | addReply(c,shared.cone); | |
485 | } | |
486 | ||
487 | /*----------------------------------------------------------------------------- | |
488 | * Expires API | |
489 | *----------------------------------------------------------------------------*/ | |
490 | ||
491 | int removeExpire(redisDb *db, robj *key) { | |
492 | /* An expire may only be removed if there is a corresponding entry in the | |
493 | * main dict. Otherwise, the key will never be freed. */ | |
494 | redisAssert(dictFind(db->dict,key->ptr) != NULL); | |
a539d29a | 495 | return dictDelete(db->expires,key->ptr) == DICT_OK; |
e2641e09 | 496 | } |
497 | ||
0cf5b7b5 | 498 | void setExpire(redisDb *db, robj *key, time_t when) { |
e2641e09 | 499 | dictEntry *de; |
500 | ||
501 | /* Reuse the sds from the main dict in the expire dict */ | |
0cf5b7b5 | 502 | de = dictFind(db->dict,key->ptr); |
503 | redisAssert(de != NULL); | |
504 | dictReplace(db->expires,dictGetEntryKey(de),(void*)when); | |
e2641e09 | 505 | } |
506 | ||
507 | /* Return the expire time of the specified key, or -1 if no expire | |
508 | * is associated with this key (i.e. the key is non volatile) */ | |
509 | time_t getExpire(redisDb *db, robj *key) { | |
510 | dictEntry *de; | |
511 | ||
512 | /* No expire? return ASAP */ | |
513 | if (dictSize(db->expires) == 0 || | |
514 | (de = dictFind(db->expires,key->ptr)) == NULL) return -1; | |
515 | ||
516 | /* The entry was found in the expire dict, this means it should also | |
517 | * be present in the main dict (safety check). */ | |
518 | redisAssert(dictFind(db->dict,key->ptr) != NULL); | |
519 | return (time_t) dictGetEntryVal(de); | |
520 | } | |
521 | ||
bcf2995c | 522 | /* Propagate expires into slaves and the AOF file. |
523 | * When a key expires in the master, a DEL operation for this key is sent | |
524 | * to all the slaves and the AOF file if enabled. | |
525 | * | |
526 | * This way the key expiry is centralized in one place, and since both | |
527 | * AOF and the master->slave link guarantee operation ordering, everything | |
528 | * will be consistent even if we allow write operations against expiring | |
529 | * keys. */ | |
530 | void propagateExpire(redisDb *db, robj *key) { | |
bcf2995c | 531 | robj *argv[2]; |
532 | ||
bcf2995c | 533 | argv[0] = createStringObject("DEL",3); |
534 | argv[1] = key; | |
535 | incrRefCount(key); | |
536 | ||
537 | if (server.appendonly) | |
1b1f47c9 | 538 | feedAppendOnlyFile(server.delCommand,db->id,argv,2); |
bcf2995c | 539 | if (listLength(server.slaves)) |
540 | replicationFeedSlaves(server.slaves,db->id,argv,2); | |
541 | ||
c25a5d3b | 542 | decrRefCount(argv[0]); |
543 | decrRefCount(argv[1]); | |
bcf2995c | 544 | } |
545 | ||
e2641e09 | 546 | int expireIfNeeded(redisDb *db, robj *key) { |
547 | time_t when = getExpire(db,key); | |
bcf2995c | 548 | |
3a73be75 | 549 | if (when < 0) return 0; /* No expire for this key */ |
550 | ||
bcf2995c | 551 | /* If we are running in the context of a slave, return ASAP: |
552 | * the slave key expiration is controlled by the master that will | |
553 | * send us synthesized DEL operations for expired keys. | |
554 | * | |
555 | * Still we try to return the right information to the caller, | |
556 | * that is, 0 if we think the key should be still valid, 1 if | |
557 | * we think the key is expired at this time. */ | |
558 | if (server.masterhost != NULL) { | |
559 | return time(NULL) > when; | |
560 | } | |
561 | ||
e2641e09 | 562 | /* Return when this key has not expired */ |
563 | if (time(NULL) <= when) return 0; | |
564 | ||
565 | /* Delete the key */ | |
566 | server.stat_expiredkeys++; | |
bcf2995c | 567 | propagateExpire(db,key); |
e2641e09 | 568 | return dbDelete(db,key); |
569 | } | |
570 | ||
571 | /*----------------------------------------------------------------------------- | |
572 | * Expires Commands | |
573 | *----------------------------------------------------------------------------*/ | |
574 | ||
575 | void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) { | |
576 | dictEntry *de; | |
144a5e72 | 577 | long seconds; |
e2641e09 | 578 | |
579 | if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return; | |
580 | ||
581 | seconds -= offset; | |
582 | ||
583 | de = dictFind(c->db->dict,key->ptr); | |
584 | if (de == NULL) { | |
585 | addReply(c,shared.czero); | |
586 | return; | |
587 | } | |
588 | if (seconds <= 0) { | |
589 | if (dbDelete(c->db,key)) server.dirty++; | |
590 | addReply(c, shared.cone); | |
cea8c5cd | 591 | signalModifiedKey(c->db,key); |
e2641e09 | 592 | return; |
593 | } else { | |
594 | time_t when = time(NULL)+seconds; | |
0cf5b7b5 | 595 | setExpire(c->db,key,when); |
596 | addReply(c,shared.cone); | |
cea8c5cd | 597 | signalModifiedKey(c->db,key); |
0cf5b7b5 | 598 | server.dirty++; |
e2641e09 | 599 | return; |
600 | } | |
601 | } | |
602 | ||
603 | void expireCommand(redisClient *c) { | |
604 | expireGenericCommand(c,c->argv[1],c->argv[2],0); | |
605 | } | |
606 | ||
607 | void expireatCommand(redisClient *c) { | |
608 | expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL)); | |
609 | } | |
610 | ||
611 | void ttlCommand(redisClient *c) { | |
c91abdcd | 612 | time_t expire, ttl = -1; |
e2641e09 | 613 | |
15db4aa0 | 614 | if (server.ds_enabled) lookupKeyRead(c->db,c->argv[1]); |
e2641e09 | 615 | expire = getExpire(c->db,c->argv[1]); |
616 | if (expire != -1) { | |
c91abdcd | 617 | ttl = (expire-time(NULL)); |
e2641e09 | 618 | if (ttl < 0) ttl = -1; |
619 | } | |
c91abdcd | 620 | addReplyLongLong(c,(long long)ttl); |
e2641e09 | 621 | } |
a539d29a | 622 | |
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 | ||
728 | robj *GetKeyInSlot(unsigned int hashslot) { | |
729 | zskiplistNode *n; | |
730 | zrangespec range; | |
731 | ||
732 | range.min = range.max = hashslot; | |
733 | range.minex = range.maxex = 0; | |
734 | ||
735 | n = zslFirstInRange(server.cluster.slots_to_keys, range); | |
736 | if (!n) return NULL; | |
737 | return n->obj; | |
738 | } |