]>
git.saurik.com Git - redis.git/blob - src/db.c
5 /*-----------------------------------------------------------------------------
7 *----------------------------------------------------------------------------*/
9 robj
*lookupKey(redisDb
*db
, robj
*key
) {
10 dictEntry
*de
= dictFind(db
->dict
,key
->ptr
);
12 robj
*val
= dictGetEntryVal(de
);
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
;
20 if (server
.ds_enabled
&&
21 cacheScheduleIOGetFlags(db
,key
) & REDIS_IO_SAVEINPROG
)
23 /* There is a save in progress for this object!
24 * Wait for it to get out. */
25 waitEmptyIOJobsQueue();
26 processAllPendingIOJobs();
27 redisAssert(!(cacheScheduleIOGetFlags(db
,key
) & REDIS_IO_SAVEINPROG
));
29 server
.stat_keyspace_hits
++;
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
37 * in a blocking way. */
38 if (server
.ds_enabled
&& cacheKeyMayExist(db
,key
)) {
39 if (cacheScheduleIOGetFlags(db
,key
) &
40 (REDIS_IO_SAVE
|REDIS_IO_SAVEINPROG
))
42 /* There is a save in progress for this object!
43 * Wait for it to get out. */
44 waitEmptyIOJobsQueue();
45 processAllPendingIOJobs();
46 redisAssert((cacheScheduleIOGetFlags(db
,key
) & (REDIS_IO_SAVE
|REDIS_IO_SAVEINPROG
)) == 0);
49 redisLog(REDIS_DEBUG
,"Force loading key %s via lookup",
51 val
= dsGet(db
,key
,&expire
);
53 int retval
= dbAdd(db
,key
,val
);
54 redisAssert(retval
== REDIS_OK
);
55 if (expire
!= -1) setExpire(db
,key
,expire
);
56 server
.stat_keyspace_hits
++;
60 server
.stat_keyspace_misses
++;
65 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
66 expireIfNeeded(db
,key
);
67 return lookupKey(db
,key
);
70 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
71 expireIfNeeded(db
,key
);
72 return lookupKey(db
,key
);
75 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
76 robj
*o
= lookupKeyRead(c
->db
, key
);
77 if (!o
) addReply(c
,reply
);
81 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
82 robj
*o
= lookupKeyWrite(c
->db
, key
);
83 if (!o
) addReply(c
,reply
);
87 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
88 * otherwise REDIS_OK is returned, and the caller should increment the
89 * refcount of 'val'. */
90 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
91 /* Perform a lookup before adding the key, as we need to copy the
93 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
96 sds copy
= sdsdup(key
->ptr
);
97 dictAdd(db
->dict
, copy
, val
);
98 if (server
.ds_enabled
) {
99 /* FIXME: remove entry from negative cache */
105 /* If the key does not exist, this is just like dbAdd(). Otherwise
106 * the value associated to the key is replaced with the new one.
108 * On update (key already existed) 0 is returned. Otherwise 1. */
109 int dbReplace(redisDb
*db
, robj
*key
, robj
*val
) {
112 if ((oldval
= dictFetchValue(db
->dict
,key
->ptr
)) == NULL
) {
113 sds copy
= sdsdup(key
->ptr
);
114 dictAdd(db
->dict
, copy
, val
);
117 dictReplace(db
->dict
, key
->ptr
, val
);
122 int dbExists(redisDb
*db
, robj
*key
) {
123 return dictFind(db
->dict
,key
->ptr
) != NULL
;
126 /* Return a random key, in form of a Redis object.
127 * If there are no keys, NULL is returned.
129 * The function makes sure to return keys not already expired. */
130 robj
*dbRandomKey(redisDb
*db
) {
131 struct dictEntry
*de
;
137 de
= dictGetRandomKey(db
->dict
);
138 if (de
== NULL
) return NULL
;
140 key
= dictGetEntryKey(de
);
141 keyobj
= createStringObject(key
,sdslen(key
));
142 if (dictFind(db
->expires
,key
)) {
143 if (expireIfNeeded(db
,keyobj
)) {
144 decrRefCount(keyobj
);
145 continue; /* search for another key. This expired. */
152 /* Delete a key, value, and associated expiration entry if any, from the DB */
153 int dbDelete(redisDb
*db
, robj
*key
) {
154 /* If diskstore is enabled make sure to awake waiting clients for this key
155 * as it is not really useful to wait for a key already deleted to be
156 * loaded from disk. */
157 if (server
.ds_enabled
) handleClientsBlockedOnSwappedKey(db
,key
);
159 /* FIXME: we should mark this key as non existing on disk in the negative
162 /* Deleting an entry from the expires dict will not free the sds of
163 * the key, because it is shared with the main dictionary. */
164 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
165 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
168 /* Empty the whole database */
169 long long emptyDb() {
171 long long removed
= 0;
173 for (j
= 0; j
< server
.dbnum
; j
++) {
174 removed
+= dictSize(server
.db
[j
].dict
);
175 dictEmpty(server
.db
[j
].dict
);
176 dictEmpty(server
.db
[j
].expires
);
181 int selectDb(redisClient
*c
, int id
) {
182 if (id
< 0 || id
>= server
.dbnum
)
184 c
->db
= &server
.db
[id
];
188 /*-----------------------------------------------------------------------------
189 * Hooks for key space changes.
191 * Every time a key in the database is modified the function
192 * signalModifiedKey() is called.
194 * Every time a DB is flushed the function signalFlushDb() is called.
195 *----------------------------------------------------------------------------*/
197 void signalModifiedKey(redisDb
*db
, robj
*key
) {
198 touchWatchedKey(db
,key
);
199 if (server
.ds_enabled
)
200 cacheScheduleIO(db
,key
,REDIS_IO_SAVE
);
203 void signalFlushedDb(int dbid
) {
204 touchWatchedKeysOnFlush(dbid
);
205 if (server
.ds_enabled
)
209 /*-----------------------------------------------------------------------------
210 * Type agnostic commands operating on the key space
211 *----------------------------------------------------------------------------*/
213 void flushdbCommand(redisClient
*c
) {
214 server
.dirty
+= dictSize(c
->db
->dict
);
215 signalFlushedDb(c
->db
->id
);
216 dictEmpty(c
->db
->dict
);
217 dictEmpty(c
->db
->expires
);
218 addReply(c
,shared
.ok
);
221 void flushallCommand(redisClient
*c
) {
223 server
.dirty
+= emptyDb();
224 addReply(c
,shared
.ok
);
225 if (server
.bgsavechildpid
!= -1) {
226 kill(server
.bgsavechildpid
,SIGKILL
);
227 rdbRemoveTempFile(server
.bgsavechildpid
);
229 rdbSave(server
.dbfilename
);
233 void delCommand(redisClient
*c
) {
236 for (j
= 1; j
< c
->argc
; j
++) {
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. */
242 if (dbDelete(c
->db
,c
->argv
[j
])) {
243 signalModifiedKey(c
->db
,c
->argv
[j
]);
246 } else if (server
.ds_enabled
) {
247 if (cacheKeyMayExist(c
->db
,c
->argv
[j
]) &&
248 dsExists(c
->db
,c
->argv
[j
]))
250 cacheScheduleIO(c
->db
,c
->argv
[j
],REDIS_IO_SAVE
);
255 addReplyLongLong(c
,deleted
);
258 void existsCommand(redisClient
*c
) {
259 expireIfNeeded(c
->db
,c
->argv
[1]);
260 if (dbExists(c
->db
,c
->argv
[1])) {
261 addReply(c
, shared
.cone
);
263 addReply(c
, shared
.czero
);
267 void selectCommand(redisClient
*c
) {
268 int id
= atoi(c
->argv
[1]->ptr
);
270 if (selectDb(c
,id
) == REDIS_ERR
) {
271 addReplyError(c
,"invalid DB index");
273 addReply(c
,shared
.ok
);
277 void randomkeyCommand(redisClient
*c
) {
280 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
281 addReply(c
,shared
.nullbulk
);
289 void keysCommand(redisClient
*c
) {
292 sds pattern
= c
->argv
[1]->ptr
;
293 int plen
= sdslen(pattern
), allkeys
;
294 unsigned long numkeys
= 0;
295 void *replylen
= addDeferredMultiBulkLength(c
);
297 di
= dictGetIterator(c
->db
->dict
);
298 allkeys
= (pattern
[0] == '*' && pattern
[1] == '\0');
299 while((de
= dictNext(di
)) != NULL
) {
300 sds key
= dictGetEntryKey(de
);
303 if (allkeys
|| stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
304 keyobj
= createStringObject(key
,sdslen(key
));
305 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
306 addReplyBulk(c
,keyobj
);
309 decrRefCount(keyobj
);
312 dictReleaseIterator(di
);
313 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
316 void dbsizeCommand(redisClient
*c
) {
317 addReplyLongLong(c
,dictSize(c
->db
->dict
));
320 void lastsaveCommand(redisClient
*c
) {
321 addReplyLongLong(c
,server
.lastsave
);
324 void typeCommand(redisClient
*c
) {
328 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
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;
341 addReplyStatus(c
,type
);
344 void saveCommand(redisClient
*c
) {
345 if (server
.bgsavechildpid
!= -1) {
346 addReplyError(c
,"Background save already in progress");
349 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
350 addReply(c
,shared
.ok
);
352 addReply(c
,shared
.err
);
356 void bgsaveCommand(redisClient
*c
) {
357 if (server
.bgsavechildpid
!= -1) {
358 addReplyError(c
,"Background save already in progress");
361 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
362 addReplyStatus(c
,"Background saving started");
364 addReply(c
,shared
.err
);
368 void shutdownCommand(redisClient
*c
) {
369 if (prepareForShutdown() == REDIS_OK
)
371 addReplyError(c
,"Errors trying to SHUTDOWN. Check logs.");
374 void renameGenericCommand(redisClient
*c
, int nx
) {
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
);
383 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
387 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
390 addReply(c
,shared
.czero
);
393 dbReplace(c
->db
,c
->argv
[2],o
);
395 dbDelete(c
->db
,c
->argv
[1]);
396 signalModifiedKey(c
->db
,c
->argv
[1]);
397 signalModifiedKey(c
->db
,c
->argv
[2]);
399 addReply(c
,nx
? shared
.cone
: shared
.ok
);
402 void renameCommand(redisClient
*c
) {
403 renameGenericCommand(c
,0);
406 void renamenxCommand(redisClient
*c
) {
407 renameGenericCommand(c
,1);
410 void moveCommand(redisClient
*c
) {
415 /* Obtain source and target DB pointers */
418 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
419 addReply(c
,shared
.outofrangeerr
);
423 selectDb(c
,srcid
); /* Back to the source DB */
425 /* If the user is moving using as target the same
426 * DB as the source DB it is probably an error. */
428 addReply(c
,shared
.sameobjecterr
);
432 /* Check if the element exists and get a reference */
433 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
435 addReply(c
,shared
.czero
);
439 /* Try to add the element to the target DB */
440 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
441 addReply(c
,shared
.czero
);
446 /* OK! key moved, free the entry in the source DB */
447 dbDelete(src
,c
->argv
[1]);
449 addReply(c
,shared
.cone
);
452 /*-----------------------------------------------------------------------------
454 *----------------------------------------------------------------------------*/
456 int 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
);
460 return dictDelete(db
->expires
,key
->ptr
) == DICT_OK
;
463 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
466 /* Reuse the sds from the main dict in the expire dict */
467 de
= dictFind(db
->dict
,key
->ptr
);
468 redisAssert(de
!= NULL
);
469 dictReplace(db
->expires
,dictGetEntryKey(de
),(void*)when
);
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) */
474 time_t getExpire(redisDb
*db
, robj
*key
) {
477 /* No expire? return ASAP */
478 if (dictSize(db
->expires
) == 0 ||
479 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
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
);
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.
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
495 void propagateExpire(redisDb
*db
, robj
*key
) {
498 argv
[0] = createStringObject("DEL",3);
502 if (server
.appendonly
)
503 feedAppendOnlyFile(server
.delCommand
,db
->id
,argv
,2);
504 if (listLength(server
.slaves
))
505 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
507 decrRefCount(argv
[0]);
508 decrRefCount(argv
[1]);
511 int expireIfNeeded(redisDb
*db
, robj
*key
) {
512 time_t when
= getExpire(db
,key
);
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.
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
;
525 if (when
< 0) return 0;
527 /* Return when this key has not expired */
528 if (time(NULL
) <= when
) return 0;
531 server
.stat_expiredkeys
++;
532 propagateExpire(db
,key
);
533 return dbDelete(db
,key
);
536 /*-----------------------------------------------------------------------------
538 *----------------------------------------------------------------------------*/
540 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
544 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
548 de
= dictFind(c
->db
->dict
,key
->ptr
);
550 addReply(c
,shared
.czero
);
554 if (dbDelete(c
->db
,key
)) server
.dirty
++;
555 addReply(c
, shared
.cone
);
556 signalModifiedKey(c
->db
,key
);
559 time_t when
= time(NULL
)+seconds
;
560 setExpire(c
->db
,key
,when
);
561 addReply(c
,shared
.cone
);
562 signalModifiedKey(c
->db
,key
);
568 void expireCommand(redisClient
*c
) {
569 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
572 void expireatCommand(redisClient
*c
) {
573 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
576 void ttlCommand(redisClient
*c
) {
577 time_t expire
, ttl
= -1;
579 expire
= getExpire(c
->db
,c
->argv
[1]);
581 ttl
= (expire
-time(NULL
));
582 if (ttl
< 0) ttl
= -1;
584 addReplyLongLong(c
,(long long)ttl
);
587 void persistCommand(redisClient
*c
) {
590 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
592 addReply(c
,shared
.czero
);
594 if (removeExpire(c
->db
,c
->argv
[1])) {
595 addReply(c
,shared
.cone
);
598 addReply(c
,shared
.czero
);