]>
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
&& val
->storage
== REDIS_DS_SAVING
) {
21 /* FIXME: change this code to just wait for our object to
22 * get out of the IO Job. */
23 waitEmptyIOJobsQueue();
24 processAllPendingIOJobs();
25 redisAssert(val
->storage
!= REDIS_DS_SAVING
);
27 server
.stat_keyspace_hits
++;
33 /* Key not found in the in memory hash table, but if disk store is
34 * enabled we may have this key on disk. If so load it in memory
37 * FIXME: race condition here. If there was an already scheduled
38 * async loading of this key, what may happen is that the old
39 * key is loaded in memory if this gets deleted in the meantime. */
40 if (server
.ds_enabled
&& cacheKeyMayExist(db
,key
)) {
41 val
= dsGet(db
,key
,&expire
);
43 int retval
= dbAdd(db
,key
,val
);
44 redisAssert(retval
== REDIS_OK
);
45 if (expire
!= -1) setExpire(db
,key
,expire
);
46 server
.stat_keyspace_hits
++;
50 server
.stat_keyspace_misses
++;
55 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
56 expireIfNeeded(db
,key
);
57 return lookupKey(db
,key
);
60 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
61 expireIfNeeded(db
,key
);
62 return lookupKey(db
,key
);
65 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
66 robj
*o
= lookupKeyRead(c
->db
, key
);
67 if (!o
) addReply(c
,reply
);
71 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
72 robj
*o
= lookupKeyWrite(c
->db
, key
);
73 if (!o
) addReply(c
,reply
);
77 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
78 * otherwise REDIS_OK is returned, and the caller should increment the
79 * refcount of 'val'. */
80 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
81 /* Perform a lookup before adding the key, as we need to copy the
83 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
86 sds copy
= sdsdup(key
->ptr
);
87 dictAdd(db
->dict
, copy
, val
);
88 if (server
.ds_enabled
) {
89 /* FIXME: remove entry from negative cache */
95 /* If the key does not exist, this is just like dbAdd(). Otherwise
96 * the value associated to the key is replaced with the new one.
98 * On update (key already existed) 0 is returned. Otherwise 1. */
99 int dbReplace(redisDb
*db
, robj
*key
, robj
*val
) {
100 if (dictFind(db
->dict
,key
->ptr
) == NULL
) {
101 sds copy
= sdsdup(key
->ptr
);
102 dictAdd(db
->dict
, copy
, val
);
105 dictReplace(db
->dict
, key
->ptr
, val
);
110 int dbExists(redisDb
*db
, robj
*key
) {
111 return dictFind(db
->dict
,key
->ptr
) != NULL
;
114 /* Return a random key, in form of a Redis object.
115 * If there are no keys, NULL is returned.
117 * The function makes sure to return keys not already expired. */
118 robj
*dbRandomKey(redisDb
*db
) {
119 struct dictEntry
*de
;
125 de
= dictGetRandomKey(db
->dict
);
126 if (de
== NULL
) return NULL
;
128 key
= dictGetEntryKey(de
);
129 keyobj
= createStringObject(key
,sdslen(key
));
130 if (dictFind(db
->expires
,key
)) {
131 if (expireIfNeeded(db
,keyobj
)) {
132 decrRefCount(keyobj
);
133 continue; /* search for another key. This expired. */
140 /* Delete a key, value, and associated expiration entry if any, from the DB */
141 int dbDelete(redisDb
*db
, robj
*key
) {
142 /* If VM is enabled make sure to awake waiting clients for this key:
143 * deleting the key will kill the I/O thread bringing the key from swap
144 * to memory, so the client will never be notified and unblocked if we
145 * don't do it now. */
146 if (server
.ds_enabled
) handleClientsBlockedOnSwappedKey(db
,key
);
148 /* FIXME: we need to delete the IO Job loading the key, or simply we can
149 * wait for it to finish. */
151 /* Deleting an entry from the expires dict will not free the sds of
152 * the key, because it is shared with the main dictionary. */
153 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
154 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
157 /* Empty the whole database */
158 long long emptyDb() {
160 long long removed
= 0;
162 for (j
= 0; j
< server
.dbnum
; j
++) {
163 removed
+= dictSize(server
.db
[j
].dict
);
164 dictEmpty(server
.db
[j
].dict
);
165 dictEmpty(server
.db
[j
].expires
);
170 int selectDb(redisClient
*c
, int id
) {
171 if (id
< 0 || id
>= server
.dbnum
)
173 c
->db
= &server
.db
[id
];
177 /*-----------------------------------------------------------------------------
178 * Hooks for key space changes.
180 * Every time a key in the database is modified the function
181 * signalModifiedKey() is called.
183 * Every time a DB is flushed the function signalFlushDb() is called.
184 *----------------------------------------------------------------------------*/
186 void signalModifiedKey(redisDb
*db
, robj
*key
) {
187 touchWatchedKey(db
,key
);
188 if (server
.ds_enabled
)
189 cacheScheduleForFlush(db
,key
);
192 void signalFlushedDb(int dbid
) {
193 touchWatchedKeysOnFlush(dbid
);
194 if (server
.ds_enabled
)
198 /*-----------------------------------------------------------------------------
199 * Type agnostic commands operating on the key space
200 *----------------------------------------------------------------------------*/
202 void flushdbCommand(redisClient
*c
) {
203 server
.dirty
+= dictSize(c
->db
->dict
);
204 signalFlushedDb(c
->db
->id
);
205 dictEmpty(c
->db
->dict
);
206 dictEmpty(c
->db
->expires
);
207 addReply(c
,shared
.ok
);
210 void flushallCommand(redisClient
*c
) {
212 server
.dirty
+= emptyDb();
213 addReply(c
,shared
.ok
);
214 if (server
.bgsavechildpid
!= -1) {
215 kill(server
.bgsavechildpid
,SIGKILL
);
216 rdbRemoveTempFile(server
.bgsavechildpid
);
218 rdbSave(server
.dbfilename
);
222 void delCommand(redisClient
*c
) {
225 for (j
= 1; j
< c
->argc
; j
++) {
226 if (server
.ds_enabled
) {
227 lookupKeyRead(c
->db
,c
->argv
[j
]);
228 /* FIXME: this can be optimized a lot, no real need to load
229 * a possibly huge value. */
231 if (dbDelete(c
->db
,c
->argv
[j
])) {
232 signalModifiedKey(c
->db
,c
->argv
[j
]);
235 } else if (server
.ds_enabled
) {
236 if (cacheKeyMayExist(c
->db
,c
->argv
[j
]) &&
237 dsExists(c
->db
,c
->argv
[j
]))
239 cacheScheduleForFlush(c
->db
,c
->argv
[j
]);
244 addReplyLongLong(c
,deleted
);
247 void existsCommand(redisClient
*c
) {
248 expireIfNeeded(c
->db
,c
->argv
[1]);
249 if (dbExists(c
->db
,c
->argv
[1])) {
250 addReply(c
, shared
.cone
);
252 addReply(c
, shared
.czero
);
256 void selectCommand(redisClient
*c
) {
257 int id
= atoi(c
->argv
[1]->ptr
);
259 if (selectDb(c
,id
) == REDIS_ERR
) {
260 addReplyError(c
,"invalid DB index");
262 addReply(c
,shared
.ok
);
266 void randomkeyCommand(redisClient
*c
) {
269 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
270 addReply(c
,shared
.nullbulk
);
278 void keysCommand(redisClient
*c
) {
281 sds pattern
= c
->argv
[1]->ptr
;
282 int plen
= sdslen(pattern
), allkeys
;
283 unsigned long numkeys
= 0;
284 void *replylen
= addDeferredMultiBulkLength(c
);
286 di
= dictGetIterator(c
->db
->dict
);
287 allkeys
= (pattern
[0] == '*' && pattern
[1] == '\0');
288 while((de
= dictNext(di
)) != NULL
) {
289 sds key
= dictGetEntryKey(de
);
292 if (allkeys
|| stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
293 keyobj
= createStringObject(key
,sdslen(key
));
294 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
295 addReplyBulk(c
,keyobj
);
298 decrRefCount(keyobj
);
301 dictReleaseIterator(di
);
302 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
305 void dbsizeCommand(redisClient
*c
) {
306 addReplyLongLong(c
,dictSize(c
->db
->dict
));
309 void lastsaveCommand(redisClient
*c
) {
310 addReplyLongLong(c
,server
.lastsave
);
313 void typeCommand(redisClient
*c
) {
317 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
322 case REDIS_STRING
: type
= "string"; break;
323 case REDIS_LIST
: type
= "list"; break;
324 case REDIS_SET
: type
= "set"; break;
325 case REDIS_ZSET
: type
= "zset"; break;
326 case REDIS_HASH
: type
= "hash"; break;
327 default: type
= "unknown"; break;
330 addReplyStatus(c
,type
);
333 void saveCommand(redisClient
*c
) {
334 if (server
.bgsavechildpid
!= -1) {
335 addReplyError(c
,"Background save already in progress");
338 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
339 addReply(c
,shared
.ok
);
341 addReply(c
,shared
.err
);
345 void bgsaveCommand(redisClient
*c
) {
346 if (server
.bgsavechildpid
!= -1) {
347 addReplyError(c
,"Background save already in progress");
350 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
351 addReplyStatus(c
,"Background saving started");
353 addReply(c
,shared
.err
);
357 void shutdownCommand(redisClient
*c
) {
358 if (prepareForShutdown() == REDIS_OK
)
360 addReplyError(c
,"Errors trying to SHUTDOWN. Check logs.");
363 void renameGenericCommand(redisClient
*c
, int nx
) {
366 /* To use the same key as src and dst is probably an error */
367 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
368 addReply(c
,shared
.sameobjecterr
);
372 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
376 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
379 addReply(c
,shared
.czero
);
382 dbReplace(c
->db
,c
->argv
[2],o
);
384 dbDelete(c
->db
,c
->argv
[1]);
385 signalModifiedKey(c
->db
,c
->argv
[1]);
386 signalModifiedKey(c
->db
,c
->argv
[2]);
388 addReply(c
,nx
? shared
.cone
: shared
.ok
);
391 void renameCommand(redisClient
*c
) {
392 renameGenericCommand(c
,0);
395 void renamenxCommand(redisClient
*c
) {
396 renameGenericCommand(c
,1);
399 void moveCommand(redisClient
*c
) {
404 /* Obtain source and target DB pointers */
407 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
408 addReply(c
,shared
.outofrangeerr
);
412 selectDb(c
,srcid
); /* Back to the source DB */
414 /* If the user is moving using as target the same
415 * DB as the source DB it is probably an error. */
417 addReply(c
,shared
.sameobjecterr
);
421 /* Check if the element exists and get a reference */
422 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
424 addReply(c
,shared
.czero
);
428 /* Try to add the element to the target DB */
429 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
430 addReply(c
,shared
.czero
);
435 /* OK! key moved, free the entry in the source DB */
436 dbDelete(src
,c
->argv
[1]);
438 addReply(c
,shared
.cone
);
441 /*-----------------------------------------------------------------------------
443 *----------------------------------------------------------------------------*/
445 int removeExpire(redisDb
*db
, robj
*key
) {
446 /* An expire may only be removed if there is a corresponding entry in the
447 * main dict. Otherwise, the key will never be freed. */
448 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
449 return dictDelete(db
->expires
,key
->ptr
) == DICT_OK
;
452 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
455 /* Reuse the sds from the main dict in the expire dict */
456 de
= dictFind(db
->dict
,key
->ptr
);
457 redisAssert(de
!= NULL
);
458 dictReplace(db
->expires
,dictGetEntryKey(de
),(void*)when
);
461 /* Return the expire time of the specified key, or -1 if no expire
462 * is associated with this key (i.e. the key is non volatile) */
463 time_t getExpire(redisDb
*db
, robj
*key
) {
466 /* No expire? return ASAP */
467 if (dictSize(db
->expires
) == 0 ||
468 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
470 /* The entry was found in the expire dict, this means it should also
471 * be present in the main dict (safety check). */
472 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
473 return (time_t) dictGetEntryVal(de
);
476 /* Propagate expires into slaves and the AOF file.
477 * When a key expires in the master, a DEL operation for this key is sent
478 * to all the slaves and the AOF file if enabled.
480 * This way the key expiry is centralized in one place, and since both
481 * AOF and the master->slave link guarantee operation ordering, everything
482 * will be consistent even if we allow write operations against expiring
484 void propagateExpire(redisDb
*db
, robj
*key
) {
487 argv
[0] = createStringObject("DEL",3);
491 if (server
.appendonly
)
492 feedAppendOnlyFile(server
.delCommand
,db
->id
,argv
,2);
493 if (listLength(server
.slaves
))
494 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
496 decrRefCount(argv
[0]);
497 decrRefCount(argv
[1]);
500 int expireIfNeeded(redisDb
*db
, robj
*key
) {
501 time_t when
= getExpire(db
,key
);
503 /* If we are running in the context of a slave, return ASAP:
504 * the slave key expiration is controlled by the master that will
505 * send us synthesized DEL operations for expired keys.
507 * Still we try to return the right information to the caller,
508 * that is, 0 if we think the key should be still valid, 1 if
509 * we think the key is expired at this time. */
510 if (server
.masterhost
!= NULL
) {
511 return time(NULL
) > when
;
514 if (when
< 0) return 0;
516 /* Return when this key has not expired */
517 if (time(NULL
) <= when
) return 0;
520 server
.stat_expiredkeys
++;
521 propagateExpire(db
,key
);
522 return dbDelete(db
,key
);
525 /*-----------------------------------------------------------------------------
527 *----------------------------------------------------------------------------*/
529 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
533 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
537 de
= dictFind(c
->db
->dict
,key
->ptr
);
539 addReply(c
,shared
.czero
);
543 if (dbDelete(c
->db
,key
)) server
.dirty
++;
544 addReply(c
, shared
.cone
);
545 signalModifiedKey(c
->db
,key
);
548 time_t when
= time(NULL
)+seconds
;
549 setExpire(c
->db
,key
,when
);
550 addReply(c
,shared
.cone
);
551 signalModifiedKey(c
->db
,key
);
557 void expireCommand(redisClient
*c
) {
558 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
561 void expireatCommand(redisClient
*c
) {
562 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
565 void ttlCommand(redisClient
*c
) {
566 time_t expire
, ttl
= -1;
568 expire
= getExpire(c
->db
,c
->argv
[1]);
570 ttl
= (expire
-time(NULL
));
571 if (ttl
< 0) ttl
= -1;
573 addReplyLongLong(c
,(long long)ttl
);
576 void persistCommand(redisClient
*c
) {
579 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
581 addReply(c
,shared
.czero
);
583 if (removeExpire(c
->db
,c
->argv
[1])) {
584 addReply(c
,shared
.cone
);
587 addReply(c
,shared
.czero
);