]> git.saurik.com Git - redis.git/blame - src/t_set.c
Remove warning.
[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
50int setTypeRemove(robj *subject, robj *value) {
51 long long llval;
52 if (subject->encoding == REDIS_ENCODING_HT) {
53 if (dictDelete(subject->ptr,value) == DICT_OK) {
54 if (htNeedsResize(subject->ptr)) dictResize(subject->ptr);
55 return 1;
56 }
57 } else if (subject->encoding == REDIS_ENCODING_INTSET) {
ec7e1389 58 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
96ffb2fe
PN
59 uint8_t success;
60 subject->ptr = intsetRemove(subject->ptr,llval,&success);
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
123/* Return random element from set. The returned object will always have
124 * an incremented refcount. */
125robj *setTypeRandomElement(robj *subject) {
126 robj *ret = NULL;
127 if (subject->encoding == REDIS_ENCODING_HT) {
128 dictEntry *de = dictGetRandomKey(subject->ptr);
129 ret = dictGetEntryKey(de);
130 incrRefCount(ret);
131 } else if (subject->encoding == REDIS_ENCODING_INTSET) {
132 long long llval = intsetRandom(subject->ptr);
133 ret = createStringObjectFromLongLong(llval);
134 } else {
135 redisPanic("Unknown set encoding");
136 }
137 return ret;
138}
139
140unsigned 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);
145 } else {
146 redisPanic("Unknown set encoding");
147 }
148}
149
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
152 * set. */
153void setTypeConvert(robj *subject, int enc) {
cb72d0f1 154 setTypeIterator *si;
96ffb2fe
PN
155 robj *element;
156 redisAssert(subject->type == REDIS_SET);
157
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));
162
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);
168
169 subject->encoding = REDIS_ENCODING_HT;
170 zfree(subject->ptr);
171 subject->ptr = d;
172 } else {
173 redisPanic("Unsupported set conversion");
174 }
175}
176
e2641e09 177void saddCommand(redisClient *c) {
178 robj *set;
179
180 set = lookupKeyWrite(c->db,c->argv[1]);
75b41de8 181 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 182 if (set == NULL) {
96ffb2fe 183 set = setTypeCreate(c->argv[2]);
e2641e09 184 dbAdd(c->db,c->argv[1],set);
185 } else {
186 if (set->type != REDIS_SET) {
187 addReply(c,shared.wrongtypeerr);
188 return;
189 }
190 }
96ffb2fe 191 if (setTypeAdd(set,c->argv[2])) {
5b4bff9c 192 touchWatchedKey(c->db,c->argv[1]);
e2641e09 193 server.dirty++;
194 addReply(c,shared.cone);
195 } else {
196 addReply(c,shared.czero);
197 }
198}
199
200void sremCommand(redisClient *c) {
201 robj *set;
202
203 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
204 checkType(c,set,REDIS_SET)) return;
205
75b41de8 206 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2fe
PN
207 if (setTypeRemove(set,c->argv[2])) {
208 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
5b4bff9c 209 touchWatchedKey(c->db,c->argv[1]);
e2641e09 210 server.dirty++;
e2641e09 211 addReply(c,shared.cone);
212 } else {
213 addReply(c,shared.czero);
214 }
215}
216
217void smoveCommand(redisClient *c) {
96ffb2fe 218 robj *srcset, *dstset, *ele;
e2641e09 219 srcset = lookupKeyWrite(c->db,c->argv[1]);
220 dstset = lookupKeyWrite(c->db,c->argv[2]);
75b41de8 221 ele = c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e09 222
96ffb2fe
PN
223 /* If the source key does not exist return 0 */
224 if (srcset == NULL) {
225 addReply(c,shared.czero);
e2641e09 226 return;
227 }
96ffb2fe
PN
228
229 /* If the source key has the wrong type, or the destination key
230 * is set and has the wrong type, return with an error. */
231 if (checkType(c,srcset,REDIS_SET) ||
232 (dstset && checkType(c,dstset,REDIS_SET))) return;
233
234 /* If srcset and dstset are equal, SMOVE is a no-op */
235 if (srcset == dstset) {
236 addReply(c,shared.cone);
e2641e09 237 return;
238 }
96ffb2fe
PN
239
240 /* If the element cannot be removed from the src set, return 0. */
241 if (!setTypeRemove(srcset,ele)) {
e2641e09 242 addReply(c,shared.czero);
243 return;
244 }
96ffb2fe
PN
245
246 /* Remove the src set from the database when empty */
247 if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]);
5b4bff9c 248 touchWatchedKey(c->db,c->argv[1]);
249 touchWatchedKey(c->db,c->argv[2]);
e2641e09 250 server.dirty++;
96ffb2fe
PN
251
252 /* Create the destination set when it doesn't exist */
e2641e09 253 if (!dstset) {
96ffb2fe 254 dstset = setTypeCreate(ele);
e2641e09 255 dbAdd(c->db,c->argv[2],dstset);
256 }
96ffb2fe
PN
257
258 /* An extra key has changed when ele was successfully added to dstset */
259 if (setTypeAdd(dstset,ele)) server.dirty++;
e2641e09 260 addReply(c,shared.cone);
261}
262
263void sismemberCommand(redisClient *c) {
264 robj *set;
265
266 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
267 checkType(c,set,REDIS_SET)) return;
268
75b41de8 269 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2fe 270 if (setTypeIsMember(set,c->argv[2]))
e2641e09 271 addReply(c,shared.cone);
272 else
273 addReply(c,shared.czero);
274}
275
276void scardCommand(redisClient *c) {
277 robj *o;
e2641e09 278
279 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
280 checkType(c,o,REDIS_SET)) return;
281
b70d3555 282 addReplyLongLong(c,setTypeSize(o));
e2641e09 283}
284
285void spopCommand(redisClient *c) {
96ffb2fe 286 robj *set, *ele;
e2641e09 287
288 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
289 checkType(c,set,REDIS_SET)) return;
290
96ffb2fe
PN
291 ele = setTypeRandomElement(set);
292 if (ele == NULL) {
e2641e09 293 addReply(c,shared.nullbulk);
294 } else {
96ffb2fe 295 setTypeRemove(set,ele);
e2641e09 296 addReplyBulk(c,ele);
96ffb2fe
PN
297 decrRefCount(ele);
298 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
5b4bff9c 299 touchWatchedKey(c->db,c->argv[1]);
e2641e09 300 server.dirty++;
301 }
302}
303
304void srandmemberCommand(redisClient *c) {
96ffb2fe 305 robj *set, *ele;
e2641e09 306
307 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
308 checkType(c,set,REDIS_SET)) return;
309
96ffb2fe
PN
310 ele = setTypeRandomElement(set);
311 if (ele == NULL) {
e2641e09 312 addReply(c,shared.nullbulk);
313 } else {
e2641e09 314 addReplyBulk(c,ele);
96ffb2fe 315 decrRefCount(ele);
e2641e09 316 }
317}
318
319int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
96ffb2fe 320 return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
e2641e09 321}
322
96ffb2fe
PN
323void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
324 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 325 setTypeIterator *si;
b301c1fc
PN
326 robj *ele, *dstset = NULL;
327 void *replylen = NULL;
e2641e09 328 unsigned long j, cardinality = 0;
329
96ffb2fe
PN
330 for (j = 0; j < setnum; j++) {
331 robj *setobj = dstkey ?
332 lookupKeyWrite(c->db,setkeys[j]) :
333 lookupKeyRead(c->db,setkeys[j]);
e2641e09 334 if (!setobj) {
96ffb2fe 335 zfree(sets);
e2641e09 336 if (dstkey) {
5b4bff9c 337 if (dbDelete(c->db,dstkey)) {
338 touchWatchedKey(c->db,dstkey);
e2641e09 339 server.dirty++;
5b4bff9c 340 }
e2641e09 341 addReply(c,shared.czero);
342 } else {
343 addReply(c,shared.emptymultibulk);
344 }
345 return;
346 }
96ffb2fe
PN
347 if (checkType(c,setobj,REDIS_SET)) {
348 zfree(sets);
e2641e09 349 return;
350 }
96ffb2fe 351 sets[j] = setobj;
e2641e09 352 }
353 /* Sort sets from the smallest to largest, this will improve our
354 * algorithm's performace */
96ffb2fe 355 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
e2641e09 356
357 /* The first thing we should output is the total number of elements...
358 * since this is a multi-bulk write, but at this stage we don't know
359 * the intersection set size, so we use a trick, append an empty object
360 * to the output list and save the pointer to later modify it with the
361 * right length */
362 if (!dstkey) {
b301c1fc 363 replylen = addDeferredMultiBulkLength(c);
e2641e09 364 } else {
365 /* If we have a target key where to store the resulting set
366 * create this key with an empty set inside */
96ffb2fe 367 dstset = createIntsetObject();
e2641e09 368 }
369
370 /* Iterate all the elements of the first (smallest) set, and test
371 * the element against all the other sets, if at least one set does
372 * not include the element it is discarded */
96ffb2fe
PN
373 si = setTypeInitIterator(sets[0]);
374 while((ele = setTypeNext(si)) != NULL) {
375 for (j = 1; j < setnum; j++)
376 if (!setTypeIsMember(sets[j],ele)) break;
377
378 /* Only take action when all sets contain the member */
379 if (j == setnum) {
380 if (!dstkey) {
381 addReplyBulk(c,ele);
382 cardinality++;
383 } else {
384 setTypeAdd(dstset,ele);
385 }
e2641e09 386 }
96ffb2fe 387 decrRefCount(ele);
e2641e09 388 }
96ffb2fe 389 setTypeReleaseIterator(si);
e2641e09 390
391 if (dstkey) {
392 /* Store the resulting set into the target, if the intersection
393 * is not an empty set. */
394 dbDelete(c->db,dstkey);
96ffb2fe 395 if (setTypeSize(dstset) > 0) {
e2641e09 396 dbAdd(c->db,dstkey,dstset);
96ffb2fe 397 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 398 } else {
399 decrRefCount(dstset);
400 addReply(c,shared.czero);
401 }
5b4bff9c 402 touchWatchedKey(c->db,dstkey);
e2641e09 403 server.dirty++;
404 } else {
b301c1fc 405 setDeferredMultiBulkLength(c,replylen,cardinality);
e2641e09 406 }
96ffb2fe 407 zfree(sets);
e2641e09 408}
409
410void sinterCommand(redisClient *c) {
411 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
412}
413
414void sinterstoreCommand(redisClient *c) {
415 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
416}
417
96ffb2fe
PN
418#define REDIS_OP_UNION 0
419#define REDIS_OP_DIFF 1
420#define REDIS_OP_INTER 2
e2641e09 421
96ffb2fe
PN
422void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
423 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f1 424 setTypeIterator *si;
96ffb2fe
PN
425 robj *ele, *dstset = NULL;
426 int j, cardinality = 0;
e2641e09 427
96ffb2fe
PN
428 for (j = 0; j < setnum; j++) {
429 robj *setobj = dstkey ?
430 lookupKeyWrite(c->db,setkeys[j]) :
431 lookupKeyRead(c->db,setkeys[j]);
e2641e09 432 if (!setobj) {
96ffb2fe 433 sets[j] = NULL;
e2641e09 434 continue;
435 }
96ffb2fe
PN
436 if (checkType(c,setobj,REDIS_SET)) {
437 zfree(sets);
e2641e09 438 return;
439 }
96ffb2fe 440 sets[j] = setobj;
e2641e09 441 }
442
443 /* We need a temp set object to store our union. If the dstkey
444 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
445 * this set object will be the resulting object to set into the target key*/
96ffb2fe 446 dstset = createIntsetObject();
e2641e09 447
448 /* Iterate all the elements of all the sets, add every element a single
449 * time to the result set */
96ffb2fe
PN
450 for (j = 0; j < setnum; j++) {
451 if (op == REDIS_OP_DIFF && j == 0 && !sets[j]) break; /* result set is empty */
452 if (!sets[j]) continue; /* non existing keys are like empty sets */
e2641e09 453
96ffb2fe
PN
454 si = setTypeInitIterator(sets[j]);
455 while((ele = setTypeNext(si)) != NULL) {
e2641e09 456 if (op == REDIS_OP_UNION || j == 0) {
96ffb2fe 457 if (setTypeAdd(dstset,ele)) {
e2641e09 458 cardinality++;
459 }
460 } else if (op == REDIS_OP_DIFF) {
96ffb2fe 461 if (setTypeRemove(dstset,ele)) {
e2641e09 462 cardinality--;
463 }
464 }
96ffb2fe 465 decrRefCount(ele);
e2641e09 466 }
96ffb2fe 467 setTypeReleaseIterator(si);
e2641e09 468
96ffb2fe 469 /* Exit when result set is empty. */
e2641e09 470 if (op == REDIS_OP_DIFF && cardinality == 0) break;
471 }
472
473 /* Output the content of the resulting set, if not in STORE mode */
474 if (!dstkey) {
0537e7bf 475 addReplyMultiBulkLen(c,cardinality);
96ffb2fe
PN
476 si = setTypeInitIterator(dstset);
477 while((ele = setTypeNext(si)) != NULL) {
e2641e09 478 addReplyBulk(c,ele);
96ffb2fe 479 decrRefCount(ele);
e2641e09 480 }
96ffb2fe 481 setTypeReleaseIterator(si);
e2641e09 482 decrRefCount(dstset);
483 } else {
484 /* If we have a target key where to store the resulting set
485 * create this key with the result set inside */
486 dbDelete(c->db,dstkey);
96ffb2fe 487 if (setTypeSize(dstset) > 0) {
e2641e09 488 dbAdd(c->db,dstkey,dstset);
96ffb2fe 489 addReplyLongLong(c,setTypeSize(dstset));
e2641e09 490 } else {
491 decrRefCount(dstset);
492 addReply(c,shared.czero);
493 }
5b4bff9c 494 touchWatchedKey(c->db,dstkey);
e2641e09 495 server.dirty++;
496 }
96ffb2fe 497 zfree(sets);
e2641e09 498}
499
500void sunionCommand(redisClient *c) {
501 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
502}
503
504void sunionstoreCommand(redisClient *c) {
505 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
506}
507
508void sdiffCommand(redisClient *c) {
509 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
510}
511
512void sdiffstoreCommand(redisClient *c) {
513 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
514}