Polishing.

Introduce factory methods on RedisSet for easier construction of RediSet. Add javadoc.
Revise clear method to perform a DEL command instead of empty set intersection.

See #2037
Original Pull Request: #2105
This commit is contained in:
Mark Paluch
2021-06-29 15:37:33 +02:00
committed by Christoph Strobl
parent 90095d1c89
commit b6820f0f61
4 changed files with 109 additions and 89 deletions

View File

@@ -16,11 +16,9 @@
package org.springframework.data.redis.support.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.BoundSetOperations;
@@ -216,10 +214,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
*/
@Override
public void clear() {
// intersect the set with a non existing one
// TODO: find a safer way to clean the set
String randomKey = UUID.randomUUID().toString();
boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey());
boundSetOps.getOperations().delete(getKey());
}
/*

View File

@@ -19,49 +19,27 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.springframework.data.redis.core.RedisOperations;
/**
* Redis extension for the {@link Set} contract. Supports {@link Set} specific operations backed by Redis operations.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
/**
* Intersect this set and another {@link RedisSet}.
* Constructs a new {@link RedisSet} instance.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(RedisSet<?> set);
/**
* Intersect this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
/**
* Union this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
* @since 2.6
*/
Set<E> union(RedisSet<?> set);
/**
* Union this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 1.0
*/
Set<E> union(Collection<? extends RedisSet<?>> sets);
static <E> RedisSet<E> create(String key, RedisOperations<String, E> operations) {
return new DefaultRedisSet<>(key, operations);
}
/**
* Diff this set and another {@link RedisSet}.
@@ -81,58 +59,6 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
*/
Set<E> diff(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}
* @since 1.0
*/
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result
* in destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
* destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Get random element from the set.
*
* @return
* @since 2.6
*/
E randomValue();
/**
* Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
@@ -155,9 +81,97 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
*/
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Intersect this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(RedisSet<?> set);
/**
* Intersect this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}
* @since 1.0
*/
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result
* in destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Get random element from the set.
*
* @return
* @since 2.6
*/
E randomValue();
/**
* @since 1.4
* @return
*/
Iterator<E> scan();
/**
* Union this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 2.6
*/
Set<E> union(RedisSet<?> set);
/**
* Union this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 1.0
*/
Set<E> union(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
* destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
}

View File

@@ -1587,6 +1587,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSAdd() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sMembers("myset"));
@@ -1595,6 +1596,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSAddMultiple() {
actual.add(connection.sAdd("myset", "foo", "bar"));
actual.add(connection.sAdd("myset", "baz"));
actual.add(connection.sMembers("myset"));
@@ -1603,6 +1605,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSCard() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sCard("myset"));
@@ -1611,6 +1614,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSDiff() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sAdd("otherset", "bar"));
@@ -1620,6 +1624,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSDiffStore() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sAdd("otherset", "bar"));
@@ -1630,6 +1635,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSInter() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sAdd("otherset", "bar"));
@@ -1649,6 +1655,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSIsMember() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sIsMember("myset", "foo"));
@@ -1659,6 +1666,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test // GH-2037
@EnabledOnCommand("SMISMEMBER")
void testSMIsMember() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sMIsMember("myset", "foo", "bar", "baz"));
@@ -1667,6 +1675,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSMove() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sAdd("otherset", "bar"));
@@ -1676,6 +1685,7 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
void testSPop() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sPop("myset"));

View File

@@ -72,6 +72,7 @@ public abstract class AbstractRedisSetIntegrationTests<T> extends AbstractRedisC
@ParameterizedRedisTest // GH-2037
@EnabledOnCommand("SMISMEMBER")
void testContainsAll() {
T t1 = getT();
T t2 = getT();
T t3 = getT();