Geo: GEOENCODE now returns score ranges.

If GEOENCODE must be our door to enter the Geocoding implementation
details and do fancy things client side, than return the scores as well
so that we can query the sorted sets directly if we wish to do the same
search multiple times, or want to compute the boxes in the client side
to refine our search needs.
This commit is contained in:
antirez 2015-06-29 09:34:05 +02:00
parent 1884bff12d
commit 6a8e108e2d

View File

@ -263,12 +263,10 @@ int geoGetPointsInRange(robj *zobj, double min, double max, double lon, double l
return ga->used - origincount; return ga->used - origincount;
} }
/* Obtain all members between the min/max of this geohash bounding box. /* Compute the sorted set scores min (inclusive), max (exclusive) we should
* Populate a geoArray of GeoPoints by calling geoGetPointsInRange(). * query in order to retrieve all the elements inside the specified area
* Return the number of points added to the array. */ * 'hash'. The two scores are returned by reference in *min and *max. */
int membersOfGeoHashBox(robj *zobj, GeoHashBits hash, geoArray *ga, double lon, double lat, double radius) { void scoresOfGeoHashBox(GeoHashBits hash, GeoHashFix52Bits *min, GeoHashFix52Bits *max) {
GeoHashFix52Bits min, max;
/* We want to compute the sorted set scores that will include all the /* We want to compute the sorted set scores that will include all the
* elements inside the specified Geohash 'hash', which has as many * elements inside the specified Geohash 'hash', which has as many
* bits as specified by hash.step * 2. * bits as specified by hash.step * 2.
@ -289,10 +287,18 @@ int membersOfGeoHashBox(robj *zobj, GeoHashBits hash, geoArray *ga, double lon,
* and * and
* 1010110000000000000000000000000000000000000000000000 (excluded). * 1010110000000000000000000000000000000000000000000000 (excluded).
*/ */
min = geohashAlign52Bits(hash); *min = geohashAlign52Bits(hash);
hash.bits++; hash.bits++;
max = geohashAlign52Bits(hash); *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;
scoresOfGeoHashBox(hash,&min,&max);
return geoGetPointsInRange(zobj, min, max, lon, lat, radius, ga); return geoGetPointsInRange(zobj, min, max, lon, lat, radius, ga);
} }
@ -621,7 +627,7 @@ void geoEncodeCommand(redisClient *c) {
double lat = (area.latitude.min + area.latitude.max) / 2; double lat = (area.latitude.min + area.latitude.max) / 2;
/* Return four nested multibulk replies. */ /* Return four nested multibulk replies. */
addReplyMultiBulkLen(c, 4); addReplyMultiBulkLen(c, 5);
/* Return the binary geohash we calculated as 52-bit integer */ /* Return the binary geohash we calculated as 52-bit integer */
addReplyLongLong(c, bits); addReplyLongLong(c, bits);
@ -640,6 +646,13 @@ void geoEncodeCommand(redisClient *c) {
addReplyMultiBulkLen(c, 2); addReplyMultiBulkLen(c, 2);
addReplyDouble(c, lon); addReplyDouble(c, lon);
addReplyDouble(c, lat); addReplyDouble(c, lat);
/* Return the two scores to query to get the range from the sorted set. */
GeoHashFix52Bits min, max;
scoresOfGeoHashBox(geohash,&min,&max);
addReplyMultiBulkLen(c, 2);
addReplyDouble(c, min);
addReplyDouble(c, max);
} }
/* GEOHASH key ele1 ele2 ... eleN /* GEOHASH key ele1 ele2 ... eleN