]>
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 deleteIfVolatile(db
,key
);
49 touchWatchedKey(db
,key
);
50 return lookupKey(db
,key
);
53 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
54 robj
*o
= lookupKeyRead(c
->db
, key
);
55 if (!o
) addReply(c
,reply
);
59 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
60 robj
*o
= lookupKeyWrite(c
->db
, key
);
61 if (!o
) addReply(c
,reply
);
65 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
66 * otherwise REDIS_OK is returned, and the caller should increment the
67 * refcount of 'val'. */
68 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
69 /* Perform a lookup before adding the key, as we need to copy the
71 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
74 sds copy
= sdsdup(key
->ptr
);
75 dictAdd(db
->dict
, copy
, val
);
80 /* If the key does not exist, this is just like dbAdd(). Otherwise
81 * the value associated to the key is replaced with the new one.
83 * On update (key already existed) 0 is returned. Otherwise 1. */
84 int dbReplace(redisDb
*db
, robj
*key
, robj
*val
) {
85 if (dictFind(db
->dict
,key
->ptr
) == NULL
) {
86 sds copy
= sdsdup(key
->ptr
);
87 dictAdd(db
->dict
, copy
, val
);
90 dictReplace(db
->dict
, key
->ptr
, val
);
95 int dbExists(redisDb
*db
, robj
*key
) {
96 return dictFind(db
->dict
,key
->ptr
) != NULL
;
99 /* Return a random key, in form of a Redis object.
100 * If there are no keys, NULL is returned.
102 * The function makes sure to return keys not already expired. */
103 robj
*dbRandomKey(redisDb
*db
) {
104 struct dictEntry
*de
;
110 de
= dictGetRandomKey(db
->dict
);
111 if (de
== NULL
) return NULL
;
113 key
= dictGetEntryKey(de
);
114 keyobj
= createStringObject(key
,sdslen(key
));
115 if (dictFind(db
->expires
,key
)) {
116 if (expireIfNeeded(db
,keyobj
)) {
117 decrRefCount(keyobj
);
118 continue; /* search for another key. This expired. */
125 /* Delete a key, value, and associated expiration entry if any, from the DB */
126 int dbDelete(redisDb
*db
, robj
*key
) {
127 /* Deleting an entry from the expires dict will not free the sds of
128 * the key, because it is shared with the main dictionary. */
129 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
130 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
133 /* Empty the whole database */
134 long long emptyDb() {
136 long long removed
= 0;
138 for (j
= 0; j
< server
.dbnum
; j
++) {
139 removed
+= dictSize(server
.db
[j
].dict
);
140 dictEmpty(server
.db
[j
].dict
);
141 dictEmpty(server
.db
[j
].expires
);
146 int selectDb(redisClient
*c
, int id
) {
147 if (id
< 0 || id
>= server
.dbnum
)
149 c
->db
= &server
.db
[id
];
153 /*-----------------------------------------------------------------------------
154 * Type agnostic commands operating on the key space
155 *----------------------------------------------------------------------------*/
157 void flushdbCommand(redisClient
*c
) {
158 server
.dirty
+= dictSize(c
->db
->dict
);
159 touchWatchedKeysOnFlush(c
->db
->id
);
160 dictEmpty(c
->db
->dict
);
161 dictEmpty(c
->db
->expires
);
162 addReply(c
,shared
.ok
);
165 void flushallCommand(redisClient
*c
) {
166 touchWatchedKeysOnFlush(-1);
167 server
.dirty
+= emptyDb();
168 addReply(c
,shared
.ok
);
169 if (server
.bgsavechildpid
!= -1) {
170 kill(server
.bgsavechildpid
,SIGKILL
);
171 rdbRemoveTempFile(server
.bgsavechildpid
);
173 rdbSave(server
.dbfilename
);
177 void delCommand(redisClient
*c
) {
180 for (j
= 1; j
< c
->argc
; j
++) {
181 if (dbDelete(c
->db
,c
->argv
[j
])) {
182 touchWatchedKey(c
->db
,c
->argv
[j
]);
187 addReplyLongLong(c
,deleted
);
190 void existsCommand(redisClient
*c
) {
191 expireIfNeeded(c
->db
,c
->argv
[1]);
192 if (dbExists(c
->db
,c
->argv
[1])) {
193 addReply(c
, shared
.cone
);
195 addReply(c
, shared
.czero
);
199 void selectCommand(redisClient
*c
) {
200 int id
= atoi(c
->argv
[1]->ptr
);
202 if (selectDb(c
,id
) == REDIS_ERR
) {
203 addReplySds(c
,sdsnew("-ERR invalid DB index\r\n"));
205 addReply(c
,shared
.ok
);
209 void randomkeyCommand(redisClient
*c
) {
212 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
213 addReply(c
,shared
.nullbulk
);
221 void keysCommand(redisClient
*c
) {
224 sds pattern
= c
->argv
[1]->ptr
;
225 int plen
= sdslen(pattern
);
226 unsigned long numkeys
= 0;
227 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
229 di
= dictGetIterator(c
->db
->dict
);
231 decrRefCount(lenobj
);
232 while((de
= dictNext(di
)) != NULL
) {
233 sds key
= dictGetEntryKey(de
);
236 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
237 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
238 keyobj
= createStringObject(key
,sdslen(key
));
239 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
240 addReplyBulk(c
,keyobj
);
243 decrRefCount(keyobj
);
246 dictReleaseIterator(di
);
247 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",numkeys
);
250 void dbsizeCommand(redisClient
*c
) {
252 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c
->db
->dict
)));
255 void lastsaveCommand(redisClient
*c
) {
257 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
260 void typeCommand(redisClient
*c
) {
264 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
269 case REDIS_STRING
: type
= "+string"; break;
270 case REDIS_LIST
: type
= "+list"; break;
271 case REDIS_SET
: type
= "+set"; break;
272 case REDIS_ZSET
: type
= "+zset"; break;
273 case REDIS_HASH
: type
= "+hash"; break;
274 default: type
= "+unknown"; break;
277 addReplySds(c
,sdsnew(type
));
278 addReply(c
,shared
.crlf
);
281 void saveCommand(redisClient
*c
) {
282 if (server
.bgsavechildpid
!= -1) {
283 addReplySds(c
,sdsnew("-ERR background save in progress\r\n"));
286 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
287 addReply(c
,shared
.ok
);
289 addReply(c
,shared
.err
);
293 void bgsaveCommand(redisClient
*c
) {
294 if (server
.bgsavechildpid
!= -1) {
295 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
298 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
299 char *status
= "+Background saving started\r\n";
300 addReplySds(c
,sdsnew(status
));
302 addReply(c
,shared
.err
);
306 void shutdownCommand(redisClient
*c
) {
307 if (prepareForShutdown() == REDIS_OK
)
309 addReplySds(c
, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
312 void renameGenericCommand(redisClient
*c
, int nx
) {
315 /* To use the same key as src and dst is probably an error */
316 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
317 addReply(c
,shared
.sameobjecterr
);
321 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
325 deleteIfVolatile(c
->db
,c
->argv
[2]);
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
[2]);
337 addReply(c
,nx
? shared
.cone
: shared
.ok
);
340 void renameCommand(redisClient
*c
) {
341 renameGenericCommand(c
,0);
344 void renamenxCommand(redisClient
*c
) {
345 renameGenericCommand(c
,1);
348 void moveCommand(redisClient
*c
) {
353 /* Obtain source and target DB pointers */
356 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
357 addReply(c
,shared
.outofrangeerr
);
361 selectDb(c
,srcid
); /* Back to the source DB */
363 /* If the user is moving using as target the same
364 * DB as the source DB it is probably an error. */
366 addReply(c
,shared
.sameobjecterr
);
370 /* Check if the element exists and get a reference */
371 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
373 addReply(c
,shared
.czero
);
377 /* Try to add the element to the target DB */
378 deleteIfVolatile(dst
,c
->argv
[1]);
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 if (dictDelete(db
->expires
,key
->ptr
) == DICT_OK
) {
406 int setExpire(redisDb
*db
, robj
*key
, time_t when
) {
409 /* Reuse the sds from the main dict in the expire dict */
410 redisAssert((de
= dictFind(db
->dict
,key
->ptr
)) != NULL
);
411 if (dictAdd(db
->expires
,dictGetEntryKey(de
),(void*)when
) == DICT_ERR
) {
418 /* Return the expire time of the specified key, or -1 if no expire
419 * is associated with this key (i.e. the key is non volatile) */
420 time_t getExpire(redisDb
*db
, robj
*key
) {
423 /* No expire? return ASAP */
424 if (dictSize(db
->expires
) == 0 ||
425 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
427 /* The entry was found in the expire dict, this means it should also
428 * be present in the main dict (safety check). */
429 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
430 return (time_t) dictGetEntryVal(de
);
433 int expireIfNeeded(redisDb
*db
, robj
*key
) {
434 time_t when
= getExpire(db
,key
);
435 if (when
< 0) return 0;
437 /* Return when this key has not expired */
438 if (time(NULL
) <= when
) return 0;
441 server
.stat_expiredkeys
++;
443 return dbDelete(db
,key
);
446 int deleteIfVolatile(redisDb
*db
, robj
*key
) {
447 if (getExpire(db
,key
) < 0) return 0;
450 server
.stat_expiredkeys
++;
452 return dbDelete(db
,key
);
455 /*-----------------------------------------------------------------------------
457 *----------------------------------------------------------------------------*/
459 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
463 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
467 de
= dictFind(c
->db
->dict
,key
->ptr
);
469 addReply(c
,shared
.czero
);
473 if (dbDelete(c
->db
,key
)) server
.dirty
++;
474 addReply(c
, shared
.cone
);
477 time_t when
= time(NULL
)+seconds
;
478 if (setExpire(c
->db
,key
,when
)) {
479 addReply(c
,shared
.cone
);
482 addReply(c
,shared
.czero
);
488 void expireCommand(redisClient
*c
) {
489 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
492 void expireatCommand(redisClient
*c
) {
493 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
496 void ttlCommand(redisClient
*c
) {
500 expire
= getExpire(c
->db
,c
->argv
[1]);
502 ttl
= (int) (expire
-time(NULL
));
503 if (ttl
< 0) ttl
= -1;
505 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",ttl
));