Skip to content

Commit e9d5e21

Browse files
Support for '-inf', 'inf', and '+inf' for WEIGHTS
Redis allows the use of these specialized numbers in the WEIGHTS argument for things like ZUNIONSTORE AND ZINTERSTORE
1 parent da0cd3f commit e9d5e21

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

redis.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,9 +4394,15 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
43944394
zend_hash_get_current_data_ex(arr_weights_hash, (void**) &data, &pointer) == SUCCESS;
43954395
zend_hash_move_forward_ex(arr_weights_hash, &pointer)) {
43964396

4397-
if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE) {
4398-
continue; // ignore non-numeric arguments.
4399-
}
4397+
// Ignore non numeric arguments, unless they're the special Redis numbers
4398+
// "inf" ,"-inf", and "+inf" which can be passed as weights
4399+
if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE &&
4400+
strncasecmp(Z_STRVAL_PP(data), "inf", sizeof("inf")) != 0 &&
4401+
strncasecmp(Z_STRVAL_PP(data), "-inf", sizeof("-inf")) != 0 &&
4402+
strncasecmp(Z_STRVAL_PP(data), "+inf", sizeof("+inf")) != 0)
4403+
{
4404+
continue;
4405+
}
44004406

44014407
old_cmd = NULL;
44024408
if(*cmd) {
@@ -4412,12 +4418,18 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
44124418
, integer_length(Z_LVAL_PP(data)), Z_LVAL_PP(data));
44134419

44144420
} else if(Z_TYPE_PP(data) == IS_DOUBLE) {
4415-
44164421
cmd_len = redis_cmd_format(&cmd,
44174422
"%s" /* cmd */
44184423
"$%f" _NL /* data, including size */
44194424
, cmd, cmd_len
44204425
, Z_DVAL_PP(data));
4426+
} else if(Z_TYPE_PP(data) == IS_STRING) {
4427+
cmd_len = redis_cmd_format(&cmd,
4428+
"%s" /* cmd */
4429+
"$%d" _NL /* data len */
4430+
"%s" _NL /* data */
4431+
, cmd, cmd_len, Z_STRLEN_PP(data),
4432+
Z_STRVAL_PP(data), Z_STRLEN_PP(data));
44214433
}
44224434

44234435
// keep track of elements added

0 commit comments

Comments
 (0)