DATAREDIS-873 - Accept single collection parameter in SetOperations diff/inter/union methods.

We now accept a single collection parameter containing keys for diff/inter/union and their …store methods. Note that diff requires a specific key order to denote the initial set to compare to.

Original pull request: #361.
This commit is contained in:
Dominys
2018-10-12 12:18:21 +03:00
committed by Mark Paluch
parent 6921ab01bf
commit 724579b8a9
4 changed files with 291 additions and 27 deletions

View File

@@ -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<K, V> implements ReactiveSetOperations<K, V>
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<V> intersect(Collection<K> 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<K, V> implements ReactiveSetOperations<K, V>
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<Long> intersectAndStore(Collection<K> 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<K, V> implements ReactiveSetOperations<K, V>
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<V> union(Collection<K> 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<K, V> implements ReactiveSetOperations<K, V>
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<Long> unionAndStore(Collection<K> 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<K, V> implements ReactiveSetOperations<K, V>
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<V> difference(Collection<K> 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<K, V> implements ReactiveSetOperations<K, V>
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<Long> differenceAndStore(Collection<K> 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)));
}

View File

@@ -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<K, V> extends AbstractOperations<K, V> implements SetOperations<K, V> {
@@ -55,7 +56,7 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
*/
@Override
public Set<V> difference(K key, K otherKey) {
return difference(key, Collections.singleton(otherKey));
return difference(Arrays.asList(key, otherKey));
}
/*
@@ -64,20 +65,31 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
*/
@Override
public Set<V> difference(K key, Collection<K> otherKeys) {
byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> 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<V> difference(Collection<K> keys) {
byte[][] rawKeys = rawKeys(keys);
Set<byte[]> 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<K, V> extends AbstractOperations<K, V> 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<K> 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<V> intersect(K key, K otherKey) {
return intersect(key, Collections.singleton(otherKey));
return intersect(Arrays.asList(key, otherKey));
}
/*
@@ -114,13 +137,24 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#intersect(java.util.Collection)
*/
@Override
public Set<V> intersect(Collection<K> keys) {
byte[][] rawKeys = rawKeys(keys);
Set<byte[]> 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<K, V> extends AbstractOperations<K, V> 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<K> 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<K, V> extends AbstractOperations<K, V> implements Set
*/
@Override
public Set<V> union(K key, K otherKey) {
return union(key, Collections.singleton(otherKey));
return union(Arrays.asList(key, otherKey));
}
/*
@@ -296,13 +340,25 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#union(java.util.Collection)
*/
@Override
public Set<V> union(Collection<K> keys) {
byte[][] rawKeys = rawKeys(keys);
Set<byte[]> 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<K, V> extends AbstractOperations<K, V> 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<K> 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)

View File

@@ -25,6 +25,7 @@ import java.util.Collection;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Roman Bezpalko
* @see <a href="http://redis.io/commands#set">Redis Documentation: Set Commands</a>
* @since 2.0
*/
@@ -119,6 +120,15 @@ public interface ReactiveSetOperations<K, V> {
*/
Flux<V> intersect(K key, Collection<K> otherKeys);
/**
* Returns the members intersecting all given sets at {@code keys}.
*
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/sinter">Redis Documentation: SINTER</a>
*/
Flux<V> intersect(Collection<K> keys);
/**
* Intersect all given sets at {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -141,6 +151,16 @@ public interface ReactiveSetOperations<K, V> {
*/
Mono<Long> intersectAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sinterstore">Redis Documentation: SINTERSTORE</a>
*/
Mono<Long> intersectAndStore(Collection<K> keys, K destKey);
/**
* Union all sets at given {@code keys} and {@code otherKey}.
*
@@ -161,6 +181,15 @@ public interface ReactiveSetOperations<K, V> {
*/
Flux<V> union(K key, Collection<K> otherKeys);
/**
* Union all sets at given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/sunion">Redis Documentation: SUNION</a>
*/
Flux<V> union(Collection<K> keys);
/**
* Union all sets at given {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -183,6 +212,16 @@ public interface ReactiveSetOperations<K, V> {
*/
Mono<Long> unionAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sunionstore">Redis Documentation: SUNIONSTORE</a>
*/
Mono<Long> unionAndStore(Collection<K> keys, K destKey);
/**
* Diff all sets for given {@code key} and {@code otherKey}.
*
@@ -203,6 +242,15 @@ public interface ReactiveSetOperations<K, V> {
*/
Flux<V> difference(K key, Collection<K> otherKeys);
/**
* Diff all sets for given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/sdiff">Redis Documentation: SDIFF</a>
*/
Flux<V> difference(Collection<K> keys);
/**
* Diff all sets for given {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -225,6 +273,16 @@ public interface ReactiveSetOperations<K, V> {
*/
Mono<Long> differenceAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sdiffstore">Redis Documentation: SDIFFSTORE</a>
*/
Mono<Long> differenceAndStore(Collection<K> keys, K destKey);
/**
* Get all elements of set at {@code key}.
*

View File

@@ -27,6 +27,7 @@ import org.springframework.lang.Nullable;
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author Roman Bezpalko
*/
public interface SetOperations<K, V> {
@@ -129,6 +130,16 @@ public interface SetOperations<K, V> {
@Nullable
Set<V> intersect(K key, Collection<K> 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 <a href="http://redis.io/commands/sinter">Redis Documentation: SINTER</a>
*/
@Nullable
Set<V> intersect(Collection<K> keys);
/**
* Intersect all given sets at {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -153,6 +164,17 @@ public interface SetOperations<K, V> {
@Nullable
Long intersectAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sinterstore">Redis Documentation: SINTERSTORE</a>
*/
@Nullable
Long intersectAndStore(Collection<K> keys, K destKey);
/**
* Union all sets at given {@code keys} and {@code otherKey}.
*
@@ -175,6 +197,16 @@ public interface SetOperations<K, V> {
@Nullable
Set<V> union(K key, Collection<K> otherKeys);
/**
* Union all sets at given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/sunion">Redis Documentation: SUNION</a>
*/
@Nullable
Set<V> union(Collection<K> keys);
/**
* Union all sets at given {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -199,6 +231,17 @@ public interface SetOperations<K, V> {
@Nullable
Long unionAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sunionstore">Redis Documentation: SUNIONSTORE</a>
*/
@Nullable
Long unionAndStore(Collection<K> keys, K destKey);
/**
* Diff all sets for given {@code key} and {@code otherKey}.
*
@@ -221,6 +264,16 @@ public interface SetOperations<K, V> {
@Nullable
Set<V> difference(K key, Collection<K> otherKeys);
/**
* Diff all sets for given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/sdiff">Redis Documentation: SDIFF</a>
*/
@Nullable
Set<V> difference(Collection<K> keys);
/**
* Diff all sets for given {@code key} and {@code otherKey} and store result in {@code destKey}.
*
@@ -245,6 +298,17 @@ public interface SetOperations<K, V> {
@Nullable
Long differenceAndStore(K key, Collection<K> 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 <a href="http://redis.io/commands/sdiffstore">Redis Documentation: SDIFFSTORE</a>
*/
@Nullable
Long differenceAndStore(Collection<K> keys, K destKey);
/**
* Get all elements of set at {@code key}.
*