From 697cc0402aed2d04baa8b167c20dfe9cf2563d63 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 25 Jan 2021 10:47:58 +0300 Subject: [PATCH] Feature - add addIfExists() method to RGeo object #3380 --- .../main/java/org/redisson/RedissonGeo.java | 30 ++++++++++++++++++- .../src/main/java/org/redisson/api/RGeo.java | 24 ++++++++++++++- .../main/java/org/redisson/api/RGeoAsync.java | 22 ++++++++++++++ .../java/org/redisson/api/RGeoReactive.java | 22 ++++++++++++++ .../main/java/org/redisson/api/RGeoRx.java | 22 ++++++++++++++ 5 files changed, 118 insertions(+), 2 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonGeo.java b/redisson/src/main/java/org/redisson/RedissonGeo.java index adfc0bdd7..dfb2fc37b 100644 --- a/redisson/src/main/java/org/redisson/RedissonGeo.java +++ b/redisson/src/main/java/org/redisson/RedissonGeo.java @@ -100,8 +100,15 @@ public class RedissonGeo extends RedissonScoredSortedSet implements RGeo addAsync(GeoEntry... entries) { - List params = new ArrayList(entries.length + 1); + return addAsync("", entries); + } + + private RFuture addAsync(String subCommand, GeoEntry... entries) { + List params = new ArrayList(entries.length + 2); params.add(getName()); + if (!subCommand.isEmpty()) { + params.add(subCommand); + } for (GeoEntry entry : entries) { params.add(entry.getLongitude()); params.add(entry.getLatitude()); @@ -110,6 +117,27 @@ public class RedissonGeo extends RedissonScoredSortedSet implements RGeo addIfExistsAsync(double longitude, double latitude, V member) { + return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEOADD, getName(), "XX", convert(longitude), + convert(latitude), encode(member)); + } + + @Override + public RFuture addIfExistsAsync(GeoEntry... entries) { + return addAsync("XX", entries); + } + @Override public Double dist(V firstMember, V secondMember, GeoUnit geoUnit) { return get(distAsync(firstMember, secondMember, geoUnit)); diff --git a/redisson/src/main/java/org/redisson/api/RGeo.java b/redisson/src/main/java/org/redisson/api/RGeo.java index feb095c67..875a62202 100644 --- a/redisson/src/main/java/org/redisson/api/RGeo.java +++ b/redisson/src/main/java/org/redisson/api/RGeo.java @@ -48,7 +48,29 @@ public interface RGeo extends RScoredSortedSet, RGeoAsync { * the score was updated */ long add(GeoEntry... entries); - + + /** + * Adds geospatial member only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param longitude - longitude of object + * @param latitude - latitude of object + * @param member - object itself + * @return number of elements added to the sorted set + */ + long addIfExists(double longitude, double latitude, V member); + + /** + * Adds geospatial members only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param entries - objects + * @return number of elements added to the sorted set + */ + long addIfExists(GeoEntry... entries); + /** * Returns distance between members in GeoUnit units. * diff --git a/redisson/src/main/java/org/redisson/api/RGeoAsync.java b/redisson/src/main/java/org/redisson/api/RGeoAsync.java index 5f092ce49..ea97b159f 100644 --- a/redisson/src/main/java/org/redisson/api/RGeoAsync.java +++ b/redisson/src/main/java/org/redisson/api/RGeoAsync.java @@ -49,6 +49,28 @@ public interface RGeoAsync extends RScoredSortedSetAsync { */ RFuture addAsync(GeoEntry... entries); + /** + * Adds geospatial member only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param longitude - longitude of object + * @param latitude - latitude of object + * @param member - object itself + * @return number of elements added to the sorted set + */ + RFuture addIfExistsAsync(double longitude, double latitude, V member); + + /** + * Adds geospatial members only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param entries - objects + * @return number of elements added to the sorted set + */ + RFuture addIfExistsAsync(GeoEntry... entries); + /** * Returns distance between members in GeoUnit units. * diff --git a/redisson/src/main/java/org/redisson/api/RGeoReactive.java b/redisson/src/main/java/org/redisson/api/RGeoReactive.java index dc93c6f49..6797f5d02 100644 --- a/redisson/src/main/java/org/redisson/api/RGeoReactive.java +++ b/redisson/src/main/java/org/redisson/api/RGeoReactive.java @@ -51,6 +51,28 @@ public interface RGeoReactive extends RScoredSortedSetReactive { */ Mono add(GeoEntry... entries); + /** + * Adds geospatial member only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param longitude - longitude of object + * @param latitude - latitude of object + * @param member - object itself + * @return number of elements added to the sorted set + */ + Mono addIfExists(double longitude, double latitude, V member); + + /** + * Adds geospatial members only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param entries - objects + * @return number of elements added to the sorted set + */ + Mono addIfExists(GeoEntry... entries); + /** * Returns distance between members in GeoUnit units. * diff --git a/redisson/src/main/java/org/redisson/api/RGeoRx.java b/redisson/src/main/java/org/redisson/api/RGeoRx.java index 3e03343d1..928caadab 100644 --- a/redisson/src/main/java/org/redisson/api/RGeoRx.java +++ b/redisson/src/main/java/org/redisson/api/RGeoRx.java @@ -51,6 +51,28 @@ public interface RGeoRx extends RScoredSortedSetRx { */ Single add(GeoEntry... entries); + /** + * Adds geospatial member only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param longitude - longitude of object + * @param latitude - latitude of object + * @param member - object itself + * @return number of elements added to the sorted set + */ + Single addIfExists(double longitude, double latitude, V member); + + /** + * Adds geospatial members only if it's already exists. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param entries - objects + * @return number of elements added to the sorted set + */ + Single addIfExists(GeoEntry... entries); + /** * Returns distance between members in GeoUnit units. *