diff --git a/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java b/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java index baa1ece95..a5afb2045 100644 --- a/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -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 extends RedissonExpirable implements RSc return get(addAndGetRevRankAsync(score, object)); } + @Override + public Collection addAndGetAllRevRank(Map map) { + return get(addAndGetAllRevRankAsync(map)); + } + @Override public RFuture addAndGetRevRankAsync(double score, V object) { return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_INTEGER, @@ -212,6 +217,32 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc Collections.singletonList(getName()), new BigDecimal(score).toPlainString(), encode(object)); } + @Override + public RFuture> addAndGetAllRevRankAsync(Map map) { + final List params = new ArrayList(map.size() * 2); + for (java.util.Map.Entry 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 extends RedissonExpirable implements RSc return get(getScoreAsync(o)); } + @Override + public Collection getAllScore(List keys) { + return get(getAllScoreAsync(keys)); + } + @Override public RFuture getScoreAsync(V o) { return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZSCORE, getName(), encode(o)); } + @Override + public RFuture> getAllScoreAsync(Collection 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)); diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java index d1179f01d..a61032923 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java @@ -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 extends RScoredSortedSetAsync, Iterable< */ Double getScore(V o); + /** + * Returns scores of elements. + * + * @param elements - elements + * @return element scores + */ + Collection getAllScore(List elements); + /** * Adds element to this set, overrides previous score if it has been already added. * @@ -290,6 +299,14 @@ public interface RScoredSortedSet extends RScoredSortedSetAsync, 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 addAndGetAllRevRank(Map map); + /** * Adds element to this set only if has not been added before. *

diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java index db312dc4e..8313da0d3 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java @@ -223,6 +223,14 @@ public interface RScoredSortedSetAsync extends RExpirableAsync, RSortableAsyn */ RFuture getScoreAsync(V o); + /** + * Returns scores of elements. + * + * @param elements - elements + * @return element scores + */ + RFuture> getAllScoreAsync(Collection elements); + /** * Adds element to this set, overrides previous score if it has been already added. * @@ -250,6 +258,14 @@ public interface RScoredSortedSetAsync extends RExpirableAsync, RSortableAsyn */ RFuture 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> addAndGetAllRevRankAsync(Map map); + /** * Adds element to this set only if has not been added before. *

diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java index 1257914c2..c6626174c 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java @@ -235,6 +235,14 @@ public interface RScoredSortedSetReactive extends RExpirableReactive, RSortab */ Mono getScore(V o); + /** + * Returns scores of elements. + * + * @param elements - elements + * @return element scores + */ + Mono> getAllScore(Collection elements); + /** * Adds element to this set, overrides previous score if it has been already added. * @@ -272,7 +280,15 @@ public interface RScoredSortedSetReactive extends RExpirableReactive, RSortab * @return reverse rank */ Mono 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> addAndGetAllRevRank(Map map); + /** * Adds element to this set only if has not been added before. *

diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java index 53c8479f1..7f0903c1c 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java @@ -236,6 +236,14 @@ public interface RScoredSortedSetRx extends RExpirableRx, RSortableRx> */ Maybe getScore(V o); + /** + * Returns scores of elements. + * + * @param elements - elements + * @return element scores + */ + Maybe> getAllScore(Collection elements); + /** * Adds element to this set, overrides previous score if it has been already added. * @@ -273,6 +281,14 @@ public interface RScoredSortedSetRx extends RExpirableRx, RSortableRx> * @return reverse rank */ Single 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> addAndGetAllRevRank(Map map); /** * Adds element to this set only if has not been added before. diff --git a/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java b/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java index 3c1056cd5..40073763d 100644 --- a/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -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 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 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 set = redisson.getScoredSortedSet("simple"); + Map map = new LinkedHashMap<>(); + map.put("alice", 10d); + map.put("bob", 1d); + Collection 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() {