Merge pull request #546 from layerhq/layer

Add support for getting a random set member
pull/555/head
Nikita Koksharov 9 years ago committed by GitHub
commit 00d92ff74c

@ -144,6 +144,16 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V> {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SPOP_SINGLE, getName());
}
@Override
public V random() {
return get(randomAsync());
}
@Override
public Future<V> randomAsync() {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER_SINGLE, getName());
}
@Override
public Future<Boolean> removeAsync(Object o) {
return commandExecutor.writeAsync(getName(o), codec, RedisCommands.SREM_SINGLE, getName(o), o);

@ -194,6 +194,16 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SPOP_SINGLE, getName());
}
@Override
public V random() {
return get(randomAsync());
}
@Override
public Future<V> randomAsync() {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER_SINGLE, getName());
}
@Override
public Future<Boolean> removeAsync(Object o) {
return commandExecutor.evalWriteAsync(getName(), codec, EVAL_CONTAINS_VALUE,

@ -36,6 +36,14 @@ public interface RSetReactive<V> extends RCollectionReactive<V> {
*/
Publisher<V> removeRandom();
/**
* Returns random element from set
* in async mode
*
* @return
*/
Publisher<V> random();
/**
* Move a member from this set to the given destination set in async mode.
*

@ -131,6 +131,7 @@ public interface RedisCommands {
RedisCommand<Boolean> SREM_SINGLE = new RedisCommand<Boolean>("SREM", new BooleanAmountReplayConvertor(), 2, ValueType.OBJECTS);
RedisCommand<Boolean> SMOVE = new RedisCommand<Boolean>("SMOVE", new BooleanReplayConvertor(), 3);
RedisCommand<Set<Object>> SMEMBERS = new RedisCommand<Set<Object>>("SMEMBERS", new ObjectSetReplayDecoder<Object>());
RedisCommand<Object> SRANDMEMBER_SINGLE = new RedisCommand<Object>("SRANDMEMBER");
RedisCommand<ListScanResult<Object>> SSCAN = new RedisCommand<ListScanResult<Object>>("SSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()), ValueType.OBJECT);
RedisCommand<ListScanResult<Object>> EVAL_SSCAN = new RedisCommand<ListScanResult<Object>>("EVAL", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()), ValueType.OBJECT);
RedisCommand<ListScanResult<Object>> EVAL_ZSCAN = new RedisCommand<ListScanResult<Object>>("EVAL", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()), ValueType.OBJECT);

@ -33,6 +33,13 @@ public interface RSet<V> extends Set<V>, RExpirable, RSetAsync<V> {
*/
V removeRandom();
/**
* Returns random element from set
*
* @return
*/
V random();
/**
* Move a member from this set to the given destination set in.
*

@ -36,6 +36,14 @@ public interface RSetAsync<V> extends RCollectionAsync<V> {
*/
Future<V> removeRandomAsync();
/**
* Returns random element from set
* in async mode
*
* @return
*/
Future<V> randomAsync();
/**
* Move a member from this set to the given destination set in async mode.
*

@ -80,6 +80,11 @@ public class RedissonSetReactive<V> extends RedissonExpirableReactive implements
return reactive(instance.removeRandomAsync());
}
@Override
public Publisher<V> random() {
return reactive(instance.randomAsync());
}
@Override
public Publisher<Boolean> remove(Object o) {
return reactive(instance.removeAsync(o));

@ -57,6 +57,19 @@ public class RedissonSetReactiveTest extends BaseReactiveTest {
Assert.assertNull(sync(set.removeRandom()));
}
@Test
public void testRandom() {
RSetReactive<Integer> set = redisson.getSet("simple");
sync(set.add(1));
sync(set.add(2));
sync(set.add(3));
MatcherAssert.assertThat(sync(set.random()), Matchers.isOneOf(1, 2, 3));
MatcherAssert.assertThat(sync(set.random()), Matchers.isOneOf(1, 2, 3));
MatcherAssert.assertThat(sync(set.random()), Matchers.isOneOf(1, 2, 3));
Assert.assertThat(sync(set), Matchers.containsInAnyOrder(1, 2, 3));
}
@Test
public void testAddBean() throws InterruptedException, ExecutionException {
SimpleBean sb = new SimpleBean();

@ -46,6 +46,19 @@ public class RedissonSetTest extends BaseTest {
assertThat(set.removeRandom()).isNull();
}
@Test
public void testRandom() {
RSet<Integer> set = redisson.getSet("simple");
set.add(1);
set.add(2);
set.add(3);
assertThat(set.random()).isIn(1, 2, 3);
assertThat(set.random()).isIn(1, 2, 3);
assertThat(set.random()).isIn(1, 2, 3);
assertThat(set).containsOnly(1, 2, 3);
}
@Test
public void testAddBean() throws InterruptedException, ExecutionException {
SimpleBean sb = new SimpleBean();

Loading…
Cancel
Save