Merge pull request #3899 from slovvik/Implement_new_SMISMEMBER_command

Added new SMISMEMBER command
pull/4748/head
Nikita Koksharov 2 years ago committed by GitHub
commit e9165903b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -386,6 +386,34 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V>, ScanIt
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SREM, args.toArray());
}
@Override
public RFuture<List<V>> containsEachAsync(Collection<V> c) {
if (c.isEmpty()) {
return RedissonPromise.newSucceededFuture(Collections.emptyList());
}
List<Object> args = new ArrayList<Object>(c.size() + 1);
args.add(getRawName());
encode(args, c);
RFuture<List<Long>> future = commandExecutor.readAsync(getRawName(), codec, RedisCommands.SMISMEMBER, args.toArray());
List<V> keysToCheck = new ArrayList<>(c);
RPromise<List<V>> result = new RedissonPromise<>();
future.onComplete((res, e) -> {
if (e != null) {
result.tryFailure(e);
return;
}
List<V> containedKeys = new ArrayList<>();
for (int i = 0; i < res.size(); i++) {
if (res.get(i) == 1) {
containedKeys.add(keysToCheck.get(i));
}
}
result.trySuccess(containedKeys);
});
return result;
}
@Override
public boolean removeAll(Collection<?> c) {
return get(removeAllAsync(c));
@ -728,6 +756,11 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V>, ScanIt
return get(tryAddAsync(values));
}
@Override
public List<V> containsEach(Collection<V> c) {
return get(containsEachAsync(c));
}
@Override
public RFuture<Boolean> tryAddAsync(V... values) {
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,

@ -67,6 +67,11 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
return get(tryAddAsync(values));
}
@Override
public List<V> containsEach(Collection<V> c) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values");
}
@Override
public RFuture<Boolean> tryAddAsync(V... values) {
return set.tryAddAsync(values);
@ -498,6 +503,11 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values");
}
@Override
public RFuture<List<V>> containsEachAsync(Collection<V> c) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values");
}
@Override
public boolean retainAll(Collection<?> c) {
return get(retainAllAsync(c));

@ -19,6 +19,7 @@ import org.redisson.api.mapreduce.RCollectionMapReduce;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
@ -324,4 +325,13 @@ public interface RSet<V> extends Set<V>, RExpirable, RSetAsync<V>, RSortable<Set
*/
boolean tryAdd(V... values);
/**
* Check if each element is contained in the specified collection.
* Returns contained elements.
*
* @param c - collection to check
* @return contained elements
*/
List<V> containsEach(Collection<V> c);
}

@ -16,6 +16,7 @@
package org.redisson.api;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
@ -179,4 +180,13 @@ public interface RSetAsync<V> extends RCollectionAsync<V>, RSortableAsync<Set<V>
*/
RFuture<Integer> removeAllCountedAsync(Collection<? extends V> c);
/**
* Check if each element is contained in the specified collection.
* Returns contained elements.
*
* @param c - collection to check
* @return contained elements
*/
RFuture<List<V>> containsEachAsync(Collection<V> c);
}

@ -215,6 +215,7 @@ public interface RedisCommands {
RedisCommand<Set<Object>> SUNION = new RedisCommand<Set<Object>>("SUNION", new ObjectSetReplayDecoder<Object>());
RedisCommand<Set<Object>> SDIFF = new RedisCommand<Set<Object>>("SDIFF", new ObjectSetReplayDecoder<Object>());
RedisCommand<Set<Object>> SINTER = new RedisCommand<Set<Object>>("SINTER", new ObjectSetReplayDecoder<Object>());
RedisCommand<List<Long>> SMISMEMBER = new RedisCommand<List<Long>>("SMISMEMBER", new ObjectListReplayDecoder<Long>());
RedisStrictCommand<Long> LPOS = new RedisStrictCommand<>("LPOS");
RedisCommand<Void> LSET = new RedisCommand<Void>("LSET", new VoidReplayConvertor());

@ -16,6 +16,7 @@ import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.assertj.core.api.ListAssert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.redisson.ClusterRunner.ClusterProcesses;
@ -26,6 +27,7 @@ import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.redisson.api.SortOrder;
import org.redisson.client.codec.IntegerCodec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.connection.balancer.RandomLoadBalancer;
@ -46,6 +48,29 @@ public class RedissonSetTest extends BaseTest {
}
@Test
public void testContainsEach() {
RSet<Integer> set = redisson.getSet("list", IntegerCodec.INSTANCE);
set.add(0);
set.add(1);
assertThat(set.containsEach(Collections.emptySet())).isEmpty();
assertThat(set.containsEach(Arrays.asList(0, 1)))
.hasSize(2)
.containsOnly(0, 1);
assertThat(set.containsEach(Arrays.asList(0, 1, 2)))
.hasSize(2)
.containsOnly(0, 1);
assertThat(set.containsEach(Arrays.asList(0, 1, 0, 2)))
.hasSize(3)
.containsOnly(0, 1, 0);
assertThat(set.containsEach(Arrays.asList(2, 3, 4)))
.hasSize(0);
}
@Test
public void testRemoveAllCounted() {
RSet<Integer> set = redisson.getSet("list", IntegerCodec.INSTANCE);

Loading…
Cancel
Save