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