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