RSet.removeRandom with elements amount added. #720

pull/727/head
Nikita 8 years ago
parent ec2e67ebdd
commit 08726b1e0b

@ -143,6 +143,16 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V> {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SPOP_SINGLE, getName());
}
@Override
public Set<V> removeRandom(int amount) {
return get(removeRandomAsync(amount));
}
@Override
public RFuture<Set<V>> removeRandomAsync(int amount) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SPOP, getName(), amount);
}
@Override
public V random() {
return get(randomAsync());

@ -26,6 +26,14 @@ import java.util.Set;
*/
public interface RSet<V> extends Set<V>, RExpirable, RSetAsync<V> {
/**
* Removes and returns random elements from set
*
* @param amount of random values
* @return random values
*/
Set<V> removeRandom(int amount);
/**
* Removes and returns random element from set
*

@ -26,6 +26,15 @@ import java.util.Set;
*/
public interface RSetAsync<V> extends RCollectionAsync<V> {
/**
* Removes and returns random elements from set
* in async mode
*
* @param amount of random values
* @return random values
*/
RFuture<Set<V>> removeRandomAsync(int amount);
/**
* Removes and returns random element from set
* in async mode

@ -136,6 +136,7 @@ public interface RedisCommands {
RedisCommand<Boolean> SADD_BOOL = new RedisCommand<Boolean>("SADD", new BooleanAmountReplayConvertor(), 2, ValueType.OBJECTS);
RedisStrictCommand<Long> SADD = new RedisStrictCommand<Long>("SADD", 2, ValueType.OBJECTS);
RedisCommand<Set<Object>> SPOP = new RedisCommand<Set<Object>>("SPOP", new ObjectSetReplayDecoder<Object>());
RedisCommand<Object> SPOP_SINGLE = new RedisCommand<Object>("SPOP");
RedisCommand<Boolean> SADD_SINGLE = new RedisCommand<Boolean>("SADD", new BooleanReplayConvertor(), 2);
RedisCommand<Boolean> SREM_SINGLE = new RedisCommand<Boolean>("SREM", new BooleanAmountReplayConvertor(), 2, ValueType.OBJECTS);

@ -43,6 +43,23 @@ public class RedissonSetTest extends BaseTest {
assertThat(set.removeRandom()).isIn(1, 2, 3);
assertThat(set.removeRandom()).isNull();
}
@Test
public void testRemoveRandomAmount() {
RSet<Integer> set = redisson.getSet("simple");
set.add(1);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
set.add(6);
assertThat(set.removeRandom(3)).isSubsetOf(1, 2, 3, 4, 5, 6).hasSize(3);
assertThat(set.removeRandom(2)).isSubsetOf(1, 2, 3, 4, 5, 6).hasSize(2);
assertThat(set.removeRandom(1)).isSubsetOf(1, 2, 3, 4, 5, 6).hasSize(1);
assertThat(set.removeRandom(4)).isEmpty();
}
@Test
public void testRandom() {

Loading…
Cancel
Save