Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf6f31e

Browse files
committed
Issue #1894
Add the ANY argument to GEOSEARCH and GEORADIUS
1 parent 677c9da commit bf6f31e

1 file changed

Lines changed: 51 additions & 15 deletions

File tree

redis_commands.c

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct geoOptions {
5252
int withdist;
5353
int withhash;
5454
long count;
55+
zend_bool any;
5556
geoSortType sort;
5657
geoStoreType store;
5758
zend_string *key;
@@ -3379,24 +3380,38 @@ geoStoreType get_georadius_store_type(zend_string *key) {
33793380
static int get_georadius_opts(HashTable *ht, geoOptions *opts) {
33803381
char *optstr;
33813382
zend_string *zkey;
3382-
zval *optval;
3383+
zval *optval, *z_tmp;
33833384

33843385
/* Iterate over our argument array, collating which ones we have */
33853386
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, zkey, optval) {
33863387
ZVAL_DEREF(optval);
33873388

33883389
/* If the key is numeric it's a non value option */
33893390
if (zkey) {
3390-
if (ZSTR_LEN(zkey) == 5 && !strcasecmp(ZSTR_VAL(zkey), "count")) {
3391-
if (Z_TYPE_P(optval) != IS_LONG || Z_LVAL_P(optval) <= 0) {
3392-
php_error_docref(NULL, E_WARNING,
3393-
"COUNT must be an integer > 0!");
3391+
if (zend_string_equals_literal_ci(zkey, "COUNT")) {
3392+
if (Z_TYPE_P(optval) == IS_ARRAY) {
3393+
if ((z_tmp = zend_hash_index_find(Z_ARRVAL_P(optval), 0)) == NULL ||
3394+
Z_TYPE_P(z_tmp) != IS_LONG ||
3395+
(opts->count = Z_LVAL_P(z_tmp)) <= 0
3396+
) {
3397+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
3398+
if (opts->key) zend_string_release(opts->key);
3399+
return FAILURE;
3400+
}
3401+
if ((z_tmp = zend_hash_index_find(Z_ARRVAL_P(optval), 1)) != NULL) {
3402+
opts->any = zval_is_true(z_tmp);
3403+
}
3404+
} else if (Z_TYPE_P(optval) == IS_LONG) {
3405+
if ((opts->count = Z_LVAL_P(optval)) <= 0) {
3406+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
3407+
if (opts->key) zend_string_release(opts->key);
3408+
return FAILURE;
3409+
}
3410+
} else {
3411+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
33943412
if (opts->key) zend_string_release(opts->key);
33953413
return FAILURE;
33963414
}
3397-
3398-
/* Set our count */
3399-
opts->count = Z_LVAL_P(optval);
34003415
} else if (opts->store == STORE_NONE) {
34013416
opts->store = get_georadius_store_type(zkey);
34023417
if (opts->store != STORE_NONE) {
@@ -3462,6 +3477,9 @@ void append_georadius_opts(RedisSock *redis_sock, smart_string *str, short *slot
34623477
if (opt->count) {
34633478
REDIS_CMD_APPEND_SSTR_STATIC(str, "COUNT");
34643479
redis_cmd_append_sstr_long(str, opt->count);
3480+
if (opt->any) {
3481+
REDIS_CMD_APPEND_SSTR_STATIC(str, "ANY");
3482+
}
34653483
}
34663484

34673485
/* Append store options if we've got them */
@@ -3516,7 +3534,7 @@ int redis_georadius_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
35163534

35173535
/* Increment argc depending on options */
35183536
argc += gopts.withcoord + gopts.withdist + gopts.withhash +
3519-
(gopts.sort != SORT_NONE) + (gopts.count ? 2 : 0) +
3537+
(gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0) +
35203538
(gopts.store != STORE_NONE ? 2 : 0);
35213539

35223540
/* Begin construction of our command */
@@ -3585,7 +3603,7 @@ int redis_georadiusbymember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
35853603

35863604
/* Increment argc based on options */
35873605
argc += gopts.withcoord + gopts.withdist + gopts.withhash +
3588-
(gopts.sort != SORT_NONE) + (gopts.count ? 2 : 0) +
3606+
(gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0) +
35893607
(gopts.store != STORE_NONE ? 2 : 0);
35903608

35913609
/* Begin command construction*/
@@ -3631,7 +3649,7 @@ redis_geosearch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
36313649
size_t keylen, unitlen;
36323650
geoOptions gopts = {0};
36333651
smart_string cmdstr = {0};
3634-
zval *position, *shape, *opts = NULL, *z_ele;
3652+
zval *position, *shape, *opts = NULL, *z_ele, *z_tmp;
36353653
zend_string *zkey;
36363654

36373655
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szzs|a",
@@ -3665,11 +3683,26 @@ redis_geosearch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
36653683
ZVAL_DEREF(z_ele);
36663684
if (zkey != NULL) {
36673685
if (zend_string_equals_literal_ci(zkey, "COUNT")) {
3668-
if (Z_TYPE_P(z_ele) != IS_LONG || Z_LVAL_P(z_ele) <= 0) {
3669-
php_error_docref(NULL, E_WARNING, "COUNT must be an integer > 0!");
3686+
if (Z_TYPE_P(z_ele) == IS_ARRAY) {
3687+
if ((z_tmp = zend_hash_index_find(Z_ARRVAL_P(z_ele), 0)) == NULL ||
3688+
Z_TYPE_P(z_tmp) != IS_LONG ||
3689+
(gopts.count = Z_LVAL_P(z_tmp)) <= 0
3690+
) {
3691+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
3692+
return FAILURE;
3693+
}
3694+
if ((z_tmp = zend_hash_index_find(Z_ARRVAL_P(z_ele), 1)) != NULL) {
3695+
gopts.any = zval_is_true(z_tmp);
3696+
}
3697+
} else if (Z_TYPE_P(z_ele) == IS_LONG) {
3698+
if ((gopts.count = Z_LVAL_P(z_ele)) <= 0) {
3699+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
3700+
return FAILURE;
3701+
}
3702+
} else {
3703+
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
36703704
return FAILURE;
36713705
}
3672-
gopts.count = Z_LVAL_P(z_ele);
36733706
}
36743707
} else if (Z_TYPE_P(z_ele) == IS_STRING) {
36753708
if (!strcasecmp(Z_STRVAL_P(z_ele), "WITHCOORD")) {
@@ -3689,7 +3722,7 @@ redis_geosearch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
36893722

36903723
/* Increment argc based on options */
36913724
argc += gopts.withcoord + gopts.withdist + gopts.withhash
3692-
+ (gopts.sort != SORT_NONE) + (gopts.count ? 2 : 0);
3725+
+ (gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0);
36933726

36943727
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "GEOSEARCH");
36953728
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
@@ -3733,6 +3766,9 @@ redis_geosearch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
37333766
if (gopts.count) {
37343767
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
37353768
redis_cmd_append_sstr_long(&cmdstr, gopts.count);
3769+
if (gopts.any) {
3770+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ANY");
3771+
}
37363772
}
37373773

37383774
if ((argc = gopts.withcoord + gopts.withdist + gopts.withhash) > 0) {

0 commit comments

Comments
 (0)