Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 618 lines (550 sloc) 20.735 kb
e2641e0 @antirez redis.c split into many different C files.
authored
1 #include "redis.h"
2
3 /*-----------------------------------------------------------------------------
4 * Set Commands
5 *----------------------------------------------------------------------------*/
6
96ffb2f @pietern merged intset code into the split files
pietern authored
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) {
ec7e138 @antirez test for intset integer encodability test and some small refactoring
authored
11 if (isObjectRepresentableAsLongLong(value,NULL) == REDIS_OK)
96ffb2f @pietern merged intset code into the split files
pietern authored
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) {
ec7e138 @antirez test for intset integer encodability test and some small refactoring
authored
24 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
96ffb2f @pietern merged intset code into the split files
pietern authored
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. */
eab0e26 @antirez replaced redisAssert() with redisAssertWithInfo() in a shitload of place...
authored
40 redisAssertWithInfo(NULL,value,dictAdd(subject->ptr,value,NULL) == DICT_OK);
96ffb2f @pietern merged intset code into the split files
pietern authored
41 incrRefCount(value);
42 return 1;
43 }
44 } else {
45 redisPanic("Unknown set encoding");
46 }
47 return 0;
48 }
49
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
50 int setTypeRemove(robj *setobj, robj *value) {
96ffb2f @pietern merged intset code into the split files
pietern authored
51 long long llval;
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
52 if (setobj->encoding == REDIS_ENCODING_HT) {
53 if (dictDelete(setobj->ptr,value) == DICT_OK) {
54 if (htNeedsResize(setobj->ptr)) dictResize(setobj->ptr);
96ffb2f @pietern merged intset code into the split files
pietern authored
55 return 1;
56 }
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
57 } else if (setobj->encoding == REDIS_ENCODING_INTSET) {
ec7e138 @antirez test for intset integer encodability test and some small refactoring
authored
58 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
59 int success;
60 setobj->ptr = intsetRemove(setobj->ptr,llval,&success);
96ffb2f @pietern merged intset code into the split files
pietern authored
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) {
ec7e138 @antirez test for intset integer encodability test and some small refactoring
authored
74 if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
96ffb2f @pietern merged intset code into the split files
pietern authored
75 return intsetFind((intset*)subject->ptr,llval);
76 }
77 } else {
78 redisPanic("Unknown set encoding");
79 }
80 return 0;
81 }
82
cb72d0f @pietern Rename iterator to setTypeIterator for consistency
pietern authored
83 setTypeIterator *setTypeInitIterator(robj *subject) {
740eee1 @pietern Fix type that was not renamed and compiler warning
pietern authored
84 setTypeIterator *si = zmalloc(sizeof(setTypeIterator));
96ffb2f @pietern merged intset code into the split files
pietern authored
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
cb72d0f @pietern Rename iterator to setTypeIterator for consistency
pietern authored
97 void setTypeReleaseIterator(setTypeIterator *si) {
96ffb2f @pietern merged intset code into the split files
pietern authored
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
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
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) {
96ffb2f @pietern merged intset code into the split files
pietern authored
115 if (si->encoding == REDIS_ENCODING_HT) {
116 dictEntry *de = dictNext(si->di);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
117 if (de == NULL) return -1;
c0ba9eb @antirez dict.c API names modified to be more coincise and consistent.
authored
118 *objele = dictGetKey(de);
96ffb2f @pietern merged intset code into the split files
pietern authored
119 } else if (si->encoding == REDIS_ENCODING_INTSET) {
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
120 if (!intsetGet(si->subject->ptr,si->ii++,llele))
121 return -1;
96ffb2f @pietern merged intset code into the split files
pietern authored
122 }
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
123 return si->encoding;
96ffb2f @pietern merged intset code into the split files
pietern authored
124 }
125
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
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 }
96ffb2f @pietern merged intset code into the split files
pietern authored
151
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
152 /* Return random element from a non empty set.
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
153 * The returned element can be a int64_t value if the set is encoded
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
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
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
160 * int64_t pointer or the redis object pointere was populated.
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
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
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
164 * copy on write friendly. */
165 int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele) {
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
166 if (setobj->encoding == REDIS_ENCODING_HT) {
167 dictEntry *de = dictGetRandomKey(setobj->ptr);
c0ba9eb @antirez dict.c API names modified to be more coincise and consistent.
authored
168 *objele = dictGetKey(de);
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
169 } else if (setobj->encoding == REDIS_ENCODING_INTSET) {
170 *llele = intsetRandom(setobj->ptr);
96ffb2f @pietern merged intset code into the split files
pietern authored
171 } else {
172 redisPanic("Unknown set encoding");
173 }
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
174 return setobj->encoding;
96ffb2f @pietern merged intset code into the split files
pietern authored
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. */
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
190 void setTypeConvert(robj *setobj, int enc) {
cb72d0f @pietern Rename iterator to setTypeIterator for consistency
pietern authored
191 setTypeIterator *si;
eab0e26 @antirez replaced redisAssert() with redisAssertWithInfo() in a shitload of place...
authored
192 redisAssertWithInfo(NULL,setobj,setobj->type == REDIS_SET &&
193 setobj->encoding == REDIS_ENCODING_INTSET);
96ffb2f @pietern merged intset code into the split files
pietern authored
194
195 if (enc == REDIS_ENCODING_HT) {
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
196 int64_t intele;
96ffb2f @pietern merged intset code into the split files
pietern authored
197 dict *d = dictCreate(&setDictType,NULL);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
198 robj *element;
199
96ffb2f @pietern merged intset code into the split files
pietern authored
200 /* Presize the dict to avoid rehashing */
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
201 dictExpand(d,intsetLen(setobj->ptr));
96ffb2f @pietern merged intset code into the split files
pietern authored
202
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
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);
eab0e26 @antirez replaced redisAssert() with redisAssertWithInfo() in a shitload of place...
authored
207 redisAssertWithInfo(NULL,element,dictAdd(d,element,NULL) == DICT_OK);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
208 }
96ffb2f @pietern merged intset code into the split files
pietern authored
209 setTypeReleaseIterator(si);
210
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
211 setobj->encoding = REDIS_ENCODING_HT;
212 zfree(setobj->ptr);
213 setobj->ptr = d;
96ffb2f @pietern merged intset code into the split files
pietern authored
214 } else {
215 redisPanic("Unsupported set conversion");
216 }
217 }
218
e2641e0 @antirez redis.c split into many different C files.
authored
219 void saddCommand(redisClient *c) {
220 robj *set;
22f294d @antirez variadic SADD
authored
221 int j, added = 0;
e2641e0 @antirez redis.c split into many different C files.
authored
222
223 set = lookupKeyWrite(c->db,c->argv[1]);
224 if (set == NULL) {
96ffb2f @pietern merged intset code into the split files
pietern authored
225 set = setTypeCreate(c->argv[2]);
e2641e0 @antirez redis.c split into many different C files.
authored
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 }
22f294d @antirez variadic SADD
authored
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++;
e2641e0 @antirez redis.c split into many different C files.
authored
237 }
22f294d @antirez variadic SADD
authored
238 if (added) signalModifiedKey(c->db,c->argv[1]);
239 server.dirty += added;
240 addReplyLongLong(c,added);
e2641e0 @antirez redis.c split into many different C files.
authored
241 }
242
243 void sremCommand(redisClient *c) {
244 robj *set;
b3a96d4 @antirez Variadic SREM
authored
245 int j, deleted = 0;
e2641e0 @antirez redis.c split into many different C files.
authored
246
247 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
248 checkType(c,set,REDIS_SET)) return;
249
b3a96d4 @antirez Variadic SREM
authored
250 for (j = 2; j < c->argc; j++) {
251 if (setTypeRemove(set,c->argv[j])) {
252 deleted++;
3738ff5 @antirez Fix for the variadic version of SREM. Regression test added.
authored
253 if (setTypeSize(set) == 0) {
254 dbDelete(c->db,c->argv[1]);
255 break;
256 }
b3a96d4 @antirez Variadic SREM
authored
257 }
258 }
259 if (deleted) {
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
260 signalModifiedKey(c->db,c->argv[1]);
b3a96d4 @antirez Variadic SREM
authored
261 server.dirty += deleted;
e2641e0 @antirez redis.c split into many different C files.
authored
262 }
b3a96d4 @antirez Variadic SREM
authored
263 addReplyLongLong(c,deleted);
e2641e0 @antirez redis.c split into many different C files.
authored
264 }
265
266 void smoveCommand(redisClient *c) {
96ffb2f @pietern merged intset code into the split files
pietern authored
267 robj *srcset, *dstset, *ele;
e2641e0 @antirez redis.c split into many different C files.
authored
268 srcset = lookupKeyWrite(c->db,c->argv[1]);
269 dstset = lookupKeyWrite(c->db,c->argv[2]);
75b41de @pietern Convert objects in the command procs instead of the protocol code
pietern authored
270 ele = c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e0 @antirez redis.c split into many different C files.
authored
271
96ffb2f @pietern merged intset code into the split files
pietern authored
272 /* If the source key does not exist return 0 */
273 if (srcset == NULL) {
274 addReply(c,shared.czero);
e2641e0 @antirez redis.c split into many different C files.
authored
275 return;
276 }
96ffb2f @pietern merged intset code into the split files
pietern authored
277
278 /* If the source key has the wrong type, or the destination key
279 * is set and has the wrong type, return with an error. */
280 if (checkType(c,srcset,REDIS_SET) ||
281 (dstset && checkType(c,dstset,REDIS_SET))) return;
282
283 /* If srcset and dstset are equal, SMOVE is a no-op */
284 if (srcset == dstset) {
285 addReply(c,shared.cone);
e2641e0 @antirez redis.c split into many different C files.
authored
286 return;
287 }
96ffb2f @pietern merged intset code into the split files
pietern authored
288
289 /* If the element cannot be removed from the src set, return 0. */
290 if (!setTypeRemove(srcset,ele)) {
e2641e0 @antirez redis.c split into many different C files.
authored
291 addReply(c,shared.czero);
292 return;
293 }
96ffb2f @pietern merged intset code into the split files
pietern authored
294
295 /* Remove the src set from the database when empty */
296 if (setTypeSize(srcset) == 0) dbDelete(c->db,c->argv[1]);
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
297 signalModifiedKey(c->db,c->argv[1]);
298 signalModifiedKey(c->db,c->argv[2]);
e2641e0 @antirez redis.c split into many different C files.
authored
299 server.dirty++;
96ffb2f @pietern merged intset code into the split files
pietern authored
300
301 /* Create the destination set when it doesn't exist */
e2641e0 @antirez redis.c split into many different C files.
authored
302 if (!dstset) {
96ffb2f @pietern merged intset code into the split files
pietern authored
303 dstset = setTypeCreate(ele);
e2641e0 @antirez redis.c split into many different C files.
authored
304 dbAdd(c->db,c->argv[2],dstset);
305 }
96ffb2f @pietern merged intset code into the split files
pietern authored
306
307 /* An extra key has changed when ele was successfully added to dstset */
308 if (setTypeAdd(dstset,ele)) server.dirty++;
e2641e0 @antirez redis.c split into many different C files.
authored
309 addReply(c,shared.cone);
310 }
311
312 void sismemberCommand(redisClient *c) {
313 robj *set;
314
315 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
316 checkType(c,set,REDIS_SET)) return;
317
75b41de @pietern Convert objects in the command procs instead of the protocol code
pietern authored
318 c->argv[2] = tryObjectEncoding(c->argv[2]);
96ffb2f @pietern merged intset code into the split files
pietern authored
319 if (setTypeIsMember(set,c->argv[2]))
e2641e0 @antirez redis.c split into many different C files.
authored
320 addReply(c,shared.cone);
321 else
322 addReply(c,shared.czero);
323 }
324
325 void scardCommand(redisClient *c) {
326 robj *o;
327
328 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
329 checkType(c,o,REDIS_SET)) return;
330
b70d355 @pietern Use existing reply functions where possible
pietern authored
331 addReplyLongLong(c,setTypeSize(o));
e2641e0 @antirez redis.c split into many different C files.
authored
332 }
333
334 void spopCommand(redisClient *c) {
c1c9d55 @antirez Fix for bug 561 and other related problems
authored
335 robj *set, *ele, *aux;
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
336 int64_t llele;
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
337 int encoding;
e2641e0 @antirez redis.c split into many different C files.
authored
338
339 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
340 checkType(c,set,REDIS_SET)) return;
341
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
342 encoding = setTypeRandomElement(set,&ele,&llele);
343 if (encoding == REDIS_ENCODING_INTSET) {
30318c1 @antirez SPOP replication/AOF patch ported to unstable branch
authored
344 ele = createStringObjectFromLongLong(llele);
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
345 set->ptr = intsetRemove(set->ptr,llele,NULL);
e2641e0 @antirez redis.c split into many different C files.
authored
346 } else {
30318c1 @antirez SPOP replication/AOF patch ported to unstable branch
authored
347 incrRefCount(ele);
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
348 setTypeRemove(set,ele);
e2641e0 @antirez redis.c split into many different C files.
authored
349 }
30318c1 @antirez SPOP replication/AOF patch ported to unstable branch
authored
350
c1c9d55 @antirez Fix for bug 561 and other related problems
authored
351 /* Replicate/AOF this command as an SREM operation */
352 aux = createStringObject("SREM",4);
353 rewriteClientCommandVector(c,3,aux,c->argv[1],ele);
354 decrRefCount(ele);
355 decrRefCount(aux);
30318c1 @antirez SPOP replication/AOF patch ported to unstable branch
authored
356
357 addReplyBulk(c,ele);
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
358 if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
359 signalModifiedKey(c->db,c->argv[1]);
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
360 server.dirty++;
e2641e0 @antirez redis.c split into many different C files.
authored
361 }
362
363 void srandmemberCommand(redisClient *c) {
96ffb2f @pietern merged intset code into the split files
pietern authored
364 robj *set, *ele;
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
365 int64_t llele;
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
366 int encoding;
e2641e0 @antirez redis.c split into many different C files.
authored
367
368 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
369 checkType(c,set,REDIS_SET)) return;
370
a5be65f @antirez COW friendly versions of SPOP and SRANDMEMBER commands, with some change...
authored
371 encoding = setTypeRandomElement(set,&ele,&llele);
372 if (encoding == REDIS_ENCODING_INTSET) {
373 addReplyBulkLongLong(c,llele);
e2641e0 @antirez redis.c split into many different C files.
authored
374 } else {
375 addReplyBulk(c,ele);
376 }
377 }
378
379 int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
96ffb2f @pietern merged intset code into the split files
pietern authored
380 return setTypeSize(*(robj**)s1)-setTypeSize(*(robj**)s2);
e2641e0 @antirez redis.c split into many different C files.
authored
381 }
382
96ffb2f @pietern merged intset code into the split files
pietern authored
383 void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
384 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f @pietern Rename iterator to setTypeIterator for consistency
pietern authored
385 setTypeIterator *si;
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
386 robj *eleobj, *dstset = NULL;
387 int64_t intobj;
b301c1f @pietern Wrapper for adding unknown multi bulk length to reply list
pietern authored
388 void *replylen = NULL;
e2641e0 @antirez redis.c split into many different C files.
authored
389 unsigned long j, cardinality = 0;
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
390 int encoding;
e2641e0 @antirez redis.c split into many different C files.
authored
391
96ffb2f @pietern merged intset code into the split files
pietern authored
392 for (j = 0; j < setnum; j++) {
393 robj *setobj = dstkey ?
394 lookupKeyWrite(c->db,setkeys[j]) :
395 lookupKeyRead(c->db,setkeys[j]);
e2641e0 @antirez redis.c split into many different C files.
authored
396 if (!setobj) {
96ffb2f @pietern merged intset code into the split files
pietern authored
397 zfree(sets);
e2641e0 @antirez redis.c split into many different C files.
authored
398 if (dstkey) {
5b4bff9 @antirez WATCH is now affected only when write commands actually modify the key c...
authored
399 if (dbDelete(c->db,dstkey)) {
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
400 signalModifiedKey(c->db,dstkey);
e2641e0 @antirez redis.c split into many different C files.
authored
401 server.dirty++;
5b4bff9 @antirez WATCH is now affected only when write commands actually modify the key c...
authored
402 }
e2641e0 @antirez redis.c split into many different C files.
authored
403 addReply(c,shared.czero);
404 } else {
405 addReply(c,shared.emptymultibulk);
406 }
407 return;
408 }
96ffb2f @pietern merged intset code into the split files
pietern authored
409 if (checkType(c,setobj,REDIS_SET)) {
410 zfree(sets);
e2641e0 @antirez redis.c split into many different C files.
authored
411 return;
412 }
96ffb2f @pietern merged intset code into the split files
pietern authored
413 sets[j] = setobj;
e2641e0 @antirez redis.c split into many different C files.
authored
414 }
415 /* Sort sets from the smallest to largest, this will improve our
416 * algorithm's performace */
96ffb2f @pietern merged intset code into the split files
pietern authored
417 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
e2641e0 @antirez redis.c split into many different C files.
authored
418
419 /* The first thing we should output is the total number of elements...
420 * since this is a multi-bulk write, but at this stage we don't know
421 * the intersection set size, so we use a trick, append an empty object
422 * to the output list and save the pointer to later modify it with the
423 * right length */
424 if (!dstkey) {
b301c1f @pietern Wrapper for adding unknown multi bulk length to reply list
pietern authored
425 replylen = addDeferredMultiBulkLength(c);
e2641e0 @antirez redis.c split into many different C files.
authored
426 } else {
427 /* If we have a target key where to store the resulting set
428 * create this key with an empty set inside */
96ffb2f @pietern merged intset code into the split files
pietern authored
429 dstset = createIntsetObject();
e2641e0 @antirez redis.c split into many different C files.
authored
430 }
431
432 /* Iterate all the elements of the first (smallest) set, and test
433 * the element against all the other sets, if at least one set does
434 * not include the element it is discarded */
96ffb2f @pietern merged intset code into the split files
pietern authored
435 si = setTypeInitIterator(sets[0]);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
436 while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
437 for (j = 1; j < setnum; j++) {
dd1eefa @antirez Fixed SINTER[STORE] problem related to the new copy on write safe iterat...
authored
438 if (sets[j] == sets[0]) continue;
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
439 if (encoding == REDIS_ENCODING_INTSET) {
440 /* intset with intset is simple... and fast */
441 if (sets[j]->encoding == REDIS_ENCODING_INTSET &&
442 !intsetFind((intset*)sets[j]->ptr,intobj))
443 {
444 break;
445 /* in order to compare an integer with an object we
446 * have to use the generic function, creating an object
447 * for this */
448 } else if (sets[j]->encoding == REDIS_ENCODING_HT) {
449 eleobj = createStringObjectFromLongLong(intobj);
450 if (!setTypeIsMember(sets[j],eleobj)) {
451 decrRefCount(eleobj);
452 break;
453 }
454 decrRefCount(eleobj);
455 }
456 } else if (encoding == REDIS_ENCODING_HT) {
457 /* Optimization... if the source object is integer
458 * encoded AND the target set is an intset, we can get
459 * a much faster path. */
460 if (eleobj->encoding == REDIS_ENCODING_INT &&
461 sets[j]->encoding == REDIS_ENCODING_INTSET &&
462 !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
463 {
464 break;
465 /* else... object to object check is easy as we use the
466 * type agnostic API here. */
467 } else if (!setTypeIsMember(sets[j],eleobj)) {
468 break;
469 }
470 }
471 }
96ffb2f @pietern merged intset code into the split files
pietern authored
472
473 /* Only take action when all sets contain the member */
474 if (j == setnum) {
475 if (!dstkey) {
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
476 if (encoding == REDIS_ENCODING_HT)
477 addReplyBulk(c,eleobj);
478 else
479 addReplyBulkLongLong(c,intobj);
96ffb2f @pietern merged intset code into the split files
pietern authored
480 cardinality++;
481 } else {
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
482 if (encoding == REDIS_ENCODING_INTSET) {
483 eleobj = createStringObjectFromLongLong(intobj);
484 setTypeAdd(dstset,eleobj);
485 decrRefCount(eleobj);
486 } else {
487 setTypeAdd(dstset,eleobj);
488 }
96ffb2f @pietern merged intset code into the split files
pietern authored
489 }
e2641e0 @antirez redis.c split into many different C files.
authored
490 }
491 }
96ffb2f @pietern merged intset code into the split files
pietern authored
492 setTypeReleaseIterator(si);
e2641e0 @antirez redis.c split into many different C files.
authored
493
494 if (dstkey) {
495 /* Store the resulting set into the target, if the intersection
496 * is not an empty set. */
497 dbDelete(c->db,dstkey);
96ffb2f @pietern merged intset code into the split files
pietern authored
498 if (setTypeSize(dstset) > 0) {
e2641e0 @antirez redis.c split into many different C files.
authored
499 dbAdd(c->db,dstkey,dstset);
96ffb2f @pietern merged intset code into the split files
pietern authored
500 addReplyLongLong(c,setTypeSize(dstset));
e2641e0 @antirez redis.c split into many different C files.
authored
501 } else {
502 decrRefCount(dstset);
503 addReply(c,shared.czero);
504 }
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
505 signalModifiedKey(c->db,dstkey);
e2641e0 @antirez redis.c split into many different C files.
authored
506 server.dirty++;
507 } else {
b301c1f @pietern Wrapper for adding unknown multi bulk length to reply list
pietern authored
508 setDeferredMultiBulkLength(c,replylen,cardinality);
e2641e0 @antirez redis.c split into many different C files.
authored
509 }
96ffb2f @pietern merged intset code into the split files
pietern authored
510 zfree(sets);
e2641e0 @antirez redis.c split into many different C files.
authored
511 }
512
513 void sinterCommand(redisClient *c) {
514 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
515 }
516
517 void sinterstoreCommand(redisClient *c) {
518 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
519 }
520
96ffb2f @pietern merged intset code into the split files
pietern authored
521 #define REDIS_OP_UNION 0
522 #define REDIS_OP_DIFF 1
523 #define REDIS_OP_INTER 2
e2641e0 @antirez redis.c split into many different C files.
authored
524
96ffb2f @pietern merged intset code into the split files
pietern authored
525 void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
526 robj **sets = zmalloc(sizeof(robj*)*setnum);
cb72d0f @pietern Rename iterator to setTypeIterator for consistency
pietern authored
527 setTypeIterator *si;
96ffb2f @pietern merged intset code into the split files
pietern authored
528 robj *ele, *dstset = NULL;
529 int j, cardinality = 0;
e2641e0 @antirez redis.c split into many different C files.
authored
530
96ffb2f @pietern merged intset code into the split files
pietern authored
531 for (j = 0; j < setnum; j++) {
532 robj *setobj = dstkey ?
533 lookupKeyWrite(c->db,setkeys[j]) :
534 lookupKeyRead(c->db,setkeys[j]);
e2641e0 @antirez redis.c split into many different C files.
authored
535 if (!setobj) {
96ffb2f @pietern merged intset code into the split files
pietern authored
536 sets[j] = NULL;
e2641e0 @antirez redis.c split into many different C files.
authored
537 continue;
538 }
96ffb2f @pietern merged intset code into the split files
pietern authored
539 if (checkType(c,setobj,REDIS_SET)) {
540 zfree(sets);
e2641e0 @antirez redis.c split into many different C files.
authored
541 return;
542 }
96ffb2f @pietern merged intset code into the split files
pietern authored
543 sets[j] = setobj;
e2641e0 @antirez redis.c split into many different C files.
authored
544 }
545
546 /* We need a temp set object to store our union. If the dstkey
547 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
548 * this set object will be the resulting object to set into the target key*/
96ffb2f @pietern merged intset code into the split files
pietern authored
549 dstset = createIntsetObject();
e2641e0 @antirez redis.c split into many different C files.
authored
550
551 /* Iterate all the elements of all the sets, add every element a single
552 * time to the result set */
96ffb2f @pietern merged intset code into the split files
pietern authored
553 for (j = 0; j < setnum; j++) {
554 if (op == REDIS_OP_DIFF && j == 0 && !sets[j]) break; /* result set is empty */
555 if (!sets[j]) continue; /* non existing keys are like empty sets */
e2641e0 @antirez redis.c split into many different C files.
authored
556
96ffb2f @pietern merged intset code into the split files
pietern authored
557 si = setTypeInitIterator(sets[j]);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
558 while((ele = setTypeNextObject(si)) != NULL) {
e2641e0 @antirez redis.c split into many different C files.
authored
559 if (op == REDIS_OP_UNION || j == 0) {
96ffb2f @pietern merged intset code into the split files
pietern authored
560 if (setTypeAdd(dstset,ele)) {
e2641e0 @antirez redis.c split into many different C files.
authored
561 cardinality++;
562 }
563 } else if (op == REDIS_OP_DIFF) {
96ffb2f @pietern merged intset code into the split files
pietern authored
564 if (setTypeRemove(dstset,ele)) {
e2641e0 @antirez redis.c split into many different C files.
authored
565 cardinality--;
566 }
567 }
96ffb2f @pietern merged intset code into the split files
pietern authored
568 decrRefCount(ele);
e2641e0 @antirez redis.c split into many different C files.
authored
569 }
96ffb2f @pietern merged intset code into the split files
pietern authored
570 setTypeReleaseIterator(si);
e2641e0 @antirez redis.c split into many different C files.
authored
571
96ffb2f @pietern merged intset code into the split files
pietern authored
572 /* Exit when result set is empty. */
e2641e0 @antirez redis.c split into many different C files.
authored
573 if (op == REDIS_OP_DIFF && cardinality == 0) break;
574 }
575
576 /* Output the content of the resulting set, if not in STORE mode */
577 if (!dstkey) {
0537e7b @pietern Use specialized function to add multi bulk reply length
pietern authored
578 addReplyMultiBulkLen(c,cardinality);
96ffb2f @pietern merged intset code into the split files
pietern authored
579 si = setTypeInitIterator(dstset);
1b508da @antirez SINTER/MEMBERS are now COW friendly, also some refactoring around was ne...
authored
580 while((ele = setTypeNextObject(si)) != NULL) {
e2641e0 @antirez redis.c split into many different C files.
authored
581 addReplyBulk(c,ele);
96ffb2f @pietern merged intset code into the split files
pietern authored
582 decrRefCount(ele);
e2641e0 @antirez redis.c split into many different C files.
authored
583 }
96ffb2f @pietern merged intset code into the split files
pietern authored
584 setTypeReleaseIterator(si);
e2641e0 @antirez redis.c split into many different C files.
authored
585 decrRefCount(dstset);
586 } else {
587 /* If we have a target key where to store the resulting set
588 * create this key with the result set inside */
589 dbDelete(c->db,dstkey);
96ffb2f @pietern merged intset code into the split files
pietern authored
590 if (setTypeSize(dstset) > 0) {
e2641e0 @antirez redis.c split into many different C files.
authored
591 dbAdd(c->db,dstkey,dstset);
96ffb2f @pietern merged intset code into the split files
pietern authored
592 addReplyLongLong(c,setTypeSize(dstset));
e2641e0 @antirez redis.c split into many different C files.
authored
593 } else {
594 decrRefCount(dstset);
595 addReply(c,shared.czero);
596 }
cea8c5c @antirez touched key for WATCH refactored into a more general thing that can be u...
authored
597 signalModifiedKey(c->db,dstkey);
e2641e0 @antirez redis.c split into many different C files.
authored
598 server.dirty++;
599 }
96ffb2f @pietern merged intset code into the split files
pietern authored
600 zfree(sets);
e2641e0 @antirez redis.c split into many different C files.
authored
601 }
602
603 void sunionCommand(redisClient *c) {
604 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
605 }
606
607 void sunionstoreCommand(redisClient *c) {
608 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
609 }
610
611 void sdiffCommand(redisClient *c) {
612 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
613 }
614
615 void sdiffstoreCommand(redisClient *c) {
616 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
617 }
Something went wrong with that request. Please try again.