Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,16 @@ public Long zInterStore(byte[] destKey, byte[]... sets) {
return convertAndReturn(delegate.zInterStore(destKey, sets), Converters.identityConverter());
}

@Override
public Long zInterCard(byte[]... sets) {
return convertAndReturn(delegate.zInterCard(sets), Converters.identityConverter());
}

@Override
public Long zInterCard(long limit, byte[]... sets) {
return convertAndReturn(delegate.zInterCard(limit, sets), Converters.identityConverter());
}

@Override
public Set<byte[]> zRange(byte[] key, long start, long end) {
return convertAndReturn(delegate.zRange(key, start, end), Converters.identityConverter());
Expand Down Expand Up @@ -2089,6 +2099,11 @@ public Long zInterStore(String destKey, String... sets) {
return zInterStore(serialize(destKey), serializeMulti(sets));
}

@Override
public Long zInterCard(String... keys) {
return zInterCard(serializeMulti(keys));
}

@Override
public byte[] zRandMember(byte[] key) {
return delegate.zRandMember(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,20 @@ default Long zInterStore(byte[] destKey, byte[]... sets) {
return zSetCommands().zInterStore(destKey, sets);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default Long zInterCard(byte[]... sets) {
return zSetCommands().zInterCard(sets);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default Long zInterCard(long limit, byte[]... sets) {
return zSetCommands().zInterCard(limit, sets);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,72 @@ default Mono<Long> zInterStore(ByteBuffer destinationKey, List<ByteBuffer> sets,
*/
Flux<NumericResponse<ZAggregateStoreCommand, Long>> zInterStore(Publisher<? extends ZAggregateStoreCommand> commands);

/**
* {@code ZINTERCARD} command parameters.
*
* @author GyeongHoe Koo
* @since 4.0
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
*/
class ZInterCardCommand implements Command {

private final List<ByteBuffer> keys;

private ZInterCardCommand(List<ByteBuffer> keys) {
this.keys = keys;
}

/**
* Creates a new {@link ZInterCardCommand} given a {@link Collection} of keys.
*
* @param keys must not be {@literal null}.
* @return a new {@link ZInterCardCommand} for a {@link Collection} of keys.
*/
public static ZInterCardCommand keys(Collection<ByteBuffer> keys) {

Assert.notNull(keys, "Keys must not be null");

return new ZInterCardCommand(new ArrayList<>(keys));
}

@Override
public @Nullable ByteBuffer getKey() {
return null;
}

/**
* @return never {@literal null}.
*/
public List<ByteBuffer> getKeys() {
return keys;
}
}

/**
* Get the number of elements in the intersection of all given sorted sets.
*
* @param keys must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
* @since 4.0
*/
default Mono<Long> zInterCard(List<ByteBuffer> keys) {

Assert.notNull(keys, "Keys must not be null");

return zInterCard(Mono.just(ZInterCardCommand.keys(keys))).next().map(NumericResponse::getOutput);
}

/**
* Get the number of elements in the intersection of all given sorted sets.
*
* @param commands must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
* @since 4.0
*/
Flux<NumericResponse<ZInterCardCommand, Long>> zInterCard(Publisher<ZInterCardCommand> commands);

/**
* Union sorted {@literal sets}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @author Mark Paluch
* @author Andrey Shlykov
* @author Shyngys Sapraliyev
* @author GyeongHoe Koo
* @see RedisCommands
*/
@NullUnmarked
Expand Down Expand Up @@ -1085,6 +1086,28 @@ default Long zInterStore(byte @NonNull [] destKey, @NonNull Aggregate aggregate,
Long zInterStore(byte @NonNull [] destKey, @NonNull Aggregate aggregate, @NonNull Weights weights,
byte @NonNull [] @NonNull... sets);

/**
* Get the number of members in the intersection of sorted {@code sets}.
*
* @param sets must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 4.0
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
*/
Long zInterCard(byte @NonNull [] @NonNull... sets);

/**
* Get the number of members in the intersection of sorted {@code sets}.
*
* @param sets must not be {@literal null}.
* @param limit the maximum cardinality to compute. If the intersection has more than {@code limit} elements,
* the computation stops and returns {@code limit}.
* @return {@literal null} when used in pipeline / transaction.
* @since 4.0
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
*/
Long zInterCard(long limit, byte @NonNull [] @NonNull... sets);

/**
* Union sorted {@code sets}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,17 @@ Set<StringTuple> zInterWithScores(@NonNull Aggregate aggregate, @NonNull Weights
Long zInterStore(@NonNull String destKey, @NonNull Aggregate aggregate, int @NonNull [] weights,
@NonNull String @NonNull... sets);

/**
* Get the number of elements in the intersection of the sorted sets at the given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 4.0
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
* @see RedisZSetCommands#zInterCard(byte[]...)
*/
Long zInterCard(@NonNull String @NonNull... keys);

/**
* Union sorted {@code sets}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,42 @@ public Long zUnionStore(byte @NonNull [] destKey, @NonNull Aggregate aggregate,
throw new InvalidDataAccessApiUsageException("ZUNIONSTORE can only be executed when all keys map to the same slot");
}

@Override
public Long zInterCard(byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {

try {
return connection.getCluster().zintercard(sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

throw new InvalidDataAccessApiUsageException("ZINTERCARD can only be executed when all keys map to the same slot");
}

@Override
public Long zInterCard(long limit, byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {

try {
return connection.getCluster().zintercard(limit, sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

throw new InvalidDataAccessApiUsageException("ZINTERCARD can only be executed when all keys map to the same slot");
}

@Override
public Cursor<@NonNull Tuple> zScan(byte @NonNull [] key, @NonNull ScanOptions options) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,24 @@ public Long zUnionStore(byte @NonNull [] destKey, byte @NonNull [] @NonNull... s
return connection.invoke().just(Jedis::zunionstore, PipelineBinaryCommands::zunionstore, destKey, sets);
}

@Override
public Long zInterCard(byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

return connection.invoke().just(Jedis::zintercard, PipelineBinaryCommands::zintercard, sets);
}

@Override
public Long zInterCard(long limit, byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

return connection.invoke().just(Jedis::zintercard, PipelineBinaryCommands::zintercard, limit, sets);
}

@Override
public Cursor<@NonNull Tuple> zScan(byte @NonNull [] key, @NonNull ScanOptions options) {
return zScan(key, CursorId.initial(), options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,19 @@ public Flux<NumericResponse<ZAggregateStoreCommand, Long>> zInterStore(
}));
}

@Override
public Flux<NumericResponse<ZInterCardCommand, Long>> zInterCard(Publisher<ZInterCardCommand> commands) {

return this.connection.execute(reactiveCommands -> Flux.from(commands).concatMap(command -> {

Assert.notEmpty(command.getKeys(), "Keys must not be null or empty");

ByteBuffer[] keys = command.getKeys().toArray(new ByteBuffer[0]);

return reactiveCommands.zintercard(keys).map(value -> new NumericResponse<>(command, value));
}));
}

@Override
public Flux<CommandResponse<ZAggregateCommand, Flux<ByteBuffer>>> zUnion(
Publisher<? extends ZAggregateCommand> commands) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,24 @@ public Long zUnionStore(byte @NonNull [] destKey, byte @NonNull [] @NonNull... s
return connection.invoke().just(RedisSortedSetAsyncCommands::zunionstore, destKey, sets);
}

@Override
public Long zInterCard(byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

return connection.invoke().just(RedisSortedSetAsyncCommands::zintercard, sets);
}

@Override
public Long zInterCard(long limit, byte @NonNull [] @NonNull... sets) {

Assert.notNull(sets, "Sets must not be null");
Assert.noNullElements(sets, "Sets must not contain null elements");

return connection.invoke().just(RedisSortedSetAsyncCommands::zintercard, limit, sets);
}

@Override
public Cursor<@NonNull Tuple> zScan(byte @NonNull [] key, @Nullable ScanOptions options) {
return zScan(key, CursorId.initial(), options != null ? options : ScanOptions.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ public Mono<Long> intersectAndStore(K key, Collection<K> otherKeys, K destKey, A
.flatMap(serialized -> zSetCommands.zInterStore(rawKey(destKey), serialized, weights, aggregate)));
}

@Override
public Mono<Long> intersectSize(K key, Collection<K> otherKeys) {

Assert.notNull(key, "Key must not be null");
Assert.notNull(otherKeys, "Other keys must not be null");

return createMono(zSetCommands -> Flux.fromIterable(getKeys(key, otherKeys)) //
.map(this::rawKey) //
.collectList() //
.flatMap(zSetCommands::zInterCard));
}

@Override
public Flux<V> union(K key, Collection<K> otherKeys) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @author Shyngys Sapraliyev
* @author John Blum
* @author Gunha Hwang
* @author GyeongHoe Koo
*/
@NullUnmarked
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {
Expand Down Expand Up @@ -588,6 +589,22 @@ public Long intersectAndStore(@NonNull K key, Collection<@NonNull K> otherKeys,
return execute(connection -> connection.zInterStore(rawDestKey, aggregate, weights, rawKeys));
}

@Override
public Long intersectSize(@NonNull K key, @NonNull Collection<@NonNull K> otherKeys) {

byte[][] rawKeys = rawKeys(key, otherKeys);

return execute(connection -> connection.zInterCard(rawKeys));
}

@Override
public Long intersectSize(@NonNull K key, @NonNull Collection<@NonNull K> otherKeys, long limit) {

byte[][] rawKeys = rawKeys(key, otherKeys);

return execute(connection -> connection.zInterCard(limit, rawKeys));
}

@Override
public Set<V> union(@NonNull K key, @NonNull Collection<@NonNull K> otherKeys) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,32 @@ default Mono<Long> intersectAndStore(@NonNull K key, @NonNull Collection<@NonNul
Mono<Long> intersectAndStore(@NonNull K key, @NonNull Collection<@NonNull K> otherKeys, @NonNull K destKey,
@NonNull Aggregate aggregate, @NonNull Weights weights);

/**
* Returns the cardinality of the sorted set which would result from the intersection of {@code key} and
* {@code otherKey}.
*
* @param key must not be {@literal null}.
* @param otherKey must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
* @since 4.0
*/
default Mono<Long> intersectSize(@NonNull K key, @NonNull K otherKey) {
return intersectSize(key, Collections.singleton(otherKey));
}

/**
* Returns the cardinality of the sorted set which would result from the intersection of {@code key} and
* {@code otherKeys}.
*
* @param key must not be {@literal null}.
* @param otherKeys must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zintercard">Redis Documentation: ZINTERCARD</a>
* @since 4.0
*/
Mono<Long> intersectSize(@NonNull K key, @NonNull Collection<@NonNull K> otherKeys);

/**
* Union sorted {@code sets}.
*
Expand Down
Loading