From 5c1fba07046dcaca242feb6997c70c30f99b85e3 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 31 Jul 2015 09:32:23 +0300 Subject: [PATCH] RSet.removeRandom and RSet.removeRandomAsync methods. #201 --- src/main/java/org/redisson/RedissonSet.java | 10 ++++++++++ .../org/redisson/client/protocol/RedisCommands.java | 1 + src/main/java/org/redisson/core/RSet.java | 7 +++++++ src/main/java/org/redisson/core/RSetAsync.java | 10 ++++++++++ src/test/java/org/redisson/RedissonSetTest.java | 13 +++++++++++++ 5 files changed, 41 insertions(+) diff --git a/src/main/java/org/redisson/RedissonSet.java b/src/main/java/org/redisson/RedissonSet.java index fe7b5222b..e37bae3cb 100644 --- a/src/main/java/org/redisson/RedissonSet.java +++ b/src/main/java/org/redisson/RedissonSet.java @@ -150,6 +150,16 @@ public class RedissonSet extends RedissonExpirable implements RSet { return commandExecutor.writeAsync(getName(), RedisCommands.SADD_SINGLE, getName(), e); } + @Override + public V removeRandom() { + return get(removeRandomAsync()); + } + + @Override + public Future removeRandomAsync() { + return commandExecutor.writeAsync(getName(), RedisCommands.SPOP_SINGLE, getName()); + } + @Override public Future removeAsync(Object o) { return commandExecutor.writeAsync(getName(), RedisCommands.SREM_SINGLE, getName(), o); diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index 4ad6f3a83..f6a6780af 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -52,6 +52,7 @@ public interface RedisCommands { RedisCommand SREM = new RedisCommand("SREM", 2, ValueType.OBJECTS); RedisCommand SADD = new RedisCommand("SADD", new BooleanAmountReplayConvertor(), 2, ValueType.OBJECTS); + RedisCommand SPOP_SINGLE = new RedisCommand("SPOP"); RedisCommand SADD_SINGLE = new RedisCommand("SADD", new BooleanReplayConvertor(), 2); RedisCommand SREM_SINGLE = new RedisCommand("SREM", new BooleanReplayConvertor(), 2); RedisCommand> SMEMBERS = new RedisCommand>("SMEMBERS", new ObjectListReplayDecoder()); diff --git a/src/main/java/org/redisson/core/RSet.java b/src/main/java/org/redisson/core/RSet.java index 6725f80fa..a46f3e9d2 100644 --- a/src/main/java/org/redisson/core/RSet.java +++ b/src/main/java/org/redisson/core/RSet.java @@ -26,4 +26,11 @@ import java.util.Set; */ public interface RSet extends Set, RExpirable, RSetAsync { + /** + * Removes and returns random element from set + * + * @return + */ + V removeRandom(); + } diff --git a/src/main/java/org/redisson/core/RSetAsync.java b/src/main/java/org/redisson/core/RSetAsync.java index bf555286d..15cf00a31 100644 --- a/src/main/java/org/redisson/core/RSetAsync.java +++ b/src/main/java/org/redisson/core/RSetAsync.java @@ -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 extends RCollectionAsync { + /** + * Removes and returns random element from set + * in async mode + * + * @return + */ + Future removeRandomAsync(); + } diff --git a/src/test/java/org/redisson/RedissonSetTest.java b/src/test/java/org/redisson/RedissonSetTest.java index e360f9e69..ce7f628aa 100644 --- a/src/test/java/org/redisson/RedissonSetTest.java +++ b/src/test/java/org/redisson/RedissonSetTest.java @@ -32,6 +32,19 @@ public class RedissonSetTest extends BaseTest { } + @Test + public void testRemoveRandom() { + RSet 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();