Skip to content

Commit e9b8f1f

Browse files
committed
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
1 parent 15c9aaf commit e9b8f1f

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
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;
@@ -207,10 +205,7 @@ public boolean add(E e) {
207205
*/
208206
@Override
209207
public void clear() {
210-
// intersect the set with a non existing one
211-
// TODO: find a safer way to clean the set
212-
String randomKey = UUID.randomUUID().toString();
213-
boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey());
208+
boundSetOps.getOperations().delete(getKey());
214209
}
215210

216211
/*

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

+108-10
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,135 @@
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

30-
Set<E> intersect(RedisSet<?> set);
33+
/**
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+
}
3143

32-
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
44+
/**
45+
* Diff this set and another {@link RedisSet}.
46+
*
47+
* @param set must not be {@literal null}.
48+
* @return a {@link Set} containing the values that differ.
49+
*/
50+
Set<E> diff(RedisSet<?> set);
3351

34-
Set<E> union(RedisSet<?> set);
52+
/**
53+
* Diff this set and other {@link RedisSet}s.
54+
*
55+
* @param sets must not be {@literal null}.
56+
* @return a {@link Set} containing the values that differ.
57+
*/
58+
Set<E> diff(Collection<? extends RedisSet<?>> sets);
3559

36-
Set<E> union(Collection<? extends RedisSet<?>> sets);
60+
/**
61+
* Create a new {@link RedisSet} by diffing this set and {@link RedisSet} and store result in destination
62+
* {@code destKey}.
63+
*
64+
* @param set must not be {@literal null}.
65+
* @param destKey must not be {@literal null}.
66+
* @return a new {@link RedisSet} pointing at {@code destKey}.
67+
*/
68+
RedisSet<E> diffAndStore(RedisSet<?> set, String destKey);
3769

38-
Set<E> diff(RedisSet<?> set);
70+
/**
71+
* Create a new {@link RedisSet} by diffing this set and the collection {@link RedisSet} and store result in
72+
* destination {@code destKey}.
73+
*
74+
* @param sets must not be {@literal null}.
75+
* @param destKey must not be {@literal null}.
76+
* @return a new {@link RedisSet} pointing at {@code destKey}.
77+
*/
78+
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
3979

40-
Set<E> diff(Collection<? extends RedisSet<?>> sets);
80+
/**
81+
* Intersect this set and another {@link RedisSet}.
82+
*
83+
* @param set must not be {@literal null}.
84+
* @return a {@link Set} containing the intersecting values.
85+
*/
86+
Set<E> intersect(RedisSet<?> set);
4187

88+
/**
89+
* Intersect this set and other {@link RedisSet}s.
90+
*
91+
* @param sets must not be {@literal null}.
92+
* @return a {@link Set} containing the intersecting values.
93+
*/
94+
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
95+
96+
/**
97+
* Create a new {@link RedisSet} by intersecting this set and {@link RedisSet} and store result in destination
98+
* {@code destKey}.
99+
*
100+
* @param set must not be {@literal null}.
101+
* @param destKey must not be {@literal null}.
102+
* @return a new {@link RedisSet} pointing at {@code destKey}
103+
*/
42104
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
43105

106+
/**
107+
* Create a new {@link RedisSet} by intersecting this set and the collection {@link RedisSet} and store result in
108+
* destination {@code destKey}.
109+
*
110+
* @param sets must not be {@literal null}.
111+
* @param destKey must not be {@literal null}.
112+
* @return a new {@link RedisSet} pointing at {@code destKey}
113+
*/
44114
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
45115

46-
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
116+
/**
117+
* Union this set and another {@link RedisSet}.
118+
*
119+
* @param set must not be {@literal null}.
120+
* @return a {@link Set} containing the combined values.
121+
*/
122+
Set<E> union(RedisSet<?> set);
47123

48-
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
124+
/**
125+
* Union this set and other {@link RedisSet}s.
126+
*
127+
* @param sets must not be {@literal null}.
128+
* @return a {@link Set} containing the combined values.
129+
*/
130+
Set<E> union(Collection<? extends RedisSet<?>> sets);
49131

50-
RedisSet<E> diffAndStore(RedisSet<?> set, String destKey);
132+
/**
133+
* Create a new {@link RedisSet} by union this set and {@link RedisSet} and store result in destination
134+
* {@code destKey}.
135+
*
136+
* @param set must not be {@literal null}.
137+
* @param destKey must not be {@literal null}.
138+
* @return a new {@link RedisSet} pointing at {@code destKey}
139+
*/
140+
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
51141

52-
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
142+
/**
143+
* Create a new {@link RedisSet} by union this set and the collection {@link RedisSet} and store result in destination
144+
* {@code destKey}.
145+
*
146+
* @param sets must not be {@literal null}.
147+
* @param destKey must not be {@literal null}.
148+
* @return a new {@link RedisSet} pointing at {@code destKey}
149+
*/
150+
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
53151

54152
/**
55153
* @since 1.4

0 commit comments

Comments
 (0)