3 /*-----------------------------------------------------------------------------
5 *----------------------------------------------------------------------------*/
7 /* Factory method to return a set that *can* hold "value". When the object has
8 * an integer-encodable value, an intset will be returned. Otherwise a regular
10 robj
*setTypeCreate(robj
*value
) {
11 if (isObjectRepresentableAsLongLong(value
,NULL
) == REDIS_OK
)
12 return createIntsetObject();
13 return createSetObject();
16 int setTypeAdd(robj
*subject
, robj
*value
) {
18 if (subject
->encoding
== REDIS_ENCODING_HT
) {
19 if (dictAdd(subject
->ptr
,value
,NULL
) == DICT_OK
) {
23 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
24 if (isObjectRepresentableAsLongLong(value
,&llval
) == REDIS_OK
) {
26 subject
->ptr
= intsetAdd(subject
->ptr
,llval
,&success
);
28 /* Convert to regular set when the intset contains
29 * too many entries. */
30 if (intsetLen(subject
->ptr
) > server
.set_max_intset_entries
)
31 setTypeConvert(subject
,REDIS_ENCODING_HT
);
35 /* Failed to get integer from object, convert to regular set. */
36 setTypeConvert(subject
,REDIS_ENCODING_HT
);
38 /* The set *was* an intset and this value is not integer
39 * encodable, so dictAdd should always work. */
40 redisAssert(dictAdd(subject
->ptr
,value
,NULL
) == DICT_OK
);
45 redisPanic("Unknown set encoding");
50 int setTypeRemove(robj
*setobj
, robj
*value
) {
52 if (setobj
->encoding
== REDIS_ENCODING_HT
) {
53 if (dictDelete(setobj
->ptr
,value
) == DICT_OK
) {
54 if (htNeedsResize(setobj
->ptr
)) dictResize(setobj
->ptr
);
57 } else if (setobj
->encoding
== REDIS_ENCODING_INTSET
) {
58 if (isObjectRepresentableAsLongLong(value
,&llval
) == REDIS_OK
) {
60 setobj
->ptr
= intsetRemove(setobj
->ptr
,llval
,&success
);
61 if (success
) return 1;
64 redisPanic("Unknown set encoding");
69 int setTypeIsMember(robj
*subject
, robj
*value
) {
71 if (subject
->encoding
== REDIS_ENCODING_HT
) {
72 return dictFind((dict
*)subject
->ptr
,value
) != NULL
;
73 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
74 if (isObjectRepresentableAsLongLong(value
,&llval
) == REDIS_OK
) {
75 return intsetFind((intset
*)subject
->ptr
,llval
);
78 redisPanic("Unknown set encoding");
83 setTypeIterator
*setTypeInitIterator(robj
*subject
) {
84 setTypeIterator
*si
= zmalloc(sizeof(setTypeIterator
));
85 si
->subject
= subject
;
86 si
->encoding
= subject
->encoding
;
87 if (si
->encoding
== REDIS_ENCODING_HT
) {
88 si
->di
= dictGetIterator(subject
->ptr
);
89 } else if (si
->encoding
== REDIS_ENCODING_INTSET
) {
92 redisPanic("Unknown set encoding");
97 void setTypeReleaseIterator(setTypeIterator
*si
) {
98 if (si
->encoding
== REDIS_ENCODING_HT
)
99 dictReleaseIterator(si
->di
);
103 /* Move to the next entry in the set. Returns the object at the current
104 * position, or NULL when the end is reached. This object will have its
105 * refcount incremented, so the caller needs to take care of this. */
106 robj
*setTypeNext(setTypeIterator
*si
) {
108 if (si
->encoding
== REDIS_ENCODING_HT
) {
109 dictEntry
*de
= dictNext(si
->di
);
111 ret
= dictGetEntryKey(de
);
114 } else if (si
->encoding
== REDIS_ENCODING_INTSET
) {
116 if (intsetGet(si
->subject
->ptr
,si
->ii
++,&llval
))
117 ret
= createStringObjectFromLongLong(llval
);
123 /* Return random element from a non empty set.
124 * The returned element can be a long long value if the set is encoded
125 * as an "intset" blob of integers, or a redis object if the set
128 * The caller provides both pointers to be populated with the right
129 * object. The return value of the function is the object->encoding
130 * field of the object and is used by the caller to check if the
131 * long long pointer or the redis object pointere was populated.
133 * When an object is returned (the set was a real set) the ref count
134 * of the object is not incremented so this function can be considered
135 * copy-on-write friendly. */
136 int setTypeRandomElement(robj
*setobj
, robj
**objele
, long long *llele
) {
137 if (setobj
->encoding
== REDIS_ENCODING_HT
) {
138 dictEntry
*de
= dictGetRandomKey(setobj
->ptr
);
139 *objele
= dictGetEntryKey(de
);
140 } else if (setobj
->encoding
== REDIS_ENCODING_INTSET
) {
141 *llele
= intsetRandom(setobj
->ptr
);
143 redisPanic("Unknown set encoding");
145 return setobj
->encoding
;
148 unsigned long setTypeSize(robj
*subject
) {
149 if (subject
->encoding
== REDIS_ENCODING_HT
) {
150 return dictSize((dict
*)subject
->ptr
);
151 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
152 return intsetLen((intset
*)subject
->ptr
);
154 redisPanic("Unknown set encoding");
158 /* Convert the set to specified encoding. The resulting dict (when converting
159 * to a hashtable) is presized to hold the number of elements in the original
161 void setTypeConvert(robj
*subject
, int enc
) {
164 redisAssert(subject
->type
== REDIS_SET
);
166 if (enc
== REDIS_ENCODING_HT
) {
167 dict
*d
= dictCreate(&setDictType
,NULL
);
168 /* Presize the dict to avoid rehashing */
169 dictExpand(d
,intsetLen(subject
->ptr
));
171 /* setTypeGet returns a robj with incremented refcount */
172 si
= setTypeInitIterator(subject
);
173 while ((element
= setTypeNext(si
)) != NULL
)
174 redisAssert(dictAdd(d
,element
,NULL
) == DICT_OK
);
175 setTypeReleaseIterator(si
);
177 subject
->encoding
= REDIS_ENCODING_HT
;
181 redisPanic("Unsupported set conversion");
185 void saddCommand(redisClient
*c
) {
188 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
189 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
191 set
= setTypeCreate(c
->argv
[2]);
192 dbAdd(c
->db
,c
->argv
[1],set
);
194 if (set
->type
!= REDIS_SET
) {
195 addReply(c
,shared
.wrongtypeerr
);
199 if (setTypeAdd(set
,c
->argv
[2])) {
200 touchWatchedKey(c
->db
,c
->argv
[1]);
202 addReply(c
,shared
.cone
);
204 addReply(c
,shared
.czero
);
208 void sremCommand(redisClient
*c
) {
211 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
212 checkType(c
,set
,REDIS_SET
)) return;
214 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
215 if (setTypeRemove(set
,c
->argv
[2])) {
216 if (setTypeSize(set
) == 0) dbDelete(c
->db
,c
->argv
[1]);
217 touchWatchedKey(c
->db
,c
->argv
[1]);
219 addReply(c
,shared
.cone
);
221 addReply(c
,shared
.czero
);
225 void smoveCommand(redisClient
*c
) {
226 robj
*srcset
, *dstset
, *ele
;
227 srcset
= lookupKeyWrite(c
->db
,c
->argv
[1]);
228 dstset
= lookupKeyWrite(c
->db
,c
->argv
[2]);
229 ele
= c
->argv
[3] = tryObjectEncoding(c
->argv
[3]);
231 /* If the source key does not exist return 0 */
232 if (srcset
== NULL
) {
233 addReply(c
,shared
.czero
);
237 /* If the source key has the wrong type, or the destination key
238 * is set and has the wrong type, return with an error. */
239 if (checkType(c
,srcset
,REDIS_SET
) ||
240 (dstset
&& checkType(c
,dstset
,REDIS_SET
))) return;
242 /* If srcset and dstset are equal, SMOVE is a no-op */
243 if (srcset
== dstset
) {
244 addReply(c
,shared
.cone
);
248 /* If the element cannot be removed from the src set, return 0. */
249 if (!setTypeRemove(srcset
,ele
)) {
250 addReply(c
,shared
.czero
);
254 /* Remove the src set from the database when empty */
255 if (setTypeSize(srcset
) == 0) dbDelete(c
->db
,c
->argv
[1]);
256 touchWatchedKey(c
->db
,c
->argv
[1]);
257 touchWatchedKey(c
->db
,c
->argv
[2]);
260 /* Create the destination set when it doesn't exist */
262 dstset
= setTypeCreate(ele
);
263 dbAdd(c
->db
,c
->argv
[2],dstset
);
266 /* An extra key has changed when ele was successfully added to dstset */
267 if (setTypeAdd(dstset
,ele
)) server
.dirty
++;
268 addReply(c
,shared
.cone
);
271 void sismemberCommand(redisClient
*c
) {
274 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
275 checkType(c
,set
,REDIS_SET
)) return;
277 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
278 if (setTypeIsMember(set
,c
->argv
[2]))
279 addReply(c
,shared
.cone
);
281 addReply(c
,shared
.czero
);
284 void scardCommand(redisClient
*c
) {
287 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
288 checkType(c
,o
,REDIS_SET
)) return;
290 addReplyLongLong(c
,setTypeSize(o
));
293 void spopCommand(redisClient
*c
) {
298 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
299 checkType(c
,set
,REDIS_SET
)) return;
301 encoding
= setTypeRandomElement(set
,&ele
,&llele
);
302 if (encoding
== REDIS_ENCODING_INTSET
) {
303 addReplyBulkLongLong(c
,llele
);
304 set
->ptr
= intsetRemove(set
->ptr
,llele
,NULL
);
307 setTypeRemove(set
,ele
);
309 if (setTypeSize(set
) == 0) dbDelete(c
->db
,c
->argv
[1]);
310 touchWatchedKey(c
->db
,c
->argv
[1]);
314 void srandmemberCommand(redisClient
*c
) {
319 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
320 checkType(c
,set
,REDIS_SET
)) return;
322 encoding
= setTypeRandomElement(set
,&ele
,&llele
);
323 if (encoding
== REDIS_ENCODING_INTSET
) {
324 addReplyBulkLongLong(c
,llele
);
330 int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
331 return setTypeSize(*(robj
**)s1
)-setTypeSize(*(robj
**)s2
);
334 void sinterGenericCommand(redisClient
*c
, robj
**setkeys
, unsigned long setnum
, robj
*dstkey
) {
335 robj
**sets
= zmalloc(sizeof(robj
*)*setnum
);
337 robj
*ele
, *dstset
= NULL
;
338 void *replylen
= NULL
;
339 unsigned long j
, cardinality
= 0;
341 for (j
= 0; j
< setnum
; j
++) {
342 robj
*setobj
= dstkey
?
343 lookupKeyWrite(c
->db
,setkeys
[j
]) :
344 lookupKeyRead(c
->db
,setkeys
[j
]);
348 if (dbDelete(c
->db
,dstkey
)) {
349 touchWatchedKey(c
->db
,dstkey
);
352 addReply(c
,shared
.czero
);
354 addReply(c
,shared
.emptymultibulk
);
358 if (checkType(c
,setobj
,REDIS_SET
)) {
364 /* Sort sets from the smallest to largest, this will improve our
365 * algorithm's performace */
366 qsort(sets
,setnum
,sizeof(robj
*),qsortCompareSetsByCardinality
);
368 /* The first thing we should output is the total number of elements...
369 * since this is a multi-bulk write, but at this stage we don't know
370 * the intersection set size, so we use a trick, append an empty object
371 * to the output list and save the pointer to later modify it with the
374 replylen
= addDeferredMultiBulkLength(c
);
376 /* If we have a target key where to store the resulting set
377 * create this key with an empty set inside */
378 dstset
= createIntsetObject();
381 /* Iterate all the elements of the first (smallest) set, and test
382 * the element against all the other sets, if at least one set does
383 * not include the element it is discarded */
384 si
= setTypeInitIterator(sets
[0]);
385 while((ele
= setTypeNext(si
)) != NULL
) {
386 for (j
= 1; j
< setnum
; j
++)
387 if (!setTypeIsMember(sets
[j
],ele
)) break;
389 /* Only take action when all sets contain the member */
395 setTypeAdd(dstset
,ele
);
400 setTypeReleaseIterator(si
);
403 /* Store the resulting set into the target, if the intersection
404 * is not an empty set. */
405 dbDelete(c
->db
,dstkey
);
406 if (setTypeSize(dstset
) > 0) {
407 dbAdd(c
->db
,dstkey
,dstset
);
408 addReplyLongLong(c
,setTypeSize(dstset
));
410 decrRefCount(dstset
);
411 addReply(c
,shared
.czero
);
413 touchWatchedKey(c
->db
,dstkey
);
416 setDeferredMultiBulkLength(c
,replylen
,cardinality
);
421 void sinterCommand(redisClient
*c
) {
422 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
425 void sinterstoreCommand(redisClient
*c
) {
426 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
429 #define REDIS_OP_UNION 0
430 #define REDIS_OP_DIFF 1
431 #define REDIS_OP_INTER 2
433 void sunionDiffGenericCommand(redisClient
*c
, robj
**setkeys
, int setnum
, robj
*dstkey
, int op
) {
434 robj
**sets
= zmalloc(sizeof(robj
*)*setnum
);
436 robj
*ele
, *dstset
= NULL
;
437 int j
, cardinality
= 0;
439 for (j
= 0; j
< setnum
; j
++) {
440 robj
*setobj
= dstkey
?
441 lookupKeyWrite(c
->db
,setkeys
[j
]) :
442 lookupKeyRead(c
->db
,setkeys
[j
]);
447 if (checkType(c
,setobj
,REDIS_SET
)) {
454 /* We need a temp set object to store our union. If the dstkey
455 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
456 * this set object will be the resulting object to set into the target key*/
457 dstset
= createIntsetObject();
459 /* Iterate all the elements of all the sets, add every element a single
460 * time to the result set */
461 for (j
= 0; j
< setnum
; j
++) {
462 if (op
== REDIS_OP_DIFF
&& j
== 0 && !sets
[j
]) break; /* result set is empty */
463 if (!sets
[j
]) continue; /* non existing keys are like empty sets */
465 si
= setTypeInitIterator(sets
[j
]);
466 while((ele
= setTypeNext(si
)) != NULL
) {
467 if (op
== REDIS_OP_UNION
|| j
== 0) {
468 if (setTypeAdd(dstset
,ele
)) {
471 } else if (op
== REDIS_OP_DIFF
) {
472 if (setTypeRemove(dstset
,ele
)) {
478 setTypeReleaseIterator(si
);
480 /* Exit when result set is empty. */
481 if (op
== REDIS_OP_DIFF
&& cardinality
== 0) break;
484 /* Output the content of the resulting set, if not in STORE mode */
486 addReplyMultiBulkLen(c
,cardinality
);
487 si
= setTypeInitIterator(dstset
);
488 while((ele
= setTypeNext(si
)) != NULL
) {
492 setTypeReleaseIterator(si
);
493 decrRefCount(dstset
);
495 /* If we have a target key where to store the resulting set
496 * create this key with the result set inside */
497 dbDelete(c
->db
,dstkey
);
498 if (setTypeSize(dstset
) > 0) {
499 dbAdd(c
->db
,dstkey
,dstset
);
500 addReplyLongLong(c
,setTypeSize(dstset
));
502 decrRefCount(dstset
);
503 addReply(c
,shared
.czero
);
505 touchWatchedKey(c
->db
,dstkey
);
511 void sunionCommand(redisClient
*c
) {
512 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_UNION
);
515 void sunionstoreCommand(redisClient
*c
) {
516 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_UNION
);
519 void sdiffCommand(redisClient
*c
) {
520 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_DIFF
);
523 void sdiffstoreCommand(redisClient
*c
) {
524 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_DIFF
);