]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
2 | ||
3 | /*----------------------------------------------------------------------------- | |
4 | * Set Commands | |
5 | *----------------------------------------------------------------------------*/ | |
6 | ||
96ffb2fe PN |
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 | |
9 | * hash table. */ | |
10 | robj *setTypeCreate(robj *value) { | |
ec7e1389 | 11 | if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK) |
96ffb2fe PN |
12 | return createIntsetObject(); |
13 | return createSetObject(); | |
14 | } | |
15 | ||
16 | int setTypeAdd(robj *subject, robj *value) { | |
17 | long long llval; | |
18 | if (subject->encoding == REDIS_ENCODING_HT) { | |
19 | if (dictAdd(subject->ptr,value,NULL) == DICT_OK) { | |
20 | incrRefCount(value); | |
21 | return 1; | |
22 | } | |
23 | } else if (subject->encoding == REDIS_ENCODING_INTSET) { | |
ec7e1389 | 24 | if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) { |
96ffb2fe PN |
25 | uint8_t success = 0; |
26 | subject->ptr = intsetAdd(subject->ptr,llval,&success); | |
27 | if (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); | |
32 | return 1; | |
33 | } | |
34 | } else { | |
35 | /* Failed to get integer from object, convert to regular set. */ | |
36 | setTypeConvert(subject,REDIS_ENCODING_HT); | |
37 | ||
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); | |
41 | incrRefCount(value); | |
42 | return 1; | |
43 | } | |
44 | } else { | |
45 | redisPanic("Unknown set encoding"); | |
46 | } | |
47 | return 0; | |
48 | } | |
49 | ||
a5be65f7 | 50 | int setTypeRemove(robj *setobj, robj *value) { |
96ffb2fe | 51 | long long llval; |
a5be65f7 | 52 | if (setobj->encoding == REDIS_ENCODING_HT) { |
53 | if (dictDelete(setobj->ptr,value) == DICT_OK) { | |
54 | if (htNeedsResize(setobj->ptr)) dictResize(setobj->ptr); | |
96ffb2fe PN |
55 | return 1; |
56 | } | |
a5be65f7 | 57 | } else if (setobj->encoding == REDIS_ENCODING_INTSET) { |
ec7e1389 | 58 | if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) { |
a5be65f7 | 59 | int success; |
60 | setobj->ptr = intsetRemove(setobj->ptr,llval,&success); | |
96ffb2fe PN |
61 | if (success) return 1; |
62 | } | |
63 | } else { | |
64 | redisPanic("Unknown set encoding"); | |
65 | } | |
66 | return 0; | |
67 | } | |
68 | ||
69 | int setTypeIsMember(robj *subject, robj *value) { | |
70 | long long llval; | |
71 | if (subject->encoding == REDIS_ENCODING_HT) { | |
72 | return dictFind((dict*)subject->ptr,value) != NULL; | |
73 | } else if (subject->encoding == REDIS_ENCODING_INTSET) { | |
ec7e1389 | 74 | if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) { |
96ffb2fe PN |
75 | return intsetFind((intset*)subject->ptr,llval); |
76 | } | |
77 | } else { | |
78 | redisPanic("Unknown set encoding"); | |
79 | } | |
80 | return 0; | |
81 | } | |
82 | ||
cb72d0f1 | 83 | setTypeIterator *setTypeInitIterator(robj *subject) { |
740eee1c | 84 | setTypeIterator *si = zmalloc(sizeof(setTypeIterator)); |
96ffb2fe PN |
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) { | |
90 | si->ii = 0; | |
91 | } else { | |
92 | redisPanic("Unknown set encoding"); | |
93 | } | |
94 | return si; | |
95 | } | |
96 | ||
cb72d0f1 | 97 | void setTypeReleaseIterator(setTypeIterator *si) { |
96ffb2fe PN |
98 | if (si->encoding == REDIS_ENCODING_HT) |
99 | dictReleaseIterator(si->di); | |
100 | zfree(si); | |
101 | } | |
102 | ||
103 | /* Move to the next entry in the set. Returns the object at the current | |
1b508da7 | 104 | * position. |
105 | * | |
106 | * Since set elements can be internally be stored as redis objects or | |
107 | * simple arrays of integers, setTypeNext returns the encoding of the | |
108 | * set object you are iterating, and will populate the appropriate pointer | |
109 | * (eobj) or (llobj) accordingly. | |
110 | * | |
111 | * When there are no longer elements -1 is returned. | |
112 | * Returned objects ref count is not incremented, so this function is | |
113 | * copy on write friendly. */ | |
114 | int setTypeNext(setTypeIterator *si, robj **objele, int64_t *llele) { | |
96ffb2fe PN |
115 | if (si->encoding == REDIS_ENCODING_HT) { |
116 | dictEntry *de = dictNext(si->di); | |
1b508da7 | 117 | if (de == NULL) return -1; |
118 | *objele = dictGetEntryKey(de); | |
96ffb2fe | 119 | } else if (si->encoding == REDIS_ENCODING_INTSET) { |
1b508da7 | 120 | if (!intsetGet(si->subject->ptr,si->ii++,llele)) |
121 | return -1; | |
96ffb2fe | 122 | } |
1b508da7 | 123 | return si->encoding; |
96ffb2fe PN |
124 | } |
125 | ||
1b508da7 | 126 | /* The not copy on write friendly version but easy to use version |
127 | * of setTypeNext() is setTypeNextObject(), returning new objects | |
128 | * or incrementing the ref count of returned objects. So if you don't | |
129 | * retain a pointer to this object you should call decrRefCount() against it. | |
130 | * | |
131 | * This function is the way to go for write operations where COW is not | |
132 | * an issue as the result will be anyway of incrementing the ref count. */ | |
133 | robj *setTypeNextObject(setTypeIterator *si) { | |
134 | int64_t intele; | |
135 | robj *objele; | |
136 | int encoding; | |
137 | ||
138 | encoding = setTypeNext(si,&objele,&intele); | |
139 | switch(encoding) { | |
140 | case -1: return NULL; | |
141 | case REDIS_ENCODING_INTSET: | |
142 | return createStringObjectFromLongLong(intele); | |
143 | case REDIS_ENCODING_HT: | |
144 | incrRefCount(objele); | |
145 | return objele; | |
146 | default: | |
147 | redisPanic("Unsupported encoding"); | |
148 | } | |
149 | return NULL; /* just to suppress warnings */ | |
150 | } | |
96ffb2fe | 151 | |
a5be65f7 | 152 | /* Return random element from a non empty set. |
1b508da7 | 153 | * The returned element can be a int64_t value if the set is encoded |
a5be65f7 | 154 | * as an "intset" blob of integers, or a redis object if the set |
155 | * is a regular set. | |
156 | * | |
157 | * The caller provides both pointers to be populated with the right | |
158 | * object. The return value of the function is the object->encoding | |
159 | * field of the object and is used by the caller to check if the | |
1b508da7 | 160 | * int64_t pointer or the redis object pointere was populated. |
a5be65f7 | 161 | * |
162 | * When an object is returned (the set was a real set) the ref count | |
163 | * of the object is not incremented so this function can be considered | |
1b508da7 | 164 | * copy on write friendly. */ |
165 | int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele) { | |
a5be65f7 | 166 | if (setobj->encoding == REDIS_ENCODING_HT) { |
167 | dictEntry *de = dictGetRandomKey(setobj->ptr); | |
168 | *objele = dictGetEntryKey(de); | |
169 | } else if (setobj->encoding == REDIS_ENCODING_INTSET) { | |
170 | *llele = intsetRandom(setobj->ptr); | |
96ffb2fe PN |
171 | } else { |
172 | redisPanic("Unknown set encoding"); | |
173 | } | |
a5be65f7 | 174 | return setobj->encoding; |
96ffb2fe PN |
175 | } |
176 | ||
177 | unsigned long setTypeSize(robj *subject) { | |
178 | if (subject->encoding == REDIS_ENCODING_HT) { | |
179 | return dictSize((dict*)subject->ptr); | |
180 | } else if (subject->encoding == REDIS_ENCODING_INTSET) { | |
181 | return intsetLen((intset*)subject->ptr); | |
182 | } else { | |
183 | redisPanic("Unknown set encoding"); | |
184 | } | |
185 | } | |
186 | ||
187 | /* Convert the set to specified encoding. The resulting dict (when converting | |
188 | * to a hashtable) is presized to hold the number of elements in the original | |
189 | * set. */ | |
1b508da7 | 190 | void setTypeConvert(robj *setobj, int enc) { |
cb72d0f1 | 191 | setTypeIterator *si; |
1b508da7 | 192 | redisAssert(setobj->type == REDIS_SET && |
193 | setobj->encoding == REDIS_ENCODING_INTSET); | |
96ffb2fe PN |
194 | |
195 | if (enc == REDIS_ENCODING_HT) { | |
1b508da7 | 196 | int64_t intele; |
96ffb2fe | 197 | dict *d = dictCreate(&setDictType,NULL); |
1b508da7 | 198 | robj *element; |
199 | ||
96ffb2fe | 200 | /* Presize the dict to avoid rehashing */ |
1b508da7 | 201 | dictExpand(d,intsetLen(setobj->ptr)); |
96ffb2fe | 202 | |
1b508da7 | 203 | /* To add the elements we extract integers and create redis objects */ |
204 | si = setTypeInitIterator(setobj); | |
205 | while (setTypeNext(si,NULL,&intele) != -1) { | |
206 | element = createStringObjectFromLongLong(intele); | |
96ffb2fe | 207 | redisAssert(dictAdd(d,element,NULL) == DICT_OK); |
1b508da7 | 208 | } |
96ffb2fe PN |
209 | setTypeReleaseIterator(si); |
210 | ||
1b508da7 | 211 | setobj->encoding = REDIS_ENCODING_HT; |
212 | zfree(setobj->ptr); | |
213 | setobj->ptr = d; | |
96ffb2fe PN |
214 | } else { |
215 | redisPanic("Unsupported set conversion"); | |
216 | } | |
217 | } | |
218 | ||
e2641e09 | 219 | void saddCommand(redisClient *c) { |
220 | robj *set; | |
22f294d2 | 221 | int j, added = 0; |
e2641e09 | 222 | |
223 | set = lookupKeyWrite(c->db,c->argv[1]); | |
224 | if (set == NULL) { | |
96ffb2fe | 225 | set = setTypeCreate(c->argv[2]); |
e2641e09 | 226 | dbAdd(c->db,c->argv[1],set); |
227 | } else { | |
228 | if (set->type != REDIS_SET) { | |
229 | addReply(c,shared.wrongtypeerr); | |
230 | return; | |
231 | } | |
232 | } | |
22f294d2 | 233 | |
234 | for (j = 2; j < c->argc; j++) { | |
235 | c->argv[j] = tryObjectEncoding(c->argv[j]); | |
236 | if (setTypeAdd(set,c->argv[j])) added++; | |
e2641e09 | 237 | } |
22f294d2 | 238 | if (added) signalModifiedKey(c->db,c->argv[1]); |
239 | server.dirty += added; | |
240 | addReplyLongLong(c,added); | |
e2641e09 | 241 | } |
242 | ||
243 | void sremCommand(redisClient *c) { | |
244 | robj *set; | |
b3a96d45 | 245 | int j, deleted = 0; |
e2641e09 | 246 | |
247 | if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || | |
248 | checkType(c,set,REDIS_SET)) return; | |
249 | ||
b3a96d45 | 250 | for (j = 2; j < c->argc; j++) { |
251 | if (setTypeRemove(set,c->argv[j])) { | |
252 | if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]); | |
253 | deleted++; | |
254 | } | |
255 | } | |
256 | if (deleted) { | |
cea8c5cd | 257 | signalModifiedKey(c->db,c->argv[1]); |
b3a96d45 | 258 | server.dirty += deleted; |
e2641e09 | 259 | } |
b3a96d45 | 260 | addReplyLongLong(c,deleted); |
e2641e09 | 261 | } |
262 | ||
263 | void smoveCommand(redisClient *c) { | |
96ffb2fe | 264 | robj *srcset, *dstset, *ele; |
e2641e09 | 265 | srcset = lookupKeyWrite(c->db,c->argv[1]); |
266 | dstset = lookupKeyWrite(c->db,c->argv[2]); | |
75b41de8 | 267 | ele = c->argv[3] = tryObjectEncoding(c->argv[3]); |
e2641e09 | 268 | |
96ffb2fe PN |
269 | /* If the source key does not exist return 0 */ |
270 | if (srcset == NULL) { | |
271 | addReply(c,shared.czero); | |
e2641e09 | 272 | return; |
273 | } | |
96ffb2fe PN |
274 | |
275 | /* If the source key has the wrong type, or the destination key | |
276 | * is set and has the wrong type, return with an error. */ | |
277 | if (checkType(c,srcset,REDIS_SET) || | |
278 | (dstset && checkType(c,dstset,REDIS_SET))) return; | |
279 | ||
280 | /* If srcset and dstset are equal, SMOVE is a no-op */ | |
281 | if (srcset == dstset) { | |
282 | addReply(c,shared.cone); | |
e2641e09 | 283 | return; |
284 | } | |
96ffb2fe PN |
285 | |
286 | /* If the element cannot be removed from the src set, return 0. */ | |
287 | if (!setTypeRemove(srcset,ele)) { | |
e2641e09 | 288 | addReply(c,shared.czero); |
289 | return; | |
290 | } | |
96ffb2fe PN |
291 | |
292 | /* Remove the src set from the database when empty */ | |
293 | if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]); | |
cea8c5cd | 294 | signalModifiedKey(c->db,c->argv[1]); |
295 | signalModifiedKey(c->db,c->argv[2]); | |
e2641e09 | 296 | server.dirty++; |
96ffb2fe PN |
297 | |
298 | /* Create the destination set when it doesn't exist */ | |
e2641e09 | 299 | if (!dstset) { |
96ffb2fe | 300 | dstset = setTypeCreate(ele); |
e2641e09 | 301 | dbAdd(c->db,c->argv[2],dstset); |
302 | } | |
96ffb2fe PN |
303 | |
304 | /* An extra key has changed when ele was successfully added to dstset */ | |
305 | if (setTypeAdd(dstset,ele)) server.dirty++; | |
e2641e09 | 306 | addReply(c,shared.cone); |
307 | } | |
308 | ||
309 | void sismemberCommand(redisClient *c) { | |
310 | robj *set; | |
311 | ||
312 | if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || | |
313 | checkType(c,set,REDIS_SET)) return; | |
314 | ||
75b41de8 | 315 | c->argv[2] = tryObjectEncoding(c->argv[2]); |
96ffb2fe | 316 | if (setTypeIsMember(set,c->argv[2])) |
e2641e09 | 317 | addReply(c,shared.cone); |
318 | else | |
319 | addReply(c,shared.czero); | |
320 | } | |
321 | ||
322 | void scardCommand(redisClient *c) { | |
323 | robj *o; | |
e2641e09 | 324 | |
325 | if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || | |
326 | checkType(c,o,REDIS_SET)) return; | |
327 | ||
b70d3555 | 328 | addReplyLongLong(c,setTypeSize(o)); |
e2641e09 | 329 | } |
330 | ||
331 | void spopCommand(redisClient *c) { | |
96ffb2fe | 332 | robj *set, *ele; |
1b508da7 | 333 | int64_t llele; |
a5be65f7 | 334 | int encoding; |
e2641e09 | 335 | |
336 | if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL || | |
337 | checkType(c,set,REDIS_SET)) return; | |
338 | ||
a5be65f7 | 339 | encoding = setTypeRandomElement(set,&ele,&llele); |
340 | if (encoding == REDIS_ENCODING_INTSET) { | |
30318c1d | 341 | ele = createStringObjectFromLongLong(llele); |
a5be65f7 | 342 | set->ptr = intsetRemove(set->ptr,llele,NULL); |
e2641e09 | 343 | } else { |
30318c1d | 344 | incrRefCount(ele); |
a5be65f7 | 345 | setTypeRemove(set,ele); |
e2641e09 | 346 | } |
30318c1d | 347 | |
348 | /* Change argv to replicate as SREM */ | |
349 | c->argc = 3; | |
350 | c->argv = zrealloc(c->argv,sizeof(robj*)*(c->argc)); | |
351 | ||
352 | /* Overwrite SREM with SPOP (same length) */ | |
353 | redisAssert(sdslen(c->argv[0]->ptr) == 4); | |
354 | memcpy(c->argv[0]->ptr, "SREM", 4); | |
355 | ||
356 | /* Popped element already has incremented refcount */ | |
357 | c->argv[2] = ele; | |
358 | ||
359 | addReplyBulk(c,ele); | |
a5be65f7 | 360 | if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]); |
cea8c5cd | 361 | signalModifiedKey(c->db,c->argv[1]); |
a5be65f7 | 362 | server.dirty++; |
e2641e09 | 363 | } |
364 | ||
365 | void srandmemberCommand(redisClient *c) { | |
96ffb2fe | 366 | robj *set, *ele; |
1b508da7 | 367 | int64_t llele; |
a5be65f7 | 368 | int encoding; |
e2641e09 | 369 | |
370 | if ((set = lookupKeyReadOrReply(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) { | |
375 | addReplyBulkLongLong(c,llele); | |
e2641e09 | 376 | } else { |
e2641e09 | 377 | addReplyBulk(c,ele); |
378 | } | |
379 | } | |
380 | ||
381 | int qsortCompareSetsByCardinality(const void *s1, const void *s2) { | |
96ffb2fe | 382 | return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2); |
e2641e09 | 383 | } |
384 | ||
96ffb2fe PN |
385 | void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) { |
386 | robj **sets = zmalloc(sizeof(robj*)*setnum); | |
cb72d0f1 | 387 | setTypeIterator *si; |
1b508da7 | 388 | robj *eleobj, *dstset = NULL; |
389 | int64_t intobj; | |
b301c1fc | 390 | void *replylen = NULL; |
e2641e09 | 391 | unsigned long j, cardinality = 0; |
1b508da7 | 392 | int encoding; |
e2641e09 | 393 | |
96ffb2fe PN |
394 | for (j = 0; j < setnum; j++) { |
395 | robj *setobj = dstkey ? | |
396 | lookupKeyWrite(c->db,setkeys[j]) : | |
397 | lookupKeyRead(c->db,setkeys[j]); | |
e2641e09 | 398 | if (!setobj) { |
96ffb2fe | 399 | zfree(sets); |
e2641e09 | 400 | if (dstkey) { |
5b4bff9c | 401 | if (dbDelete(c->db,dstkey)) { |
cea8c5cd | 402 | signalModifiedKey(c->db,dstkey); |
e2641e09 | 403 | server.dirty++; |
5b4bff9c | 404 | } |
e2641e09 | 405 | addReply(c,shared.czero); |
406 | } else { | |
407 | addReply(c,shared.emptymultibulk); | |
408 | } | |
409 | return; | |
410 | } | |
96ffb2fe PN |
411 | if (checkType(c,setobj,REDIS_SET)) { |
412 | zfree(sets); | |
e2641e09 | 413 | return; |
414 | } | |
96ffb2fe | 415 | sets[j] = setobj; |
e2641e09 | 416 | } |
417 | /* Sort sets from the smallest to largest, this will improve our | |
418 | * algorithm's performace */ | |
96ffb2fe | 419 | qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality); |
e2641e09 | 420 | |
421 | /* The first thing we should output is the total number of elements... | |
422 | * since this is a multi-bulk write, but at this stage we don't know | |
423 | * the intersection set size, so we use a trick, append an empty object | |
424 | * to the output list and save the pointer to later modify it with the | |
425 | * right length */ | |
426 | if (!dstkey) { | |
b301c1fc | 427 | replylen = addDeferredMultiBulkLength(c); |
e2641e09 | 428 | } else { |
429 | /* If we have a target key where to store the resulting set | |
430 | * create this key with an empty set inside */ | |
96ffb2fe | 431 | dstset = createIntsetObject(); |
e2641e09 | 432 | } |
433 | ||
434 | /* Iterate all the elements of the first (smallest) set, and test | |
435 | * the element against all the other sets, if at least one set does | |
436 | * not include the element it is discarded */ | |
96ffb2fe | 437 | si = setTypeInitIterator(sets[0]); |
1b508da7 | 438 | while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) { |
439 | for (j = 1; j < setnum; j++) { | |
dd1eefa4 | 440 | if (sets[j] == sets[0]) continue; |
1b508da7 | 441 | if (encoding == REDIS_ENCODING_INTSET) { |
442 | /* intset with intset is simple... and fast */ | |
443 | if (sets[j]->encoding == REDIS_ENCODING_INTSET && | |
444 | !intsetFind((intset*)sets[j]->ptr,intobj)) | |
445 | { | |
446 | break; | |
447 | /* in order to compare an integer with an object we | |
448 | * have to use the generic function, creating an object | |
449 | * for this */ | |
450 | } else if (sets[j]->encoding == REDIS_ENCODING_HT) { | |
451 | eleobj = createStringObjectFromLongLong(intobj); | |
452 | if (!setTypeIsMember(sets[j],eleobj)) { | |
453 | decrRefCount(eleobj); | |
454 | break; | |
455 | } | |
456 | decrRefCount(eleobj); | |
457 | } | |
458 | } else if (encoding == REDIS_ENCODING_HT) { | |
459 | /* Optimization... if the source object is integer | |
460 | * encoded AND the target set is an intset, we can get | |
461 | * a much faster path. */ | |
462 | if (eleobj->encoding == REDIS_ENCODING_INT && | |
463 | sets[j]->encoding == REDIS_ENCODING_INTSET && | |
464 | !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr)) | |
465 | { | |
466 | break; | |
467 | /* else... object to object check is easy as we use the | |
468 | * type agnostic API here. */ | |
469 | } else if (!setTypeIsMember(sets[j],eleobj)) { | |
470 | break; | |
471 | } | |
472 | } | |
473 | } | |
96ffb2fe PN |
474 | |
475 | /* Only take action when all sets contain the member */ | |
476 | if (j == setnum) { | |
477 | if (!dstkey) { | |
1b508da7 | 478 | if (encoding == REDIS_ENCODING_HT) |
479 | addReplyBulk(c,eleobj); | |
480 | else | |
481 | addReplyBulkLongLong(c,intobj); | |
96ffb2fe PN |
482 | cardinality++; |
483 | } else { | |
1b508da7 | 484 | if (encoding == REDIS_ENCODING_INTSET) { |
485 | eleobj = createStringObjectFromLongLong(intobj); | |
486 | setTypeAdd(dstset,eleobj); | |
487 | decrRefCount(eleobj); | |
488 | } else { | |
489 | setTypeAdd(dstset,eleobj); | |
490 | } | |
96ffb2fe | 491 | } |
e2641e09 | 492 | } |
493 | } | |
96ffb2fe | 494 | setTypeReleaseIterator(si); |
e2641e09 | 495 | |
496 | if (dstkey) { | |
497 | /* Store the resulting set into the target, if the intersection | |
498 | * is not an empty set. */ | |
499 | dbDelete(c->db,dstkey); | |
96ffb2fe | 500 | if (setTypeSize(dstset) > 0) { |
e2641e09 | 501 | dbAdd(c->db,dstkey,dstset); |
96ffb2fe | 502 | addReplyLongLong(c,setTypeSize(dstset)); |
e2641e09 | 503 | } else { |
504 | decrRefCount(dstset); | |
505 | addReply(c,shared.czero); | |
506 | } | |
cea8c5cd | 507 | signalModifiedKey(c->db,dstkey); |
e2641e09 | 508 | server.dirty++; |
509 | } else { | |
b301c1fc | 510 | setDeferredMultiBulkLength(c,replylen,cardinality); |
e2641e09 | 511 | } |
96ffb2fe | 512 | zfree(sets); |
e2641e09 | 513 | } |
514 | ||
515 | void sinterCommand(redisClient *c) { | |
516 | sinterGenericCommand(c,c->argv+1,c->argc-1,NULL); | |
517 | } | |
518 | ||
519 | void sinterstoreCommand(redisClient *c) { | |
520 | sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]); | |
521 | } | |
522 | ||
96ffb2fe PN |
523 | #define REDIS_OP_UNION 0 |
524 | #define REDIS_OP_DIFF 1 | |
525 | #define REDIS_OP_INTER 2 | |
e2641e09 | 526 | |
96ffb2fe PN |
527 | void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) { |
528 | robj **sets = zmalloc(sizeof(robj*)*setnum); | |
cb72d0f1 | 529 | setTypeIterator *si; |
96ffb2fe PN |
530 | robj *ele, *dstset = NULL; |
531 | int j, cardinality = 0; | |
e2641e09 | 532 | |
96ffb2fe PN |
533 | for (j = 0; j < setnum; j++) { |
534 | robj *setobj = dstkey ? | |
535 | lookupKeyWrite(c->db,setkeys[j]) : | |
536 | lookupKeyRead(c->db,setkeys[j]); | |
e2641e09 | 537 | if (!setobj) { |
96ffb2fe | 538 | sets[j] = NULL; |
e2641e09 | 539 | continue; |
540 | } | |
96ffb2fe PN |
541 | if (checkType(c,setobj,REDIS_SET)) { |
542 | zfree(sets); | |
e2641e09 | 543 | return; |
544 | } | |
96ffb2fe | 545 | sets[j] = setobj; |
e2641e09 | 546 | } |
547 | ||
548 | /* We need a temp set object to store our union. If the dstkey | |
549 | * is not NULL (that is, we are inside an SUNIONSTORE operation) then | |
550 | * this set object will be the resulting object to set into the target key*/ | |
96ffb2fe | 551 | dstset = createIntsetObject(); |
e2641e09 | 552 | |
553 | /* Iterate all the elements of all the sets, add every element a single | |
554 | * time to the result set */ | |
96ffb2fe PN |
555 | for (j = 0; j < setnum; j++) { |
556 | if (op == REDIS_OP_DIFF && j == 0 && !sets[j]) break; /* result set is empty */ | |
557 | if (!sets[j]) continue; /* non existing keys are like empty sets */ | |
e2641e09 | 558 | |
96ffb2fe | 559 | si = setTypeInitIterator(sets[j]); |
1b508da7 | 560 | while((ele = setTypeNextObject(si)) != NULL) { |
e2641e09 | 561 | if (op == REDIS_OP_UNION || j == 0) { |
96ffb2fe | 562 | if (setTypeAdd(dstset,ele)) { |
e2641e09 | 563 | cardinality++; |
564 | } | |
565 | } else if (op == REDIS_OP_DIFF) { | |
96ffb2fe | 566 | if (setTypeRemove(dstset,ele)) { |
e2641e09 | 567 | cardinality--; |
568 | } | |
569 | } | |
96ffb2fe | 570 | decrRefCount(ele); |
e2641e09 | 571 | } |
96ffb2fe | 572 | setTypeReleaseIterator(si); |
e2641e09 | 573 | |
96ffb2fe | 574 | /* Exit when result set is empty. */ |
e2641e09 | 575 | if (op == REDIS_OP_DIFF && cardinality == 0) break; |
576 | } | |
577 | ||
578 | /* Output the content of the resulting set, if not in STORE mode */ | |
579 | if (!dstkey) { | |
0537e7bf | 580 | addReplyMultiBulkLen(c,cardinality); |
96ffb2fe | 581 | si = setTypeInitIterator(dstset); |
1b508da7 | 582 | while((ele = setTypeNextObject(si)) != NULL) { |
e2641e09 | 583 | addReplyBulk(c,ele); |
96ffb2fe | 584 | decrRefCount(ele); |
e2641e09 | 585 | } |
96ffb2fe | 586 | setTypeReleaseIterator(si); |
e2641e09 | 587 | decrRefCount(dstset); |
588 | } else { | |
589 | /* If we have a target key where to store the resulting set | |
590 | * create this key with the result set inside */ | |
591 | dbDelete(c->db,dstkey); | |
96ffb2fe | 592 | if (setTypeSize(dstset) > 0) { |
e2641e09 | 593 | dbAdd(c->db,dstkey,dstset); |
96ffb2fe | 594 | addReplyLongLong(c,setTypeSize(dstset)); |
e2641e09 | 595 | } else { |
596 | decrRefCount(dstset); | |
597 | addReply(c,shared.czero); | |
598 | } | |
cea8c5cd | 599 | signalModifiedKey(c->db,dstkey); |
e2641e09 | 600 | server.dirty++; |
601 | } | |
96ffb2fe | 602 | zfree(sets); |
e2641e09 | 603 | } |
604 | ||
605 | void sunionCommand(redisClient *c) { | |
606 | sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION); | |
607 | } | |
608 | ||
609 | void sunionstoreCommand(redisClient *c) { | |
610 | sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION); | |
611 | } | |
612 | ||
613 | void sdiffCommand(redisClient *c) { | |
614 | sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF); | |
615 | } | |
616 | ||
617 | void sdiffstoreCommand(redisClient *c) { | |
618 | sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF); | |
619 | } |