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