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