diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java index abc04d67d..7f404dcf5 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java @@ -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 extends AbstractRedisCollection 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()); } /* diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java b/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java index 84895dc25..c4d650cb1 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java @@ -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 extends RedisCollection, Set { /** - * 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 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 intersect(Collection> 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 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 union(Collection> sets); + static RedisSet create(String key, RedisOperations operations) { + return new DefaultRedisSet<>(key, operations); + } /** * Diff this set and another {@link RedisSet}. @@ -81,58 +59,6 @@ public interface RedisSet extends RedisCollection, Set { */ Set diff(Collection> 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 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 intersectAndStore(Collection> 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 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 unionAndStore(Collection> 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 extends RedisCollection, Set { */ RedisSet diffAndStore(Collection> 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 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 intersect(Collection> 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 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 intersectAndStore(Collection> sets, String destKey); + + /** + * Get random element from the set. + * + * @return + * @since 2.6 + */ + E randomValue(); + /** * @since 1.4 * @return */ Iterator 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 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 union(Collection> 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 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 unionAndStore(Collection> sets, String destKey); } diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java index 212d4ee7f..1e663e83c 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java @@ -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")); diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java index 848101f9c..9a0b9f2e1 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java @@ -72,6 +72,7 @@ public abstract class AbstractRedisSetIntegrationTests extends AbstractRedisC @ParameterizedRedisTest // GH-2037 @EnabledOnCommand("SMISMEMBER") void testContainsAll() { + T t1 = getT(); T t2 = getT(); T t3 = getT();