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

pull/3417/head
Nikita Koksharov 4 years ago
parent e6b8ceb8b3
commit d76bc1f3a6

@ -182,6 +182,26 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.writeAsync(getName(), codec, RedisCommands.BZPOPMAX_VALUE, getName(), toSeconds(timeout, unit));
}
@Override
public V random() {
return get(randomAsync());
}
@Override
public Collection<V> random(int count) {
return get(randomAsync(count));
}
@Override
public RFuture<V> randomAsync() {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANDMEMBER_SINGLE, getName());
}
@Override
public RFuture<Collection<V>> randomAsync(int count) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANDMEMBER, getName(), count);
}
@Override
public boolean add(double score, V object) {
return get(addAsync(score, object));

@ -204,6 +204,21 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
*/
Double lastScore();
/**
* Returns random element from this sorted set
*
* @return random element
*/
V random();
/**
* Returns random elements from this sorted set limited by <code>count</code>
*
* @param count - values amount to return
* @return random elements
*/
Collection<V> random(int count);
/**
* Adds all elements contained in the specified map to this sorted set.
* Map contains of score mapped by object.

@ -165,6 +165,21 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/
RFuture<Double> lastScoreAsync();
/**
* Returns random element from this sorted set
*
* @return value
*/
RFuture<V> randomAsync();
/**
* Returns random elements from this sorted set limited by <code>count</code>
*
* @param count - values amount to return
* @return value
*/
RFuture<Collection<V>> randomAsync(int count);
/**
* Adds all elements contained in the specified map to this sorted set.
* Map contains of score mapped by object.

@ -21,6 +21,8 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
@ -155,6 +157,21 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
*/
Mono<Double> lastScore();
/**
* Returns random element from this sorted set
*
* @return random element
*/
Mono<V> random();
/**
* Returns random elements from this sorted set limited by <code>count</code>
*
* @param count - values amount to return
* @return random elements
*/
Mono<Collection<V>> random(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.

@ -156,6 +156,21 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
*/
Maybe<Double> lastScore();
/**
* Returns random element from this sorted set
*
* @return random element
*/
Maybe<V> random();
/**
* Returns random elements from this sorted set limited by <code>count</code>
*
* @param count - values amount to return
* @return random elements
*/
Single<Collection<V>> random(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,8 @@ public interface RedisCommands {
RedisStrictCommand<Void> ASKING = new RedisStrictCommand<Void>("ASKING", new VoidReplayConvertor());
RedisStrictCommand<Void> READONLY = new RedisStrictCommand<Void>("READONLY", new VoidReplayConvertor());
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<>());
RedisCommand<List<Object>> ZUNION = new RedisCommand<>("ZUNION", new ObjectListReplayDecoder<>());
RedisCommand<List<Object>> ZINTER = new RedisCommand<>("ZINTER", new ObjectListReplayDecoder<>());

@ -31,6 +31,17 @@ import org.redisson.config.Config;
public class RedissonScoredSortedSetTest extends BaseTest {
@Test
public void testRandom() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("test");
set.add(1, 10);
set.add(2, 20);
set.add(3, 30);
assertThat(set.random()).isIn(10, 20, 30);
assertThat(set.random(2)).containsAnyOf(10, 20, 30).hasSize(2);
}
@Test
public void testTakeFirst() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("5.0.0") > 0);

Loading…
Cancel
Save