2010-06-21 18:07:48 -04:00
|
|
|
#include "redis.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* C-level DB API
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
robj *lookupKey(redisDb *db, robj *key) {
|
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
|
|
|
if (de) {
|
|
|
|
robj *val = dictGetEntryVal(de);
|
|
|
|
|
2010-11-02 13:59:48 -04:00
|
|
|
/* Update the access time for the aging algorithm.
|
|
|
|
* Don't do it if we have a saving child, as this will trigger
|
|
|
|
* a copy on write madness. */
|
|
|
|
if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1)
|
|
|
|
val->lru = server.lruclock;
|
2010-10-14 07:52:58 -04:00
|
|
|
|
2011-01-01 15:35:56 -05:00
|
|
|
if (server.ds_enabled &&
|
|
|
|
cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG)
|
|
|
|
{
|
|
|
|
/* There is a save in progress for this object!
|
|
|
|
* Wait for it to get out. */
|
2010-12-28 12:06:40 -05:00
|
|
|
waitEmptyIOJobsQueue();
|
2010-12-30 12:37:46 -05:00
|
|
|
processAllPendingIOJobs();
|
2011-01-01 15:35:56 -05:00
|
|
|
redisAssert(!(cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG));
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-10-15 06:19:21 -04:00
|
|
|
server.stat_keyspace_hits++;
|
2010-06-21 18:07:48 -04:00
|
|
|
return val;
|
|
|
|
} else {
|
2010-12-30 18:18:17 -05:00
|
|
|
time_t expire;
|
|
|
|
robj *val;
|
|
|
|
|
|
|
|
/* Key not found in the in memory hash table, but if disk store is
|
|
|
|
* enabled we may have this key on disk. If so load it in memory
|
2011-01-01 15:39:48 -05:00
|
|
|
* in a blocking way. */
|
2010-12-30 18:18:17 -05:00
|
|
|
if (server.ds_enabled && cacheKeyMayExist(db,key)) {
|
2011-01-02 15:39:17 -05:00
|
|
|
if (cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG) {
|
2011-01-01 15:39:48 -05:00
|
|
|
/* There is a save in progress for this object!
|
|
|
|
* Wait for it to get out. */
|
|
|
|
waitEmptyIOJobsQueue();
|
|
|
|
processAllPendingIOJobs();
|
2011-01-02 15:39:17 -05:00
|
|
|
redisAssert((cacheScheduleIOGetFlags(db,key) & REDIS_IO_SAVEINPROG) == 0);
|
2011-01-01 15:39:48 -05:00
|
|
|
}
|
|
|
|
|
2010-12-31 11:32:59 -05:00
|
|
|
redisLog(REDIS_DEBUG,"Force loading key %s via lookup",
|
|
|
|
key->ptr);
|
2010-12-30 18:18:17 -05:00
|
|
|
val = dsGet(db,key,&expire);
|
|
|
|
if (val) {
|
|
|
|
int retval = dbAdd(db,key,val);
|
|
|
|
redisAssert(retval == REDIS_OK);
|
|
|
|
if (expire != -1) setExpire(db,key,expire);
|
|
|
|
server.stat_keyspace_hits++;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 06:19:21 -04:00
|
|
|
server.stat_keyspace_misses++;
|
2010-06-21 18:07:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyRead(redisDb *db, robj *key) {
|
|
|
|
expireIfNeeded(db,key);
|
|
|
|
return lookupKey(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyWrite(redisDb *db, robj *key) {
|
2010-08-02 12:13:39 -04:00
|
|
|
expireIfNeeded(db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return lookupKey(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
|
|
|
|
robj *o = lookupKeyRead(c->db, key);
|
|
|
|
if (!o) addReply(c,reply);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
|
|
|
|
robj *o = lookupKeyWrite(c->db, key);
|
|
|
|
if (!o) addReply(c,reply);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the key to the DB. If the key already exists REDIS_ERR is returned,
|
|
|
|
* otherwise REDIS_OK is returned, and the caller should increment the
|
|
|
|
* refcount of 'val'. */
|
|
|
|
int dbAdd(redisDb *db, robj *key, robj *val) {
|
|
|
|
/* Perform a lookup before adding the key, as we need to copy the
|
|
|
|
* key value. */
|
|
|
|
if (dictFind(db->dict, key->ptr) != NULL) {
|
|
|
|
return REDIS_ERR;
|
|
|
|
} else {
|
|
|
|
sds copy = sdsdup(key->ptr);
|
|
|
|
dictAdd(db->dict, copy, val);
|
2010-12-30 10:41:36 -05:00
|
|
|
if (server.ds_enabled) {
|
|
|
|
/* FIXME: remove entry from negative cache */
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the key does not exist, this is just like dbAdd(). Otherwise
|
|
|
|
* the value associated to the key is replaced with the new one.
|
|
|
|
*
|
|
|
|
* On update (key already existed) 0 is returned. Otherwise 1. */
|
|
|
|
int dbReplace(redisDb *db, robj *key, robj *val) {
|
2010-12-31 08:30:24 -05:00
|
|
|
robj *oldval;
|
|
|
|
|
|
|
|
if ((oldval = dictFetchValue(db->dict,key->ptr)) == NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
sds copy = sdsdup(key->ptr);
|
|
|
|
dictAdd(db->dict, copy, val);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
dictReplace(db->dict, key->ptr, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int dbExists(redisDb *db, robj *key) {
|
|
|
|
return dictFind(db->dict,key->ptr) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a random key, in form of a Redis object.
|
|
|
|
* If there are no keys, NULL is returned.
|
|
|
|
*
|
|
|
|
* The function makes sure to return keys not already expired. */
|
|
|
|
robj *dbRandomKey(redisDb *db) {
|
|
|
|
struct dictEntry *de;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
sds key;
|
|
|
|
robj *keyobj;
|
|
|
|
|
|
|
|
de = dictGetRandomKey(db->dict);
|
|
|
|
if (de == NULL) return NULL;
|
|
|
|
|
|
|
|
key = dictGetEntryKey(de);
|
|
|
|
keyobj = createStringObject(key,sdslen(key));
|
|
|
|
if (dictFind(db->expires,key)) {
|
|
|
|
if (expireIfNeeded(db,keyobj)) {
|
|
|
|
decrRefCount(keyobj);
|
|
|
|
continue; /* search for another key. This expired. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keyobj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a key, value, and associated expiration entry if any, from the DB */
|
|
|
|
int dbDelete(redisDb *db, robj *key) {
|
2010-12-31 11:32:59 -05:00
|
|
|
/* If diskstore is enabled make sure to awake waiting clients for this key
|
|
|
|
* as it is not really useful to wait for a key already deleted to be
|
|
|
|
* loaded from disk. */
|
2010-12-28 12:06:40 -05:00
|
|
|
if (server.ds_enabled) handleClientsBlockedOnSwappedKey(db,key);
|
|
|
|
|
2011-01-01 15:35:56 -05:00
|
|
|
/* FIXME: we should mark this key as non existing on disk in the negative
|
|
|
|
* cache. */
|
2010-12-28 12:06:40 -05:00
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
/* Deleting an entry from the expires dict will not free the sds of
|
|
|
|
* the key, because it is shared with the main dictionary. */
|
|
|
|
if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
|
|
|
|
return dictDelete(db->dict,key->ptr) == DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty the whole database */
|
|
|
|
long long emptyDb() {
|
|
|
|
int j;
|
|
|
|
long long removed = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < server.dbnum; j++) {
|
|
|
|
removed += dictSize(server.db[j].dict);
|
|
|
|
dictEmpty(server.db[j].dict);
|
|
|
|
dictEmpty(server.db[j].expires);
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
int selectDb(redisClient *c, int id) {
|
|
|
|
if (id < 0 || id >= server.dbnum)
|
|
|
|
return REDIS_ERR;
|
|
|
|
c->db = &server.db[id];
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-29 13:39:42 -05:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Hooks for key space changes.
|
|
|
|
*
|
|
|
|
* Every time a key in the database is modified the function
|
|
|
|
* signalModifiedKey() is called.
|
|
|
|
*
|
|
|
|
* Every time a DB is flushed the function signalFlushDb() is called.
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void signalModifiedKey(redisDb *db, robj *key) {
|
|
|
|
touchWatchedKey(db,key);
|
|
|
|
if (server.ds_enabled)
|
2011-01-01 15:35:56 -05:00
|
|
|
cacheScheduleIO(db,key,REDIS_IO_SAVE);
|
2010-12-29 13:39:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void signalFlushedDb(int dbid) {
|
|
|
|
touchWatchedKeysOnFlush(dbid);
|
|
|
|
if (server.ds_enabled)
|
|
|
|
dsFlushDb(dbid);
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Type agnostic commands operating on the key space
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void flushdbCommand(redisClient *c) {
|
|
|
|
server.dirty += dictSize(c->db->dict);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalFlushedDb(c->db->id);
|
2010-06-21 18:07:48 -04:00
|
|
|
dictEmpty(c->db->dict);
|
|
|
|
dictEmpty(c->db->expires);
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flushallCommand(redisClient *c) {
|
2010-12-29 13:39:42 -05:00
|
|
|
signalFlushedDb(-1);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty += emptyDb();
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
if (server.bgsavechildpid != -1) {
|
|
|
|
kill(server.bgsavechildpid,SIGKILL);
|
|
|
|
rdbRemoveTempFile(server.bgsavechildpid);
|
|
|
|
}
|
|
|
|
rdbSave(server.dbfilename);
|
|
|
|
server.dirty++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delCommand(redisClient *c) {
|
|
|
|
int deleted = 0, j;
|
|
|
|
|
|
|
|
for (j = 1; j < c->argc; j++) {
|
2010-12-30 13:16:59 -05:00
|
|
|
if (server.ds_enabled) {
|
|
|
|
lookupKeyRead(c->db,c->argv[j]);
|
|
|
|
/* FIXME: this can be optimized a lot, no real need to load
|
|
|
|
* a possibly huge value. */
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
if (dbDelete(c->db,c->argv[j])) {
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,c->argv[j]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
deleted++;
|
2010-12-30 13:16:59 -05:00
|
|
|
} else if (server.ds_enabled) {
|
|
|
|
if (cacheKeyMayExist(c->db,c->argv[j]) &&
|
|
|
|
dsExists(c->db,c->argv[j]))
|
|
|
|
{
|
2011-01-01 15:35:56 -05:00
|
|
|
cacheScheduleIO(c->db,c->argv[j],REDIS_IO_SAVE);
|
2010-12-30 13:16:59 -05:00
|
|
|
deleted = 1;
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addReplyLongLong(c,deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void existsCommand(redisClient *c) {
|
|
|
|
expireIfNeeded(c->db,c->argv[1]);
|
|
|
|
if (dbExists(c->db,c->argv[1])) {
|
|
|
|
addReply(c, shared.cone);
|
|
|
|
} else {
|
|
|
|
addReply(c, shared.czero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectCommand(redisClient *c) {
|
|
|
|
int id = atoi(c->argv[1]->ptr);
|
|
|
|
|
|
|
|
if (selectDb(c,id) == REDIS_ERR) {
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyError(c,"invalid DB index");
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomkeyCommand(redisClient *c) {
|
|
|
|
robj *key;
|
|
|
|
|
|
|
|
if ((key = dbRandomKey(c->db)) == NULL) {
|
|
|
|
addReply(c,shared.nullbulk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addReplyBulk(c,key);
|
|
|
|
decrRefCount(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void keysCommand(redisClient *c) {
|
|
|
|
dictIterator *di;
|
|
|
|
dictEntry *de;
|
|
|
|
sds pattern = c->argv[1]->ptr;
|
2010-08-30 05:51:45 -04:00
|
|
|
int plen = sdslen(pattern), allkeys;
|
2010-06-21 18:07:48 -04:00
|
|
|
unsigned long numkeys = 0;
|
2010-08-30 10:02:06 -04:00
|
|
|
void *replylen = addDeferredMultiBulkLength(c);
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
di = dictGetIterator(c->db->dict);
|
2010-08-30 05:51:45 -04:00
|
|
|
allkeys = (pattern[0] == '*' && pattern[1] == '\0');
|
2010-06-21 18:07:48 -04:00
|
|
|
while((de = dictNext(di)) != NULL) {
|
|
|
|
sds key = dictGetEntryKey(de);
|
|
|
|
robj *keyobj;
|
|
|
|
|
2010-08-30 05:51:45 -04:00
|
|
|
if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
keyobj = createStringObject(key,sdslen(key));
|
|
|
|
if (expireIfNeeded(c->db,keyobj) == 0) {
|
|
|
|
addReplyBulk(c,keyobj);
|
|
|
|
numkeys++;
|
|
|
|
}
|
|
|
|
decrRefCount(keyobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dictReleaseIterator(di);
|
2010-08-30 10:02:06 -04:00
|
|
|
setDeferredMultiBulkLength(c,replylen,numkeys);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void dbsizeCommand(redisClient *c) {
|
2010-09-02 08:30:56 -04:00
|
|
|
addReplyLongLong(c,dictSize(c->db->dict));
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void lastsaveCommand(redisClient *c) {
|
2010-09-02 08:30:56 -04:00
|
|
|
addReplyLongLong(c,server.lastsave);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void typeCommand(redisClient *c) {
|
|
|
|
robj *o;
|
|
|
|
char *type;
|
|
|
|
|
|
|
|
o = lookupKeyRead(c->db,c->argv[1]);
|
|
|
|
if (o == NULL) {
|
2010-09-02 13:52:24 -04:00
|
|
|
type = "none";
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
switch(o->type) {
|
2010-09-02 13:52:24 -04:00
|
|
|
case REDIS_STRING: type = "string"; break;
|
|
|
|
case REDIS_LIST: type = "list"; break;
|
|
|
|
case REDIS_SET: type = "set"; break;
|
|
|
|
case REDIS_ZSET: type = "zset"; break;
|
|
|
|
case REDIS_HASH: type = "hash"; break;
|
|
|
|
default: type = "unknown"; break;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
}
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyStatus(c,type);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void saveCommand(redisClient *c) {
|
|
|
|
if (server.bgsavechildpid != -1) {
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyError(c,"Background save already in progress");
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rdbSave(server.dbfilename) == REDIS_OK) {
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bgsaveCommand(redisClient *c) {
|
|
|
|
if (server.bgsavechildpid != -1) {
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyError(c,"Background save already in progress");
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyStatus(c,"Background saving started");
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
addReply(c,shared.err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdownCommand(redisClient *c) {
|
|
|
|
if (prepareForShutdown() == REDIS_OK)
|
|
|
|
exit(0);
|
2010-09-02 13:52:24 -04:00
|
|
|
addReplyError(c,"Errors trying to SHUTDOWN. Check logs.");
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void renameGenericCommand(redisClient *c, int nx) {
|
|
|
|
robj *o;
|
|
|
|
|
|
|
|
/* To use the same key as src and dst is probably an error */
|
|
|
|
if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
|
|
|
|
addReply(c,shared.sameobjecterr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
incrRefCount(o);
|
|
|
|
if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) {
|
|
|
|
if (nx) {
|
|
|
|
decrRefCount(o);
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbReplace(c->db,c->argv[2],o);
|
|
|
|
}
|
|
|
|
dbDelete(c->db,c->argv[1]);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,c->argv[1]);
|
|
|
|
signalModifiedKey(c->db,c->argv[2]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
addReply(c,nx ? shared.cone : shared.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void renameCommand(redisClient *c) {
|
|
|
|
renameGenericCommand(c,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void renamenxCommand(redisClient *c) {
|
|
|
|
renameGenericCommand(c,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void moveCommand(redisClient *c) {
|
|
|
|
robj *o;
|
|
|
|
redisDb *src, *dst;
|
|
|
|
int srcid;
|
|
|
|
|
|
|
|
/* Obtain source and target DB pointers */
|
|
|
|
src = c->db;
|
|
|
|
srcid = c->db->id;
|
|
|
|
if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
|
|
|
|
addReply(c,shared.outofrangeerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst = c->db;
|
|
|
|
selectDb(c,srcid); /* Back to the source DB */
|
|
|
|
|
|
|
|
/* If the user is moving using as target the same
|
|
|
|
* DB as the source DB it is probably an error. */
|
|
|
|
if (src == dst) {
|
|
|
|
addReply(c,shared.sameobjecterr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the element exists and get a reference */
|
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
if (!o) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to add the element to the target DB */
|
|
|
|
if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
incrRefCount(o);
|
|
|
|
|
|
|
|
/* OK! key moved, free the entry in the source DB */
|
|
|
|
dbDelete(src,c->argv[1]);
|
|
|
|
server.dirty++;
|
|
|
|
addReply(c,shared.cone);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Expires API
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
int removeExpire(redisDb *db, robj *key) {
|
|
|
|
/* An expire may only be removed if there is a corresponding entry in the
|
|
|
|
* main dict. Otherwise, the key will never be freed. */
|
|
|
|
redisAssert(dictFind(db->dict,key->ptr) != NULL);
|
2010-08-03 08:19:20 -04:00
|
|
|
return dictDelete(db->expires,key->ptr) == DICT_OK;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 06:26:30 -04:00
|
|
|
void setExpire(redisDb *db, robj *key, time_t when) {
|
2010-06-21 18:07:48 -04:00
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
/* Reuse the sds from the main dict in the expire dict */
|
2010-08-03 06:26:30 -04:00
|
|
|
de = dictFind(db->dict,key->ptr);
|
|
|
|
redisAssert(de != NULL);
|
|
|
|
dictReplace(db->expires,dictGetEntryKey(de),(void*)when);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the expire time of the specified key, or -1 if no expire
|
|
|
|
* is associated with this key (i.e. the key is non volatile) */
|
|
|
|
time_t getExpire(redisDb *db, robj *key) {
|
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
/* No expire? return ASAP */
|
|
|
|
if (dictSize(db->expires) == 0 ||
|
|
|
|
(de = dictFind(db->expires,key->ptr)) == NULL) return -1;
|
|
|
|
|
|
|
|
/* The entry was found in the expire dict, this means it should also
|
|
|
|
* be present in the main dict (safety check). */
|
|
|
|
redisAssert(dictFind(db->dict,key->ptr) != NULL);
|
|
|
|
return (time_t) dictGetEntryVal(de);
|
|
|
|
}
|
|
|
|
|
2010-08-02 12:13:39 -04:00
|
|
|
/* Propagate expires into slaves and the AOF file.
|
|
|
|
* When a key expires in the master, a DEL operation for this key is sent
|
|
|
|
* to all the slaves and the AOF file if enabled.
|
|
|
|
*
|
|
|
|
* This way the key expiry is centralized in one place, and since both
|
|
|
|
* AOF and the master->slave link guarantee operation ordering, everything
|
|
|
|
* will be consistent even if we allow write operations against expiring
|
|
|
|
* keys. */
|
|
|
|
void propagateExpire(redisDb *db, robj *key) {
|
|
|
|
robj *argv[2];
|
|
|
|
|
|
|
|
argv[0] = createStringObject("DEL",3);
|
|
|
|
argv[1] = key;
|
|
|
|
incrRefCount(key);
|
|
|
|
|
|
|
|
if (server.appendonly)
|
2010-11-03 06:23:59 -04:00
|
|
|
feedAppendOnlyFile(server.delCommand,db->id,argv,2);
|
2010-08-02 12:13:39 -04:00
|
|
|
if (listLength(server.slaves))
|
|
|
|
replicationFeedSlaves(server.slaves,db->id,argv,2);
|
|
|
|
|
2010-08-02 15:37:39 -04:00
|
|
|
decrRefCount(argv[0]);
|
|
|
|
decrRefCount(argv[1]);
|
2010-08-02 12:13:39 -04:00
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
int expireIfNeeded(redisDb *db, robj *key) {
|
|
|
|
time_t when = getExpire(db,key);
|
2010-08-02 12:13:39 -04:00
|
|
|
|
|
|
|
/* If we are running in the context of a slave, return ASAP:
|
|
|
|
* the slave key expiration is controlled by the master that will
|
|
|
|
* send us synthesized DEL operations for expired keys.
|
|
|
|
*
|
|
|
|
* Still we try to return the right information to the caller,
|
|
|
|
* that is, 0 if we think the key should be still valid, 1 if
|
|
|
|
* we think the key is expired at this time. */
|
|
|
|
if (server.masterhost != NULL) {
|
|
|
|
return time(NULL) > when;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
if (when < 0) return 0;
|
|
|
|
|
|
|
|
/* Return when this key has not expired */
|
|
|
|
if (time(NULL) <= when) return 0;
|
|
|
|
|
|
|
|
/* Delete the key */
|
|
|
|
server.stat_expiredkeys++;
|
2010-08-02 12:13:39 -04:00
|
|
|
propagateExpire(db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return dbDelete(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Expires Commands
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
|
|
|
|
dictEntry *de;
|
2010-10-07 10:21:35 -04:00
|
|
|
long seconds;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
|
|
|
|
|
|
|
|
seconds -= offset;
|
|
|
|
|
|
|
|
de = dictFind(c->db->dict,key->ptr);
|
|
|
|
if (de == NULL) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (seconds <= 0) {
|
|
|
|
if (dbDelete(c->db,key)) server.dirty++;
|
|
|
|
addReply(c, shared.cone);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
time_t when = time(NULL)+seconds;
|
2010-08-03 06:26:30 -04:00
|
|
|
setExpire(c->db,key,when);
|
|
|
|
addReply(c,shared.cone);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,key);
|
2010-08-03 06:26:30 -04:00
|
|
|
server.dirty++;
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expireCommand(redisClient *c) {
|
|
|
|
expireGenericCommand(c,c->argv[1],c->argv[2],0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expireatCommand(redisClient *c) {
|
|
|
|
expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ttlCommand(redisClient *c) {
|
2010-08-23 11:06:38 -04:00
|
|
|
time_t expire, ttl = -1;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
expire = getExpire(c->db,c->argv[1]);
|
|
|
|
if (expire != -1) {
|
2010-08-23 11:06:38 -04:00
|
|
|
ttl = (expire-time(NULL));
|
2010-06-21 18:07:48 -04:00
|
|
|
if (ttl < 0) ttl = -1;
|
|
|
|
}
|
2010-08-23 11:06:38 -04:00
|
|
|
addReplyLongLong(c,(long long)ttl);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-08-03 08:19:20 -04:00
|
|
|
|
|
|
|
void persistCommand(redisClient *c) {
|
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
de = dictFind(c->db->dict,c->argv[1]->ptr);
|
|
|
|
if (de == NULL) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
} else {
|
2010-08-03 08:25:22 -04:00
|
|
|
if (removeExpire(c->db,c->argv[1])) {
|
2010-08-03 08:19:20 -04:00
|
|
|
addReply(c,shared.cone);
|
2010-08-03 08:25:22 -04:00
|
|
|
server.dirty++;
|
|
|
|
} else {
|
2010-08-03 08:19:20 -04:00
|
|
|
addReply(c,shared.czero);
|
2010-08-03 08:25:22 -04:00
|
|
|
}
|
2010-08-03 08:19:20 -04:00
|
|
|
}
|
|
|
|
}
|