]> git.saurik.com Git - redis.git/blob - src/t_set.c
test for intset integer encodability test and some small refactoring
[redis.git] / src / t_set.c
1 #include "redis.h"
2
3 /*-----------------------------------------------------------------------------
4 * Set Commands
5 *----------------------------------------------------------------------------*/
6
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) {
11 if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK)
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) {
24 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
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
50 int 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) {
58 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
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
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) {
74 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
75 return intsetFind((intset*)subject->ptr,llval);
76 }
77 } else {
78 redisPanic("Unknown set encoding");
79 }
80 return 0;
81 }
82
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) {
90 si->ii = 0;
91 } else {
92 redisPanic("Unknown set encoding");
93 }
94 return si;
95 }
96
97 void setTypeReleaseIterator(setTypeIterator *si) {
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. */
106 robj *setTypeNext(setTypeIterator *si) {
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) {
115 int64_t llval;
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. */
125 robj *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
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);
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. */
153 void setTypeConvert(robj *subject, int enc) {
154 setTypeIterator *si;
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
177 void saddCommand(redisClient *c) {
178 robj *set;
179
180 set = lookupKeyWrite(c->db,c->argv[1]);
181 if (set == NULL) {
182 set = setTypeCreate(c->argv[2]);
183 dbAdd(c->db,c->argv[1],set);
184 } else {
185 if (set->type != REDIS_SET) {
186 addReply(c,shared.wrongtypeerr);
187 return;
188 }
189 }
190 if (setTypeAdd(set,c->argv[2])) {
191 touchWatchedKey(c->db,c->argv[1]);
192 server.dirty++;
193 addReply(c,shared.cone);
194 } else {
195 addReply(c,shared.czero);
196 }
197 }
198
199 void sremCommand(redisClient *c) {
200 robj *set;
201
202 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
203 checkType(c,set,REDIS_SET)) return;
204
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]);
208 server.dirty++;
209 addReply(c,shared.cone);
210 } else {
211 addReply(c,shared.czero);
212 }
213 }
214
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]);
219 ele = c->argv[3];
220
221 /* If the source key does not exist return 0 */
222 if (srcset == NULL) {
223 addReply(c,shared.czero);
224 return;
225 }
226
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;
231
232 /* If srcset and dstset are equal, SMOVE is a no-op */
233 if (srcset == dstset) {
234 addReply(c,shared.cone);
235 return;
236 }
237
238 /* If the element cannot be removed from the src set, return 0. */
239 if (!setTypeRemove(srcset,ele)) {
240 addReply(c,shared.czero);
241 return;
242 }
243
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]);
248 server.dirty++;
249
250 /* Create the destination set when it doesn't exist */
251 if (!dstset) {
252 dstset = setTypeCreate(ele);
253 dbAdd(c->db,c->argv[2],dstset);
254 }
255
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);
259 }
260
261 void sismemberCommand(redisClient *c) {
262 robj *set;
263
264 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
265 checkType(c,set,REDIS_SET)) return;
266
267 if (setTypeIsMember(set,c->argv[2]))
268 addReply(c,shared.cone);
269 else
270 addReply(c,shared.czero);
271 }
272
273 void scardCommand(redisClient *c) {
274 robj *o;
275
276 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
277 checkType(c,o,REDIS_SET)) return;
278
279 addReplyUlong(c,setTypeSize(o));
280 }
281
282 void spopCommand(redisClient *c) {
283 robj *set, *ele;
284
285 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
286 checkType(c,set,REDIS_SET)) return;
287
288 ele = setTypeRandomElement(set);
289 if (ele == NULL) {
290 addReply(c,shared.nullbulk);
291 } else {
292 setTypeRemove(set,ele);
293 addReplyBulk(c,ele);
294 decrRefCount(ele);
295 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
296 touchWatchedKey(c->db,c->argv[1]);
297 server.dirty++;
298 }
299 }
300
301 void srandmemberCommand(redisClient *c) {
302 robj *set, *ele;
303
304 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
305 checkType(c,set,REDIS_SET)) return;
306
307 ele = setTypeRandomElement(set);
308 if (ele == NULL) {
309 addReply(c,shared.nullbulk);
310 } else {
311 addReplyBulk(c,ele);
312 decrRefCount(ele);
313 }
314 }
315
316 int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
317 return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
318 }
319
320 void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
321 robj **sets = zmalloc(sizeof(robj*)*setnum);
322 setTypeIterator *si;
323 robj *ele, *lenobj = NULL, *dstset = NULL;
324 unsigned long j, cardinality = 0;
325
326 for (j = 0; j < setnum; j++) {
327 robj *setobj = dstkey ?
328 lookupKeyWrite(c->db,setkeys[j]) :
329 lookupKeyRead(c->db,setkeys[j]);
330 if (!setobj) {
331 zfree(sets);
332 if (dstkey) {
333 if (dbDelete(c->db,dstkey)) {
334 touchWatchedKey(c->db,dstkey);
335 server.dirty++;
336 }
337 addReply(c,shared.czero);
338 } else {
339 addReply(c,shared.emptymultibulk);
340 }
341 return;
342 }
343 if (checkType(c,setobj,REDIS_SET)) {
344 zfree(sets);
345 return;
346 }
347 sets[j] = setobj;
348 }
349 /* Sort sets from the smallest to largest, this will improve our
350 * algorithm's performace */
351 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
352
353 /* The first thing we should output is the total number of elements...
354 * since this is a multi-bulk write, but at this stage we don't know
355 * the intersection set size, so we use a trick, append an empty object
356 * to the output list and save the pointer to later modify it with the
357 * right length */
358 if (!dstkey) {
359 lenobj = createObject(REDIS_STRING,NULL);
360 addReply(c,lenobj);
361 decrRefCount(lenobj);
362 } else {
363 /* If we have a target key where to store the resulting set
364 * create this key with an empty set inside */
365 dstset = createIntsetObject();
366 }
367
368 /* Iterate all the elements of the first (smallest) set, and test
369 * the element against all the other sets, if at least one set does
370 * not include the element it is discarded */
371 si = setTypeInitIterator(sets[0]);
372 while((ele = setTypeNext(si)) != NULL) {
373 for (j = 1; j < setnum; j++)
374 if (!setTypeIsMember(sets[j],ele)) break;
375
376 /* Only take action when all sets contain the member */
377 if (j == setnum) {
378 if (!dstkey) {
379 addReplyBulk(c,ele);
380 cardinality++;
381 } else {
382 setTypeAdd(dstset,ele);
383 }
384 }
385 decrRefCount(ele);
386 }
387 setTypeReleaseIterator(si);
388
389 if (dstkey) {
390 /* Store the resulting set into the target, if the intersection
391 * is not an empty set. */
392 dbDelete(c->db,dstkey);
393 if (setTypeSize(dstset) > 0) {
394 dbAdd(c->db,dstkey,dstset);
395 addReplyLongLong(c,setTypeSize(dstset));
396 } else {
397 decrRefCount(dstset);
398 addReply(c,shared.czero);
399 }
400 touchWatchedKey(c->db,dstkey);
401 server.dirty++;
402 } else {
403 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
404 }
405 zfree(sets);
406 }
407
408 void sinterCommand(redisClient *c) {
409 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
410 }
411
412 void sinterstoreCommand(redisClient *c) {
413 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
414 }
415
416 #define REDIS_OP_UNION 0
417 #define REDIS_OP_DIFF 1
418 #define REDIS_OP_INTER 2
419
420 void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
421 robj **sets = zmalloc(sizeof(robj*)*setnum);
422 setTypeIterator *si;
423 robj *ele, *dstset = NULL;
424 int j, cardinality = 0;
425
426 for (j = 0; j < setnum; j++) {
427 robj *setobj = dstkey ?
428 lookupKeyWrite(c->db,setkeys[j]) :
429 lookupKeyRead(c->db,setkeys[j]);
430 if (!setobj) {
431 sets[j] = NULL;
432 continue;
433 }
434 if (checkType(c,setobj,REDIS_SET)) {
435 zfree(sets);
436 return;
437 }
438 sets[j] = setobj;
439 }
440
441 /* We need a temp set object to store our union. If the dstkey
442 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
443 * this set object will be the resulting object to set into the target key*/
444 dstset = createIntsetObject();
445
446 /* Iterate all the elements of all the sets, add every element a single
447 * time to the result set */
448 for (j = 0; j < setnum; j++) {
449 if (op == REDIS_OP_DIFF && j == 0 && !sets[j]) break; /* result set is empty */
450 if (!sets[j]) continue; /* non existing keys are like empty sets */
451
452 si = setTypeInitIterator(sets[j]);
453 while((ele = setTypeNext(si)) != NULL) {
454 if (op == REDIS_OP_UNION || j == 0) {
455 if (setTypeAdd(dstset,ele)) {
456 cardinality++;
457 }
458 } else if (op == REDIS_OP_DIFF) {
459 if (setTypeRemove(dstset,ele)) {
460 cardinality--;
461 }
462 }
463 decrRefCount(ele);
464 }
465 setTypeReleaseIterator(si);
466
467 /* Exit when result set is empty. */
468 if (op == REDIS_OP_DIFF && cardinality == 0) break;
469 }
470
471 /* Output the content of the resulting set, if not in STORE mode */
472 if (!dstkey) {
473 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
474 si = setTypeInitIterator(dstset);
475 while((ele = setTypeNext(si)) != NULL) {
476 addReplyBulk(c,ele);
477 decrRefCount(ele);
478 }
479 setTypeReleaseIterator(si);
480 decrRefCount(dstset);
481 } else {
482 /* If we have a target key where to store the resulting set
483 * create this key with the result set inside */
484 dbDelete(c->db,dstkey);
485 if (setTypeSize(dstset) > 0) {
486 dbAdd(c->db,dstkey,dstset);
487 addReplyLongLong(c,setTypeSize(dstset));
488 } else {
489 decrRefCount(dstset);
490 addReply(c,shared.czero);
491 }
492 touchWatchedKey(c->db,dstkey);
493 server.dirty++;
494 }
495 zfree(sets);
496 }
497
498 void sunionCommand(redisClient *c) {
499 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
500 }
501
502 void sunionstoreCommand(redisClient *c) {
503 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
504 }
505
506 void sdiffCommand(redisClient *c) {
507 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
508 }
509
510 void sdiffstoreCommand(redisClient *c) {
511 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
512 }