Skip to content
This repository
Browse code

add LISMEMBER.

  • Loading branch information...
commit 0b6677608d54ba31d338ed23fb50754890b97e4d 1 parent 0c7a9de
Robey Pointer authored
1  src/redis.c
@@ -91,6 +91,7 @@ struct redisCommand readonlyCommandTable[] = {
91 91 {"blpop",blpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
92 92 {"llen",llenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
93 93 {"lindex",lindexCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
  94 + {"lismember",lismemberCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
94 95 {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
95 96 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
96 97 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
1  src/redis.h
@@ -808,6 +808,7 @@ void lpopCommand(redisClient *c);
808 808 void rpopCommand(redisClient *c);
809 809 void llenCommand(redisClient *c);
810 810 void lindexCommand(redisClient *c);
  811 +void lismemberCommand(redisClient *c);
811 812 void lrangeCommand(redisClient *c);
812 813 void ltrimCommand(redisClient *c);
813 814 void typeCommand(redisClient *c);
29 src/t_list.c
@@ -405,6 +405,35 @@ void lindexCommand(redisClient *c) {
405 405 }
406 406 }
407 407
  408 +void lismemberCommand(redisClient *c) {
  409 + robj *subject;
  410 + robj *refval = c->argv[2];
  411 + listTypeIterator *iter;
  412 + listTypeEntry entry;
  413 + int found = 0;
  414 +
  415 + if ((subject = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
  416 + checkType(c,subject,REDIS_LIST)) return;
  417 +
  418 + redisAssert(refval->encoding == REDIS_ENCODING_RAW);
  419 +
  420 + /* Seek refval from head to tail */
  421 + iter = listTypeInitIterator(subject,0,REDIS_TAIL);
  422 + while (listTypeNext(iter,&entry)) {
  423 + if (listTypeEqual(&entry,refval)) {
  424 + found = 1;
  425 + break;
  426 + }
  427 + }
  428 + listTypeReleaseIterator(iter);
  429 +
  430 + if (found) {
  431 + addReply(c,shared.cone);
  432 + } else {
  433 + addReply(c,shared.czero);
  434 + }
  435 +}
  436 +
408 437 void lsetCommand(redisClient *c) {
409 438 robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);
410 439 if (o == NULL || checkType(c,o,REDIS_LIST)) return;
20 tests/unit/type/list.tcl
@@ -55,6 +55,26 @@ start_server {
55 55 assert_equal $largevalue(linkedlist) [r lindex mylist2 2]
56 56 }
57 57
  58 + test {LISMEMBER - ziplist} {
  59 + r del mylist1
  60 + assert_equal 1 [r rpush mylist1 house]
  61 + assert_equal 2 [r rpush mylist1 boat]
  62 + assert_encoding ziplist mylist1
  63 + assert_equal 1 [r lismember mylist1 house]
  64 + assert_equal 1 [r lismember mylist1 boat]
  65 + assert_equal 0 [r lismember mylist1 airplane]
  66 + }
  67 +
  68 + test {LISMEMBER - regular list} {
  69 + r del mylist1
  70 + assert_equal 1 [r rpush mylist1 $largevalue(linkedlist)]
  71 + assert_equal 2 [r rpush mylist1 boat]
  72 + assert_encoding linkedlist mylist1
  73 + assert_equal 1 [r lismember mylist1 $largevalue(linkedlist)]
  74 + assert_equal 1 [r lismember mylist1 boat]
  75 + assert_equal 0 [r lismember mylist1 airplane]
  76 + }
  77 +
58 78 test {DEL a list - ziplist} {
59 79 assert_equal 1 [r del myziplist2]
60 80 assert_equal 0 [r exists myziplist2]

0 comments on commit 0b66776

Please sign in to comment.
Something went wrong with that request. Please try again.