]>
git.saurik.com Git - redis.git/blob - src/db.c
1b0289bcd97d4512b6f6b9359a120545ec95946f
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
++;
30 /* FIXME: Check if the object is on disk, if it is, load it
31 * in a blocking way now. If we are sure there are no collisions
32 * it would be cool to load this directly here without IO thread
34 server
.stat_keyspace_misses
++;
39 robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
40 expireIfNeeded(db
,key
);
41 return lookupKey(db
,key
);
44 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
45 expireIfNeeded(db
,key
);
46 return lookupKey(db
,key
);
49 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
50 robj
*o
= lookupKeyRead(c
->db
, key
);
51 if (!o
) addReply(c
,reply
);
55 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
56 robj
*o
= lookupKeyWrite(c
->db
, key
);
57 if (!o
) addReply(c
,reply
);
61 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
62 * otherwise REDIS_OK is returned, and the caller should increment the
63 * refcount of 'val'. */
64 int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
65 /* Perform a lookup before adding the key, as we need to copy the
67 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
70 sds copy
= sdsdup(key
->ptr
);
71 dictAdd(db
->dict
, copy
, val
);
72 if (server
.ds_enabled
) {
73 /* FIXME: remove entry from negative cache */
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 /* If VM is enabled make sure to awake waiting clients for this key:
127 * deleting the key will kill the I/O thread bringing the key from swap
128 * to memory, so the client will never be notified and unblocked if we
129 * don't do it now. */
130 if (server
.ds_enabled
) handleClientsBlockedOnSwappedKey(db
,key
);
132 /* FIXME: we need to delete the IO Job loading the key, or simply we can
133 * wait for it to finish. */
135 /* Deleting an entry from the expires dict will not free the sds of
136 * the key, because it is shared with the main dictionary. */
137 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
138 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
141 /* Empty the whole database */
142 long long emptyDb() {
144 long long removed
= 0;
146 for (j
= 0; j
< server
.dbnum
; j
++) {
147 removed
+= dictSize(server
.db
[j
].dict
);
148 dictEmpty(server
.db
[j
].dict
);
149 dictEmpty(server
.db
[j
].expires
);
154 int selectDb(redisClient
*c
, int id
) {
155 if (id
< 0 || id
>= server
.dbnum
)
157 c
->db
= &server
.db
[id
];
161 /*-----------------------------------------------------------------------------
162 * Hooks for key space changes.
164 * Every time a key in the database is modified the function
165 * signalModifiedKey() is called.
167 * Every time a DB is flushed the function signalFlushDb() is called.
168 *----------------------------------------------------------------------------*/
170 void signalModifiedKey(redisDb
*db
, robj
*key
) {
171 touchWatchedKey(db
,key
);
172 if (server
.ds_enabled
)
173 cacheScheduleForFlush(db
,key
);
176 void signalFlushedDb(int dbid
) {
177 touchWatchedKeysOnFlush(dbid
);
178 if (server
.ds_enabled
)
182 /*-----------------------------------------------------------------------------
183 * Type agnostic commands operating on the key space
184 *----------------------------------------------------------------------------*/
186 void flushdbCommand(redisClient
*c
) {
187 server
.dirty
+= dictSize(c
->db
->dict
);
188 signalFlushedDb(c
->db
->id
);
189 dictEmpty(c
->db
->dict
);
190 dictEmpty(c
->db
->expires
);
191 addReply(c
,shared
.ok
);
194 void flushallCommand(redisClient
*c
) {
196 server
.dirty
+= emptyDb();
197 addReply(c
,shared
.ok
);
198 if (server
.bgsavechildpid
!= -1) {
199 kill(server
.bgsavechildpid
,SIGKILL
);
200 rdbRemoveTempFile(server
.bgsavechildpid
);
202 rdbSave(server
.dbfilename
);
206 void delCommand(redisClient
*c
) {
209 for (j
= 1; j
< c
->argc
; j
++) {
210 if (server
.ds_enabled
) {
211 lookupKeyRead(c
->db
,c
->argv
[j
]);
212 /* FIXME: this can be optimized a lot, no real need to load
213 * a possibly huge value. */
215 if (dbDelete(c
->db
,c
->argv
[j
])) {
216 signalModifiedKey(c
->db
,c
->argv
[j
]);
219 } else if (server
.ds_enabled
) {
220 if (cacheKeyMayExist(c
->db
,c
->argv
[j
]) &&
221 dsExists(c
->db
,c
->argv
[j
]))
223 cacheScheduleForFlush(c
->db
,c
->argv
[j
]);
228 addReplyLongLong(c
,deleted
);
231 void existsCommand(redisClient
*c
) {
232 expireIfNeeded(c
->db
,c
->argv
[1]);
233 if (dbExists(c
->db
,c
->argv
[1])) {
234 addReply(c
, shared
.cone
);
236 addReply(c
, shared
.czero
);
240 void selectCommand(redisClient
*c
) {
241 int id
= atoi(c
->argv
[1]->ptr
);
243 if (selectDb(c
,id
) == REDIS_ERR
) {
244 addReplyError(c
,"invalid DB index");
246 addReply(c
,shared
.ok
);
250 void randomkeyCommand(redisClient
*c
) {
253 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
254 addReply(c
,shared
.nullbulk
);
262 void keysCommand(redisClient
*c
) {
265 sds pattern
= c
->argv
[1]->ptr
;
266 int plen
= sdslen(pattern
), allkeys
;
267 unsigned long numkeys
= 0;
268 void *replylen
= addDeferredMultiBulkLength(c
);
270 di
= dictGetIterator(c
->db
->dict
);
271 allkeys
= (pattern
[0] == '*' && pattern
[1] == '\0');
272 while((de
= dictNext(di
)) != NULL
) {
273 sds key
= dictGetEntryKey(de
);
276 if (allkeys
|| stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
277 keyobj
= createStringObject(key
,sdslen(key
));
278 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
279 addReplyBulk(c
,keyobj
);
282 decrRefCount(keyobj
);
285 dictReleaseIterator(di
);
286 setDeferredMultiBulkLength(c
,replylen
,numkeys
);
289 void dbsizeCommand(redisClient
*c
) {
290 addReplyLongLong(c
,dictSize(c
->db
->dict
));
293 void lastsaveCommand(redisClient
*c
) {
294 addReplyLongLong(c
,server
.lastsave
);
297 void typeCommand(redisClient
*c
) {
301 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
306 case REDIS_STRING
: type
= "string"; break;
307 case REDIS_LIST
: type
= "list"; break;
308 case REDIS_SET
: type
= "set"; break;
309 case REDIS_ZSET
: type
= "zset"; break;
310 case REDIS_HASH
: type
= "hash"; break;
311 default: type
= "unknown"; break;
314 addReplyStatus(c
,type
);
317 void saveCommand(redisClient
*c
) {
318 if (server
.bgsavechildpid
!= -1) {
319 addReplyError(c
,"Background save already in progress");
322 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
323 addReply(c
,shared
.ok
);
325 addReply(c
,shared
.err
);
329 void bgsaveCommand(redisClient
*c
) {
330 if (server
.bgsavechildpid
!= -1) {
331 addReplyError(c
,"Background save already in progress");
334 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
335 addReplyStatus(c
,"Background saving started");
337 addReply(c
,shared
.err
);
341 void shutdownCommand(redisClient
*c
) {
342 if (prepareForShutdown() == REDIS_OK
)
344 addReplyError(c
,"Errors trying to SHUTDOWN. Check logs.");
347 void renameGenericCommand(redisClient
*c
, int nx
) {
350 /* To use the same key as src and dst is probably an error */
351 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
352 addReply(c
,shared
.sameobjecterr
);
356 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
360 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
363 addReply(c
,shared
.czero
);
366 dbReplace(c
->db
,c
->argv
[2],o
);
368 dbDelete(c
->db
,c
->argv
[1]);
369 signalModifiedKey(c
->db
,c
->argv
[1]);
370 signalModifiedKey(c
->db
,c
->argv
[2]);
372 addReply(c
,nx
? shared
.cone
: shared
.ok
);
375 void renameCommand(redisClient
*c
) {
376 renameGenericCommand(c
,0);
379 void renamenxCommand(redisClient
*c
) {
380 renameGenericCommand(c
,1);
383 void moveCommand(redisClient
*c
) {
388 /* Obtain source and target DB pointers */
391 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
392 addReply(c
,shared
.outofrangeerr
);
396 selectDb(c
,srcid
); /* Back to the source DB */
398 /* If the user is moving using as target the same
399 * DB as the source DB it is probably an error. */
401 addReply(c
,shared
.sameobjecterr
);
405 /* Check if the element exists and get a reference */
406 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
408 addReply(c
,shared
.czero
);
412 /* Try to add the element to the target DB */
413 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
414 addReply(c
,shared
.czero
);
419 /* OK! key moved, free the entry in the source DB */
420 dbDelete(src
,c
->argv
[1]);
422 addReply(c
,shared
.cone
);
425 /*-----------------------------------------------------------------------------
427 *----------------------------------------------------------------------------*/
429 int removeExpire(redisDb
*db
, robj
*key
) {
430 /* An expire may only be removed if there is a corresponding entry in the
431 * main dict. Otherwise, the key will never be freed. */
432 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
433 return dictDelete(db
->expires
,key
->ptr
) == DICT_OK
;
436 void setExpire(redisDb
*db
, robj
*key
, time_t when
) {
439 /* Reuse the sds from the main dict in the expire dict */
440 de
= dictFind(db
->dict
,key
->ptr
);
441 redisAssert(de
!= NULL
);
442 dictReplace(db
->expires
,dictGetEntryKey(de
),(void*)when
);
445 /* Return the expire time of the specified key, or -1 if no expire
446 * is associated with this key (i.e. the key is non volatile) */
447 time_t getExpire(redisDb
*db
, robj
*key
) {
450 /* No expire? return ASAP */
451 if (dictSize(db
->expires
) == 0 ||
452 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
454 /* The entry was found in the expire dict, this means it should also
455 * be present in the main dict (safety check). */
456 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
457 return (time_t) dictGetEntryVal(de
);
460 /* Propagate expires into slaves and the AOF file.
461 * When a key expires in the master, a DEL operation for this key is sent
462 * to all the slaves and the AOF file if enabled.
464 * This way the key expiry is centralized in one place, and since both
465 * AOF and the master->slave link guarantee operation ordering, everything
466 * will be consistent even if we allow write operations against expiring
468 void propagateExpire(redisDb
*db
, robj
*key
) {
471 argv
[0] = createStringObject("DEL",3);
475 if (server
.appendonly
)
476 feedAppendOnlyFile(server
.delCommand
,db
->id
,argv
,2);
477 if (listLength(server
.slaves
))
478 replicationFeedSlaves(server
.slaves
,db
->id
,argv
,2);
480 decrRefCount(argv
[0]);
481 decrRefCount(argv
[1]);
484 int expireIfNeeded(redisDb
*db
, robj
*key
) {
485 time_t when
= getExpire(db
,key
);
487 /* If we are running in the context of a slave, return ASAP:
488 * the slave key expiration is controlled by the master that will
489 * send us synthesized DEL operations for expired keys.
491 * Still we try to return the right information to the caller,
492 * that is, 0 if we think the key should be still valid, 1 if
493 * we think the key is expired at this time. */
494 if (server
.masterhost
!= NULL
) {
495 return time(NULL
) > when
;
498 if (when
< 0) return 0;
500 /* Return when this key has not expired */
501 if (time(NULL
) <= when
) return 0;
504 server
.stat_expiredkeys
++;
505 propagateExpire(db
,key
);
506 return dbDelete(db
,key
);
509 /*-----------------------------------------------------------------------------
511 *----------------------------------------------------------------------------*/
513 void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
517 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
521 de
= dictFind(c
->db
->dict
,key
->ptr
);
523 addReply(c
,shared
.czero
);
527 if (dbDelete(c
->db
,key
)) server
.dirty
++;
528 addReply(c
, shared
.cone
);
529 signalModifiedKey(c
->db
,key
);
532 time_t when
= time(NULL
)+seconds
;
533 setExpire(c
->db
,key
,when
);
534 addReply(c
,shared
.cone
);
535 signalModifiedKey(c
->db
,key
);
541 void expireCommand(redisClient
*c
) {
542 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
545 void expireatCommand(redisClient
*c
) {
546 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
549 void ttlCommand(redisClient
*c
) {
550 time_t expire
, ttl
= -1;
552 expire
= getExpire(c
->db
,c
->argv
[1]);
554 ttl
= (expire
-time(NULL
));
555 if (ttl
< 0) ttl
= -1;
557 addReplyLongLong(c
,(long long)ttl
);
560 void persistCommand(redisClient
*c
) {
563 de
= dictFind(c
->db
->dict
,c
->argv
[1]->ptr
);
565 addReply(c
,shared
.czero
);
567 if (removeExpire(c
->db
,c
->argv
[1])) {
568 addReply(c
,shared
.cone
);
571 addReply(c
,shared
.czero
);