2012-11-08 12:25:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
#include "redis.h"
|
2013-10-09 09:37:20 -04:00
|
|
|
#include "cluster.h"
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
#include <signal.h>
|
2011-11-09 10:51:19 -05:00
|
|
|
#include <ctype.h>
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2013-02-22 04:16:21 -05:00
|
|
|
void slotToKeyAdd(robj *key);
|
|
|
|
void slotToKeyDel(robj *key);
|
|
|
|
void slotToKeyFlush(void);
|
2011-04-28 13:00:33 -04:00
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* C-level DB API
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
robj *lookupKey(redisDb *db, robj *key) {
|
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
|
|
|
if (de) {
|
2011-11-08 11:07:55 -05:00
|
|
|
robj *val = dictGetVal(de);
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2013-01-16 12:00:20 -05:00
|
|
|
/* Update the access time for the ageing algorithm.
|
2010-11-02 13:59:48 -04:00
|
|
|
* Don't do it if we have a saving child, as this will trigger
|
|
|
|
* a copy on write madness. */
|
2011-12-21 06:22:13 -05:00
|
|
|
if (server.rdb_child_pid == -1 && server.aof_child_pid == -1)
|
2014-03-20 06:47:12 -04:00
|
|
|
val->lru = LRU_CLOCK();
|
2010-06-21 18:07:48 -04:00
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyRead(redisDb *db, robj *key) {
|
2012-02-01 15:47:41 -05:00
|
|
|
robj *val;
|
|
|
|
|
Better read-only behavior for expired keys in slaves.
Slaves key expire is orchestrated by the master. Sometimes the master
will send the synthesized DEL to expire keys on the slave with a non
trivial delay (when the key is not accessed, only the incremental expiry
algorithm will expire it in background).
During that time, a key is logically expired, but slaves still return
the key if you GET (or whatever) it. This is a bad behavior.
However we can't simply trust the slave view of the key, since we need
the master to be able to send write commands to update the slave data
set, and DELs should only happen when the key is expired in the master
in order to ensure consistency.
However 99.99% of the issues with this behavior is when a client which
is not a master sends a read only command. In this case we are safe and
can consider the key as non existing.
This commit does a few changes in order to make this sane:
1. lookupKeyRead() is modified in order to return NULL if the above
conditions are met.
2. Calls to lookupKeyRead() in commands actually writing to the data set
are repliaced with calls to lookupKeyWrite().
There are redundand checks, so for example, if in "2" something was
overlooked, we should be still safe, since anyway, when the master
writes the behavior is to don't care about what expireIfneeded()
returns.
This commit is related to #1768, #1770, #2131.
2014-12-10 10:10:21 -05:00
|
|
|
if (expireIfNeeded(db,key) == 1) {
|
|
|
|
/* Key expired. If we are in the context of a master, expireIfNeeded()
|
|
|
|
* returns 0 only when the key does not exist at all, so it's save
|
|
|
|
* to return NULL ASAP. */
|
|
|
|
if (server.masterhost == NULL) return NULL;
|
|
|
|
|
|
|
|
/* However if we are in the context of a slave, expireIfNeeded() will
|
|
|
|
* not really try to expire the key, it only returns information
|
|
|
|
* about the "logical" status of the key: key expiring is up to the
|
|
|
|
* master in order to have a consistent view of master's data set.
|
|
|
|
*
|
|
|
|
* However, if the command caller is not the master, and as additional
|
|
|
|
* safety measure, the command invoked is a read-only command, we can
|
|
|
|
* safely return NULL here, and provide a more consistent behavior
|
|
|
|
* to clients accessign expired values in a read-only fashion, that
|
|
|
|
* will say the key as non exisitng.
|
|
|
|
*
|
|
|
|
* Notably this covers GETs when slaves are used to scale reads. */
|
|
|
|
if (server.current_client &&
|
|
|
|
server.current_client != server.master &&
|
|
|
|
server.current_client->cmd &&
|
|
|
|
server.current_client->cmd->flags & REDIS_CMD_READONLY)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 15:47:41 -05:00
|
|
|
val = lookupKey(db,key);
|
|
|
|
if (val == NULL)
|
|
|
|
server.stat_keyspace_misses++;
|
|
|
|
else
|
|
|
|
server.stat_keyspace_hits++;
|
|
|
|
return val;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:34:27 -04:00
|
|
|
/* Add the key to the DB. It's up to the caller to increment the reference
|
2013-01-16 12:00:20 -05:00
|
|
|
* counter of the value if needed.
|
2011-06-14 09:34:27 -04:00
|
|
|
*
|
|
|
|
* The program is aborted if the key already exists. */
|
|
|
|
void dbAdd(redisDb *db, robj *key, robj *val) {
|
|
|
|
sds copy = sdsdup(key->ptr);
|
|
|
|
int retval = dictAdd(db->dict, copy, val);
|
|
|
|
|
2011-10-04 12:43:03 -04:00
|
|
|
redisAssertWithInfo(NULL,key,retval == REDIS_OK);
|
2014-04-07 21:22:30 -04:00
|
|
|
if (val->type == REDIS_LIST) signalListAsReady(db, key);
|
2013-02-22 04:16:21 -05:00
|
|
|
if (server.cluster_enabled) slotToKeyAdd(key);
|
2011-06-14 09:34:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Overwrite an existing key with a new value. Incrementing the reference
|
|
|
|
* count of the new value is up to the caller.
|
|
|
|
* This function does not modify the expire time of the existing key.
|
|
|
|
*
|
|
|
|
* The program is aborted if the key was not already present. */
|
|
|
|
void dbOverwrite(redisDb *db, robj *key, robj *val) {
|
2014-03-20 11:20:37 -04:00
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2011-10-04 12:43:03 -04:00
|
|
|
redisAssertWithInfo(NULL,key,de != NULL);
|
2011-06-14 09:34:27 -04:00
|
|
|
dictReplace(db->dict, key->ptr, val);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:34:27 -04:00
|
|
|
/* High level Set operation. This function can be used in order to set
|
|
|
|
* a key, whatever it was existing or not, to a new object.
|
2010-06-21 18:07:48 -04:00
|
|
|
*
|
2011-06-14 09:34:27 -04:00
|
|
|
* 1) The ref count of the value object is incremented.
|
|
|
|
* 2) clients WATCHing for the destination key notified.
|
|
|
|
* 3) The expire time of the key is reset (the key is made persistent). */
|
|
|
|
void setKey(redisDb *db, robj *key, robj *val) {
|
|
|
|
if (lookupKeyWrite(db,key) == NULL) {
|
|
|
|
dbAdd(db,key,val);
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
2011-06-14 09:34:27 -04:00
|
|
|
dbOverwrite(db,key,val);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2011-06-14 09:34:27 -04:00
|
|
|
incrRefCount(val);
|
|
|
|
removeExpire(db,key);
|
2012-01-30 04:27:50 -05:00
|
|
|
signalModifiedKey(db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-03-20 11:20:37 -04:00
|
|
|
dictEntry *de;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
sds key;
|
|
|
|
robj *keyobj;
|
|
|
|
|
|
|
|
de = dictGetRandomKey(db->dict);
|
|
|
|
if (de == NULL) return NULL;
|
|
|
|
|
2011-11-08 11:07:55 -05:00
|
|
|
key = dictGetKey(de);
|
2010-06-21 18:07:48 -04:00
|
|
|
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) {
|
|
|
|
/* 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);
|
2011-04-28 13:00:33 -04:00
|
|
|
if (dictDelete(db->dict,key->ptr) == DICT_OK) {
|
2013-02-22 04:16:21 -05:00
|
|
|
if (server.cluster_enabled) slotToKeyDel(key);
|
2011-04-28 13:00:33 -04:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2014-03-30 12:32:17 -04:00
|
|
|
/* Prepare the string object stored at 'key' to be modified destructively
|
|
|
|
* to implement commands like SETBIT or APPEND.
|
|
|
|
*
|
|
|
|
* An object is usually ready to be modified unless one of the two conditions
|
|
|
|
* are true:
|
|
|
|
*
|
|
|
|
* 1) The object 'o' is shared (refcount > 1), we don't want to affect
|
|
|
|
* other users.
|
|
|
|
* 2) The object encoding is not "RAW".
|
|
|
|
*
|
|
|
|
* If the object is found in one of the above conditions (or both) by the
|
|
|
|
* function, an unshared / not-encoded copy of the string object is stored
|
|
|
|
* at 'key' in the specified 'db'. Otherwise the object 'o' itself is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* USAGE:
|
|
|
|
*
|
|
|
|
* The object 'o' is what the caller already obtained by looking up 'key'
|
|
|
|
* in 'db', the usage pattern looks like this:
|
|
|
|
*
|
|
|
|
* o = lookupKeyWrite(db,key);
|
|
|
|
* if (checkType(c,o,REDIS_STRING)) return;
|
|
|
|
* o = dbUnshareStringValue(db,key,o);
|
|
|
|
*
|
|
|
|
* At this point the caller is ready to modify the object, for example
|
|
|
|
* using an sdscat() call to append some data, or anything else.
|
|
|
|
*/
|
|
|
|
robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) {
|
|
|
|
redisAssert(o->type == REDIS_STRING);
|
|
|
|
if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
|
|
|
|
robj *decoded = getDecodedObject(o);
|
|
|
|
o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr));
|
|
|
|
decrRefCount(decoded);
|
|
|
|
dbOverwrite(db,key,o);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2013-12-10 12:18:24 -05:00
|
|
|
long long emptyDb(void(callback)(void*)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
int j;
|
|
|
|
long long removed = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < server.dbnum; j++) {
|
|
|
|
removed += dictSize(server.db[j].dict);
|
2013-12-10 12:18:24 -05:00
|
|
|
dictEmpty(server.db[j].dict,callback);
|
|
|
|
dictEmpty(server.db[j].expires,callback);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2013-03-21 12:13:08 -04:00
|
|
|
if (server.cluster_enabled) slotToKeyFlush();
|
2010-06-21 18:07:48 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void signalFlushedDb(int dbid) {
|
|
|
|
touchWatchedKeysOnFlush(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);
|
2013-12-10 12:18:24 -05:00
|
|
|
dictEmpty(c->db->dict,NULL);
|
|
|
|
dictEmpty(c->db->expires,NULL);
|
2013-02-22 04:16:21 -05:00
|
|
|
if (server.cluster_enabled) slotToKeyFlush();
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flushallCommand(redisClient *c) {
|
2010-12-29 13:39:42 -05:00
|
|
|
signalFlushedDb(-1);
|
2013-12-10 12:18:24 -05:00
|
|
|
server.dirty += emptyDb(NULL);
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.ok);
|
2011-12-21 06:22:13 -05:00
|
|
|
if (server.rdb_child_pid != -1) {
|
2013-01-14 04:29:14 -05:00
|
|
|
kill(server.rdb_child_pid,SIGUSR1);
|
2011-12-21 06:22:13 -05:00
|
|
|
rdbRemoveTempFile(server.rdb_child_pid);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2011-10-17 04:28:57 -04:00
|
|
|
if (server.saveparamslen > 0) {
|
|
|
|
/* Normally rdbSave() will reset dirty, but we don't want this here
|
|
|
|
* as otherwise FLUSHALL will not be replicated nor put into the AOF. */
|
|
|
|
int saved_dirty = server.dirty;
|
2011-12-21 06:22:13 -05:00
|
|
|
rdbSave(server.rdb_filename);
|
2011-10-17 04:28:57 -04:00
|
|
|
server.dirty = saved_dirty;
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delCommand(redisClient *c) {
|
|
|
|
int deleted = 0, j;
|
|
|
|
|
|
|
|
for (j = 1; j < c->argc; j++) {
|
2014-04-10 17:08:02 -04:00
|
|
|
expireIfNeeded(c->db,c->argv[j]);
|
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]);
|
2013-01-25 07:19:08 -05:00
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,
|
|
|
|
"del",c->argv[j],c->db->id);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2012-09-11 04:32:04 -04:00
|
|
|
long id;
|
|
|
|
|
|
|
|
if (getLongFromObjectOrReply(c, c->argv[1], &id,
|
|
|
|
"invalid DB index") != REDIS_OK)
|
|
|
|
return;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2011-05-05 12:10:02 -04:00
|
|
|
if (server.cluster_enabled && id != 0) {
|
2011-03-29 11:51:15 -04:00
|
|
|
addReplyError(c,"SELECT is not allowed in cluster mode");
|
|
|
|
return;
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
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
|
|
|
|
2012-04-30 13:16:20 -04:00
|
|
|
di = dictGetSafeIterator(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) {
|
2011-11-08 11:07:55 -05:00
|
|
|
sds key = dictGetKey(de);
|
2010-06-21 18:07:48 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* This callback is used by scanGenericCommand in order to collect elements
|
|
|
|
* returned by the dictionary iterator into a list. */
|
2012-07-09 04:00:26 -04:00
|
|
|
void scanCallback(void *privdata, const dictEntry *de) {
|
2013-10-28 06:11:34 -04:00
|
|
|
void **pd = (void**) privdata;
|
|
|
|
list *keys = pd[0];
|
|
|
|
robj *o = pd[1];
|
|
|
|
robj *key, *val = NULL;
|
|
|
|
|
|
|
|
if (o == NULL) {
|
|
|
|
sds sdskey = dictGetKey(de);
|
|
|
|
key = createStringObject(sdskey, sdslen(sdskey));
|
|
|
|
} else if (o->type == REDIS_SET) {
|
|
|
|
key = dictGetKey(de);
|
|
|
|
incrRefCount(key);
|
|
|
|
} else if (o->type == REDIS_HASH) {
|
|
|
|
key = dictGetKey(de);
|
|
|
|
incrRefCount(key);
|
|
|
|
val = dictGetVal(de);
|
|
|
|
incrRefCount(val);
|
|
|
|
} else if (o->type == REDIS_ZSET) {
|
|
|
|
key = dictGetKey(de);
|
|
|
|
incrRefCount(key);
|
2014-12-02 12:19:30 -05:00
|
|
|
val = createStringObjectFromLongDouble(*(double*)dictGetVal(de),0);
|
2013-10-28 06:11:34 -04:00
|
|
|
} else {
|
|
|
|
redisPanic("Type not handled in SCAN callback.");
|
|
|
|
}
|
|
|
|
|
|
|
|
listAddNodeTail(keys, key);
|
|
|
|
if (val) listAddNodeTail(keys, val);
|
2012-07-09 04:00:26 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 11:23:11 -05:00
|
|
|
/* Try to parse a SCAN cursor stored at object 'o':
|
2013-11-05 09:47:50 -05:00
|
|
|
* if the cursor is valid, store it as unsigned integer into *cursor and
|
|
|
|
* returns REDIS_OK. Otherwise return REDIS_ERR and send an error to the
|
|
|
|
* client. */
|
|
|
|
int parseScanCursorOrReply(redisClient *c, robj *o, unsigned long *cursor) {
|
|
|
|
char *eptr;
|
|
|
|
|
|
|
|
/* Use strtoul() because we need an *unsigned* long, so
|
|
|
|
* getLongLongFromObject() does not cover the whole cursor space. */
|
|
|
|
errno = 0;
|
|
|
|
*cursor = strtoul(o->ptr, &eptr, 10);
|
|
|
|
if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || errno == ERANGE)
|
|
|
|
{
|
|
|
|
addReplyError(c, "invalid cursor");
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* This command implements SCAN, HSCAN and SSCAN commands.
|
2013-12-05 10:35:32 -05:00
|
|
|
* If object 'o' is passed, then it must be a Hash or Set object, otherwise
|
2013-10-28 06:11:34 -04:00
|
|
|
* if 'o' is NULL the command will operate on the dictionary associated with
|
|
|
|
* the current database.
|
|
|
|
*
|
|
|
|
* When 'o' is not NULL the function assumes that the first argument in
|
|
|
|
* the client arguments vector is a key so it skips it before iterating
|
|
|
|
* in order to parse options.
|
|
|
|
*
|
2013-12-05 10:35:32 -05:00
|
|
|
* In the case of a Hash object the function returns both the field and value
|
2013-10-28 06:11:34 -04:00
|
|
|
* of every element on the Hash. */
|
2013-11-05 09:47:50 -05:00
|
|
|
void scanGenericCommand(redisClient *c, robj *o, unsigned long cursor) {
|
2012-07-09 04:00:26 -04:00
|
|
|
int i, j;
|
|
|
|
list *keys = listCreate();
|
2013-10-25 05:54:37 -04:00
|
|
|
listNode *node, *nextnode;
|
2013-10-28 06:11:34 -04:00
|
|
|
long count = 10;
|
2014-12-11 22:02:39 -05:00
|
|
|
sds pat = NULL;
|
|
|
|
int patlen = 0, use_pattern = 0;
|
2013-10-28 06:11:34 -04:00
|
|
|
dict *ht;
|
|
|
|
|
|
|
|
/* Object must be NULL (to iterate keys names), or the type of the object
|
|
|
|
* must be Set, Sorted Set, or Hash. */
|
|
|
|
redisAssert(o == NULL || o->type == REDIS_SET || o->type == REDIS_HASH ||
|
|
|
|
o->type == REDIS_ZSET);
|
|
|
|
|
|
|
|
/* Set i to the first option argument. The previous one is the cursor. */
|
|
|
|
i = (o == NULL) ? 2 : 3; /* Skip the key argument if needed. */
|
2012-07-09 04:00:26 -04:00
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* Step 1: Parse options. */
|
2012-07-09 04:00:26 -04:00
|
|
|
while (i < c->argc) {
|
|
|
|
j = c->argc - i;
|
|
|
|
if (!strcasecmp(c->argv[i]->ptr, "count") && j >= 2) {
|
2013-10-25 06:01:49 -04:00
|
|
|
if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL)
|
|
|
|
!= REDIS_OK)
|
|
|
|
{
|
2012-07-09 04:00:26 -04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < 1) {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 2;
|
2013-10-25 05:45:32 -04:00
|
|
|
} else if (!strcasecmp(c->argv[i]->ptr, "match") && j >= 2) {
|
2012-07-09 04:00:26 -04:00
|
|
|
pat = c->argv[i+1]->ptr;
|
|
|
|
patlen = sdslen(pat);
|
|
|
|
|
2013-10-31 05:35:56 -04:00
|
|
|
/* The pattern always matches if it is exactly "*", so it is
|
|
|
|
* equivalent to disabling it. */
|
|
|
|
use_pattern = !(pat[0] == '*' && patlen == 1);
|
2012-07-09 04:00:26 -04:00
|
|
|
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* Step 2: Iterate the collection.
|
|
|
|
*
|
|
|
|
* Note that if the object is encoded with a ziplist, intset, or any other
|
2013-12-05 10:35:32 -05:00
|
|
|
* representation that is not a hash table, we are sure that it is also
|
2013-10-28 06:11:34 -04:00
|
|
|
* composed of a small number of elements. So to avoid taking state we
|
|
|
|
* just return everything inside the object in a single call, setting the
|
|
|
|
* cursor to zero to signal the end of the iteration. */
|
|
|
|
|
2013-12-05 10:35:32 -05:00
|
|
|
/* Handle the case of a hash table. */
|
2013-10-28 06:11:34 -04:00
|
|
|
ht = NULL;
|
|
|
|
if (o == NULL) {
|
|
|
|
ht = c->db->dict;
|
|
|
|
} else if (o->type == REDIS_SET && o->encoding == REDIS_ENCODING_HT) {
|
|
|
|
ht = o->ptr;
|
|
|
|
} else if (o->type == REDIS_HASH && o->encoding == REDIS_ENCODING_HT) {
|
|
|
|
ht = o->ptr;
|
|
|
|
count *= 2; /* We return key / value for this type. */
|
|
|
|
} else if (o->type == REDIS_ZSET && o->encoding == REDIS_ENCODING_SKIPLIST) {
|
|
|
|
zset *zs = o->ptr;
|
|
|
|
ht = zs->dict;
|
|
|
|
count *= 2; /* We return key / value for this type. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ht) {
|
|
|
|
void *privdata[2];
|
2014-09-09 05:53:30 -04:00
|
|
|
/* We set the max number of iterations to ten times the specified
|
|
|
|
* COUNT, so if the hash table is in a pathological state (very
|
|
|
|
* sparsely populated) we avoid to block too much time at the cost
|
|
|
|
* of returning no or very few elements. */
|
|
|
|
long maxiterations = count*10;
|
2013-10-28 06:11:34 -04:00
|
|
|
|
|
|
|
/* We pass two pointers to the callback: the list to which it will
|
|
|
|
* add new elements, and the object containing the dictionary so that
|
|
|
|
* it is possible to fetch more data in a type-dependent way. */
|
|
|
|
privdata[0] = keys;
|
|
|
|
privdata[1] = o;
|
|
|
|
do {
|
|
|
|
cursor = dictScan(ht, cursor, scanCallback, privdata);
|
2014-09-09 05:53:30 -04:00
|
|
|
} while (cursor &&
|
|
|
|
maxiterations-- &&
|
|
|
|
listLength(keys) < (unsigned long)count);
|
2013-10-28 06:11:34 -04:00
|
|
|
} else if (o->type == REDIS_SET) {
|
|
|
|
int pos = 0;
|
2013-11-05 05:57:30 -05:00
|
|
|
int64_t ll;
|
2013-10-28 06:11:34 -04:00
|
|
|
|
|
|
|
while(intsetGet(o->ptr,pos++,&ll))
|
|
|
|
listAddNodeTail(keys,createStringObjectFromLongLong(ll));
|
2013-11-05 09:32:21 -05:00
|
|
|
cursor = 0;
|
2013-10-28 06:11:34 -04:00
|
|
|
} else if (o->type == REDIS_HASH || o->type == REDIS_ZSET) {
|
|
|
|
unsigned char *p = ziplistIndex(o->ptr,0);
|
|
|
|
unsigned char *vstr;
|
|
|
|
unsigned int vlen;
|
|
|
|
long long vll;
|
|
|
|
|
|
|
|
while(p) {
|
|
|
|
ziplistGet(p,&vstr,&vlen,&vll);
|
|
|
|
listAddNodeTail(keys,
|
|
|
|
(vstr != NULL) ? createStringObject((char*)vstr,vlen) :
|
|
|
|
createStringObjectFromLongLong(vll));
|
2013-10-28 06:32:34 -04:00
|
|
|
p = ziplistNext(o->ptr,p);
|
2013-10-28 06:11:34 -04:00
|
|
|
}
|
2013-11-05 09:32:21 -05:00
|
|
|
cursor = 0;
|
2013-10-28 06:11:34 -04:00
|
|
|
} else {
|
|
|
|
redisPanic("Not handled encoding in SCAN.");
|
|
|
|
}
|
2012-07-09 04:00:26 -04:00
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* Step 3: Filter elements. */
|
2013-10-25 05:54:37 -04:00
|
|
|
node = listFirst(keys);
|
|
|
|
while (node) {
|
|
|
|
robj *kobj = listNodeValue(node);
|
|
|
|
nextnode = listNextNode(node);
|
2013-10-31 05:32:33 -04:00
|
|
|
int filter = 0;
|
|
|
|
|
|
|
|
/* Filter element if it does not match the pattern. */
|
2013-10-31 05:35:56 -04:00
|
|
|
if (!filter && use_pattern) {
|
2013-10-31 05:32:33 -04:00
|
|
|
if (sdsEncodedObject(kobj)) {
|
|
|
|
if (!stringmatchlen(pat, patlen, kobj->ptr, sdslen(kobj->ptr), 0))
|
|
|
|
filter = 1;
|
|
|
|
} else {
|
|
|
|
char buf[REDIS_LONGSTR_SIZE];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
redisAssert(kobj->encoding == REDIS_ENCODING_INT);
|
|
|
|
len = ll2string(buf,sizeof(buf),(long)kobj->ptr);
|
|
|
|
if (!stringmatchlen(pat, patlen, buf, len, 0)) filter = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-07-09 04:00:26 -04:00
|
|
|
|
2013-10-31 05:32:33 -04:00
|
|
|
/* Filter element if it is an expired key. */
|
|
|
|
if (!filter && o == NULL && expireIfNeeded(c->db, kobj)) filter = 1;
|
|
|
|
|
|
|
|
/* Remove the element and its associted value if needed. */
|
|
|
|
if (filter) {
|
2012-07-09 04:00:26 -04:00
|
|
|
decrRefCount(kobj);
|
2013-10-25 05:54:37 -04:00
|
|
|
listDelNode(keys, node);
|
2013-11-05 06:16:29 -05:00
|
|
|
}
|
|
|
|
|
2013-12-05 10:35:32 -05:00
|
|
|
/* If this is a hash or a sorted set, we have a flat list of
|
2013-11-05 06:16:29 -05:00
|
|
|
* key-value elements, so if this element was filtered, remove the
|
|
|
|
* value, or skip it if it was not filtered: we only match keys. */
|
|
|
|
if (o && (o->type == REDIS_ZSET || o->type == REDIS_HASH)) {
|
|
|
|
node = nextnode;
|
|
|
|
nextnode = listNextNode(node);
|
|
|
|
if (filter) {
|
2013-10-28 06:11:34 -04:00
|
|
|
kobj = listNodeValue(node);
|
|
|
|
decrRefCount(kobj);
|
|
|
|
listDelNode(keys, node);
|
|
|
|
}
|
2012-07-09 04:00:26 -04:00
|
|
|
}
|
2013-10-25 05:54:37 -04:00
|
|
|
node = nextnode;
|
2012-07-09 04:00:26 -04:00
|
|
|
}
|
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* Step 4: Reply to the client. */
|
2012-07-09 04:00:26 -04:00
|
|
|
addReplyMultiBulkLen(c, 2);
|
2014-08-13 05:44:38 -04:00
|
|
|
addReplyBulkLongLong(c,cursor);
|
2012-07-09 04:00:26 -04:00
|
|
|
|
|
|
|
addReplyMultiBulkLen(c, listLength(keys));
|
2013-10-25 05:54:37 -04:00
|
|
|
while ((node = listFirst(keys)) != NULL) {
|
|
|
|
robj *kobj = listNodeValue(node);
|
2012-07-09 04:00:26 -04:00
|
|
|
addReplyBulk(c, kobj);
|
|
|
|
decrRefCount(kobj);
|
2013-10-25 05:54:37 -04:00
|
|
|
listDelNode(keys, node);
|
2012-07-09 04:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2013-10-25 05:58:03 -04:00
|
|
|
listSetFreeMethod(keys,decrRefCountVoid);
|
2012-07-09 04:00:26 -04:00
|
|
|
listRelease(keys);
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:11:34 -04:00
|
|
|
/* The SCAN command completely relies on scanGenericCommand. */
|
|
|
|
void scanCommand(redisClient *c) {
|
2013-11-05 09:47:50 -05:00
|
|
|
unsigned long cursor;
|
|
|
|
if (parseScanCursorOrReply(c,c->argv[1],&cursor) == REDIS_ERR) return;
|
|
|
|
scanGenericCommand(c,NULL,cursor);
|
2013-10-28 06:11:34 -04:00
|
|
|
}
|
|
|
|
|
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 shutdownCommand(redisClient *c) {
|
2011-11-18 08:10:48 -05:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (c->argc > 2) {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
} else if (c->argc == 2) {
|
|
|
|
if (!strcasecmp(c->argv[1]->ptr,"nosave")) {
|
|
|
|
flags |= REDIS_SHUTDOWN_NOSAVE;
|
|
|
|
} else if (!strcasecmp(c->argv[1]->ptr,"save")) {
|
|
|
|
flags |= REDIS_SHUTDOWN_SAVE;
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 05:22:24 -05:00
|
|
|
/* When SHUTDOWN is called while the server is loading a dataset in
|
|
|
|
* memory we need to make sure no attempt is performed to save
|
2013-06-27 06:14:23 -04:00
|
|
|
* the dataset on shutdown (otherwise it could overwrite the current DB
|
2014-02-07 05:22:24 -05:00
|
|
|
* with half-read data).
|
|
|
|
*
|
|
|
|
* Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */
|
|
|
|
if (server.loading || server.sentinel_mode)
|
2013-06-27 06:14:23 -04:00
|
|
|
flags = (flags & ~REDIS_SHUTDOWN_SAVE) | REDIS_SHUTDOWN_NOSAVE;
|
2011-11-18 08:10:48 -05:00
|
|
|
if (prepareForShutdown(flags) == 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;
|
2011-11-09 10:51:19 -05:00
|
|
|
long long expire;
|
2015-02-23 05:24:24 -05:00
|
|
|
int samekey = 0;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2015-02-23 05:24:24 -05:00
|
|
|
/* When source and dest key is the same, no operation is performed,
|
|
|
|
* if the key exists, however we still return an error on unexisting key. */
|
|
|
|
if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) samekey = 1;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-02-23 05:24:24 -05:00
|
|
|
if (samekey) {
|
|
|
|
addReply(c,nx ? shared.czero : shared.ok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
incrRefCount(o);
|
2011-10-10 09:21:19 -04:00
|
|
|
expire = getExpire(c->db,c->argv[1]);
|
2011-06-14 09:34:27 -04:00
|
|
|
if (lookupKeyWrite(c->db,c->argv[2]) != NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
if (nx) {
|
|
|
|
decrRefCount(o);
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-23 10:44:45 -05:00
|
|
|
/* Overwrite: delete the old key before creating the new one
|
|
|
|
* with the same name. */
|
2011-10-10 09:21:19 -04:00
|
|
|
dbDelete(c->db,c->argv[2]);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2011-10-10 09:21:19 -04:00
|
|
|
dbAdd(c->db,c->argv[2],o);
|
|
|
|
if (expire != -1) setExpire(c->db,c->argv[2],expire);
|
2010-06-21 18:07:48 -04:00
|
|
|
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]);
|
2013-01-25 07:19:08 -05:00
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_from",
|
|
|
|
c->argv[1],c->db->id);
|
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_to",
|
|
|
|
c->argv[2],c->db->id);
|
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;
|
2014-08-01 14:55:24 -04:00
|
|
|
long long dbid;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2011-03-29 11:51:15 -04:00
|
|
|
if (server.cluster_enabled) {
|
|
|
|
addReplyError(c,"MOVE is not allowed in cluster mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
/* Obtain source and target DB pointers */
|
|
|
|
src = c->db;
|
|
|
|
srcid = c->db->id;
|
2014-08-01 14:55:24 -04:00
|
|
|
|
|
|
|
if (getLongLongFromObject(c->argv[2],&dbid) == REDIS_ERR ||
|
|
|
|
dbid < INT_MIN || dbid > INT_MAX ||
|
|
|
|
selectDb(c,dbid) == REDIS_ERR)
|
|
|
|
{
|
2010-06-21 18:07:48 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:34:27 -04:00
|
|
|
/* Return zero if the key already exists in the target DB */
|
|
|
|
if (lookupKeyWrite(dst,c->argv[1]) != NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-14 09:34:27 -04:00
|
|
|
dbAdd(dst,c->argv[1],o);
|
2010-06-21 18:07:48 -04:00
|
|
|
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. */
|
2011-10-04 12:43:03 -04:00
|
|
|
redisAssertWithInfo(NULL,key,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
|
|
|
}
|
|
|
|
|
2011-11-09 10:51:19 -05:00
|
|
|
void setExpire(redisDb *db, robj *key, long long when) {
|
|
|
|
dictEntry *kde, *de;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
/* Reuse the sds from the main dict in the expire dict */
|
2011-11-09 10:51:19 -05:00
|
|
|
kde = dictFind(db->dict,key->ptr);
|
|
|
|
redisAssertWithInfo(NULL,key,kde != NULL);
|
|
|
|
de = dictReplaceRaw(db->expires,dictGetKey(kde));
|
|
|
|
dictSetSignedIntegerVal(de,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) */
|
2011-11-09 10:51:19 -05:00
|
|
|
long long getExpire(redisDb *db, robj *key) {
|
2010-06-21 18:07:48 -04:00
|
|
|
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). */
|
2011-10-04 12:43:03 -04:00
|
|
|
redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
|
2011-11-09 10:51:19 -05:00
|
|
|
return dictGetSignedIntegerVal(de);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
2012-02-04 02:58:37 -05:00
|
|
|
argv[0] = shared.del;
|
2010-08-02 12:13:39 -04:00
|
|
|
argv[1] = key;
|
2012-02-04 02:58:37 -05:00
|
|
|
incrRefCount(argv[0]);
|
|
|
|
incrRefCount(argv[1]);
|
2010-08-02 12:13:39 -04:00
|
|
|
|
2011-12-21 04:31:34 -05:00
|
|
|
if (server.aof_state != REDIS_AOF_OFF)
|
2010-11-03 06:23:59 -04:00
|
|
|
feedAppendOnlyFile(server.delCommand,db->id,argv,2);
|
2013-01-30 12:33:16 -05:00
|
|
|
replicationFeedSlaves(server.slaves,db->id,argv,2);
|
2010-08-02 12:13:39 -04:00
|
|
|
|
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) {
|
2014-02-03 10:15:53 -05:00
|
|
|
mstime_t when = getExpire(db,key);
|
|
|
|
mstime_t now;
|
2010-08-02 12:13:39 -04:00
|
|
|
|
2011-01-14 03:53:57 -05:00
|
|
|
if (when < 0) return 0; /* No expire for this key */
|
|
|
|
|
2011-07-03 06:18:44 -04:00
|
|
|
/* Don't expire anything while loading. It will be done later. */
|
|
|
|
if (server.loading) return 0;
|
|
|
|
|
2014-02-03 10:15:53 -05:00
|
|
|
/* If we are in the context of a Lua script, we claim that time is
|
|
|
|
* blocked to when the Lua script started. This way a key can expire
|
|
|
|
* only the first time it is accessed and not in the middle of the
|
|
|
|
* script execution, making propagation to slaves / AOF consistent.
|
|
|
|
* See issue #1525 on Github for more information. */
|
|
|
|
now = server.lua_caller ? server.lua_time_start : mstime();
|
|
|
|
|
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.
|
|
|
|
*
|
2014-06-26 12:48:40 -04:00
|
|
|
* Still we try to return the right information to the caller,
|
2010-08-02 12:13:39 -04:00
|
|
|
* that is, 0 if we think the key should be still valid, 1 if
|
|
|
|
* we think the key is expired at this time. */
|
2014-02-03 10:15:53 -05:00
|
|
|
if (server.masterhost != NULL) return now > when;
|
2010-08-02 12:13:39 -04:00
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
/* Return when this key has not expired */
|
2014-02-03 10:15:53 -05:00
|
|
|
if (now <= when) return 0;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
/* Delete the key */
|
|
|
|
server.stat_expiredkeys++;
|
2010-08-02 12:13:39 -04:00
|
|
|
propagateExpire(db,key);
|
2013-01-28 07:00:03 -05:00
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_EXPIRED,
|
|
|
|
"expired",key,db->id);
|
2010-06-21 18:07:48 -04:00
|
|
|
return dbDelete(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Expires Commands
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
2012-04-05 09:52:08 -04:00
|
|
|
/* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT
|
|
|
|
* and PEXPIREAT. Because the commad second argument may be relative or absolute
|
|
|
|
* the "basetime" argument is used to signal what the base time is (either 0
|
|
|
|
* for *AT variants of the command, or the current time for relative expires).
|
|
|
|
*
|
|
|
|
* unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for
|
2013-01-16 12:00:20 -05:00
|
|
|
* the argv[2] parameter. The basetime is always specified in milliseconds. */
|
2012-04-05 09:52:08 -04:00
|
|
|
void expireGenericCommand(redisClient *c, long long basetime, int unit) {
|
2011-11-09 10:51:19 -05:00
|
|
|
robj *key = c->argv[1], *param = c->argv[2];
|
2012-04-05 09:52:08 -04:00
|
|
|
long long when; /* unix time in milliseconds when the key will expire. */
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2012-04-05 09:52:08 -04:00
|
|
|
if (getLongLongFromObjectOrReply(c, param, &when, NULL) != REDIS_OK)
|
2011-11-09 10:51:19 -05:00
|
|
|
return;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2012-04-05 09:52:08 -04:00
|
|
|
if (unit == UNIT_SECONDS) when *= 1000;
|
|
|
|
when += basetime;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2013-03-28 07:45:07 -04:00
|
|
|
/* No key, return zero. */
|
Better read-only behavior for expired keys in slaves.
Slaves key expire is orchestrated by the master. Sometimes the master
will send the synthesized DEL to expire keys on the slave with a non
trivial delay (when the key is not accessed, only the incremental expiry
algorithm will expire it in background).
During that time, a key is logically expired, but slaves still return
the key if you GET (or whatever) it. This is a bad behavior.
However we can't simply trust the slave view of the key, since we need
the master to be able to send write commands to update the slave data
set, and DELs should only happen when the key is expired in the master
in order to ensure consistency.
However 99.99% of the issues with this behavior is when a client which
is not a master sends a read only command. In this case we are safe and
can consider the key as non existing.
This commit does a few changes in order to make this sane:
1. lookupKeyRead() is modified in order to return NULL if the above
conditions are met.
2. Calls to lookupKeyRead() in commands actually writing to the data set
are repliaced with calls to lookupKeyWrite().
There are redundand checks, so for example, if in "2" something was
overlooked, we should be still safe, since anyway, when the master
writes the behavior is to don't care about what expireIfneeded()
returns.
This commit is related to #1768, #1770, #2131.
2014-12-10 10:10:21 -05:00
|
|
|
if (lookupKeyWrite(c->db,key) == NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 07:45:07 -04:00
|
|
|
|
2011-07-07 10:24:37 -04:00
|
|
|
/* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past
|
|
|
|
* should never be executed as a DEL when load the AOF or in the context
|
|
|
|
* of a slave instance.
|
|
|
|
*
|
|
|
|
* Instead we take the other branch of the IF statement setting an expire
|
|
|
|
* (possibly in the past) and wait for an explicit DEL from the master. */
|
2012-04-05 09:52:08 -04:00
|
|
|
if (when <= mstime() && !server.loading && !server.masterhost) {
|
2011-07-07 10:24:37 -04:00
|
|
|
robj *aux;
|
|
|
|
|
2011-10-04 12:43:03 -04:00
|
|
|
redisAssertWithInfo(c,key,dbDelete(c->db,key));
|
2011-07-07 10:24:37 -04:00
|
|
|
server.dirty++;
|
|
|
|
|
|
|
|
/* Replicate/AOF this as an explicit DEL. */
|
|
|
|
aux = createStringObject("DEL",3);
|
|
|
|
rewriteClientCommandVector(c,2,aux,key);
|
|
|
|
decrRefCount(aux);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,key);
|
2013-01-25 07:19:08 -05:00
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
|
2011-07-07 10:24:37 -04:00
|
|
|
addReply(c, shared.cone);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
} else {
|
2012-04-05 09:52:08 -04:00
|
|
|
setExpire(c->db,key,when);
|
2010-08-03 06:26:30 -04:00
|
|
|
addReply(c,shared.cone);
|
2010-12-29 13:39:42 -05:00
|
|
|
signalModifiedKey(c->db,key);
|
2013-01-25 07:19:08 -05:00
|
|
|
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"expire",key,c->db->id);
|
2010-08-03 06:26:30 -04:00
|
|
|
server.dirty++;
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expireCommand(redisClient *c) {
|
2012-03-28 05:48:36 -04:00
|
|
|
expireGenericCommand(c,mstime(),UNIT_SECONDS);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void expireatCommand(redisClient *c) {
|
2012-03-28 05:48:36 -04:00
|
|
|
expireGenericCommand(c,0,UNIT_SECONDS);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2011-11-10 11:52:02 -05:00
|
|
|
void pexpireCommand(redisClient *c) {
|
2012-03-28 05:48:36 -04:00
|
|
|
expireGenericCommand(c,mstime(),UNIT_MILLISECONDS);
|
2011-11-10 11:52:02 -05:00
|
|
|
}
|
2011-11-09 12:05:35 -05:00
|
|
|
|
2011-11-10 11:52:02 -05:00
|
|
|
void pexpireatCommand(redisClient *c) {
|
2012-03-28 05:48:36 -04:00
|
|
|
expireGenericCommand(c,0,UNIT_MILLISECONDS);
|
2011-11-10 11:52:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ttlGenericCommand(redisClient *c, int output_ms) {
|
|
|
|
long long expire, ttl = -1;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2012-11-12 17:04:36 -05:00
|
|
|
/* If the key does not exist at all, return -2 */
|
2013-03-26 06:42:14 -04:00
|
|
|
if (lookupKeyRead(c->db,c->argv[1]) == NULL) {
|
2012-11-12 17:04:36 -05:00
|
|
|
addReplyLongLong(c,-2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* The key exists. Return -1 if it has no expire, or the actual
|
|
|
|
* TTL value otherwise. */
|
2013-03-26 06:42:14 -04:00
|
|
|
expire = getExpire(c->db,c->argv[1]);
|
2010-06-21 18:07:48 -04:00
|
|
|
if (expire != -1) {
|
2011-11-09 10:51:19 -05:00
|
|
|
ttl = expire-mstime();
|
2013-03-26 06:42:14 -04:00
|
|
|
if (ttl < 0) ttl = 0;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2011-11-09 10:51:19 -05:00
|
|
|
if (ttl == -1) {
|
|
|
|
addReplyLongLong(c,-1);
|
|
|
|
} else {
|
2011-11-09 12:05:35 -05:00
|
|
|
addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000));
|
2011-11-09 10:51:19 -05:00
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-08-03 08:19:20 -04:00
|
|
|
|
2011-11-10 11:52:02 -05:00
|
|
|
void ttlCommand(redisClient *c) {
|
|
|
|
ttlGenericCommand(c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pttlCommand(redisClient *c) {
|
|
|
|
ttlGenericCommand(c, 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2011-03-23 13:09:17 -04:00
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* API to get key arguments from commands
|
|
|
|
* ---------------------------------------------------------------------------*/
|
|
|
|
|
2014-03-10 10:24:38 -04:00
|
|
|
/* The base case is to use the keys position as given in the command table
|
|
|
|
* (firstkey, lastkey, step). */
|
2011-03-23 13:09:17 -04:00
|
|
|
int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) {
|
|
|
|
int j, i = 0, last, *keys;
|
|
|
|
REDIS_NOTUSED(argv);
|
|
|
|
|
|
|
|
if (cmd->firstkey == 0) {
|
|
|
|
*numkeys = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
last = cmd->lastkey;
|
|
|
|
if (last < 0) last = argc+last;
|
|
|
|
keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1));
|
|
|
|
for (j = cmd->firstkey; j <= last; j += cmd->keystep) {
|
|
|
|
redisAssert(j < argc);
|
2011-03-28 11:54:42 -04:00
|
|
|
keys[i++] = j;
|
2011-03-23 13:09:17 -04:00
|
|
|
}
|
2011-03-28 11:54:42 -04:00
|
|
|
*numkeys = i;
|
2011-03-23 13:09:17 -04:00
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2014-03-10 10:31:01 -04:00
|
|
|
/* Return all the arguments that are keys in the command passed via argc / argv.
|
|
|
|
*
|
|
|
|
* The command returns the positions of all the key arguments inside the array,
|
|
|
|
* so the actual return value is an heap allocated array of integers. The
|
|
|
|
* length of the array is returned by reference into *numkeys.
|
|
|
|
*
|
|
|
|
* 'cmd' must be point to the corresponding entry into the redisCommand
|
|
|
|
* table, according to the command name in argv[0].
|
2014-03-10 10:24:38 -04:00
|
|
|
*
|
|
|
|
* This function uses the command table if a command-specific helper function
|
|
|
|
* is not required, otherwise it calls the command-specific function. */
|
2014-03-10 10:31:01 -04:00
|
|
|
int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
|
2011-03-23 13:09:17 -04:00
|
|
|
if (cmd->getkeys_proc) {
|
2014-03-10 08:18:41 -04:00
|
|
|
return cmd->getkeys_proc(cmd,argv,argc,numkeys);
|
2011-03-23 13:09:17 -04:00
|
|
|
} else {
|
|
|
|
return getKeysUsingCommandTable(cmd,argv,argc,numkeys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 10:24:38 -04:00
|
|
|
/* Free the result of getKeysFromCommand. */
|
2011-03-23 13:09:17 -04:00
|
|
|
void getKeysFreeResult(int *result) {
|
|
|
|
zfree(result);
|
|
|
|
}
|
|
|
|
|
2014-03-10 10:24:38 -04:00
|
|
|
/* Helper function to extract keys from following commands:
|
|
|
|
* ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options>
|
|
|
|
* ZINTERSTORE <destkey> <num-keys> <key> <key> ... <key> <options> */
|
|
|
|
int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
|
2011-03-23 13:09:17 -04:00
|
|
|
int i, num, *keys;
|
|
|
|
REDIS_NOTUSED(cmd);
|
|
|
|
|
|
|
|
num = atoi(argv[2]->ptr);
|
|
|
|
/* Sanity check. Don't return any key if the command is going to
|
|
|
|
* reply with syntax error. */
|
|
|
|
if (num > (argc-3)) {
|
|
|
|
*numkeys = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-07 16:32:04 -05:00
|
|
|
|
|
|
|
/* Keys in z{union,inter}store come from two places:
|
2014-03-10 06:43:56 -04:00
|
|
|
* argv[1] = storage key,
|
|
|
|
* argv[3...n] = keys to intersect */
|
2014-03-07 16:32:04 -05:00
|
|
|
keys = zmalloc(sizeof(int)*(num+1));
|
|
|
|
|
|
|
|
/* Add all key positions for argv[3...n] to keys[] */
|
2011-03-23 13:09:17 -04:00
|
|
|
for (i = 0; i < num; i++) keys[i] = 3+i;
|
2014-03-07 16:32:04 -05:00
|
|
|
|
2014-03-10 06:43:56 -04:00
|
|
|
/* Finally add the argv[1] key position (the storage key target). */
|
2014-03-07 16:32:04 -05:00
|
|
|
keys[num] = 1;
|
|
|
|
*numkeys = num+1; /* Total keys = {union,inter} keys + storage key */
|
2011-03-23 13:09:17 -04:00
|
|
|
return keys;
|
|
|
|
}
|
2011-04-28 13:00:33 -04:00
|
|
|
|
2014-03-10 10:26:10 -04:00
|
|
|
/* Helper function to extract keys from the following commands:
|
|
|
|
* EVAL <script> <num-keys> <key> <key> ... <key> [more stuff]
|
|
|
|
* EVALSHA <script> <num-keys> <key> <key> ... <key> [more stuff] */
|
|
|
|
int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
|
|
|
|
int i, num, *keys;
|
|
|
|
REDIS_NOTUSED(cmd);
|
|
|
|
|
|
|
|
num = atoi(argv[2]->ptr);
|
|
|
|
/* Sanity check. Don't return any key if the command is going to
|
|
|
|
* reply with syntax error. */
|
|
|
|
if (num > (argc-3)) {
|
|
|
|
*numkeys = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = zmalloc(sizeof(int)*num);
|
2014-03-10 11:23:42 -04:00
|
|
|
*numkeys = num;
|
2014-03-10 10:26:10 -04:00
|
|
|
|
|
|
|
/* Add all key positions for argv[3...n] to keys[] */
|
|
|
|
for (i = 0; i < num; i++) keys[i] = 3+i;
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2014-03-10 11:26:08 -04:00
|
|
|
/* Helper function to extract keys from the SORT command.
|
|
|
|
*
|
|
|
|
* SORT <sort-key> ... STORE <store-key> ...
|
|
|
|
*
|
|
|
|
* The first argument of SORT is always a key, however a list of options
|
|
|
|
* follow in SQL-alike style. Here we parse just the minimum in order to
|
|
|
|
* correctly identify keys in the "STORE" option. */
|
|
|
|
int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
|
2014-07-21 17:31:21 -04:00
|
|
|
int i, j, num, *keys, found_store = 0;
|
2014-03-10 11:26:08 -04:00
|
|
|
REDIS_NOTUSED(cmd);
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
keys = zmalloc(sizeof(int)*2); /* Alloc 2 places for the worst case. */
|
|
|
|
|
|
|
|
keys[num++] = 1; /* <sort-key> is always present. */
|
|
|
|
|
|
|
|
/* Search for STORE option. By default we consider options to don't
|
|
|
|
* have arguments, so if we find an unknown option name we scan the
|
|
|
|
* next. However there are options with 1 or 2 arguments, so we
|
|
|
|
* provide a list here in order to skip the right number of args. */
|
|
|
|
struct {
|
|
|
|
char *name;
|
|
|
|
int skip;
|
|
|
|
} skiplist[] = {
|
|
|
|
{"limit", 2},
|
|
|
|
{"get", 1},
|
|
|
|
{"by", 1},
|
|
|
|
{NULL, 0} /* End of elements. */
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 2; i < argc; i++) {
|
|
|
|
for (j = 0; skiplist[j].name != NULL; j++) {
|
|
|
|
if (!strcasecmp(argv[i]->ptr,skiplist[j].name)) {
|
|
|
|
i += skiplist[j].skip;
|
|
|
|
break;
|
|
|
|
} else if (!strcasecmp(argv[i]->ptr,"store") && i+1 < argc) {
|
2014-03-10 11:39:07 -04:00
|
|
|
/* Note: we don't increment "num" here and continue the loop
|
|
|
|
* to be sure to process the *last* "STORE" option if multiple
|
|
|
|
* ones are provided. This is same behavior as SORT. */
|
2014-07-21 17:31:21 -04:00
|
|
|
found_store = 1;
|
2014-03-10 11:39:07 -04:00
|
|
|
keys[num] = i+1; /* <store-key> */
|
2014-03-10 11:26:08 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 17:31:21 -04:00
|
|
|
*numkeys = num + found_store;
|
2014-03-10 11:26:08 -04:00
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2011-04-28 13:00:33 -04:00
|
|
|
/* Slot to Key API. This is used by Redis Cluster in order to obtain in
|
|
|
|
* a fast way a key that belongs to a specified hash slot. This is useful
|
|
|
|
* while rehashing the cluster. */
|
2013-02-22 04:16:21 -05:00
|
|
|
void slotToKeyAdd(robj *key) {
|
2011-04-28 13:00:33 -04:00
|
|
|
unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
|
|
|
|
|
2013-02-14 07:20:56 -05:00
|
|
|
zslInsert(server.cluster->slots_to_keys,hashslot,key);
|
2011-04-28 13:00:33 -04:00
|
|
|
incrRefCount(key);
|
|
|
|
}
|
|
|
|
|
2013-02-22 04:16:21 -05:00
|
|
|
void slotToKeyDel(robj *key) {
|
2011-04-28 13:00:33 -04:00
|
|
|
unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
|
|
|
|
|
2013-02-14 07:20:56 -05:00
|
|
|
zslDelete(server.cluster->slots_to_keys,hashslot,key);
|
2011-04-28 13:00:33 -04:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:16:21 -05:00
|
|
|
void slotToKeyFlush(void) {
|
2013-02-22 04:15:32 -05:00
|
|
|
zslFree(server.cluster->slots_to_keys);
|
|
|
|
server.cluster->slots_to_keys = zslCreate();
|
|
|
|
}
|
|
|
|
|
2013-02-25 05:24:42 -05:00
|
|
|
unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) {
|
2011-04-28 13:00:33 -04:00
|
|
|
zskiplistNode *n;
|
|
|
|
zrangespec range;
|
2011-04-29 10:17:58 -04:00
|
|
|
int j = 0;
|
2011-04-28 13:00:33 -04:00
|
|
|
|
|
|
|
range.min = range.max = hashslot;
|
|
|
|
range.minex = range.maxex = 0;
|
2014-05-14 04:46:37 -04:00
|
|
|
|
2014-04-17 08:30:12 -04:00
|
|
|
n = zslFirstInRange(server.cluster->slots_to_keys, &range);
|
2011-04-29 10:17:58 -04:00
|
|
|
while(n && n->score == hashslot && count--) {
|
|
|
|
keys[j++] = n->obj;
|
|
|
|
n = n->level[0].forward;
|
|
|
|
}
|
|
|
|
return j;
|
2011-04-28 13:00:33 -04:00
|
|
|
}
|
2013-02-25 05:15:03 -05:00
|
|
|
|
2014-05-14 04:46:37 -04:00
|
|
|
/* Remove all the keys in the specified hash slot.
|
|
|
|
* The number of removed items is returned. */
|
|
|
|
unsigned int delKeysInSlot(unsigned int hashslot) {
|
|
|
|
zskiplistNode *n;
|
|
|
|
zrangespec range;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
range.min = range.max = hashslot;
|
|
|
|
range.minex = range.maxex = 0;
|
|
|
|
|
|
|
|
n = zslFirstInRange(server.cluster->slots_to_keys, &range);
|
|
|
|
while(n && n->score == hashslot) {
|
|
|
|
robj *key = n->obj;
|
|
|
|
n = n->level[0].forward; /* Go to the next item before freeing it. */
|
|
|
|
incrRefCount(key); /* Protect the object while freeing it. */
|
|
|
|
dbDelete(&server.db[0],key);
|
|
|
|
decrRefCount(key);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2013-02-25 05:24:42 -05:00
|
|
|
unsigned int countKeysInSlot(unsigned int hashslot) {
|
2013-02-25 06:37:50 -05:00
|
|
|
zskiplist *zsl = server.cluster->slots_to_keys;
|
|
|
|
zskiplistNode *zn;
|
2013-02-25 05:15:03 -05:00
|
|
|
zrangespec range;
|
2013-02-25 06:37:50 -05:00
|
|
|
int rank, count = 0;
|
2013-02-25 05:15:03 -05:00
|
|
|
|
|
|
|
range.min = range.max = hashslot;
|
|
|
|
range.minex = range.maxex = 0;
|
2013-02-25 06:37:50 -05:00
|
|
|
|
|
|
|
/* Find first element in range */
|
2014-04-17 08:30:12 -04:00
|
|
|
zn = zslFirstInRange(zsl, &range);
|
2013-02-25 06:37:50 -05:00
|
|
|
|
|
|
|
/* Use rank of first element, if any, to determine preliminary count */
|
|
|
|
if (zn != NULL) {
|
|
|
|
rank = zslGetRank(zsl, zn->score, zn->obj);
|
|
|
|
count = (zsl->length - (rank - 1));
|
|
|
|
|
|
|
|
/* Find last element in range */
|
2014-04-17 08:30:12 -04:00
|
|
|
zn = zslLastInRange(zsl, &range);
|
2013-02-25 06:37:50 -05:00
|
|
|
|
|
|
|
/* Use rank of last element, if any, to determine the actual count */
|
|
|
|
if (zn != NULL) {
|
|
|
|
rank = zslGetRank(zsl, zn->score, zn->obj);
|
|
|
|
count -= (zsl->length - rank);
|
|
|
|
}
|
2013-02-25 05:15:03 -05:00
|
|
|
}
|
2013-02-25 06:37:50 -05:00
|
|
|
return count;
|
2013-02-25 05:15:03 -05:00
|
|
|
}
|