1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "server.h"
31 
32 #define LIST_MAX_ITEM_SIZE ((1ull<<32)-1024)
33 
34 /*-----------------------------------------------------------------------------
35  * List API
36  *----------------------------------------------------------------------------*/
37 
38 /* The function pushes an element to the specified list object 'subject',
39  * at head or tail position as specified by 'where'.
40  *
41  * There is no need for the caller to increment the refcount of 'value' as
42  * the function takes care of it if needed. */
listTypePush(robj * subject,robj * value,int where)43 void listTypePush(robj *subject, robj *value, int where) {
44     if (subject->encoding == OBJ_ENCODING_QUICKLIST) {
45         int pos = (where == LIST_HEAD) ? QUICKLIST_HEAD : QUICKLIST_TAIL;
46         value = getDecodedObject(value);
47         size_t len = sdslen(value->ptr);
48         quicklistPush(subject->ptr, value->ptr, len, pos);
49         decrRefCount(value);
50     } else {
51         serverPanic("Unknown list encoding");
52     }
53 }
54 
listPopSaver(unsigned char * data,unsigned int sz)55 void *listPopSaver(unsigned char *data, unsigned int sz) {
56     return createStringObject((char*)data,sz);
57 }
58 
listTypePop(robj * subject,int where)59 robj *listTypePop(robj *subject, int where) {
60     long long vlong;
61     robj *value = NULL;
62 
63     int ql_where = where == LIST_HEAD ? QUICKLIST_HEAD : QUICKLIST_TAIL;
64     if (subject->encoding == OBJ_ENCODING_QUICKLIST) {
65         if (quicklistPopCustom(subject->ptr, ql_where, (unsigned char **)&value,
66                                NULL, &vlong, listPopSaver)) {
67             if (!value)
68                 value = createStringObjectFromLongLong(vlong);
69         }
70     } else {
71         serverPanic("Unknown list encoding");
72     }
73     return value;
74 }
75 
listTypeLength(const robj * subject)76 unsigned long listTypeLength(const robj *subject) {
77     if (subject->encoding == OBJ_ENCODING_QUICKLIST) {
78         return quicklistCount(subject->ptr);
79     } else {
80         serverPanic("Unknown list encoding");
81     }
82 }
83 
84 /* Initialize an iterator at the specified index. */
listTypeInitIterator(robj * subject,long index,unsigned char direction)85 listTypeIterator *listTypeInitIterator(robj *subject, long index,
86                                        unsigned char direction) {
87     listTypeIterator *li = zmalloc(sizeof(listTypeIterator));
88     li->subject = subject;
89     li->encoding = subject->encoding;
90     li->direction = direction;
91     li->iter = NULL;
92     /* LIST_HEAD means start at TAIL and move *towards* head.
93      * LIST_TAIL means start at HEAD and move *towards tail. */
94     int iter_direction =
95         direction == LIST_HEAD ? AL_START_TAIL : AL_START_HEAD;
96     if (li->encoding == OBJ_ENCODING_QUICKLIST) {
97         li->iter = quicklistGetIteratorAtIdx(li->subject->ptr,
98                                              iter_direction, index);
99     } else {
100         serverPanic("Unknown list encoding");
101     }
102     return li;
103 }
104 
105 /* Clean up the iterator. */
listTypeReleaseIterator(listTypeIterator * li)106 void listTypeReleaseIterator(listTypeIterator *li) {
107     zfree(li->iter);
108     zfree(li);
109 }
110 
111 /* Stores pointer to current the entry in the provided entry structure
112  * and advances the position of the iterator. Returns 1 when the current
113  * entry is in fact an entry, 0 otherwise. */
listTypeNext(listTypeIterator * li,listTypeEntry * entry)114 int listTypeNext(listTypeIterator *li, listTypeEntry *entry) {
115     /* Protect from converting when iterating */
116     serverAssert(li->subject->encoding == li->encoding);
117 
118     entry->li = li;
119     if (li->encoding == OBJ_ENCODING_QUICKLIST) {
120         return quicklistNext(li->iter, &entry->entry);
121     } else {
122         serverPanic("Unknown list encoding");
123     }
124     return 0;
125 }
126 
127 /* Return entry or NULL at the current position of the iterator. */
listTypeGet(listTypeEntry * entry)128 robj *listTypeGet(listTypeEntry *entry) {
129     robj *value = NULL;
130     if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
131         if (entry->entry.value) {
132             value = createStringObject((char *)entry->entry.value,
133                                        entry->entry.sz);
134         } else {
135             value = createStringObjectFromLongLong(entry->entry.longval);
136         }
137     } else {
138         serverPanic("Unknown list encoding");
139     }
140     return value;
141 }
142 
listTypeInsert(listTypeEntry * entry,robj * value,int where)143 void listTypeInsert(listTypeEntry *entry, robj *value, int where) {
144     if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
145         value = getDecodedObject(value);
146         sds str = value->ptr;
147         size_t len = sdslen(str);
148         if (where == LIST_TAIL) {
149             quicklistInsertAfter((quicklist *)entry->entry.quicklist,
150                                  &entry->entry, str, len);
151         } else if (where == LIST_HEAD) {
152             quicklistInsertBefore((quicklist *)entry->entry.quicklist,
153                                   &entry->entry, str, len);
154         }
155         decrRefCount(value);
156     } else {
157         serverPanic("Unknown list encoding");
158     }
159 }
160 
161 /* Compare the given object with the entry at the current position. */
listTypeEqual(listTypeEntry * entry,robj * o)162 int listTypeEqual(listTypeEntry *entry, robj *o) {
163     if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
164         serverAssertWithInfo(NULL,o,sdsEncodedObject(o));
165         return quicklistCompare(entry->entry.zi,o->ptr,sdslen(o->ptr));
166     } else {
167         serverPanic("Unknown list encoding");
168     }
169 }
170 
171 /* Delete the element pointed to. */
listTypeDelete(listTypeIterator * iter,listTypeEntry * entry)172 void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry) {
173     if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
174         quicklistDelEntry(iter->iter, &entry->entry);
175     } else {
176         serverPanic("Unknown list encoding");
177     }
178 }
179 
180 /* Create a quicklist from a single ziplist */
listTypeConvert(robj * subject,int enc)181 void listTypeConvert(robj *subject, int enc) {
182     serverAssertWithInfo(NULL,subject,subject->type==OBJ_LIST);
183     serverAssertWithInfo(NULL,subject,subject->encoding==OBJ_ENCODING_ZIPLIST);
184 
185     if (enc == OBJ_ENCODING_QUICKLIST) {
186         size_t zlen = server.list_max_ziplist_size;
187         int depth = server.list_compress_depth;
188         subject->ptr = quicklistCreateFromZiplist(zlen, depth, subject->ptr);
189         subject->encoding = OBJ_ENCODING_QUICKLIST;
190     } else {
191         serverPanic("Unsupported list conversion");
192     }
193 }
194 
195 /*-----------------------------------------------------------------------------
196  * List Commands
197  *----------------------------------------------------------------------------*/
198 
pushGenericCommand(client * c,int where)199 void pushGenericCommand(client *c, int where) {
200     int j, pushed = 0;
201 
202     for (j = 2; j < c->argc; j++) {
203         if (sdslen(c->argv[j]->ptr) > LIST_MAX_ITEM_SIZE) {
204             addReplyError(c, "Element too large");
205             return;
206         }
207     }
208 
209     robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
210 
211     if (lobj && lobj->type != OBJ_LIST) {
212         addReply(c,shared.wrongtypeerr);
213         return;
214     }
215 
216     for (j = 2; j < c->argc; j++) {
217         if (!lobj) {
218             lobj = createQuicklistObject();
219             quicklistSetOptions(lobj->ptr, server.list_max_ziplist_size,
220                                 server.list_compress_depth);
221             dbAdd(c->db,c->argv[1],lobj);
222         }
223         listTypePush(lobj,c->argv[j],where);
224         pushed++;
225     }
226     addReplyLongLong(c, (lobj ? listTypeLength(lobj) : 0));
227     if (pushed) {
228         char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
229 
230         signalModifiedKey(c,c->db,c->argv[1]);
231         notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
232     }
233     server.dirty += pushed;
234 }
235 
lpushCommand(client * c)236 void lpushCommand(client *c) {
237     pushGenericCommand(c,LIST_HEAD);
238 }
239 
rpushCommand(client * c)240 void rpushCommand(client *c) {
241     pushGenericCommand(c,LIST_TAIL);
242 }
243 
pushxGenericCommand(client * c,int where)244 void pushxGenericCommand(client *c, int where) {
245     int j, pushed = 0;
246     robj *subject;
247 
248     if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
249         checkType(c,subject,OBJ_LIST)) return;
250 
251     for (j = 2; j < c->argc; j++) {
252         listTypePush(subject,c->argv[j],where);
253         pushed++;
254     }
255 
256     addReplyLongLong(c,listTypeLength(subject));
257 
258     if (pushed) {
259         char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
260         signalModifiedKey(c,c->db,c->argv[1]);
261         notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
262     }
263     server.dirty += pushed;
264 }
265 
lpushxCommand(client * c)266 void lpushxCommand(client *c) {
267     pushxGenericCommand(c,LIST_HEAD);
268 }
269 
rpushxCommand(client * c)270 void rpushxCommand(client *c) {
271     pushxGenericCommand(c,LIST_TAIL);
272 }
273 
linsertCommand(client * c)274 void linsertCommand(client *c) {
275     int where;
276     robj *subject;
277     listTypeIterator *iter;
278     listTypeEntry entry;
279     int inserted = 0;
280 
281     if (strcasecmp(c->argv[2]->ptr,"after") == 0) {
282         where = LIST_TAIL;
283     } else if (strcasecmp(c->argv[2]->ptr,"before") == 0) {
284         where = LIST_HEAD;
285     } else {
286         addReply(c,shared.syntaxerr);
287         return;
288     }
289 
290     if (sdslen(c->argv[4]->ptr) > LIST_MAX_ITEM_SIZE) {
291         addReplyError(c, "Element too large");
292         return;
293     }
294 
295     if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
296         checkType(c,subject,OBJ_LIST)) return;
297 
298     /* Seek pivot from head to tail */
299     iter = listTypeInitIterator(subject,0,LIST_TAIL);
300     while (listTypeNext(iter,&entry)) {
301         if (listTypeEqual(&entry,c->argv[3])) {
302             listTypeInsert(&entry,c->argv[4],where);
303             inserted = 1;
304             break;
305         }
306     }
307     listTypeReleaseIterator(iter);
308 
309     if (inserted) {
310         signalModifiedKey(c,c->db,c->argv[1]);
311         notifyKeyspaceEvent(NOTIFY_LIST,"linsert",
312                             c->argv[1],c->db->id);
313         server.dirty++;
314     } else {
315         /* Notify client of a failed insert */
316         addReplyLongLong(c,-1);
317         return;
318     }
319 
320     addReplyLongLong(c,listTypeLength(subject));
321 }
322 
llenCommand(client * c)323 void llenCommand(client *c) {
324     robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.czero);
325     if (o == NULL || checkType(c,o,OBJ_LIST)) return;
326     addReplyLongLong(c,listTypeLength(o));
327 }
328 
lindexCommand(client * c)329 void lindexCommand(client *c) {
330     robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp]);
331     if (o == NULL || checkType(c,o,OBJ_LIST)) return;
332     long index;
333     robj *value = NULL;
334 
335     if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK))
336         return;
337 
338     if (o->encoding == OBJ_ENCODING_QUICKLIST) {
339         quicklistEntry entry;
340         if (quicklistIndex(o->ptr, index, &entry)) {
341             if (entry.value) {
342                 value = createStringObject((char*)entry.value,entry.sz);
343             } else {
344                 value = createStringObjectFromLongLong(entry.longval);
345             }
346             addReplyBulk(c,value);
347             decrRefCount(value);
348         } else {
349             addReplyNull(c);
350         }
351     } else {
352         serverPanic("Unknown list encoding");
353     }
354 }
355 
lsetCommand(client * c)356 void lsetCommand(client *c) {
357     robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);
358     if (o == NULL || checkType(c,o,OBJ_LIST)) return;
359     long index;
360     robj *value = c->argv[3];
361 
362     if (sdslen(value->ptr) > LIST_MAX_ITEM_SIZE) {
363         addReplyError(c, "Element too large");
364         return;
365     }
366 
367     if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK))
368         return;
369 
370     if (o->encoding == OBJ_ENCODING_QUICKLIST) {
371         quicklist *ql = o->ptr;
372         int replaced = quicklistReplaceAtIndex(ql, index,
373                                                value->ptr, sdslen(value->ptr));
374         if (!replaced) {
375             addReply(c,shared.outofrangeerr);
376         } else {
377             addReply(c,shared.ok);
378             signalModifiedKey(c,c->db,c->argv[1]);
379             notifyKeyspaceEvent(NOTIFY_LIST,"lset",c->argv[1],c->db->id);
380             server.dirty++;
381         }
382     } else {
383         serverPanic("Unknown list encoding");
384     }
385 }
386 
popGenericCommand(client * c,int where)387 void popGenericCommand(client *c, int where) {
388     robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]);
389     if (o == NULL || checkType(c,o,OBJ_LIST)) return;
390 
391     robj *value = listTypePop(o,where);
392     if (value == NULL) {
393         addReplyNull(c);
394     } else {
395         char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
396 
397         addReplyBulk(c,value);
398         decrRefCount(value);
399         notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
400         if (listTypeLength(o) == 0) {
401             notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
402                                 c->argv[1],c->db->id);
403             dbDelete(c->db,c->argv[1]);
404         }
405         signalModifiedKey(c,c->db,c->argv[1]);
406         server.dirty++;
407     }
408 }
409 
lpopCommand(client * c)410 void lpopCommand(client *c) {
411     popGenericCommand(c,LIST_HEAD);
412 }
413 
rpopCommand(client * c)414 void rpopCommand(client *c) {
415     popGenericCommand(c,LIST_TAIL);
416 }
417 
lrangeCommand(client * c)418 void lrangeCommand(client *c) {
419     robj *o;
420     long start, end, llen, rangelen;
421 
422     if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) ||
423         (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return;
424 
425     if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray)) == NULL
426          || checkType(c,o,OBJ_LIST)) return;
427     llen = listTypeLength(o);
428 
429     /* convert negative indexes */
430     if (start < 0) start = llen+start;
431     if (end < 0) end = llen+end;
432     if (start < 0) start = 0;
433 
434     /* Invariant: start >= 0, so this test will be true when end < 0.
435      * The range is empty when start > end or start >= length. */
436     if (start > end || start >= llen) {
437         addReply(c,shared.emptyarray);
438         return;
439     }
440     if (end >= llen) end = llen-1;
441     rangelen = (end-start)+1;
442 
443     /* Return the result in form of a multi-bulk reply */
444     addReplyArrayLen(c,rangelen);
445     if (o->encoding == OBJ_ENCODING_QUICKLIST) {
446         listTypeIterator *iter = listTypeInitIterator(o, start, LIST_TAIL);
447 
448         while(rangelen--) {
449             listTypeEntry entry;
450             listTypeNext(iter, &entry);
451             quicklistEntry *qe = &entry.entry;
452             if (qe->value) {
453                 addReplyBulkCBuffer(c,qe->value,qe->sz);
454             } else {
455                 addReplyBulkLongLong(c,qe->longval);
456             }
457         }
458         listTypeReleaseIterator(iter);
459     } else {
460         serverPanic("List encoding is not QUICKLIST!");
461     }
462 }
463 
ltrimCommand(client * c)464 void ltrimCommand(client *c) {
465     robj *o;
466     long start, end, llen, ltrim, rtrim;
467 
468     if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) ||
469         (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return;
470 
471     if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
472         checkType(c,o,OBJ_LIST)) return;
473     llen = listTypeLength(o);
474 
475     /* convert negative indexes */
476     if (start < 0) start = llen+start;
477     if (end < 0) end = llen+end;
478     if (start < 0) start = 0;
479 
480     /* Invariant: start >= 0, so this test will be true when end < 0.
481      * The range is empty when start > end or start >= length. */
482     if (start > end || start >= llen) {
483         /* Out of range start or start > end result in empty list */
484         ltrim = llen;
485         rtrim = 0;
486     } else {
487         if (end >= llen) end = llen-1;
488         ltrim = start;
489         rtrim = llen-end-1;
490     }
491 
492     /* Remove list elements to perform the trim */
493     if (o->encoding == OBJ_ENCODING_QUICKLIST) {
494         quicklistDelRange(o->ptr,0,ltrim);
495         quicklistDelRange(o->ptr,-rtrim,rtrim);
496     } else {
497         serverPanic("Unknown list encoding");
498     }
499 
500     notifyKeyspaceEvent(NOTIFY_LIST,"ltrim",c->argv[1],c->db->id);
501     if (listTypeLength(o) == 0) {
502         dbDelete(c->db,c->argv[1]);
503         notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
504     }
505     signalModifiedKey(c,c->db,c->argv[1]);
506     server.dirty++;
507     addReply(c,shared.ok);
508 }
509 
510 /* LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
511  *
512  * The "rank" is the position of the match, so if it is 1, the first match
513  * is returned, if it is 2 the second match is returned and so forth.
514  * It is 1 by default. If negative has the same meaning but the search is
515  * performed starting from the end of the list.
516  *
517  * If COUNT is given, instead of returning the single element, a list of
518  * all the matching elements up to "num-matches" are returned. COUNT can
519  * be combiled with RANK in order to returning only the element starting
520  * from the Nth. If COUNT is zero, all the matching elements are returned.
521  *
522  * MAXLEN tells the command to scan a max of len elements. If zero (the
523  * default), all the elements in the list are scanned if needed.
524  *
525  * The returned elements indexes are always referring to what LINDEX
526  * would return. So first element from head is 0, and so forth. */
lposCommand(client * c)527 void lposCommand(client *c) {
528     robj *o, *ele;
529     ele = c->argv[2];
530     int direction = LIST_TAIL;
531     long rank = 1, count = -1, maxlen = 0; /* Count -1: option not given. */
532 
533     if (sdslen(ele->ptr) > LIST_MAX_ITEM_SIZE) {
534         addReplyError(c, "Element too large");
535         return;
536     }
537 
538     /* Parse the optional arguments. */
539     for (int j = 3; j < c->argc; j++) {
540         char *opt = c->argv[j]->ptr;
541         int moreargs = (c->argc-1)-j;
542 
543         if (!strcasecmp(opt,"RANK") && moreargs) {
544             j++;
545             if (getLongFromObjectOrReply(c, c->argv[j], &rank, NULL) != C_OK)
546                 return;
547             if (rank == 0) {
548                 addReplyError(c,"RANK can't be zero: use 1 to start from "
549                                 "the first match, 2 from the second, ...");
550                 return;
551             }
552         } else if (!strcasecmp(opt,"COUNT") && moreargs) {
553             j++;
554             if (getLongFromObjectOrReply(c, c->argv[j], &count, NULL) != C_OK)
555                 return;
556             if (count < 0) {
557                 addReplyError(c,"COUNT can't be negative");
558                 return;
559             }
560         } else if (!strcasecmp(opt,"MAXLEN") && moreargs) {
561             j++;
562             if (getLongFromObjectOrReply(c, c->argv[j], &maxlen, NULL) != C_OK)
563                 return;
564             if (maxlen < 0) {
565                 addReplyError(c,"MAXLEN can't be negative");
566                 return;
567             }
568         } else {
569             addReply(c,shared.syntaxerr);
570             return;
571         }
572     }
573 
574     /* A negative rank means start from the tail. */
575     if (rank < 0) {
576         rank = -rank;
577         direction = LIST_HEAD;
578     }
579 
580     /* We return NULL or an empty array if there is no such key (or
581      * if we find no matches, depending on the presence of the COUNT option. */
582     if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) {
583         if (count != -1)
584             addReply(c,shared.emptyarray);
585         else
586             addReply(c,shared.null[c->resp]);
587         return;
588     }
589     if (checkType(c,o,OBJ_LIST)) return;
590 
591     /* If we got the COUNT option, prepare to emit an array. */
592     void *arraylenptr = NULL;
593     if (count != -1) arraylenptr = addReplyDeferredLen(c);
594 
595     /* Seek the element. */
596     listTypeIterator *li;
597     li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction);
598     listTypeEntry entry;
599     long llen = listTypeLength(o);
600     long index = 0, matches = 0, matchindex = -1, arraylen = 0;
601     while (listTypeNext(li,&entry) && (maxlen == 0 || index < maxlen)) {
602         if (listTypeEqual(&entry,ele)) {
603             matches++;
604             matchindex = (direction == LIST_TAIL) ? index : llen - index - 1;
605             if (matches >= rank) {
606                 if (arraylenptr) {
607                     arraylen++;
608                     addReplyLongLong(c,matchindex);
609                     if (count && matches-rank+1 >= count) break;
610                 } else {
611                     break;
612                 }
613             }
614         }
615         index++;
616         matchindex = -1; /* Remember if we exit the loop without a match. */
617     }
618     listTypeReleaseIterator(li);
619 
620     /* Reply to the client. Note that arraylenptr is not NULL only if
621      * the COUNT option was selected. */
622     if (arraylenptr != NULL) {
623         setDeferredArrayLen(c,arraylenptr,arraylen);
624     } else {
625         if (matchindex != -1)
626             addReplyLongLong(c,matchindex);
627         else
628             addReply(c,shared.null[c->resp]);
629     }
630 }
631 
lremCommand(client * c)632 void lremCommand(client *c) {
633     robj *subject, *obj;
634     obj = c->argv[3];
635     long toremove;
636     long removed = 0;
637 
638     if (sdslen(obj->ptr) > LIST_MAX_ITEM_SIZE) {
639         addReplyError(c, "Element too large");
640         return;
641     }
642 
643     if ((getLongFromObjectOrReply(c, c->argv[2], &toremove, NULL) != C_OK))
644         return;
645 
646     subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero);
647     if (subject == NULL || checkType(c,subject,OBJ_LIST)) return;
648 
649     listTypeIterator *li;
650     if (toremove < 0) {
651         toremove = -toremove;
652         li = listTypeInitIterator(subject,-1,LIST_HEAD);
653     } else {
654         li = listTypeInitIterator(subject,0,LIST_TAIL);
655     }
656 
657     listTypeEntry entry;
658     while (listTypeNext(li,&entry)) {
659         if (listTypeEqual(&entry,obj)) {
660             listTypeDelete(li, &entry);
661             server.dirty++;
662             removed++;
663             if (toremove && removed == toremove) break;
664         }
665     }
666     listTypeReleaseIterator(li);
667 
668     if (removed) {
669         signalModifiedKey(c,c->db,c->argv[1]);
670         notifyKeyspaceEvent(NOTIFY_LIST,"lrem",c->argv[1],c->db->id);
671     }
672 
673     if (listTypeLength(subject) == 0) {
674         dbDelete(c->db,c->argv[1]);
675         notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
676     }
677 
678     addReplyLongLong(c,removed);
679 }
680 
681 /* This is the semantic of this command:
682  *  RPOPLPUSH srclist dstlist:
683  *    IF LLEN(srclist) > 0
684  *      element = RPOP srclist
685  *      LPUSH dstlist element
686  *      RETURN element
687  *    ELSE
688  *      RETURN nil
689  *    END
690  *  END
691  *
692  * The idea is to be able to get an element from a list in a reliable way
693  * since the element is not just returned but pushed against another list
694  * as well. This command was originally proposed by Ezra Zygmuntowicz.
695  */
696 
rpoplpushHandlePush(client * c,robj * dstkey,robj * dstobj,robj * value)697 void rpoplpushHandlePush(client *c, robj *dstkey, robj *dstobj, robj *value) {
698     /* Create the list if the key does not exist */
699     if (!dstobj) {
700         dstobj = createQuicklistObject();
701         quicklistSetOptions(dstobj->ptr, server.list_max_ziplist_size,
702                             server.list_compress_depth);
703         dbAdd(c->db,dstkey,dstobj);
704     }
705     signalModifiedKey(c,c->db,dstkey);
706     listTypePush(dstobj,value,LIST_HEAD);
707     notifyKeyspaceEvent(NOTIFY_LIST,"lpush",dstkey,c->db->id);
708     /* Always send the pushed value to the client. */
709     addReplyBulk(c,value);
710 }
711 
rpoplpushCommand(client * c)712 void rpoplpushCommand(client *c) {
713     robj *sobj, *value;
714     if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]))
715         == NULL || checkType(c,sobj,OBJ_LIST)) return;
716 
717     if (listTypeLength(sobj) == 0) {
718         /* This may only happen after loading very old RDB files. Recent
719          * versions of Redis delete keys of empty lists. */
720         addReplyNull(c);
721     } else {
722         robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
723         robj *touchedkey = c->argv[1];
724 
725         if (dobj && checkType(c,dobj,OBJ_LIST)) return;
726         value = listTypePop(sobj,LIST_TAIL);
727         /* We saved touched key, and protect it, since rpoplpushHandlePush
728          * may change the client command argument vector (it does not
729          * currently). */
730         incrRefCount(touchedkey);
731         rpoplpushHandlePush(c,c->argv[2],dobj,value);
732 
733         /* listTypePop returns an object with its refcount incremented */
734         decrRefCount(value);
735 
736         /* Delete the source list when it is empty */
737         notifyKeyspaceEvent(NOTIFY_LIST,"rpop",touchedkey,c->db->id);
738         if (listTypeLength(sobj) == 0) {
739             dbDelete(c->db,touchedkey);
740             notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
741                                 touchedkey,c->db->id);
742         }
743         signalModifiedKey(c,c->db,touchedkey);
744         decrRefCount(touchedkey);
745         server.dirty++;
746         if (c->cmd->proc == brpoplpushCommand) {
747             rewriteClientCommandVector(c,3,shared.rpoplpush,c->argv[1],c->argv[2]);
748         }
749     }
750 }
751 
752 /*-----------------------------------------------------------------------------
753  * Blocking POP operations
754  *----------------------------------------------------------------------------*/
755 
756 /* This is a helper function for handleClientsBlockedOnKeys(). Its work
757  * is to serve a specific client (receiver) that is blocked on 'key'
758  * in the context of the specified 'db', doing the following:
759  *
760  * 1) Provide the client with the 'value' element.
761  * 2) If the dstkey is not NULL (we are serving a BRPOPLPUSH) also push the
762  *    'value' element on the destination list (the LPUSH side of the command).
763  * 3) Propagate the resulting BRPOP, BLPOP and additional LPUSH if any into
764  *    the AOF and replication channel.
765  *
766  * The argument 'where' is LIST_TAIL or LIST_HEAD, and indicates if the
767  * 'value' element was popped from the head (BLPOP) or tail (BRPOP) so that
768  * we can propagate the command properly.
769  *
770  * The function returns C_OK if we are able to serve the client, otherwise
771  * C_ERR is returned to signal the caller that the list POP operation
772  * should be undone as the client was not served: This only happens for
773  * BRPOPLPUSH that fails to push the value to the destination key as it is
774  * of the wrong type. */
serveClientBlockedOnList(client * receiver,robj * key,robj * dstkey,redisDb * db,robj * value,int where)775 int serveClientBlockedOnList(client *receiver, robj *key, robj *dstkey, redisDb *db, robj *value, int where)
776 {
777     robj *argv[3];
778 
779     if (dstkey == NULL) {
780         /* Propagate the [LR]POP operation. */
781         argv[0] = (where == LIST_HEAD) ? shared.lpop :
782                                           shared.rpop;
783         argv[1] = key;
784         propagate((where == LIST_HEAD) ?
785             server.lpopCommand : server.rpopCommand,
786             db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL);
787 
788         /* BRPOP/BLPOP */
789         addReplyArrayLen(receiver,2);
790         addReplyBulk(receiver,key);
791         addReplyBulk(receiver,value);
792 
793         /* Notify event. */
794         char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
795         notifyKeyspaceEvent(NOTIFY_LIST,event,key,receiver->db->id);
796     } else {
797         /* BRPOPLPUSH */
798         robj *dstobj =
799             lookupKeyWrite(receiver->db,dstkey);
800         if (!(dstobj &&
801              checkType(receiver,dstobj,OBJ_LIST)))
802         {
803             rpoplpushHandlePush(receiver,dstkey,dstobj,
804                 value);
805             /* Propagate the RPOPLPUSH operation. */
806             argv[0] = shared.rpoplpush;
807             argv[1] = key;
808             argv[2] = dstkey;
809             propagate(server.rpoplpushCommand,
810                 db->id,argv,3,
811                 PROPAGATE_AOF|
812                 PROPAGATE_REPL);
813 
814             /* Notify event ("lpush" was notified by rpoplpushHandlePush). */
815             notifyKeyspaceEvent(NOTIFY_LIST,"rpop",key,receiver->db->id);
816         } else {
817             /* BRPOPLPUSH failed because of wrong
818              * destination type. */
819             return C_ERR;
820         }
821     }
822     return C_OK;
823 }
824 
825 /* Blocking RPOP/LPOP */
blockingPopGenericCommand(client * c,int where)826 void blockingPopGenericCommand(client *c, int where) {
827     robj *o;
828     mstime_t timeout;
829     int j;
830 
831     if (getTimeoutFromObjectOrReply(c,c->argv[c->argc-1],&timeout,UNIT_SECONDS)
832         != C_OK) return;
833 
834     for (j = 1; j < c->argc-1; j++) {
835         o = lookupKeyWrite(c->db,c->argv[j]);
836         if (o != NULL) {
837             if (o->type != OBJ_LIST) {
838                 addReply(c,shared.wrongtypeerr);
839                 return;
840             } else {
841                 if (listTypeLength(o) != 0) {
842                     /* Non empty list, this is like a normal [LR]POP. */
843                     char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
844                     robj *value = listTypePop(o,where);
845                     serverAssert(value != NULL);
846 
847                     addReplyArrayLen(c,2);
848                     addReplyBulk(c,c->argv[j]);
849                     addReplyBulk(c,value);
850                     decrRefCount(value);
851                     notifyKeyspaceEvent(NOTIFY_LIST,event,
852                                         c->argv[j],c->db->id);
853                     if (listTypeLength(o) == 0) {
854                         dbDelete(c->db,c->argv[j]);
855                         notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
856                                             c->argv[j],c->db->id);
857                     }
858                     signalModifiedKey(c,c->db,c->argv[j]);
859                     server.dirty++;
860 
861                     /* Replicate it as an [LR]POP instead of B[LR]POP. */
862                     rewriteClientCommandVector(c,2,
863                         (where == LIST_HEAD) ? shared.lpop : shared.rpop,
864                         c->argv[j]);
865                     return;
866                 }
867             }
868         }
869     }
870 
871     /* If we are inside a MULTI/EXEC and the list is empty the only thing
872      * we can do is treating it as a timeout (even with timeout 0). */
873     if (c->flags & CLIENT_MULTI) {
874         addReplyNullArray(c);
875         return;
876     }
877 
878     /* If the keys do not exist we must block */
879     blockForKeys(c,BLOCKED_LIST,c->argv + 1,c->argc - 2,timeout,NULL,NULL);
880 }
881 
blpopCommand(client * c)882 void blpopCommand(client *c) {
883     blockingPopGenericCommand(c,LIST_HEAD);
884 }
885 
brpopCommand(client * c)886 void brpopCommand(client *c) {
887     blockingPopGenericCommand(c,LIST_TAIL);
888 }
889 
brpoplpushCommand(client * c)890 void brpoplpushCommand(client *c) {
891     mstime_t timeout;
892 
893     if (getTimeoutFromObjectOrReply(c,c->argv[3],&timeout,UNIT_SECONDS)
894         != C_OK) return;
895 
896     robj *key = lookupKeyWrite(c->db, c->argv[1]);
897 
898     if (key == NULL) {
899         if (c->flags & CLIENT_MULTI) {
900             /* Blocking against an empty list in a multi state
901              * returns immediately. */
902             addReplyNull(c);
903         } else {
904             /* The list is empty and the client blocks. */
905             blockForKeys(c,BLOCKED_LIST,c->argv + 1,1,timeout,c->argv[2],NULL);
906         }
907     } else {
908         if (key->type != OBJ_LIST) {
909             addReply(c, shared.wrongtypeerr);
910         } else {
911             /* The list exists and has elements, so
912              * the regular rpoplpushCommand is executed. */
913             serverAssertWithInfo(c,key,listTypeLength(key) > 0);
914             rpoplpushCommand(c);
915         }
916     }
917 }
918