Add multi member / batch methods to scored sorted set.

Signed-off-by: Johno Crawford <johno.crawford@gmail.com>
pull/2982/head
Johno Crawford 5 years ago
parent f4ad9382e1
commit 4611323560
No known key found for this signature in database
GPG Key ID: 747B819B88FCAB8A

@ -15,20 +15,6 @@
*/
package org.redisson;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.redisson.api.RFuture;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
@ -48,6 +34,20 @@ import org.redisson.iterator.RedissonBaseIterator;
import org.redisson.mapreduce.RedissonCollectionMapReduce;
import org.redisson.misc.RedissonPromise;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
*
* @author Nikita Koksharov
@ -204,6 +204,11 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return get(addAndGetRevRankAsync(score, object));
}
@Override
public Collection<Long> addAndGetAllRevRank(Map<? extends V, Double> map) {
return get(addAndGetAllRevRankAsync(map));
}
@Override
public RFuture<Integer> addAndGetRevRankAsync(double score, V object) {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_INTEGER,
@ -212,6 +217,32 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
Collections.<Object>singletonList(getName()), new BigDecimal(score).toPlainString(), encode(object));
}
@Override
public RFuture<Collection<Long>> addAndGetAllRevRankAsync(Map<? extends V, Double> map) {
final List<Object> params = new ArrayList<Object>(map.size() * 2);
for (java.util.Map.Entry<? extends V, Double> t : map.entrySet()) {
if (t.getKey() == null) {
throw new NullPointerException("map key can't be null");
}
if (t.getValue() == null) {
throw new NullPointerException("map value can't be null");
}
params.add(encode(t.getKey()));
params.add(BigDecimal.valueOf(t.getValue()).toPlainString());
}
return commandExecutor.evalReadAsync((String) null, LongCodec.INSTANCE, RedisCommands.EVAL_LIST,
"local r = {} " +
"for i, v in ipairs(ARGV) do " +
"if i % 2 == 0 then " +
"redis.call('zadd', KEYS[1], ARGV[i], ARGV[i-1]); " +
"r[#r+1] = redis.call('zrevrank', KEYS[1], ARGV[i-1]); " +
"end; " +
"end;" +
"return r;",
Collections.singletonList(getName()), params.toArray());
}
@Override
public boolean tryAdd(double score, V object) {
return get(tryAddAsync(score, object));
@ -372,11 +403,27 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return get(getScoreAsync(o));
}
@Override
public Collection<Double> getAllScore(List<V> keys) {
return get(getAllScoreAsync(keys));
}
@Override
public RFuture<Double> getScoreAsync(V o) {
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZSCORE, getName(), encode(o));
}
@Override
public RFuture<Collection<Double>> getAllScoreAsync(Collection<V> elements) {
return commandExecutor.evalReadAsync((String) null, DoubleCodec.INSTANCE, RedisCommands.EVAL_LIST,
"local r = {} " +
"for i, v in ipairs(ARGV) do " +
"r[#r+1] = redis.call('ZSCORE', KEYS[1], ARGV[i]); " +
"end;" +
"return r;",
Collections.singletonList(getName()), encode(elements).toArray());
}
@Override
public Integer rank(V o) {
return get(rankAsync(o));

@ -17,6 +17,7 @@ package org.redisson.api;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -263,6 +264,14 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
*/
Double getScore(V o);
/**
* Returns scores of elements.
*
* @param elements - elements
* @return element scores
*/
Collection<Double> getAllScore(List<V> elements);
/**
* Adds element to this set, overrides previous score if it has been already added.
*
@ -290,6 +299,14 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
*/
Integer addAndGetRevRank(double score, V object);
/**
* Adds elements to this set, overrides previous score if it has been already added.
* Finally returns reverse rank collection of the items
* @param map - map of object and scores, make sure to use an ordered map
* @return collection of reverse ranks
*/
Collection<Long> addAndGetAllRevRank(Map<? extends V, Double> map);
/**
* Adds element to this set only if has not been added before.
* <p>

@ -223,6 +223,14 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/
RFuture<Double> getScoreAsync(V o);
/**
* Returns scores of elements.
*
* @param elements - elements
* @return element scores
*/
RFuture<Collection<Double>> getAllScoreAsync(Collection<V> elements);
/**
* Adds element to this set, overrides previous score if it has been already added.
*
@ -250,6 +258,14 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/
RFuture<Integer> addAndGetRevRankAsync(double score, V object);
/**
* Adds elements to this set, overrides previous score if it has been already added.
* Finally returns reverse rank collection of the items
* @param map - map of object and scores, make sure to use an ordered map
* @return collection of reverse ranks
*/
RFuture<Collection<Long>> addAndGetAllRevRankAsync(Map<? extends V, Double> map);
/**
* Adds element to this set only if has not been added before.
* <p>

@ -235,6 +235,14 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
*/
Mono<Double> getScore(V o);
/**
* Returns scores of elements.
*
* @param elements - elements
* @return element scores
*/
Mono<Collection<Double>> getAllScore(Collection<V> elements);
/**
* Adds element to this set, overrides previous score if it has been already added.
*
@ -272,7 +280,15 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
* @return reverse rank
*/
Mono<Integer> addAndGetRevRank(double score, V object);
/**
* Adds elements to this set, overrides previous score if it has been already added.
* Finally returns reverse rank collection of the items
* @param map - map of object and scores, make sure to use an ordered map
* @return collection of reverse ranks
*/
Mono<Collection<Long>> addAndGetAllRevRank(Map<? extends V, Double> map);
/**
* Adds element to this set only if has not been added before.
* <p>

@ -236,6 +236,14 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
*/
Maybe<Double> getScore(V o);
/**
* Returns scores of elements.
*
* @param elements - elements
* @return element scores
*/
Maybe<Collection<Double>> getAllScore(Collection<V> elements);
/**
* Adds element to this set, overrides previous score if it has been already added.
*
@ -273,6 +281,14 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
* @return reverse rank
*/
Single<Integer> addAndGetRevRank(double score, V object);
/**
* Adds elements to this set, overrides previous score if it has been already added.
* Finally returns reverse rank collection of the items
* @param map - map of object and scores, make sure to use an ordered map
* @return collection of reverse ranks
*/
Single<Collection<Long>> addAndGetAllRevRank(Map<? extends V, Double> map);
/**
* Adds element to this set only if has not been added before.

@ -7,6 +7,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
@ -1226,6 +1227,21 @@ public class RedissonScoredSortedSetTest extends BaseTest {
res2 = set2.getScore("1");
Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
}
@Test
public void testAddAndGetAll() throws InterruptedException {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(100.2, "1");
Double res2 = set.addScore("1", new Double(12.1));
Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
res2 = set.getScore("1");
Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
Collection<Double> res = set.getAllScore(Arrays.asList("1", "42", "100"));
Assert.assertArrayEquals(new Double[] {112.3d, null, null},
res.toArray());
}
@Test
public void testAddScoreAndGetRank() throws InterruptedException {
@ -1265,7 +1281,18 @@ public class RedissonScoredSortedSetTest extends BaseTest {
assertThat(score).isEqualTo(14);
}
@Test
public void testAddAndGetAllRevRank() throws InterruptedException {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
Map<String, Double> map = new LinkedHashMap<>();
map.put("alice", 10d);
map.put("bob", 1d);
Collection<Long> res = set.addAndGetAllRevRank(map);
Assert.assertArrayEquals(new Long[]{0L, 1L}, res.toArray());
assertThat(set.revRank("alice")).isEqualTo(0);
assertThat(set.revRank("bob")).isEqualTo(1);
}
@Test
public void testIntersection() {

Loading…
Cancel
Save