Skip to content

Commit

Permalink
With the latest Redis release, the geo commands may return doubles co…
Browse files Browse the repository at this point in the history
…ntaining un-parseable characters.

This is due to an optimization (https://github.com/redis/redis/pull/11552/files) added in the Redis:7 stream.

(cherry picked from commit df5d507)
  • Loading branch information
cescoffier authored and gsmet committed Dec 20, 2022
1 parent 82607ee commit 4636511
Showing 1 changed file with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.OptionalDouble;
import java.util.OptionalLong;
import java.util.Set;
import java.util.regex.Pattern;

import io.quarkus.redis.datasource.codecs.Codec;
import io.quarkus.redis.datasource.codecs.Codecs;
Expand All @@ -34,6 +35,8 @@ class AbstractGeoCommands<K, V> extends AbstractRedisCommands {
protected final Codec<K> keyCodec;
protected final Codec<V> valueCodec;

private static final Pattern NOISE_REMOVER_PATTERN = Pattern.compile("[^a-zA-Z0-9\\.]");

AbstractGeoCommands(RedisCommandExecutor redis, Class<K> k, Class<V> v) {
super(redis, new Marshaller(k, v));
this.typeOfValue = v;
Expand Down Expand Up @@ -233,15 +236,15 @@ Double decodeDistance(Response r) {
if (r == null) {
return null;
}
return r.toDouble();
return parseDouble(r);
}

List<GeoPosition> decodeGeoPositions(Response response) {
return marshaller.decodeAsList(response, nested -> {
if (nested == null) {
return null;
} else {
return GeoPosition.of(nested.get(0).toDouble(), nested.get(1).toDouble());
return GeoPosition.of(parseDouble(nested.get(0)), parseDouble(nested.get(1)));
}
});
}
Expand All @@ -261,33 +264,33 @@ List<GeoValue<V>> decodeAsListOfGeoValues(Response r, boolean withDistance, bool

V member = marshaller.decode(typeOfValue, response.get(0));
if (withCoordinates && withDistance && withHash) {
double dist = response.get(1).toDouble();
double dist = parseDouble(response.get(1));
long hash = response.get(2).toLong();
double longitude = response.get(3).get(0).toDouble();
double latitude = response.get(3).get(1).toDouble();
double longitude = parseDouble(response.get(3).get(0));
double latitude = parseDouble(response.get(3).get(1));
list.add(new GeoValue<>(member, OptionalDouble.of(dist), OptionalLong.of(hash),
OptionalDouble.of(longitude), OptionalDouble.of(latitude)));
} else if (withCoordinates && withDistance) {
double dist = response.get(1).toDouble();
double longitude = response.get(2).get(0).toDouble();
double latitude = response.get(2).get(1).toDouble();
double dist = parseDouble(response.get(1));
double longitude = parseDouble(response.get(2).get(0));
double latitude = parseDouble(response.get(2).get(1));
list.add(new GeoValue<>(member, OptionalDouble.of(dist), OptionalLong.empty(),
OptionalDouble.of(longitude), OptionalDouble.of(latitude)));
} else if (withCoordinates && withHash) {
long hash = response.get(1).toLong();
double longitude = response.get(2).get(0).toDouble();
double latitude = response.get(2).get(1).toDouble();
double longitude = parseDouble(response.get(2).get(0));
double latitude = parseDouble(response.get(2).get(1));
list.add(new GeoValue<>(member, OptionalDouble.empty(), OptionalLong.of(hash),
OptionalDouble.of(longitude), OptionalDouble.of(latitude)));
} else if (withCoordinates) {
// Only coordinates
double longitude = response.get(1).get(0).toDouble();
double latitude = response.get(1).get(1).toDouble();
double longitude = parseDouble(response.get(1).get(0));
double latitude = parseDouble(response.get(1).get(1));
list.add(new GeoValue<>(member, OptionalDouble.empty(), OptionalLong.empty(), OptionalDouble.of(longitude),
OptionalDouble.of(latitude)));
} else if (withDistance && !withHash) {
// Only distance
double dist = response.get(1).toDouble();
double dist = parseDouble(response.get(1));
list.add(new GeoValue<>(member, OptionalDouble.of(dist), OptionalLong.empty(), OptionalDouble.empty(),
OptionalDouble.empty()));
} else if (!withDistance) {
Expand All @@ -297,12 +300,23 @@ List<GeoValue<V>> decodeAsListOfGeoValues(Response r, boolean withDistance, bool
OptionalDouble.empty()));
} else {
// Distance and Hash
double dist = response.get(1).toDouble();
double dist = parseDouble(response.get(1));
long hash = response.get(2).toLong();
list.add(new GeoValue<>(member, OptionalDouble.of(dist), OptionalLong.of(hash),
OptionalDouble.empty(), OptionalDouble.empty()));
}
}
return list;
}

private static double parseDouble(Response response) {
double dist;
try {
dist = response.toDouble();
} catch (NumberFormatException e) {
String s = NOISE_REMOVER_PATTERN.matcher(response.toString()).replaceAll("");
dist = Double.parseDouble(s);
}
return dist;
}
}

0 comments on commit 4636511

Please sign in to comment.