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