/*
* Copyright 2017 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.cluster.api.async.RedisClusterAsyncCommands;
import io.lettuce.core.cluster.api.sync.RedisClusterCommands;
import java.util.List;
import java.util.Properties;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.connection.lettuce.LettuceConnection.LettuceResult;
import org.springframework.data.redis.connection.lettuce.LettuceConnection.LettuceTxResult;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
/**
* @author Mark Paluch
* @since 2.0
*/
class LettuceServerCommands implements RedisServerCommands {
private final LettuceConnection connection;
public LettuceServerCommands(LettuceConnection connection) {
this.connection = connection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#bgReWriteAof()
*/
@Override
public void bgReWriteAof() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().bgrewriteaof()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().bgrewriteaof()));
return;
}
getConnection().bgrewriteaof();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#bgSave()
*/
@Override
public void bgSave() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().bgsave()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().bgsave()));
return;
}
getConnection().bgsave();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#lastSave()
*/
@Override
public Long lastSave() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().lastsave(), LettuceConverters.dateToLong()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().lastsave(), LettuceConverters.dateToLong()));
return null;
}
return LettuceConverters.toLong(getConnection().lastsave());
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#save()
*/
@Override
public void save() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().save()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().save()));
return;
}
getConnection().save();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#dbSize()
*/
@Override
public Long dbSize() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().dbsize()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().dbsize()));
return null;
}
return getConnection().dbsize();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#flushDb()
*/
@Override
public void flushDb() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().flushdb()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().flushdb()));
return;
}
getConnection().flushdb();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#flushAll()
*/
@Override
public void flushAll() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().flushall()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().flushall()));
return;
}
getConnection().flushall();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#info()
*/
@Override
public Properties info() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().info(), LettuceConverters.stringToProps()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().info(), LettuceConverters.stringToProps()));
return null;
}
return LettuceConverters.toProperties(getConnection().info());
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#info(java.lang.String)
*/
@Override
public Properties info(String section) {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().info(section), LettuceConverters.stringToProps()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().info(section), LettuceConverters.stringToProps()));
return null;
}
return LettuceConverters.toProperties(getConnection().info(section));
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#shutdown()
*/
@Override
public void shutdown() {
try {
if (isPipelined()) {
getAsyncConnection().shutdown(true);
return;
}
getConnection().shutdown(true);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption)
*/
@Override
public void shutdown(ShutdownOption option) {
if (option == null) {
shutdown();
return;
}
boolean save = ShutdownOption.SAVE.equals(option);
try {
if (isPipelined()) {
getAsyncConnection().shutdown(save);
return;
}
getConnection().shutdown(save);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#getConfig(java.lang.String)
*/
@Override
public List<String> getConfig(String param) {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().configGet(param)));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().configGet(param)));
return null;
}
return getConnection().configGet(param);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#setConfig(java.lang.String, java.lang.String)
*/
@Override
public void setConfig(String param, String value) {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().configSet(param, value)));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().configSet(param, value)));
return;
}
getConnection().configSet(param, value);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#resetConfigStats()
*/
@Override
public void resetConfigStats() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().configResetstat()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().configResetstat()));
return;
}
getConnection().configResetstat();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#time()
*/
@Override
public Long time() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().time(), LettuceConverters.toTimeConverter()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().time(), LettuceConverters.toTimeConverter()));
return null;
}
return LettuceConverters.toTimeConverter().convert(getConnection().time());
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#killClient(java.lang.String, int)
*/
@Override
public void killClient(String host, int port) {
Assert.hasText(host, "Host for 'CLIENT KILL' must not be 'null' or 'empty'.");
String client = String.format("%s:%s", host, port);
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().clientKill(client)));
return;
}
getConnection().clientKill(client);
} catch (Exception e) {
throw convertLettuceAccessException(e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(byte[])
*/
@Override
public void setClientName(byte[] name) {
if (isQueueing()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().clientSetname(name)));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxStatusResult(getConnection().clientSetname(name)));
return;
}
getAsyncConnection().clientSetname(name);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
*/
@Override
public String getClientName() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().clientGetname(), LettuceConverters.bytesToString()));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().clientGetname(), LettuceConverters.bytesToString()));
return null;
}
return LettuceConverters.toString(getConnection().clientGetname());
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientList()
*/
@Override
public List<RedisClientInfo> getClientList() {
if (isPipelined()) {
throw new UnsupportedOperationException("Cannot be called in pipeline mode.");
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getAsyncConnection().clientList(),
LettuceConverters.stringToRedisClientListConverter()));
return null;
}
return LettuceConverters.toListOfRedisClientInformation(getConnection().clientList());
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
*/
@Override
public void slaveOf(String host, int port) {
Assert.hasText(host, "Host must not be null for 'SLAVEOF' command.");
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().slaveof(host, port)));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().slaveof(host, port)));
return;
}
getConnection().slaveof(host, port);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
*/
@Override
public void slaveOfNoOne() {
try {
if (isPipelined()) {
pipeline(connection.newLettuceStatusResult(getAsyncConnection().slaveofNoOne()));
return;
}
if (isQueueing()) {
transaction(connection.newLettuceTxResult(getConnection().slaveofNoOne()));
return;
}
getConnection().slaveofNoOne();
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#migrate(byte[], org.springframework.data.redis.connection.RedisNode, int, org.springframework.data.redis.connection.RedisServerCommands.MigrateOption)
*/
@Override
public void migrate(byte[] key, RedisNode target, int dbIndex, MigrateOption option) {
migrate(key, target, dbIndex, option, Long.MAX_VALUE);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#migrate(byte[], org.springframework.data.redis.connection.RedisNode, int, org.springframework.data.redis.connection.RedisServerCommands.MigrateOption, long)
*/
@Override
public void migrate(byte[] key, RedisNode target, int dbIndex, MigrateOption option, long timeout) {
try {
if (isPipelined()) {
pipeline(connection
.newLettuceResult(getAsyncConnection().migrate(target.getHost(), target.getPort(), key, dbIndex, timeout)));
return;
}
if (isQueueing()) {
transaction(connection
.newLettuceTxResult(getConnection().migrate(target.getHost(), target.getPort(), key, dbIndex, timeout)));
return;
}
getConnection().migrate(target.getHost(), target.getPort(), key, dbIndex, timeout);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}
private boolean isQueueing() {
return connection.isQueueing();
}
private void pipeline(LettuceResult result) {
connection.pipeline(result);
}
private void transaction(LettuceTxResult result) {
connection.transaction(result);
}
private RedisClusterAsyncCommands<byte[], byte[]> getAsyncConnection() {
return connection.getAsyncConnection();
}
public RedisClusterCommands<byte[], byte[]> getConnection() {
return connection.getConnection();
}
private DataAccessException convertLettuceAccessException(Exception ex) {
return connection.convertLettuceAccessException(ex);
}
}