diff --git a/redisson/src/main/java/org/redisson/RedissonSet.java b/redisson/src/main/java/org/redisson/RedissonSet.java index d62aef93c..03109c507 100644 --- a/redisson/src/main/java/org/redisson/RedissonSet.java +++ b/redisson/src/main/java/org/redisson/RedissonSet.java @@ -250,6 +250,23 @@ public class RedissonSet extends RedissonExpirable implements RSet, ScanIt return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD_BOOL, args.toArray()); } + @Override + public RFuture addAllCountedAsync(Collection c) { + if (c.isEmpty()) { + return RedissonPromise.newSucceededFuture(0); + } + + List args = new ArrayList<>(c.size() + 1); + args.add(getRawName()); + encode(args, c); + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD, args.toArray()); + } + + @Override + public int addAllCounted(Collection c) { + return get(addAllCountedAsync(c)); + } + @Override public boolean retainAll(Collection c) { return get(retainAllAsync(c)); @@ -286,6 +303,24 @@ public class RedissonSet extends RedissonExpirable implements RSet, ScanIt return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SREM_SINGLE, args.toArray()); } + @Override + public int removeAllCounted(Collection c) { + return get(removeAllCountedAsync(c)); + } + + @Override + public RFuture removeAllCountedAsync(Collection c) { + if (c.isEmpty()) { + return RedissonPromise.newSucceededFuture(0); + } + + List args = new ArrayList(c.size() + 1); + args.add(getRawName()); + encode(args, c); + + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SREM, args.toArray()); + } + @Override public boolean removeAll(Collection c) { return get(removeAllAsync(c)); diff --git a/redisson/src/main/java/org/redisson/RedissonSetMultimapValues.java b/redisson/src/main/java/org/redisson/RedissonSetMultimapValues.java index 4eb9fb2b0..b2c6af4d1 100644 --- a/redisson/src/main/java/org/redisson/RedissonSetMultimapValues.java +++ b/redisson/src/main/java/org/redisson/RedissonSetMultimapValues.java @@ -87,37 +87,37 @@ public class RedissonSetMultimapValues extends RedissonExpirable implements R @Override public RFuture clearExpireAsync() { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture expireAsync(Instant instant) { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture expireAsync(long timeToLive, TimeUnit timeUnit) { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture expireAtAsync(long timestamp) { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture remainTimeToLiveAsync() { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture renameAsync(String newName) { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override public RFuture renamenxAsync(String newName) { - throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set"); + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); } @Override @@ -416,6 +416,29 @@ public class RedissonSetMultimapValues extends RedissonExpirable implements R return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD_BOOL, args.toArray()); } + @Override + public RFuture addAllCountedAsync(Collection c) { + List args = new ArrayList<>(c.size() + 1); + args.add(getRawName()); + encodeMapValues(args, c); + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD, args.toArray()); + } + + @Override + public int addAllCounted(Collection c) { + return get(addAllCountedAsync(c)); + } + + @Override + public int removeAllCounted(Collection c) { + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); + } + + @Override + public RFuture removeAllCountedAsync(Collection c) { + throw new UnsupportedOperationException("This operation is not supported for SetMultimap values"); + } + @Override public boolean retainAll(Collection c) { return get(retainAllAsync(c)); diff --git a/redisson/src/main/java/org/redisson/api/RSet.java b/redisson/src/main/java/org/redisson/api/RSet.java index 7aa55396d..461276990 100644 --- a/redisson/src/main/java/org/redisson/api/RSet.java +++ b/redisson/src/main/java/org/redisson/api/RSet.java @@ -17,6 +17,7 @@ package org.redisson.api; import org.redisson.api.mapreduce.RCollectionMapReduce; +import java.util.Collection; import java.util.Iterator; import java.util.Set; import java.util.stream.Stream; @@ -30,6 +31,24 @@ import java.util.stream.Stream; */ public interface RSet extends Set, RExpirable, RSetAsync, RSortable> { + /** + * Adds all elements contained in the specified collection. + * Returns number of added elements. + * + * @param c - collection of elements to add + * @return number of added elements + */ + int addAllCounted(Collection c); + + /** + * Removes all elements contained in the specified collection. + * Returns number of removed elements. + * + * @param c - collection of elements to add + * @return number of removed elements + */ + int removeAllCounted(Collection c); + /** * Returns RCountDownLatch instance associated with value * diff --git a/redisson/src/main/java/org/redisson/api/RSetAsync.java b/redisson/src/main/java/org/redisson/api/RSetAsync.java index 2adbcab83..56f78dac0 100644 --- a/redisson/src/main/java/org/redisson/api/RSetAsync.java +++ b/redisson/src/main/java/org/redisson/api/RSetAsync.java @@ -15,6 +15,7 @@ */ package org.redisson.api; +import java.util.Collection; import java.util.Set; /** @@ -139,4 +140,22 @@ public interface RSetAsync extends RCollectionAsync, RSortableAsync */ RFuture tryAddAsync(V... values); + /** + * Adds all elements contained in the specified collection. + * Returns number of added elements. + * + * @param c - collection of elements to add + * @return number of added elements + */ + RFuture addAllCountedAsync(Collection c); + + /** + * Removes all elements contained in the specified collection. + * Returns number of removed elements. + * + * @param c - collection of elements to add + * @return number of removed elements + */ + RFuture removeAllCountedAsync(Collection c); + } diff --git a/redisson/src/main/java/org/redisson/api/RSetReactive.java b/redisson/src/main/java/org/redisson/api/RSetReactive.java index 4ae42b47f..bb962da5d 100644 --- a/redisson/src/main/java/org/redisson/api/RSetReactive.java +++ b/redisson/src/main/java/org/redisson/api/RSetReactive.java @@ -18,6 +18,7 @@ package org.redisson.api; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.util.Collection; import java.util.Set; /** @@ -29,6 +30,24 @@ import java.util.Set; */ public interface RSetReactive extends RCollectionReactive, RSortableReactive> { + /** + * Adds all elements contained in the specified collection. + * Returns number of added elements. + * + * @param c - collection of elements to add + * @return number of added elements + */ + Mono addAllCounted(Collection c); + + /** + * Removes all elements contained in the specified collection. + * Returns number of removed elements. + * + * @param c - collection of elements to add + * @return number of removed elements + */ + Mono removeAllCounted(Collection c); + /** * Returns RPermitExpirableSemaphore instance associated with value * diff --git a/redisson/src/main/java/org/redisson/api/RSetRx.java b/redisson/src/main/java/org/redisson/api/RSetRx.java index becedd368..ef9b3a220 100644 --- a/redisson/src/main/java/org/redisson/api/RSetRx.java +++ b/redisson/src/main/java/org/redisson/api/RSetRx.java @@ -15,6 +15,7 @@ */ package org.redisson.api; +import java.util.Collection; import java.util.Set; import io.reactivex.rxjava3.core.Flowable; @@ -30,6 +31,24 @@ import io.reactivex.rxjava3.core.Single; */ public interface RSetRx extends RCollectionRx, RSortableRx> { + /** + * Adds all elements contained in the specified collection. + * Returns number of added elements. + * + * @param c - collection of elements to add + * @return number of added elements + */ + Single addAllCounted(Collection c); + + /** + * Removes all elements contained in the specified collection. + * Returns number of removed elements. + * + * @param c - collection of elements to add + * @return number of removed elements + */ + Single removeAllCounted(Collection c); + /** * Returns RPermitExpirableSemaphore instance associated with value * diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index 89006465f..717a41210 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -144,6 +144,7 @@ public interface RedisCommands { RedisCommand> SPOP = new RedisCommand>("SPOP", new ObjectSetReplayDecoder()); RedisCommand SPOP_SINGLE = new RedisCommand("SPOP"); RedisCommand SADD_SINGLE = new RedisCommand("SADD", new BooleanReplayConvertor()); + RedisCommand SREM = new RedisCommand<>("SREM", new IntegerReplayConvertor()); RedisCommand SREM_SINGLE = new RedisCommand("SREM", new BooleanAmountReplayConvertor()); RedisCommand SMOVE = new RedisCommand("SMOVE", new BooleanReplayConvertor()); RedisCommand> SMEMBERS = new RedisCommand>("SMEMBERS", new ObjectSetReplayDecoder()); diff --git a/redisson/src/test/java/org/redisson/RedissonSetTest.java b/redisson/src/test/java/org/redisson/RedissonSetTest.java index c1586b8d6..b82f8bd0f 100644 --- a/redisson/src/test/java/org/redisson/RedissonSetTest.java +++ b/redisson/src/test/java/org/redisson/RedissonSetTest.java @@ -42,6 +42,17 @@ public class RedissonSetTest extends BaseTest { } + @Test + public void testRemoveAllCounted() { + RSet set = redisson.getSet("list", IntegerCodec.INSTANCE); + set.add(0); + set.add(1); + set.add(2); + set.add(3); + + assertThat(set.removeAllCounted(Arrays.asList(1, 2, 3, 4, 5))).isEqualTo(3); + } + @Test public void testTryAdd() { RSet set = redisson.getSet("list", IntegerCodec.INSTANCE);