All Downloads are FREE. Search and download functionalities are using the official Maven repository.
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.LettuceReactiveClusterServerCommands Maven / Gradle / Ivy
/*
* Copyright 2017-2018 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
*
* http://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.api.reactive.RedisServerReactiveCommands;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.ClusterTopologyProvider;
import org.springframework.data.redis.connection.ReactiveClusterServerCommands;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.util.Assert;
/**
* {@link ReactiveClusterServerCommands} implementation for {@literal Lettuce}.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0
*/
class LettuceReactiveClusterServerCommands extends LettuceReactiveServerCommands
implements ReactiveClusterServerCommands {
private final LettuceReactiveRedisClusterConnection connection;
private final ClusterTopologyProvider topologyProvider;
/**
* Create new {@link LettuceReactiveClusterServerCommands}.
*
* @param connection must not be {@literal null}.
* @param topologyProvider must not be {@literal null}.
* @throws IllegalArgumentException when {@code connection} is {@literal null}.
* @throws IllegalArgumentException when {@code topologyProvider} is {@literal null}.
*/
LettuceReactiveClusterServerCommands(LettuceReactiveRedisClusterConnection connection,
ClusterTopologyProvider topologyProvider) {
super(connection);
this.connection = connection;
this.topologyProvider = topologyProvider;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#bgReWriteAof(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono bgReWriteAof(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::bgrewriteaof).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#bgSave(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono bgSave(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::bgsave).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#lastSave(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono lastSave(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::lastsave).map(Date::getTime).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#save(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono save(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::save).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#dbSize(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono dbSize(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::dbsize).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono flushDb(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::flushdb).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#flushAll(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono flushAll(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::flushall).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#info()
*/
@Override
public Mono info() {
return Flux.merge(executeOnAllNodes(this::info)).collect(PropertiesCollector.INSTANCE);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#info(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono info(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::info) //
.map(LettuceConverters::toProperties) //
.next();
}
/* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#info(java.lang.String)
*/
@Override
public Mono info(String section) {
Assert.hasText(section, "Section must not be null or empty!");
return Flux.merge(executeOnAllNodes(redisClusterNode -> info(redisClusterNode, section)))
.collect(PropertiesCollector.INSTANCE);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#info(org.springframework.data.redis.connection.RedisClusterNode, java.lang.String)
*/
@Override
public Mono info(RedisClusterNode node, String section) {
Assert.hasText(section, "Section must not be null or empty!");
return connection.execute(node, c -> c.info(section)) //
.map(LettuceConverters::toProperties).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#getConfig(java.lang.String)
*/
@Override
public Mono getConfig(String pattern) {
Assert.hasText(pattern, "Pattern must not be null or empty!");
return Flux.merge(executeOnAllNodes(node -> getConfig(node, pattern))) //
.collect(PropertiesCollector.INSTANCE);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#getConfig(org.springframework.data.redis.connection.RedisClusterNode, java.lang.String)
*/
@Override
public Mono getConfig(RedisClusterNode node, String pattern) {
Assert.hasText(pattern, "Pattern must not be null or empty!");
return connection.execute(node, c -> c.configGet(pattern)) //
.map(LettuceConverters::toProperties) //
.next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#setConfig(java.lang.String, java.lang.String)
*/
@Override
public Mono setConfig(String param, String value) {
return Flux.merge(executeOnAllNodes(node -> setConfig(node, param, value))).map(Tuple2::getT2).last();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#setConfig(org.springframework.data.redis.connection.RedisClusterNode, java.lang.String, java.lang.String)
*/
@Override
public Mono setConfig(RedisClusterNode node, String param, String value) {
Assert.hasText(param, "Parameter must not be null or empty!");
Assert.hasText(value, "Value must not be null or empty!");
return connection.execute(node, c -> c.configSet(param, value)).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#resetConfigStats()
*/
@Override
public Mono resetConfigStats() {
return Flux.merge(executeOnAllNodes(this::resetConfigStats)).map(Tuple2::getT2).last();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#resetConfigStats(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono resetConfigStats(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::configResetstat).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#time(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Mono time(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::time) //
.map(ByteUtils::getBytes) //
.collectList() //
.map(LettuceConverters.toTimeConverter()::convert);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceReactiveServerCommands#getClientList()
*/
@Override
public Flux getClientList() {
return Flux.merge(executeOnAllNodesMany(this::getClientList)).map(Tuple2::getT2);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#getClientList(org.springframework.data.redis.connection.RedisClusterNode)
*/
@Override
public Flux getClientList(RedisClusterNode node) {
return connection.execute(node, RedisServerReactiveCommands::clientList)
.concatMapIterable(LettuceConverters.stringToRedisClientListConverter()::convert);
}
private Collection>> executeOnAllNodes(
Function> callback) {
Set nodes = topologyProvider.getTopology().getNodes();
List>> pipeline = new ArrayList<>(nodes.size());
for (RedisClusterNode redisClusterNode : nodes) {
pipeline.add(callback.apply(redisClusterNode).map(p -> Tuples.of(redisClusterNode, p)));
}
return pipeline;
}
private Collection>> executeOnAllNodesMany(
Function> callback) {
Set nodes = topologyProvider.getTopology().getNodes();
List>> pipeline = new ArrayList<>(nodes.size());
for (RedisClusterNode redisClusterNode : nodes) {
pipeline.add(callback.apply(redisClusterNode).map(p -> Tuples.of(redisClusterNode, p)));
}
return pipeline;
}
/**
* Collector to merge {@link Tuple2} of {@link RedisClusterNode} and {@link Properties} into a single
* {@link Properties} object by prefixing original the keys with {@link RedisClusterNode#asString()}.
*/
private enum PropertiesCollector implements Collector, Properties, Properties> {
INSTANCE;
/*
* (non-Javadoc)
* @see java.util.stream.Collector#supplier()
*/
@Override
public Supplier supplier() {
return Properties::new;
}
/* (non-Javadoc)
* @see java.util.stream.Collector#accumulator()
*/
@Override
public BiConsumer> accumulator() {
return (properties, tuple) -> {
for (Entry entry : tuple.getT2().entrySet()) {
properties.put(tuple.getT1().asString() + "." + entry.getKey(), entry.getValue());
}
};
}
/*
* (non-Javadoc)
* @see java.util.stream.Collector#combiner()
*/
@Override
public BinaryOperator combiner() {
return (left, right) -> {
Properties merge = new Properties();
merge.putAll(left);
merge.putAll(right);
return merge;
};
}
/*
* (non-Javadoc)
* @see java.util.stream.Collector#finisher()
*/
@Override
public Function finisher() {
return properties -> properties;
}
/*
* (non-Javadoc)
* @see java.util.stream.Collector#characteristics()
*/
@Override
public Set characteristics() {
return new HashSet<>(Arrays.asList(Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH));
}
}
}