2010-06-21 18:07:48 -04:00
|
|
|
#include "redis.h"
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Set Commands
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
/* Factory method to return a set that *can* hold "value". When the object has
|
|
|
|
* an integer-encodable value, an intset will be returned. Otherwise a regular
|
|
|
|
* hash table. */
|
|
|
|
robj *setTypeCreate(robj *value) {
|
2010-08-26 12:47:03 -04:00
|
|
|
if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK)
|
2010-07-02 13:57:12 -04:00
|
|
|
return createIntsetObject();
|
|
|
|
return createSetObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
int setTypeAdd(robj *subject, robj *value) {
|
|
|
|
long long llval;
|
|
|
|
if (subject->encoding == REDIS_ENCODING_HT) {
|
|
|
|
if (dictAdd(subject->ptr,value,NULL) == DICT_OK) {
|
|
|
|
incrRefCount(value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (subject->encoding == REDIS_ENCODING_INTSET) {
|
2010-08-26 12:47:03 -04:00
|
|
|
if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
|
2010-07-02 13:57:12 -04:00
|
|
|
uint8_t success = 0;
|
|
|
|
subject->ptr = intsetAdd(subject->ptr,llval,&success);
|
|
|
|
if (success) {
|
|
|
|
/* Convert to regular set when the intset contains
|
|
|
|
* too many entries. */
|
|
|
|
if (intsetLen(subject->ptr) > server.set_max_intset_entries)
|
|
|
|
setTypeConvert(subject,REDIS_ENCODING_HT);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Failed to get integer from object, convert to regular set. */
|
|
|
|
setTypeConvert(subject,REDIS_ENCODING_HT);
|
|
|
|
|
|
|
|
/* The set *was* an intset and this value is not integer
|
|
|
|
* encodable, so dictAdd should always work. */
|
|
|
|
redisAssert(dictAdd(subject->ptr,value,NULL) == DICT_OK);
|
|
|
|
incrRefCount(value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int setTypeRemove(robj *subject, robj *value) {
|
|
|
|
long long llval;
|
|
|
|
if (subject->encoding == REDIS_ENCODING_HT) {
|
|
|
|
if (dictDelete(subject->ptr,value) == DICT_OK) {
|
|
|
|
if (htNeedsResize(subject->ptr)) dictResize(subject->ptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (subject->encoding == REDIS_ENCODING_INTSET) {
|
2010-08-26 12:47:03 -04:00
|
|
|
if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
|
2010-07-02 13:57:12 -04:00
|
|
|
uint8_t success;
|
|
|
|
subject->ptr = intsetRemove(subject->ptr,llval,&success);
|
|
|
|
if (success) return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int setTypeIsMember(robj *subject, robj *value) {
|
|
|
|
long long llval;
|
|
|
|
if (subject->encoding == REDIS_ENCODING_HT) {
|
|
|
|
return dictFind((dict*)subject->ptr,value) != NULL;
|
|
|
|
} else if (subject->encoding == REDIS_ENCODING_INTSET) {
|
2010-08-26 12:47:03 -04:00
|
|
|
if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
|
2010-07-02 13:57:12 -04:00
|
|
|
return intsetFind((intset*)subject->ptr,llval);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-21 05:25:13 -04:00
|
|
|
setTypeIterator *setTypeInitIterator(robj *subject) {
|
2010-08-26 06:13:51 -04:00
|
|
|
setTypeIterator *si = zmalloc(sizeof(setTypeIterator));
|
2010-07-02 13:57:12 -04:00
|
|
|
si->subject = subject;
|
|
|
|
si->encoding = subject->encoding;
|
|
|
|
if (si->encoding == REDIS_ENCODING_HT) {
|
|
|
|
si->di = dictGetIterator(subject->ptr);
|
|
|
|
} else if (si->encoding == REDIS_ENCODING_INTSET) {
|
|
|
|
si->ii = 0;
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
2010-08-21 05:25:13 -04:00
|
|
|
void setTypeReleaseIterator(setTypeIterator *si) {
|
2010-07-02 13:57:12 -04:00
|
|
|
if (si->encoding == REDIS_ENCODING_HT)
|
|
|
|
dictReleaseIterator(si->di);
|
|
|
|
zfree(si);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to the next entry in the set. Returns the object at the current
|
|
|
|
* position, or NULL when the end is reached. This object will have its
|
|
|
|
* refcount incremented, so the caller needs to take care of this. */
|
2010-08-21 05:25:13 -04:00
|
|
|
robj *setTypeNext(setTypeIterator *si) {
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *ret = NULL;
|
|
|
|
if (si->encoding == REDIS_ENCODING_HT) {
|
|
|
|
dictEntry *de = dictNext(si->di);
|
|
|
|
if (de != NULL) {
|
|
|
|
ret = dictGetEntryKey(de);
|
|
|
|
incrRefCount(ret);
|
|
|
|
}
|
|
|
|
} else if (si->encoding == REDIS_ENCODING_INTSET) {
|
2010-08-26 12:11:26 -04:00
|
|
|
int64_t llval;
|
2010-07-02 13:57:12 -04:00
|
|
|
if (intsetGet(si->subject->ptr,si->ii++,&llval))
|
|
|
|
ret = createStringObjectFromLongLong(llval);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return random element from set. The returned object will always have
|
|
|
|
* an incremented refcount. */
|
|
|
|
robj *setTypeRandomElement(robj *subject) {
|
|
|
|
robj *ret = NULL;
|
|
|
|
if (subject->encoding == REDIS_ENCODING_HT) {
|
|
|
|
dictEntry *de = dictGetRandomKey(subject->ptr);
|
|
|
|
ret = dictGetEntryKey(de);
|
|
|
|
incrRefCount(ret);
|
|
|
|
} else if (subject->encoding == REDIS_ENCODING_INTSET) {
|
|
|
|
long long llval = intsetRandom(subject->ptr);
|
|
|
|
ret = createStringObjectFromLongLong(llval);
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long setTypeSize(robj *subject) {
|
|
|
|
if (subject->encoding == REDIS_ENCODING_HT) {
|
|
|
|
return dictSize((dict*)subject->ptr);
|
|
|
|
} else if (subject->encoding == REDIS_ENCODING_INTSET) {
|
|
|
|
return intsetLen((intset*)subject->ptr);
|
|
|
|
} else {
|
|
|
|
redisPanic("Unknown set encoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the set to specified encoding. The resulting dict (when converting
|
|
|
|
* to a hashtable) is presized to hold the number of elements in the original
|
|
|
|
* set. */
|
|
|
|
void setTypeConvert(robj *subject, int enc) {
|
2010-08-21 05:25:13 -04:00
|
|
|
setTypeIterator *si;
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *element;
|
|
|
|
redisAssert(subject->type == REDIS_SET);
|
|
|
|
|
|
|
|
if (enc == REDIS_ENCODING_HT) {
|
|
|
|
dict *d = dictCreate(&setDictType,NULL);
|
|
|
|
/* Presize the dict to avoid rehashing */
|
|
|
|
dictExpand(d,intsetLen(subject->ptr));
|
|
|
|
|
|
|
|
/* setTypeGet returns a robj with incremented refcount */
|
|
|
|
si = setTypeInitIterator(subject);
|
|
|
|
while ((element = setTypeNext(si)) != NULL)
|
|
|
|
redisAssert(dictAdd(d,element,NULL) == DICT_OK);
|
|
|
|
setTypeReleaseIterator(si);
|
|
|
|
|
|
|
|
subject->encoding = REDIS_ENCODING_HT;
|
|
|
|
zfree(subject->ptr);
|
|
|
|
subject->ptr = d;
|
|
|
|
} else {
|
|
|
|
redisPanic("Unsupported set conversion");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
void saddCommand(redisClient *c) {
|
|
|
|
robj *set;
|
|
|
|
|
|
|
|
set = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
if (set == NULL) {
|
2010-07-02 13:57:12 -04:00
|
|
|
set = setTypeCreate(c->argv[2]);
|
2010-06-21 18:07:48 -04:00
|
|
|
dbAdd(c->db,c->argv[1],set);
|
|
|
|
} else {
|
|
|
|
if (set->type != REDIS_SET) {
|
|
|
|
addReply(c,shared.wrongtypeerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeAdd(set,c->argv[2])) {
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[1]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
addReply(c,shared.cone);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sremCommand(redisClient *c) {
|
|
|
|
robj *set;
|
|
|
|
|
|
|
|
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
|
|
|
checkType(c,set,REDIS_SET)) return;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeRemove(set,c->argv[2])) {
|
|
|
|
if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[1]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
addReply(c,shared.cone);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void smoveCommand(redisClient *c) {
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *srcset, *dstset, *ele;
|
2010-06-21 18:07:48 -04:00
|
|
|
srcset = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
dstset = lookupKeyWrite(c->db,c->argv[2]);
|
2010-07-02 13:57:12 -04:00
|
|
|
ele = c->argv[3];
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
/* If the source key does not exist return 0 */
|
|
|
|
if (srcset == NULL) {
|
|
|
|
addReply(c,shared.czero);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
|
|
|
|
/* If the source key has the wrong type, or the destination key
|
|
|
|
* is set and has the wrong type, return with an error. */
|
|
|
|
if (checkType(c,srcset,REDIS_SET) ||
|
|
|
|
(dstset && checkType(c,dstset,REDIS_SET))) return;
|
|
|
|
|
|
|
|
/* If srcset and dstset are equal, SMOVE is a no-op */
|
|
|
|
if (srcset == dstset) {
|
|
|
|
addReply(c,shared.cone);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
|
|
|
|
/* If the element cannot be removed from the src set, return 0. */
|
|
|
|
if (!setTypeRemove(srcset,ele)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
|
|
|
|
/* Remove the src set from the database when empty */
|
|
|
|
if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]);
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[1]);
|
|
|
|
touchWatchedKey(c->db,c->argv[2]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
2010-07-02 13:57:12 -04:00
|
|
|
|
|
|
|
/* Create the destination set when it doesn't exist */
|
2010-06-21 18:07:48 -04:00
|
|
|
if (!dstset) {
|
2010-07-02 13:57:12 -04:00
|
|
|
dstset = setTypeCreate(ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
dbAdd(c->db,c->argv[2],dstset);
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
|
|
|
|
/* An extra key has changed when ele was successfully added to dstset */
|
|
|
|
if (setTypeAdd(dstset,ele)) server.dirty++;
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.cone);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sismemberCommand(redisClient *c) {
|
|
|
|
robj *set;
|
|
|
|
|
|
|
|
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
|
|
|
checkType(c,set,REDIS_SET)) return;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeIsMember(set,c->argv[2]))
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.cone);
|
|
|
|
else
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scardCommand(redisClient *c) {
|
|
|
|
robj *o;
|
|
|
|
|
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
|
|
|
checkType(c,o,REDIS_SET)) return;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
addReplyUlong(c,setTypeSize(o));
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void spopCommand(redisClient *c) {
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *set, *ele;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
|
|
|
|
checkType(c,set,REDIS_SET)) return;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
ele = setTypeRandomElement(set);
|
|
|
|
if (ele == NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.nullbulk);
|
|
|
|
} else {
|
2010-07-02 13:57:12 -04:00
|
|
|
setTypeRemove(set,ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
addReplyBulk(c,ele);
|
2010-07-02 13:57:12 -04:00
|
|
|
decrRefCount(ele);
|
|
|
|
if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[1]);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void srandmemberCommand(redisClient *c) {
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *set, *ele;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
|
|
|
|
checkType(c,set,REDIS_SET)) return;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
ele = setTypeRandomElement(set);
|
|
|
|
if (ele == NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.nullbulk);
|
|
|
|
} else {
|
|
|
|
addReplyBulk(c,ele);
|
2010-07-02 13:57:12 -04:00
|
|
|
decrRefCount(ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
|
2010-07-02 13:57:12 -04:00
|
|
|
return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
|
|
|
|
robj **sets = zmalloc(sizeof(robj*)*setnum);
|
2010-08-21 05:25:13 -04:00
|
|
|
setTypeIterator *si;
|
2010-08-30 10:02:06 -04:00
|
|
|
robj *ele, *dstset = NULL;
|
|
|
|
void *replylen = NULL;
|
2010-06-21 18:07:48 -04:00
|
|
|
unsigned long j, cardinality = 0;
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
for (j = 0; j < setnum; j++) {
|
|
|
|
robj *setobj = dstkey ?
|
|
|
|
lookupKeyWrite(c->db,setkeys[j]) :
|
|
|
|
lookupKeyRead(c->db,setkeys[j]);
|
2010-06-21 18:07:48 -04:00
|
|
|
if (!setobj) {
|
2010-07-02 13:57:12 -04:00
|
|
|
zfree(sets);
|
2010-06-21 18:07:48 -04:00
|
|
|
if (dstkey) {
|
2010-07-12 06:01:15 -04:00
|
|
|
if (dbDelete(c->db,dstkey)) {
|
|
|
|
touchWatchedKey(c->db,dstkey);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
2010-07-12 06:01:15 -04:00
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
addReply(c,shared.czero);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.emptymultibulk);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
if (checkType(c,setobj,REDIS_SET)) {
|
|
|
|
zfree(sets);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
sets[j] = setobj;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
/* Sort sets from the smallest to largest, this will improve our
|
|
|
|
* algorithm's performace */
|
2010-07-02 13:57:12 -04:00
|
|
|
qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
/* The first thing we should output is the total number of elements...
|
|
|
|
* since this is a multi-bulk write, but at this stage we don't know
|
|
|
|
* the intersection set size, so we use a trick, append an empty object
|
|
|
|
* to the output list and save the pointer to later modify it with the
|
|
|
|
* right length */
|
|
|
|
if (!dstkey) {
|
2010-08-30 10:02:06 -04:00
|
|
|
replylen = addDeferredMultiBulkLength(c);
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
/* If we have a target key where to store the resulting set
|
|
|
|
* create this key with an empty set inside */
|
2010-07-02 13:57:12 -04:00
|
|
|
dstset = createIntsetObject();
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate all the elements of the first (smallest) set, and test
|
|
|
|
* the element against all the other sets, if at least one set does
|
|
|
|
* not include the element it is discarded */
|
2010-07-02 13:57:12 -04:00
|
|
|
si = setTypeInitIterator(sets[0]);
|
|
|
|
while((ele = setTypeNext(si)) != NULL) {
|
|
|
|
for (j = 1; j < setnum; j++)
|
|
|
|
if (!setTypeIsMember(sets[j],ele)) break;
|
|
|
|
|
|
|
|
/* Only take action when all sets contain the member */
|
|
|
|
if (j == setnum) {
|
|
|
|
if (!dstkey) {
|
|
|
|
addReplyBulk(c,ele);
|
|
|
|
cardinality++;
|
|
|
|
} else {
|
|
|
|
setTypeAdd(dstset,ele);
|
|
|
|
}
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
decrRefCount(ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
setTypeReleaseIterator(si);
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
if (dstkey) {
|
|
|
|
/* Store the resulting set into the target, if the intersection
|
|
|
|
* is not an empty set. */
|
|
|
|
dbDelete(c->db,dstkey);
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeSize(dstset) > 0) {
|
2010-06-21 18:07:48 -04:00
|
|
|
dbAdd(c->db,dstkey,dstset);
|
2010-07-02 13:57:12 -04:00
|
|
|
addReplyLongLong(c,setTypeSize(dstset));
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
decrRefCount(dstset);
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
}
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,dstkey);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
} else {
|
2010-08-30 10:02:06 -04:00
|
|
|
setDeferredMultiBulkLength(c,replylen,cardinality);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
zfree(sets);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void sinterCommand(redisClient *c) {
|
|
|
|
sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sinterstoreCommand(redisClient *c) {
|
|
|
|
sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
|
|
|
|
}
|
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
#define REDIS_OP_UNION 0
|
|
|
|
#define REDIS_OP_DIFF 1
|
|
|
|
#define REDIS_OP_INTER 2
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
|
|
|
|
robj **sets = zmalloc(sizeof(robj*)*setnum);
|
2010-08-21 05:25:13 -04:00
|
|
|
setTypeIterator *si;
|
2010-07-02 13:57:12 -04:00
|
|
|
robj *ele, *dstset = NULL;
|
|
|
|
int j, cardinality = 0;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
for (j = 0; j < setnum; j++) {
|
|
|
|
robj *setobj = dstkey ?
|
|
|
|
lookupKeyWrite(c->db,setkeys[j]) :
|
|
|
|
lookupKeyRead(c->db,setkeys[j]);
|
2010-06-21 18:07:48 -04:00
|
|
|
if (!setobj) {
|
2010-07-02 13:57:12 -04:00
|
|
|
sets[j] = NULL;
|
2010-06-21 18:07:48 -04:00
|
|
|
continue;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
if (checkType(c,setobj,REDIS_SET)) {
|
|
|
|
zfree(sets);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
sets[j] = setobj;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We need a temp set object to store our union. If the dstkey
|
|
|
|
* is not NULL (that is, we are inside an SUNIONSTORE operation) then
|
|
|
|
* this set object will be the resulting object to set into the target key*/
|
2010-07-02 13:57:12 -04:00
|
|
|
dstset = createIntsetObject();
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
/* Iterate all the elements of all the sets, add every element a single
|
|
|
|
* time to the result set */
|
2010-07-02 13:57:12 -04:00
|
|
|
for (j = 0; j < setnum; j++) {
|
|
|
|
if (op == REDIS_OP_DIFF && j == 0 && !sets[j]) break; /* result set is empty */
|
|
|
|
if (!sets[j]) continue; /* non existing keys are like empty sets */
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
si = setTypeInitIterator(sets[j]);
|
|
|
|
while((ele = setTypeNext(si)) != NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
if (op == REDIS_OP_UNION || j == 0) {
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeAdd(dstset,ele)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
cardinality++;
|
|
|
|
}
|
|
|
|
} else if (op == REDIS_OP_DIFF) {
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeRemove(dstset,ele)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
cardinality--;
|
|
|
|
}
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
decrRefCount(ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
setTypeReleaseIterator(si);
|
2010-06-21 18:07:48 -04:00
|
|
|
|
2010-07-02 13:57:12 -04:00
|
|
|
/* Exit when result set is empty. */
|
2010-06-21 18:07:48 -04:00
|
|
|
if (op == REDIS_OP_DIFF && cardinality == 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output the content of the resulting set, if not in STORE mode */
|
|
|
|
if (!dstkey) {
|
2010-09-02 06:38:34 -04:00
|
|
|
addReplyMultiBulkLen(c,cardinality);
|
2010-07-02 13:57:12 -04:00
|
|
|
si = setTypeInitIterator(dstset);
|
|
|
|
while((ele = setTypeNext(si)) != NULL) {
|
2010-06-21 18:07:48 -04:00
|
|
|
addReplyBulk(c,ele);
|
2010-07-02 13:57:12 -04:00
|
|
|
decrRefCount(ele);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
setTypeReleaseIterator(si);
|
2010-06-21 18:07:48 -04:00
|
|
|
decrRefCount(dstset);
|
|
|
|
} else {
|
|
|
|
/* If we have a target key where to store the resulting set
|
|
|
|
* create this key with the result set inside */
|
|
|
|
dbDelete(c->db,dstkey);
|
2010-07-02 13:57:12 -04:00
|
|
|
if (setTypeSize(dstset) > 0) {
|
2010-06-21 18:07:48 -04:00
|
|
|
dbAdd(c->db,dstkey,dstset);
|
2010-07-02 13:57:12 -04:00
|
|
|
addReplyLongLong(c,setTypeSize(dstset));
|
2010-06-21 18:07:48 -04:00
|
|
|
} else {
|
|
|
|
decrRefCount(dstset);
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
}
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,dstkey);
|
2010-06-21 18:07:48 -04:00
|
|
|
server.dirty++;
|
|
|
|
}
|
2010-07-02 13:57:12 -04:00
|
|
|
zfree(sets);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void sunionCommand(redisClient *c) {
|
|
|
|
sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sunionstoreCommand(redisClient *c) {
|
|
|
|
sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdiffCommand(redisClient *c) {
|
|
|
|
sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdiffstoreCommand(redisClient *c) {
|
|
|
|
sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
|
|
|
|
}
|