Feature - add tryAdd() method to RGeo object #3381

pull/3384/head
Nikita Koksharov 4 years ago
parent 697cc0402a
commit 6852bd390e

@ -16,10 +16,7 @@
package org.redisson;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import org.redisson.api.GeoEntry;
import org.redisson.api.GeoOrder;
@ -118,7 +115,7 @@ public class RedissonGeo<V> extends RedissonScoredSortedSet<V> implements RGeo<V
}
@Override
public long addIfExists(double longitude, double latitude, V member) {
public Boolean addIfExists(double longitude, double latitude, V member) {
return get(addIfExistsAsync(longitude, latitude, member));
}
@ -128,9 +125,16 @@ public class RedissonGeo<V> extends RedissonScoredSortedSet<V> implements RGeo<V
}
@Override
public RFuture<Long> addIfExistsAsync(double longitude, double latitude, V member) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEOADD, getName(), "XX", convert(longitude),
convert(latitude), encode(member));
public RFuture<Boolean> addIfExistsAsync(double longitude, double latitude, V member) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local value = redis.call('geopos', KEYS[1], ARGV[3]); "
+ "if value[1] ~= false then "
+ "redis.call('geoadd', KEYS[1], ARGV[1], ARGV[2], ARGV[3]); "
+ "return 1; "
+ "end; "
+ "return 0; ",
Collections.singletonList(getName()),
convert(longitude), convert(latitude), encode(member));
}
@Override
@ -138,6 +142,27 @@ public class RedissonGeo<V> extends RedissonScoredSortedSet<V> implements RGeo<V
return addAsync("XX", entries);
}
@Override
public boolean tryAdd(double longitude, double latitude, V member) {
return get(tryAddAsync(longitude, latitude, member));
}
@Override
public long tryAdd(GeoEntry... entries) {
return get(tryAddAsync(entries));
}
@Override
public RFuture<Boolean> tryAddAsync(double longitude, double latitude, V member) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEOADD_BOOLEAN, getName(), "NX", convert(longitude),
convert(latitude), encode(member));
}
@Override
public RFuture<Long> tryAddAsync(GeoEntry... entries) {
return addAsync("NX", entries);
}
@Override
public Double dist(V firstMember, V secondMember, GeoUnit geoUnit) {
return get(distAsync(firstMember, secondMember, geoUnit));

@ -59,7 +59,7 @@ public interface RGeo<V> extends RScoredSortedSet<V>, RGeoAsync<V> {
* @param member - object itself
* @return number of elements added to the sorted set
*/
long addIfExists(double longitude, double latitude, V member);
Boolean addIfExists(double longitude, double latitude, V member);
/**
* Adds geospatial members only if it's already exists.
@ -71,6 +71,28 @@ public interface RGeo<V> extends RScoredSortedSet<V>, RGeoAsync<V> {
*/
long addIfExists(GeoEntry... entries);
/**
* Adds geospatial member only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param longitude - longitude of object
* @param latitude - latitude of object
* @param member - object itself
* @return number of elements added to the sorted set
*/
boolean tryAdd(double longitude, double latitude, V member);
/**
* Adds geospatial members only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param entries - objects
* @return number of elements added to the sorted set
*/
long tryAdd(GeoEntry... entries);
/**
* Returns distance between members in <code>GeoUnit</code> units.
*

@ -59,7 +59,7 @@ public interface RGeoAsync<V> extends RScoredSortedSetAsync<V> {
* @param member - object itself
* @return number of elements added to the sorted set
*/
RFuture<Long> addIfExistsAsync(double longitude, double latitude, V member);
RFuture<Boolean> addIfExistsAsync(double longitude, double latitude, V member);
/**
* Adds geospatial members only if it's already exists.
@ -71,6 +71,28 @@ public interface RGeoAsync<V> extends RScoredSortedSetAsync<V> {
*/
RFuture<Long> addIfExistsAsync(GeoEntry... entries);
/**
* Adds geospatial member only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param longitude - longitude of object
* @param latitude - latitude of object
* @param member - object itself
* @return number of elements added to the sorted set
*/
RFuture<Boolean> tryAddAsync(double longitude, double latitude, V member);
/**
* Adds geospatial members only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param entries - objects
* @return number of elements added to the sorted set
*/
RFuture<Long> tryAddAsync(GeoEntry... entries);
/**
* Returns distance between members in <code>GeoUnit</code> units.
*

@ -18,6 +18,7 @@ package org.redisson.api;
import java.util.List;
import java.util.Map;
import io.reactivex.rxjava3.core.Single;
import reactor.core.publisher.Mono;
/**
@ -73,6 +74,28 @@ public interface RGeoReactive<V> extends RScoredSortedSetReactive<V> {
*/
Mono<Long> addIfExists(GeoEntry... entries);
/**
* Adds geospatial member only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param longitude - longitude of object
* @param latitude - latitude of object
* @param member - object itself
* @return number of elements added to the sorted set
*/
Mono<Boolean> tryAdd(double longitude, double latitude, V member);
/**
* Adds geospatial members only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param entries - objects
* @return number of elements added to the sorted set
*/
Mono<Long> tryAdd(GeoEntry... entries);
/**
* Returns distance between members in <code>GeoUnit</code> units.
*

@ -73,6 +73,28 @@ public interface RGeoRx<V> extends RScoredSortedSetRx<V> {
*/
Single<Long> addIfExists(GeoEntry... entries);
/**
* Adds geospatial member only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param longitude - longitude of object
* @param latitude - latitude of object
* @param member - object itself
* @return number of elements added to the sorted set
*/
Single<Boolean> tryAdd(double longitude, double latitude, V member);
/**
* Adds geospatial members only if has not been added before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param entries - objects
* @return number of elements added to the sorted set
*/
Single<Long> tryAdd(GeoEntry... entries);
/**
* Returns distance between members in <code>GeoUnit</code> units.
*

@ -62,6 +62,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> DEBUG = new RedisStrictCommand<Void>("DEBUG");
RedisStrictCommand<Long> GEOADD = new RedisStrictCommand<Long>("GEOADD");
RedisStrictCommand<Boolean> GEOADD_BOOLEAN = new RedisStrictCommand<>("GEOADD", new BooleanReplayConvertor());
RedisCommand<Double> GEODIST = new RedisCommand<Double>("GEODIST", new DoubleReplayConvertor());
RedisCommand<List<Object>> GEORADIUS_RO = new RedisCommand<List<Object>>("GEORADIUS_RO", new ObjectListReplayDecoder<Object>());
RedisCommand<List<Object>> GEORADIUSBYMEMBER_RO = new RedisCommand<List<Object>>("GEORADIUSBYMEMBER_RO", new ObjectListReplayDecoder<Object>());

@ -36,6 +36,28 @@ public class RedissonGeoTest extends BaseTest {
assertThat(geo.add(2.51, 3.12, "city1")).isEqualTo(1);
}
@Test
public void testAddIfExists() {
RGeo<String> geo = redisson.getGeo("test");
assertThat(geo.add(2.51, 3.12, "city1")).isEqualTo(1);
assertThat(geo.addIfExists(2.9, 3.9, "city1")).isTrue();
Map<String, GeoPosition> pos = geo.pos("city1");
System.out.println("" + pos.get("city1"));
assertThat(pos.get("city1").getLatitude()).isBetween(3.8, 3.9);
assertThat(pos.get("city1").getLongitude()).isBetween(2.8, 3.0);
assertThat(geo.addIfExists(2.12, 3.5, "city2")).isFalse();
}
@Test
public void testTryAdd() {
RGeo<String> geo = redisson.getGeo("test");
assertThat(geo.add(2.51, 3.12, "city1")).isEqualTo(1);
assertThat(geo.tryAdd(2.5, 3.1, "city1")).isFalse();
assertThat(geo.tryAdd(2.12, 3.5, "city2")).isTrue();
}
@Test
public void testAddEntries() {
RGeo<String> geo = redisson.getGeo("test");

Loading…
Cancel
Save