2014-05-12 14:38:17 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
|
Multiple GEORADIUS bugs fixed.
By grepping the continuous integration errors log a number of GEORADIUS
tests failures were detected.
Fortunately when a GEORADIUS failure happens, the test suite logs enough
information in order to reproduce the problem: the PRNG seed,
coordinates and radius of the query.
By reproducing the issues, three different bugs were discovered and
fixed in this commit. This commit also improves the already good
reporting of the fuzzer and adds the failure vectors as regression
tests.
The issues found:
1. We need larger squares around the poles in order to cover the area
requested by the user. There were already checks in order to use a
smaller step (larger squares) but the limit set (+/- 67 degrees) is not
enough in certain edge cases, so 66 is used now.
2. Even near the equator, when the search area center is very near the
edge of the square, the north, south, west or ovest square may not be
able to fully cover the specified radius. Now a test is performed at the
edge of the initial guessed search area, and larger squares are used in
case the test fails.
3. Because of rounding errors between Redis and Tcl, sometimes the test
signaled false positives. This is now addressed.
Whenever possible the original code was improved a bit in other ways. A
debugging example stanza was added in order to make the next debugging
session simpler when the next bug is found.
2016-07-27 05:07:23 -04:00
|
|
|
* Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>.
|
2014-05-12 14:38:17 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "geo.h"
|
|
|
|
#include "geohash_helper.h"
|
Multiple GEORADIUS bugs fixed.
By grepping the continuous integration errors log a number of GEORADIUS
tests failures were detected.
Fortunately when a GEORADIUS failure happens, the test suite logs enough
information in order to reproduce the problem: the PRNG seed,
coordinates and radius of the query.
By reproducing the issues, three different bugs were discovered and
fixed in this commit. This commit also improves the already good
reporting of the fuzzer and adds the failure vectors as regression
tests.
The issues found:
1. We need larger squares around the poles in order to cover the area
requested by the user. There were already checks in order to use a
smaller step (larger squares) but the limit set (+/- 67 degrees) is not
enough in certain edge cases, so 66 is used now.
2. Even near the equator, when the search area center is very near the
edge of the square, the north, south, west or ovest square may not be
able to fully cover the specified radius. Now a test is performed at the
edge of the initial guessed search area, and larger squares are used in
case the test fails.
3. Because of rounding errors between Redis and Tcl, sometimes the test
signaled false positives. This is now addressed.
Whenever possible the original code was improved a bit in other ways. A
debugging example stanza was added in order to make the next debugging
session simpler when the next bug is found.
2016-07-27 05:07:23 -04:00
|
|
|
#include "debugmacro.h"
|
2015-06-22 12:08:06 -04:00
|
|
|
|
|
|
|
/* Things exported from t_zset.c only for geo.c, since it is the only other
|
|
|
|
* part of Redis that requires close zset introspection. */
|
|
|
|
unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
|
|
|
|
int zslValueLteMax(double value, zrangespec *spec);
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
/* ====================================================================
|
2015-06-22 12:08:06 -04:00
|
|
|
* This file implements the following commands:
|
|
|
|
*
|
2014-05-12 14:38:17 -04:00
|
|
|
* - geoadd - add coordinates for value to geoset
|
|
|
|
* - georadius - search radius by coordinates in geoset
|
|
|
|
* - georadiusbymember - search radius based on geoset member position
|
|
|
|
* ==================================================================== */
|
|
|
|
|
2015-06-22 12:08:06 -04:00
|
|
|
/* ====================================================================
|
|
|
|
* geoArray implementation
|
|
|
|
* ==================================================================== */
|
|
|
|
|
|
|
|
/* Create a new array of geoPoints. */
|
|
|
|
geoArray *geoArrayCreate(void) {
|
|
|
|
geoArray *ga = zmalloc(sizeof(*ga));
|
|
|
|
/* It gets allocated on first geoArrayAppend() call. */
|
|
|
|
ga->array = NULL;
|
|
|
|
ga->buckets = 0;
|
|
|
|
ga->used = 0;
|
|
|
|
return ga;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new entry and return its pointer so that the caller can populate
|
|
|
|
* it with data. */
|
|
|
|
geoPoint *geoArrayAppend(geoArray *ga) {
|
|
|
|
if (ga->used == ga->buckets) {
|
|
|
|
ga->buckets = (ga->buckets == 0) ? 8 : ga->buckets*2;
|
|
|
|
ga->array = zrealloc(ga->array,sizeof(geoPoint)*ga->buckets);
|
|
|
|
}
|
|
|
|
geoPoint *gp = ga->array+ga->used;
|
|
|
|
ga->used++;
|
|
|
|
return gp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy a geoArray created with geoArrayCreate(). */
|
|
|
|
void geoArrayFree(geoArray *ga) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < ga->used; i++) sdsfree(ga->array[i].member);
|
|
|
|
zfree(ga->array);
|
|
|
|
zfree(ga);
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:38:17 -04:00
|
|
|
/* ====================================================================
|
|
|
|
* Helpers
|
|
|
|
* ==================================================================== */
|
2015-06-29 09:57:17 -04:00
|
|
|
int decodeGeohash(double bits, double *xy) {
|
2014-05-12 14:38:17 -04:00
|
|
|
GeoHashBits hash = { .bits = (uint64_t)bits, .step = GEO_STEP_MAX };
|
2015-06-25 12:05:45 -04:00
|
|
|
return geohashDecodeToLongLatWGS84(hash, xy);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Input Argument Helper */
|
2015-06-23 04:27:45 -04:00
|
|
|
/* Take a pointer to the latitude arg then use the next arg for longitude.
|
2015-07-26 17:17:55 -04:00
|
|
|
* On parse error C_ERR is returned, otherwise C_OK. */
|
2015-10-07 16:27:18 -04:00
|
|
|
int extractLongLatOrReply(client *c, robj **argv, double *xy) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 2; i++) {
|
2015-06-25 12:05:45 -04:00
|
|
|
if (getDoubleFromObjectOrReply(c, argv[i], xy + i, NULL) !=
|
2015-07-26 17:17:55 -04:00
|
|
|
C_OK) {
|
|
|
|
return C_ERR;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
2015-10-07 16:27:18 -04:00
|
|
|
}
|
|
|
|
if (xy[0] < GEO_LONG_MIN || xy[0] > GEO_LONG_MAX ||
|
|
|
|
xy[1] < GEO_LAT_MIN || xy[1] > GEO_LAT_MAX) {
|
|
|
|
addReplySds(c, sdscatprintf(sdsempty(),
|
|
|
|
"-ERR invalid longitude,latitude pair %f,%f\r\n",xy[0],xy[1]));
|
|
|
|
return C_ERR;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
2015-07-26 17:17:55 -04:00
|
|
|
return C_OK;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Input Argument Helper */
|
2015-06-22 12:08:06 -04:00
|
|
|
/* Decode lat/long from a zset member's score.
|
2015-07-26 17:17:55 -04:00
|
|
|
* Returns C_OK on successful decoding, otherwise C_ERR is returned. */
|
2015-06-29 09:57:17 -04:00
|
|
|
int longLatFromMember(robj *zobj, robj *member, double *xy) {
|
2014-05-12 14:38:17 -04:00
|
|
|
double score = 0;
|
|
|
|
|
2015-08-04 03:20:55 -04:00
|
|
|
if (zsetScore(zobj, member->ptr, &score) == C_ERR) return C_ERR;
|
2015-07-26 17:17:55 -04:00
|
|
|
if (!decodeGeohash(score, xy)) return C_ERR;
|
|
|
|
return C_OK;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 06:44:31 -04:00
|
|
|
/* Check that the unit argument matches one of the known units, and returns
|
|
|
|
* the conversion factor to meters (you need to divide meters by the conversion
|
|
|
|
* factor to convert to the right unit).
|
|
|
|
*
|
|
|
|
* If the unit is not valid, an error is reported to the client, and a value
|
|
|
|
* less than zero is returned. */
|
2015-07-26 09:20:46 -04:00
|
|
|
double extractUnitOrReply(client *c, robj *unit) {
|
2015-06-29 06:44:31 -04:00
|
|
|
char *u = unit->ptr;
|
|
|
|
|
2015-06-29 10:02:33 -04:00
|
|
|
if (!strcmp(u, "m")) {
|
2015-06-29 06:44:31 -04:00
|
|
|
return 1;
|
2015-06-29 10:02:33 -04:00
|
|
|
} else if (!strcmp(u, "km")) {
|
|
|
|
return 1000;
|
|
|
|
} else if (!strcmp(u, "ft")) {
|
2015-06-29 06:44:31 -04:00
|
|
|
return 0.3048;
|
2015-06-29 10:02:33 -04:00
|
|
|
} else if (!strcmp(u, "mi")) {
|
2015-06-29 06:44:31 -04:00
|
|
|
return 1609.34;
|
|
|
|
} else {
|
2015-06-29 10:02:33 -04:00
|
|
|
addReplyError(c,
|
|
|
|
"unsupported unit provided. please use m, km, ft, mi");
|
2015-06-29 06:44:31 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 04:27:45 -04:00
|
|
|
/* Input Argument Helper.
|
|
|
|
* Extract the dinstance from the specified two arguments starting at 'argv'
|
|
|
|
* that shouldbe in the form: <number> <unit> and return the dinstance in the
|
2018-07-01 01:24:50 -04:00
|
|
|
* specified unit on success. *conversions is populated with the coefficient
|
2015-06-23 04:27:45 -04:00
|
|
|
* to use in order to convert meters to the unit.
|
|
|
|
*
|
|
|
|
* On error a value less than zero is returned. */
|
2015-07-26 09:20:46 -04:00
|
|
|
double extractDistanceOrReply(client *c, robj **argv,
|
2014-05-12 14:38:17 -04:00
|
|
|
double *conversion) {
|
|
|
|
double distance;
|
|
|
|
if (getDoubleFromObjectOrReply(c, argv[0], &distance,
|
2015-07-26 17:17:55 -04:00
|
|
|
"need numeric radius") != C_OK) {
|
2014-05-12 14:38:17 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-23 06:58:50 -04:00
|
|
|
if (distance < 0) {
|
|
|
|
addReplyError(c,"radius cannot be negative");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-20 04:12:38 -05:00
|
|
|
|
2015-06-29 06:44:31 -04:00
|
|
|
double to_meters = extractUnitOrReply(c,argv[1]);
|
2016-05-04 02:59:56 -04:00
|
|
|
if (to_meters < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-06-29 06:44:31 -04:00
|
|
|
if (conversion) *conversion = to_meters;
|
2014-05-12 14:38:17 -04:00
|
|
|
return distance * to_meters;
|
|
|
|
}
|
|
|
|
|
2016-02-18 18:01:34 -05:00
|
|
|
/* The default addReplyDouble has too much accuracy. We use this
|
2015-06-22 07:08:46 -04:00
|
|
|
* for returning location distances. "5.2145 meters away" is nicer
|
|
|
|
* than "5.2144992818115 meters away." We provide 4 digits after the dot
|
|
|
|
* so that the returned value is decently accurate even when the unit is
|
|
|
|
* the kilometer. */
|
2015-07-26 09:20:46 -04:00
|
|
|
void addReplyDoubleDistance(client *c, double d) {
|
2015-06-22 07:08:46 -04:00
|
|
|
char dbuf[128];
|
|
|
|
int dlen = snprintf(dbuf, sizeof(dbuf), "%.4f", d);
|
2014-05-12 14:38:17 -04:00
|
|
|
addReplyBulkCBuffer(c, dbuf, dlen);
|
|
|
|
}
|
|
|
|
|
2015-06-22 12:08:06 -04:00
|
|
|
/* Helper function for geoGetPointsInRange(): given a sorted set score
|
|
|
|
* representing a point, and another point (the center of our search) and
|
|
|
|
* a radius, appends this entry as a geoPoint into the specified geoArray
|
|
|
|
* only if the point is within the search area.
|
|
|
|
*
|
2015-07-26 17:17:55 -04:00
|
|
|
* returns C_OK if the point is included, or REIDS_ERR if it is outside. */
|
2015-06-25 12:05:45 -04:00
|
|
|
int geoAppendIfWithinRadius(geoArray *ga, double lon, double lat, double radius, double score, sds member) {
|
|
|
|
double distance, xy[2];
|
2015-06-22 12:08:06 -04:00
|
|
|
|
2015-07-26 17:17:55 -04:00
|
|
|
if (!decodeGeohash(score,xy)) return C_ERR; /* Can't decode. */
|
2015-06-23 03:30:14 -04:00
|
|
|
/* Note that geohashGetDistanceIfInRadiusWGS84() takes arguments in
|
|
|
|
* reverse order: longitude first, latitude later. */
|
2015-06-25 12:05:45 -04:00
|
|
|
if (!geohashGetDistanceIfInRadiusWGS84(lon,lat, xy[0], xy[1],
|
2015-06-23 03:03:56 -04:00
|
|
|
radius, &distance))
|
|
|
|
{
|
2015-07-26 17:17:55 -04:00
|
|
|
return C_ERR;
|
2015-06-22 12:08:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the new element. */
|
|
|
|
geoPoint *gp = geoArrayAppend(ga);
|
2015-06-25 12:05:45 -04:00
|
|
|
gp->longitude = xy[0];
|
|
|
|
gp->latitude = xy[1];
|
2015-06-22 12:08:06 -04:00
|
|
|
gp->dist = distance;
|
|
|
|
gp->member = member;
|
|
|
|
gp->score = score;
|
2015-07-26 17:17:55 -04:00
|
|
|
return C_OK;
|
2015-06-22 12:08:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Query a Redis sorted set to extract all the elements between 'min' and
|
|
|
|
* 'max', appending them into the array of geoPoint structures 'gparray'.
|
|
|
|
* The command returns the number of elements added to the array.
|
|
|
|
*
|
|
|
|
* Elements which are farest than 'radius' from the specified 'x' and 'y'
|
|
|
|
* coordinates are not included.
|
|
|
|
*
|
|
|
|
* The ability of this function to append to an existing set of points is
|
|
|
|
* important for good performances because querying by radius is performed
|
|
|
|
* using multiple queries to the sorted set, that we later need to sort
|
|
|
|
* via qsort. Similarly we need to be able to reject points outside the search
|
|
|
|
* radius area ASAP in order to allocate and process more points than needed. */
|
2015-06-25 12:05:45 -04:00
|
|
|
int geoGetPointsInRange(robj *zobj, double min, double max, double lon, double lat, double radius, geoArray *ga) {
|
2015-06-22 12:08:06 -04:00
|
|
|
/* minex 0 = include min in range; maxex 1 = exclude max in range */
|
|
|
|
/* That's: min <= val < max */
|
|
|
|
zrangespec range = { .min = min, .max = max, .minex = 0, .maxex = 1 };
|
|
|
|
size_t origincount = ga->used;
|
|
|
|
sds member;
|
|
|
|
|
2015-07-26 09:28:00 -04:00
|
|
|
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
|
2015-06-22 12:08:06 -04:00
|
|
|
unsigned char *zl = zobj->ptr;
|
|
|
|
unsigned char *eptr, *sptr;
|
|
|
|
unsigned char *vstr = NULL;
|
|
|
|
unsigned int vlen = 0;
|
|
|
|
long long vlong = 0;
|
|
|
|
double score = 0;
|
|
|
|
|
|
|
|
if ((eptr = zzlFirstInRange(zl, &range)) == NULL) {
|
|
|
|
/* Nothing exists starting at our min. No results. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr = ziplistNext(zl, eptr);
|
|
|
|
while (eptr) {
|
|
|
|
score = zzlGetScore(sptr);
|
|
|
|
|
|
|
|
/* If we fell out of range, break. */
|
|
|
|
if (!zslValueLteMax(score, &range))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We know the element exists. ziplistGet should always succeed */
|
|
|
|
ziplistGet(eptr, &vstr, &vlen, &vlong);
|
|
|
|
member = (vstr == NULL) ? sdsfromlonglong(vlong) :
|
|
|
|
sdsnewlen(vstr,vlen);
|
2015-06-25 12:05:45 -04:00
|
|
|
if (geoAppendIfWithinRadius(ga,lon,lat,radius,score,member)
|
2015-07-26 17:17:55 -04:00
|
|
|
== C_ERR) sdsfree(member);
|
2015-06-22 12:08:06 -04:00
|
|
|
zzlNext(zl, &eptr, &sptr);
|
|
|
|
}
|
2015-07-26 09:28:00 -04:00
|
|
|
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
|
2015-06-22 12:08:06 -04:00
|
|
|
zset *zs = zobj->ptr;
|
|
|
|
zskiplist *zsl = zs->zsl;
|
|
|
|
zskiplistNode *ln;
|
|
|
|
|
|
|
|
if ((ln = zslFirstInRange(zsl, &range)) == NULL) {
|
|
|
|
/* Nothing exists starting at our min. No results. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ln) {
|
2015-08-04 03:20:55 -04:00
|
|
|
sds ele = ln->ele;
|
2015-06-22 12:08:06 -04:00
|
|
|
/* Abort when the node is no longer in range. */
|
|
|
|
if (!zslValueLteMax(ln->score, &range))
|
|
|
|
break;
|
|
|
|
|
2015-08-04 03:20:55 -04:00
|
|
|
ele = sdsdup(ele);
|
|
|
|
if (geoAppendIfWithinRadius(ga,lon,lat,radius,ln->score,ele)
|
|
|
|
== C_ERR) sdsfree(ele);
|
2015-06-22 12:08:06 -04:00
|
|
|
ln = ln->level[0].forward;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ga->used - origincount;
|
|
|
|
}
|
|
|
|
|
2015-06-29 03:34:05 -04:00
|
|
|
/* Compute the sorted set scores min (inclusive), max (exclusive) we should
|
|
|
|
* query in order to retrieve all the elements inside the specified area
|
|
|
|
* 'hash'. The two scores are returned by reference in *min and *max. */
|
|
|
|
void scoresOfGeoHashBox(GeoHashBits hash, GeoHashFix52Bits *min, GeoHashFix52Bits *max) {
|
2015-06-24 11:37:20 -04:00
|
|
|
/* We want to compute the sorted set scores that will include all the
|
|
|
|
* elements inside the specified Geohash 'hash', which has as many
|
|
|
|
* bits as specified by hash.step * 2.
|
|
|
|
*
|
|
|
|
* So if step is, for example, 3, and the hash value in binary
|
|
|
|
* is 101010, since our score is 52 bits we want every element which
|
|
|
|
* is in binary: 101010?????????????????????????????????????????????
|
|
|
|
* Where ? can be 0 or 1.
|
|
|
|
*
|
|
|
|
* To get the min score we just use the initial hash value left
|
|
|
|
* shifted enough to get the 52 bit value. Later we increment the
|
|
|
|
* 6 bit prefis (see the hash.bits++ statement), and get the new
|
|
|
|
* prefix: 101011, which we align again to 52 bits to get the maximum
|
|
|
|
* value (which is excluded from the search). So we get everything
|
|
|
|
* between the two following scores (represented in binary):
|
|
|
|
*
|
|
|
|
* 1010100000000000000000000000000000000000000000000000 (included)
|
|
|
|
* and
|
|
|
|
* 1010110000000000000000000000000000000000000000000000 (excluded).
|
|
|
|
*/
|
2015-06-29 03:34:05 -04:00
|
|
|
*min = geohashAlign52Bits(hash);
|
2014-05-12 14:38:17 -04:00
|
|
|
hash.bits++;
|
2015-06-29 03:34:05 -04:00
|
|
|
*max = geohashAlign52Bits(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain all members between the min/max of this geohash bounding box.
|
|
|
|
* Populate a geoArray of GeoPoints by calling geoGetPointsInRange().
|
|
|
|
* Return the number of points added to the array. */
|
|
|
|
int membersOfGeoHashBox(robj *zobj, GeoHashBits hash, geoArray *ga, double lon, double lat, double radius) {
|
|
|
|
GeoHashFix52Bits min, max;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-06-29 03:34:05 -04:00
|
|
|
scoresOfGeoHashBox(hash,&min,&max);
|
2015-06-25 12:05:45 -04:00
|
|
|
return geoGetPointsInRange(zobj, min, max, lon, lat, radius, ga);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search all eight neighbors + self geohash box */
|
2015-06-25 12:05:45 -04:00
|
|
|
int membersOfAllNeighbors(robj *zobj, GeoHashRadius n, double lon, double lat, double radius, geoArray *ga) {
|
2014-05-12 14:38:17 -04:00
|
|
|
GeoHashBits neighbors[9];
|
2015-09-14 17:05:40 -04:00
|
|
|
unsigned int i, count = 0, last_processed = 0;
|
2016-12-05 08:02:32 -05:00
|
|
|
int debugmsg = 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
neighbors[0] = n.hash;
|
|
|
|
neighbors[1] = n.neighbors.north;
|
|
|
|
neighbors[2] = n.neighbors.south;
|
|
|
|
neighbors[3] = n.neighbors.east;
|
|
|
|
neighbors[4] = n.neighbors.west;
|
|
|
|
neighbors[5] = n.neighbors.north_east;
|
|
|
|
neighbors[6] = n.neighbors.north_west;
|
|
|
|
neighbors[7] = n.neighbors.south_east;
|
|
|
|
neighbors[8] = n.neighbors.south_west;
|
|
|
|
|
|
|
|
/* For each neighbor (*and* our own hashbox), get all the matching
|
|
|
|
* members and add them to the potential result list. */
|
2015-06-22 05:24:58 -04:00
|
|
|
for (i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) {
|
2016-12-05 08:02:32 -05:00
|
|
|
if (HASHISZERO(neighbors[i])) {
|
|
|
|
if (debugmsg) D("neighbors[%d] is zero",i);
|
2014-05-12 14:38:17 -04:00
|
|
|
continue;
|
2016-12-05 08:02:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Debugging info. */
|
|
|
|
if (debugmsg) {
|
|
|
|
GeoHashRange long_range, lat_range;
|
|
|
|
geohashGetCoordRange(&long_range,&lat_range);
|
|
|
|
GeoHashArea myarea = {{0}};
|
|
|
|
geohashDecode(long_range, lat_range, neighbors[i], &myarea);
|
|
|
|
|
|
|
|
/* Dump center square. */
|
|
|
|
D("neighbors[%d]:\n",i);
|
|
|
|
D("area.longitude.min: %f\n", myarea.longitude.min);
|
|
|
|
D("area.longitude.max: %f\n", myarea.longitude.max);
|
|
|
|
D("area.latitude.min: %f\n", myarea.latitude.min);
|
|
|
|
D("area.latitude.max: %f\n", myarea.latitude.max);
|
|
|
|
D("\n");
|
|
|
|
}
|
2015-09-14 17:05:40 -04:00
|
|
|
|
|
|
|
/* When a huge Radius (in the 5000 km range or more) is used,
|
|
|
|
* adjacent neighbors can be the same, leading to duplicated
|
|
|
|
* elements. Skip every range which is the same as the one
|
|
|
|
* processed previously. */
|
|
|
|
if (last_processed &&
|
|
|
|
neighbors[i].bits == neighbors[last_processed].bits &&
|
|
|
|
neighbors[i].step == neighbors[last_processed].step)
|
2016-12-05 08:02:32 -05:00
|
|
|
{
|
|
|
|
if (debugmsg)
|
|
|
|
D("Skipping processing of %d, same as previous\n",i);
|
2015-09-14 17:05:40 -04:00
|
|
|
continue;
|
2016-12-05 08:02:32 -05:00
|
|
|
}
|
2015-06-25 12:05:45 -04:00
|
|
|
count += membersOfGeoHashBox(zobj, neighbors[i], ga, lon, lat, radius);
|
2015-09-14 17:05:40 -04:00
|
|
|
last_processed = i;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
2015-06-22 12:08:06 -04:00
|
|
|
return count;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort comparators for qsort() */
|
|
|
|
static int sort_gp_asc(const void *a, const void *b) {
|
2015-06-22 05:53:14 -04:00
|
|
|
const struct geoPoint *gpa = a, *gpb = b;
|
2014-05-12 14:38:17 -04:00
|
|
|
/* We can't do adist - bdist because they are doubles and
|
|
|
|
* the comparator returns an int. */
|
|
|
|
if (gpa->dist > gpb->dist)
|
|
|
|
return 1;
|
|
|
|
else if (gpa->dist == gpb->dist)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sort_gp_desc(const void *a, const void *b) {
|
|
|
|
return -sort_gp_asc(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
* Commands
|
|
|
|
* ==================================================================== */
|
|
|
|
|
2015-06-29 03:16:27 -04:00
|
|
|
/* GEOADD key long lat name [long2 lat2 name2 ... longN latN nameN] */
|
2015-07-26 09:20:46 -04:00
|
|
|
void geoaddCommand(client *c) {
|
2015-06-29 03:20:07 -04:00
|
|
|
/* Check arguments number for sanity. */
|
|
|
|
if ((c->argc - 2) % 3 != 0) {
|
2014-05-12 14:38:17 -04:00
|
|
|
/* Need an odd number of arguments if we got this far... */
|
2015-06-29 03:20:07 -04:00
|
|
|
addReplyError(c, "syntax error. Try GEOADD key [x1] [y1] [name1] "
|
|
|
|
"[x2] [y2] [name2] ... ");
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int elements = (c->argc - 2) / 3;
|
2015-06-23 04:18:23 -04:00
|
|
|
int argc = 2+elements*2; /* ZADD key score ele ... */
|
|
|
|
robj **argv = zcalloc(argc*sizeof(robj*));
|
|
|
|
argv[0] = createRawStringObject("zadd",4);
|
|
|
|
argv[1] = c->argv[1]; /* key */
|
|
|
|
incrRefCount(argv[1]);
|
|
|
|
|
|
|
|
/* Create the argument vector to call ZADD in order to add all
|
|
|
|
* the score,value pairs to the requested zset, where score is actually
|
|
|
|
* an encoded version of lat,long. */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < elements; i++) {
|
2015-06-25 12:05:45 -04:00
|
|
|
double xy[2];
|
2015-06-23 04:18:23 -04:00
|
|
|
|
2015-07-26 17:17:55 -04:00
|
|
|
if (extractLongLatOrReply(c, (c->argv+2)+(i*3),xy) == C_ERR) {
|
2015-06-23 04:18:23 -04:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if (argv[i]) decrRefCount(argv[i]);
|
|
|
|
zfree(argv);
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
2015-06-23 04:18:23 -04:00
|
|
|
}
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-06-23 04:18:23 -04:00
|
|
|
/* Turn the coordinates into the score of the element. */
|
2014-05-12 14:38:17 -04:00
|
|
|
GeoHashBits hash;
|
2015-06-29 03:20:07 -04:00
|
|
|
geohashEncodeWGS84(xy[0], xy[1], GEO_STEP_MAX, &hash);
|
2014-05-12 14:38:17 -04:00
|
|
|
GeoHashFix52Bits bits = geohashAlign52Bits(hash);
|
2015-07-26 09:28:00 -04:00
|
|
|
robj *score = createObject(OBJ_STRING, sdsfromlonglong(bits));
|
2014-05-12 14:38:17 -04:00
|
|
|
robj *val = c->argv[2 + i * 3 + 2];
|
2015-06-23 04:18:23 -04:00
|
|
|
argv[2+i*2] = score;
|
|
|
|
argv[3+i*2] = val;
|
|
|
|
incrRefCount(val);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-23 04:18:23 -04:00
|
|
|
/* Finally call ZADD that will do the work for us. */
|
|
|
|
replaceClientCommandVector(c,argc,argv);
|
|
|
|
zaddCommand(c);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SORT_NONE 0
|
|
|
|
#define SORT_ASC 1
|
|
|
|
#define SORT_DESC 2
|
|
|
|
|
2017-06-30 04:03:37 -04:00
|
|
|
#define RADIUS_COORDS (1<<0) /* Search around coordinates. */
|
|
|
|
#define RADIUS_MEMBER (1<<1) /* Search around member. */
|
|
|
|
#define RADIUS_NOSTORE (1<<2) /* Do not acceot STORE/STOREDIST option. */
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-06-27 03:38:39 -04:00
|
|
|
/* GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC]
|
2016-02-18 04:24:11 -05:00
|
|
|
* [COUNT count] [STORE key] [STOREDIST key]
|
2015-06-27 03:38:39 -04:00
|
|
|
* GEORADIUSBYMEMBER key member radius unit ... options ... */
|
2017-06-30 04:03:37 -04:00
|
|
|
void georadiusGeneric(client *c, int flags) {
|
2014-05-12 14:38:17 -04:00
|
|
|
robj *key = c->argv[1];
|
2016-02-18 04:24:11 -05:00
|
|
|
robj *storekey = NULL;
|
|
|
|
int storedist = 0; /* 0 for STORE, 1 for STOREDIST. */
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
/* Look up the requested zset */
|
|
|
|
robj *zobj = NULL;
|
2019-10-01 04:38:56 -04:00
|
|
|
if ((zobj = lookupKeyReadOrReply(c, key, shared.emptyarray)) == NULL ||
|
2015-07-26 09:28:00 -04:00
|
|
|
checkType(c, zobj, OBJ_ZSET)) {
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
/* Find long/lat to use for radius search based on inquiry type */
|
2014-05-12 14:38:17 -04:00
|
|
|
int base_args;
|
2015-06-25 12:05:45 -04:00
|
|
|
double xy[2] = { 0 };
|
2017-06-30 04:03:37 -04:00
|
|
|
if (flags & RADIUS_COORDS) {
|
2014-05-12 14:38:17 -04:00
|
|
|
base_args = 6;
|
2015-07-26 17:17:55 -04:00
|
|
|
if (extractLongLatOrReply(c, c->argv + 2, xy) == C_ERR)
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
2017-06-30 04:03:37 -04:00
|
|
|
} else if (flags & RADIUS_MEMBER) {
|
2014-05-12 14:38:17 -04:00
|
|
|
base_args = 5;
|
|
|
|
robj *member = c->argv[2];
|
2015-07-26 17:17:55 -04:00
|
|
|
if (longLatFromMember(zobj, member, xy) == C_ERR) {
|
2014-05-12 14:38:17 -04:00
|
|
|
addReplyError(c, "could not decode requested zset member");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-30 04:03:37 -04:00
|
|
|
addReplyError(c, "Unknown georadius search type");
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract radius and units from arguments */
|
|
|
|
double radius_meters = 0, conversion = 1;
|
|
|
|
if ((radius_meters = extractDistanceOrReply(c, c->argv + base_args - 2,
|
|
|
|
&conversion)) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Discover and populate all optional parameters. */
|
2015-06-27 03:38:39 -04:00
|
|
|
int withdist = 0, withhash = 0, withcoords = 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
int sort = SORT_NONE;
|
2015-06-27 04:23:58 -04:00
|
|
|
long long count = 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
if (c->argc > base_args) {
|
|
|
|
int remaining = c->argc - base_args;
|
|
|
|
for (int i = 0; i < remaining; i++) {
|
|
|
|
char *arg = c->argv[base_args + i]->ptr;
|
2015-06-27 03:43:47 -04:00
|
|
|
if (!strcasecmp(arg, "withdist")) {
|
2015-06-22 05:24:58 -04:00
|
|
|
withdist = 1;
|
2015-06-27 03:43:47 -04:00
|
|
|
} else if (!strcasecmp(arg, "withhash")) {
|
2015-06-22 05:24:58 -04:00
|
|
|
withhash = 1;
|
2015-06-27 03:43:47 -04:00
|
|
|
} else if (!strcasecmp(arg, "withcoord")) {
|
2015-06-22 05:24:58 -04:00
|
|
|
withcoords = 1;
|
2015-06-27 03:43:47 -04:00
|
|
|
} else if (!strcasecmp(arg, "asc")) {
|
2014-05-12 14:38:17 -04:00
|
|
|
sort = SORT_ASC;
|
2015-06-27 03:43:47 -04:00
|
|
|
} else if (!strcasecmp(arg, "desc")) {
|
2014-05-12 14:38:17 -04:00
|
|
|
sort = SORT_DESC;
|
2015-10-06 03:25:28 -04:00
|
|
|
} else if (!strcasecmp(arg, "count") && (i+1) < remaining) {
|
2015-06-27 04:23:58 -04:00
|
|
|
if (getLongLongFromObjectOrReply(c, c->argv[base_args+i+1],
|
2015-07-26 17:17:55 -04:00
|
|
|
&count, NULL) != C_OK) return;
|
2015-06-27 04:23:58 -04:00
|
|
|
if (count <= 0) {
|
|
|
|
addReplyError(c,"COUNT must be > 0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i++;
|
2017-06-30 04:03:37 -04:00
|
|
|
} else if (!strcasecmp(arg, "store") &&
|
|
|
|
(i+1) < remaining &&
|
|
|
|
!(flags & RADIUS_NOSTORE))
|
|
|
|
{
|
2016-02-18 04:24:11 -05:00
|
|
|
storekey = c->argv[base_args+i+1];
|
|
|
|
storedist = 0;
|
|
|
|
i++;
|
2017-06-30 04:03:37 -04:00
|
|
|
} else if (!strcasecmp(arg, "storedist") &&
|
|
|
|
(i+1) < remaining &&
|
|
|
|
!(flags & RADIUS_NOSTORE))
|
|
|
|
{
|
2016-02-18 04:24:11 -05:00
|
|
|
storekey = c->argv[base_args+i+1];
|
|
|
|
storedist = 1;
|
|
|
|
i++;
|
2015-06-27 03:43:47 -04:00
|
|
|
} else {
|
2014-05-12 14:38:17 -04:00
|
|
|
addReply(c, shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 04:24:11 -05:00
|
|
|
/* Trap options not compatible with STORE and STOREDIST. */
|
|
|
|
if (storekey && (withdist || withhash || withcoords)) {
|
|
|
|
addReplyError(c,
|
|
|
|
"STORE option in GEORADIUS is not compatible with "
|
|
|
|
"WITHDIST, WITHHASH and WITHCOORDS options");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-27 04:23:58 -04:00
|
|
|
/* COUNT without ordering does not make much sense, force ASC
|
|
|
|
* ordering if COUNT was specified but no sorting was requested. */
|
|
|
|
if (count != 0 && sort == SORT_NONE) sort = SORT_ASC;
|
|
|
|
|
2014-05-12 14:38:17 -04:00
|
|
|
/* Get all neighbor geohash boxes for our radius search */
|
|
|
|
GeoHashRadius georadius =
|
2015-06-25 12:05:45 -04:00
|
|
|
geohashGetAreasByRadiusWGS84(xy[0], xy[1], radius_meters);
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
/* Search the zset for all matching points */
|
2015-06-22 12:08:06 -04:00
|
|
|
geoArray *ga = geoArrayCreate();
|
2015-06-25 12:05:45 -04:00
|
|
|
membersOfAllNeighbors(zobj, georadius, xy[0], xy[1], radius_meters, ga);
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
/* If no matching results, the user gets an empty reply. */
|
2016-02-18 04:24:11 -05:00
|
|
|
if (ga->used == 0 && storekey == NULL) {
|
2019-10-01 13:18:08 -04:00
|
|
|
addReply(c,shared.emptyarray);
|
2015-06-22 12:08:06 -04:00
|
|
|
geoArrayFree(ga);
|
2014-05-12 14:38:17 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-22 12:08:06 -04:00
|
|
|
long result_length = ga->used;
|
2016-02-18 04:24:11 -05:00
|
|
|
long returned_items = (count == 0 || result_length < count) ?
|
|
|
|
result_length : count;
|
2014-05-12 14:38:17 -04:00
|
|
|
long option_length = 0;
|
|
|
|
|
|
|
|
/* Process [optional] requested sorting */
|
|
|
|
if (sort == SORT_ASC) {
|
2015-06-22 12:08:06 -04:00
|
|
|
qsort(ga->array, result_length, sizeof(geoPoint), sort_gp_asc);
|
2014-05-12 14:38:17 -04:00
|
|
|
} else if (sort == SORT_DESC) {
|
2015-06-22 12:08:06 -04:00
|
|
|
qsort(ga->array, result_length, sizeof(geoPoint), sort_gp_desc);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 04:24:11 -05:00
|
|
|
if (storekey == NULL) {
|
|
|
|
/* No target key, return results to user. */
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2016-02-18 04:24:11 -05:00
|
|
|
/* Our options are self-contained nested multibulk replies, so we
|
|
|
|
* only need to track how many of those nested replies we return. */
|
2014-05-12 14:38:17 -04:00
|
|
|
if (withdist)
|
2016-02-18 04:24:11 -05:00
|
|
|
option_length++;
|
|
|
|
|
|
|
|
if (withcoords)
|
|
|
|
option_length++;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
if (withhash)
|
2016-02-18 04:24:11 -05:00
|
|
|
option_length++;
|
|
|
|
|
2018-11-30 05:07:07 -05:00
|
|
|
/* The array len we send is exactly result_length. The result is
|
2016-02-18 04:24:11 -05:00
|
|
|
* either all strings of just zset members *or* a nested multi-bulk
|
|
|
|
* reply containing the zset member string _and_ all the additional
|
|
|
|
* options the user enabled for this request. */
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c, returned_items);
|
2016-02-18 04:24:11 -05:00
|
|
|
|
|
|
|
/* Finally send results back to the caller */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < returned_items; i++) {
|
|
|
|
geoPoint *gp = ga->array+i;
|
|
|
|
gp->dist /= conversion; /* Fix according to unit. */
|
|
|
|
|
|
|
|
/* If we have options in option_length, return each sub-result
|
|
|
|
* as a nested multi-bulk. Add 1 to account for result value
|
|
|
|
* itself. */
|
|
|
|
if (option_length)
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c, option_length + 1);
|
2016-02-18 04:24:11 -05:00
|
|
|
|
|
|
|
addReplyBulkSds(c,gp->member);
|
|
|
|
gp->member = NULL;
|
|
|
|
|
|
|
|
if (withdist)
|
|
|
|
addReplyDoubleDistance(c, gp->dist);
|
|
|
|
|
|
|
|
if (withhash)
|
|
|
|
addReplyLongLong(c, gp->score);
|
|
|
|
|
|
|
|
if (withcoords) {
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c, 2);
|
2016-02-18 18:11:30 -05:00
|
|
|
addReplyHumanLongDouble(c, gp->longitude);
|
|
|
|
addReplyHumanLongDouble(c, gp->latitude);
|
2016-02-18 04:24:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Target key, create a sorted set with the results. */
|
|
|
|
robj *zobj;
|
|
|
|
zset *zs;
|
|
|
|
int i;
|
|
|
|
size_t maxelelen = 0;
|
|
|
|
|
|
|
|
if (returned_items) {
|
|
|
|
zobj = createZsetObject();
|
|
|
|
zs = zobj->ptr;
|
|
|
|
}
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2016-02-18 04:24:11 -05:00
|
|
|
for (i = 0; i < returned_items; i++) {
|
|
|
|
zskiplistNode *znode;
|
|
|
|
geoPoint *gp = ga->array+i;
|
|
|
|
gp->dist /= conversion; /* Fix according to unit. */
|
|
|
|
double score = storedist ? gp->dist : gp->score;
|
|
|
|
size_t elelen = sdslen(gp->member);
|
|
|
|
|
|
|
|
if (maxelelen < elelen) maxelelen = elelen;
|
|
|
|
znode = zslInsert(zs->zsl,score,gp->member);
|
|
|
|
serverAssert(dictAdd(zs->dict,gp->member,&znode->score) == DICT_OK);
|
|
|
|
gp->member = NULL;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
2015-06-27 04:23:58 -04:00
|
|
|
|
2016-02-18 04:24:11 -05:00
|
|
|
if (returned_items) {
|
|
|
|
zsetConvertToZiplistIfNeeded(zobj,maxelelen);
|
2019-12-18 05:58:02 -05:00
|
|
|
setKey(c->db,storekey,zobj);
|
2016-02-18 04:24:11 -05:00
|
|
|
decrRefCount(zobj);
|
2019-03-14 07:11:16 -04:00
|
|
|
notifyKeyspaceEvent(NOTIFY_ZSET,"georadiusstore",storekey,
|
2016-02-18 04:24:11 -05:00
|
|
|
c->db->id);
|
|
|
|
server.dirty += returned_items;
|
|
|
|
} else if (dbDelete(c->db,storekey)) {
|
|
|
|
signalModifiedKey(c->db,storekey);
|
|
|
|
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",storekey,c->db->id);
|
|
|
|
server.dirty++;
|
|
|
|
}
|
|
|
|
addReplyLongLong(c, returned_items);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
2015-06-22 12:08:06 -04:00
|
|
|
geoArrayFree(ga);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 03:16:27 -04:00
|
|
|
/* GEORADIUS wrapper function. */
|
2015-07-26 09:20:46 -04:00
|
|
|
void georadiusCommand(client *c) {
|
2015-06-29 06:07:18 -04:00
|
|
|
georadiusGeneric(c, RADIUS_COORDS);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 03:16:27 -04:00
|
|
|
/* GEORADIUSBYMEMBER wrapper function. */
|
2017-06-30 04:03:37 -04:00
|
|
|
void georadiusbymemberCommand(client *c) {
|
2015-06-29 06:07:18 -04:00
|
|
|
georadiusGeneric(c, RADIUS_MEMBER);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2017-06-30 04:03:37 -04:00
|
|
|
/* GEORADIUS_RO wrapper function. */
|
|
|
|
void georadiusroCommand(client *c) {
|
|
|
|
georadiusGeneric(c, RADIUS_COORDS|RADIUS_NOSTORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GEORADIUSBYMEMBER_RO wrapper function. */
|
|
|
|
void georadiusbymemberroCommand(client *c) {
|
|
|
|
georadiusGeneric(c, RADIUS_MEMBER|RADIUS_NOSTORE);
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:31:14 -04:00
|
|
|
/* GEOHASH key ele1 ele2 ... eleN
|
|
|
|
*
|
|
|
|
* Returns an array with an 11 characters geohash representation of the
|
|
|
|
* position of the specified elements. */
|
2015-07-26 09:20:46 -04:00
|
|
|
void geohashCommand(client *c) {
|
2015-06-24 10:31:14 -04:00
|
|
|
char *geoalphabet= "0123456789bcdefghjkmnpqrstuvwxyz";
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Look up the requested zset */
|
2016-12-20 04:19:06 -05:00
|
|
|
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
|
|
|
if (zobj && checkType(c, zobj, OBJ_ZSET)) return;
|
2015-06-24 10:31:14 -04:00
|
|
|
|
|
|
|
/* Geohash elements one after the other, using a null bulk reply for
|
|
|
|
* missing elements. */
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c,c->argc-2);
|
2015-06-24 10:31:14 -04:00
|
|
|
for (j = 2; j < c->argc; j++) {
|
|
|
|
double score;
|
2016-12-20 04:19:06 -05:00
|
|
|
if (!zobj || zsetScore(zobj, c->argv[j]->ptr, &score) == C_ERR) {
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyNull(c);
|
2015-06-24 10:31:14 -04:00
|
|
|
} else {
|
|
|
|
/* The internal format we use for geocoding is a bit different
|
|
|
|
* than the standard, since we use as initial latitude range
|
|
|
|
* -85,85, while the normal geohashing algorithm uses -90,90.
|
|
|
|
* So we have to decode our position and re-encode using the
|
|
|
|
* standard ranges in order to output a valid geohash string. */
|
|
|
|
|
|
|
|
/* Decode... */
|
2015-06-25 12:05:45 -04:00
|
|
|
double xy[2];
|
|
|
|
if (!decodeGeohash(score,xy)) {
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyNull(c);
|
2015-06-24 10:31:14 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-encode */
|
|
|
|
GeoHashRange r[2];
|
|
|
|
GeoHashBits hash;
|
2015-06-25 12:05:45 -04:00
|
|
|
r[0].min = -180;
|
|
|
|
r[0].max = 180;
|
|
|
|
r[1].min = -90;
|
|
|
|
r[1].max = 90;
|
|
|
|
geohashEncode(&r[0],&r[1],xy[0],xy[1],26,&hash);
|
2015-06-24 10:31:14 -04:00
|
|
|
|
2019-12-18 06:54:46 -05:00
|
|
|
char buf[12];
|
2015-06-24 10:31:14 -04:00
|
|
|
int i;
|
2019-12-18 06:54:46 -05:00
|
|
|
for (i = 0; i < 11; i++) {
|
2015-06-24 10:31:14 -04:00
|
|
|
int idx = (hash.bits >> (52-((i+1)*5))) & 0x1f;
|
|
|
|
buf[i] = geoalphabet[idx];
|
|
|
|
}
|
2019-12-18 06:54:46 -05:00
|
|
|
buf[11] = '\0';
|
|
|
|
addReplyBulkCBuffer(c,buf,11);
|
2015-06-24 10:31:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 04:47:07 -04:00
|
|
|
|
|
|
|
/* GEOPOS key ele1 ele2 ... eleN
|
|
|
|
*
|
|
|
|
* Returns an array of two-items arrays representing the x,y position of each
|
|
|
|
* element specified in the arguments. For missing elements NULL is returned. */
|
2015-07-26 09:20:46 -04:00
|
|
|
void geoposCommand(client *c) {
|
2015-06-29 04:47:07 -04:00
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Look up the requested zset */
|
2016-12-20 04:12:38 -05:00
|
|
|
robj *zobj = lookupKeyRead(c->db, c->argv[1]);
|
|
|
|
if (zobj && checkType(c, zobj, OBJ_ZSET)) return;
|
2015-06-29 04:47:07 -04:00
|
|
|
|
|
|
|
/* Report elements one after the other, using a null bulk reply for
|
|
|
|
* missing elements. */
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c,c->argc-2);
|
2015-06-29 04:47:07 -04:00
|
|
|
for (j = 2; j < c->argc; j++) {
|
|
|
|
double score;
|
2016-12-20 04:12:38 -05:00
|
|
|
if (!zobj || zsetScore(zobj, c->argv[j]->ptr, &score) == C_ERR) {
|
2018-11-30 10:36:55 -05:00
|
|
|
addReplyNullArray(c);
|
2015-06-29 04:47:07 -04:00
|
|
|
} else {
|
|
|
|
/* Decode... */
|
|
|
|
double xy[2];
|
|
|
|
if (!decodeGeohash(score,xy)) {
|
2018-11-30 10:36:55 -05:00
|
|
|
addReplyNullArray(c);
|
2015-06-29 04:47:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyArrayLen(c,2);
|
2016-02-18 18:11:30 -05:00
|
|
|
addReplyHumanLongDouble(c,xy[0]);
|
|
|
|
addReplyHumanLongDouble(c,xy[1]);
|
2015-06-29 04:47:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 06:44:31 -04:00
|
|
|
|
|
|
|
/* GEODIST key ele1 ele2 [unit]
|
|
|
|
*
|
|
|
|
* Return the distance, in meters by default, otherwise accordig to "unit",
|
|
|
|
* between points ele1 and ele2. If one or more elements are missing NULL
|
|
|
|
* is returned. */
|
2015-07-26 09:20:46 -04:00
|
|
|
void geodistCommand(client *c) {
|
2015-06-29 06:44:31 -04:00
|
|
|
double to_meter = 1;
|
|
|
|
|
|
|
|
/* Check if there is the unit to extract, otherwise assume meters. */
|
|
|
|
if (c->argc == 5) {
|
|
|
|
to_meter = extractUnitOrReply(c,c->argv[4]);
|
|
|
|
if (to_meter < 0) return;
|
|
|
|
} else if (c->argc > 5) {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up the requested zset */
|
|
|
|
robj *zobj = NULL;
|
2018-11-30 05:07:07 -05:00
|
|
|
if ((zobj = lookupKeyReadOrReply(c, c->argv[1], shared.null[c->resp]))
|
2015-07-26 09:28:00 -04:00
|
|
|
== NULL || checkType(c, zobj, OBJ_ZSET)) return;
|
2015-06-29 06:44:31 -04:00
|
|
|
|
|
|
|
/* Get the scores. We need both otherwise NULL is returned. */
|
|
|
|
double score1, score2, xyxy[4];
|
2015-08-04 03:20:55 -04:00
|
|
|
if (zsetScore(zobj, c->argv[2]->ptr, &score1) == C_ERR ||
|
|
|
|
zsetScore(zobj, c->argv[3]->ptr, &score2) == C_ERR)
|
2015-06-29 06:44:31 -04:00
|
|
|
{
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyNull(c);
|
2015-06-29 06:44:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode & compute the distance. */
|
|
|
|
if (!decodeGeohash(score1,xyxy) || !decodeGeohash(score2,xyxy+2))
|
2018-11-30 05:07:07 -05:00
|
|
|
addReplyNull(c);
|
2015-06-29 06:44:31 -04:00
|
|
|
else
|
2016-02-18 18:00:39 -05:00
|
|
|
addReplyDoubleDistance(c,
|
2015-06-29 06:44:31 -04:00
|
|
|
geohashGetDistance(xyxy[0],xyxy[1],xyxy[2],xyxy[3]) / to_meter);
|
|
|
|
}
|