diff --git a/src/main/java/org/springframework/data/redis/core/DefaultReactiveSetOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultReactiveSetOperations.java index 6f34e2ba3..6bd44aeae 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultReactiveSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultReactiveSetOperations.java @@ -37,6 +37,7 @@ import org.springframework.util.Assert; * * @author Mark Paluch * @author Christoph Strobl + * @author Roman Bezpalko * @since 2.0 */ @RequiredArgsConstructor @@ -169,10 +170,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(key, "Key must not be null!"); Assert.notNull(otherKeys, "Other keys must not be null!"); - return createFlux(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // + return intersect(getKeys(key, otherKeys)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#intersect(java.util.Collection) + */ + @Override + public Flux intersect(Collection keys) { + + Assert.notNull(keys, "Keys must not be null!"); + + return createFlux(connection -> Flux.fromIterable(keys) .map(this::rawKey) // .collectList() // - .flatMapMany(connection::sInter) // + .flatMapMany(connection::sInter) .map(this::readValue)); } @@ -200,9 +213,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(otherKeys, "Other keys must not be null!"); Assert.notNull(destKey, "Destination key must not be null!"); - return createMono(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // - .map(this::rawKey) // - .collectList() // + return intersectAndStore(getKeys(key, otherKeys), destKey); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#intersectAndStore(java.util.Collection, java.lang.Object) + */ + @Override + public Mono intersectAndStore(Collection keys, K destKey) { + + Assert.notNull(keys, "Keys must not be null!"); + Assert.notNull(destKey, "Destination key must not be null!"); + + return createMono(connection -> Flux.fromIterable(keys) + .map(this::rawKey) + .collectList() .flatMap(rawKeys -> connection.sInterStore(rawKey(destKey), rawKeys))); } @@ -229,10 +255,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(key, "Key must not be null!"); Assert.notNull(otherKeys, "Other keys must not be null!"); - return createFlux(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // - .map(this::rawKey) // - .collectList() // - .flatMapMany(connection::sUnion) // + return union(getKeys(key, otherKeys)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#union(java.util.Collection) + */ + @Override + public Flux union(Collection keys) { + + Assert.notNull(keys, "Keys must not be null!"); + + return createFlux(connection -> Flux.fromIterable(keys) + .map(this::rawKey) + .collectList() + .flatMapMany(connection::sUnion) .map(this::readValue)); } @@ -261,9 +299,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(otherKeys, "Other keys must not be null!"); Assert.notNull(destKey, "Destination key must not be null!"); - return createMono(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // - .map(this::rawKey) // - .collectList() // + return unionAndStore(getKeys(key, otherKeys), destKey); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#unionAndStore(java.util.Collection, java.lang.Object) + */ + @Override + public Mono unionAndStore(Collection keys, K destKey) { + + Assert.notNull(keys, "Keys must not be null!"); + Assert.notNull(destKey, "Destination key must not be null!"); + + return createMono(connection -> Flux.fromIterable(keys) + .map(this::rawKey) + .collectList() .flatMap(rawKeys -> connection.sUnionStore(rawKey(destKey), rawKeys))); } @@ -290,10 +341,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(key, "Key must not be null!"); Assert.notNull(otherKeys, "Other keys must not be null!"); - return createFlux(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // - .map(this::rawKey) // - .collectList() // - .flatMapMany(connection::sDiff) // + return difference(getKeys(key, otherKeys)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#difference(java.util.Collection) + */ + @Override + public Flux difference(Collection keys) { + + Assert.notNull(keys, "Keys must not be null!"); + + return createFlux(connection -> Flux.fromIterable(keys) + .map(this::rawKey) + .collectList() + .flatMapMany(connection::sDiff) .map(this::readValue)); } @@ -322,9 +385,22 @@ class DefaultReactiveSetOperations implements ReactiveSetOperations Assert.notNull(otherKeys, "Other keys must not be null!"); Assert.notNull(destKey, "Destination key must not be null!"); - return createMono(connection -> Flux.fromIterable(getKeys(key, otherKeys)) // - .map(this::rawKey) // - .collectList() // + return differenceAndStore(getKeys(key, otherKeys), destKey); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ReactiveSetOperations#differenceAndStore(java.util.Collection, java.lang.Object) + */ + @Override + public Mono differenceAndStore(Collection keys, K destKey) { + + Assert.notNull(keys, "Keys must not be null!"); + Assert.notNull(destKey, "Destination key must not be null!"); + + return createMono(connection -> Flux.fromIterable(keys) + .map(this::rawKey) + .collectList() .flatMap(rawKeys -> connection.sDiffStore(rawKey(destKey), rawKeys))); } diff --git a/src/main/java/org/springframework/data/redis/core/DefaultSetOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultSetOperations.java index fe1c4ae5d..67444fd99 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultSetOperations.java @@ -15,8 +15,8 @@ */ package org.springframework.data.redis.core; +import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -30,6 +30,7 @@ import org.springframework.util.Assert; * @author Costin Leau * @author Christoph Strobl * @author Mark Paluch + * @author Roman Bezpalko */ class DefaultSetOperations extends AbstractOperations implements SetOperations { @@ -55,7 +56,7 @@ class DefaultSetOperations extends AbstractOperations implements Set */ @Override public Set difference(K key, K otherKey) { - return difference(key, Collections.singleton(otherKey)); + return difference(Arrays.asList(key, otherKey)); } /* @@ -64,20 +65,31 @@ class DefaultSetOperations extends AbstractOperations implements Set */ @Override public Set difference(K key, Collection otherKeys) { - byte[][] rawKeys = rawKeys(key, otherKeys); Set rawValues = execute(connection -> connection.sDiff(rawKeys), true); return deserializeValues(rawValues); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#difference(java.util.Collection) + */ + @Override + public Set difference(Collection keys) { + byte[][] rawKeys = rawKeys(keys); + Set rawValues = execute(connection -> connection.sDiff(rawKeys), true); + + return deserializeValues(rawValues); + } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#differenceAndStore(java.lang.Object, java.lang.Object, java.lang.Object) */ @Override public Long differenceAndStore(K key, K otherKey, K destKey) { - return differenceAndStore(key, Collections.singleton(otherKey), destKey); + return differenceAndStore(Arrays.asList(key, otherKey), destKey); } /* @@ -92,13 +104,24 @@ class DefaultSetOperations extends AbstractOperations implements Set return execute(connection -> connection.sDiffStore(rawDestKey, rawKeys), true); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#differenceAndStore(java.util.Collection, java.lang.Object) + */ + @Override + public Long differenceAndStore(Collection keys, K destKey) { + byte[][] rawKeys = rawKeys(keys); + byte[] rawDestKey = rawKey(destKey); + return execute(connection -> connection.sDiffStore(rawDestKey, rawKeys), true); + } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#intersect(java.lang.Object, java.lang.Object) */ @Override public Set intersect(K key, K otherKey) { - return intersect(key, Collections.singleton(otherKey)); + return intersect(Arrays.asList(key, otherKey)); } /* @@ -114,13 +137,24 @@ class DefaultSetOperations extends AbstractOperations implements Set return deserializeValues(rawValues); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#intersect(java.util.Collection) + */ + @Override + public Set intersect(Collection keys) { + byte[][] rawKeys = rawKeys(keys); + Set rawValues = execute(connection -> connection.sInter(rawKeys), true); + + return deserializeValues(rawValues); } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#intersectAndStore(java.lang.Object, java.lang.Object, java.lang.Object) */ @Override public Long intersectAndStore(K key, K otherKey, K destKey) { - return intersectAndStore(key, Collections.singleton(otherKey), destKey); + return intersectAndStore(Arrays.asList(key, otherKey), destKey); } /* @@ -135,6 +169,16 @@ class DefaultSetOperations extends AbstractOperations implements Set return execute(connection -> connection.sInterStore(rawDestKey, rawKeys), true); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#intersectAndStore(java.util.Collection, java.lang.Object) + */ + @Override + public Long intersectAndStore(Collection keys, K destKey) { + byte[][] rawKeys = rawKeys(keys); + byte[] rawDestKey = rawKey(destKey); + return execute(connection -> connection.sInterStore(rawDestKey, rawKeys), true); } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#isMember(java.lang.Object, java.lang.Object) @@ -280,7 +324,7 @@ class DefaultSetOperations extends AbstractOperations implements Set */ @Override public Set union(K key, K otherKey) { - return union(key, Collections.singleton(otherKey)); + return union(Arrays.asList(key, otherKey)); } /* @@ -296,13 +340,25 @@ class DefaultSetOperations extends AbstractOperations implements Set return deserializeValues(rawValues); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#union(java.util.Collection) + */ + @Override + public Set union(Collection keys) { + byte[][] rawKeys = rawKeys(keys); + Set rawValues = execute(connection -> connection.sUnion(rawKeys), true); + + return deserializeValues(rawValues); + } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#union(java.lang.Object, java.lang.Object, java.lang.Object) */ @Override public Long unionAndStore(K key, K otherKey, K destKey) { - return unionAndStore(key, Collections.singleton(otherKey), destKey); + return unionAndStore(Arrays.asList(key, otherKey), destKey); } /* @@ -317,6 +373,16 @@ class DefaultSetOperations extends AbstractOperations implements Set return execute(connection -> connection.sUnionStore(rawDestKey, rawKeys), true); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.SetOperations#union(java.util.Collection, java.lang.Object) + */ + @Override + public Long unionAndStore(Collection keys, K destKey) { + byte[][] rawKeys = rawKeys(keys); + byte[] rawDestKey = rawKey(destKey); + return execute(connection -> connection.sUnionStore(rawDestKey, rawKeys), true); } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.SetOperations#sScan(java.lang.Object, org.springframework.data.redis.core.ScanOptions) diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveSetOperations.java b/src/main/java/org/springframework/data/redis/core/ReactiveSetOperations.java index f9864e04c..2fd3d71db 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveSetOperations.java @@ -25,6 +25,7 @@ import java.util.Collection; * * @author Mark Paluch * @author Christoph Strobl + * @author Roman Bezpalko * @see Redis Documentation: Set Commands * @since 2.0 */ @@ -119,6 +120,15 @@ public interface ReactiveSetOperations { */ Flux intersect(K key, Collection otherKeys); + /** + * Returns the members intersecting all given sets at {@code keys}. + * + * @param keys must not be {@literal null}. + * @return + * @see Redis Documentation: SINTER + */ + Flux intersect(Collection keys); + /** * Intersect all given sets at {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -141,6 +151,16 @@ public interface ReactiveSetOperations { */ Mono intersectAndStore(K key, Collection otherKeys, K destKey); + /** + * Intersect all given sets at {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return + * @see Redis Documentation: SINTERSTORE + */ + Mono intersectAndStore(Collection keys, K destKey); + /** * Union all sets at given {@code keys} and {@code otherKey}. * @@ -161,6 +181,15 @@ public interface ReactiveSetOperations { */ Flux union(K key, Collection otherKeys); + /** + * Union all sets at given {@code keys}. + * + * @param keys must not be {@literal null}. + * @return + * @see Redis Documentation: SUNION + */ + Flux union(Collection keys); + /** * Union all sets at given {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -183,6 +212,16 @@ public interface ReactiveSetOperations { */ Mono unionAndStore(K key, Collection otherKeys, K destKey); + /** + * Union all sets at given {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return + * @see Redis Documentation: SUNIONSTORE + */ + Mono unionAndStore(Collection keys, K destKey); + /** * Diff all sets for given {@code key} and {@code otherKey}. * @@ -203,6 +242,15 @@ public interface ReactiveSetOperations { */ Flux difference(K key, Collection otherKeys); + /** + * Diff all sets for given {@code keys}. + * + * @param keys must not be {@literal null}. + * @return + * @see Redis Documentation: SDIFF + */ + Flux difference(Collection keys); + /** * Diff all sets for given {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -225,6 +273,16 @@ public interface ReactiveSetOperations { */ Mono differenceAndStore(K key, Collection otherKeys, K destKey); + /** + * Diff all sets for given {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return + * @see Redis Documentation: SDIFFSTORE + */ + Mono differenceAndStore(Collection keys, K destKey); + /** * Get all elements of set at {@code key}. * diff --git a/src/main/java/org/springframework/data/redis/core/SetOperations.java b/src/main/java/org/springframework/data/redis/core/SetOperations.java index 443e79d02..ebd564527 100644 --- a/src/main/java/org/springframework/data/redis/core/SetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/SetOperations.java @@ -27,6 +27,7 @@ import org.springframework.lang.Nullable; * @author Costin Leau * @author Christoph Strobl * @author Mark Paluch + * @author Roman Bezpalko */ public interface SetOperations { @@ -129,6 +130,16 @@ public interface SetOperations { @Nullable Set intersect(K key, Collection otherKeys); + /** + * Returns the members intersecting all given sets at {@code keys}. + * + * @param keys must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SINTER + */ + @Nullable + Set intersect(Collection keys); + /** * Intersect all given sets at {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -153,6 +164,17 @@ public interface SetOperations { @Nullable Long intersectAndStore(K key, Collection otherKeys, K destKey); + /** + * Intersect all given sets at {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SINTERSTORE + */ + @Nullable + Long intersectAndStore(Collection keys, K destKey); + /** * Union all sets at given {@code keys} and {@code otherKey}. * @@ -175,6 +197,16 @@ public interface SetOperations { @Nullable Set union(K key, Collection otherKeys); + /** + * Union all sets at given {@code keys}. + * + * @param keys must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SUNION + */ + @Nullable + Set union(Collection keys); + /** * Union all sets at given {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -199,6 +231,17 @@ public interface SetOperations { @Nullable Long unionAndStore(K key, Collection otherKeys, K destKey); + /** + * Union all sets at given {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SUNIONSTORE + */ + @Nullable + Long unionAndStore(Collection keys, K destKey); + /** * Diff all sets for given {@code key} and {@code otherKey}. * @@ -221,6 +264,16 @@ public interface SetOperations { @Nullable Set difference(K key, Collection otherKeys); + /** + * Diff all sets for given {@code keys}. + * + * @param keys must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SDIFF + */ + @Nullable + Set difference(Collection keys); + /** * Diff all sets for given {@code key} and {@code otherKey} and store result in {@code destKey}. * @@ -245,6 +298,17 @@ public interface SetOperations { @Nullable Long differenceAndStore(K key, Collection otherKeys, K destKey); + /** + * Diff all sets for given {@code keys} and store result in {@code destKey}. + * + * @param keys must not be {@literal null}. + * @param destKey must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + * @see Redis Documentation: SDIFFSTORE + */ + @Nullable + Long differenceAndStore(Collection keys, K destKey); + /** * Get all elements of set at {@code key}. *