Skip to content

Commit

Permalink
Add support for SINTERCARD command #1902
Browse files Browse the repository at this point in the history
Original pull request: #1980.
  • Loading branch information
dengliming authored and mp911de committed Feb 28, 2022
1 parent 1cae15f commit c4a053c
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,16 @@ public RedisFuture<Long> sinter(ValueStreamingChannel<V> channel, K... keys) {
return dispatch(commandBuilder.sinter(channel, keys));
}

@Override
public RedisFuture<Long> sintercard(K... keys) {
return dispatch(commandBuilder.sintercard(keys));
}

@Override
public RedisFuture<Long> sintercard(int limit, K... keys) {
return dispatch(commandBuilder.sintercard(limit, keys));
}

@Override
public RedisFuture<Long> sinterstore(K destination, K... keys) {
return dispatch(commandBuilder.sinterstore(destination, keys));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,16 @@ public Mono<Long> sinter(ValueStreamingChannel<V> channel, K... keys) {
return createMono(() -> commandBuilder.sinter(channel, keys));
}

@Override
public Mono<Long> sintercard(K... keys) {
return createMono(() -> commandBuilder.sintercard(keys));
}

@Override
public Mono<Long> sintercard(int limit, K... keys) {
return createMono(() -> commandBuilder.sintercard(limit, keys));
}

@Override
public Mono<Long> sinterstore(K destination, K... keys) {
return createMono(() -> commandBuilder.sinterstore(destination, keys));
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,20 @@ Command<K, V, Long> sinter(ValueStreamingChannel<V> channel, K... keys) {
return createCommand(SINTER, new ValueStreamingOutput<>(codec, channel), args);
}

Command<K, V, Long> sintercard(K... keys) {
notEmpty(keys);

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

Command<K, V, Long> sintercard(int limit, K... keys) {
notEmpty(keys);

CommandArgs<K, V> args = new CommandArgs<>(codec).add(keys.length).addKeys(keys).add(LIMIT).add(limit);
return createCommand(SINTERCARD, new IntegerOutput<>(codec), args);
}

Command<K, V, Long> sinterstore(K destination, K... keys) {
LettuceAssert.notNull(destination, "Destination " + MUST_NOT_BE_NULL);
notEmpty(keys);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/lettuce/core/api/async/RedisSetAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ public interface RedisSetAsyncCommands<K, V> {
*/
RedisFuture<Long> sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
RedisFuture<Long> sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
RedisFuture<Long> sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ public interface RedisSetReactiveCommands<K, V> {
@Deprecated
Mono<Long> sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Mono<Long> sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Mono<Long> sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisSetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ public interface RedisSetCommands<K, V> {
*/
Long sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Long sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Long sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,6 +97,26 @@ public interface NodeSelectionSetAsyncCommands<K, V> {
*/
AsyncExecutions<Long> sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
AsyncExecutions<Long> sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
AsyncExecutions<Long> sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ public interface NodeSelectionSetCommands<K, V> {
*/
Executions<Long> sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Executions<Long> sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Executions<Long> sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
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 @@ -74,7 +74,7 @@ public enum CommandType implements ProtocolKeyword {

// Sets

SADD, SCARD, SDIFF, SDIFFSTORE, SINTER, SINTERSTORE, SISMEMBER, SMISMEMBER, SMEMBERS, SMOVE, SPOP, SRANDMEMBER, SREM, SSCAN, SUNION, SUNIONSTORE,
SADD, SCARD, SDIFF, SDIFFSTORE, SINTER, SINTERCARD, SINTERSTORE, SISMEMBER, SMISMEMBER, SMEMBERS, SMOVE, SPOP, SRANDMEMBER, SREM, SSCAN, SUNION, SUNIONSTORE,

// Sorted Set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package io.lettuce.core.sentinel.api.async;

import java.net.SocketAddress;
import java.util.List;
import java.util.Map;

import java.util.List;
import java.net.SocketAddress;
import io.lettuce.core.KillArgs;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.output.CommandOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ interface RedisSetCoroutinesCommands<K : Any, V : Any> {
*/
fun sinter(vararg keys: K): Flow<V>

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
suspend fun sintercard(vararg keys: K): Long?

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
suspend fun sintercard(limit: Int, vararg keys: K): Long?

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal class RedisSetCoroutinesCommandsImpl<K : Any, V : Any>(internal val ops

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

override suspend fun sintercard(vararg keys: K): Long? = ops.sintercard(*keys).awaitFirstOrNull()

override suspend fun sintercard(limit: Int, vararg keys: K): Long? = ops.sintercard(limit, *keys).awaitFirstOrNull()

override suspend fun sinterstore(destination: K, vararg keys: K): Long? = ops.sinterstore(destination, *keys).awaitFirstOrNull()

override suspend fun sismember(key: K, member: V): Boolean? = ops.sismember(key, member).awaitFirstOrNull()
Expand Down
20 changes: 20 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisSetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ public interface RedisSetCommands<K, V> {
*/
Long sinter(ValueStreamingChannel<V> channel, K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Long sintercard(K... keys);

/**
* This command works exactly like sinter but instead of returning the result set, it returns just the cardinality of the
* result.
*
* @param limit If the intersection cardinality reaches limit partway through the computation, the algorithm will exit and
* yield limit as the cardinality.
* @param keys the key.
* @return The cardinality of the set which would result from the intersection of all the given sets.
*/
Long sintercard(int limit, K... keys);

/**
* Intersect multiple sets and store the resulting set in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ void sinter() {
assertThat(redis.sinter("key1", "key2", "key3")).isEqualTo(set("c"));
}

@Test
@EnabledOnCommand("SINTERCARD") // Redis 7.0
void sintercard() {
setupSet();
assertThat(redis.sintercard("key1", "key3")).isEqualTo(2);
assertThat(redis.sintercard(1, "key1", "key3")).isEqualTo(1);
}

@Test
void sinterStreaming() {
setupSet();
Expand Down

0 comments on commit c4a053c

Please sign in to comment.