]> git.saurik.com Git - redis.git/blame_incremental - src/db.c
support for write operations against expiring keys, by master-controlled expiring...
[redis.git] / src / db.c
... / ...
CommitLineData
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
14 if (server.vm_enabled) {
15 if (val->storage == REDIS_VM_MEMORY ||
16 val->storage == REDIS_VM_SWAPPING)
17 {
18 /* If we were swapping the object out, cancel the operation */
19 if (val->storage == REDIS_VM_SWAPPING)
20 vmCancelThreadedIOJob(val);
21 /* Update the access time for the aging algorithm. */
22 val->lru = server.lruclock;
23 } else {
24 int notify = (val->storage == REDIS_VM_LOADING);
25
26 /* Our value was swapped on disk. Bring it at home. */
27 redisAssert(val->type == REDIS_VMPOINTER);
28 val = vmLoadObject(val);
29 dictGetEntryVal(de) = val;
30
31 /* Clients blocked by the VM subsystem may be waiting for
32 * this key... */
33 if (notify) handleClientsBlockedOnSwappedKey(db,key);
34 }
35 }
36 return val;
37 } else {
38 return NULL;
39 }
40}
41
42robj *lookupKeyRead(redisDb *db, robj *key) {
43 expireIfNeeded(db,key);
44 return lookupKey(db,key);
45}
46
47robj *lookupKeyWrite(redisDb *db, robj *key) {
48 expireIfNeeded(db,key);
49 return lookupKey(db,key);
50}
51
52robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
53 robj *o = lookupKeyRead(c->db, key);
54 if (!o) addReply(c,reply);
55 return o;
56}
57
58robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
59 robj *o = lookupKeyWrite(c->db, key);
60 if (!o) addReply(c,reply);
61 return o;
62}
63
64/* Add the key to the DB. If the key already exists REDIS_ERR is returned,
65 * otherwise REDIS_OK is returned, and the caller should increment the
66 * refcount of 'val'. */
67int dbAdd(redisDb *db, robj *key, robj *val) {
68 /* Perform a lookup before adding the key, as we need to copy the
69 * key value. */
70 if (dictFind(db->dict, key->ptr) != NULL) {
71 return REDIS_ERR;
72 } else {
73 sds copy = sdsdup(key->ptr);
74 dictAdd(db->dict, copy, val);
75 return REDIS_OK;
76 }
77}
78
79/* If the key does not exist, this is just like dbAdd(). Otherwise
80 * the value associated to the key is replaced with the new one.
81 *
82 * On update (key already existed) 0 is returned. Otherwise 1. */
83int dbReplace(redisDb *db, robj *key, robj *val) {
84 if (dictFind(db->dict,key->ptr) == NULL) {
85 sds copy = sdsdup(key->ptr);
86 dictAdd(db->dict, copy, val);
87 return 1;
88 } else {
89 dictReplace(db->dict, key->ptr, val);
90 return 0;
91 }
92}
93
94int dbExists(redisDb *db, robj *key) {
95 return dictFind(db->dict,key->ptr) != NULL;
96}
97
98/* Return a random key, in form of a Redis object.
99 * If there are no keys, NULL is returned.
100 *
101 * The function makes sure to return keys not already expired. */
102robj *dbRandomKey(redisDb *db) {
103 struct dictEntry *de;
104
105 while(1) {
106 sds key;
107 robj *keyobj;
108
109 de = dictGetRandomKey(db->dict);
110 if (de == NULL) return NULL;
111
112 key = dictGetEntryKey(de);
113 keyobj = createStringObject(key,sdslen(key));
114 if (dictFind(db->expires,key)) {
115 if (expireIfNeeded(db,keyobj)) {
116 decrRefCount(keyobj);
117 continue; /* search for another key. This expired. */
118 }
119 }
120 return keyobj;
121 }
122}
123
124/* Delete a key, value, and associated expiration entry if any, from the DB */
125int dbDelete(redisDb *db, robj *key) {
126 /* Deleting an entry from the expires dict will not free the sds of
127 * the key, because it is shared with the main dictionary. */
128 if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
129 return dictDelete(db->dict,key->ptr) == DICT_OK;
130}
131
132/* Empty the whole database */
133long long emptyDb() {
134 int j;
135 long long removed = 0;
136
137 for (j = 0; j < server.dbnum; j++) {
138 removed += dictSize(server.db[j].dict);
139 dictEmpty(server.db[j].dict);
140 dictEmpty(server.db[j].expires);
141 }
142 return removed;
143}
144
145int selectDb(redisClient *c, int id) {
146 if (id < 0 || id >= server.dbnum)
147 return REDIS_ERR;
148 c->db = &server.db[id];
149 return REDIS_OK;
150}
151
152/*-----------------------------------------------------------------------------
153 * Type agnostic commands operating on the key space
154 *----------------------------------------------------------------------------*/
155
156void flushdbCommand(redisClient *c) {
157 server.dirty += dictSize(c->db->dict);
158 touchWatchedKeysOnFlush(c->db->id);
159 dictEmpty(c->db->dict);
160 dictEmpty(c->db->expires);
161 addReply(c,shared.ok);
162}
163
164void flushallCommand(redisClient *c) {
165 touchWatchedKeysOnFlush(-1);
166 server.dirty += emptyDb();
167 addReply(c,shared.ok);
168 if (server.bgsavechildpid != -1) {
169 kill(server.bgsavechildpid,SIGKILL);
170 rdbRemoveTempFile(server.bgsavechildpid);
171 }
172 rdbSave(server.dbfilename);
173 server.dirty++;
174}
175
176void delCommand(redisClient *c) {
177 int deleted = 0, j;
178
179 for (j = 1; j < c->argc; j++) {
180 if (dbDelete(c->db,c->argv[j])) {
181 touchWatchedKey(c->db,c->argv[j]);
182 server.dirty++;
183 deleted++;
184 }
185 }
186 addReplyLongLong(c,deleted);
187}
188
189void existsCommand(redisClient *c) {
190 expireIfNeeded(c->db,c->argv[1]);
191 if (dbExists(c->db,c->argv[1])) {
192 addReply(c, shared.cone);
193 } else {
194 addReply(c, shared.czero);
195 }
196}
197
198void selectCommand(redisClient *c) {
199 int id = atoi(c->argv[1]->ptr);
200
201 if (selectDb(c,id) == REDIS_ERR) {
202 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
203 } else {
204 addReply(c,shared.ok);
205 }
206}
207
208void randomkeyCommand(redisClient *c) {
209 robj *key;
210
211 if ((key = dbRandomKey(c->db)) == NULL) {
212 addReply(c,shared.nullbulk);
213 return;
214 }
215
216 addReplyBulk(c,key);
217 decrRefCount(key);
218}
219
220void keysCommand(redisClient *c) {
221 dictIterator *di;
222 dictEntry *de;
223 sds pattern = c->argv[1]->ptr;
224 int plen = sdslen(pattern);
225 unsigned long numkeys = 0;
226 robj *lenobj = createObject(REDIS_STRING,NULL);
227
228 di = dictGetIterator(c->db->dict);
229 addReply(c,lenobj);
230 decrRefCount(lenobj);
231 while((de = dictNext(di)) != NULL) {
232 sds key = dictGetEntryKey(de);
233 robj *keyobj;
234
235 if ((pattern[0] == '*' && pattern[1] == '\0') ||
236 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
237 keyobj = createStringObject(key,sdslen(key));
238 if (expireIfNeeded(c->db,keyobj) == 0) {
239 addReplyBulk(c,keyobj);
240 numkeys++;
241 }
242 decrRefCount(keyobj);
243 }
244 }
245 dictReleaseIterator(di);
246 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
247}
248
249void dbsizeCommand(redisClient *c) {
250 addReplySds(c,
251 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
252}
253
254void lastsaveCommand(redisClient *c) {
255 addReplySds(c,
256 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
257}
258
259void typeCommand(redisClient *c) {
260 robj *o;
261 char *type;
262
263 o = lookupKeyRead(c->db,c->argv[1]);
264 if (o == NULL) {
265 type = "+none";
266 } else {
267 switch(o->type) {
268 case REDIS_STRING: type = "+string"; break;
269 case REDIS_LIST: type = "+list"; break;
270 case REDIS_SET: type = "+set"; break;
271 case REDIS_ZSET: type = "+zset"; break;
272 case REDIS_HASH: type = "+hash"; break;
273 default: type = "+unknown"; break;
274 }
275 }
276 addReplySds(c,sdsnew(type));
277 addReply(c,shared.crlf);
278}
279
280void saveCommand(redisClient *c) {
281 if (server.bgsavechildpid != -1) {
282 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
283 return;
284 }
285 if (rdbSave(server.dbfilename) == REDIS_OK) {
286 addReply(c,shared.ok);
287 } else {
288 addReply(c,shared.err);
289 }
290}
291
292void bgsaveCommand(redisClient *c) {
293 if (server.bgsavechildpid != -1) {
294 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
295 return;
296 }
297 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
298 char *status = "+Background saving started\r\n";
299 addReplySds(c,sdsnew(status));
300 } else {
301 addReply(c,shared.err);
302 }
303}
304
305void shutdownCommand(redisClient *c) {
306 if (prepareForShutdown() == REDIS_OK)
307 exit(0);
308 addReplySds(c, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
309}
310
311void renameGenericCommand(redisClient *c, int nx) {
312 robj *o;
313
314 /* To use the same key as src and dst is probably an error */
315 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
316 addReply(c,shared.sameobjecterr);
317 return;
318 }
319
320 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
321 return;
322
323 incrRefCount(o);
324 if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) {
325 if (nx) {
326 decrRefCount(o);
327 addReply(c,shared.czero);
328 return;
329 }
330 dbReplace(c->db,c->argv[2],o);
331 }
332 dbDelete(c->db,c->argv[1]);
333 touchWatchedKey(c->db,c->argv[1]);
334 touchWatchedKey(c->db,c->argv[2]);
335 server.dirty++;
336 addReply(c,nx ? shared.cone : shared.ok);
337}
338
339void renameCommand(redisClient *c) {
340 renameGenericCommand(c,0);
341}
342
343void renamenxCommand(redisClient *c) {
344 renameGenericCommand(c,1);
345}
346
347void moveCommand(redisClient *c) {
348 robj *o;
349 redisDb *src, *dst;
350 int srcid;
351
352 /* Obtain source and target DB pointers */
353 src = c->db;
354 srcid = c->db->id;
355 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
356 addReply(c,shared.outofrangeerr);
357 return;
358 }
359 dst = c->db;
360 selectDb(c,srcid); /* Back to the source DB */
361
362 /* If the user is moving using as target the same
363 * DB as the source DB it is probably an error. */
364 if (src == dst) {
365 addReply(c,shared.sameobjecterr);
366 return;
367 }
368
369 /* Check if the element exists and get a reference */
370 o = lookupKeyWrite(c->db,c->argv[1]);
371 if (!o) {
372 addReply(c,shared.czero);
373 return;
374 }
375
376 /* Try to add the element to the target DB */
377 if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) {
378 addReply(c,shared.czero);
379 return;
380 }
381 incrRefCount(o);
382
383 /* OK! key moved, free the entry in the source DB */
384 dbDelete(src,c->argv[1]);
385 server.dirty++;
386 addReply(c,shared.cone);
387}
388
389/*-----------------------------------------------------------------------------
390 * Expires API
391 *----------------------------------------------------------------------------*/
392
393int removeExpire(redisDb *db, robj *key) {
394 /* An expire may only be removed if there is a corresponding entry in the
395 * main dict. Otherwise, the key will never be freed. */
396 redisAssert(dictFind(db->dict,key->ptr) != NULL);
397 if (dictDelete(db->expires,key->ptr) == DICT_OK) {
398 return 1;
399 } else {
400 return 0;
401 }
402}
403
404int setExpire(redisDb *db, robj *key, time_t when) {
405 dictEntry *de;
406
407 /* Reuse the sds from the main dict in the expire dict */
408 redisAssert((de = dictFind(db->dict,key->ptr)) != NULL);
409 if (dictAdd(db->expires,dictGetEntryKey(de),(void*)when) == DICT_ERR) {
410 return 0;
411 } else {
412 return 1;
413 }
414}
415
416/* Return the expire time of the specified key, or -1 if no expire
417 * is associated with this key (i.e. the key is non volatile) */
418time_t getExpire(redisDb *db, robj *key) {
419 dictEntry *de;
420
421 /* No expire? return ASAP */
422 if (dictSize(db->expires) == 0 ||
423 (de = dictFind(db->expires,key->ptr)) == NULL) return -1;
424
425 /* The entry was found in the expire dict, this means it should also
426 * be present in the main dict (safety check). */
427 redisAssert(dictFind(db->dict,key->ptr) != NULL);
428 return (time_t) dictGetEntryVal(de);
429}
430
431/* Propagate expires into slaves and the AOF file.
432 * When a key expires in the master, a DEL operation for this key is sent
433 * to all the slaves and the AOF file if enabled.
434 *
435 * This way the key expiry is centralized in one place, and since both
436 * AOF and the master->slave link guarantee operation ordering, everything
437 * will be consistent even if we allow write operations against expiring
438 * keys. */
439void propagateExpire(redisDb *db, robj *key) {
440 struct redisCommand *cmd;
441 robj *argv[2];
442
443 cmd = lookupCommand("del");
444 argv[0] = createStringObject("DEL",3);
445 argv[1] = key;
446 incrRefCount(key);
447
448 if (server.appendonly)
449 feedAppendOnlyFile(cmd,db->id,argv,2);
450 if (listLength(server.slaves))
451 replicationFeedSlaves(server.slaves,db->id,argv,2);
452
453 decrRefCount(key);
454}
455
456int expireIfNeeded(redisDb *db, robj *key) {
457 time_t when = getExpire(db,key);
458
459 /* If we are running in the context of a slave, return ASAP:
460 * the slave key expiration is controlled by the master that will
461 * send us synthesized DEL operations for expired keys.
462 *
463 * Still we try to return the right information to the caller,
464 * that is, 0 if we think the key should be still valid, 1 if
465 * we think the key is expired at this time. */
466 if (server.masterhost != NULL) {
467 return time(NULL) > when;
468 }
469
470 if (when < 0) return 0;
471
472 /* Return when this key has not expired */
473 if (time(NULL) <= when) return 0;
474
475 /* Delete the key */
476 server.stat_expiredkeys++;
477 server.dirty++;
478 propagateExpire(db,key);
479 return dbDelete(db,key);
480}
481
482/*-----------------------------------------------------------------------------
483 * Expires Commands
484 *----------------------------------------------------------------------------*/
485
486void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
487 dictEntry *de;
488 time_t seconds;
489
490 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
491
492 seconds -= offset;
493
494 de = dictFind(c->db->dict,key->ptr);
495 if (de == NULL) {
496 addReply(c,shared.czero);
497 return;
498 }
499 if (seconds <= 0) {
500 if (dbDelete(c->db,key)) server.dirty++;
501 addReply(c, shared.cone);
502 touchWatchedKey(c->db,key);
503 return;
504 } else {
505 time_t when = time(NULL)+seconds;
506 if (setExpire(c->db,key,when)) {
507 addReply(c,shared.cone);
508 touchWatchedKey(c->db,key);
509 server.dirty++;
510 } else {
511 addReply(c,shared.czero);
512 }
513 return;
514 }
515}
516
517void expireCommand(redisClient *c) {
518 expireGenericCommand(c,c->argv[1],c->argv[2],0);
519}
520
521void expireatCommand(redisClient *c) {
522 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
523}
524
525void ttlCommand(redisClient *c) {
526 time_t expire;
527 int ttl = -1;
528
529 expire = getExpire(c->db,c->argv[1]);
530 if (expire != -1) {
531 ttl = (int) (expire-time(NULL));
532 if (ttl < 0) ttl = -1;
533 }
534 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
535}