DATAREDIS-567 - Remove Support for SRP and JRedis.
This commit is contained in:
committed by
Mark Paluch
parent
186b6dd457
commit
f6411b3811
@@ -16,9 +16,7 @@
|
||||
package org.springframework.data.redis.connection;
|
||||
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.connection.srp.SrpConnectionFactory;
|
||||
|
||||
/**
|
||||
* Utilities for examining a {@link RedisConnection}
|
||||
@@ -29,16 +27,7 @@ import org.springframework.data.redis.connection.srp.SrpConnectionFactory;
|
||||
public abstract class ConnectionUtils {
|
||||
|
||||
public static boolean isAsync(RedisConnectionFactory connectionFactory) {
|
||||
return (connectionFactory instanceof LettuceConnectionFactory)
|
||||
|| (connectionFactory instanceof SrpConnectionFactory);
|
||||
}
|
||||
|
||||
public static boolean isSrp(RedisConnectionFactory connectionFactory) {
|
||||
return connectionFactory instanceof SrpConnectionFactory;
|
||||
}
|
||||
|
||||
public static boolean isJredis(RedisConnectionFactory connectionFactory) {
|
||||
return connectionFactory instanceof JredisConnectionFactory;
|
||||
return (connectionFactory instanceof LettuceConnectionFactory);
|
||||
}
|
||||
|
||||
public static boolean isLettuce(RedisConnectionFactory connectionFactory) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.jredis;
|
||||
|
||||
import org.jredis.ClientRuntimeException;
|
||||
import org.jredis.JRedis;
|
||||
import org.jredis.connector.Connection;
|
||||
import org.jredis.connector.Connection.Socket.Property;
|
||||
import org.jredis.connector.ConnectionSpec;
|
||||
import org.jredis.ri.alphazero.JRedisClient;
|
||||
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.Pool;
|
||||
import org.springframework.data.redis.connection.RedisClusterConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConnection;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Connection factory using creating <a href="http://github.com/alphazero/jredis">JRedis</a> based connections.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
public class JredisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
||||
|
||||
private ConnectionSpec connectionSpec;
|
||||
|
||||
private String hostName = "localhost";
|
||||
private int port = DEFAULT_REDIS_PORT;
|
||||
private String password = null;
|
||||
private int timeout;
|
||||
private int dbIndex = DEFAULT_REDIS_DB;
|
||||
private Pool<JRedis> pool;
|
||||
|
||||
private static final int DEFAULT_REDIS_PORT = 6379;
|
||||
private static final int DEFAULT_REDIS_DB = 0;
|
||||
private static final byte[] DEFAULT_REDIS_PASSWORD = null;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>JredisConnectionFactory</code> instance.
|
||||
*/
|
||||
public JredisConnectionFactory() {}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>JredisConnectionFactory</code> instance. Will override the other connection parameters
|
||||
* passed to the factory.
|
||||
*
|
||||
* @param connectionSpec already configured connection.
|
||||
*/
|
||||
public JredisConnectionFactory(ConnectionSpec connectionSpec) {
|
||||
this.connectionSpec = connectionSpec;
|
||||
}
|
||||
|
||||
public JredisConnectionFactory(Pool<JRedis> pool) {
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
if (connectionSpec == null && pool == null) {
|
||||
Assert.hasText(hostName);
|
||||
connectionSpec = DefaultConnectionSpec.newSpec(hostName, port, dbIndex, DEFAULT_REDIS_PASSWORD);
|
||||
connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false);
|
||||
|
||||
if (StringUtils.hasLength(password)) {
|
||||
connectionSpec.setCredentials(password);
|
||||
}
|
||||
|
||||
if (timeout > 0) {
|
||||
connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() throws Exception {
|
||||
if (pool != null) {
|
||||
pool.destroy();
|
||||
pool = null;
|
||||
}
|
||||
}
|
||||
|
||||
public RedisConnection getConnection() {
|
||||
JredisConnection connection;
|
||||
if (pool != null) {
|
||||
connection = new JredisConnection(pool.getResource(), pool);
|
||||
} else {
|
||||
connection = new JredisConnection(new JRedisClient(connectionSpec), null);
|
||||
}
|
||||
return postProcessConnection(connection);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisConnectionFactory#getClusterConnection()
|
||||
*/
|
||||
@Override
|
||||
public RedisClusterConnection getClusterConnection() {
|
||||
throw new UnsupportedOperationException("Jredis does not support Redis Cluster.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Post process a newly retrieved connection. Useful for decorating or executing initialization commands on a new
|
||||
* connection. This implementation simply returns the connection.
|
||||
*
|
||||
* @param connection
|
||||
* @return processed connection
|
||||
*/
|
||||
protected RedisConnection postProcessConnection(JredisConnection connection) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
|
||||
if (ex instanceof ClientRuntimeException) {
|
||||
return JredisUtils.convertJredisAccessException((ClientRuntimeException) ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Redis host name of this factory.
|
||||
*
|
||||
* @return Returns the hostName
|
||||
*/
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Redis host name for this factory.
|
||||
*
|
||||
* @param hostName The hostName to set.
|
||||
*/
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Redis port.
|
||||
*
|
||||
* @return Returns the port
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Redis port.
|
||||
*
|
||||
* @param port The port to set.
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password used for authenticating with the Redis server.
|
||||
*
|
||||
* @return password for authentication
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the password used for authenticating with the Redis server.
|
||||
*
|
||||
* @param password the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the database.
|
||||
*
|
||||
* @return Returns the database index
|
||||
*/
|
||||
public int getDatabase() {
|
||||
return dbIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index of the database used by this connection factory. Can be between 0 (default) and 15.
|
||||
*
|
||||
* @param index database index
|
||||
*/
|
||||
public void setDatabase(int index) {
|
||||
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
|
||||
this.dbIndex = index;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link JredisConnection} does not support pipeline or transactions
|
||||
*/
|
||||
public boolean getConvertPipelineAndTxResults() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSentinelConnection getSentinelConnection() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.jredis;
|
||||
|
||||
import org.apache.commons.pool2.BasePooledObjectFactory;
|
||||
import org.apache.commons.pool2.PooledObject;
|
||||
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.jredis.JRedis;
|
||||
import org.jredis.connector.Connection;
|
||||
import org.jredis.connector.Connection.Socket.Property;
|
||||
import org.jredis.connector.ConnectionSpec;
|
||||
import org.jredis.ri.alphazero.JRedisClient;
|
||||
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
|
||||
import org.springframework.data.redis.connection.Pool;
|
||||
import org.springframework.data.redis.connection.PoolException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* JRedis implementation of {@link Pool}
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
public class JredisPool implements Pool<JRedis> {
|
||||
|
||||
private final GenericObjectPool<JRedis> internalPool;
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} and {@link ConnectionSpec} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName The Redis host
|
||||
* @param port The Redis port
|
||||
*/
|
||||
public JredisPool(String hostName, int port) {
|
||||
this(hostName, port, 0, null, 0, new GenericObjectPoolConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link ConnectionSpec} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName The Redis host
|
||||
* @param port The Redis port
|
||||
* @param poolConfig The pool {@link Config}
|
||||
*/
|
||||
public JredisPool(String hostName, int port, GenericObjectPoolConfig poolConfig) {
|
||||
this(hostName, port, 0, null, 0, poolConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
|
||||
*/
|
||||
public JredisPool(ConnectionSpec connectionSpec) {
|
||||
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), new GenericObjectPoolConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
|
||||
* @param poolConfig The pool {@link Config}
|
||||
*/
|
||||
public JredisPool(ConnectionSpec connectionSpec, GenericObjectPoolConfig poolConfig) {
|
||||
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName The Redis host
|
||||
* @param port The Redis port
|
||||
* @param dbIndex The index of the database all connections should use. The database will only be selected on initial
|
||||
* creation of the pooled {@link JRedis} instances. Since calling select directly on {@link JRedis} is not
|
||||
* supported, it is assumed that connections can be re-used without subsequent selects.
|
||||
* @param password The password used for authenticating with the Redis server or null if no password required
|
||||
* @param timeout The socket timeout or 0 to use the default socket timeout
|
||||
*/
|
||||
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout) {
|
||||
this(hostName, port, dbIndex, password, timeout, new GenericObjectPoolConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param hostName The Redis host
|
||||
* @param port The Redis port
|
||||
* @param dbIndex The index of the database all connections should use
|
||||
* @param password The password used for authenticating with the Redis server or null if no password required
|
||||
* @param timeout The socket timeout or 0 to use the default socket timeout
|
||||
* @param poolConfig The pool {@link Config}
|
||||
*/
|
||||
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout,
|
||||
GenericObjectPoolConfig poolConfig) {
|
||||
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec(hostName, port, dbIndex, null);
|
||||
connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false);
|
||||
if (StringUtils.hasLength(password)) {
|
||||
connectionSpec.setCredentials(password);
|
||||
}
|
||||
if (timeout > 0) {
|
||||
connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout);
|
||||
}
|
||||
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
|
||||
}
|
||||
|
||||
public JRedis getResource() {
|
||||
try {
|
||||
return internalPool.borrowObject();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not get a resource from the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnBrokenResource(final JRedis resource) {
|
||||
try {
|
||||
internalPool.invalidateObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not invalidate the broken resource", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnResource(final JRedis resource) {
|
||||
try {
|
||||
internalPool.returnObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not return the resource to the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
internalPool.close();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not destroy the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class JredisFactory extends BasePooledObjectFactory<JRedis> {
|
||||
|
||||
private final ConnectionSpec connectionSpec;
|
||||
|
||||
public JredisFactory(ConnectionSpec connectionSpec) {
|
||||
super();
|
||||
this.connectionSpec = connectionSpec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyObject(final PooledObject<JRedis> obj) throws Exception {
|
||||
try {
|
||||
obj.getObject().quit();
|
||||
} catch (Exception e) {
|
||||
// Errors may happen if returning a broken resource
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateObject(final PooledObject<JRedis> obj) {
|
||||
try {
|
||||
obj.getObject().ping();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JRedis create() throws Exception {
|
||||
return new JRedisClient(connectionSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PooledObject<JRedis> wrap(JRedis obj) {
|
||||
return new DefaultPooledObject<JRedis>(obj);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.jredis;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.jredis.ClientRuntimeException;
|
||||
import org.jredis.RedisException;
|
||||
import org.jredis.RedisType;
|
||||
import org.jredis.Sort;
|
||||
import org.jredis.connector.NotConnectedException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.SortParameters;
|
||||
import org.springframework.data.redis.connection.SortParameters.Order;
|
||||
import org.springframework.data.redis.connection.SortParameters.Range;
|
||||
|
||||
/**
|
||||
* Helper class featuring methods for JRedis connection handling, providing support for exception translation.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class JredisUtils {
|
||||
|
||||
/**
|
||||
* Converts the given, native JRedis exception to Spring's DAO hierarchy.
|
||||
*
|
||||
* @param ex JRedis exception
|
||||
* @return converted exception
|
||||
*/
|
||||
public static DataAccessException convertJredisAccessException(RedisException ex) {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given, native JRedis exception to Spring's DAO hierarchy.
|
||||
*
|
||||
* @param ex JRedis exception
|
||||
* @return converted exception
|
||||
*/
|
||||
public static DataAccessException convertJredisAccessException(ClientRuntimeException ex) {
|
||||
if (ex instanceof NotConnectedException) {
|
||||
return new RedisConnectionFailureException(ex.getMessage(), ex);
|
||||
}
|
||||
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
static DataType convertDataType(RedisType type) {
|
||||
switch (type) {
|
||||
case NONE:
|
||||
return DataType.NONE;
|
||||
case string:
|
||||
return DataType.STRING;
|
||||
case list:
|
||||
return DataType.LIST;
|
||||
case set:
|
||||
return DataType.SET;
|
||||
// case zset:
|
||||
// return DataType.ZSET;
|
||||
case hash:
|
||||
return DataType.HASH;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static Sort applySortingParams(Sort jredisSort, SortParameters params, byte[] storeKey) {
|
||||
if (params != null) {
|
||||
byte[] byPattern = params.getByPattern();
|
||||
if (byPattern != null) {
|
||||
jredisSort.BY(byPattern);
|
||||
}
|
||||
byte[][] getPattern = params.getGetPattern();
|
||||
|
||||
if (getPattern != null && getPattern.length > 0) {
|
||||
for (byte[] bs : getPattern) {
|
||||
jredisSort.GET(bs);
|
||||
}
|
||||
}
|
||||
Range limit = params.getLimit();
|
||||
if (limit != null) {
|
||||
jredisSort.LIMIT(limit.getStart(), limit.getCount());
|
||||
}
|
||||
Order order = params.getOrder();
|
||||
if (order != null && order.equals(Order.DESC)) {
|
||||
jredisSort.DESC();
|
||||
}
|
||||
Boolean isAlpha = params.isAlphabetic();
|
||||
if (isAlpha != null && isAlpha) {
|
||||
jredisSort.ALPHA();
|
||||
}
|
||||
}
|
||||
|
||||
if (storeKey != null) {
|
||||
jredisSort.STORE(storeKey);
|
||||
}
|
||||
|
||||
return jredisSort;
|
||||
}
|
||||
|
||||
static Properties info(Map<String, String> map) {
|
||||
Properties info = new Properties();
|
||||
info.putAll(map);
|
||||
return info;
|
||||
}
|
||||
|
||||
static Long toLong(Boolean source) {
|
||||
return source ? 1l : 0l;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Connection package for <a href="http://github.com/alphazero/jredis">JRedis</a> library.
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
package org.springframework.data.redis.connection.jredis;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,180 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.srp;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.ExceptionTranslationStrategy;
|
||||
import org.springframework.data.redis.PassThroughExceptionTranslationStrategy;
|
||||
import org.springframework.data.redis.connection.RedisClusterConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConnection;
|
||||
|
||||
/**
|
||||
* Connection factory creating <a href="http://github.com/spullara/redis-protocol">Redis Protocol</a> based connections.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Thomas Darimont
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
public class SrpConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
||||
|
||||
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy(
|
||||
SrpConverters.exceptionConverter());
|
||||
|
||||
private String hostName = "localhost";
|
||||
private int port = 6379;
|
||||
private BlockingQueue<SrpConnection> trackedConnections = new ArrayBlockingQueue<SrpConnection>(50);
|
||||
private boolean convertPipelineAndTxResults = true;
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>SRedisConnectionFactory</code> instance with default settings.
|
||||
*/
|
||||
public SrpConnectionFactory() {}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>SRedisConnectionFactory</code> instance with default settings.
|
||||
*/
|
||||
public SrpConnectionFactory(String host, int port) {
|
||||
this.hostName = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {}
|
||||
|
||||
public void destroy() {
|
||||
SrpConnection con;
|
||||
do {
|
||||
con = trackedConnections.poll();
|
||||
if (con != null && !con.isClosed()) {
|
||||
try {
|
||||
con.close();
|
||||
} catch (Exception ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
} while (con != null);
|
||||
}
|
||||
|
||||
public RedisConnection getConnection() {
|
||||
SrpConnection connection = password != null ? new SrpConnection(hostName, port, password, trackedConnections)
|
||||
: new SrpConnection(hostName, port, trackedConnections);
|
||||
connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
|
||||
return connection;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisConnectionFactory#getClusterConnection()
|
||||
*/
|
||||
@Override
|
||||
public RedisClusterConnection getClusterConnection() {
|
||||
throw new UnsupportedOperationException("Srp does not support Redis Cluster.");
|
||||
}
|
||||
|
||||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
|
||||
return EXCEPTION_TRANSLATION.translate(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current host.
|
||||
*
|
||||
* @return the host
|
||||
*/
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the host.
|
||||
*
|
||||
* @param host the host to set
|
||||
*/
|
||||
public void setHostName(String host) {
|
||||
this.hostName = host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current port.
|
||||
*
|
||||
* @return the port
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the port.
|
||||
*
|
||||
* @param port the port to set
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password used for authenticating with the Redis server.
|
||||
*
|
||||
* @return password for authentication
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the password used for authenticating with the Redis server.
|
||||
*
|
||||
* @param password the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies if pipelined results should be converted to the expected data type. If false, results of
|
||||
* {@link SrpConnection#closePipeline()} and {@link SrpConnection#exec()} will be of the type returned by the SRP
|
||||
* driver
|
||||
*
|
||||
* @return Whether or not to convert pipeline and tx results
|
||||
*/
|
||||
public boolean getConvertPipelineAndTxResults() {
|
||||
return convertPipelineAndTxResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies if pipelined results should be converted to the expected data type. If false, results of
|
||||
* {@link SrpConnection#closePipeline()} and {@link SrpConnection#exec()} will be of the type returned by the SRP
|
||||
* driver
|
||||
*
|
||||
* @param convertPipelineAndTxResults Whether or not to convert pipeline and tx results
|
||||
*/
|
||||
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) {
|
||||
this.convertPipelineAndTxResults = convertPipelineAndTxResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSentinelConnection getSentinelConnection() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,458 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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.srp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.RedisSystemException;
|
||||
import org.springframework.data.redis.connection.DefaultTuple;
|
||||
import org.springframework.data.redis.connection.RedisListCommands.Position;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands.BitOperation;
|
||||
import org.springframework.data.redis.connection.RedisZSetCommands.Range.Boundary;
|
||||
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
|
||||
import org.springframework.data.redis.connection.convert.Converters;
|
||||
import org.springframework.data.redis.connection.convert.LongToBooleanConverter;
|
||||
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
|
||||
import org.springframework.data.redis.core.types.RedisClientInfo;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import redis.client.RedisException;
|
||||
import redis.reply.IntegerReply;
|
||||
import redis.reply.Reply;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
/**
|
||||
* SRP type converters
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("rawtypes")
|
||||
abstract public class SrpConverters extends Converters {
|
||||
|
||||
private static final byte[] BEFORE = "BEFORE".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] AFTER = "AFTER".getBytes(Charsets.UTF_8);
|
||||
private static final Converter<Reply[], List<byte[]>> REPLIES_TO_BYTES_LIST;
|
||||
private static final Converter<Reply[], Set<byte[]>> REPLIES_TO_BYTES_SET;
|
||||
private static final Converter<Reply[], Set<Tuple>> REPLIES_TO_TUPLE_SET;
|
||||
private static final Converter<Reply[], Map<byte[], byte[]>> REPLIES_TO_BYTES_MAP;
|
||||
private static final Converter<Reply[], List<Boolean>> REPLIES_TO_BOOLEAN_LIST;
|
||||
private static final Converter<Reply[], List<String>> REPLIES_TO_STRING_LIST;
|
||||
private static final Converter<Reply, String> REPLY_TO_STRING;
|
||||
private static final Converter<byte[], Properties> BYTES_TO_PROPERTIES;
|
||||
private static final Converter<byte[], String> BYTES_TO_STRING;
|
||||
private static final Converter<byte[], Double> BYTES_TO_DOUBLE;
|
||||
private static final Converter<Reply[], Long> REPLIES_TO_TIME_AS_LONG;
|
||||
private static final Converter<Reply, List<RedisClientInfo>> REPLY_T0_LIST_OF_CLIENT_INFO;
|
||||
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
|
||||
private static final Converter<byte[], List<RedisClientInfo>> BYTEARRAY_T0_LIST_OF_CLIENT_INFO;
|
||||
private static final Converter<IntegerReply, Boolean> INTEGER_REPLY_TO_BOOLEAN;
|
||||
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
|
||||
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER;
|
||||
|
||||
static {
|
||||
|
||||
REPLIES_TO_BYTES_LIST = new Converter<Reply[], List<byte[]>>() {
|
||||
public List<byte[]> convert(Reply[] replies) {
|
||||
if (replies == null) {
|
||||
return null;
|
||||
}
|
||||
List<byte[]> list = new ArrayList<byte[]>(replies.length);
|
||||
for (Reply reply : replies) {
|
||||
Object data = reply.data();
|
||||
if (data == null) {
|
||||
list.add(null);
|
||||
} else if (data instanceof byte[])
|
||||
list.add((byte[]) data);
|
||||
else
|
||||
throw new IllegalArgumentException("array contains more then just nulls and bytes -> " + data);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
};
|
||||
|
||||
REPLIES_TO_BYTES_SET = new Converter<Reply[], Set<byte[]>>() {
|
||||
public Set<byte[]> convert(Reply[] source) {
|
||||
return source != null ? new LinkedHashSet<byte[]>(SrpConverters.toBytesList(source)) : null;
|
||||
}
|
||||
};
|
||||
|
||||
BYTES_TO_PROPERTIES = new Converter<byte[], Properties>() {
|
||||
public Properties convert(byte[] source) {
|
||||
return source != null ? SrpConverters.toProperties(new String(source, Charsets.UTF_8)) : null;
|
||||
}
|
||||
};
|
||||
|
||||
BYTES_TO_DOUBLE = new Converter<byte[], Double>() {
|
||||
public Double convert(byte[] bytes) {
|
||||
return (bytes == null || bytes.length == 0 ? null : Double.valueOf(new String(bytes, Charsets.UTF_8)));
|
||||
}
|
||||
};
|
||||
|
||||
REPLIES_TO_TIME_AS_LONG = new Converter<Reply[], Long>() {
|
||||
|
||||
@Override
|
||||
public Long convert(Reply[] reply) {
|
||||
|
||||
Assert.notEmpty(reply, "Received invalid result from server. Expected 2 items in collection.");
|
||||
Assert.isTrue(reply.length == 2, "Received invalid nr of arguments from redis server. Expected 2 received "
|
||||
+ reply.length);
|
||||
|
||||
List<String> serverTimeInformation = REPLIES_TO_STRING_LIST.convert(reply);
|
||||
|
||||
return Converters.toTimeMillis(serverTimeInformation.get(0), serverTimeInformation.get(1));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
REPLIES_TO_TUPLE_SET = new Converter<Reply[], Set<Tuple>>() {
|
||||
public Set<Tuple> convert(Reply[] byteArrays) {
|
||||
if (byteArrays == null) {
|
||||
return null;
|
||||
}
|
||||
Set<Tuple> tuples = new LinkedHashSet<Tuple>(byteArrays.length / 2 + 1);
|
||||
for (int i = 0; i < byteArrays.length; i++) {
|
||||
byte[] value = (byte[]) byteArrays[i].data();
|
||||
i++;
|
||||
Double score = SrpConverters.toDouble((byte[]) byteArrays[i].data());
|
||||
tuples.add(new DefaultTuple(value, score));
|
||||
}
|
||||
return tuples;
|
||||
}
|
||||
};
|
||||
|
||||
REPLIES_TO_BYTES_MAP = new Converter<Reply[], Map<byte[], byte[]>>() {
|
||||
public Map<byte[], byte[]> convert(Reply[] byteArrays) {
|
||||
if (byteArrays == null) {
|
||||
return null;
|
||||
}
|
||||
Map<byte[], byte[]> map = new LinkedHashMap<byte[], byte[]>(byteArrays.length / 2);
|
||||
for (int i = 0; i < byteArrays.length; i++) {
|
||||
map.put((byte[]) byteArrays[i++].data(), (byte[]) byteArrays[i].data());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
};
|
||||
|
||||
BYTES_TO_STRING = new Converter<byte[], String>() {
|
||||
public String convert(byte[] data) {
|
||||
return data != null ? new String((byte[]) data, Charsets.UTF_8) : null;
|
||||
}
|
||||
};
|
||||
|
||||
REPLIES_TO_BOOLEAN_LIST = new Converter<Reply[], List<Boolean>>() {
|
||||
public List<Boolean> convert(Reply[] source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
List<Boolean> results = new ArrayList<Boolean>();
|
||||
for (Reply r : source) {
|
||||
results.add(SrpConverters.toBoolean(((IntegerReply) r).data()));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
REPLIES_TO_STRING_LIST = new Converter<Reply[], List<String>>() {
|
||||
public List<String> convert(Reply[] source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> results = new ArrayList<String>();
|
||||
for (Reply r : source) {
|
||||
results.add(SrpConverters.toString((byte[]) r.data()));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
REPLY_TO_STRING = new Converter<Reply, String>() {
|
||||
|
||||
@Override
|
||||
public String convert(Reply source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return SrpConverters.toString((byte[]) source.data());
|
||||
}
|
||||
};
|
||||
|
||||
REPLY_T0_LIST_OF_CLIENT_INFO = new Converter<Reply, List<RedisClientInfo>>() {
|
||||
|
||||
@Override
|
||||
public List<RedisClientInfo> convert(Reply source) {
|
||||
|
||||
if (source == null || source.data() == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Assert.isInstanceOf(byte[].class, source.data(), "Expected data to be an instace of byte [].");
|
||||
return BYTEARRAY_T0_LIST_OF_CLIENT_INFO.convert((byte[]) source.data());
|
||||
}
|
||||
};
|
||||
|
||||
BYTEARRAY_T0_LIST_OF_CLIENT_INFO = new Converter<byte[], List<RedisClientInfo>>() {
|
||||
|
||||
@Override
|
||||
public List<RedisClientInfo> convert(byte[] source) {
|
||||
if (source == null || source.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
String s = SrpConverters.toString(source);
|
||||
return STRING_TO_LIST_OF_CLIENT_INFO.convert(s.split("\\r?\\n"));
|
||||
}
|
||||
};
|
||||
|
||||
INTEGER_REPLY_TO_BOOLEAN = new Converter<IntegerReply, Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean convert(IntegerReply source) {
|
||||
if (source == null || source.data() == null) {
|
||||
return false;
|
||||
}
|
||||
return source.data() == 1;
|
||||
}
|
||||
};
|
||||
|
||||
EXCEPTION_CONVERTER = new Converter<Exception, DataAccessException>() {
|
||||
|
||||
@Override
|
||||
public DataAccessException convert(Exception ex) {
|
||||
|
||||
if (ex instanceof RedisException) {
|
||||
return new RedisSystemException("redis exception", ex);
|
||||
}
|
||||
|
||||
if (ex instanceof IOException) {
|
||||
return new RedisConnectionFailureException("Redis connection failed", (IOException) ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
|
||||
return REPLIES_TO_BYTES_LIST;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], Set<byte[]>> repliesToBytesSet() {
|
||||
return REPLIES_TO_BYTES_SET;
|
||||
}
|
||||
|
||||
public static Converter<byte[], Properties> bytesToProperties() {
|
||||
return BYTES_TO_PROPERTIES;
|
||||
}
|
||||
|
||||
public static Converter<byte[], Double> bytesToDouble() {
|
||||
return BYTES_TO_DOUBLE;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], Set<Tuple>> repliesToTupleSet() {
|
||||
return REPLIES_TO_TUPLE_SET;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], Map<byte[], byte[]>> repliesToBytesMap() {
|
||||
return REPLIES_TO_BYTES_MAP;
|
||||
}
|
||||
|
||||
public static Converter<byte[], String> bytesToString() {
|
||||
return BYTES_TO_STRING;
|
||||
}
|
||||
|
||||
public static Converter<Reply, String> replyToString() {
|
||||
return REPLY_TO_STRING;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], List<Boolean>> repliesToBooleanList() {
|
||||
return REPLIES_TO_BOOLEAN_LIST;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], List<String>> repliesToStringList() {
|
||||
return REPLIES_TO_STRING_LIST;
|
||||
}
|
||||
|
||||
public static Converter<Reply[], Long> repliesToTimeAsLong() {
|
||||
return REPLIES_TO_TIME_AS_LONG;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @since 1.3
|
||||
*/
|
||||
public static List<RedisClientInfo> toListOfRedisClientInformation(Reply reply) {
|
||||
return REPLY_T0_LIST_OF_CLIENT_INFO.convert(reply);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @since 1.3
|
||||
*/
|
||||
public static Converter<Long, Boolean> longToBooleanConverter() {
|
||||
return LONG_TO_BOOLEAN;
|
||||
}
|
||||
|
||||
public static List<byte[]> toBytesList(Reply[] source) {
|
||||
return REPLIES_TO_BYTES_LIST.convert(source);
|
||||
}
|
||||
|
||||
public static Set<byte[]> toBytesSet(Reply[] source) {
|
||||
return REPLIES_TO_BYTES_SET.convert(source);
|
||||
}
|
||||
|
||||
public static Properties toProperties(byte[] source) {
|
||||
return BYTES_TO_PROPERTIES.convert(source);
|
||||
}
|
||||
|
||||
public static Double toDouble(byte[] source) {
|
||||
return BYTES_TO_DOUBLE.convert(source);
|
||||
}
|
||||
|
||||
public static Set<Tuple> toTupleSet(Reply[] source) {
|
||||
return REPLIES_TO_TUPLE_SET.convert(source);
|
||||
}
|
||||
|
||||
public static Map<byte[], byte[]> toBytesMap(Reply[] source) {
|
||||
return REPLIES_TO_BYTES_MAP.convert(source);
|
||||
}
|
||||
|
||||
public static String toString(Reply source) {
|
||||
return REPLY_TO_STRING.convert(source);
|
||||
}
|
||||
|
||||
public static String toString(byte[] source) {
|
||||
return BYTES_TO_STRING.convert(source);
|
||||
}
|
||||
|
||||
public static List<Boolean> toBooleanList(Reply[] source) {
|
||||
return REPLIES_TO_BOOLEAN_LIST.convert(source);
|
||||
}
|
||||
|
||||
public static List<String> toStringList(Reply[] source) {
|
||||
return REPLIES_TO_STRING_LIST.convert(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts given {@link Reply}s to {@link Long}.
|
||||
*
|
||||
* @param source Array holding time values in seconds and microseconds.
|
||||
* @return
|
||||
*/
|
||||
public static Long toTimeAsLong(Reply[] source) {
|
||||
return REPLIES_TO_TIME_AS_LONG.convert(source);
|
||||
}
|
||||
|
||||
public static byte[] toBytes(BitOperation op) {
|
||||
Assert.notNull(op, "The bit operation is required");
|
||||
return op.name().toUpperCase().getBytes(Charsets.UTF_8);
|
||||
}
|
||||
|
||||
public static byte[][] toByteArrays(Map<byte[], byte[]> source) {
|
||||
byte[][] result = new byte[source.size() * 2][];
|
||||
int index = 0;
|
||||
for (Map.Entry<byte[], byte[]> entry : source.entrySet()) {
|
||||
result[index++] = entry.getKey();
|
||||
result[index++] = entry.getValue();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] toBytes(Position source) {
|
||||
Assert.notNull("list positions are mandatory");
|
||||
return (Position.AFTER.equals(source) ? AFTER : BEFORE);
|
||||
}
|
||||
|
||||
public static List<String> toStringList(String source) {
|
||||
return Collections.singletonList(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.3
|
||||
* @return
|
||||
*/
|
||||
public static Converter<byte[], List<RedisClientInfo>> replyToListOfRedisClientInfo() {
|
||||
return BYTEARRAY_T0_LIST_OF_CLIENT_INFO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an {@link IntegerReply} to a {@link Boolean} by inspecting {@link IntegerReply#data()}.
|
||||
*
|
||||
* @since 1.3
|
||||
* @param reply
|
||||
* @return
|
||||
*/
|
||||
public static Boolean toBoolean(IntegerReply reply) {
|
||||
return INTEGER_REPLY_TO_BOOLEAN.convert(reply);
|
||||
}
|
||||
|
||||
public static Converter<Exception, DataAccessException> exceptionConverter() {
|
||||
return EXCEPTION_CONVERTER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given {@link Boundary} to its binary representation suitable for {@literal ZRANGEBY*} commands, despite
|
||||
* {@literal ZRANGEBYLEX}.
|
||||
*
|
||||
* @param boundary
|
||||
* @param defaultValue
|
||||
* @return
|
||||
* @since 1.6
|
||||
*/
|
||||
public static byte[] boundaryToBytesForZRange(Boundary boundary, byte[] defaultValue) {
|
||||
|
||||
if (boundary == null || boundary.getValue() == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return boundaryToBytes(boundary, new byte[] {}, "(".getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
public static byte[] toBytes(String source) {
|
||||
return source.getBytes(Charsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] boundaryToBytes(Boundary boundary, byte[] inclPrefix, byte[] exclPrefix) {
|
||||
|
||||
byte[] prefix = boundary.isIncluding() ? inclPrefix : exclPrefix;
|
||||
byte[] value = null;
|
||||
if (boundary.getValue() instanceof byte[]) {
|
||||
value = (byte[]) boundary.getValue();
|
||||
} else {
|
||||
value = toBytes(boundary.getValue().toString());
|
||||
}
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(prefix.length + value.length);
|
||||
buffer.put(prefix);
|
||||
buffer.put(value);
|
||||
return buffer.array();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.srp;
|
||||
|
||||
import org.springframework.data.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import redis.client.ReplyListener;
|
||||
|
||||
/**
|
||||
* MessageListener wrapper around SRP {@link ReplyListener}.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
class SrpMessageListener implements ReplyListener {
|
||||
|
||||
private final MessageListener listener;
|
||||
|
||||
SrpMessageListener(MessageListener listener) {
|
||||
Assert.notNull(listener, "message listener is required");
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void message(byte[] channel, byte[] message) {
|
||||
listener.onMessage(new DefaultMessage(channel, message), null);
|
||||
}
|
||||
|
||||
public void pmessage(byte[] pattern, byte[] channel, byte[] message) {
|
||||
listener.onMessage(new DefaultMessage(channel, message), pattern);
|
||||
}
|
||||
|
||||
public void psubscribed(byte[] arg0, int arg1) {}
|
||||
|
||||
public void punsubscribed(byte[] arg0, int arg1) {}
|
||||
|
||||
public void subscribed(byte[] arg0, int arg1) {}
|
||||
|
||||
public void unsubscribed(byte[] arg0, int arg1) {}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2016 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.srp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.redis.connection.ReturnType;
|
||||
|
||||
import redis.reply.Reply;
|
||||
|
||||
/**
|
||||
* Converts the value returned by SRP script eval to the expected {@link ReturnType}
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
public class SrpScriptReturnConverter implements Converter<Object, Object> {
|
||||
|
||||
private ReturnType returnType;
|
||||
|
||||
public SrpScriptReturnConverter(ReturnType returnType) {
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
public Object convert(Object source) {
|
||||
if (returnType == ReturnType.MULTI) {
|
||||
Reply<?>[] replies = (Reply[]) source;
|
||||
List<Object> results = new ArrayList<Object>();
|
||||
for (Reply<?> reply : replies) {
|
||||
results.add(reply.data());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
if (returnType == ReturnType.BOOLEAN) {
|
||||
// Lua false comes back as a null bulk reply
|
||||
if (source == null) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return ((Long) source == 1);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.srp;
|
||||
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.connection.util.AbstractSubscription;
|
||||
|
||||
import redis.client.RedisClient;
|
||||
import redis.client.ReplyListener;
|
||||
|
||||
/**
|
||||
* Message subscription on top of SRP.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
class SrpSubscription extends AbstractSubscription {
|
||||
|
||||
private final RedisClient client;
|
||||
private final ReplyListener listener;
|
||||
|
||||
SrpSubscription(MessageListener listener, RedisClient client) {
|
||||
super(listener);
|
||||
this.client = client;
|
||||
this.listener = new SrpMessageListener(listener);
|
||||
client.addListener(this.listener);
|
||||
}
|
||||
|
||||
protected void doClose() {
|
||||
if (!getChannels().isEmpty()) {
|
||||
client.unsubscribe((Object[]) null);
|
||||
}
|
||||
if (!getPatterns().isEmpty()) {
|
||||
client.punsubscribe((Object[]) null);
|
||||
}
|
||||
client.removeListener(this.listener);
|
||||
}
|
||||
|
||||
protected void doPsubscribe(byte[]... patterns) {
|
||||
client.psubscribe((Object[]) patterns);
|
||||
}
|
||||
|
||||
protected void doPUnsubscribe(boolean all, byte[]... patterns) {
|
||||
if (all) {
|
||||
client.punsubscribe((Object[]) null);
|
||||
} else {
|
||||
client.punsubscribe((Object[]) patterns);
|
||||
}
|
||||
}
|
||||
|
||||
protected void doSubscribe(byte[]... channels) {
|
||||
client.subscribe((Object[]) channels);
|
||||
}
|
||||
|
||||
protected void doUnsubscribe(boolean all, byte[]... channels) {
|
||||
if (all) {
|
||||
client.unsubscribe((Object[]) null);
|
||||
} else {
|
||||
client.unsubscribe((Object[]) channels);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,334 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2016 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.srp;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.RedisSystemException;
|
||||
import org.springframework.data.redis.connection.DefaultTuple;
|
||||
import org.springframework.data.redis.connection.RedisListCommands.Position;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands.BitOperation;
|
||||
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
|
||||
import org.springframework.data.redis.connection.ReturnType;
|
||||
import org.springframework.data.redis.connection.SortParameters;
|
||||
import org.springframework.util.Assert;
|
||||
import redis.client.RedisException;
|
||||
import redis.reply.BulkReply;
|
||||
import redis.reply.IntegerReply;
|
||||
import redis.reply.MultiBulkReply;
|
||||
import redis.reply.Reply;
|
||||
import redis.reply.StatusReply;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Helper class featuring methods for SRedis connection handling, providing support for exception translation.
|
||||
* Deprecated. Use {@link SrpConverters} instead.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
@Deprecated
|
||||
abstract class SrpUtils {
|
||||
|
||||
private static final byte[] ONE = new byte[] { '1' };
|
||||
private static final byte[] ZERO = new byte[] { '0' };
|
||||
private static final byte[] BEFORE = "BEFORE".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] AFTER = "AFTER".getBytes(Charsets.UTF_8);
|
||||
static final byte[] WITHSCORES = "WITHSCORES".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] SPACE = " ".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] BY = "BY".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] GET = "GET".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] ALPHA = "ALPHA".getBytes(Charsets.UTF_8);
|
||||
private static final byte[] STORE = "STORE".getBytes(Charsets.UTF_8);
|
||||
|
||||
static DataAccessException convertSRedisAccessException(RuntimeException ex) {
|
||||
if (ex instanceof RedisException) {
|
||||
return new RedisSystemException("redis exception", ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static Properties info(BulkReply reply) {
|
||||
Properties info = new Properties();
|
||||
// use the same charset as the library
|
||||
StringReader stringReader = new StringReader(new String(reply.data(), Charsets.UTF_8));
|
||||
try {
|
||||
info.load(stringReader);
|
||||
} catch (Exception ex) {
|
||||
throw new RedisSystemException("Cannot read Redis info", ex);
|
||||
} finally {
|
||||
stringReader.close();
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static List<byte[]> toBytesList(Reply[] replies) {
|
||||
if (replies == null) {
|
||||
return null;
|
||||
}
|
||||
List<byte[]> list = new ArrayList<byte[]>(replies.length);
|
||||
for (Reply reply : replies) {
|
||||
Object data = reply.data();
|
||||
if (data == null) {
|
||||
list.add(null);
|
||||
} else if (data instanceof byte[])
|
||||
list.add((byte[]) data);
|
||||
else
|
||||
throw new IllegalArgumentException("array contains more then just nulls and bytes -> " + data);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
static List<String> asStatusList(Reply[] replies) {
|
||||
List<String> statuses = new ArrayList<String>();
|
||||
for (Reply reply : replies) {
|
||||
statuses.add(((StatusReply) reply).data());
|
||||
}
|
||||
return statuses;
|
||||
}
|
||||
|
||||
static <T> List<T> toList(T[] byteArrays) {
|
||||
return Arrays.asList(byteArrays);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static Set<byte[]> toSet(Reply[] byteArrays) {
|
||||
return new LinkedHashSet<byte[]>(toBytesList(byteArrays));
|
||||
}
|
||||
|
||||
static byte[][] convert(Map<byte[], byte[]> hgetAll) {
|
||||
byte[][] result = new byte[hgetAll.size() * 2][];
|
||||
|
||||
int index = 0;
|
||||
for (Map.Entry<byte[], byte[]> entry : hgetAll.entrySet()) {
|
||||
result[index++] = entry.getKey();
|
||||
result[index++] = entry.getValue();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static byte[] asBit(boolean value) {
|
||||
return (value ? ONE : ZERO);
|
||||
}
|
||||
|
||||
static byte[] convertPosition(Position where) {
|
||||
Assert.notNull("list positions are mandatory");
|
||||
return (Position.AFTER.equals(where) ? AFTER : BEFORE);
|
||||
}
|
||||
|
||||
static Double toDouble(byte[] bytes) {
|
||||
return (bytes == null || bytes.length == 0 ? null : Double.valueOf(new String(bytes, Charsets.UTF_8)));
|
||||
}
|
||||
|
||||
static Long toLong(Object[] bytes) {
|
||||
return (bytes == null || bytes.length == 0 ? null : Long.valueOf(new String((byte[]) bytes[0], Charsets.UTF_8)));
|
||||
}
|
||||
|
||||
static Set<Tuple> convertTuple(MultiBulkReply zrange) {
|
||||
return convertTuple(zrange.data());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static Set<Tuple> convertTuple(Reply[] byteArrays) {
|
||||
Set<Tuple> tuples = new LinkedHashSet<Tuple>(byteArrays.length / 2 + 1);
|
||||
|
||||
for (int i = 0; i < byteArrays.length; i++) {
|
||||
byte[] value = (byte[]) byteArrays[i].data();
|
||||
i++;
|
||||
Double score = toDouble((byte[]) byteArrays[i].data());
|
||||
tuples.add(new DefaultTuple(value, score));
|
||||
}
|
||||
|
||||
return tuples;
|
||||
}
|
||||
|
||||
static Object[] convert(int timeout, byte[]... keys) {
|
||||
int length = (keys != null ? keys.length + 1 : 1);
|
||||
|
||||
Object[] args = new Object[length];
|
||||
if (keys != null) {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
args[i] = keys[i];
|
||||
}
|
||||
}
|
||||
args[length - 1] = String.valueOf(timeout).getBytes();
|
||||
return args;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static Map<byte[], byte[]> toMap(Reply[] byteArrays) {
|
||||
Map<byte[], byte[]> map = new LinkedHashMap<byte[], byte[]>(byteArrays.length / 2);
|
||||
for (int i = 0; i < byteArrays.length; i++) {
|
||||
map.put((byte[]) byteArrays[i++].data(), (byte[]) byteArrays[i].data());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
static Boolean asBoolean(IntegerReply reply) {
|
||||
if (reply == null) {
|
||||
return null;
|
||||
}
|
||||
return (Long.valueOf(1).equals(reply.data()));
|
||||
}
|
||||
|
||||
static byte[] limit(long offset, long count) {
|
||||
return ("LIMIT " + offset + " " + count).getBytes(Charsets.UTF_8);
|
||||
}
|
||||
|
||||
static Object[] limitParams(long offset, long count) {
|
||||
return new Object[] { "LIMIT".getBytes(Charsets.UTF_8), String.valueOf(offset).getBytes(Charsets.UTF_8),
|
||||
String.valueOf(count).getBytes(Charsets.UTF_8) };
|
||||
}
|
||||
|
||||
static byte[] sort(SortParameters params) {
|
||||
return sort(params, null);
|
||||
}
|
||||
|
||||
static byte[] sort(SortParameters params, byte[] sortKey) {
|
||||
List<byte[]> arrays = new ArrayList<byte[]>();
|
||||
|
||||
Object[] sortParams = sortParams(params, sortKey);
|
||||
for (Object param : sortParams) {
|
||||
arrays.add((byte[]) param);
|
||||
arrays.add(SPACE);
|
||||
}
|
||||
arrays.remove(arrays.size() - 1);
|
||||
|
||||
// concatenate array
|
||||
int size = 0;
|
||||
|
||||
for (Object bs : arrays) {
|
||||
size += ((byte[]) bs).length;
|
||||
}
|
||||
byte[] result = new byte[size];
|
||||
|
||||
int index = 0;
|
||||
for (byte[] bs : arrays) {
|
||||
System.arraycopy(bs, 0, result, index, bs.length);
|
||||
index += bs.length;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static Object[] sortParams(SortParameters params) {
|
||||
return sortParams(params, null);
|
||||
}
|
||||
|
||||
static Object[] sortParams(SortParameters params, byte[] sortKey) {
|
||||
List<byte[]> arrays = new ArrayList<byte[]>();
|
||||
|
||||
if (params != null) {
|
||||
if (params.getByPattern() != null) {
|
||||
arrays.add(BY);
|
||||
arrays.add(params.getByPattern());
|
||||
}
|
||||
|
||||
if (params.getLimit() != null) {
|
||||
arrays.add(limit(params.getLimit().getStart(), params.getLimit().getCount()));
|
||||
}
|
||||
|
||||
if (params.getGetPattern() != null) {
|
||||
byte[][] pattern = params.getGetPattern();
|
||||
for (byte[] bs : pattern) {
|
||||
arrays.add(GET);
|
||||
arrays.add(bs);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.getOrder() != null) {
|
||||
arrays.add(params.getOrder().name().getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
Boolean isAlpha = params.isAlphabetic();
|
||||
if (isAlpha != null && isAlpha) {
|
||||
arrays.add(ALPHA);
|
||||
}
|
||||
}
|
||||
|
||||
if (sortKey != null) {
|
||||
arrays.add(STORE);
|
||||
arrays.add(sortKey);
|
||||
}
|
||||
|
||||
return arrays.toArray();
|
||||
}
|
||||
|
||||
static byte[] bitOp(BitOperation op) {
|
||||
Assert.notNull(op, "The bit operation is required");
|
||||
return op.name().toUpperCase().getBytes(Charsets.UTF_8);
|
||||
}
|
||||
|
||||
static String asShasum(Reply reply) {
|
||||
Object data = reply.data();
|
||||
return (data instanceof String ? (String) data : new String((byte[]) data, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
static List<Boolean> asBooleanList(Reply reply) {
|
||||
if (!(reply instanceof MultiBulkReply)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
List<Boolean> results = new ArrayList<Boolean>();
|
||||
for (Reply r : ((MultiBulkReply) reply).data()) {
|
||||
results.add(SrpUtils.asBoolean((IntegerReply) r));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
static List<Long> asIntegerList(Reply[] replies) {
|
||||
List<Long> results = new ArrayList<Long>();
|
||||
for (Reply reply : replies) {
|
||||
results.add(((IntegerReply) reply).data());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
static List<Object> asList(MultiBulkReply genericReply) {
|
||||
Reply[] replies = genericReply.data();
|
||||
List<Object> results = new ArrayList<Object>();
|
||||
for (Reply reply : replies) {
|
||||
results.add(reply.data());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
static Object convertScriptReturn(ReturnType returnType, Reply reply) {
|
||||
if (reply instanceof MultiBulkReply) {
|
||||
return SrpUtils.asList((MultiBulkReply) reply);
|
||||
}
|
||||
if (returnType == ReturnType.BOOLEAN) {
|
||||
// Lua false comes back as a null bulk reply
|
||||
if (reply.data() == null) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return ((Long) reply.data() == 1);
|
||||
}
|
||||
return reply.data();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Connection package for <a href="https://github.com/spullara/redis-protocol">spullara Redis Protocol</a> library.
|
||||
* @deprecated since 1.7. Will be removed in subsequent version.
|
||||
*/
|
||||
package org.springframework.data.redis.connection.srp;
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jredis.Redis;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user