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
Original Pull Request: #2105
This commit is contained in:
Mark Paluch
2021-06-29 15:37:33 +02:00
committed by Christoph Strobl
parent 90095d1c89
commit b6820f0f61
4 changed files with 109 additions and 89 deletions

View File

@@ -16,11 +16,9 @@
package org.springframework.data.redis.support.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.BoundSetOperations;
@@ -216,10 +214,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
*/
@Override
public void clear() {
// intersect the set with a non existing one
// TODO: find a safer way to clean the set
String randomKey = UUID.randomUUID().toString();
boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey());
boundSetOps.getOperations().delete(getKey());
}
/*

View File

@@ -19,49 +19,27 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.springframework.data.redis.core.RedisOperations;
/**
* Redis extension for the {@link Set} contract. Supports {@link Set} specific operations backed by Redis operations.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
/**
* Intersect this set and another {@link RedisSet}.
* Constructs a new {@link RedisSet} instance.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(RedisSet<?> set);
/**
* Intersect this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
/**
* Union this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
* @since 2.6
*/
Set<E> union(RedisSet<?> set);
/**
* Union this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 1.0
*/
Set<E> union(Collection<? extends RedisSet<?>> sets);
static <E> RedisSet<E> create(String key, RedisOperations<String, E> operations) {
return new DefaultRedisSet<>(key, operations);
}
/**
* Diff this set and another {@link RedisSet}.
@@ -81,58 +59,6 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
*/
Set<E> diff(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}
* @since 1.0
*/
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result
* in destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
* destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Get random element from the set.
*
* @return
* @since 2.6
*/
E randomValue();
/**
* Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
@@ -155,9 +81,97 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
*/
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Intersect this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(RedisSet<?> set);
/**
* Intersect this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the intersecting values.
* @since 1.0
*/
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}
* @since 1.0
*/
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result
* in destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
/**
* Get random element from the set.
*
* @return
* @since 2.6
*/
E randomValue();
/**
* @since 1.4
* @return
*/
Iterator<E> scan();
/**
* Union this set and another {@link RedisSet}.
*
* @param set must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 2.6
*/
Set<E> union(RedisSet<?> set);
/**
* Union this set and other {@link RedisSet}s.
*
* @param sets must not be {@literal null}.
* @return a {@link Set} containing the combined values.
* @since 1.0
*/
Set<E> union(Collection<? extends RedisSet<?>> sets);
/**
* Create a new {@link RedisSet} by union this sorted set and {@link RedisSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
/**
* Create a new {@link RedisSet} by union this sorted set and the collection {@link RedisSet} and store result in
* destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisSet} pointing at {@code destKey}.
* @since 1.0
*/
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
}