Feature - add random() method to RScoredSortedSet object #3401

pull/3417/head
Nikita Koksharov 4 years ago
parent 8c8a96dd22
commit ce07beee56

@ -202,6 +202,16 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANDMEMBER, getName(), count);
}
@Override
public Map<V, Double> randomEntries(int count) {
return get(randomEntriesAsync(count));
}
@Override
public RFuture<Map<V, Double>> randomEntriesAsync(int count) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANDMEMBER_ENTRIES, getName(), count, "WITHSCORES");
}
@Override
public boolean add(double score, V object) {
return get(addAsync(score, object));

@ -206,6 +206,8 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
/**
* Returns random element from this sorted set
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return random element
*/
@ -213,12 +215,25 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
/**
* Returns random elements from this sorted set limited by <code>count</code>
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - values amount to return
* @return random elements
*/
Collection<V> random(int count);
/**
* Returns random entries from this sorted set limited by <code>count</code>.
* Each map entry uses element as key and score as value.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - entries amount to return
* @return random entries
*/
Map<V, Double> randomEntries(int count);
/**
* Adds all elements contained in the specified map to this sorted set.
* Map contains of score mapped by object.

@ -167,6 +167,8 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
/**
* Returns random element from this sorted set
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return value
*/
@ -174,12 +176,25 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
/**
* Returns random elements from this sorted set limited by <code>count</code>
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - values amount to return
* @return value
*/
RFuture<Collection<V>> randomAsync(int count);
/**
* Returns random entries from this sorted set limited by <code>count</code>.
* Each map entry uses element as key and score as value.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - entries amount to return
* @return random entries
*/
RFuture<Map<V, Double>> randomEntriesAsync(int count);
/**
* Adds all elements contained in the specified map to this sorted set.
* Map contains of score mapped by object.

@ -157,6 +157,8 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
/**
* Returns random element from this sorted set
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return random element
*/
@ -164,12 +166,25 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
/**
* Returns random elements from this sorted set limited by <code>count</code>
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - values amount to return
* @return random elements
*/
Mono<Collection<V>> random(int count);
/**
* Returns random entries from this sorted set limited by <code>count</code>.
* Each map entry uses element as key and score as value.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - entries amount to return
* @return random entries
*/
Mono<Map<V, Double>> randomEntries(int count);
/**
* Returns an iterator over elements in this set.
* If <code>pattern</code> is not null then only elements match this pattern are loaded.

@ -158,6 +158,8 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
/**
* Returns random element from this sorted set
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return random element
*/
@ -165,12 +167,25 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
/**
* Returns random elements from this sorted set limited by <code>count</code>
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - values amount to return
* @return random elements
*/
Single<Collection<V>> random(int count);
/**
* Returns random entries from this sorted set limited by <code>count</code>.
* Each map entry uses element as key and score as value.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param count - entries amount to return
* @return random entries
*/
Single<Map<V, Double>> randomEntries(int count);
/**
* Returns an iterator over elements in this set.
* If <code>pattern</code> is not null then only elements match this pattern are loaded.

@ -95,6 +95,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> ASKING = new RedisStrictCommand<Void>("ASKING", new VoidReplayConvertor());
RedisStrictCommand<Void> READONLY = new RedisStrictCommand<Void>("READONLY", new VoidReplayConvertor());
RedisCommand<Map<Object, Object>> ZRANDMEMBER_ENTRIES = new RedisCommand<>("ZRANDMEMBER", new ScoredSortedSetRandomMapDecoder());
RedisCommand<Set<Object>> ZRANDMEMBER = new RedisCommand<>("ZRANDMEMBER", new ObjectSetReplayDecoder<>());
RedisCommand<Object> ZRANDMEMBER_SINGLE = new RedisCommand<>("ZRANDMEMBER");
RedisStrictCommand<List<Object>> ZDIFF = new RedisStrictCommand<>("ZDIFF", new ObjectListReplayDecoder<>());

@ -29,32 +29,14 @@ import org.redisson.client.protocol.Decoder;
* @author Nikita Koksharov
*
*/
public class GeoDistanceMapDecoder implements MultiDecoder<Map<Object, Object>> {
public class ScoredSortedSetRandomMapDecoder extends ObjectMapReplayDecoder<Object, Object> {
private final Codec codec;
public GeoDistanceMapDecoder(Codec codec) {
super();
this.codec = codec;
}
@Override
public Decoder<Object> getDecoder(int paramNum, State state) {
if (paramNum % 2 == 0) {
return codec.getValueDecoder();
return super.getDecoder(paramNum, state);
}
return DoubleCodec.INSTANCE.getValueDecoder();
}
@Override
public Map<Object, Object> decode(List<Object> parts, State state) {
Map<Object, Object> result = new HashMap<Object, Object>(parts.size()/2);
for (int i = 0; i < parts.size(); i++) {
if (i % 2 != 0) {
result.put(parts.get(i-1), parts.get(i));
}
}
return result;
}
}

@ -1,6 +1,7 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import java.io.IOException;
import java.util.Arrays;
@ -40,6 +41,9 @@ public class RedissonScoredSortedSetTest extends BaseTest {
assertThat(set.random()).isIn(10, 20, 30);
assertThat(set.random(2)).containsAnyOf(10, 20, 30).hasSize(2);
Map<Integer, Double> map = set.randomEntries(2);
assertThat(map).containsAnyOf(entry(10, 1D), entry(20, 2D), entry(30, 3D)).hasSize(2);
}
@Test

Loading…
Cancel
Save