From f225b3533dd2a9baf996c39d5eee3e7767e6d178 Mon Sep 17 00:00:00 2001 From: Thomas Rausch Date: Sat, 9 Jan 2016 11:44:22 +0100 Subject: [PATCH] Support for SMOVE command in RSet, RSetAsync, RSetReactive. #366 --- src/main/java/org/redisson/RedissonSet.java | 10 ++++++ .../java/org/redisson/api/RSetReactive.java | 9 ++++++ .../client/protocol/RedisCommands.java | 1 + src/main/java/org/redisson/core/RSet.java | 10 ++++++ .../java/org/redisson/core/RSetAsync.java | 10 ++++++ .../reactive/RedissonSetReactive.java | 5 +++ .../org/redisson/RedissonSetReactiveTest.java | 31 +++++++++++++++++++ .../java/org/redisson/RedissonSetTest.java | 30 ++++++++++++++++++ 8 files changed, 106 insertions(+) diff --git a/src/main/java/org/redisson/RedissonSet.java b/src/main/java/org/redisson/RedissonSet.java index 831f1e2e7..fd480eb86 100644 --- a/src/main/java/org/redisson/RedissonSet.java +++ b/src/main/java/org/redisson/RedissonSet.java @@ -189,6 +189,16 @@ public class RedissonSet extends RedissonExpirable implements RSet { return get(removeAsync((V)value)); } + @Override + public Future moveAsync(String destination, V member) { + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SMOVE, getName(), destination, member); + } + + @Override + public boolean move(String destination, V member) { + return get(moveAsync(destination, member)); + } + @Override public boolean containsAll(Collection c) { return get(containsAllAsync(c)); diff --git a/src/main/java/org/redisson/api/RSetReactive.java b/src/main/java/org/redisson/api/RSetReactive.java index d433b8621..8e6e54148 100644 --- a/src/main/java/org/redisson/api/RSetReactive.java +++ b/src/main/java/org/redisson/api/RSetReactive.java @@ -34,4 +34,13 @@ public interface RSetReactive extends RCollectionReactive { */ Publisher removeRandom(); + /** + * Move a member from this set to the given destination set in async mode. + * + * @param destination the destination set + * @param member the member to move + * @return true if the element is moved, false if the element is not a + * member of this set or no operation was performed + */ + Publisher move(String destination, V member); } diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index 63acfcad4..dfe80442f 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -108,6 +108,7 @@ public interface RedisCommands { 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 SMOVE = new RedisCommand("SMOVE", new BooleanReplayConvertor(), 3); RedisCommand> SMEMBERS = new RedisCommand>("SMEMBERS", new ObjectListReplayDecoder()); RedisCommand> SSCAN = new RedisCommand>("SSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder(), new ListScanResultReplayDecoder()), ValueType.OBJECT); RedisCommand> EVAL_SSCAN = new RedisCommand>("EVAL", new NestedMultiDecoder(new ObjectListReplayDecoder(), new ListScanResultReplayDecoder()), ValueType.OBJECT); diff --git a/src/main/java/org/redisson/core/RSet.java b/src/main/java/org/redisson/core/RSet.java index a46f3e9d2..e4961a9b8 100644 --- a/src/main/java/org/redisson/core/RSet.java +++ b/src/main/java/org/redisson/core/RSet.java @@ -33,4 +33,14 @@ public interface RSet extends Set, RExpirable, RSetAsync { */ V removeRandom(); + /** + * Move a member from this set to the given destination set in. + * + * @param destination the destination set + * @param member the member to move + * @return true if the element is moved, false if the element is not a + * member of this set or no operation was performed + */ + boolean move(String destination, V member); + } diff --git a/src/main/java/org/redisson/core/RSetAsync.java b/src/main/java/org/redisson/core/RSetAsync.java index 15cf00a31..9ecdd9eb9 100644 --- a/src/main/java/org/redisson/core/RSetAsync.java +++ b/src/main/java/org/redisson/core/RSetAsync.java @@ -34,4 +34,14 @@ public interface RSetAsync extends RCollectionAsync { */ Future removeRandomAsync(); + /** + * Move a member from this set to the given destination set in async mode. + * + * @param destination the destination set + * @param member the member to move + * @return true if the element is moved, false if the element is not a + * member of this set or no operation was performed + */ + Future moveAsync(String destination, V member); + } diff --git a/src/main/java/org/redisson/reactive/RedissonSetReactive.java b/src/main/java/org/redisson/reactive/RedissonSetReactive.java index 016a066b8..f561e9d5d 100644 --- a/src/main/java/org/redisson/reactive/RedissonSetReactive.java +++ b/src/main/java/org/redisson/reactive/RedissonSetReactive.java @@ -79,6 +79,11 @@ public class RedissonSetReactive extends RedissonExpirableReactive implements return commandExecutor.writeReactive(getName(), codec, RedisCommands.SREM_SINGLE, getName(), o); } + @Override + public Publisher move(String destination, V member) { + return commandExecutor.writeReactive(getName(), codec, RedisCommands.SMOVE, getName(), destination, member); + } + @Override public Publisher containsAll(Collection c) { return commandExecutor.evalReadReactive(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, diff --git a/src/test/java/org/redisson/RedissonSetReactiveTest.java b/src/test/java/org/redisson/RedissonSetReactiveTest.java index 578fe517e..5a356cca9 100644 --- a/src/test/java/org/redisson/RedissonSetReactiveTest.java +++ b/src/test/java/org/redisson/RedissonSetReactiveTest.java @@ -221,4 +221,35 @@ public class RedissonSetReactiveTest extends BaseReactiveTest { Assert.assertFalse(sync(set.retainAll(Arrays.asList(1, 2)))); // nothing changed Assert.assertThat(sync(set), Matchers.containsInAnyOrder(1, 2)); } + + + @Test + public void testMove() throws Exception { + RSetReactive set = redisson.getSet("set"); + RSetReactive otherSet = redisson.getSet("otherSet"); + + sync(set.add(1)); + sync(set.add(2)); + + Assert.assertTrue(sync(set.move("otherSet", 1))); + + Assert.assertEquals(1, sync(set.size()).intValue()); + Assert.assertThat(sync(set), Matchers.contains(2)); + + Assert.assertEquals(1, sync(otherSet.size()).intValue()); + Assert.assertThat(sync(otherSet), Matchers.contains(1)); + } + + @Test + public void testMoveNoMember() throws Exception { + RSetReactive set = redisson.getSet("set"); + RSetReactive otherSet = redisson.getSet("otherSet"); + + set.add(1); + + Assert.assertFalse(sync(set.move("otherSet", 2))); + + Assert.assertEquals(1, sync(set.size()).intValue()); + Assert.assertEquals(0, sync(otherSet.size()).intValue()); + } } diff --git a/src/test/java/org/redisson/RedissonSetTest.java b/src/test/java/org/redisson/RedissonSetTest.java index f31f725fb..648ea7586 100644 --- a/src/test/java/org/redisson/RedissonSetTest.java +++ b/src/test/java/org/redisson/RedissonSetTest.java @@ -288,4 +288,34 @@ public class RedissonSetTest extends BaseTest { Assert.assertFalse(set.retainAll(Arrays.asList(1, 2))); // nothing changed Assert.assertThat(set, Matchers.containsInAnyOrder(1, 2)); } + + @Test + public void testMove() throws Exception { + RSet set = redisson.getSet("set"); + RSet otherSet = redisson.getSet("otherSet"); + + set.add(1); + set.add(2); + + Assert.assertTrue(set.move("otherSet", 1)); + + Assert.assertEquals(1, set.size()); + Assert.assertThat(set, Matchers.contains(2)); + + Assert.assertEquals(1, otherSet.size()); + Assert.assertThat(otherSet, Matchers.contains(1)); + } + + @Test + public void testMoveNoMember() throws Exception { + RSet set = redisson.getSet("set"); + RSet otherSet = redisson.getSet("otherSet"); + + set.add(1); + + Assert.assertFalse(set.move("otherSet", 2)); + + Assert.assertEquals(1, set.size()); + Assert.assertEquals(0, otherSet.size()); + } }