Skip to content

Commit 5b32dee

Browse files
committed
Add intersectSize methods for SINTERCARD operation in Reactive APIs
Signed-off-by: Kiminni <imk0980@gmail.com>
1 parent a58f2f8 commit 5b32dee

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

src/main/java/org/springframework/data/redis/connection/ReactiveSetCommands.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Christoph Strobl
4545
* @author Mark Paluch
46+
* @author Mingi Lee
4647
* @since 2.0
4748
*/
4849
public interface ReactiveSetCommands {
@@ -775,6 +776,73 @@ default Mono<Long> sInterStore(ByteBuffer destinationKey, Collection<ByteBuffer>
775776
*/
776777
Flux<NumericResponse<SInterStoreCommand, Long>> sInterStore(Publisher<SInterStoreCommand> commands);
777778

779+
/**
780+
* {@code SINTERCARD} command parameters.
781+
*
782+
* @author Mingi Lee
783+
* @since 4.0
784+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
785+
*/
786+
class SInterCardCommand implements Command {
787+
788+
private final List<ByteBuffer> keys;
789+
790+
private SInterCardCommand(List<ByteBuffer> keys) {
791+
this.keys = keys;
792+
}
793+
794+
/**
795+
* Creates a new {@link SInterCardCommand} given a {@link Collection} of keys.
796+
*
797+
* @param keys must not be {@literal null}.
798+
* @return a new {@link SInterCardCommand} for a {@link Collection} of keys.
799+
*/
800+
public static SInterCardCommand keys(Collection<ByteBuffer> keys) {
801+
802+
Assert.notNull(keys, "Keys must not be null");
803+
804+
return new SInterCardCommand(new ArrayList<>(keys));
805+
}
806+
807+
@Override
808+
public @Nullable ByteBuffer getKey() {
809+
return null;
810+
}
811+
812+
/**
813+
* @return never {@literal null}.
814+
*/
815+
public List<ByteBuffer> getKeys() {
816+
return keys;
817+
}
818+
}
819+
820+
/**
821+
* Returns the cardinality of the set which would result from the intersection of all given sets at {@literal keys}.
822+
*
823+
* @param keys must not be {@literal null}.
824+
* @return
825+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
826+
* @since 4.0
827+
*/
828+
default Mono<Long> sInterCard(Collection<ByteBuffer> keys) {
829+
830+
Assert.notNull(keys, "Keys must not be null");
831+
832+
return sInterCard(Mono.just(SInterCardCommand.keys(keys))).next().map(NumericResponse::getOutput);
833+
}
834+
835+
/**
836+
* Returns the cardinality of the set which would result from the intersection of all given sets at
837+
* {@link SInterCardCommand#getKeys()}.
838+
*
839+
* @param commands must not be {@literal null}.
840+
* @return
841+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
842+
* @since 4.0
843+
*/
844+
Flux<NumericResponse<SInterCardCommand, Long>> sInterCard(Publisher<SInterCardCommand> commands);
845+
778846
/**
779847
* {@code SUNION} command parameters.
780848
*

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveSetCommands.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
/**
3737
* @author Christoph Strobl
3838
* @author Mark Paluch
39+
* @author Mingi Lee
3940
* @since 2.0
4041
*/
4142
class LettuceReactiveSetCommands implements ReactiveSetCommands {
@@ -175,6 +176,18 @@ public Flux<NumericResponse<SInterStoreCommand, Long>> sInterStore(Publisher<SIn
175176
}));
176177
}
177178

179+
@Override
180+
public Flux<NumericResponse<SInterCardCommand, Long>> sInterCard(Publisher<SInterCardCommand> commands) {
181+
182+
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
183+
184+
Assert.notNull(command.getKeys(), "Keys must not be null");
185+
186+
return cmd.sintercard(command.getKeys().toArray(new ByteBuffer[0]))
187+
.map(value -> new NumericResponse<>(command, value));
188+
}));
189+
}
190+
178191
@Override
179192
public Flux<CommandResponse<SUnionCommand, Flux<ByteBuffer>>> sUnion(Publisher<SUnionCommand> commands) {
180193

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Christoph Strobl
4242
* @author Roman Bezpalko
4343
* @author John Blum
44+
* @author Mingi Lee
4445
* @since 2.0
4546
*/
4647
class DefaultReactiveSetOperations<K, V> implements ReactiveSetOperations<K, V> {
@@ -213,6 +214,35 @@ public Mono<Long> intersectAndStore(Collection<K> keys, K destKey) {
213214
.flatMap(rawKeys -> setCommands.sInterStore(rawKey(destKey), rawKeys)));
214215
}
215216

217+
@Override
218+
public Mono<Long> intersectSize(K key, K otherKey) {
219+
220+
Assert.notNull(key, "Key must not be null");
221+
Assert.notNull(otherKey, "Other key must not be null");
222+
223+
return intersectSize(key, Collections.singleton(otherKey));
224+
}
225+
226+
@Override
227+
public Mono<Long> intersectSize(K key, Collection<K> otherKeys) {
228+
229+
Assert.notNull(key, "Key must not be null");
230+
Assert.notNull(otherKeys, "Other keys must not be null");
231+
232+
return intersectSize(getKeys(key, otherKeys));
233+
}
234+
235+
@Override
236+
public Mono<Long> intersectSize(Collection<K> keys) {
237+
238+
Assert.notNull(keys, "Keys must not be null");
239+
240+
return createMono(setCommands -> Flux.fromIterable(keys) //
241+
.map(this::rawKey) //
242+
.collectList() //
243+
.flatMap(setCommands::sInterCard));
244+
}
245+
216246
@Override
217247
public Flux<V> union(K key, K otherKey) {
218248

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Mark Paluch
3434
* @author Christoph Strobl
3535
* @author Roman Bezpalko
36+
* @author Mingi Lee
3637
* @see <a href="https://redis.io/commands#set">Redis Documentation: Set Commands</a>
3738
* @since 2.0
3839
*/
@@ -181,6 +182,38 @@ public interface ReactiveSetOperations<K, V> {
181182
*/
182183
Mono<Long> intersectAndStore(Collection<K> keys, K destKey);
183184

185+
/**
186+
* Returns the cardinality of the set which would result from the intersection of {@code key} and {@code otherKey}.
187+
*
188+
* @param key must not be {@literal null}.
189+
* @param otherKey must not be {@literal null}.
190+
* @return
191+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
192+
* @since 4.0
193+
*/
194+
Mono<Long> intersectSize(K key, K otherKey);
195+
196+
/**
197+
* Returns the cardinality of the set which would result from the intersection of {@code key} and {@code otherKeys}.
198+
*
199+
* @param key must not be {@literal null}.
200+
* @param otherKeys must not be {@literal null}.
201+
* @return
202+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
203+
* @since 4.0
204+
*/
205+
Mono<Long> intersectSize(K key, Collection<K> otherKeys);
206+
207+
/**
208+
* Returns the cardinality of the set which would result from the intersection of all given sets at {@code keys}.
209+
*
210+
* @param keys must not be {@literal null}.
211+
* @return
212+
* @see <a href="https://redis.io/commands/sintercard">Redis Documentation: SINTERCARD</a>
213+
* @since 4.0
214+
*/
215+
Mono<Long> intersectSize(Collection<K> keys);
216+
184217
/**
185218
* Union all sets at given {@code keys} and {@code otherKey}.
186219
*

0 commit comments

Comments
 (0)