]>
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 val
->lru
= server
.lruclock
;
17 if (server
.vm_enabled
) {
18 if (val
->storage
== REDIS_VM_MEMORY
||
19 val
->storage
== REDIS_VM_SWAPPING
)
21 /* If we were swapping the object out, cancel the operation */
22 if (val
->storage
== REDIS_VM_SWAPPING
)
23 vmCancelThreadedIOJob(val
);
25 int notify
= (val
->storage
== REDIS_VM_LOADING
);
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
;
32 /* Clients blocked by the VM subsystem may be waiting for
34 if (notify
) handleClientsBlockedOnSwappedKey(db
,key
);
37 server
.stat_keyspace_hits
++;
40 server
.stat_keyspace_misses
++;
45 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
46 expireIfNeeded(db
,key
);
47 return lookupKey(db
,key
);
50 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
51 expireIfNeeded(db
,key
);
52 return lookupKey(db
,key
);
55 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
56 robj
*o
= lookupKeyRead(c
->db
, key
);
57 if (!o
) addReply(c
,reply
);
61 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
62 robj
*o
= lookupKeyWrite(c
->db
, key
);
63 if (!o
) addReply(c
,reply
);
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
73 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
76 sds copy
= sdsdup(key
->ptr
);
77 dictAdd(db
->dict
, copy
, val
);
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.
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
);
92 dictReplace(db
->dict
, key
->ptr
, val
);
97 int dbExists(redisDb
*db
, robj
*key
) {
98 return dictFind(db
->dict
,key
->ptr
) != NULL
;
101 /* Return a random key, in form of a Redis object.
102 * If there are no keys, NULL is returned.
104 * The function makes sure to return keys not already expired. */
105 robj
*dbRandomKey(redisDb
*db
) {
106 struct dictEntry
*de
;
112 de
= dictGetRandomKey(db
->dict
);
113 if (de
== NULL
) return NULL
;
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. */
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
;
140 /* Empty the whole database */
141 long long emptyDb() {
143 long long removed
= 0;
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
);
153 int selectDb(redisClient
*c
, int id
) {
154 if (id
< 0 || id
>= server
.dbnum
)
156 c
->db
= &server
.db
[id
];
160 /*-----------------------------------------------------------------------------
161 * Type agnostic commands operating on the key space
162 *----------------------------------------------------------------------------*/
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
);
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
);
180 rdbSave(server
.dbfilename
);
184 void delCommand(redisClient
*c
) {
187 for (j
= 1; j
< c
->argc
; j
++) {
188 if (dbDelete(c
->db
,c
->argv
[j
])) {
189 touchWatchedKey(c
->db
,c
->argv
[j
]);
194 addReplyLongLong(c
,deleted
);
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
);
202 addReply(c
, shared
.czero
);
206 void selectCommand(redisClient
*c
) {
207 int id
= atoi(c
->argv
[1]->ptr
);
209 if (selectDb(c
,id
) == REDIS_ERR
) {
210 addReplyError(c
,"invalid DB index");
212 addReply(c
,shared
.ok
);
216 void randomkeyCommand(redisClient
*c
) {
219 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
220 addReply(c
,shared
.nullbulk
);
228 void keysCommand(redisClient
*c
) {
231 sds pattern
= c
->argv
[1]->ptr
;
232 int plen
= sdslen(pattern
), allkeys
;
233 unsigned long numkeys
= 0;
234 void *replylen
= addDeferredMultiBulkLength(c
);
236 di
= dictGetIterator(c
->db
->dict
);
237 allkeys
= (pattern
[0] == '*' && pattern
[1] == '\0');
238 while((de
= dictNext(di
)) != NULL
) {
239 sds key
= dictGetEntryKey(de
);
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
);
248 decrRefCount(keyobj
);
251 dictReleaseIterator(di
);
252 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
255 void dbsizeCommand(redisClient
*c
) {
256 addReplyLongLong(c
,dictSize(c
->db
->dict
));
259 void lastsaveCommand(redisClient
*c
) {
260 addReplyLongLong(c
,server
.lastsave
);
263 void typeCommand(redisClient
*c
) {
267 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
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;
280 addReplyStatus(c
,type
);
283 void saveCommand(redisClient
*c
) {
284 if (server
.bgsavechildpid
!= -1) {
285 addReplyError(c
,"Background save already in progress");
288 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
289 addReply(c
,shared
.ok
);
291 addReply(c
,shared
.err
);
295 void bgsaveCommand(redisClient
*c
) {
296 if (server
.bgsavechildpid
!= -1) {
297 addReplyError(c
,"Background save already in progress");
300 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
301 addReplyStatus(c
,"Background saving started");
303 addReply(c
,shared
.err
);
307 void shutdownCommand(redisClient
*c
) {
308 if (prepareForShutdown() == REDIS_OK
)
310 addReplyError(c
,"Errors trying to SHUTDOWN. Check logs.");
313 void renameGenericCommand(redisClient
*c
, int nx
) {
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
);
322 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
326 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
329 addReply(c
,shared
.czero
);
332 dbReplace(c
->db
,c
->argv
[2],o
);
334 dbDelete(c
->db
,c
->argv
[1]);
335 touchWatchedKey(c
->db
,c
->argv
[1]);
336 touchWatchedKey(c
->db
,c
->argv
[2]);
338 addReply(c
,nx
? shared
.cone
: shared
.ok
);
341 void renameCommand(redisClient
*c
) {
342 renameGenericCommand(c
,0);
345 void renamenxCommand(redisClient
*c
) {
346 renameGenericCommand(c
,1);
349 void moveCommand(redisClient
*c
) {
354 /* Obtain source and target DB pointers */
357 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
358 addReply(c
,shared
.outofrangeerr
);
362 selectDb(c
,srcid
); /* Back to the source DB */
364 /* If the user is moving using as target the same
365 * DB as the source DB it is probably an error. */
367 addReply(c
,shared
.sameobjecterr
);
371 /* Check if the element exists and get a reference */
372 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
374 addReply(c
,shared
.czero
);
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
);
385 /* OK! key moved, free the entry in the source DB */
386 dbDelete(src
,c
->argv
[1]);
388 addReply(c
,shared
.cone
);
391 /*-----------------------------------------------------------------------------
393 *----------------------------------------------------------------------------*/
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
;
402 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
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
);
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
) {
416 /* No expire? return ASAP */
417 if (dictSize(db
->expires
) == 0 ||
418 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
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
);
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.
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
434 void propagateExpire(redisDb
*db
, robj
*key
) {
435 struct redisCommand
*cmd
;
438 cmd
= lookupCommand("del");
439 argv
[0] = createStringObject("DEL",3);
443 if (server
.appendonly
)
444 feedAppendOnlyFile(cmd
,db
->id
,argv
,2);
445 if (listLength(server
.slaves
))
446 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
448 decrRefCount(argv
[0]);
449 decrRefCount(argv
[1]);
452 int expireIfNeeded(redisDb
*db
, robj
*key
) {
453 time_t when
= getExpire(db
,key
);
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.
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
;
466 if (when
< 0) return 0;
468 /* Return when this key has not expired */
469 if (time(NULL
) <= when
) return 0;
472 server
.stat_expiredkeys
++;
473 propagateExpire(db
,key
);
474 return dbDelete(db
,key
);
477 /*-----------------------------------------------------------------------------
479 *----------------------------------------------------------------------------*/
481 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
485 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
489 de
= dictFind(c
->db
->dict
,key
->ptr
);
491 addReply(c
,shared
.czero
);
495 if (dbDelete(c
->db
,key
)) server
.dirty
++;
496 addReply(c
, shared
.cone
);
497 touchWatchedKey(c
->db
,key
);
500 time_t when
= time(NULL
)+seconds
;
501 setExpire(c
->db
,key
,when
);
502 addReply(c
,shared
.cone
);
503 touchWatchedKey(c
->db
,key
);
509 void expireCommand(redisClient
*c
) {
510 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
513 void expireatCommand(redisClient
*c
) {
514 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
517 void ttlCommand(redisClient
*c
) {
518 time_t expire
, ttl
= -1;
520 expire
= getExpire(c
->db
,c
->argv
[1]);
522 ttl
= (expire
-time(NULL
));
523 if (ttl
< 0) ttl
= -1;
525 addReplyLongLong(c
,(long long)ttl
);
528 void persistCommand(redisClient
*c
) {
531 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
533 addReply(c
,shared
.czero
);
535 if (removeExpire(c
->db
,c
->argv
[1])) {
536 addReply(c
,shared
.cone
);
539 addReply(c
,shared
.czero
);