Skip to content

Commit 4740424

Browse files
QuChen88antirez
authored andcommitted
Implement getKeys procedure for georadius and georadiusbymember
commands.
1 parent 5877c02 commit 4740424

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/db.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,44 @@ int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkey
13121312
return keys;
13131313
}
13141314

1315+
/* Helper function to extract keys from following commands:
1316+
* GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC]
1317+
* [COUNT count] [STORE key] [STOREDIST key]
1318+
* GEORADIUSBYMEMBER key member radius unit ... options ... */
1319+
int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
1320+
int i, num, *keys;
1321+
UNUSED(cmd);
1322+
1323+
/* Check for the presence of the stored key in the command */
1324+
int stored_key = -1;
1325+
for (i = 5; i < argc; i++) {
1326+
char *arg = argv[i]->ptr;
1327+
/* For the case when user specifies both "store" and "storedist" options, the
1328+
* second key specified would override the first key. This behavior is kept
1329+
* the same as in georadiusCommand method.
1330+
*/
1331+
if ((!strcasecmp(arg, "store") || !strcasecmp(arg, "storedist")) && ((i+1) < argc)) {
1332+
stored_key = i+1;
1333+
i++;
1334+
}
1335+
}
1336+
num = 1 + (stored_key == -1 ? 0 : 1);
1337+
1338+
/* Keys in the command come from two places:
1339+
* argv[1] = key,
1340+
* argv[5...n] = stored key if present
1341+
*/
1342+
keys = zmalloc(sizeof(int) * num);
1343+
1344+
/* Add all key positions to keys[] */
1345+
keys[0] = 1;
1346+
if(num > 1) {
1347+
keys[1] = stored_key;
1348+
}
1349+
*numkeys = num;
1350+
return keys;
1351+
}
1352+
13151353
/* Slot to Key API. This is used by Redis Cluster in order to obtain in
13161354
* a fast way a key that belongs to a specified hash slot. This is useful
13171355
* while rehashing the cluster and in other conditions when we need to

src/server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ struct redisCommand redisCommandTable[] = {
290290
{"wait",waitCommand,3,"s",0,NULL,0,0,0,0,0},
291291
{"command",commandCommand,0,"lt",0,NULL,0,0,0,0,0},
292292
{"geoadd",geoaddCommand,-5,"wm",0,NULL,1,1,1,0,0},
293-
{"georadius",georadiusCommand,-6,"w",0,NULL,1,1,1,0,0},
294-
{"georadiusbymember",georadiusByMemberCommand,-5,"w",0,NULL,1,1,1,0,0},
293+
{"georadius",georadiusCommand,-6,"w",0,georadiusGetKeys,1,1,1,0,0},
294+
{"georadiusbymember",georadiusByMemberCommand,-5,"w",0,georadiusGetKeys,1,1,1,0,0},
295295
{"geohash",geohashCommand,-2,"r",0,NULL,1,1,1,0,0},
296296
{"geopos",geoposCommand,-2,"r",0,NULL,1,1,1,0,0},
297297
{"geodist",geodistCommand,-4,"r",0,NULL,1,1,1,0,0},

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *num
17301730
int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
17311731
int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
17321732
int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1733+
int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
17331734

17341735
/* Cluster */
17351736
void clusterInit(void);

0 commit comments

Comments
 (0)