Skip to content

Commit

Permalink
(#2119) Implements GeoRadiusParam.withHash()
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 committed Feb 21, 2020
1 parent 528debd commit 6de5033
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,13 @@ public List<GeoRadiusResponse> build(Object data) {
Object info = informations.get(idx);
if (info instanceof List<?>) {
// coordinate
List<Object> coord = (List<Object>) info;
List<Object> coord = (List<Object>)info;

resp.setCoordinate(new GeoCoordinate(DOUBLE.build(coord.get(0)),
DOUBLE.build(coord.get(1))));
} else if (info instanceof Long) {
// score
resp.setScore(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 score;

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 getScore() {
return score;
}

public void setScore(long score) {
this.score = score;
}
}
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("3479447370796909", Long.toString(response.getScore()));

// 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("3479447370796909", Long.toString(response.getScore()));
}

@Test
Expand Down

0 comments on commit 6de5033

Please sign in to comment.