]> git.saurik.com Git - redis.git/blame - src/t_set.c
Change function name to match what it does
[redis.git] / src / t_set.c
CommitLineData
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. */
10robj *setTypeCreate(robj *value) {
ec7e1389 11 if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK)
96ffb2fe
PN
12 return createIntsetObject();
13 return createSetObject();
14}
15
16int 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 50int 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
69int 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 83setTypeIterator *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 97void 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
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. */
cb72d0f1 106robj *setTypeNext(setTypeIterator *si) {
96ffb2fe
PN
107 robj *ret = NULL;
108 if (si->encoding == REDIS_ENCODING_HT) {
109 dictEntry *de = dictNext(si->di);
110 if (de != NULL) {
111 ret = dictGetEntryKey(de);
112 incrRefCount(ret);
113 }
114 } else if (si->encoding == REDIS_ENCODING_INTSET) {
23c64fe5 115 int64_t llval;
96ffb2fe
PN
116 if (intsetGet(si->subject->ptr,si->ii++,&llval))
117 ret = createStringObjectFromLongLong(llval);
118 }
119 return ret;
120}
121
122
a5be65f7 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
126 * is a regular set.
127 *
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.
132 *
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. */
136int 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);
96ffb2fe
PN
142 } else {
143 redisPanic("Unknown set encoding");
144 }
a5be65f7 145 return setobj->encoding;
96ffb2fe
PN
146}
147
148unsigned 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);
153 } else {
154 redisPanic("Unknown set encoding");
155 }
156}
157
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
160 * set. */
161void setTypeConvert(robj *subject, int enc) {
cb72d0f1 162 setTypeIterator *si;
96ffb2fe
PN
163 robj *element;
164 redisAssert(subject->type == REDIS_SET);
165
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));
170
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);
176
177 subject->encoding = REDIS_ENCODING_HT;
178 zfree(subject->ptr);
179 subject->ptr = d;
180 } else {
181 redisPanic("Unsupported set conversion");
182 }
183}
184
e2641e09 185void saddCommand(redisClient *c) {
186 robj *set;
187
188 set = lookupKeyWrite(c->db,c->argv[1]);
75b41de8 189 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 190 if (set == NULL) {
96ffb2fe 191 set = setTypeCreate(c->argv[2]);
e2641e09 192 dbAdd(c->db,c->argv[1],set);
193 } else {
194 if (set->type != REDIS_SET) {
195 addReply(c,shared.wrongtypeerr);
196 return;
197 }
198 }
96ffb2fe 199 if (setTypeAdd(set,c->argv[2])) {
5b4bff9c 200 touchWatchedKey(c->db,c->argv[1]);
e2641e09 201 server.dirty++;
202 addReply(c,shared.cone);
203 } else {
204 addReply(c,shared.czero);
205 }
206}
207
208void sremCommand(redisClient *c) {
209 robj *set;
210
211 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
212 checkType(c,set,REDIS_SET)) return;
213
75b41de8 214 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2fe
PN
215 if (setTypeRemove(set,c->argv[2])) {
216 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
5b4bff9c 217 touchWatchedKey(c->db,c->argv[1]);
e2641e09 218 server.dirty++;
e2641e09 219 addReply(c,shared.cone);
220 } else {
221 addReply(c,shared.czero);
222 }
223}
224
225void smoveCommand(redisClient *c) {
96ffb2fe 226 robj *srcset, *dstset, *ele;
e2641e09 227 srcset = lookupKeyWrite(c->db,c->argv[1]);
228 dstset = lookupKeyWrite(c->db,c->argv[2]);
75b41de8 229 ele = c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e09 230
96ffb2fe
PN
231 /* If the source key does not exist return 0 */
232 if (srcset == NULL) {
233 addReply(c,shared.czero);
e2641e09 234 return;
235 }
96ffb2fe
PN
236
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;
241
242 /* If srcset and dstset are equal, SMOVE is a no-op */
243 if (srcset == dstset) {
244 addReply(c,shared.cone);
e2641e09 245 return;
246 }
96ffb2fe
PN
247
248 /* If the element cannot be removed from the src set, return 0. */
249 if (!setTypeRemove(srcset,ele)) {
e2641e09 250 addReply(c,shared.czero);
251 return;
252 }
96ffb2fe
PN
253
254 /* Remove the src set from the database when empty */
255 if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]);
5b4bff9c 256 touchWatchedKey(c->db,c->argv[1]);
257 touchWatchedKey(c->db,c->argv[2]);
e2641e09 258 server.dirty++;
96ffb2fe
PN
259
260 /* Create the destination set when it doesn't exist */
e2641e09 261 if (!dstset) {
96ffb2fe 262 dstset = setTypeCreate(ele);
e2641e09 263 dbAdd(c->db,c->argv[2],dstset);
264 }
96ffb2fe
PN
265
266 /* An extra key has changed when ele was successfully added to dstset */
267 if (setTypeAdd(dstset,ele)) server.dirty++;
e2641e09 268 addReply(c,shared.cone);
269}
270
271void sismemberCommand(redisClient *c) {
272 robj *set;
273
274 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
275 checkType(c,set,REDIS_SET)) return;
276
75b41de8 277 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2fe 278 if (setTypeIsMember(set,c->argv[2]))
e2641e09 279 addReply(c,shared.cone);
280 else
281 addReply(c,shared.czero);
282}
283
284void scardCommand(redisClient *c) {
285 robj *o;
e2641e09 286
287 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
288 checkType(c,o,REDIS_SET)) return;
289
b70d3555 290 addReplyLongLong(c,setTypeSize(o));
e2641e09 291}
292
293void spopCommand(redisClient *c) {
96ffb2fe 294 robj *set, *ele;
a5be65f7 295 long long llele;
296 int encoding;
e2641e09 297
298 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
299 checkType(c,set,REDIS_SET)) return;
300
a5be65f7 301 encoding = setTypeRandomElement(set,&ele,&llele);
302 if (encoding == REDIS_ENCODING_INTSET) {
303 addReplyBulkLongLong(c,llele);
304 set->ptr = intsetRemove(set->ptr,llele,NULL);
e2641e09 305 } else {
e2641e09 306 addReplyBulk(c,ele);
a5be65f7 307 setTypeRemove(set,ele);
e2641e09 308 }
a5be65f7 309 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
310 touchWatchedKey(c->db,c->argv[1]);
311 server.dirty++;
e2641e09 312}
313
314void srandmemberCommand(redisClient *c) {
96ffb2fe 315 robj *set, *ele;
a5be65f7 316 long long llele;
317 int encoding;
e2641e09 318
319 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
320 checkType(c,set,REDIS_SET)) return;
321
a5be65f7 322 encoding = setTypeRandomElement(set,&ele,&llele);
323 if (encoding == REDIS_ENCODING_INTSET) {
324 addReplyBulkLongLong(c,llele);
e2641e09 325 } else {
e2641e09 326 addReplyBulk(c,ele);
327 }
328}
329
330int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
96ffb2fe 331 return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
e2641e09 332}
333
96ffb2fe
PN
334void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
335 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 336 setTypeIterator *si;
b301c1fc
PN
337 robj *ele, *dstset = NULL;
338 void *replylen = NULL;
e2641e09 339 unsigned long j, cardinality = 0;
340
96ffb2fe
PN
341 for (j = 0; j < setnum; j++) {
342 robj *setobj = dstkey ?
343 lookupKeyWrite(c->db,setkeys[j]) :
344 lookupKeyRead(c->db,setkeys[j]);
e2641e09 345 if (!setobj) {
96ffb2fe 346 zfree(sets);
e2641e09 347 if (dstkey) {
5b4bff9c 348 if (dbDelete(c->db,dstkey)) {
349 touchWatchedKey(c->db,dstkey);
e2641e09 350 server.dirty++;
5b4bff9c 351 }
e2641e09 352 addReply(c,shared.czero);
353 } else {
354 addReply(c,shared.emptymultibulk);
355 }
356 return;
357 }
96ffb2fe
PN
358 if (checkType(c,setobj,REDIS_SET)) {
359 zfree(sets);
e2641e09 360 return;
361 }
96ffb2fe 362 sets[j] = setobj;
e2641e09 363 }
364 /* Sort sets from the smallest to largest, this will improve our
365 * algorithm's performace */
96ffb2fe 366 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
e2641e09 367
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
372 * right length */
373 if (!dstkey) {
b301c1fc 374 replylen = addDeferredMultiBulkLength(c);
e2641e09 375 } else {
376 /* If we have a target key where to store the resulting set
377 * create this key with an empty set inside */
96ffb2fe 378 dstset = createIntsetObject();
e2641e09 379 }
380
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 */
96ffb2fe
PN
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;
388
389 /* Only take action when all sets contain the member */
390 if (j == setnum) {
391 if (!dstkey) {
392 addReplyBulk(c,ele);
393 cardinality++;
394 } else {
395 setTypeAdd(dstset,ele);
396 }
e2641e09 397 }
96ffb2fe 398 decrRefCount(ele);
e2641e09 399 }
96ffb2fe 400 setTypeReleaseIterator(si);
e2641e09 401
402 if (dstkey) {
403 /* Store the resulting set into the target, if the intersection
404 * is not an empty set. */
405 dbDelete(c->db,dstkey);
96ffb2fe 406 if (setTypeSize(dstset) > 0) {
e2641e09 407 dbAdd(c->db,dstkey,dstset);
96ffb2fe 408 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 409 } else {
410 decrRefCount(dstset);
411 addReply(c,shared.czero);
412 }
5b4bff9c 413 touchWatchedKey(c->db,dstkey);
e2641e09 414 server.dirty++;
415 } else {
b301c1fc 416 setDeferredMultiBulkLength(c,replylen,cardinality);
e2641e09 417 }
96ffb2fe 418 zfree(sets);
e2641e09 419}
420
421void sinterCommand(redisClient *c) {
422 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
423}
424
425void sinterstoreCommand(redisClient *c) {
426 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
427}
428
96ffb2fe
PN
429#define REDIS_OP_UNION 0
430#define REDIS_OP_DIFF 1
431#define REDIS_OP_INTER 2
e2641e09 432
96ffb2fe
PN
433void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
434 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 435 setTypeIterator *si;
96ffb2fe
PN
436 robj *ele, *dstset = NULL;
437 int j, cardinality = 0;
e2641e09 438
96ffb2fe
PN
439 for (j = 0; j < setnum; j++) {
440 robj *setobj = dstkey ?
441 lookupKeyWrite(c->db,setkeys[j]) :
442 lookupKeyRead(c->db,setkeys[j]);
e2641e09 443 if (!setobj) {
96ffb2fe 444 sets[j] = NULL;
e2641e09 445 continue;
446 }
96ffb2fe
PN
447 if (checkType(c,setobj,REDIS_SET)) {
448 zfree(sets);
e2641e09 449 return;
450 }
96ffb2fe 451 sets[j] = setobj;
e2641e09 452 }
453
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*/
96ffb2fe 457 dstset = createIntsetObject();
e2641e09 458
459 /* Iterate all the elements of all the sets, add every element a single
460 * time to the result set */
96ffb2fe
PN
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 */
e2641e09 464
96ffb2fe
PN
465 si = setTypeInitIterator(sets[j]);
466 while((ele = setTypeNext(si)) != NULL) {
e2641e09 467 if (op == REDIS_OP_UNION || j == 0) {
96ffb2fe 468 if (setTypeAdd(dstset,ele)) {
e2641e09 469 cardinality++;
470 }
471 } else if (op == REDIS_OP_DIFF) {
96ffb2fe 472 if (setTypeRemove(dstset,ele)) {
e2641e09 473 cardinality--;
474 }
475 }
96ffb2fe 476 decrRefCount(ele);
e2641e09 477 }
96ffb2fe 478 setTypeReleaseIterator(si);
e2641e09 479
96ffb2fe 480 /* Exit when result set is empty. */
e2641e09 481 if (op == REDIS_OP_DIFF && cardinality == 0) break;
482 }
483
484 /* Output the content of the resulting set, if not in STORE mode */
485 if (!dstkey) {
0537e7bf 486 addReplyMultiBulkLen(c,cardinality);
96ffb2fe
PN
487 si = setTypeInitIterator(dstset);
488 while((ele = setTypeNext(si)) != NULL) {
e2641e09 489 addReplyBulk(c,ele);
96ffb2fe 490 decrRefCount(ele);
e2641e09 491 }
96ffb2fe 492 setTypeReleaseIterator(si);
e2641e09 493 decrRefCount(dstset);
494 } else {
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);
96ffb2fe 498 if (setTypeSize(dstset) > 0) {
e2641e09 499 dbAdd(c->db,dstkey,dstset);
96ffb2fe 500 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 501 } else {
502 decrRefCount(dstset);
503 addReply(c,shared.czero);
504 }
5b4bff9c 505 touchWatchedKey(c->db,dstkey);
e2641e09 506 server.dirty++;
507 }
96ffb2fe 508 zfree(sets);
e2641e09 509}
510
511void sunionCommand(redisClient *c) {
512 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
513}
514
515void sunionstoreCommand(redisClient *c) {
516 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
517}
518
519void sdiffCommand(redisClient *c) {
520 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
521}
522
523void sdiffstoreCommand(redisClient *c) {
524 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
525}