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