]> git.saurik.com Git - redis.git/blame - src/t_set.c
Client should not block multiple times on the same key.
[redis.git] / src / t_set.c
CommitLineData
4365e5b2 1/*
2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
e2641e09 30#include "redis.h"
31
32/*-----------------------------------------------------------------------------
33 * Set Commands
34 *----------------------------------------------------------------------------*/
35
be90c803 36void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op);
37
96ffb2fe
PN
38/* Factory method to return a set that *can* hold "value". When the object has
39 * an integer-encodable value, an intset will be returned. Otherwise a regular
40 * hash table. */
41robj *setTypeCreate(robj *value) {
ec7e1389 42 if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK)
96ffb2fe
PN
43 return createIntsetObject();
44 return createSetObject();
45}
46
47int setTypeAdd(robj *subject, robj *value) {
48 long long llval;
49 if (subject->encoding == REDIS_ENCODING_HT) {
50 if (dictAdd(subject->ptr,value,NULL) == DICT_OK) {
51 incrRefCount(value);
52 return 1;
53 }
54 } else if (subject->encoding == REDIS_ENCODING_INTSET) {
ec7e1389 55 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
96ffb2fe
PN
56 uint8_t success = 0;
57 subject->ptr = intsetAdd(subject->ptr,llval,&success);
58 if (success) {
59 /* Convert to regular set when the intset contains
60 * too many entries. */
61 if (intsetLen(subject->ptr) > server.set_max_intset_entries)
62 setTypeConvert(subject,REDIS_ENCODING_HT);
63 return 1;
64 }
65 } else {
66 /* Failed to get integer from object, convert to regular set. */
67 setTypeConvert(subject,REDIS_ENCODING_HT);
68
69 /* The set *was* an intset and this value is not integer
70 * encodable, so dictAdd should always work. */
eab0e26e 71 redisAssertWithInfo(NULL,value,dictAdd(subject->ptr,value,NULL) == DICT_OK);
96ffb2fe
PN
72 incrRefCount(value);
73 return 1;
74 }
75 } else {
76 redisPanic("Unknown set encoding");
77 }
78 return 0;
79}
80
a5be65f7 81int setTypeRemove(robj *setobj, robj *value) {
96ffb2fe 82 long long llval;
a5be65f7 83 if (setobj->encoding == REDIS_ENCODING_HT) {
84 if (dictDelete(setobj->ptr,value) == DICT_OK) {
85 if (htNeedsResize(setobj->ptr)) dictResize(setobj->ptr);
96ffb2fe
PN
86 return 1;
87 }
a5be65f7 88 } else if (setobj->encoding == REDIS_ENCODING_INTSET) {
ec7e1389 89 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
a5be65f7 90 int success;
91 setobj->ptr = intsetRemove(setobj->ptr,llval,&success);
96ffb2fe
PN
92 if (success) return 1;
93 }
94 } else {
95 redisPanic("Unknown set encoding");
96 }
97 return 0;
98}
99
100int setTypeIsMember(robj *subject, robj *value) {
101 long long llval;
102 if (subject->encoding == REDIS_ENCODING_HT) {
103 return dictFind((dict*)subject->ptr,value) != NULL;
104 } else if (subject->encoding == REDIS_ENCODING_INTSET) {
ec7e1389 105 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
96ffb2fe
PN
106 return intsetFind((intset*)subject->ptr,llval);
107 }
108 } else {
109 redisPanic("Unknown set encoding");
110 }
111 return 0;
112}
113
cb72d0f1 114setTypeIterator *setTypeInitIterator(robj *subject) {
740eee1c 115 setTypeIterator *si = zmalloc(sizeof(setTypeIterator));
96ffb2fe
PN
116 si->subject = subject;
117 si->encoding = subject->encoding;
118 if (si->encoding == REDIS_ENCODING_HT) {
119 si->di = dictGetIterator(subject->ptr);
120 } else if (si->encoding == REDIS_ENCODING_INTSET) {
121 si->ii = 0;
122 } else {
123 redisPanic("Unknown set encoding");
124 }
125 return si;
126}
127
cb72d0f1 128void setTypeReleaseIterator(setTypeIterator *si) {
96ffb2fe
PN
129 if (si->encoding == REDIS_ENCODING_HT)
130 dictReleaseIterator(si->di);
131 zfree(si);
132}
133
134/* Move to the next entry in the set. Returns the object at the current
1b508da7 135 * position.
136 *
137 * Since set elements can be internally be stored as redis objects or
138 * simple arrays of integers, setTypeNext returns the encoding of the
139 * set object you are iterating, and will populate the appropriate pointer
140 * (eobj) or (llobj) accordingly.
141 *
142 * When there are no longer elements -1 is returned.
143 * Returned objects ref count is not incremented, so this function is
144 * copy on write friendly. */
145int setTypeNext(setTypeIterator *si, robj **objele, int64_t *llele) {
96ffb2fe
PN
146 if (si->encoding == REDIS_ENCODING_HT) {
147 dictEntry *de = dictNext(si->di);
1b508da7 148 if (de == NULL) return -1;
c0ba9ebe 149 *objele = dictGetKey(de);
96ffb2fe 150 } else if (si->encoding == REDIS_ENCODING_INTSET) {
1b508da7 151 if (!intsetGet(si->subject->ptr,si->ii++,llele))
152 return -1;
96ffb2fe 153 }
1b508da7 154 return si->encoding;
96ffb2fe
PN
155}
156
1b508da7 157/* The not copy on write friendly version but easy to use version
158 * of setTypeNext() is setTypeNextObject(), returning new objects
159 * or incrementing the ref count of returned objects. So if you don't
160 * retain a pointer to this object you should call decrRefCount() against it.
161 *
162 * This function is the way to go for write operations where COW is not
163 * an issue as the result will be anyway of incrementing the ref count. */
164robj *setTypeNextObject(setTypeIterator *si) {
165 int64_t intele;
166 robj *objele;
167 int encoding;
168
169 encoding = setTypeNext(si,&objele,&intele);
170 switch(encoding) {
171 case -1: return NULL;
172 case REDIS_ENCODING_INTSET:
173 return createStringObjectFromLongLong(intele);
174 case REDIS_ENCODING_HT:
175 incrRefCount(objele);
176 return objele;
177 default:
178 redisPanic("Unsupported encoding");
179 }
180 return NULL; /* just to suppress warnings */
181}
96ffb2fe 182
a5be65f7 183/* Return random element from a non empty set.
1b508da7 184 * The returned element can be a int64_t value if the set is encoded
a5be65f7 185 * as an "intset" blob of integers, or a redis object if the set
186 * is a regular set.
187 *
188 * The caller provides both pointers to be populated with the right
189 * object. The return value of the function is the object->encoding
190 * field of the object and is used by the caller to check if the
1b508da7 191 * int64_t pointer or the redis object pointere was populated.
a5be65f7 192 *
193 * When an object is returned (the set was a real set) the ref count
194 * of the object is not incremented so this function can be considered
1b508da7 195 * copy on write friendly. */
196int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele) {
a5be65f7 197 if (setobj->encoding == REDIS_ENCODING_HT) {
198 dictEntry *de = dictGetRandomKey(setobj->ptr);
c0ba9ebe 199 *objele = dictGetKey(de);
a5be65f7 200 } else if (setobj->encoding == REDIS_ENCODING_INTSET) {
201 *llele = intsetRandom(setobj->ptr);
96ffb2fe
PN
202 } else {
203 redisPanic("Unknown set encoding");
204 }
a5be65f7 205 return setobj->encoding;
96ffb2fe
PN
206}
207
208unsigned long setTypeSize(robj *subject) {
209 if (subject->encoding == REDIS_ENCODING_HT) {
210 return dictSize((dict*)subject->ptr);
211 } else if (subject->encoding == REDIS_ENCODING_INTSET) {
212 return intsetLen((intset*)subject->ptr);
213 } else {
214 redisPanic("Unknown set encoding");
215 }
216}
217
218/* Convert the set to specified encoding. The resulting dict (when converting
65fd32ab 219 * to a hash table) is presized to hold the number of elements in the original
96ffb2fe 220 * set. */
1b508da7 221void setTypeConvert(robj *setobj, int enc) {
cb72d0f1 222 setTypeIterator *si;
eab0e26e 223 redisAssertWithInfo(NULL,setobj,setobj->type == REDIS_SET &&
224 setobj->encoding == REDIS_ENCODING_INTSET);
96ffb2fe
PN
225
226 if (enc == REDIS_ENCODING_HT) {
1b508da7 227 int64_t intele;
96ffb2fe 228 dict *d = dictCreate(&setDictType,NULL);
1b508da7 229 robj *element;
230
96ffb2fe 231 /* Presize the dict to avoid rehashing */
1b508da7 232 dictExpand(d,intsetLen(setobj->ptr));
96ffb2fe 233
1b508da7 234 /* To add the elements we extract integers and create redis objects */
235 si = setTypeInitIterator(setobj);
236 while (setTypeNext(si,NULL,&intele) != -1) {
237 element = createStringObjectFromLongLong(intele);
eab0e26e 238 redisAssertWithInfo(NULL,element,dictAdd(d,element,NULL) == DICT_OK);
1b508da7 239 }
96ffb2fe
PN
240 setTypeReleaseIterator(si);
241
1b508da7 242 setobj->encoding = REDIS_ENCODING_HT;
243 zfree(setobj->ptr);
244 setobj->ptr = d;
96ffb2fe
PN
245 } else {
246 redisPanic("Unsupported set conversion");
247 }
248}
249
e2641e09 250void saddCommand(redisClient *c) {
251 robj *set;
22f294d2 252 int j, added = 0;
e2641e09 253
254 set = lookupKeyWrite(c->db,c->argv[1]);
255 if (set == NULL) {
96ffb2fe 256 set = setTypeCreate(c->argv[2]);
e2641e09 257 dbAdd(c->db,c->argv[1],set);
258 } else {
259 if (set->type != REDIS_SET) {
260 addReply(c,shared.wrongtypeerr);
261 return;
262 }
263 }
22f294d2 264
265 for (j = 2; j < c->argc; j++) {
266 c->argv[j] = tryObjectEncoding(c->argv[j]);
267 if (setTypeAdd(set,c->argv[j])) added++;
e2641e09 268 }
22f294d2 269 if (added) signalModifiedKey(c->db,c->argv[1]);
270 server.dirty += added;
271 addReplyLongLong(c,added);
e2641e09 272}
273
274void sremCommand(redisClient *c) {
275 robj *set;
b3a96d45 276 int j, deleted = 0;
e2641e09 277
278 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
279 checkType(c,set,REDIS_SET)) return;
280
b3a96d45 281 for (j = 2; j < c->argc; j++) {
282 if (setTypeRemove(set,c->argv[j])) {
b3a96d45 283 deleted++;
3738ff5f 284 if (setTypeSize(set) == 0) {
285 dbDelete(c->db,c->argv[1]);
286 break;
287 }
b3a96d45 288 }
289 }
290 if (deleted) {
cea8c5cd 291 signalModifiedKey(c->db,c->argv[1]);
b3a96d45 292 server.dirty += deleted;
e2641e09 293 }
b3a96d45 294 addReplyLongLong(c,deleted);
e2641e09 295}
296
297void smoveCommand(redisClient *c) {
96ffb2fe 298 robj *srcset, *dstset, *ele;
e2641e09 299 srcset = lookupKeyWrite(c->db,c->argv[1]);
300 dstset = lookupKeyWrite(c->db,c->argv[2]);
75b41de8 301 ele = c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e09 302
96ffb2fe
PN
303 /* If the source key does not exist return 0 */
304 if (srcset == NULL) {
305 addReply(c,shared.czero);
e2641e09 306 return;
307 }
96ffb2fe
PN
308
309 /* If the source key has the wrong type, or the destination key
310 * is set and has the wrong type, return with an error. */
311 if (checkType(c,srcset,REDIS_SET) ||
312 (dstset && checkType(c,dstset,REDIS_SET))) return;
313
314 /* If srcset and dstset are equal, SMOVE is a no-op */
315 if (srcset == dstset) {
316 addReply(c,shared.cone);
e2641e09 317 return;
318 }
96ffb2fe
PN
319
320 /* If the element cannot be removed from the src set, return 0. */
321 if (!setTypeRemove(srcset,ele)) {
e2641e09 322 addReply(c,shared.czero);
323 return;
324 }
96ffb2fe
PN
325
326 /* Remove the src set from the database when empty */
327 if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]);
cea8c5cd 328 signalModifiedKey(c->db,c->argv[1]);
329 signalModifiedKey(c->db,c->argv[2]);
e2641e09 330 server.dirty++;
96ffb2fe
PN
331
332 /* Create the destination set when it doesn't exist */
e2641e09 333 if (!dstset) {
96ffb2fe 334 dstset = setTypeCreate(ele);
e2641e09 335 dbAdd(c->db,c->argv[2],dstset);
336 }
96ffb2fe
PN
337
338 /* An extra key has changed when ele was successfully added to dstset */
339 if (setTypeAdd(dstset,ele)) server.dirty++;
e2641e09 340 addReply(c,shared.cone);
341}
342
343void sismemberCommand(redisClient *c) {
344 robj *set;
345
346 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
347 checkType(c,set,REDIS_SET)) return;
348
75b41de8 349 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2fe 350 if (setTypeIsMember(set,c->argv[2]))
e2641e09 351 addReply(c,shared.cone);
352 else
353 addReply(c,shared.czero);
354}
355
356void scardCommand(redisClient *c) {
357 robj *o;
e2641e09 358
359 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
360 checkType(c,o,REDIS_SET)) return;
361
b70d3555 362 addReplyLongLong(c,setTypeSize(o));
e2641e09 363}
364
365void spopCommand(redisClient *c) {
c1c9d551 366 robj *set, *ele, *aux;
1b508da7 367 int64_t llele;
a5be65f7 368 int encoding;
e2641e09 369
370 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
371 checkType(c,set,REDIS_SET)) return;
372
a5be65f7 373 encoding = setTypeRandomElement(set,&ele,&llele);
374 if (encoding == REDIS_ENCODING_INTSET) {
30318c1d 375 ele = createStringObjectFromLongLong(llele);
a5be65f7 376 set->ptr = intsetRemove(set->ptr,llele,NULL);
e2641e09 377 } else {
30318c1d 378 incrRefCount(ele);
a5be65f7 379 setTypeRemove(set,ele);
e2641e09 380 }
30318c1d 381
c1c9d551 382 /* Replicate/AOF this command as an SREM operation */
383 aux = createStringObject("SREM",4);
384 rewriteClientCommandVector(c,3,aux,c->argv[1],ele);
385 decrRefCount(ele);
386 decrRefCount(aux);
30318c1d 387
388 addReplyBulk(c,ele);
a5be65f7 389 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
cea8c5cd 390 signalModifiedKey(c->db,c->argv[1]);
a5be65f7 391 server.dirty++;
e2641e09 392}
393
be90c803 394/* handle the "SRANDMEMBER key <count>" variant. The normal version of the
395 * command is handled by the srandmemberCommand() function itself. */
396
397/* How many times bigger should be the set compared to the requested size
398 * for us to don't use the "remove elements" strategy? Read later in the
399 * implementation for more info. */
400#define SRANDMEMBER_SUB_STRATEGY_MUL 3
401
402void srandmemberWithCountCommand(redisClient *c) {
403 long l;
404 unsigned long count, size;
405 int uniq = 1;
406 robj *set, *ele;
407 int64_t llele;
408 int encoding;
409
410 dict *d;
411
412 if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != REDIS_OK) return;
413 if (l >= 0) {
414 count = (unsigned) l;
415 } else {
416 /* A negative count means: return the same elements multiple times
417 * (i.e. don't remove the extracted element after every extraction). */
418 count = -l;
419 uniq = 0;
420 }
421
422 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk))
423 == NULL || checkType(c,set,REDIS_SET)) return;
424 size = setTypeSize(set);
425
426 /* If count is zero, serve it ASAP to avoid special cases later. */
427 if (count == 0) {
428 addReply(c,shared.emptymultibulk);
429 return;
430 }
431
432 /* CASE 1: The count was negative, so the extraction method is just:
433 * "return N random elements" sampling the whole set every time.
434 * This case is trivial and can be served without auxiliary data
435 * structures. */
436 if (!uniq) {
437 addReplyMultiBulkLen(c,count);
438 while(count--) {
439 encoding = setTypeRandomElement(set,&ele,&llele);
440 if (encoding == REDIS_ENCODING_INTSET) {
441 addReplyBulkLongLong(c,llele);
442 } else {
443 addReplyBulk(c,ele);
444 }
445 }
446 return;
447 }
448
449 /* CASE 2:
450 * The number of requested elements is greater than the number of
451 * elements inside the set: simply return the whole set. */
452 if (count >= size) {
453 sunionDiffGenericCommand(c,c->argv,c->argc-1,NULL,REDIS_OP_UNION);
454 return;
455 }
456
457 /* For CASE 3 and CASE 4 we need an auxiliary dictionary. */
458 d = dictCreate(&setDictType,NULL);
459
460 /* CASE 3:
461 * The number of elements inside the set is not greater than
462 * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
463 * In this case we create a set from scratch with all the elements, and
464 * subtract random elements to reach the requested number of elements.
465 *
466 * This is done because if the number of requsted elements is just
467 * a bit less than the number of elements in the set, the natural approach
468 * used into CASE 3 is highly inefficient. */
469 if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {
470 setTypeIterator *si;
471
472 /* Add all the elements into the temporary dictionary. */
473 si = setTypeInitIterator(set);
474 while((encoding = setTypeNext(si,&ele,&llele)) != -1) {
475 int retval;
476
477 if (encoding == REDIS_ENCODING_INTSET) {
478 retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL);
479 } else if (ele->encoding == REDIS_ENCODING_RAW) {
480 retval = dictAdd(d,dupStringObject(ele),NULL);
481 } else if (ele->encoding == REDIS_ENCODING_INT) {
482 retval = dictAdd(d,
483 createStringObjectFromLongLong((long)ele->ptr),NULL);
484 }
485 redisAssert(retval == DICT_OK);
486 }
487 setTypeReleaseIterator(si);
488 redisAssert(dictSize(d) == size);
489
490 /* Remove random elements to reach the right count. */
491 while(size > count) {
492 dictEntry *de;
493
494 de = dictGetRandomKey(d);
495 dictDelete(d,dictGetKey(de));
496 size--;
497 }
498 }
499
500 /* CASE 4: We have a big set compared to the requested number of elements.
501 * In this case we can simply get random elements from the set and add
502 * to the temporary set, trying to eventually get enough unique elements
503 * to reach the specified count. */
504 else {
505 unsigned long added = 0;
506
507 while(added < count) {
be90c803 508 encoding = setTypeRandomElement(set,&ele,&llele);
509 if (encoding == REDIS_ENCODING_INTSET) {
578c9459 510 ele = createStringObjectFromLongLong(llele);
be90c803 511 } else if (ele->encoding == REDIS_ENCODING_RAW) {
578c9459 512 ele = dupStringObject(ele);
be90c803 513 } else if (ele->encoding == REDIS_ENCODING_INT) {
578c9459 514 ele = createStringObjectFromLongLong((long)ele->ptr);
be90c803 515 }
578c9459 516 /* Try to add the object to the dictionary. If it already exists
517 * free it, otherwise increment the number of objects we have
518 * in the result dictionary. */
519 if (dictAdd(d,ele,NULL) == DICT_OK)
520 added++;
521 else
522 decrRefCount(ele);
be90c803 523 }
524 }
525
526 /* CASE 3 & 4: send the result to the user. */
527 {
528 dictIterator *di;
529 dictEntry *de;
530
531 addReplyMultiBulkLen(c,count);
532 di = dictGetIterator(d);
533 while((de = dictNext(di)) != NULL)
534 addReplyBulk(c,dictGetKey(de));
535 dictReleaseIterator(di);
536 dictRelease(d);
537 }
538}
539
e2641e09 540void srandmemberCommand(redisClient *c) {
96ffb2fe 541 robj *set, *ele;
1b508da7 542 int64_t llele;
a5be65f7 543 int encoding;
e2641e09 544
be90c803 545 if (c->argc == 3) {
546 srandmemberWithCountCommand(c);
547 return;
548 } else if (c->argc > 3) {
549 addReply(c,shared.syntaxerr);
550 return;
551 }
552
e2641e09 553 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
554 checkType(c,set,REDIS_SET)) return;
555
a5be65f7 556 encoding = setTypeRandomElement(set,&ele,&llele);
557 if (encoding == REDIS_ENCODING_INTSET) {
558 addReplyBulkLongLong(c,llele);
e2641e09 559 } else {
e2641e09 560 addReplyBulk(c,ele);
561 }
562}
563
564int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
96ffb2fe 565 return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
e2641e09 566}
567
f50e6584 568/* This is used by SDIFF and in this case we can receive NULL that should
569 * be handled as empty sets. */
570int qsortCompareSetsByRevCardinality(const void *s1, const void *s2) {
571 robj *o1 = *(robj**)s1, *o2 = *(robj**)s2;
572
573 return (o2 ? setTypeSize(o2) : 0) - (o1 ? setTypeSize(o1) : 0);
574}
575
96ffb2fe
PN
576void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
577 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 578 setTypeIterator *si;
1b508da7 579 robj *eleobj, *dstset = NULL;
580 int64_t intobj;
b301c1fc 581 void *replylen = NULL;
e2641e09 582 unsigned long j, cardinality = 0;
1b508da7 583 int encoding;
e2641e09 584
96ffb2fe
PN
585 for (j = 0; j < setnum; j++) {
586 robj *setobj = dstkey ?
587 lookupKeyWrite(c->db,setkeys[j]) :
588 lookupKeyRead(c->db,setkeys[j]);
e2641e09 589 if (!setobj) {
96ffb2fe 590 zfree(sets);
e2641e09 591 if (dstkey) {
5b4bff9c 592 if (dbDelete(c->db,dstkey)) {
cea8c5cd 593 signalModifiedKey(c->db,dstkey);
e2641e09 594 server.dirty++;
5b4bff9c 595 }
e2641e09 596 addReply(c,shared.czero);
597 } else {
598 addReply(c,shared.emptymultibulk);
599 }
600 return;
601 }
96ffb2fe
PN
602 if (checkType(c,setobj,REDIS_SET)) {
603 zfree(sets);
e2641e09 604 return;
605 }
96ffb2fe 606 sets[j] = setobj;
e2641e09 607 }
608 /* Sort sets from the smallest to largest, this will improve our
609 * algorithm's performace */
96ffb2fe 610 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
e2641e09 611
612 /* The first thing we should output is the total number of elements...
613 * since this is a multi-bulk write, but at this stage we don't know
614 * the intersection set size, so we use a trick, append an empty object
615 * to the output list and save the pointer to later modify it with the
616 * right length */
617 if (!dstkey) {
b301c1fc 618 replylen = addDeferredMultiBulkLength(c);
e2641e09 619 } else {
620 /* If we have a target key where to store the resulting set
621 * create this key with an empty set inside */
96ffb2fe 622 dstset = createIntsetObject();
e2641e09 623 }
624
625 /* Iterate all the elements of the first (smallest) set, and test
626 * the element against all the other sets, if at least one set does
627 * not include the element it is discarded */
96ffb2fe 628 si = setTypeInitIterator(sets[0]);
1b508da7 629 while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
630 for (j = 1; j < setnum; j++) {
dd1eefa4 631 if (sets[j] == sets[0]) continue;
1b508da7 632 if (encoding == REDIS_ENCODING_INTSET) {
633 /* intset with intset is simple... and fast */
634 if (sets[j]->encoding == REDIS_ENCODING_INTSET &&
635 !intsetFind((intset*)sets[j]->ptr,intobj))
636 {
637 break;
638 /* in order to compare an integer with an object we
639 * have to use the generic function, creating an object
640 * for this */
641 } else if (sets[j]->encoding == REDIS_ENCODING_HT) {
642 eleobj = createStringObjectFromLongLong(intobj);
643 if (!setTypeIsMember(sets[j],eleobj)) {
644 decrRefCount(eleobj);
645 break;
646 }
647 decrRefCount(eleobj);
648 }
649 } else if (encoding == REDIS_ENCODING_HT) {
650 /* Optimization... if the source object is integer
651 * encoded AND the target set is an intset, we can get
652 * a much faster path. */
653 if (eleobj->encoding == REDIS_ENCODING_INT &&
654 sets[j]->encoding == REDIS_ENCODING_INTSET &&
655 !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
656 {
657 break;
658 /* else... object to object check is easy as we use the
659 * type agnostic API here. */
660 } else if (!setTypeIsMember(sets[j],eleobj)) {
661 break;
662 }
663 }
664 }
96ffb2fe
PN
665
666 /* Only take action when all sets contain the member */
667 if (j == setnum) {
668 if (!dstkey) {
1b508da7 669 if (encoding == REDIS_ENCODING_HT)
670 addReplyBulk(c,eleobj);
671 else
672 addReplyBulkLongLong(c,intobj);
96ffb2fe
PN
673 cardinality++;
674 } else {
1b508da7 675 if (encoding == REDIS_ENCODING_INTSET) {
676 eleobj = createStringObjectFromLongLong(intobj);
677 setTypeAdd(dstset,eleobj);
678 decrRefCount(eleobj);
679 } else {
680 setTypeAdd(dstset,eleobj);
681 }
96ffb2fe 682 }
e2641e09 683 }
684 }
96ffb2fe 685 setTypeReleaseIterator(si);
e2641e09 686
687 if (dstkey) {
688 /* Store the resulting set into the target, if the intersection
689 * is not an empty set. */
690 dbDelete(c->db,dstkey);
96ffb2fe 691 if (setTypeSize(dstset) > 0) {
e2641e09 692 dbAdd(c->db,dstkey,dstset);
96ffb2fe 693 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 694 } else {
695 decrRefCount(dstset);
696 addReply(c,shared.czero);
697 }
cea8c5cd 698 signalModifiedKey(c->db,dstkey);
e2641e09 699 server.dirty++;
700 } else {
b301c1fc 701 setDeferredMultiBulkLength(c,replylen,cardinality);
e2641e09 702 }
96ffb2fe 703 zfree(sets);
e2641e09 704}
705
706void sinterCommand(redisClient *c) {
707 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
708}
709
710void sinterstoreCommand(redisClient *c) {
711 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
712}
713
96ffb2fe
PN
714#define REDIS_OP_UNION 0
715#define REDIS_OP_DIFF 1
716#define REDIS_OP_INTER 2
e2641e09 717
96ffb2fe
PN
718void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
719 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 720 setTypeIterator *si;
96ffb2fe
PN
721 robj *ele, *dstset = NULL;
722 int j, cardinality = 0;
f50e6584 723 int diff_algo = 1;
e2641e09 724
96ffb2fe
PN
725 for (j = 0; j < setnum; j++) {
726 robj *setobj = dstkey ?
727 lookupKeyWrite(c->db,setkeys[j]) :
728 lookupKeyRead(c->db,setkeys[j]);
e2641e09 729 if (!setobj) {
96ffb2fe 730 sets[j] = NULL;
e2641e09 731 continue;
732 }
96ffb2fe
PN
733 if (checkType(c,setobj,REDIS_SET)) {
734 zfree(sets);
e2641e09 735 return;
736 }
96ffb2fe 737 sets[j] = setobj;
e2641e09 738 }
739
f50e6584 740 /* Select what DIFF algorithm to use.
741 *
742 * Algorithm 1 is O(N*M) where N is the size of the element first set
743 * and M the total number of sets.
744 *
745 * Algorithm 2 is O(N) where N is the total number of elements in all
746 * the sets.
747 *
748 * We compute what is the best bet with the current input here. */
749 if (op == REDIS_OP_DIFF && sets[0]) {
750 long long algo_one_work = 0, algo_two_work = 0;
751
752 for (j = 0; j < setnum; j++) {
753 if (sets[j] == NULL) continue;
754
755 algo_one_work += setTypeSize(sets[0]);
756 algo_two_work += setTypeSize(sets[j]);
757 }
758
759 /* Algorithm 1 has better constant times and performs less operations
760 * if there are elements in common. Give it some advantage. */
761 algo_one_work /= 2;
762 diff_algo = (algo_one_work <= algo_two_work) ? 1 : 2;
763
764 if (diff_algo == 1 && setnum > 1) {
765 /* With algorithm 1 it is better to order the sets to subtract
766 * by decreasing size, so that we are more likely to find
767 * duplicated elements ASAP. */
768 qsort(sets+1,setnum-1,sizeof(robj*),
769 qsortCompareSetsByRevCardinality);
770 }
771 }
772
e2641e09 773 /* We need a temp set object to store our union. If the dstkey
774 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
775 * this set object will be the resulting object to set into the target key*/
96ffb2fe 776 dstset = createIntsetObject();
e2641e09 777
f50e6584 778 if (op == REDIS_OP_UNION) {
779 /* Union is trivial, just add every element of every set to the
780 * temporary set. */
781 for (j = 0; j < setnum; j++) {
782 if (!sets[j]) continue; /* non existing keys are like empty sets */
e2641e09 783
f50e6584 784 si = setTypeInitIterator(sets[j]);
785 while((ele = setTypeNextObject(si)) != NULL) {
786 if (setTypeAdd(dstset,ele)) cardinality++;
787 decrRefCount(ele);
788 }
789 setTypeReleaseIterator(si);
790 }
791 } else if (op == REDIS_OP_DIFF && sets[0] && diff_algo == 1) {
792 /* DIFF Algorithm 1:
793 *
794 * We perform the diff by iterating all the elements of the first set,
795 * and only adding it to the target set if the element does not exist
796 * into all the other sets.
797 *
798 * This way we perform at max N*M operations, where N is the size of
799 * the first set, and M the number of sets. */
800 si = setTypeInitIterator(sets[0]);
1b508da7 801 while((ele = setTypeNextObject(si)) != NULL) {
f50e6584 802 for (j = 1; j < setnum; j++) {
803 if (!sets[j]) continue; /* no key is an empty set. */
804 if (setTypeIsMember(sets[j],ele)) break;
805 }
806 if (j == setnum) {
807 /* There is no other set with this element. Add it. */
808 setTypeAdd(dstset,ele);
809 cardinality++;
e2641e09 810 }
96ffb2fe 811 decrRefCount(ele);
e2641e09 812 }
96ffb2fe 813 setTypeReleaseIterator(si);
f50e6584 814 } else if (op == REDIS_OP_DIFF && sets[0] && diff_algo == 2) {
815 /* DIFF Algorithm 2:
816 *
817 * Add all the elements of the first set to the auxiliary set.
818 * Then remove all the elements of all the next sets from it.
819 *
820 * This is O(N) where N is the sum of all the elements in every
821 * set. */
822 for (j = 0; j < setnum; j++) {
823 if (!sets[j]) continue; /* non existing keys are like empty sets */
824
825 si = setTypeInitIterator(sets[j]);
826 while((ele = setTypeNextObject(si)) != NULL) {
827 if (j == 0) {
828 if (setTypeAdd(dstset,ele)) cardinality++;
829 } else {
830 if (setTypeRemove(dstset,ele)) cardinality--;
831 }
832 decrRefCount(ele);
833 }
834 setTypeReleaseIterator(si);
e2641e09 835
f50e6584 836 /* Exit if result set is empty as any additional removal
837 * of elements will have no effect. */
838 if (cardinality == 0) break;
839 }
e2641e09 840 }
841
842 /* Output the content of the resulting set, if not in STORE mode */
843 if (!dstkey) {
0537e7bf 844 addReplyMultiBulkLen(c,cardinality);
96ffb2fe 845 si = setTypeInitIterator(dstset);
1b508da7 846 while((ele = setTypeNextObject(si)) != NULL) {
e2641e09 847 addReplyBulk(c,ele);
96ffb2fe 848 decrRefCount(ele);
e2641e09 849 }
96ffb2fe 850 setTypeReleaseIterator(si);
e2641e09 851 decrRefCount(dstset);
852 } else {
853 /* If we have a target key where to store the resulting set
854 * create this key with the result set inside */
855 dbDelete(c->db,dstkey);
96ffb2fe 856 if (setTypeSize(dstset) > 0) {
e2641e09 857 dbAdd(c->db,dstkey,dstset);
96ffb2fe 858 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 859 } else {
860 decrRefCount(dstset);
861 addReply(c,shared.czero);
862 }
cea8c5cd 863 signalModifiedKey(c->db,dstkey);
e2641e09 864 server.dirty++;
865 }
96ffb2fe 866 zfree(sets);
e2641e09 867}
868
869void sunionCommand(redisClient *c) {
870 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
871}
872
873void sunionstoreCommand(redisClient *c) {
874 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
875}
876
877void sdiffCommand(redisClient *c) {
878 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
879}
880
881void sdiffstoreCommand(redisClient *c) {
882 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
883}