Support ZRANDMEMBER via RedisZSet.

Add randomValue() method to both RedisZSet and RedisSet.

See: #2049
Original Pull Request: #2104
This commit is contained in:
Christoph Strobl
2021-06-30 14:08:27 +02:00
parent 4c4c96e9cf
commit 280d9541da
6 changed files with 60 additions and 4 deletions

View File

@@ -187,6 +187,15 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisSet#randomElement()
*/
@Override
public E randomValue() {
return boundSetOps.randomMember();
}
/*
* (non-Javadoc)
* @see java.util.AbstractCollection#add(java.lang.Object)

View File

@@ -277,6 +277,15 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#randomElement()
*/
@Override
public E randomValue() {
return boundZSetOps.randomMember();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#range(long, long)

View File

@@ -82,8 +82,8 @@ 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}.
* 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}.
@@ -93,8 +93,8 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
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}.
* 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}.
@@ -125,6 +125,14 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
*/
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}.

View File

@@ -236,6 +236,14 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
*/
RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
/**
* Get random element from the set.
*
* @return
* @since 2.6
*/
E randomValue();
/**
* Get elements between {@code start} and {@code end} from sorted set.
*

View File

@@ -302,4 +302,14 @@ public abstract class AbstractRedisSetIntegrationTests<T> extends AbstractRedisC
}
cursor.close();
}
@ParameterizedRedisTest // GH-2049
void randMemberReturnsSomething() {
Object[] valuesArray = new Object[]{getT(), getT(), getT()};
collection.addAll((List<T>) Arrays.asList(valuesArray));
assertThat(set.randomValue()).isIn(valuesArray);
}
}

View File

@@ -21,6 +21,7 @@ import static org.assertj.core.api.Assumptions.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -856,4 +857,15 @@ public abstract class AbstractRedisZSetTestIntegration<T> extends AbstractRedisC
assertThat(zSet.addIfAbsent(t1, 1)).isTrue();
assertThat(zSet.addIfAbsent(t1, 1)).isFalse();
}
@ParameterizedRedisTest // GH-2049
@EnabledOnCommand("ZRANDMEMBER")
void randMemberReturnsSomething() {
Object[] valuesArray = new Object[]{getT(), getT(), getT()};
collection.addAll((List<T>) Arrays.asList(valuesArray));
assertThat(zSet.randomValue()).isIn(valuesArray);
}
}