diff --git a/src/main/java/org/redisson/RedissonSet.java b/src/main/java/org/redisson/RedissonSet.java index fc149db07..7303a68cc 100644 --- a/src/main/java/org/redisson/RedissonSet.java +++ b/src/main/java/org/redisson/RedissonSet.java @@ -263,6 +263,58 @@ public class RedissonSet extends RedissonExpirable implements RSet { return commandExecutor.writeAsync(getName(), codec, RedisCommands.SUNION, args.toArray()); } + @Override + public int diff(String... names) { + return get(diffAsync(names)); + } + + @Override + public Future diffAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SDIFFSTORE_INT, args.toArray()); + } + + @Override + public Set readDiff(String... names) { + return get(readDiffAsync(names)); + } + + @Override + public Future> readDiffAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SDIFF, args.toArray()); + } + + @Override + public int intersection(String... names) { + return get(intersectionAsync(names)); + } + + @Override + public Future intersectionAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SINTERSTORE_INT, args.toArray()); + } + + @Override + public Set readIntersection(String... names) { + return get(readIntersectionAsync(names)); + } + + @Override + public Future> readIntersectionAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SINTER, args.toArray()); + } + @Override public void clear() { delete(); diff --git a/src/main/java/org/redisson/RedissonSetMultimapValues.java b/src/main/java/org/redisson/RedissonSetMultimapValues.java index a4cd1513c..629cbd111 100644 --- a/src/main/java/org/redisson/RedissonSetMultimapValues.java +++ b/src/main/java/org/redisson/RedissonSetMultimapValues.java @@ -394,4 +394,56 @@ public class RedissonSetMultimapValues extends RedissonExpirable implements R delete(); } + @Override + public int diff(String... names) { + return get(diffAsync(names)); + } + + @Override + public Future diffAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SDIFFSTORE_INT, args.toArray()); + } + + @Override + public Set readDiff(String... names) { + return get(readDiffAsync(names)); + } + + @Override + public Future> readDiffAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SDIFF, args.toArray()); + } + + @Override + public int intersection(String... names) { + return get(intersectionAsync(names)); + } + + @Override + public Future intersectionAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SINTERSTORE_INT, args.toArray()); + } + + @Override + public Set readIntersection(String... names) { + return get(readIntersectionAsync(names)); + } + + @Override + public Future> readIntersectionAsync(String... names) { + List args = new ArrayList(names.length + 1); + args.add(getName()); + args.addAll(Arrays.asList(names)); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.SINTER, args.toArray()); + } + } diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index 490914c6c..37568cb95 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -129,8 +129,12 @@ public interface RedisCommands { RedisStrictCommand SCARD_INT = new RedisStrictCommand("SCARD", new IntegerReplayConvertor()); RedisStrictCommand SCARD = new RedisStrictCommand("SCARD"); RedisStrictCommand SUNIONSTORE_INT = new RedisStrictCommand("SUNIONSTORE", new IntegerReplayConvertor()); + RedisStrictCommand SDIFFSTORE_INT = new RedisStrictCommand("SDIFFSTORE", new IntegerReplayConvertor()); + RedisStrictCommand SINTERSTORE_INT = new RedisStrictCommand("SINTERSTORE", new IntegerReplayConvertor()); RedisStrictCommand SUNIONSTORE = new RedisStrictCommand("SUNIONSTORE"); RedisCommand> SUNION = new RedisCommand>("SUNION", new ObjectSetReplayDecoder()); + RedisCommand> SDIFF = new RedisCommand>("SDIFF", new ObjectSetReplayDecoder()); + RedisCommand> SINTER = new RedisCommand>("SINTER", new ObjectSetReplayDecoder()); RedisCommand LSET = new RedisCommand("LSET", new VoidReplayConvertor(), 3); RedisCommand LPOP = new RedisCommand("LPOP"); diff --git a/src/main/java/org/redisson/core/RSet.java b/src/main/java/org/redisson/core/RSet.java index d98db75f6..316a1c6ff 100644 --- a/src/main/java/org/redisson/core/RSet.java +++ b/src/main/java/org/redisson/core/RSet.java @@ -68,4 +68,40 @@ public interface RSet extends Set, RExpirable, RSetAsync { */ Set readUnion(String... names); + /** + * Diff sets specified by name and write to current set. + * If current set already exists, it is overwritten. + * + * @param names + * @return + */ + int diff(String... names); + + /** + * Diff sets specified by name with current set. + * Without current set state change. + * + * @param names + * @return + */ + + Set readDiff(String... names); + /** + * Intersection sets specified by name and write to current set. + * If current set already exists, it is overwritten. + * + * @param names + * @return + */ + int intersection(String... names); + + /** + * Intersection sets specified by name with current set. + * Without current set state change. + * + * @param names + * @return + */ + Set readIntersection(String... names); + } diff --git a/src/main/java/org/redisson/core/RSetAsync.java b/src/main/java/org/redisson/core/RSetAsync.java index f74512191..214bebc7d 100644 --- a/src/main/java/org/redisson/core/RSetAsync.java +++ b/src/main/java/org/redisson/core/RSetAsync.java @@ -71,4 +71,40 @@ public interface RSetAsync extends RCollectionAsync { */ Future> readUnionAsync(String... keys); + /** + * Diff sets specified by name and write to current set. + * If current set already exists, it is overwritten. + * + * @param names + * @return + */ + Future diffAsync(String... keys); + + /** + * Diff sets specified by name with current set. + * Without current set state change. + * + * @param names + * @return + */ + Future> readDiffAsync(String... keys); + + /** + * Intersection sets specified by name and write to current set. + * If current set already exists, it is overwritten. + * + * @param names + * @return + */ + Future intersectionAsync(String... keys); + + /** + * Intersection sets specified by name with current set. + * Without current set state change. + * + * @param names + * @return + */ + Future> readIntersectionAsync(String... keys); + } diff --git a/src/test/java/org/redisson/RedissonSetTest.java b/src/test/java/org/redisson/RedissonSetTest.java index 0ee987094..4e0fac31f 100644 --- a/src/test/java/org/redisson/RedissonSetTest.java +++ b/src/test/java/org/redisson/RedissonSetTest.java @@ -334,7 +334,81 @@ public class RedissonSetTest extends BaseTest { assertThat(set).containsOnly(5, 6); } + @Test + public void testDiff() { + RSet set = redisson.getSet("set"); + set.add(5); + set.add(6); + RSet set1 = redisson.getSet("set1"); + set1.add(1); + set1.add(2); + set1.add(3); + RSet set2 = redisson.getSet("set2"); + set2.add(3); + set2.add(4); + set2.add(5); + + assertThat(set.diff("set1", "set2")).isEqualTo(2); + assertThat(set).containsOnly(1, 2); + } + + @Test + public void testReadDiff() { + RSet set = redisson.getSet("set"); + set.add(5); + set.add(7); + set.add(6); + RSet set1 = redisson.getSet("set1"); + set1.add(1); + set1.add(2); + set1.add(5); + RSet set2 = redisson.getSet("set2"); + set2.add(3); + set2.add(4); + set2.add(5); + + assertThat(set.readDiff("set1", "set2")).containsOnly(7, 6); + assertThat(set).containsOnly(6, 5, 7); + } + + @Test + public void testIntersection() { + RSet set = redisson.getSet("set"); + set.add(5); + set.add(6); + RSet set1 = redisson.getSet("set1"); + set1.add(1); + set1.add(2); + set1.add(3); + RSet set2 = redisson.getSet("set2"); + set2.add(3); + set2.add(4); + set2.add(5); + + assertThat(set.intersection("set1", "set2")).isEqualTo(1); + assertThat(set).containsOnly(3); + } + + @Test + public void testReadIntersection() { + RSet set = redisson.getSet("set"); + set.add(5); + set.add(7); + set.add(6); + RSet set1 = redisson.getSet("set1"); + set1.add(1); + set1.add(2); + set1.add(5); + RSet set2 = redisson.getSet("set2"); + set2.add(3); + set2.add(4); + set2.add(5); + + assertThat(set.readIntersection("set1", "set2")).containsOnly(5); + assertThat(set).containsOnly(6, 5, 7); + } + @Test public void testMove() throws Exception { RSet set = redisson.getSet("set");