Skip to content

Commit

Permalink
Add support for ZDIFF and ZDIFFSTORE commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed Jan 13, 2021
1 parent 9a5de3f commit 26db17f
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,21 @@ public RedisFuture<Long> zcount(K key, Range<? extends Number> range) {
return dispatch(commandBuilder.zcount(key, range));
}

@Override
public RedisFuture<List<V>> zdiff(K... keys) {
return dispatch(commandBuilder.zdiff(keys));
}

@Override
public RedisFuture<Long> zdiffstore(K destKey, K... srcKeys) {
return dispatch(commandBuilder.zdiffstore(destKey, srcKeys));
}

@Override
public RedisFuture<List<ScoredValue<V>>> zdiffWithScores(K... keys) {
return dispatch(commandBuilder.zdiffWithScores(keys));
}

@Override
public RedisFuture<Double> zincrby(K key, double amount, V member) {
return dispatch(commandBuilder.zincrby(key, amount, member));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,21 @@ public Mono<Long> zcount(K key, Range<? extends Number> range) {
return createMono(() -> commandBuilder.zcount(key, range));
}

@Override
public Flux<V> zdiff(K... keys) {
return createDissolvingFlux(() -> commandBuilder.zdiff(keys));
}

@Override
public Mono<Long> zdiffstore(K destKey, K... srcKeys) {
return createMono(() -> commandBuilder.zdiffstore(destKey, srcKeys));
}

@Override
public Flux<ScoredValue<V>> zdiffWithScores(K... keys) {
return createDissolvingFlux(() -> commandBuilder.zdiffWithScores(keys));
}

@Override
public Mono<Double> zincrby(K key, double amount, V member) {
return createMono(() -> commandBuilder.zincrby(key, amount, member));
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,32 @@ Command<K, V, Long> zcount(K key, Range<? extends Number> range) {
return createCommand(ZCOUNT, new IntegerOutput<>(codec), args);
}

Command<K, V, List<V> > zdiff(K... keys) {
notEmpty(keys);

CommandArgs<K, V> args = new CommandArgs<>(codec);
args.add(keys.length).addKeys(keys);
System.out.println(args);
return createCommand(ZDIFF, new ValueListOutput<>(codec), args);
}

Command<K, V, Long> zdiffstore(K destKey, K... srcKeys) {
notNullKey(destKey);
notEmpty(srcKeys);

CommandArgs<K, V> args = new CommandArgs<>(codec);
args.addKey(destKey).add(srcKeys.length).addKeys(srcKeys);
return createCommand(ZDIFFSTORE, new IntegerOutput<>(codec), args);
}

Command<K, V, List<ScoredValue<V>>> zdiffWithScores(K... keys) {
notEmpty(keys);

CommandArgs<K, V> args = new CommandArgs<>(codec);
args.add(keys.length).addKeys(keys).add(WITHSCORES);
return createCommand(ZDIFF, new ScoredValueListOutput<>(codec), args);
}

Command<K, V, Double> zincrby(K key, double amount, V member) {
notNullKey(key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ public interface RedisSortedSetAsyncCommands<K, V> {
*/
RedisFuture<Long> zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of elements.
* @since 6.2
*/
RedisFuture<List<V>> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
RedisFuture<Long> zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of scored values.
* @since 6.2
*/
RedisFuture<List<ScoredValue<V>>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ public interface RedisSortedSetReactiveCommands<K, V> {
*/
Mono<Long> zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return V array-reply list of elements.
* @since 6.2
*/
Flux<V> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
Mono<Long> zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return V array-reply list of scored values.
* @since 6.2
*/
Flux<ScoredValue<V>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisSortedSetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ public interface RedisSortedSetCommands<K, V> {
*/
Long zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of elements.
* @since 6.2
*/
List<V> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
Long zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of scored values.
* @since 6.2
*/
List<ScoredValue<V>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ public interface NodeSelectionSortedSetAsyncCommands<K, V> {
*/
AsyncExecutions<Long> zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of elements.
* @since 6.2
*/
AsyncExecutions<List<V>> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
AsyncExecutions<Long> zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of scored values.
* @since 6.2
*/
AsyncExecutions<List<ScoredValue<V>>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ public interface NodeSelectionSortedSetCommands<K, V> {
*/
Executions<Long> zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of elements.
* @since 6.2
*/
Executions<List<V>> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
Executions<Long> zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of scored values.
* @since 6.2
*/
Executions<List<ScoredValue<V>>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public enum CommandType implements ProtocolKeyword {

// Sorted Set

BZPOPMIN, BZPOPMAX, ZADD, ZCARD, ZCOUNT, ZINCRBY, ZINTER, ZINTERSTORE, ZLEXCOUNT, ZMSCORE, ZPOPMIN, ZPOPMAX, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYLEX, ZREVRANGEBYSCORE, ZREVRANK, ZSCAN, ZSCORE, ZUNION, ZUNIONSTORE, ZREMRANGEBYLEX, ZRANGEBYLEX,
BZPOPMIN, BZPOPMAX, ZADD, ZCARD, ZCOUNT, ZDIFF, ZDIFFSTORE, ZINCRBY, ZINTER, ZINTERSTORE, ZLEXCOUNT, ZMSCORE, ZPOPMIN, ZPOPMAX, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYLEX, ZREVRANGEBYSCORE, ZREVRANK, ZSCAN, ZSCORE, ZUNION, ZUNIONSTORE, ZREMRANGEBYLEX, ZRANGEBYLEX,

// Scripting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ interface RedisSortedSetCoroutinesCommands<K : Any, V : Any> {
*/
suspend fun zcount(key: K, range: Range<out Number>): Long?

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List<V> array-reply list of elements.
* @since 6.2
*/
suspend fun zdiff(vararg keys: K): Flow<V>

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
suspend fun zdiffstore(destKey: K, vararg srcKeys: K): Long?

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List<V> array-reply list of scored values.
* @since 6.2
*/
suspend fun zdiffWithScores(vararg keys: K): Flow<ScoredValue<V>>

/**
* Increment the score of a member in a sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ internal class RedisSortedSetCoroutinesCommandsImpl<K : Any, V : Any>(internal v

override suspend fun zcount(key: K, range: Range<out Number>): Long? = ops.zcount(key, range).awaitFirstOrNull()

override suspend fun zdiff(vararg keys: K): Flow<V> = ops.zdiff(*keys).asFlow()

override suspend fun zdiffstore(destKey: K, vararg srcKeys: K): Long? = ops.zdiffstore(destKey, *srcKeys).awaitFirstOrNull()

override suspend fun zdiffWithScores(vararg keys: K): Flow<ScoredValue<V>> = ops.zdiffWithScores(*keys).asFlow()

override suspend fun zincrby(key: K, amount: Double, member: V): Double? = ops.zincrby(key, amount, member).awaitFirstOrNull()

override fun zinter(vararg keys: K): Flow<V> = ops.zinter(*keys).asFlow()
Expand Down
28 changes: 28 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisSortedSetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,34 @@ public interface RedisSortedSetCommands<K, V> {
*/
Long zcount(K key, Range<? extends Number> range);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of elements.
* @since 6.2
*/
List<V> zdiff(K... keys);

/**
* Computes the difference between the first and all successive input sorted sets and stores the result in destination.
*
* @param destKey the dest key.
* @param srcKeys the src keys.
* @return Long the number of elements in the resulting sorted set at destination.
* @since 6.2
*/
Long zdiffstore(K destKey, K... srcKeys);

/**
* Computes the difference between the first and all successive input sorted sets.
*
* @param keys the keys.
* @return List&lt;V&gt; array-reply list of scored values.
* @since 6.2
*/
List<ScoredValue<V>> zdiffWithScores(K... keys);

/**
* Increment the score of a member in a sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,38 @@ void zcount() {
assertThat(redis.zcount(key, Range.unbounded())).isEqualTo(3);
}

@Test
@EnabledOnCommand("ZDIFF") // Redis 6.2
void zdiff() {
String zset1 = "zset1";
String zset2 = "zset2";

assertThat(redis.zadd(zset1, 1.0, "one")).isEqualTo(1);
assertThat(redis.zadd(zset1, 2.0, "two")).isEqualTo(1);
assertThat(redis.zadd(zset1, 3.0, "three")).isEqualTo(1);
assertThat(redis.zadd(zset2, 1.0, "one")).isEqualTo(1);
assertThat(redis.zadd(zset2, 2.0, "two")).isEqualTo(1);

assertThat(redis.zdiff(zset1, zset2)).isEqualTo(list("three"));
assertThat(redis.zdiffWithScores(zset1, zset2)).isEqualTo(svlist(sv(3.0, "three")));
}

@Test
@EnabledOnCommand("ZDIFFSTORE") // Redis 6.2
void zdiffstore() {
String zset1 = "zset1";
String zset2 = "zset2";

assertThat(redis.zadd(zset1, 1.0, "one")).isEqualTo(1);
assertThat(redis.zadd(zset1, 2.0, "two")).isEqualTo(1);
assertThat(redis.zadd(zset1, 3.0, "three")).isEqualTo(1);
assertThat(redis.zadd(zset2, 1.0, "one")).isEqualTo(1);
assertThat(redis.zadd(zset2, 2.0, "two")).isEqualTo(1);

assertThat(redis.zdiffstore("out", zset1, zset2)).isEqualTo(1);
assertThat(redis.zrangeWithScores("out", 0, -1)).isEqualTo(svlist(sv(3.0, "three")));
}

@Test
void zincrby() {
assertThat(redis.zincrby(key, 0.0, "a")).isEqualTo(0, offset(0.1));
Expand Down

0 comments on commit 26db17f

Please sign in to comment.