Feature - addAllIfLess(), addAllIfGreater(), addAllIfExist(), addAllIfAbsent() methods added to RScoredSortedSet object. #4134

pull/4162/head
Nikita Koksharov 3 years ago
parent 12f7fedd6f
commit 979f0a0961

@ -278,7 +278,7 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public RFuture<Boolean> addIfExistsAsync(double score, V object) { public RFuture<Boolean> addIfExistsAsync(double score, V object) {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_BOOL, getRawName(), "XX", BigDecimal.valueOf(score).toPlainString(), encode(object)); return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_BOOL, getRawName(), "XX", "CH", BigDecimal.valueOf(score).toPlainString(), encode(object));
} }
@Override @Override
@ -369,6 +369,93 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray()); return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray());
} }
@Override
public int addAllIfAbsent(Map<V, Double> objects) {
return get(addAllIfAbsentAsync(objects));
}
@Override
public RFuture<Integer> addAllIfAbsentAsync(Map<V, Double> objects) {
if (objects.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> params = new ArrayList<>(objects.size()*2+1);
params.add(getRawName());
params.add("NX");
for (Entry<V, Double> entry : objects.entrySet()) {
params.add(BigDecimal.valueOf(entry.getValue()).toPlainString());
params.add(encode(entry.getKey()));
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray());
}
@Override
public int addAllIfExist(Map<V, Double> objects) {
return get(addAllIfExistAsync(objects));
}
@Override
public RFuture<Integer> addAllIfExistAsync(Map<V, Double> objects) {
if (objects.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> params = new ArrayList<>(objects.size()*2+1);
params.add(getRawName());
params.add("XX");
params.add("CH");
for (Entry<V, Double> entry : objects.entrySet()) {
params.add(BigDecimal.valueOf(entry.getValue()).toPlainString());
params.add(encode(entry.getKey()));
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray());
}
@Override
public int addAllIfGreater(Map<V, Double> objects) {
return get(addAllIfGreaterAsync(objects));
}
@Override
public RFuture<Integer> addAllIfGreaterAsync(Map<V, Double> objects) {
if (objects.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> params = new ArrayList<>(objects.size()*2+1);
params.add(getRawName());
params.add("GT");
params.add("CH");
for (Entry<V, Double> entry : objects.entrySet()) {
params.add(BigDecimal.valueOf(entry.getValue()).toPlainString());
params.add(encode(entry.getKey()));
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray());
}
@Override
public int addAllIfLess(Map<V, Double> objects) {
return get(addAllIfLessAsync(objects));
}
@Override
public RFuture<Integer> addAllIfLessAsync(Map<V, Double> objects) {
if (objects.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> params = new ArrayList<>(objects.size()*2+1);
params.add(getRawName());
params.add("LT");
params.add("CH");
for (Entry<V, Double> entry : objects.entrySet()) {
params.add(BigDecimal.valueOf(entry.getValue()).toPlainString());
params.add(encode(entry.getKey()));
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_INT, params.toArray());
}
@Override @Override
public RFuture<Boolean> tryAddAsync(double score, V object) { public RFuture<Boolean> tryAddAsync(double score, V object) {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_BOOL, getRawName(), "NX", BigDecimal.valueOf(score).toPlainString(), encode(object)); return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.ZADD_BOOL, getRawName(), "NX", BigDecimal.valueOf(score).toPlainString(), encode(object));

@ -243,6 +243,46 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
*/ */
int addAll(Map<V, Double> objects); int addAll(Map<V, Double> objects);
/**
* Adds elements to this set only if they haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
int addAllIfAbsent(Map<V, Double> objects);
/**
* Adds elements to this set only if they already exist.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
int addAllIfExist(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores greater than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
int addAllIfGreater(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores less than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
int addAllIfLess(Map<V, Double> objects);
/** /**
* Removes values by score range. * Removes values by score range.
* *

@ -204,6 +204,46 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/ */
RFuture<Integer> addAllAsync(Map<V, Double> objects); RFuture<Integer> addAllAsync(Map<V, Double> objects);
/**
* Adds elements to this set only if they haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
RFuture<Integer> addAllIfAbsentAsync(Map<V, Double> objects);
/**
* Adds elements to this set only if they already exist.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
RFuture<Integer> addAllIfExistAsync(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores greater than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
RFuture<Integer> addAllIfGreaterAsync(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores less than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
RFuture<Integer> addAllIfLessAsync(Map<V, Double> objects);
/** /**
* Removes values by score range. * Removes values by score range.
* *

@ -15,18 +15,17 @@
*/ */
package org.redisson.api; package org.redisson.api;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/** /**
* Reactive interface for SortedSet object * Reactive interface for SortedSet object
* *
@ -299,7 +298,47 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
* @return amount of added elements, not including already existing in this sorted set * @return amount of added elements, not including already existing in this sorted set
*/ */
Mono<Integer> addAll(Map<V, Double> objects); Mono<Integer> addAll(Map<V, Double> objects);
/**
* Adds elements to this set only if they haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Mono<Integer> addAllIfAbsent(Map<V, Double> objects);
/**
* Adds elements to this set only if they already exist.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Mono<Integer> addAllIfExist(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores greater than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Mono<Integer> addAllIfGreater(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores less than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Mono<Integer> addAllIfLess(Map<V, Double> objects);
/** /**
* Adds element to this set, overrides previous score if it has been already added. * Adds element to this set, overrides previous score if it has been already added.
* Finally return the rank of the item * Finally return the rank of the item

@ -15,19 +15,18 @@
*/ */
package org.redisson.api; package org.redisson.api;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
/** /**
* RxJava2 interface for scored sorted set data structure. * RxJava2 interface for scored sorted set data structure.
* *
@ -300,7 +299,47 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
* @return amount of added elements, not including already existing in this sorted set * @return amount of added elements, not including already existing in this sorted set
*/ */
Single<Integer> addAll(Map<V, Double> objects); Single<Integer> addAll(Map<V, Double> objects);
/**
* Adds elements to this set only if they haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Single<Integer> addAllIfAbsent(Map<V, Double> objects);
/**
* Adds elements to this set only if they already exist.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Single<Integer> addAllIfExist(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores greater than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Single<Integer> addAllIfGreater(Map<V, Double> objects);
/**
* Adds elements to this set only if new scores less than current score of existed elements.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
Single<Integer> addAllIfLess(Map<V, Double> objects);
/** /**
* Adds element to this set, overrides previous score if it has been already added. * Adds element to this set, overrides previous score if it has been already added.
* Finally return the rank of the item * Finally return the rank of the item

@ -1,29 +1,5 @@
package org.redisson; package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -34,6 +10,19 @@ import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.ScoredEntry; import org.redisson.client.protocol.ScoredEntry;
import org.redisson.config.Config; import org.redisson.config.Config;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RedissonScoredSortedSetTest extends BaseTest { public class RedissonScoredSortedSetTest extends BaseTest {
@Test @Test
@ -422,6 +411,87 @@ public class RedissonScoredSortedSetTest extends BaseTest {
new ScoredEntry<String>(0.1, "1"), new ScoredEntry<String>(0.2, "2"), new ScoredEntry<String>(0.3, "3")); new ScoredEntry<String>(0.1, "1"), new ScoredEntry<String>(0.2, "2"), new ScoredEntry<String>(0.3, "3"));
} }
@Test
public void testAddAllIfAbsent() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(10, "1981");
set.add(11, "1984");
Map<String, Double> map = new HashMap<>();
map.put("1981", 111D);
map.put("1982", 112D);
map.put("1983", 113D);
map.put("1984", 114D);
assertThat(set.addAllIfAbsent(map)).isEqualTo(2);
assertThat(set.getScore("1981")).isEqualTo(10);
assertThat(set.getScore("1984")).isEqualTo(11);
assertThat(set).contains("1981", "1982", "1983", "1984");
}
@Test
public void testAddAllIfExist() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(10, "1981");
set.add(11, "1984");
Map<String, Double> map = new HashMap<>();
map.put("1981", 111D);
map.put("1982", 112D);
map.put("1983", 113D);
map.put("1984", 114D);
assertThat(set.addAllIfExist(map)).isEqualTo(2);
assertThat(set.getScore("1981")).isEqualTo(111D);
assertThat(set.getScore("1984")).isEqualTo(114D);
}
@Test
public void testAddAllIfGreater() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(10, "1981");
set.add(11, "1984");
set.add(13, "1985");
Map<String, Double> map = new HashMap<>();
map.put("1981", 111D);
map.put("1982", 112D);
map.put("1983", 113D);
map.put("1984", 8D);
map.put("1985", 3D);
assertThat(set.addAllIfGreater(map)).isEqualTo(3);
assertThat(set.size()).isEqualTo(5);
assertThat(set.getScore("1981")).isEqualTo(111D);
assertThat(set.getScore("1982")).isEqualTo(112D);
assertThat(set.getScore("1983")).isEqualTo(113D);
assertThat(set.getScore("1984")).isEqualTo(11D);
assertThat(set.getScore("1985")).isEqualTo(13D);
}
@Test
public void testAddAllIfLess() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(10D, "1981");
set.add(11D, "1984");
set.add(13D, "1985");
Map<String, Double> map = new HashMap<>();
map.put("1981", 111D);
map.put("1982", 112D);
map.put("1983", 113D);
map.put("1984", 8D);
map.put("1985", 3D);
assertThat(set.addAllIfLess(map)).isEqualTo(4);
assertThat(set.size()).isEqualTo(5);
assertThat(set.getScore("1981")).isEqualTo(10D);
assertThat(set.getScore("1982")).isEqualTo(112D);
assertThat(set.getScore("1983")).isEqualTo(113D);
assertThat(set.getScore("1984")).isEqualTo(8D);
assertThat(set.getScore("1985")).isEqualTo(3D);
}
@Test @Test
public void testAddIfGreater() { public void testAddIfGreater() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple"); RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
@ -449,7 +519,7 @@ public class RedissonScoredSortedSetTest extends BaseTest {
assertThat(set.addIfExists(123.81, "1980")).isFalse(); assertThat(set.addIfExists(123.81, "1980")).isFalse();
assertThat(set.getScore("1980")).isNull(); assertThat(set.getScore("1980")).isNull();
set.add(111, "1980"); set.add(111, "1980");
assertThat(set.addIfExists(32, "1980")).isFalse(); assertThat(set.addIfExists(32, "1980")).isTrue();
assertThat(set.getScore("1980")).isEqualTo(32); assertThat(set.getScore("1980")).isEqualTo(32);
} }

Loading…
Cancel
Save