Feature - added addAllCounted() and removeAllCounted() methods to RSet, RSetRx and RSetReactive objects #3797

pull/3826/head
Nikita Koksharov 3 years ago
parent 9a7eb2ea14
commit b2cd714587

@ -250,6 +250,23 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V>, ScanIt
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD_BOOL, args.toArray());
}
@Override
public RFuture<Integer> addAllCountedAsync(Collection<? extends V> c) {
if (c.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> 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<? extends V> c) {
return get(addAllCountedAsync(c));
}
@Override
public boolean retainAll(Collection<?> c) {
return get(retainAllAsync(c));
@ -286,6 +303,24 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V>, ScanIt
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SREM_SINGLE, args.toArray());
}
@Override
public int removeAllCounted(Collection<? extends V> c) {
return get(removeAllCountedAsync(c));
}
@Override
public RFuture<Integer> removeAllCountedAsync(Collection<? extends V> c) {
if (c.isEmpty()) {
return RedissonPromise.newSucceededFuture(0);
}
List<Object> args = new ArrayList<Object>(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));

@ -87,37 +87,37 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
@Override
public RFuture<Boolean> 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<Boolean> 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<Boolean> 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<Boolean> 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<Long> 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<Void> 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<Boolean> 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<V> extends RedissonExpirable implements R
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SADD_BOOL, args.toArray());
}
@Override
public RFuture<Integer> addAllCountedAsync(Collection<? extends V> c) {
List<Object> 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<? extends V> c) {
return get(addAllCountedAsync(c));
}
@Override
public int removeAllCounted(Collection<? extends V> c) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values");
}
@Override
public RFuture<Integer> removeAllCountedAsync(Collection<? extends V> c) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values");
}
@Override
public boolean retainAll(Collection<?> c) {
return get(retainAllAsync(c));

@ -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<V> extends Set<V>, RExpirable, RSetAsync<V>, RSortable<Set<V>> {
/**
* 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<? extends V> 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<? extends V> c);
/**
* Returns <code>RCountDownLatch</code> instance associated with <code>value</code>
*

@ -15,6 +15,7 @@
*/
package org.redisson.api;
import java.util.Collection;
import java.util.Set;
/**
@ -139,4 +140,22 @@ public interface RSetAsync<V> extends RCollectionAsync<V>, RSortableAsync<Set<V>
*/
RFuture<Boolean> 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<Integer> addAllCountedAsync(Collection<? extends V> 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<Integer> removeAllCountedAsync(Collection<? extends V> c);
}

@ -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<V> extends RCollectionReactive<V>, RSortableReactive<Set<V>> {
/**
* 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<Integer> addAllCounted(Collection<? extends V> 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<Integer> removeAllCounted(Collection<? extends V> c);
/**
* Returns <code>RPermitExpirableSemaphore</code> instance associated with <code>value</code>
*

@ -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<V> extends RCollectionRx<V>, RSortableRx<Set<V>> {
/**
* 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<Integer> addAllCounted(Collection<? extends V> 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<Integer> removeAllCounted(Collection<? extends V> c);
/**
* Returns <code>RPermitExpirableSemaphore</code> instance associated with <code>value</code>
*

@ -144,6 +144,7 @@ public interface RedisCommands {
RedisCommand<Set<Object>> SPOP = new RedisCommand<Set<Object>>("SPOP", new ObjectSetReplayDecoder<Object>());
RedisCommand<Object> SPOP_SINGLE = new RedisCommand<Object>("SPOP");
RedisCommand<Boolean> SADD_SINGLE = new RedisCommand<Boolean>("SADD", new BooleanReplayConvertor());
RedisCommand<Integer> SREM = new RedisCommand<>("SREM", new IntegerReplayConvertor());
RedisCommand<Boolean> SREM_SINGLE = new RedisCommand<Boolean>("SREM", new BooleanAmountReplayConvertor());
RedisCommand<Boolean> SMOVE = new RedisCommand<Boolean>("SMOVE", new BooleanReplayConvertor());
RedisCommand<Set<Object>> SMEMBERS = new RedisCommand<Set<Object>>("SMEMBERS", new ObjectSetReplayDecoder<Object>());

@ -42,6 +42,17 @@ public class RedissonSetTest extends BaseTest {
}
@Test
public void testRemoveAllCounted() {
RSet<Integer> 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<String> set = redisson.getSet("list", IntegerCodec.INSTANCE);

Loading…
Cancel
Save