Skip to content

Commit

Permalink
#2119 Implements GeoRadiusParam.withHash() (#2137)
Browse files Browse the repository at this point in the history
In Redis source https://github.com/antirez/redis/blob/5a72c5058c27cdc778cde8f61d16691b11a6adc5/src/geo.c#L624-L62 :

    if (withhash)
        addReplyLongLong(c, gp->score);

So the score type is long.

Co-authored-by: M Sazzadul Hoque <sazzad16@users.noreply.github.com>
  • Loading branch information
yangbodong22011 and sazzad16 authored Mar 14, 2020
1 parent 5089d96 commit 3289d69
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ public List<GeoRadiusResponse> build(Object data) {

resp.setCoordinate(new GeoCoordinate(DOUBLE.build(coord.get(0)),
DOUBLE.build(coord.get(1))));
} else if (info instanceof Long) {
// score
resp.setRawScore(LONG.build(info));
} else {
// distance
resp.setDistance(DOUBLE.build(info));
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/GeoRadiusResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class GeoRadiusResponse {
private byte[] member;
private double distance;
private GeoCoordinate coordinate;
private long rawScore;

public GeoRadiusResponse(byte[] member) {
this.member = member;
Expand Down Expand Up @@ -34,4 +35,12 @@ public double getDistance() {
public GeoCoordinate getCoordinate() {
return coordinate;
}

public long getRawScore() {
return rawScore;
}

public void setRawScore(long rawScore) {
this.rawScore = rawScore;
}
}
12 changes: 9 additions & 3 deletions src/main/java/redis/clients/jedis/params/GeoRadiusParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
public class GeoRadiusParam extends Params {
private static final String WITHCOORD = "withcoord";
private static final String WITHDIST = "withdist";

// Do not add WITHHASH since we can't classify result of WITHHASH and WITHDIST,
// and WITHHASH is for debugging purposes
private static final String WITHHASH = "withhash";

private static final String ASC = "asc";
private static final String DESC = "desc";
Expand All @@ -33,6 +31,11 @@ public GeoRadiusParam withDist() {
return this;
}

public GeoRadiusParam withHash() {
addParam(WITHHASH);
return this;
}

public GeoRadiusParam sortAscending() {
addParam(ASC);
return this;
Expand Down Expand Up @@ -62,6 +65,9 @@ public byte[][] getByteParams(byte[]... args) {
if (contains(WITHDIST)) {
byteParams.add(SafeEncoder.encode(WITHDIST));
}
if (contains(WITHHASH)) {
byteParams.add(SafeEncoder.encode(WITHHASH));
}

if (contains(COUNT)) {
byteParams.add(SafeEncoder.encode(COUNT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,20 @@ public void georadius() {

// sort, count 1, withdist, withcoord
members = jedis.georadius("Sicily", 15, 37, 200, GeoUnit.KM, GeoRadiusParam.geoRadiusParam()
.sortAscending().count(1).withCoord().withDist());
.sortAscending().count(1).withCoord().withDist().withHash());
assertEquals(1, members.size());
GeoRadiusResponse response = members.get(0);
assertTrue(equalsWithinEpsilon(56.4413, response.getDistance()));
assertTrue(equalsWithinEpsilon(15.087269, response.getCoordinate().getLongitude()));
assertTrue(equalsWithinEpsilon(37.502669, response.getCoordinate().getLatitude()));
assertEquals(3479447370796909L, response.getRawScore());

// sort, count 1, with hash
members = jedis.georadius("Sicily", 15, 37, 200, GeoUnit.KM, GeoRadiusParam.geoRadiusParam()
.sortAscending().count(1).withHash());
assertEquals(1, members.size());
response = members.get(0);
assertEquals(3479447370796909L, response.getRawScore());
}

@Test
Expand Down

0 comments on commit 3289d69

Please sign in to comment.