]>
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 if (server
.vm_enabled
) {
15 if (val
->storage
== REDIS_VM_MEMORY
||
16 val
->storage
== REDIS_VM_SWAPPING
)
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
;
24 int notify
= (val
->storage
== REDIS_VM_LOADING
);
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
;
31 /* Clients blocked by the VM subsystem may be waiting for
33 if (notify
) handleClientsBlockedOnSwappedKey(db
,key
);
42 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
43 expireIfNeeded(db
,key
);
44 return lookupKey(db
,key
);
47 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
48 expireIfNeeded(db
,key
);
49 return lookupKey(db
,key
);
52 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
53 robj
*o
= lookupKeyRead(c
->db
, key
);
54 if (!o
) addReply(c
,reply
);
58 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
59 robj
*o
= lookupKeyWrite(c
->db
, key
);
60 if (!o
) addReply(c
,reply
);
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'. */
67 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
68 /* Perform a lookup before adding the key, as we need to copy the
70 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
73 sds copy
= sdsdup(key
->ptr
);
74 dictAdd(db
->dict
, copy
, val
);
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.
82 * On update (key already existed) 0 is returned. Otherwise 1. */
83 int 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
);
89 dictReplace(db
->dict
, key
->ptr
, val
);
94 int dbExists(redisDb
*db
, robj
*key
) {
95 return dictFind(db
->dict
,key
->ptr
) != NULL
;
98 /* Return a random key, in form of a Redis object.
99 * If there are no keys, NULL is returned.
101 * The function makes sure to return keys not already expired. */
102 robj
*dbRandomKey(redisDb
*db
) {
103 struct dictEntry
*de
;
109 de
= dictGetRandomKey(db
->dict
);
110 if (de
== NULL
) return NULL
;
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. */
124 /* Delete a key, value, and associated expiration entry if any, from the DB */
125 int dbDelete(redisDb
*db
, robj
*key
) {
126 /* Deleting an entry from the expires dict will not free the sds of
127 * the key, because it is shared with the main dictionary. */
128 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
129 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
132 /* Empty the whole database */
133 long long emptyDb() {
135 long long removed
= 0;
137 for (j
= 0; j
< server
.dbnum
; j
++) {
138 removed
+= dictSize(server
.db
[j
].dict
);
139 dictEmpty(server
.db
[j
].dict
);
140 dictEmpty(server
.db
[j
].expires
);
145 int selectDb(redisClient
*c
, int id
) {
146 if (id
< 0 || id
>= server
.dbnum
)
148 c
->db
= &server
.db
[id
];
152 /*-----------------------------------------------------------------------------
153 * Type agnostic commands operating on the key space
154 *----------------------------------------------------------------------------*/
156 void flushdbCommand(redisClient
*c
) {
157 server
.dirty
+= dictSize(c
->db
->dict
);
158 touchWatchedKeysOnFlush(c
->db
->id
);
159 dictEmpty(c
->db
->dict
);
160 dictEmpty(c
->db
->expires
);
161 addReply(c
,shared
.ok
);
164 void flushallCommand(redisClient
*c
) {
165 touchWatchedKeysOnFlush(-1);
166 server
.dirty
+= emptyDb();
167 addReply(c
,shared
.ok
);
168 if (server
.bgsavechildpid
!= -1) {
169 kill(server
.bgsavechildpid
,SIGKILL
);
170 rdbRemoveTempFile(server
.bgsavechildpid
);
172 rdbSave(server
.dbfilename
);
176 void delCommand(redisClient
*c
) {
179 for (j
= 1; j
< c
->argc
; j
++) {
180 if (dbDelete(c
->db
,c
->argv
[j
])) {
181 touchWatchedKey(c
->db
,c
->argv
[j
]);
186 addReplyLongLong(c
,deleted
);
189 void existsCommand(redisClient
*c
) {
190 expireIfNeeded(c
->db
,c
->argv
[1]);
191 if (dbExists(c
->db
,c
->argv
[1])) {
192 addReply(c
, shared
.cone
);
194 addReply(c
, shared
.czero
);
198 void selectCommand(redisClient
*c
) {
199 int id
= atoi(c
->argv
[1]->ptr
);
201 if (selectDb(c
,id
) == REDIS_ERR
) {
202 addReplySds(c
,sdsnew("-ERR invalid DB index\r\n"));
204 addReply(c
,shared
.ok
);
208 void randomkeyCommand(redisClient
*c
) {
211 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
212 addReply(c
,shared
.nullbulk
);
220 void keysCommand(redisClient
*c
) {
223 sds pattern
= c
->argv
[1]->ptr
;
224 int plen
= sdslen(pattern
);
225 unsigned long numkeys
= 0;
226 void *replylen
= addDeferredMultiBulkLength(c
);
228 di
= dictGetIterator(c
->db
->dict
);
229 while((de
= dictNext(di
)) != NULL
) {
230 sds key
= dictGetEntryKey(de
);
233 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
234 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
235 keyobj
= createStringObject(key
,sdslen(key
));
236 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
237 addReplyBulk(c
,keyobj
);
240 decrRefCount(keyobj
);
243 dictReleaseIterator(di
);
244 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
247 void dbsizeCommand(redisClient
*c
) {
248 addReplyLongLong(c
,dictSize(c
->db
->dict
));
251 void lastsaveCommand(redisClient
*c
) {
252 addReplyLongLong(c
,server
.lastsave
);
255 void typeCommand(redisClient
*c
) {
259 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
264 case REDIS_STRING
: type
= "+string"; break;
265 case REDIS_LIST
: type
= "+list"; break;
266 case REDIS_SET
: type
= "+set"; break;
267 case REDIS_ZSET
: type
= "+zset"; break;
268 case REDIS_HASH
: type
= "+hash"; break;
269 default: type
= "+unknown"; break;
272 addReplySds(c
,sdsnew(type
));
273 addReply(c
,shared
.crlf
);
276 void saveCommand(redisClient
*c
) {
277 if (server
.bgsavechildpid
!= -1) {
278 addReplySds(c
,sdsnew("-ERR background save in progress\r\n"));
281 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
282 addReply(c
,shared
.ok
);
284 addReply(c
,shared
.err
);
288 void bgsaveCommand(redisClient
*c
) {
289 if (server
.bgsavechildpid
!= -1) {
290 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
293 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
294 char *status
= "+Background saving started\r\n";
295 addReplySds(c
,sdsnew(status
));
297 addReply(c
,shared
.err
);
301 void shutdownCommand(redisClient
*c
) {
302 if (prepareForShutdown() == REDIS_OK
)
304 addReplySds(c
, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
307 void renameGenericCommand(redisClient
*c
, int nx
) {
310 /* To use the same key as src and dst is probably an error */
311 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
312 addReply(c
,shared
.sameobjecterr
);
316 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
320 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
323 addReply(c
,shared
.czero
);
326 dbReplace(c
->db
,c
->argv
[2],o
);
328 dbDelete(c
->db
,c
->argv
[1]);
329 touchWatchedKey(c
->db
,c
->argv
[1]);
330 touchWatchedKey(c
->db
,c
->argv
[2]);
332 addReply(c
,nx
? shared
.cone
: shared
.ok
);
335 void renameCommand(redisClient
*c
) {
336 renameGenericCommand(c
,0);
339 void renamenxCommand(redisClient
*c
) {
340 renameGenericCommand(c
,1);
343 void moveCommand(redisClient
*c
) {
348 /* Obtain source and target DB pointers */
351 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
352 addReply(c
,shared
.outofrangeerr
);
356 selectDb(c
,srcid
); /* Back to the source DB */
358 /* If the user is moving using as target the same
359 * DB as the source DB it is probably an error. */
361 addReply(c
,shared
.sameobjecterr
);
365 /* Check if the element exists and get a reference */
366 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
368 addReply(c
,shared
.czero
);
372 /* Try to add the element to the target DB */
373 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
374 addReply(c
,shared
.czero
);
379 /* OK! key moved, free the entry in the source DB */
380 dbDelete(src
,c
->argv
[1]);
382 addReply(c
,shared
.cone
);
385 /*-----------------------------------------------------------------------------
387 *----------------------------------------------------------------------------*/
389 int removeExpire(redisDb
*db
, robj
*key
) {
390 /* An expire may only be removed if there is a corresponding entry in the
391 * main dict. Otherwise, the key will never be freed. */
392 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
393 return dictDelete(db
->expires
,key
->ptr
) == DICT_OK
;
396 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
399 /* Reuse the sds from the main dict in the expire dict */
400 de
= dictFind(db
->dict
,key
->ptr
);
401 redisAssert(de
!= NULL
);
402 dictReplace(db
->expires
,dictGetEntryKey(de
),(void*)when
);
405 /* Return the expire time of the specified key, or -1 if no expire
406 * is associated with this key (i.e. the key is non volatile) */
407 time_t getExpire(redisDb
*db
, robj
*key
) {
410 /* No expire? return ASAP */
411 if (dictSize(db
->expires
) == 0 ||
412 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
414 /* The entry was found in the expire dict, this means it should also
415 * be present in the main dict (safety check). */
416 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
417 return (time_t) dictGetEntryVal(de
);
420 /* Propagate expires into slaves and the AOF file.
421 * When a key expires in the master, a DEL operation for this key is sent
422 * to all the slaves and the AOF file if enabled.
424 * This way the key expiry is centralized in one place, and since both
425 * AOF and the master->slave link guarantee operation ordering, everything
426 * will be consistent even if we allow write operations against expiring
428 void propagateExpire(redisDb
*db
, robj
*key
) {
429 struct redisCommand
*cmd
;
432 cmd
= lookupCommand("del");
433 argv
[0] = createStringObject("DEL",3);
437 if (server
.appendonly
)
438 feedAppendOnlyFile(cmd
,db
->id
,argv
,2);
439 if (listLength(server
.slaves
))
440 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
442 decrRefCount(argv
[0]);
443 decrRefCount(argv
[1]);
446 int expireIfNeeded(redisDb
*db
, robj
*key
) {
447 time_t when
= getExpire(db
,key
);
449 /* If we are running in the context of a slave, return ASAP:
450 * the slave key expiration is controlled by the master that will
451 * send us synthesized DEL operations for expired keys.
453 * Still we try to return the right information to the caller,
454 * that is, 0 if we think the key should be still valid, 1 if
455 * we think the key is expired at this time. */
456 if (server
.masterhost
!= NULL
) {
457 return time(NULL
) > when
;
460 if (when
< 0) return 0;
462 /* Return when this key has not expired */
463 if (time(NULL
) <= when
) return 0;
466 server
.stat_expiredkeys
++;
468 propagateExpire(db
,key
);
469 return dbDelete(db
,key
);
472 /*-----------------------------------------------------------------------------
474 *----------------------------------------------------------------------------*/
476 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
480 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
484 de
= dictFind(c
->db
->dict
,key
->ptr
);
486 addReply(c
,shared
.czero
);
490 if (dbDelete(c
->db
,key
)) server
.dirty
++;
491 addReply(c
, shared
.cone
);
492 touchWatchedKey(c
->db
,key
);
495 time_t when
= time(NULL
)+seconds
;
496 setExpire(c
->db
,key
,when
);
497 addReply(c
,shared
.cone
);
498 touchWatchedKey(c
->db
,key
);
504 void expireCommand(redisClient
*c
) {
505 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
508 void expireatCommand(redisClient
*c
) {
509 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
512 void ttlCommand(redisClient
*c
) {
513 time_t expire
, ttl
= -1;
515 expire
= getExpire(c
->db
,c
->argv
[1]);
517 ttl
= (expire
-time(NULL
));
518 if (ttl
< 0) ttl
= -1;
520 addReplyLongLong(c
,(long long)ttl
);
523 void persistCommand(redisClient
*c
) {
526 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
528 addReply(c
,shared
.czero
);
530 if (removeExpire(c
->db
,c
->argv
[1])) {
531 addReply(c
,shared
.cone
);
534 addReply(c
,shared
.czero
);