Skip to content

Commit b6820f0

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent 90095d1 commit b6820f0

File tree

4 files changed

+73
-53
lines changed

4 files changed

+73
-53
lines changed

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
package org.springframework.data.redis.support.collections;
1717

1818
import java.util.Collection;
19-
import java.util.Collections;
2019
import java.util.Iterator;
2120
import java.util.Map;
2221
import java.util.Set;
23-
import java.util.UUID;
2422

2523
import org.springframework.data.redis.connection.DataType;
2624
import org.springframework.data.redis.core.BoundSetOperations;
@@ -216,10 +214,7 @@ public boolean add(E e) {
216214
*/
217215
@Override
218216
public void clear() {
219-
// intersect the set with a non existing one
220-
// TODO: find a safer way to clean the set
221-
String randomKey = UUID.randomUUID().toString();
222-
boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey());
217+
boundSetOps.getOperations().delete(getKey());
223218
}
224219

225220
/*

src/main/java/org/springframework/data/redis/support/collections/RedisSet.java

+61-47
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,85 @@
1919
import java.util.Iterator;
2020
import java.util.Set;
2121

22+
import org.springframework.data.redis.core.RedisOperations;
23+
2224
/**
2325
* Redis extension for the {@link Set} contract. Supports {@link Set} specific operations backed by Redis operations.
2426
*
2527
* @author Costin Leau
2628
* @author Christoph Strobl
29+
* @author Mark Paluch
2730
*/
2831
public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
2932

3033
/**
31-
* Intersect this set and another {@link RedisSet}.
34+
* Constructs a new {@link RedisSet} instance.
35+
*
36+
* @param key Redis key of this set.
37+
* @param operations {@link RedisOperations} for the value type of this set.
38+
* @since 2.6
39+
*/
40+
static <E> RedisSet<E> create(String key, RedisOperations<String, E> operations) {
41+
return new DefaultRedisSet<>(key, operations);
42+
}
43+
44+
/**
45+
* Diff this set and another {@link RedisSet}.
3246
*
3347
* @param set must not be {@literal null}.
34-
* @return a {@link Set} containing the intersecting values.
48+
* @return a {@link Set} containing the values that differ.
3549
* @since 1.0
3650
*/
37-
Set<E> intersect(RedisSet<?> set);
51+
Set<E> diff(RedisSet<?> set);
3852

3953
/**
40-
* Intersect this set and other {@link RedisSet}s.
54+
* Diff this set and other {@link RedisSet}s.
4155
*
4256
* @param sets must not be {@literal null}.
43-
* @return a {@link Set} containing the intersecting values.
57+
* @return a {@link Set} containing the values that differ.
4458
* @since 1.0
4559
*/
46-
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
60+
Set<E> diff(Collection<? extends RedisSet<?>> sets);
4761

4862
/**
49-
* Union this set and another {@link RedisSet}.
63+
* Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination
64+
* {@code destKey}.
5065
*
5166
* @param set must not be {@literal null}.
52-
* @return a {@link Set} containing the combined values.
53-
* @since 2.6
67+
* @param destKey must not be {@literal null}.
68+
* @return a new {@link RedisSet} pointing at {@code destKey}.
69+
* @since 1.0
5470
*/
55-
Set<E> union(RedisSet<?> set);
71+
RedisSet<E> diffAndStore(RedisSet<?> set, String destKey);
5672

5773
/**
58-
* Union this set and other {@link RedisSet}s.
74+
* Create a new {@link RedisSet} by diffing this sorted set and the collection {@link RedisSet} and store result in
75+
* destination {@code destKey}.
5976
*
6077
* @param sets must not be {@literal null}.
61-
* @return a {@link Set} containing the combined values.
78+
* @param destKey must not be {@literal null}.
79+
* @return a new {@link RedisSet} pointing at {@code destKey}.
6280
* @since 1.0
6381
*/
64-
Set<E> union(Collection<? extends RedisSet<?>> sets);
82+
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
6583

6684
/**
67-
* Diff this set and another {@link RedisSet}.
85+
* Intersect this set and another {@link RedisSet}.
6886
*
6987
* @param set must not be {@literal null}.
70-
* @return a {@link Set} containing the values that differ.
88+
* @return a {@link Set} containing the intersecting values.
7189
* @since 1.0
7290
*/
73-
Set<E> diff(RedisSet<?> set);
91+
Set<E> intersect(RedisSet<?> set);
7492

7593
/**
76-
* Diff this set and other {@link RedisSet}s.
94+
* Intersect this set and other {@link RedisSet}s.
7795
*
7896
* @param sets must not be {@literal null}.
79-
* @return a {@link Set} containing the values that differ.
97+
* @return a {@link Set} containing the intersecting values.
8098
* @since 1.0
8199
*/
82-
Set<E> diff(Collection<? extends RedisSet<?>> sets);
100+
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
83101

84102
/**
85103
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
@@ -104,60 +122,56 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
104122
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
105123

106124
/**
107-
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
108-
* {@code destKey}.
125+
* Get random element from the set.
109126
*
110-
* @param set must not be {@literal null}.
111-
* @param destKey must not be {@literal null}.
112-
* @return a new {@link RedisSet} pointing at {@code destKey}.
113-
* @since 1.0
127+
* @return
128+
* @since 2.6
114129
*/
115-
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
130+
E randomValue();
116131

117132
/**
118-
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
119-
* destination {@code destKey}.
120-
*
121-
* @param sets must not be {@literal null}.
122-
* @param destKey must not be {@literal null}.
123-
* @return a new {@link RedisSet} pointing at {@code destKey}.
124-
* @since 1.0
133+
* @since 1.4
134+
* @return
125135
*/
126-
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
136+
Iterator<E> scan();
127137

128138
/**
129-
* Get random element from the set.
139+
* Union this set and another {@link RedisSet}.
130140
*
131-
* @return
141+
* @param set must not be {@literal null}.
142+
* @return a {@link Set} containing the combined values.
132143
* @since 2.6
133144
*/
134-
E randomValue();
145+
Set<E> union(RedisSet<?> set);
135146

136147
/**
137-
* Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination
148+
* Union this set and other {@link RedisSet}s.
149+
*
150+
* @param sets must not be {@literal null}.
151+
* @return a {@link Set} containing the combined values.
152+
* @since 1.0
153+
*/
154+
Set<E> union(Collection<? extends RedisSet<?>> sets);
155+
156+
/**
157+
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
138158
* {@code destKey}.
139159
*
140160
* @param set must not be {@literal null}.
141161
* @param destKey must not be {@literal null}.
142162
* @return a new {@link RedisSet} pointing at {@code destKey}.
143163
* @since 1.0
144164
*/
145-
RedisSet<E> diffAndStore(RedisSet<?> set, String destKey);
165+
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
146166

147167
/**
148-
* Create a new {@link RedisSet} by diffing this sorted set and the collection {@link RedisSet} and store result in
168+
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
149169
* destination {@code destKey}.
150170
*
151171
* @param sets must not be {@literal null}.
152172
* @param destKey must not be {@literal null}.
153173
* @return a new {@link RedisSet} pointing at {@code destKey}.
154174
* @since 1.0
155175
*/
156-
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
157-
158-
/**
159-
* @since 1.4
160-
* @return
161-
*/
162-
Iterator<E> scan();
176+
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
163177
}

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ void lPosNonExisting() {
15871587

15881588
@Test
15891589
void testSAdd() {
1590+
15901591
actual.add(connection.sAdd("myset", "foo"));
15911592
actual.add(connection.sAdd("myset", "bar"));
15921593
actual.add(connection.sMembers("myset"));
@@ -1595,6 +1596,7 @@ void testSAdd() {
15951596

15961597
@Test
15971598
void testSAddMultiple() {
1599+
15981600
actual.add(connection.sAdd("myset", "foo", "bar"));
15991601
actual.add(connection.sAdd("myset", "baz"));
16001602
actual.add(connection.sMembers("myset"));
@@ -1603,6 +1605,7 @@ void testSAddMultiple() {
16031605

16041606
@Test
16051607
void testSCard() {
1608+
16061609
actual.add(connection.sAdd("myset", "foo"));
16071610
actual.add(connection.sAdd("myset", "bar"));
16081611
actual.add(connection.sCard("myset"));
@@ -1611,6 +1614,7 @@ void testSCard() {
16111614

16121615
@Test
16131616
void testSDiff() {
1617+
16141618
actual.add(connection.sAdd("myset", "foo"));
16151619
actual.add(connection.sAdd("myset", "bar"));
16161620
actual.add(connection.sAdd("otherset", "bar"));
@@ -1620,6 +1624,7 @@ void testSDiff() {
16201624

16211625
@Test
16221626
void testSDiffStore() {
1627+
16231628
actual.add(connection.sAdd("myset", "foo"));
16241629
actual.add(connection.sAdd("myset", "bar"));
16251630
actual.add(connection.sAdd("otherset", "bar"));
@@ -1630,6 +1635,7 @@ void testSDiffStore() {
16301635

16311636
@Test
16321637
void testSInter() {
1638+
16331639
actual.add(connection.sAdd("myset", "foo"));
16341640
actual.add(connection.sAdd("myset", "bar"));
16351641
actual.add(connection.sAdd("otherset", "bar"));
@@ -1649,6 +1655,7 @@ void testSInterStore() {
16491655

16501656
@Test
16511657
void testSIsMember() {
1658+
16521659
actual.add(connection.sAdd("myset", "foo"));
16531660
actual.add(connection.sAdd("myset", "bar"));
16541661
actual.add(connection.sIsMember("myset", "foo"));
@@ -1659,6 +1666,7 @@ void testSIsMember() {
16591666
@Test // GH-2037
16601667
@EnabledOnCommand("SMISMEMBER")
16611668
void testSMIsMember() {
1669+
16621670
actual.add(connection.sAdd("myset", "foo"));
16631671
actual.add(connection.sAdd("myset", "bar"));
16641672
actual.add(connection.sMIsMember("myset", "foo", "bar", "baz"));
@@ -1667,6 +1675,7 @@ void testSMIsMember() {
16671675

16681676
@Test
16691677
void testSMove() {
1678+
16701679
actual.add(connection.sAdd("myset", "foo"));
16711680
actual.add(connection.sAdd("myset", "bar"));
16721681
actual.add(connection.sAdd("otherset", "bar"));
@@ -1676,6 +1685,7 @@ void testSMove() {
16761685

16771686
@Test
16781687
void testSPop() {
1688+
16791689
actual.add(connection.sAdd("myset", "foo"));
16801690
actual.add(connection.sAdd("myset", "bar"));
16811691
actual.add(connection.sPop("myset"));

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private RedisSet<T> createSetFor(String key) {
7272
@ParameterizedRedisTest // GH-2037
7373
@EnabledOnCommand("SMISMEMBER")
7474
void testContainsAll() {
75+
7576
T t1 = getT();
7677
T t2 = getT();
7778
T t3 = getT();

0 commit comments

Comments
 (0)