]> git.saurik.com Git - redis.git/blame - src/db.c
new parsing code bugfixing
[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
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) {
bcf2995c 48 expireIfNeeded(db,key);
e2641e09 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) {
7f00cd22 126 /* If VM is enabled make sure to awake waiting clients for this key:
127 * deleting the key will kill the I/O thread bringing the key from swap
128 * to memory, so the client will never be notified and unblocked if we
129 * don't do it now. */
da14590b 130 if (server.vm_enabled) handleClientsBlockedOnSwappedKey(db,key);
e2641e09 131 /* Deleting an entry from the expires dict will not free the sds of
132 * the key, because it is shared with the main dictionary. */
133 if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
134 return dictDelete(db->dict,key->ptr) == DICT_OK;
135}
136
137/* Empty the whole database */
138long long emptyDb() {
139 int j;
140 long long removed = 0;
141
142 for (j = 0; j < server.dbnum; j++) {
143 removed += dictSize(server.db[j].dict);
144 dictEmpty(server.db[j].dict);
145 dictEmpty(server.db[j].expires);
146 }
147 return removed;
148}
149
150int selectDb(redisClient *c, int id) {
151 if (id < 0 || id >= server.dbnum)
152 return REDIS_ERR;
153 c->db = &server.db[id];
154 return REDIS_OK;
155}
156
157/*-----------------------------------------------------------------------------
158 * Type agnostic commands operating on the key space
159 *----------------------------------------------------------------------------*/
160
161void flushdbCommand(redisClient *c) {
162 server.dirty += dictSize(c->db->dict);
163 touchWatchedKeysOnFlush(c->db->id);
164 dictEmpty(c->db->dict);
165 dictEmpty(c->db->expires);
166 addReply(c,shared.ok);
167}
168
169void flushallCommand(redisClient *c) {
170 touchWatchedKeysOnFlush(-1);
171 server.dirty += emptyDb();
172 addReply(c,shared.ok);
173 if (server.bgsavechildpid != -1) {
174 kill(server.bgsavechildpid,SIGKILL);
175 rdbRemoveTempFile(server.bgsavechildpid);
176 }
177 rdbSave(server.dbfilename);
178 server.dirty++;
179}
180
181void delCommand(redisClient *c) {
182 int deleted = 0, j;
183
184 for (j = 1; j < c->argc; j++) {
185 if (dbDelete(c->db,c->argv[j])) {
186 touchWatchedKey(c->db,c->argv[j]);
187 server.dirty++;
188 deleted++;
189 }
190 }
191 addReplyLongLong(c,deleted);
192}
193
194void existsCommand(redisClient *c) {
195 expireIfNeeded(c->db,c->argv[1]);
196 if (dbExists(c->db,c->argv[1])) {
197 addReply(c, shared.cone);
198 } else {
199 addReply(c, shared.czero);
200 }
201}
202
203void selectCommand(redisClient *c) {
204 int id = atoi(c->argv[1]->ptr);
205
206 if (selectDb(c,id) == REDIS_ERR) {
3ab20376 207 addReplyError(c,"invalid DB index");
e2641e09 208 } else {
209 addReply(c,shared.ok);
210 }
211}
212
213void randomkeyCommand(redisClient *c) {
214 robj *key;
215
216 if ((key = dbRandomKey(c->db)) == NULL) {
217 addReply(c,shared.nullbulk);
218 return;
219 }
220
221 addReplyBulk(c,key);
222 decrRefCount(key);
223}
224
225void keysCommand(redisClient *c) {
226 dictIterator *di;
227 dictEntry *de;
228 sds pattern = c->argv[1]->ptr;
e0e1c195 229 int plen = sdslen(pattern), allkeys;
e2641e09 230 unsigned long numkeys = 0;
b301c1fc 231 void *replylen = addDeferredMultiBulkLength(c);
e2641e09 232
233 di = dictGetIterator(c->db->dict);
e0e1c195 234 allkeys = (pattern[0] == '*' && pattern[1] == '\0');
e2641e09 235 while((de = dictNext(di)) != NULL) {
236 sds key = dictGetEntryKey(de);
237 robj *keyobj;
238
e0e1c195 239 if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {
e2641e09 240 keyobj = createStringObject(key,sdslen(key));
241 if (expireIfNeeded(c->db,keyobj) == 0) {
242 addReplyBulk(c,keyobj);
243 numkeys++;
244 }
245 decrRefCount(keyobj);
246 }
247 }
248 dictReleaseIterator(di);
b301c1fc 249 setDeferredMultiBulkLength(c,replylen,numkeys);
e2641e09 250}
251
252void dbsizeCommand(redisClient *c) {
b70d3555 253 addReplyLongLong(c,dictSize(c->db->dict));
e2641e09 254}
255
256void lastsaveCommand(redisClient *c) {
b70d3555 257 addReplyLongLong(c,server.lastsave);
e2641e09 258}
259
260void typeCommand(redisClient *c) {
261 robj *o;
262 char *type;
263
264 o = lookupKeyRead(c->db,c->argv[1]);
265 if (o == NULL) {
3ab20376 266 type = "none";
e2641e09 267 } else {
268 switch(o->type) {
3ab20376
PN
269 case REDIS_STRING: type = "string"; break;
270 case REDIS_LIST: type = "list"; break;
271 case REDIS_SET: type = "set"; break;
272 case REDIS_ZSET: type = "zset"; break;
273 case REDIS_HASH: type = "hash"; break;
274 default: type = "unknown"; break;
e2641e09 275 }
276 }
3ab20376 277 addReplyStatus(c,type);
e2641e09 278}
279
280void saveCommand(redisClient *c) {
281 if (server.bgsavechildpid != -1) {
3ab20376 282 addReplyError(c,"Background save already in progress");
e2641e09 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) {
3ab20376 294 addReplyError(c,"Background save already in progress");
e2641e09 295 return;
296 }
297 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
3ab20376 298 addReplyStatus(c,"Background saving started");
e2641e09 299 } else {
300 addReply(c,shared.err);
301 }
302}
303
304void shutdownCommand(redisClient *c) {
305 if (prepareForShutdown() == REDIS_OK)
306 exit(0);
3ab20376 307 addReplyError(c,"Errors trying to SHUTDOWN. Check logs.");
e2641e09 308}
309
310void renameGenericCommand(redisClient *c, int nx) {
311 robj *o;
312
313 /* To use the same key as src and dst is probably an error */
314 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
315 addReply(c,shared.sameobjecterr);
316 return;
317 }
318
319 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
320 return;
321
322 incrRefCount(o);
e2641e09 323 if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) {
324 if (nx) {
325 decrRefCount(o);
326 addReply(c,shared.czero);
327 return;
328 }
329 dbReplace(c->db,c->argv[2],o);
330 }
331 dbDelete(c->db,c->argv[1]);
5b4bff9c 332 touchWatchedKey(c->db,c->argv[1]);
e2641e09 333 touchWatchedKey(c->db,c->argv[2]);
334 server.dirty++;
335 addReply(c,nx ? shared.cone : shared.ok);
336}
337
338void renameCommand(redisClient *c) {
339 renameGenericCommand(c,0);
340}
341
342void renamenxCommand(redisClient *c) {
343 renameGenericCommand(c,1);
344}
345
346void moveCommand(redisClient *c) {
347 robj *o;
348 redisDb *src, *dst;
349 int srcid;
350
351 /* Obtain source and target DB pointers */
352 src = c->db;
353 srcid = c->db->id;
354 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
355 addReply(c,shared.outofrangeerr);
356 return;
357 }
358 dst = c->db;
359 selectDb(c,srcid); /* Back to the source DB */
360
361 /* If the user is moving using as target the same
362 * DB as the source DB it is probably an error. */
363 if (src == dst) {
364 addReply(c,shared.sameobjecterr);
365 return;
366 }
367
368 /* Check if the element exists and get a reference */
369 o = lookupKeyWrite(c->db,c->argv[1]);
370 if (!o) {
371 addReply(c,shared.czero);
372 return;
373 }
374
375 /* Try to add the element to the target DB */
e2641e09 376 if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) {
377 addReply(c,shared.czero);
378 return;
379 }
380 incrRefCount(o);
381
382 /* OK! key moved, free the entry in the source DB */
383 dbDelete(src,c->argv[1]);
384 server.dirty++;
385 addReply(c,shared.cone);
386}
387
388/*-----------------------------------------------------------------------------
389 * Expires API
390 *----------------------------------------------------------------------------*/
391
392int removeExpire(redisDb *db, robj *key) {
393 /* An expire may only be removed if there is a corresponding entry in the
394 * main dict. Otherwise, the key will never be freed. */
395 redisAssert(dictFind(db->dict,key->ptr) != NULL);
a539d29a 396 return dictDelete(db->expires,key->ptr) == DICT_OK;
e2641e09 397}
398
0cf5b7b5 399void setExpire(redisDb *db, robj *key, time_t when) {
e2641e09 400 dictEntry *de;
401
402 /* Reuse the sds from the main dict in the expire dict */
0cf5b7b5 403 de = dictFind(db->dict,key->ptr);
404 redisAssert(de != NULL);
405 dictReplace(db->expires,dictGetEntryKey(de),(void*)when);
e2641e09 406}
407
408/* Return the expire time of the specified key, or -1 if no expire
409 * is associated with this key (i.e. the key is non volatile) */
410time_t getExpire(redisDb *db, robj *key) {
411 dictEntry *de;
412
413 /* No expire? return ASAP */
414 if (dictSize(db->expires) == 0 ||
415 (de = dictFind(db->expires,key->ptr)) == NULL) return -1;
416
417 /* The entry was found in the expire dict, this means it should also
418 * be present in the main dict (safety check). */
419 redisAssert(dictFind(db->dict,key->ptr) != NULL);
420 return (time_t) dictGetEntryVal(de);
421}
422
bcf2995c 423/* Propagate expires into slaves and the AOF file.
424 * When a key expires in the master, a DEL operation for this key is sent
425 * to all the slaves and the AOF file if enabled.
426 *
427 * This way the key expiry is centralized in one place, and since both
428 * AOF and the master->slave link guarantee operation ordering, everything
429 * will be consistent even if we allow write operations against expiring
430 * keys. */
431void propagateExpire(redisDb *db, robj *key) {
432 struct redisCommand *cmd;
433 robj *argv[2];
434
435 cmd = lookupCommand("del");
436 argv[0] = createStringObject("DEL",3);
437 argv[1] = key;
438 incrRefCount(key);
439
440 if (server.appendonly)
441 feedAppendOnlyFile(cmd,db->id,argv,2);
442 if (listLength(server.slaves))
443 replicationFeedSlaves(server.slaves,db->id,argv,2);
444
c25a5d3b 445 decrRefCount(argv[0]);
446 decrRefCount(argv[1]);
bcf2995c 447}
448
e2641e09 449int expireIfNeeded(redisDb *db, robj *key) {
450 time_t when = getExpire(db,key);
bcf2995c 451
452 /* If we are running in the context of a slave, return ASAP:
453 * the slave key expiration is controlled by the master that will
454 * send us synthesized DEL operations for expired keys.
455 *
456 * Still we try to return the right information to the caller,
457 * that is, 0 if we think the key should be still valid, 1 if
458 * we think the key is expired at this time. */
459 if (server.masterhost != NULL) {
460 return time(NULL) > when;
461 }
462
e2641e09 463 if (when < 0) return 0;
464
465 /* Return when this key has not expired */
466 if (time(NULL) <= when) return 0;
467
468 /* Delete the key */
469 server.stat_expiredkeys++;
470 server.dirty++;
bcf2995c 471 propagateExpire(db,key);
e2641e09 472 return dbDelete(db,key);
473}
474
475/*-----------------------------------------------------------------------------
476 * Expires Commands
477 *----------------------------------------------------------------------------*/
478
479void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
480 dictEntry *de;
481 time_t seconds;
482
483 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
484
485 seconds -= offset;
486
487 de = dictFind(c->db->dict,key->ptr);
488 if (de == NULL) {
489 addReply(c,shared.czero);
490 return;
491 }
492 if (seconds <= 0) {
493 if (dbDelete(c->db,key)) server.dirty++;
494 addReply(c, shared.cone);
b7a8daef 495 touchWatchedKey(c->db,key);
e2641e09 496 return;
497 } else {
498 time_t when = time(NULL)+seconds;
0cf5b7b5 499 setExpire(c->db,key,when);
500 addReply(c,shared.cone);
501 touchWatchedKey(c->db,key);
502 server.dirty++;
e2641e09 503 return;
504 }
505}
506
507void expireCommand(redisClient *c) {
508 expireGenericCommand(c,c->argv[1],c->argv[2],0);
509}
510
511void expireatCommand(redisClient *c) {
512 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
513}
514
515void ttlCommand(redisClient *c) {
c91abdcd 516 time_t expire, ttl = -1;
e2641e09 517
518 expire = getExpire(c->db,c->argv[1]);
519 if (expire != -1) {
c91abdcd 520 ttl = (expire-time(NULL));
e2641e09 521 if (ttl < 0) ttl = -1;
522 }
c91abdcd 523 addReplyLongLong(c,(long long)ttl);
e2641e09 524}
a539d29a 525
526void persistCommand(redisClient *c) {
527 dictEntry *de;
528
529 de = dictFind(c->db->dict,c->argv[1]->ptr);
530 if (de == NULL) {
531 addReply(c,shared.czero);
532 } else {
1fb4e8de 533 if (removeExpire(c->db,c->argv[1])) {
a539d29a 534 addReply(c,shared.cone);
1fb4e8de 535 server.dirty++;
536 } else {
a539d29a 537 addReply(c,shared.czero);
1fb4e8de 538 }
a539d29a 539 }
540}