Skip to content

Commit

Permalink
Add support for HRANDFIELD and ZRANDMEMBER commands #1605
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Mar 4, 2021
1 parent aef96d1 commit 7755da8
Show file tree
Hide file tree
Showing 24 changed files with 918 additions and 114 deletions.
40 changes: 40 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,26 @@ public RedisFuture<String> hmset(K key, Map<K, V> map) {
return dispatch(commandBuilder.hmset(key, map));
}

@Override
public RedisFuture<K> hrandfield(K key) {
return dispatch(commandBuilder.hrandfield(key));
}

@Override
public RedisFuture<List<K>> hrandfield(K key, long count) {
return dispatch(commandBuilder.hrandfield(key, count));
}

@Override
public RedisFuture<KeyValue<K, V>> hrandfieldWithvalues(K key) {
return dispatch(commandBuilder.hrandfieldWithvalues(key));
}

@Override
public RedisFuture<List<KeyValue<K, V>>> hrandfieldWithvalues(K key, long count) {
return dispatch(commandBuilder.hrandfieldWithvalues(key, count));
}

@Override
public RedisFuture<MapScanCursor<K, V>> hscan(K key) {
return dispatch(commandBuilder.hscan(key));
Expand Down Expand Up @@ -2053,6 +2073,26 @@ public RedisFuture<List<ScoredValue<V>>> zpopmax(K key, long count) {
return dispatch(commandBuilder.zpopmax(key, count));
}

@Override
public RedisFuture<V> zrandmember(K key) {
return dispatch(commandBuilder.zrandmember(key));
}

@Override
public RedisFuture<List<V>> zrandmember(K key, long count) {
return dispatch(commandBuilder.zrandmember(key, count));
}

@Override
public RedisFuture<ScoredValue<V>> zrandmemberWithscores(K key) {
return dispatch(commandBuilder.zrandmemberWithscores(key));
}

@Override
public RedisFuture<List<ScoredValue<V>>> zrandmemberWithscores(K key, long count) {
return dispatch(commandBuilder.zrandmemberWithscores(key, count));
}

@Override
public RedisFuture<List<V>> zrange(K key, long start, long stop) {
return dispatch(commandBuilder.zrange(key, start, stop));
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,26 @@ public Mono<Long> hmget(KeyValueStreamingChannel<K, V> channel, K key, K... fiel
return createMono(() -> commandBuilder.hmget(channel, key, fields));
}

@Override
public Mono<K> hrandfield(K key) {
return createMono(() -> commandBuilder.hrandfield(key));
}

@Override
public Flux<K> hrandfield(K key, long count) {
return createDissolvingFlux(() -> commandBuilder.hrandfield(key, count));
}

@Override
public Mono<KeyValue<K, V>> hrandfieldWithvalues(K key) {
return createMono(() -> commandBuilder.hrandfieldWithvalues(key));
}

@Override
public Flux<KeyValue<K, V>> hrandfieldWithvalues(K key, long count) {
return createDissolvingFlux(() -> commandBuilder.hrandfieldWithvalues(key, count));
}

@Override
public Mono<String> hmset(K key, Map<K, V> map) {
return createMono(() -> commandBuilder.hmset(key, map));
Expand Down Expand Up @@ -2126,6 +2146,26 @@ public Flux<ScoredValue<V>> zpopmax(K key, long count) {
return createDissolvingFlux(() -> commandBuilder.zpopmax(key, count));
}

@Override
public Mono<V> zrandmember(K key) {
return createMono(() -> commandBuilder.zrandmember(key));
}

@Override
public Flux<V> zrandmember(K key, long count) {
return createDissolvingFlux(() -> commandBuilder.zrandmember(key, count));
}

@Override
public Mono<ScoredValue<V>> zrandmemberWithscores(K key) {
return createMono(() -> commandBuilder.zrandmemberWithscores(key));
}

@Override
public Flux<ScoredValue<V>> zrandmemberWithscores(K key, long count) {
return createDissolvingFlux(() -> commandBuilder.zrandmemberWithscores(key, count));
}

@Override
public Flux<V> zrange(K key, long start, long stop) {
return createDissolvingFlux(() -> commandBuilder.zrange(key, start, stop));
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,34 @@ Command<K, V, String> hmset(K key, Map<K, V> map) {
return createCommand(HMSET, new StatusOutput<>(codec), args);
}

Command<K, V, K> hrandfield(K key) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
return createCommand(HRANDFIELD, new KeyOutput<>(codec), args);
}

Command<K, V, List<K>> hrandfield(K key, long count) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).add(count);
return createCommand(HRANDFIELD, new KeyListOutput<>(codec), args);
}

Command<K, V, KeyValue<K, V>> hrandfieldWithvalues(K key) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).add(1).add(WITHVALUES);
return createCommand(HRANDFIELD, new KeyValueOutput<>(codec), args);
}

Command<K, V, List<KeyValue<K, V>>> hrandfieldWithvalues(K key, long count) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).add(count).add(WITHVALUES);
return createCommand(HRANDFIELD, new KeyValueListOutput<>(codec), args);
}

Command<K, V, MapScanCursor<K, V>> hscan(K key) {
notNullKey(key);

Expand Down Expand Up @@ -2987,6 +3015,38 @@ Command<K, V, List<ScoredValue<V>>> zpopmax(K key, long count) {
return createCommand(ZPOPMAX, new ScoredValueListOutput<>(codec), args);
}

Command<K, V, V> zrandmember(K key) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKeys(key);

return createCommand(ZRANDMEMBER, new ValueOutput<>(codec), args);
}

Command<K, V, List<V>> zrandmember(K key, long count) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKeys(key).add(count);

return createCommand(ZRANDMEMBER, new ValueListOutput<>(codec), args);
}

Command<K, V, ScoredValue<V>> zrandmemberWithscores(K key) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKeys(key).add(1).add(WITHSCORES);

return createCommand(ZRANDMEMBER, new ScoredValueOutput<>(codec), args);
}

Command<K, V, List<ScoredValue<V>>> zrandmemberWithscores(K key, long count) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKeys(key).add(count).add(WITHSCORES);

return createCommand(ZRANDMEMBER, new ScoredValueListOutput<>(codec), args);
}

Command<K, V, List<V>> zrange(K key, long start, long stop) {
notNullKey(key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import java.util.List;
import java.util.Map;

import io.lettuce.core.*;
import io.lettuce.core.KeyValue;
import io.lettuce.core.MapScanCursor;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.StreamScanCursor;
import io.lettuce.core.output.KeyStreamingChannel;
import io.lettuce.core.output.KeyValueStreamingChannel;
import io.lettuce.core.output.ValueStreamingChannel;
Expand Down Expand Up @@ -152,11 +157,51 @@ public interface RedisHashAsyncCommands<K, V> {
* Set multiple hash fields to multiple values.
*
* @param key the key.
* @param map the null.
* @param map the hash to apply.
* @return String simple-string-reply.
*/
RedisFuture<String> hmset(K key, Map<K, V> map);

/**
* Return a random field from the hash stored at {@code key}.
*
* @param key the key.
* @return hash field name.
* @since 6.1
*/
RedisFuture<K> hrandfield(K key);

/**
* Return {@code count} random fields from the hash stored at {@code key}.
*
* @param key the key.
* @param count the number of fields to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return array-reply list of field names.
* @since 6.1
*/
RedisFuture<List<K>> hrandfield(K key, long count);

/**
* Return a random field along its value from the hash stored at {@code key}.
*
* @param key the key.
* @return array-reply the key and value.
* @since 6.1
*/
RedisFuture<KeyValue<K, V>> hrandfieldWithvalues(K key);

/**
* Return {@code count} random fields along their value from the hash stored at {@code key}.
*
* @param key the key.
* @param count the number of fields to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return array-reply the keys and values.
* @since 6.1
*/
RedisFuture<List<KeyValue<K, V>>> hrandfieldWithvalues(K key, long count);

/**
* Incrementally iterate hash fields and associated values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,48 @@ public interface RedisSortedSetAsyncCommands<K, V> {
*/
RedisFuture<List<ScoredValue<V>>> zpopmax(K key, long count);

/**
* Return a random member from the sorted set stored at {@code key}.
*
* @param key the key.
* @return element.
* @since 6.1
*/
RedisFuture<V> zrandmember(K key);

/**
* Return {@code count} random members from the sorted set stored at {@code key}.
*
* @param key the key.
* @param count the number of members to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return List&lt;ScoredValue&lt;V&gt;&gt; array-reply list of scores and elements.
* @since 6.1
*/
RedisFuture<List<V>> zrandmember(K key, long count);

/**
* Return a random member along its value from the sorted set stored at {@code key}.
*
* @param key the key.
* @param count the number of members to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return the score and element.
* @since 6.1
*/
RedisFuture<ScoredValue<V>> zrandmemberWithscores(K key);

/**
* Return {@code count} random members along their value from the sorted set stored at {@code key}.
*
* @param key the key.
* @param count the number of members to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return List&lt;ScoredValue&lt;V&gt;&gt; array-reply list of scores and elements.
* @since 6.1
*/
RedisFuture<List<ScoredValue<V>>> zrandmemberWithscores(K key, long count);

/**
* Return a range of members in a sorted set, by index.
*
Expand Down Expand Up @@ -1238,20 +1280,21 @@ public interface RedisSortedSetAsyncCommands<K, V> {
* Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores
* the result in the {@code dstKey} destination key.
*
* @param dstKey the dst key.
* @param srcKey the src key.
* @param dstKey the src key.
* @param srcKey the dst key.
* @param range the lexicographical range.
* @return The number of elements in the resulting sorted set.
* @since 6.1
*/
RedisFuture<Long> zrevrangestorebylex(K dstKey, K srcKey, Range<? extends V> range, Limit limit);

/**
* Get the specified range of elements in the sorted set stored at {@code srcKey} with scores ordered from high to low and
* Get the specified range of elements in the sorted set stored at {@code srcKey with scores ordered from high to low and
* stores the result in the {@code dstKey} destination key.
*
* @param dstKey the dst key.
* @param srcKey the src key.
* @param dstKey the src key.
*
* @param srcKey the dst key.
* @param range the score range.
* @return The number of elements in the resulting sorted set.
* @since 6.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import io.lettuce.core.*;
import io.lettuce.core.KeyValue;
import io.lettuce.core.MapScanCursor;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.StreamScanCursor;
import io.lettuce.core.output.KeyStreamingChannel;
import io.lettuce.core.output.KeyValueStreamingChannel;
import io.lettuce.core.output.ValueStreamingChannel;
Expand Down Expand Up @@ -159,11 +163,53 @@ public interface RedisHashReactiveCommands<K, V> {
* Set multiple hash fields to multiple values.
*
* @param key the key.
* @param map the null.
* @param map the hash to apply.
* @return String simple-string-reply.
*/
Mono<String> hmset(K key, Map<K, V> map);

/**
* Return a random field from the hash stored at {@code key}.
*
* @param key the key.
* @return hash field name.
* @since 6.1
*/
Mono<K> hrandfield(K key);

/**
* Return {@code count} random fields from the hash stored at {@code key}.
*
* @param key the key.
* @param count the number of fields to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return array-reply list of field names.
* @since 6.1
*/
Flux<K> hrandfield(K key, long count);

/**
* Return a random field along its value from the hash stored at {@code key}.
*
* @param key the key.
* @param count the number of fields to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return array-reply the key and value.
* @since 6.1
*/
Mono<KeyValue<K, V>> hrandfieldWithvalues(K key);

/**
* Return {@code count} random fields along their value from the hash stored at {@code key}.
*
* @param key the key.
* @param count the number of fields to return. If the provided count argument is positive, return an array of distinct
* fields.
* @return array-reply the keys and values.
* @since 6.1
*/
Flux<KeyValue<K, V>> hrandfieldWithvalues(K key, long count);

/**
* Incrementally iterate hash fields and associated values.
*
Expand Down
Loading

0 comments on commit 7755da8

Please sign in to comment.