Skip to content

Commit 8dc6f01

Browse files
mp911dechristophstrobl
authored andcommitted
Add difference, intersect, and union support to RedisZSet.
Original Pull Request: #2097
1 parent 1d8ff06 commit 8dc6f01

File tree

4 files changed

+425
-49
lines changed

4 files changed

+425
-49
lines changed

src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,20 @@ default Set<TypedTuple<V>> differenceWithScores(K otherKey) {
481481
@Nullable
482482
Set<TypedTuple<V>> differenceWithScores(Collection<K> otherKeys);
483483

484+
/**
485+
* Diff sorted {@code sets} and store result in destination {@code destKey}.
486+
*
487+
* @param otherKeys must not be {@literal null}.
488+
* @param destKey must not be {@literal null}.
489+
* @return {@literal null} when used in pipeline / transaction.
490+
* @since 2.6
491+
* @see <a href="https://redis.io/commands/zdiffstore">Redis Documentation: ZDIFFSTORE</a>
492+
*/
493+
@Nullable
494+
default Long differenceAndStore(K otherKey, K destKey) {
495+
return differenceAndStore(Collections.singleton(otherKey), destKey);
496+
}
497+
484498
/**
485499
* Diff sorted {@code sets} and store result in destination {@code destKey}.
486500
*

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

Lines changed: 150 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,100 @@ public DefaultRedisZSet(BoundZSetOperations<String, E> boundOps, double defaultS
105105
this.defaultScore = defaultScore;
106106
}
107107

108+
/*
109+
* (non-Javadoc)
110+
* @see org.springframework.data.redis.support.collections.RedisZSet#difference(org.springframework.data.redis.support.collections.RedisZSet)
111+
*/
112+
@Override
113+
public Set<E> difference(RedisZSet<?> set) {
114+
return boundZSetOps.difference(set.getKey());
115+
}
116+
117+
/*
118+
* (non-Javadoc)
119+
* @see org.springframework.data.redis.support.collections.RedisZSet#difference(java.util.Collection)
120+
*/
121+
@Override
122+
public Set<E> difference(Collection<? extends RedisZSet<?>> sets) {
123+
return boundZSetOps.difference(CollectionUtils.extractKeys(sets));
124+
}
125+
126+
/*
127+
* (non-Javadoc)
128+
* @see org.springframework.data.redis.support.collections.RedisZSet#differenceWithScores(org.springframework.data.redis.support.collections.RedisZSet)
129+
*/
130+
@Override
131+
public Set<TypedTuple<E>> differenceWithScores(RedisZSet<?> set) {
132+
return boundZSetOps.differenceWithScores(set.getKey());
133+
}
134+
135+
/*
136+
* (non-Javadoc)
137+
* @see org.springframework.data.redis.support.collections.RedisZSet#differenceWithScores(java.util.Collection)
138+
*/
139+
@Override
140+
public Set<TypedTuple<E>> differenceWithScores(Collection<? extends RedisZSet<?>> sets) {
141+
return boundZSetOps.differenceWithScores(CollectionUtils.extractKeys(sets));
142+
}
143+
144+
/*
145+
* (non-Javadoc)
146+
* @see org.springframework.data.redis.support.collections.RedisZSet#differenceAndStore(org.springframework.data.redis.support.collections.RedisZSet, java.lang.String)
147+
*/
148+
@Override
149+
public RedisZSet<E> differenceAndStore(RedisZSet<?> set, String destKey) {
150+
151+
boundZSetOps.differenceAndStore(set.getKey(), destKey);
152+
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
153+
}
154+
155+
/*
156+
* (non-Javadoc)
157+
* @see org.springframework.data.redis.support.collections.RedisZSet#differenceAndStore(java.util.Collection, java.lang.String)
158+
*/
159+
@Override
160+
public RedisZSet<E> differenceAndStore(Collection<? extends RedisZSet<?>> sets, String destKey) {
161+
162+
boundZSetOps.differenceAndStore(CollectionUtils.extractKeys(sets), destKey);
163+
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
164+
}
165+
166+
/*
167+
* (non-Javadoc)
168+
* @see org.springframework.data.redis.support.collections.RedisZSet#intersect(org.springframework.data.redis.support.collections.RedisZSet)
169+
*/
170+
@Override
171+
public Set<E> intersect(RedisZSet<?> set) {
172+
return boundZSetOps.intersect(set.getKey());
173+
}
174+
175+
/*
176+
* (non-Javadoc)
177+
* @see org.springframework.data.redis.support.collections.RedisZSet#intersect(java.util.Collection)
178+
*/
179+
@Override
180+
public Set<E> intersect(Collection<? extends RedisZSet<?>> sets) {
181+
return boundZSetOps.intersect(CollectionUtils.extractKeys(sets));
182+
}
183+
184+
/*
185+
* (non-Javadoc)
186+
* @see org.springframework.data.redis.support.collections.RedisZSet#intersectWithScores(org.springframework.data.redis.support.collections.RedisZSet)
187+
*/
188+
@Override
189+
public Set<TypedTuple<E>> intersectWithScores(RedisZSet<?> set) {
190+
return boundZSetOps.intersectWithScores(set.getKey());
191+
}
192+
193+
/*
194+
* (non-Javadoc)
195+
* @see org.springframework.data.redis.support.collections.RedisZSet#intersectWithScores(java.util.Collection)
196+
*/
197+
@Override
198+
public Set<TypedTuple<E>> intersectWithScores(Collection<? extends RedisZSet<?>> sets) {
199+
return boundZSetOps.intersectWithScores(CollectionUtils.extractKeys(sets));
200+
}
201+
108202
/*
109203
* (non-Javadoc)
110204
* @see org.springframework.data.redis.support.collections.RedisZSet#intersectAndStore(org.springframework.data.redis.support.collections.RedisZSet, java.lang.String)
@@ -127,6 +221,62 @@ public RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, S
127221
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
128222
}
129223

224+
/*
225+
* (non-Javadoc)
226+
* @see org.springframework.data.redis.support.collections.RedisZSet#union(org.springframework.data.redis.support.collections.RedisZSet)
227+
*/
228+
@Override
229+
public Set<E> union(RedisZSet<?> set) {
230+
return boundZSetOps.union(set.getKey());
231+
}
232+
233+
/*
234+
* (non-Javadoc)
235+
* @see org.springframework.data.redis.support.collections.RedisZSet#union(java.util.Collection)
236+
*/
237+
@Override
238+
public Set<E> union(Collection<? extends RedisZSet<?>> sets) {
239+
return boundZSetOps.union(CollectionUtils.extractKeys(sets));
240+
}
241+
242+
/*
243+
* (non-Javadoc)
244+
* @see org.springframework.data.redis.support.collections.RedisZSet#unionWithScores(org.springframework.data.redis.support.collections.RedisZSet)
245+
*/
246+
@Override
247+
public Set<TypedTuple<E>> unionWithScores(RedisZSet<?> set) {
248+
return boundZSetOps.unionWithScores(set.getKey());
249+
}
250+
251+
/*
252+
* (non-Javadoc)
253+
* @see org.springframework.data.redis.support.collections.RedisZSet#unionWithScores(java.util.Collection)
254+
*/
255+
@Override
256+
public Set<TypedTuple<E>> unionWithScores(Collection<? extends RedisZSet<?>> sets) {
257+
return boundZSetOps.unionWithScores(CollectionUtils.extractKeys(sets));
258+
}
259+
260+
/*
261+
* (non-Javadoc)
262+
* @see org.springframework.data.redis.support.collections.RedisZSet#unionAndStore(org.springframework.data.redis.support.collections.RedisZSet, java.lang.String)
263+
*/
264+
@Override
265+
public RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey) {
266+
boundZSetOps.unionAndStore(set.getKey(), destKey);
267+
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
268+
}
269+
270+
/*
271+
* (non-Javadoc)
272+
* @see org.springframework.data.redis.support.collections.RedisZSet#unionAndStore(java.util.Collection, java.lang.String)
273+
*/
274+
@Override
275+
public RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey) {
276+
boundZSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
277+
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
278+
}
279+
130280
/*
131281
* (non-Javadoc)
132282
* @see org.springframework.data.redis.support.collections.RedisZSet#range(long, long)
@@ -247,26 +397,6 @@ public RedisZSet<E> removeByScore(double min, double max) {
247397
return this;
248398
}
249399

250-
/*
251-
* (non-Javadoc)
252-
* @see org.springframework.data.redis.support.collections.RedisZSet#unionAndStore(org.springframework.data.redis.support.collections.RedisZSet, java.lang.String)
253-
*/
254-
@Override
255-
public RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey) {
256-
boundZSetOps.unionAndStore(set.getKey(), destKey);
257-
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
258-
}
259-
260-
/*
261-
* (non-Javadoc)
262-
* @see org.springframework.data.redis.support.collections.RedisZSet#unionAndStore(java.util.Collection, java.lang.String)
263-
*/
264-
@Override
265-
public RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey) {
266-
boundZSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
267-
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
268-
}
269-
270400
/*
271401
* (non-Javadoc)
272402
* @see java.util.AbstractCollection#add(java.lang.Object)

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,100 @@ static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations
6666
return new DefaultRedisZSet<>(key, operations, defaultScore);
6767
}
6868

69+
/**
70+
* Diff this set and another {@link RedisZSet}.
71+
*
72+
* @param set must not be {@literal null}.
73+
* @return a {@link Set} containing the values that differ.
74+
* @since 2.6
75+
*/
76+
Set<E> difference(RedisZSet<?> set);
77+
78+
/**
79+
* Diff this set and other {@link RedisZSet}s.
80+
*
81+
* @param sets must not be {@literal null}.
82+
* @return a {@link Set} containing the values that differ.
83+
* @since 2.6
84+
*/
85+
Set<E> difference(Collection<? extends RedisZSet<?>> sets);
86+
87+
/**
88+
* Diff this set and another {@link RedisZSet}.
89+
*
90+
* @param set must not be {@literal null}.
91+
* @return a {@link Set} containing the values that differ with their scores.
92+
* @since 2.6
93+
*/
94+
Set<TypedTuple<E>> differenceWithScores(RedisZSet<?> set);
95+
96+
/**
97+
* Diff this set and other {@link RedisZSet}s.
98+
*
99+
* @param set must not be {@literal null}.
100+
* @return a {@link Set} containing the values that differ with their scores.
101+
* @since 2.6
102+
*/
103+
Set<TypedTuple<E>> differenceWithScores(Collection<? extends RedisZSet<?>> sets);
104+
105+
/**
106+
* Create a new {@link RedisZSet} by diffing this sorted set and {@link RedisZSet} and store result in destination
107+
* {@code destKey}.
108+
*
109+
* @param set must not be {@literal null}.
110+
* @param destKey must not be {@literal null}.
111+
* @return a new {@link RedisZSet} pointing at {@code destKey}.
112+
* @since 2.6
113+
*/
114+
RedisZSet<E> differenceAndStore(RedisZSet<?> set, String destKey);
115+
116+
/**
117+
* Create a new {@link RedisZSet} by diffing this sorted set and the collection {@link RedisZSet} and store result in
118+
* destination {@code destKey}.
119+
*
120+
* @param sets must not be {@literal null}.
121+
* @param destKey must not be {@literal null}.
122+
* @return a new {@link RedisZSet} pointing at {@code destKey}.
123+
* @since 2.6
124+
*/
125+
RedisZSet<E> differenceAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
126+
127+
/**
128+
* Intersect this set and another {@link RedisZSet}.
129+
*
130+
* @param set must not be {@literal null}.
131+
* @return a {@link Set} containing the intersecting values.
132+
* @since 2.6
133+
*/
134+
Set<E> intersect(RedisZSet<?> set);
135+
136+
/**
137+
* Intersect this set and other {@link RedisZSet}s.
138+
*
139+
* @param sets must not be {@literal null}.
140+
* @return a {@link Set} containing the intersecting values.
141+
* @since 2.6
142+
*/
143+
Set<E> intersect(Collection<? extends RedisZSet<?>> sets);
144+
145+
/**
146+
* Intersect this set and another {@link RedisZSet}.
147+
*
148+
* @param set must not be {@literal null}.
149+
* @return a {@link Set} containing the intersecting values with their scores.
150+
* @since 2.6
151+
*/
152+
Set<TypedTuple<E>> intersectWithScores(RedisZSet<?> set);
153+
154+
/**
155+
* Intersect this set and other {@link RedisZSet}s.
156+
*
157+
* @param set must not be {@literal null}.
158+
* @return a {@link Set} containing the intersecting values with their scores.
159+
* @since 2.6
160+
*/
161+
Set<TypedTuple<E>> intersectWithScores(Collection<? extends RedisZSet<?>> sets);
162+
69163
/**
70164
* Create a new {@link RedisZSet} by intersecting this sorted set and {@link RedisZSet} and store result in
71165
* destination {@code destKey}.
@@ -86,6 +180,42 @@ static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations
86180
*/
87181
RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
88182

183+
/**
184+
* Union this set and another {@link RedisZSet}.
185+
*
186+
* @param set must not be {@literal null}.
187+
* @return a {@link Set} containing the combined values.
188+
* @since 2.6
189+
*/
190+
Set<E> union(RedisZSet<?> set);
191+
192+
/**
193+
* Union this set and other {@link RedisZSet}s.
194+
*
195+
* @param sets must not be {@literal null}.
196+
* @return a {@link Set} containing the combined values.
197+
* @since 2.6
198+
*/
199+
Set<E> union(Collection<? extends RedisZSet<?>> sets);
200+
201+
/**
202+
* Union this set and another {@link RedisZSet}.
203+
*
204+
* @param set must not be {@literal null}.
205+
* @return a {@link Set} containing the combined values with their scores.
206+
* @since 2.6
207+
*/
208+
Set<TypedTuple<E>> unionWithScores(RedisZSet<?> set);
209+
210+
/**
211+
* Union this set and other {@link RedisZSet}s.
212+
*
213+
* @param set must not be {@literal null}.
214+
* @return a {@link Set} containing the combined values with their scores.
215+
* @since 2.6
216+
*/
217+
Set<TypedTuple<E>> unionWithScores(Collection<? extends RedisZSet<?>> sets);
218+
89219
/**
90220
* Create a new {@link RedisZSet} by union this sorted set and {@link RedisZSet} and store result in destination
91221
* {@code destKey}.

0 commit comments

Comments
 (0)