Skip to content

Commit b1ad543

Browse files
ZRANGEBYLEX command
This commit adds the command ZRANGEBYLEX to phpredis, which was introduced in 2.8.9. Like with most commands, phpredis will do some simple validation on the client side, to avoid sending calls which are not correct (e.g. min/max that aren't valid for the call, etc). Addresses phpredis#498 and phpredis#465
1 parent c610cec commit b1ad543

File tree

7 files changed

+224
-88
lines changed

7 files changed

+224
-88
lines changed

README.markdown

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,6 +2488,7 @@ while(($arr_mems = $redis->sscan('set', $it, "*pattern*"))!==FALSE) {
24882488
* [zInter](#zinter) - Intersect multiple sorted sets and store the resulting sorted set in a new key
24892489
* [zRange](#zrange) - Return a range of members in a sorted set, by index
24902490
* [zRangeByScore, zRevRangeByScore](#zrangebyscore-zrevrangebyscore) - Return a range of members in a sorted set, by score
2491+
* [zRangeByLex](#zrangebylex) - Return a lexigraphical range from members that share the same score
24912492
* [zRank, zRevRank](#zrank-zrevrank) - Determine the index of a member in a sorted set
24922493
* [zRem, zDelete](#zrem-zdelete) - Remove one or more members from a sorted set
24932494
* [zRemRangeByRank, zDeleteRangeByRank](#zremrangebyrank-zdeleterangebyrank) - Remove all members in a sorted set within the given indexes
@@ -2671,6 +2672,30 @@ $redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 1)); /* array('val2
26712672
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE, 'limit' => array(1, 1)); /* array('val2' => 2) */
26722673
~~~
26732674

2675+
### zRangeByLex
2676+
-----
2677+
_**Description**_: Returns a lexigraphical range of members in a sorted set, assuming the members have the same score. The min and max values are required to start with '(' (exclusive), '[' (inclusive), or be exactly the values '-' (negative inf) or '+' (positive inf). The command must be called with either three *or* five arguments or will return FALSE.
2678+
2679+
##### *Parameters*
2680+
*key*: The ZSET you wish to run against
2681+
*min*: The minimum alphanumeric value you wish to get
2682+
*max*: The maximum alphanumeric value you wish to get
2683+
*offset*: Optional argument if you wish to start somewhere other than the first element.
2684+
*limit*: Optional argument if you wish to limit the number of elements returned.
2685+
2686+
##### *Return value*
2687+
*Array* containing the values in the specified range.
2688+
2689+
##### *Example*
2690+
~~~
2691+
foreach(Array('a','b','c','d','e','f','g') as $c)
2692+
$redis->zAdd('key',0,$c);
2693+
2694+
$redis->zRangeByLex('key','-','[c') /* Array('a','b','c'); */
2695+
$redis->zRangeByLex('key','-','(c') /* Array('a','b') */
2696+
$redis->zRangeByLex('key','-','[c',1,2) /* Array('b','c') */
2697+
~~~
2698+
26742699
### zRank, zRevRank
26752700
-----
26762701
_**Description**_: Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score. zRevRank starts at 0 for the item with the *largest* score.

common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
/* reply types */
3232
typedef enum _REDIS_REPLY_TYPE {
33-
TYPE_LINE = '+',
33+
TYPE_EOF = EOF,
34+
TYPE_LINE = '+',
3435
TYPE_INT = ':',
3536
TYPE_ERR = '-',
3637
TYPE_BULK = '$',
@@ -178,6 +179,12 @@ else if(redis_sock->mode == MULTI) { \
178179
#define IS_EX_PX_ARG(a) (IS_EX_ARG(a) || IS_PX_ARG(a))
179180
#define IS_NX_XX_ARG(a) (IS_NX_ARG(a) || IS_XX_ARG(a))
180181

182+
/* Given a string and length, validate a zRangeByLex argument. The semantics
183+
* here are that the argument must start with '(' or '[' or be just the char
184+
* '+' or '-' */
185+
#define IS_LEX_ARG(s,l) \
186+
(l>0 && (*s=='(' || *s=='[' || (l==1 && (*s=='+' || *s=='-'))))
187+
181188
typedef enum {ATOMIC, MULTI, PIPELINE} redis_mode;
182189

183190
typedef struct fold_item {

library.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,7 @@ redis_read_multibulk_recursive(RedisSock *redis_sock, int elements, zval **z_ret
19111911
add_next_index_zval(*z_ret, z_subelem);
19121912
redis_read_multibulk_recursive(redis_sock, reply_info, &z_subelem TSRMLS_CC);
19131913
break;
1914+
default: break; /* We know it's not < 0 from previous check */
19141915
}
19151916

19161917
/* Decrement our element counter */

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ PHP_METHOD(Redis, zDelete);
111111
PHP_METHOD(Redis, zRange);
112112
PHP_METHOD(Redis, zReverseRange);
113113
PHP_METHOD(Redis, zRangeByScore);
114+
PHP_METHOD(Redis, zRangeByLex);
114115
PHP_METHOD(Redis, zRevRangeByScore);
115116
PHP_METHOD(Redis, zCount);
116117
PHP_METHOD(Redis, zDeleteRangeByScore);

0 commit comments

Comments
 (0)