RSet.removeRandom and RSet.removeRandomAsync methods. #201

pull/243/head
Nikita 10 years ago
parent 6bc5faa442
commit 5c1fba0704

@ -150,6 +150,16 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V> {
return commandExecutor.writeAsync(getName(), RedisCommands.SADD_SINGLE, getName(), e);
}
@Override
public V removeRandom() {
return get(removeRandomAsync());
}
@Override
public Future<V> removeRandomAsync() {
return commandExecutor.writeAsync(getName(), RedisCommands.SPOP_SINGLE, getName());
}
@Override
public Future<Boolean> removeAsync(Object o) {
return commandExecutor.writeAsync(getName(), RedisCommands.SREM_SINGLE, getName(), o);

@ -52,6 +52,7 @@ public interface RedisCommands {
RedisCommand<Long> SREM = new RedisCommand<Long>("SREM", 2, ValueType.OBJECTS);
RedisCommand<Boolean> SADD = new RedisCommand<Boolean>("SADD", new BooleanAmountReplayConvertor(), 2, ValueType.OBJECTS);
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 BooleanReplayConvertor(), 2);
RedisCommand<List<Object>> SMEMBERS = new RedisCommand<List<Object>>("SMEMBERS", new ObjectListReplayDecoder<Object>());

@ -26,4 +26,11 @@ import java.util.Set;
*/
public interface RSet<V> extends Set<V>, RExpirable, RSetAsync<V> {
/**
* Removes and returns random element from set
*
* @return
*/
V removeRandom();
}

@ -15,6 +15,8 @@
*/
package org.redisson.core;
import io.netty.util.concurrent.Future;
/**
* Async set functions
*
@ -24,4 +26,12 @@ package org.redisson.core;
*/
public interface RSetAsync<V> extends RCollectionAsync<V> {
/**
* Removes and returns random element from set
* in async mode
*
* @return
*/
Future<V> removeRandomAsync();
}

@ -32,6 +32,19 @@ public class RedissonSetTest extends BaseTest {
}
@Test
public void testRemoveRandom() {
RSet<Integer> set = redisson.getSet("simple");
set.add(1);
set.add(2);
set.add(3);
MatcherAssert.assertThat(set.removeRandom(), Matchers.isOneOf(1, 2, 3));
MatcherAssert.assertThat(set.removeRandom(), Matchers.isOneOf(1, 2, 3));
MatcherAssert.assertThat(set.removeRandom(), Matchers.isOneOf(1, 2, 3));
Assert.assertNull(set.removeRandom());
}
@Test
public void testAddBean() throws InterruptedException, ExecutionException {
SimpleBean sb = new SimpleBean();

Loading…
Cancel
Save