Skip to content

Commit 568fc3a

Browse files
committed
[WIP] Issue phpredis#1894
Add GT and LT options to ZADD.
1 parent a95384f commit 568fc3a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

redis_commands.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,7 @@ int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
28422842
char **cmd, int *cmd_len, short *slot, void **ctx)
28432843
{
28442844
zval *z_args;
2845-
char *key, *val, *exp_type = NULL;
2845+
char *key, *val, *exp_type = NULL, *range_type = NULL;
28462846
size_t key_len, val_len;
28472847
int key_free, val_free;
28482848
int num = ZEND_NUM_ARGS(), i = 1, argc;
@@ -2857,7 +2857,7 @@ int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
28572857
return FAILURE;
28582858
}
28592859

2860-
// Need key, [NX|XX] [CH] [INCR] score, value, [score, value...] */
2860+
// Need key, [NX|XX] [LT|GT] [CH] [INCR] score, value, [score, value...] */
28612861
if (num % 2 == 0) {
28622862
if (Z_TYPE(z_args[1]) != IS_ARRAY) {
28632863
efree(z_args);
@@ -2868,6 +2868,8 @@ int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
28682868
if (Z_TYPE_P(z_opt) == IS_STRING) {
28692869
if (ZVAL_IS_NX_XX_ARG(z_opt)) {
28702870
exp_type = Z_STRVAL_P(z_opt);
2871+
} else if (ZVAL_STRICMP_STATIC(z_opt, "LT") || ZVAL_STRICMP_STATIC(z_opt, "GT")) {
2872+
range_type = Z_STRVAL_P(z_opt);
28712873
} else if (ZVAL_STRICMP_STATIC(z_opt, "CH")) {
28722874
ch = 1;
28732875
} else if (ZVAL_STRICMP_STATIC(z_opt, "INCR")) {
@@ -2883,6 +2885,7 @@ int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
28832885
} ZEND_HASH_FOREACH_END();
28842886
argc = num - 1;
28852887
if (exp_type) argc++;
2888+
if (range_type) argc++;
28862889
argc += ch + incr;
28872890
i++;
28882891
} else {
@@ -2905,6 +2908,7 @@ int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
29052908
if (key_free) efree(key);
29062909

29072910
if (exp_type) redis_cmd_append_sstr(&cmdstr, exp_type, 2);
2911+
if (range_type) redis_cmd_append_sstr(&cmdstr, range_type, 2);
29082912
if (ch) redis_cmd_append_sstr(&cmdstr, "CH", 2);
29092913
if (incr) redis_cmd_append_sstr(&cmdstr, "INCR", 4);
29102914

tests/RedisTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,11 @@ public function testZX() {
22052205
$this->assertTrue(1 === $this->redis->zAdd('key', [], 1, 'val1')); // empty options
22062206
$this->assertTrue(1 === $this->redis->zAdd('key', ['nx'], 3, 'val3')); // nx option
22072207
$this->assertTrue(0 === $this->redis->zAdd('key', ['xx'], 3, 'val3')); // xx option
2208+
2209+
if (version_compare($this->version, "6.2.0") >= 0) {
2210+
$this->assertTrue(0 === $this->redis->zAdd('key', ['lt'], 4, 'val3')); // lt option
2211+
$this->assertTrue(0 === $this->redis->zAdd('key', ['gt'], 2, 'val3')); // gt option
2212+
}
22082213
}
22092214

22102215
$this->assertTrue(['val0', 'val1', 'val2', 'val3', 'val4', 'val5'] === $this->redis->zRange('key', 0, -1));

0 commit comments

Comments
 (0)