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
*subject
, robj
*value
) {
52 if (subject
->encoding
== REDIS_ENCODING_HT
) {
53 if (dictDelete(subject
->ptr
,value
) == DICT_OK
) {
54 if (htNeedsResize(subject
->ptr
)) dictResize(subject
->ptr
);
57 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
58 if (isObjectRepresentableAsLongLong(value
,&llval
) == REDIS_OK
) {
60 subject
->ptr
= intsetRemove(subject
->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 set. The returned object will always have
124 * an incremented refcount. */
125 robj
*setTypeRandomElement(robj
*subject
) {
127 if (subject
->encoding
== REDIS_ENCODING_HT
) {
128 dictEntry
*de
= dictGetRandomKey(subject
->ptr
);
129 ret
= dictGetEntryKey(de
);
131 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
132 long long llval
= intsetRandom(subject
->ptr
);
133 ret
= createStringObjectFromLongLong(llval
);
135 redisPanic("Unknown set encoding");
140 unsigned long setTypeSize(robj
*subject
) {
141 if (subject
->encoding
== REDIS_ENCODING_HT
) {
142 return dictSize((dict
*)subject
->ptr
);
143 } else if (subject
->encoding
== REDIS_ENCODING_INTSET
) {
144 return intsetLen((intset
*)subject
->ptr
);
146 redisPanic("Unknown set encoding");
150 /* Convert the set to specified encoding. The resulting dict (when converting
151 * to a hashtable) is presized to hold the number of elements in the original
153 void setTypeConvert(robj
*subject
, int enc
) {
156 redisAssert(subject
->type
== REDIS_SET
);
158 if (enc
== REDIS_ENCODING_HT
) {
159 dict
*d
= dictCreate(&setDictType
,NULL
);
160 /* Presize the dict to avoid rehashing */
161 dictExpand(d
,intsetLen(subject
->ptr
));
163 /* setTypeGet returns a robj with incremented refcount */
164 si
= setTypeInitIterator(subject
);
165 while ((element
= setTypeNext(si
)) != NULL
)
166 redisAssert(dictAdd(d
,element
,NULL
) == DICT_OK
);
167 setTypeReleaseIterator(si
);
169 subject
->encoding
= REDIS_ENCODING_HT
;
173 redisPanic("Unsupported set conversion");
177 void saddCommand(redisClient
*c
) {
180 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
182 set
= setTypeCreate(c
->argv
[2]);
183 dbAdd(c
->db
,c
->argv
[1],set
);
185 if (set
->type
!= REDIS_SET
) {
186 addReply(c
,shared
.wrongtypeerr
);
190 if (setTypeAdd(set
,c
->argv
[2])) {
191 touchWatchedKey(c
->db
,c
->argv
[1]);
193 addReply(c
,shared
.cone
);
195 addReply(c
,shared
.czero
);
199 void sremCommand(redisClient
*c
) {
202 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
203 checkType(c
,set
,REDIS_SET
)) return;
205 if (setTypeRemove(set
,c
->argv
[2])) {
206 if (setTypeSize(set
) == 0) dbDelete(c
->db
,c
->argv
[1]);
207 touchWatchedKey(c
->db
,c
->argv
[1]);
209 addReply(c
,shared
.cone
);
211 addReply(c
,shared
.czero
);
215 void smoveCommand(redisClient
*c
) {
216 robj
*srcset
, *dstset
, *ele
;
217 srcset
= lookupKeyWrite(c
->db
,c
->argv
[1]);
218 dstset
= lookupKeyWrite(c
->db
,c
->argv
[2]);
221 /* If the source key does not exist return 0 */
222 if (srcset
== NULL
) {
223 addReply(c
,shared
.czero
);
227 /* If the source key has the wrong type, or the destination key
228 * is set and has the wrong type, return with an error. */
229 if (checkType(c
,srcset
,REDIS_SET
) ||
230 (dstset
&& checkType(c
,dstset
,REDIS_SET
))) return;
232 /* If srcset and dstset are equal, SMOVE is a no-op */
233 if (srcset
== dstset
) {
234 addReply(c
,shared
.cone
);
238 /* If the element cannot be removed from the src set, return 0. */
239 if (!setTypeRemove(srcset
,ele
)) {
240 addReply(c
,shared
.czero
);
244 /* Remove the src set from the database when empty */
245 if (setTypeSize(srcset
) == 0) dbDelete(c
->db
,c
->argv
[1]);
246 touchWatchedKey(c
->db
,c
->argv
[1]);
247 touchWatchedKey(c
->db
,c
->argv
[2]);
250 /* Create the destination set when it doesn't exist */
252 dstset
= setTypeCreate(ele
);
253 dbAdd(c
->db
,c
->argv
[2],dstset
);
256 /* An extra key has changed when ele was successfully added to dstset */
257 if (setTypeAdd(dstset
,ele
)) server
.dirty
++;
258 addReply(c
,shared
.cone
);
261 void sismemberCommand(redisClient
*c
) {
264 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
265 checkType(c
,set
,REDIS_SET
)) return;
267 if (setTypeIsMember(set
,c
->argv
[2]))
268 addReply(c
,shared
.cone
);
270 addReply(c
,shared
.czero
);
273 void scardCommand(redisClient
*c
) {
276 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
277 checkType(c
,o
,REDIS_SET
)) return;
279 addReplyLongLong(c
,setTypeSize(o
));
282 void spopCommand(redisClient
*c
) {
285 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
286 checkType(c
,set
,REDIS_SET
)) return;
288 ele
= setTypeRandomElement(set
);
290 addReply(c
,shared
.nullbulk
);
292 setTypeRemove(set
,ele
);
295 if (setTypeSize(set
) == 0) dbDelete(c
->db
,c
->argv
[1]);
296 touchWatchedKey(c
->db
,c
->argv
[1]);
301 void srandmemberCommand(redisClient
*c
) {
304 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
305 checkType(c
,set
,REDIS_SET
)) return;
307 ele
= setTypeRandomElement(set
);
309 addReply(c
,shared
.nullbulk
);
316 int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
317 return setTypeSize(*(robj
**)s1
)-setTypeSize(*(robj
**)s2
);
320 void sinterGenericCommand(redisClient
*c
, robj
**setkeys
, unsigned long setnum
, robj
*dstkey
) {
321 robj
**sets
= zmalloc(sizeof(robj
*)*setnum
);
323 robj
*ele
, *dstset
= NULL
;
324 void *replylen
= NULL
;
325 unsigned long j
, cardinality
= 0;
327 for (j
= 0; j
< setnum
; j
++) {
328 robj
*setobj
= dstkey
?
329 lookupKeyWrite(c
->db
,setkeys
[j
]) :
330 lookupKeyRead(c
->db
,setkeys
[j
]);
334 if (dbDelete(c
->db
,dstkey
)) {
335 touchWatchedKey(c
->db
,dstkey
);
338 addReply(c
,shared
.czero
);
340 addReply(c
,shared
.emptymultibulk
);
344 if (checkType(c
,setobj
,REDIS_SET
)) {
350 /* Sort sets from the smallest to largest, this will improve our
351 * algorithm's performace */
352 qsort(sets
,setnum
,sizeof(robj
*),qsortCompareSetsByCardinality
);
354 /* The first thing we should output is the total number of elements...
355 * since this is a multi-bulk write, but at this stage we don't know
356 * the intersection set size, so we use a trick, append an empty object
357 * to the output list and save the pointer to later modify it with the
360 replylen
= addDeferredMultiBulkLength(c
);
362 /* If we have a target key where to store the resulting set
363 * create this key with an empty set inside */
364 dstset
= createIntsetObject();
367 /* Iterate all the elements of the first (smallest) set, and test
368 * the element against all the other sets, if at least one set does
369 * not include the element it is discarded */
370 si
= setTypeInitIterator(sets
[0]);
371 while((ele
= setTypeNext(si
)) != NULL
) {
372 for (j
= 1; j
< setnum
; j
++)
373 if (!setTypeIsMember(sets
[j
],ele
)) break;
375 /* Only take action when all sets contain the member */
381 setTypeAdd(dstset
,ele
);
386 setTypeReleaseIterator(si
);
389 /* Store the resulting set into the target, if the intersection
390 * is not an empty set. */
391 dbDelete(c
->db
,dstkey
);
392 if (setTypeSize(dstset
) > 0) {
393 dbAdd(c
->db
,dstkey
,dstset
);
394 addReplyLongLong(c
,setTypeSize(dstset
));
396 decrRefCount(dstset
);
397 addReply(c
,shared
.czero
);
399 touchWatchedKey(c
->db
,dstkey
);
402 setDeferredMultiBulkLength(c
,replylen
,cardinality
);
407 void sinterCommand(redisClient
*c
) {
408 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
411 void sinterstoreCommand(redisClient
*c
) {
412 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
415 #define REDIS_OP_UNION 0
416 #define REDIS_OP_DIFF 1
417 #define REDIS_OP_INTER 2
419 void sunionDiffGenericCommand(redisClient
*c
, robj
**setkeys
, int setnum
, robj
*dstkey
, int op
) {
420 robj
**sets
= zmalloc(sizeof(robj
*)*setnum
);
422 robj
*ele
, *dstset
= NULL
;
423 int j
, cardinality
= 0;
425 for (j
= 0; j
< setnum
; j
++) {
426 robj
*setobj
= dstkey
?
427 lookupKeyWrite(c
->db
,setkeys
[j
]) :
428 lookupKeyRead(c
->db
,setkeys
[j
]);
433 if (checkType(c
,setobj
,REDIS_SET
)) {
440 /* We need a temp set object to store our union. If the dstkey
441 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
442 * this set object will be the resulting object to set into the target key*/
443 dstset
= createIntsetObject();
445 /* Iterate all the elements of all the sets, add every element a single
446 * time to the result set */
447 for (j
= 0; j
< setnum
; j
++) {
448 if (op
== REDIS_OP_DIFF
&& j
== 0 && !sets
[j
]) break; /* result set is empty */
449 if (!sets
[j
]) continue; /* non existing keys are like empty sets */
451 si
= setTypeInitIterator(sets
[j
]);
452 while((ele
= setTypeNext(si
)) != NULL
) {
453 if (op
== REDIS_OP_UNION
|| j
== 0) {
454 if (setTypeAdd(dstset
,ele
)) {
457 } else if (op
== REDIS_OP_DIFF
) {
458 if (setTypeRemove(dstset
,ele
)) {
464 setTypeReleaseIterator(si
);
466 /* Exit when result set is empty. */
467 if (op
== REDIS_OP_DIFF
&& cardinality
== 0) break;
470 /* Output the content of the resulting set, if not in STORE mode */
472 addReplyMultiBulkLen(c
,cardinality
);
473 si
= setTypeInitIterator(dstset
);
474 while((ele
= setTypeNext(si
)) != NULL
) {
478 setTypeReleaseIterator(si
);
479 decrRefCount(dstset
);
481 /* If we have a target key where to store the resulting set
482 * create this key with the result set inside */
483 dbDelete(c
->db
,dstkey
);
484 if (setTypeSize(dstset
) > 0) {
485 dbAdd(c
->db
,dstkey
,dstset
);
486 addReplyLongLong(c
,setTypeSize(dstset
));
488 decrRefCount(dstset
);
489 addReply(c
,shared
.czero
);
491 touchWatchedKey(c
->db
,dstkey
);
497 void sunionCommand(redisClient
*c
) {
498 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_UNION
);
501 void sunionstoreCommand(redisClient
*c
) {
502 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_UNION
);
505 void sdiffCommand(redisClient
*c
) {
506 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_DIFF
);
509 void sdiffstoreCommand(redisClient
*c
) {
510 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_DIFF
);