]>
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 redisAssert(val
->storage
!= REDIS_DS_SAVING
);
26 server
.stat_keyspace_hits
++;
29 /* FIXME: Check if the object is on disk, if it is, load it
30 * in a blocking way now. */
31 server
.stat_keyspace_misses
++;
36 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
37 expireIfNeeded(db
,key
);
38 return lookupKey(db
,key
);
41 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
42 expireIfNeeded(db
,key
);
43 return lookupKey(db
,key
);
46 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
47 robj
*o
= lookupKeyRead(c
->db
, key
);
48 if (!o
) addReply(c
,reply
);
52 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
53 robj
*o
= lookupKeyWrite(c
->db
, key
);
54 if (!o
) addReply(c
,reply
);
58 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
59 * otherwise REDIS_OK is returned, and the caller should increment the
60 * refcount of 'val'. */
61 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
62 /* Perform a lookup before adding the key, as we need to copy the
64 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
67 sds copy
= sdsdup(key
->ptr
);
68 dictAdd(db
->dict
, copy
, val
);
73 /* If the key does not exist, this is just like dbAdd(). Otherwise
74 * the value associated to the key is replaced with the new one.
76 * On update (key already existed) 0 is returned. Otherwise 1. */
77 int dbReplace(redisDb
*db
, robj
*key
, robj
*val
) {
78 if (dictFind(db
->dict
,key
->ptr
) == NULL
) {
79 sds copy
= sdsdup(key
->ptr
);
80 dictAdd(db
->dict
, copy
, val
);
83 dictReplace(db
->dict
, key
->ptr
, val
);
88 int dbExists(redisDb
*db
, robj
*key
) {
89 return dictFind(db
->dict
,key
->ptr
) != NULL
;
92 /* Return a random key, in form of a Redis object.
93 * If there are no keys, NULL is returned.
95 * The function makes sure to return keys not already expired. */
96 robj
*dbRandomKey(redisDb
*db
) {
103 de
= dictGetRandomKey(db
->dict
);
104 if (de
== NULL
) return NULL
;
106 key
= dictGetEntryKey(de
);
107 keyobj
= createStringObject(key
,sdslen(key
));
108 if (dictFind(db
->expires
,key
)) {
109 if (expireIfNeeded(db
,keyobj
)) {
110 decrRefCount(keyobj
);
111 continue; /* search for another key. This expired. */
118 /* Delete a key, value, and associated expiration entry if any, from the DB */
119 int dbDelete(redisDb
*db
, robj
*key
) {
120 /* If VM is enabled make sure to awake waiting clients for this key:
121 * deleting the key will kill the I/O thread bringing the key from swap
122 * to memory, so the client will never be notified and unblocked if we
123 * don't do it now. */
124 if (server
.ds_enabled
) handleClientsBlockedOnSwappedKey(db
,key
);
126 /* FIXME: we need to delete the IO Job loading the key, or simply we can
127 * wait for it to finish. */
129 /* Deleting an entry from the expires dict will not free the sds of
130 * the key, because it is shared with the main dictionary. */
131 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
132 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
135 /* Empty the whole database */
136 long long emptyDb() {
138 long long removed
= 0;
140 for (j
= 0; j
< server
.dbnum
; j
++) {
141 removed
+= dictSize(server
.db
[j
].dict
);
142 dictEmpty(server
.db
[j
].dict
);
143 dictEmpty(server
.db
[j
].expires
);
148 int selectDb(redisClient
*c
, int id
) {
149 if (id
< 0 || id
>= server
.dbnum
)
151 c
->db
= &server
.db
[id
];
155 /*-----------------------------------------------------------------------------
156 * Hooks for key space changes.
158 * Every time a key in the database is modified the function
159 * signalModifiedKey() is called.
161 * Every time a DB is flushed the function signalFlushDb() is called.
162 *----------------------------------------------------------------------------*/
164 void signalModifiedKey(redisDb
*db
, robj
*key
) {
165 touchWatchedKey(db
,key
);
166 if (server
.ds_enabled
)
167 cacheScheduleForFlush(db
,key
);
170 void signalFlushedDb(int dbid
) {
171 touchWatchedKeysOnFlush(dbid
);
172 if (server
.ds_enabled
)
176 /*-----------------------------------------------------------------------------
177 * Type agnostic commands operating on the key space
178 *----------------------------------------------------------------------------*/
180 void flushdbCommand(redisClient
*c
) {
181 server
.dirty
+= dictSize(c
->db
->dict
);
182 signalFlushedDb(c
->db
->id
);
183 dictEmpty(c
->db
->dict
);
184 dictEmpty(c
->db
->expires
);
185 addReply(c
,shared
.ok
);
188 void flushallCommand(redisClient
*c
) {
190 server
.dirty
+= emptyDb();
191 addReply(c
,shared
.ok
);
192 if (server
.bgsavechildpid
!= -1) {
193 kill(server
.bgsavechildpid
,SIGKILL
);
194 rdbRemoveTempFile(server
.bgsavechildpid
);
196 rdbSave(server
.dbfilename
);
200 void delCommand(redisClient
*c
) {
203 for (j
= 1; j
< c
->argc
; j
++) {
204 if (dbDelete(c
->db
,c
->argv
[j
])) {
205 signalModifiedKey(c
->db
,c
->argv
[j
]);
210 addReplyLongLong(c
,deleted
);
213 void existsCommand(redisClient
*c
) {
214 expireIfNeeded(c
->db
,c
->argv
[1]);
215 if (dbExists(c
->db
,c
->argv
[1])) {
216 addReply(c
, shared
.cone
);
218 addReply(c
, shared
.czero
);
222 void selectCommand(redisClient
*c
) {
223 int id
= atoi(c
->argv
[1]->ptr
);
225 if (selectDb(c
,id
) == REDIS_ERR
) {
226 addReplyError(c
,"invalid DB index");
228 addReply(c
,shared
.ok
);
232 void randomkeyCommand(redisClient
*c
) {
235 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
236 addReply(c
,shared
.nullbulk
);
244 void keysCommand(redisClient
*c
) {
247 sds pattern
= c
->argv
[1]->ptr
;
248 int plen
= sdslen(pattern
), allkeys
;
249 unsigned long numkeys
= 0;
250 void *replylen
= addDeferredMultiBulkLength(c
);
252 di
= dictGetIterator(c
->db
->dict
);
253 allkeys
= (pattern
[0] == '*' && pattern
[1] == '\0');
254 while((de
= dictNext(di
)) != NULL
) {
255 sds key
= dictGetEntryKey(de
);
258 if (allkeys
|| stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
259 keyobj
= createStringObject(key
,sdslen(key
));
260 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
261 addReplyBulk(c
,keyobj
);
264 decrRefCount(keyobj
);
267 dictReleaseIterator(di
);
268 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
271 void dbsizeCommand(redisClient
*c
) {
272 addReplyLongLong(c
,dictSize(c
->db
->dict
));
275 void lastsaveCommand(redisClient
*c
) {
276 addReplyLongLong(c
,server
.lastsave
);
279 void typeCommand(redisClient
*c
) {
283 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
288 case REDIS_STRING
: type
= "string"; break;
289 case REDIS_LIST
: type
= "list"; break;
290 case REDIS_SET
: type
= "set"; break;
291 case REDIS_ZSET
: type
= "zset"; break;
292 case REDIS_HASH
: type
= "hash"; break;
293 default: type
= "unknown"; break;
296 addReplyStatus(c
,type
);
299 void saveCommand(redisClient
*c
) {
300 if (server
.bgsavechildpid
!= -1) {
301 addReplyError(c
,"Background save already in progress");
304 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
305 addReply(c
,shared
.ok
);
307 addReply(c
,shared
.err
);
311 void bgsaveCommand(redisClient
*c
) {
312 if (server
.bgsavechildpid
!= -1) {
313 addReplyError(c
,"Background save already in progress");
316 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
317 addReplyStatus(c
,"Background saving started");
319 addReply(c
,shared
.err
);
323 void shutdownCommand(redisClient
*c
) {
324 if (prepareForShutdown() == REDIS_OK
)
326 addReplyError(c
,"Errors trying to SHUTDOWN. Check logs.");
329 void renameGenericCommand(redisClient
*c
, int nx
) {
332 /* To use the same key as src and dst is probably an error */
333 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
334 addReply(c
,shared
.sameobjecterr
);
338 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
342 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
345 addReply(c
,shared
.czero
);
348 dbReplace(c
->db
,c
->argv
[2],o
);
350 dbDelete(c
->db
,c
->argv
[1]);
351 signalModifiedKey(c
->db
,c
->argv
[1]);
352 signalModifiedKey(c
->db
,c
->argv
[2]);
354 addReply(c
,nx
? shared
.cone
: shared
.ok
);
357 void renameCommand(redisClient
*c
) {
358 renameGenericCommand(c
,0);
361 void renamenxCommand(redisClient
*c
) {
362 renameGenericCommand(c
,1);
365 void moveCommand(redisClient
*c
) {
370 /* Obtain source and target DB pointers */
373 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
374 addReply(c
,shared
.outofrangeerr
);
378 selectDb(c
,srcid
); /* Back to the source DB */
380 /* If the user is moving using as target the same
381 * DB as the source DB it is probably an error. */
383 addReply(c
,shared
.sameobjecterr
);
387 /* Check if the element exists and get a reference */
388 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
390 addReply(c
,shared
.czero
);
394 /* Try to add the element to the target DB */
395 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
396 addReply(c
,shared
.czero
);
401 /* OK! key moved, free the entry in the source DB */
402 dbDelete(src
,c
->argv
[1]);
404 addReply(c
,shared
.cone
);
407 /*-----------------------------------------------------------------------------
409 *----------------------------------------------------------------------------*/
411 int removeExpire(redisDb
*db
, robj
*key
) {
412 /* An expire may only be removed if there is a corresponding entry in the
413 * main dict. Otherwise, the key will never be freed. */
414 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
415 return dictDelete(db
->expires
,key
->ptr
) == DICT_OK
;
418 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
421 /* Reuse the sds from the main dict in the expire dict */
422 de
= dictFind(db
->dict
,key
->ptr
);
423 redisAssert(de
!= NULL
);
424 dictReplace(db
->expires
,dictGetEntryKey(de
),(void*)when
);
427 /* Return the expire time of the specified key, or -1 if no expire
428 * is associated with this key (i.e. the key is non volatile) */
429 time_t getExpire(redisDb
*db
, robj
*key
) {
432 /* No expire? return ASAP */
433 if (dictSize(db
->expires
) == 0 ||
434 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
436 /* The entry was found in the expire dict, this means it should also
437 * be present in the main dict (safety check). */
438 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
439 return (time_t) dictGetEntryVal(de
);
442 /* Propagate expires into slaves and the AOF file.
443 * When a key expires in the master, a DEL operation for this key is sent
444 * to all the slaves and the AOF file if enabled.
446 * This way the key expiry is centralized in one place, and since both
447 * AOF and the master->slave link guarantee operation ordering, everything
448 * will be consistent even if we allow write operations against expiring
450 void propagateExpire(redisDb
*db
, robj
*key
) {
453 argv
[0] = createStringObject("DEL",3);
457 if (server
.appendonly
)
458 feedAppendOnlyFile(server
.delCommand
,db
->id
,argv
,2);
459 if (listLength(server
.slaves
))
460 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
462 decrRefCount(argv
[0]);
463 decrRefCount(argv
[1]);
466 int expireIfNeeded(redisDb
*db
, robj
*key
) {
467 time_t when
= getExpire(db
,key
);
469 /* If we are running in the context of a slave, return ASAP:
470 * the slave key expiration is controlled by the master that will
471 * send us synthesized DEL operations for expired keys.
473 * Still we try to return the right information to the caller,
474 * that is, 0 if we think the key should be still valid, 1 if
475 * we think the key is expired at this time. */
476 if (server
.masterhost
!= NULL
) {
477 return time(NULL
) > when
;
480 if (when
< 0) return 0;
482 /* Return when this key has not expired */
483 if (time(NULL
) <= when
) return 0;
486 server
.stat_expiredkeys
++;
487 propagateExpire(db
,key
);
488 return dbDelete(db
,key
);
491 /*-----------------------------------------------------------------------------
493 *----------------------------------------------------------------------------*/
495 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
499 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
503 de
= dictFind(c
->db
->dict
,key
->ptr
);
505 addReply(c
,shared
.czero
);
509 if (dbDelete(c
->db
,key
)) server
.dirty
++;
510 addReply(c
, shared
.cone
);
511 signalModifiedKey(c
->db
,key
);
514 time_t when
= time(NULL
)+seconds
;
515 setExpire(c
->db
,key
,when
);
516 addReply(c
,shared
.cone
);
517 signalModifiedKey(c
->db
,key
);
523 void expireCommand(redisClient
*c
) {
524 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
527 void expireatCommand(redisClient
*c
) {
528 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
531 void ttlCommand(redisClient
*c
) {
532 time_t expire
, ttl
= -1;
534 expire
= getExpire(c
->db
,c
->argv
[1]);
536 ttl
= (expire
-time(NULL
));
537 if (ttl
< 0) ttl
= -1;
539 addReplyLongLong(c
,(long long)ttl
);
542 void persistCommand(redisClient
*c
) {
545 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
547 addReply(c
,shared
.czero
);
549 if (removeExpire(c
->db
,c
->argv
[1])) {
550 addReply(c
,shared
.cone
);
553 addReply(c
,shared
.czero
);