Commits

Anonymous committed 0b66776

add LISMEMBER.

  • Participants
  • Parent commits 0c7a9de
  • Tags twitter-20100804

Comments (0)

Files changed (4)

     {"blpop",blpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
     {"llen",llenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
     {"lindex",lindexCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
+    {"lismember",lismemberCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
     {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
     {"lrange",lrangeCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
     {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
 void rpopCommand(redisClient *c);
 void llenCommand(redisClient *c);
 void lindexCommand(redisClient *c);
+void lismemberCommand(redisClient *c);
 void lrangeCommand(redisClient *c);
 void ltrimCommand(redisClient *c);
 void typeCommand(redisClient *c);
     }
 }
 
+void lismemberCommand(redisClient *c) {
+    robj *subject;
+    robj *refval = c->argv[2];
+    listTypeIterator *iter;
+    listTypeEntry entry;
+    int found = 0;
+
+    if ((subject = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
+        checkType(c,subject,REDIS_LIST)) return;
+
+    redisAssert(refval->encoding == REDIS_ENCODING_RAW);
+
+    /* Seek refval from head to tail */
+    iter = listTypeInitIterator(subject,0,REDIS_TAIL);
+    while (listTypeNext(iter,&entry)) {
+        if (listTypeEqual(&entry,refval)) {
+            found = 1;
+            break;
+        }
+    }
+    listTypeReleaseIterator(iter);
+
+    if (found) {
+        addReply(c,shared.cone);
+    } else {
+        addReply(c,shared.czero);
+    }
+}
+
 void lsetCommand(redisClient *c) {
     robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);
     if (o == NULL || checkType(c,o,REDIS_LIST)) return;

tests/unit/type/list.tcl

         assert_equal $largevalue(linkedlist) [r lindex mylist2 2]
     }
 
+    test {LISMEMBER - ziplist} {
+        r del mylist1
+        assert_equal 1 [r rpush mylist1 house]
+        assert_equal 2 [r rpush mylist1 boat]
+        assert_encoding ziplist mylist1
+        assert_equal 1 [r lismember mylist1 house]
+        assert_equal 1 [r lismember mylist1 boat]
+        assert_equal 0 [r lismember mylist1 airplane]
+    }
+
+    test {LISMEMBER - regular list} {
+        r del mylist1
+        assert_equal 1 [r rpush mylist1 $largevalue(linkedlist)]
+        assert_equal 2 [r rpush mylist1 boat]
+        assert_encoding linkedlist mylist1
+        assert_equal 1 [r lismember mylist1 $largevalue(linkedlist)]
+        assert_equal 1 [r lismember mylist1 boat]
+        assert_equal 0 [r lismember mylist1 airplane]
+    }
+
     test {DEL a list - ziplist} {
         assert_equal 1 [r del myziplist2]
         assert_equal 0 [r exists myziplist2]