Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.springframework.data.redis.connection.lettuce.LettuceReactiveZSetCommands Maven / Gradle / Ivy
/*
* Copyright 2016-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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.Range;
import io.lettuce.core.ScanStream;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.Value;
import io.lettuce.core.ZAddArgs;
import io.lettuce.core.ZStoreArgs;
import org.springframework.data.redis.core.TimeoutUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.redis.connection.DefaultTuple;
import org.springframework.data.redis.connection.ReactiveRedisConnection.CommandResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyScanCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.MultiValueResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import org.springframework.data.redis.connection.ReactiveZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* @author Christoph Strobl
* @author Mark Paluch
* @author Michele Mancioppi
* @author Andrey Shlykov
* @since 2.0
*/
class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
private final LettuceReactiveRedisConnection connection;
/**
* Create new {@link LettuceReactiveZSetCommands}.
*
* @param connection must not be {@literal null}.
*/
LettuceReactiveZSetCommands(LettuceReactiveRedisConnection connection) {
Assert.notNull(connection, "Connection must not be null!");
this.connection = connection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zAdd(org.reactivestreams.Publisher)
*/
@Override
@SuppressWarnings("unchecked")
public Flux> zAdd(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notEmpty(command.getTuples(), "Tuples must not be empty or null!");
ZAddArgs args = null;
if (command.isIncr() || command.isUpsert() || command.isReturnTotalChanged()) {
if (command.isIncr()) {
if (command.getTuples().size() > 1) {
throw new IllegalArgumentException("ZADD INCR must not contain more than one tuple!");
}
Tuple tuple = command.getTuples().iterator().next();
return cmd.zaddincr(command.getKey(), tuple.getScore(), ByteBuffer.wrap(tuple.getValue()))
.map(value -> new NumericResponse<>(command, value));
}
if (command.isReturnTotalChanged()) {
args = ZAddArgs.Builder.ch();
}
if (command.isUpsert()) {
args = args == null ? ZAddArgs.Builder.nx() : args.nx();
} else {
args = args == null ? ZAddArgs.Builder.xx() : args.xx();
}
if (command.isGt()) {
args = args == null ? ZAddArgs.Builder.gt() : args.gt();
}
if (command.isLt()) {
args = args == null ? ZAddArgs.Builder.lt() : args.lt();
}
}
ScoredValue[] values = (ScoredValue[]) command.getTuples().stream()
.map(tuple -> ScoredValue.fromNullable(tuple.getScore(), ByteBuffer.wrap(tuple.getValue())))
.toArray(ScoredValue[]::new);
Mono result = args == null ? cmd.zadd(command.getKey(), values) : cmd.zadd(command.getKey(), args, values);
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRem(org.reactivestreams.Publisher)
*/
@Override
public Flux> zRem(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notEmpty(command.getValues(), "Values must not be null or empty!");
return cmd.zrem(command.getKey(), command.getValues().stream().toArray(ByteBuffer[]::new))
.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zIncrBy(org.reactivestreams.Publisher)
*/
@Override
public Flux> zIncrBy(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getValue(), "Member must not be null!");
Assert.notNull(command.getIncrement(), "Increment value must not be null!");
return cmd.zincrby(command.getKey(), command.getIncrement().doubleValue(), command.getValue())
.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRandMember(Publisher)
*/
@Override
public Flux>> zRandMember(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
return new CommandResponse<>(command, cmd.zrandmember(command.getKey(), command.getCount()));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRandMemberWithScore(Publisher)
*/
@Override
public Flux>> zRandMemberWithScore(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
return new CommandResponse<>(command, cmd.zrandmemberWithScores(command.getKey(), command.getCount())
.map(this::toTuple));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRank(org.reactivestreams.Publisher)
*/
@Override
public Flux> zRank(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getValue(), "Value must not be null!");
Mono result = ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)
? cmd.zrank(command.getKey(), command.getValue())
: cmd.zrevrank(command.getKey(), command.getValue());
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRange(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zRange(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Flux result;
long start = LettuceConverters.getLowerBoundIndex(command.getRange());
long stop = LettuceConverters.getUpperBoundIndex(command.getRange());
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
if (command.isWithScores()) {
result = cmd.zrangeWithScores(command.getKey(), start, stop).map(this::toTuple);
} else {
result = cmd.zrange(command.getKey(), start, stop).map(value -> toTuple(value, Double.NaN));
}
} else {
if (command.isWithScores()) {
result = cmd.zrevrangeWithScores(command.getKey(), start, stop).map(this::toTuple);
} else {
result = cmd.zrevrange(command.getKey(), start, stop).map(value -> toTuple(value, Double.NaN));
}
}
return Mono.just(new CommandResponse<>(command, result));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRange(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zRangeByScore(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
boolean isLimited = command.getLimit().isPresent() && !command.getLimit().get().isUnlimited();
Publisher result;
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
Range range = RangeConverter.toRange(command.getRange());
if (command.isWithScores()) {
if (!isLimited) {
result = cmd.zrangebyscoreWithScores(command.getKey(), range).map(this::toTuple);
} else {
result = cmd
.zrangebyscoreWithScores(command.getKey(), range, LettuceConverters.toLimit(command.getLimit().get()))
.map(this::toTuple);
}
} else {
if (!isLimited) {
result = cmd.zrangebyscore(command.getKey(), range).map(value -> toTuple(value, Double.NaN));
} else {
result = cmd.zrangebyscore(command.getKey(), range, LettuceConverters.toLimit(command.getLimit().get()))
.map(value -> toTuple(value, Double.NaN));
}
}
} else {
Range range = RangeConverter.toRange(command.getRange());
if (command.isWithScores()) {
if (!isLimited) {
result = cmd.zrevrangebyscoreWithScores(command.getKey(), range).map(this::toTuple);
} else {
result = cmd.zrevrangebyscoreWithScores(command.getKey(), range,
LettuceConverters.toLimit(command.getLimit().get())).map(this::toTuple);
}
} else {
if (!isLimited) {
result = cmd.zrevrangebyscore(command.getKey(), range).map(value -> toTuple(value, Double.NaN));
} else {
result = cmd.zrevrangebyscore(command.getKey(), range, LettuceConverters.toLimit(command.getLimit().get()))
.map(value -> toTuple(value, Double.NaN));
}
}
}
return Mono.just(new CommandResponse<>(command, Flux.from(result)));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zScan(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zScan(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getOptions(), "ScanOptions must not be null!");
Flux result = ScanStream.zscan(cmd, command.getKey(), LettuceConverters.toScanArgs(command.getOptions()))
.map(this::toTuple);
return Mono.just(new CommandResponse<>(command, result));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zCount(org.reactivestreams.Publisher)
*/
@Override
public Flux> zCount(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Range range = RangeConverter.toRange(command.getRange());
Mono result = cmd.zcount(command.getKey(), range);
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zLexCount(org.reactivestreams.Publisher)
*/
@Override
public Flux> zLexCount(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Mono result = cmd.zlexcount(command.getKey(), RangeConverter.toRange(command.getRange()));
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zPop(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zPop(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Flux> result;
if (command.getCount() > 1) {
result = command.getDirection() == PopDirection.MIN ? cmd.zpopmin(command.getKey(), command.getCount())
: cmd.zpopmax(command.getKey(), command.getCount());
} else {
result = (command.getDirection() == PopDirection.MIN ? cmd.zpopmin(command.getKey())
: cmd.zpopmax(command.getKey())).flux();
}
return new CommandResponse<>(command, result.filter(Value::hasValue).map(this::toTuple));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#bZPop(org.reactivestreams.Publisher)
*/
@Override
public Flux>> bZPop(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getTimeout(), "Timeout must not be null!");
if(command.getTimeUnit() == TimeUnit.MILLISECONDS) {
double timeout = TimeoutUtils.toDoubleSeconds(command.getTimeout(), command.getTimeUnit());
Mono> result = (command.getDirection() == PopDirection.MIN
? cmd.bzpopmin(timeout, command.getKey())
: cmd.bzpopmax(timeout, command.getKey())).filter(Value::hasValue).map(Value::getValue);
return new CommandResponse<>(command, result.filter(Value::hasValue).map(this::toTuple).flux());
}
long timeout = command.getTimeUnit().toSeconds(command.getTimeout());
Mono> result = (command.getDirection() == PopDirection.MIN
? cmd.bzpopmin(timeout, command.getKey())
: cmd.bzpopmax(timeout, command.getKey())).filter(Value::hasValue).map(Value::getValue);
return new CommandResponse<>(command, result.filter(Value::hasValue).map(this::toTuple).flux());
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zCard(org.reactivestreams.Publisher)
*/
@Override
public Flux> zCard(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
return cmd.zcard(command.getKey()).map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zScore(org.reactivestreams.Publisher)
*/
@Override
public Flux> zScore(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getValue(), "Value must not be null!");
return cmd.zscore(command.getKey(), command.getValue()).map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zMScore(org.reactivestreams.Publisher)
*/
@Override
public Flux> zMScore(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getValues(), "Values must not be null!");
return cmd.zmscore(command.getKey(), command.getValues().toArray(new ByteBuffer[0]))
.map(value -> new MultiValueResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRemRangeByRank(org.reactivestreams.Publisher)
*/
@Override
public Flux> zRemRangeByRank(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Mono result = cmd.zremrangebyrank(command.getKey(), //
LettuceConverters.getLowerBoundIndex(command.getRange()), //
LettuceConverters.getUpperBoundIndex(command.getRange()));
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRemRangeByRank(org.reactivestreams.Publisher)
*/
@Override
public Flux> zRemRangeByScore(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Range range = RangeConverter.toRange(command.getRange());
Mono result = cmd.zremrangebyscore(command.getKey(), range);
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRemRangeByLex(org.reactivestreams.Publisher)
*/
@Override
public Flux> zRemRangeByLex(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Mono result = cmd.zremrangebylex(command.getKey(), RangeConverter.toRange(command.getRange()));
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zDiff(Publisher)
*/
@Override
public Flux>> zDiff(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getKeys(), "Keys must not be null or empty!");
ByteBuffer[] sourceKeys = command.getKeys().toArray(new ByteBuffer[0]);
return new CommandResponse<>(command, cmd.zdiff(sourceKeys));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zDiffWithScores(Publisher)
*/
@Override
public Flux>> zDiffWithScores(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getKeys(), "Keys must not be null or empty!");
ByteBuffer[] sourceKeys = command.getKeys().toArray(new ByteBuffer[0]);
return new CommandResponse<>(command, cmd.zdiffWithScores(sourceKeys).map(this::toTuple));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zDiffStore(Publisher)
*/
@Override
public Flux> zDiffStore(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Destination key must not be null!");
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ByteBuffer[] sourceKeys = command.getSourceKeys().toArray(new ByteBuffer[0]);
return cmd.zdiffstore(command.getKey(), sourceKeys).map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zInter(Publisher)
*/
@Override
public Flux>> zInter(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().toArray(new ByteBuffer[0]);
Flux result = args != null ? cmd.zinter(args, sourceKeys) : cmd.zinter(sourceKeys);
return new CommandResponse<>(command, result);
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zInterWithScores(Publisher)
*/
@Override
public Flux>> zInterWithScores(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().toArray(new ByteBuffer[0]);
Flux> result = args != null ? cmd.zinterWithScores(args, sourceKeys)
: cmd.zinterWithScores(sourceKeys);
return new CommandResponse<>(command, result.map(this::toTuple));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zInterStore(org.reactivestreams.Publisher)
*/
@Override
public Flux> zInterStore(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Destination key must not be null!");
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().toArray(new ByteBuffer[0]);
Mono result = args != null ? cmd.zinterstore(command.getKey(), args, sourceKeys)
: cmd.zinterstore(command.getKey(), sourceKeys);
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zUnion(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zUnion(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().stream().toArray(ByteBuffer[]::new);
Flux result = args != null ? cmd.zunion(args, sourceKeys) : cmd.zunion(sourceKeys);
return new CommandResponse<>(command, result);
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zUnion(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zUnionWithScores(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().stream().toArray(ByteBuffer[]::new);
Flux> result = args != null ? cmd.zunionWithScores(args, sourceKeys)
: cmd.zunionWithScores(sourceKeys);
return new CommandResponse<>(command, result.map(this::toTuple));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zUnionStore(org.reactivestreams.Publisher)
*/
@Override
public Flux> zUnionStore(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Destination key must not be null!");
Assert.notEmpty(command.getSourceKeys(), "Source keys must not be null or empty!");
ZStoreArgs args = null;
if (command.getAggregateFunction().isPresent() || !command.getWeights().isEmpty()) {
args = zStoreArgs(command.getAggregateFunction().isPresent() ? command.getAggregateFunction().get() : null,
command.getWeights());
}
ByteBuffer[] sourceKeys = command.getSourceKeys().stream().toArray(ByteBuffer[]::new);
Mono result = args != null ? cmd.zunionstore(command.getKey(), args, sourceKeys)
: cmd.zunionstore(command.getKey(), sourceKeys);
return result.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zRangeByLex(org.reactivestreams.Publisher)
*/
@Override
public Flux>> zRangeByLex(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Destination key must not be null!");
Flux result;
if (!command.getLimit().isUnlimited()) {
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
result = cmd.zrangebylex(command.getKey(), RangeConverter.toRange(command.getRange()),
LettuceConverters.toLimit(command.getLimit()));
} else {
result = cmd.zrevrangebylex(command.getKey(), RangeConverter.toRange(command.getRange()),
LettuceConverters.toLimit(command.getLimit()));
}
} else {
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
result = cmd.zrangebylex(command.getKey(), RangeConverter.toRange(command.getRange()));
} else {
result = cmd.zrevrangebylex(command.getKey(), RangeConverter.toRange(command.getRange()));
}
}
return Mono.just(new CommandResponse<>(command, result));
}));
}
private static ZStoreArgs zStoreArgs(@Nullable Aggregate aggregate, @Nullable List weights) {
ZStoreArgs args = new ZStoreArgs();
if (aggregate != null) {
switch (aggregate) {
case MIN:
args.min();
break;
case MAX:
args.max();
break;
default:
args.sum();
break;
}
}
if (weights != null) {
args.weights(weights.stream().mapToDouble(it -> it).toArray());
}
return args;
}
private Tuple toTuple(ScoredValue scoredValue) {
return scoredValue.map(it -> new DefaultTuple(ByteUtils.getBytes(it), scoredValue.getScore())).getValue();
}
private Tuple toTuple(ByteBuffer value, double score) {
return new DefaultTuple(ByteUtils.getBytes(value), score);
}
protected LettuceReactiveRedisConnection getConnection() {
return connection;
}
}