first draft for SPRP driver
This commit is contained in:
31
.classpath
31
.classpath
@@ -8,31 +8,36 @@
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.codehaus.jackson/jackson-core-asl/sources/jackson-core-asl-1.8.6-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.codehaus.jackson/jackson-core-asl/jars/jackson-core-asl-1.8.6.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.codehaus.jackson/jackson-mapper-asl/sources/jackson-mapper-asl-1.8.6-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.codehaus.jackson/jackson-mapper-asl/jars/jackson-mapper-asl-1.8.6.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-aop/sources/spring-aop-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-aop/jars/spring-aop-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/log4j/log4j/sources/log4j-1.2.16-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/log4j/log4j/bundles/log4j-1.2.16.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-oxm/sources/spring-oxm-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-oxm/jars/spring-oxm-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-log4j12/sources/slf4j-log4j12-1.6.4-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.6.4.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.idevlab/rjc/sources/rjc-0.6.4-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.idevlab/rjc/bundles/rjc-0.6.4.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/com.github.spullara.redis/client/sources/client-0.1-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/com.github.spullara.redis/client/jars/client-0.1.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.jredis/jredis-anthonylauzon/sources/jredis-anthonylauzon-03122010-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.jredis/jredis-anthonylauzon/jars/jredis-anthonylauzon-03122010.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-core/sources/spring-core-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-core/jars/spring-core-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-context-support/sources/spring-context-support-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-context-support/jars/spring-context-support-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry kind="lib" path="C:/Users/costin/.gradle/cache/com.google.code.findbugs/jsr305/jars/jsr305-1.3.9.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/com.google.guava/guava/sources/guava-11.0-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/com.google.guava/guava/jars/guava-11.0.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/aopalliance/aopalliance/sources/aopalliance-1.0-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/aopalliance/aopalliance/jars/aopalliance-1.0.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-expression/sources/spring-expression-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-expression/jars/spring-expression-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/xpp3/xpp3_min/sources/xpp3_min-1.1.4c-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/xpp3/xpp3_min/jars/xpp3_min-1.1.4c.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-tx/sources/spring-tx-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-tx/jars/spring-tx-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-test/sources/spring-test-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-test/jars/spring-test-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-api/sources/slf4j-api-1.6.4-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.6.4.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-test/sources/spring-test-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-test/jars/spring-test-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/com.github.spullara.redis/protocol/sources/protocol-0.1-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/com.github.spullara.redis/protocol/jars/protocol-0.1.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-oxm/sources/spring-oxm-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-oxm/jars/spring-oxm-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-beans/sources/spring-beans-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-beans/jars/spring-beans-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-core/sources/spring-core-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-core/jars/spring-core-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/jcl-over-slf4j/sources/jcl-over-slf4j-1.6.4-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/jcl-over-slf4j/jars/jcl-over-slf4j-1.6.4.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/com.github.spullara.cli-parser/cli-parser/sources/cli-parser-1.1-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/com.github.spullara.cli-parser/cli-parser/jars/cli-parser-1.1.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/redis.clients/jedis/sources/jedis-2.0.0-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/redis.clients/jedis/jars/jedis-2.0.0.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/jcl-over-slf4j/sources/jcl-over-slf4j-1.6.3-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/jcl-over-slf4j/jars/jcl-over-slf4j-1.6.3.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-log4j12/sources/slf4j-log4j12-1.6.3-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.6.3.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/commons-pool/commons-pool/sources/commons-pool-1.5.6-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/commons-pool/commons-pool/jars/commons-pool-1.5.6.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-aop/sources/spring-aop-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-aop/jars/spring-aop-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-api/sources/slf4j-api-1.6.3-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.6.3.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-context/sources/spring-context-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-context/jars/spring-context-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-beans/sources/spring-beans-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-beans/jars/spring-beans-3.1.0.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-expression/sources/spring-expression-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-expression/jars/spring-expression-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-context/sources/spring-context-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-context/jars/spring-context-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-tx/sources/spring-tx-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-tx/jars/spring-tx-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-asm/sources/spring-asm-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-asm/jars/spring-asm-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/javax.annotation/jsr250-api/sources/jsr250-api-1.0-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/javax.annotation/jsr250-api/jars/jsr250-api-1.0.jar" exported="true"/>
|
||||
<classpathentry kind="lib" path="C:/Users/costin/.gradle/cache/commons-beanutils/commons-beanutils-core/jars/commons-beanutils-core-1.8.3.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/com.thoughtworks.xstream/xstream/sources/xstream-1.3-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/com.thoughtworks.xstream/xstream/jars/xstream-1.3.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-context-support/sources/spring-context-support-3.1.1.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-context-support/jars/spring-context-support-3.1.1.RELEASE.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.mockito/mockito-all/sources/mockito-all-1.8.5-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.mockito/mockito-all/jars/mockito-all-1.8.5.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/junit/junit/sources/junit-4.8.1-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/junit/junit/jars/junit-4.8.1.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/commons-lang/commons-lang/sources/commons-lang-2.5-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/commons-lang/commons-lang/jars/commons-lang-2.5.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/commons-logging/commons-logging/sources/commons-logging-1.1.1-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar" exported="true"/>
|
||||
<classpathentry sourcepath="C:/Users/costin/.gradle/cache/org.springframework/spring-asm/sources/spring-asm-3.1.0.RELEASE-sources.jar" kind="lib" path="C:/Users/costin/.gradle/cache/org.springframework/spring-asm/jars/spring-asm-3.1.0.RELEASE.jar" exported="true"/>
|
||||
</classpath>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
#Tue Dec 13 20:42:40 EET 2011
|
||||
#Fri Mar 30 10:10:50 EEST 2012
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
|
||||
@@ -82,6 +82,7 @@ dependencies {
|
||||
|
||||
// Redis Drivers
|
||||
compile "redis.clients:jedis:$jedisVersion"
|
||||
compile "com.github.spullara.redis:client:$sredisVersion"
|
||||
compile("org.jredis:jredis-anthonylauzon:$jredisVersion") { optional = true }
|
||||
compile("org.idevlab:rjc:$rjcVersion") { optional = true }
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ log4jVersion = 1.2.16
|
||||
slf4jVersion = 1.6.4
|
||||
|
||||
# Common libraries
|
||||
springVersion = 3.1.0.RELEASE
|
||||
springVersion = 3.1.1.RELEASE
|
||||
jacksonVersion = 1.8.6
|
||||
|
||||
# Testing
|
||||
@@ -15,7 +15,8 @@ mockitoVersion = 1.8.5
|
||||
# Drivers
|
||||
jedisVersion = 2.0.0
|
||||
jredisVersion = 03122010
|
||||
rjcVersion= 0.6.4
|
||||
rjcVersion = 0.6.4
|
||||
sredisVersion = 0.1
|
||||
|
||||
# Manifest properties
|
||||
|
||||
@@ -24,6 +25,7 @@ spring.range = "[3.1.0, 4.0.0)"
|
||||
jedis.range = "[2.0.0, 2.0.0]"
|
||||
jackson.range = "[1.6, 2.0.0)"
|
||||
rjc.range = "[0.6.4, 0.6.4]"
|
||||
sredis.range = "[0.1, 1.0)"
|
||||
|
||||
# --------------------
|
||||
# Project wide version
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.sredis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.Socket;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import redis.client.SocketPool;
|
||||
|
||||
/**
|
||||
* Connection factory creating <a href="http://github.com/spullara/redis-protocol">Redis Protocol</a> based connections.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class SRedisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
||||
|
||||
private final static Log log = LogFactory.getLog(JedisConnectionFactory.class);
|
||||
|
||||
private String hostName = "localhost";
|
||||
private int port = 6379;
|
||||
private SocketPool pool;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>SRedisConnectionFactory</code> instance
|
||||
* with default settings.
|
||||
*/
|
||||
public SRedisConnectionFactory() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>SRedisConnectionFactory</code> instance
|
||||
* with default settings.
|
||||
*/
|
||||
public SRedisConnectionFactory(String host, int port) {
|
||||
this.hostName = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
pool = new SocketPool(hostName, port);
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
Field f = ReflectionUtils.findField(SocketPool.class, "queue");
|
||||
ReflectionUtils.makeAccessible(f);
|
||||
Queue<Socket> queue = (Queue<Socket>) ReflectionUtils.getField(f, pool);
|
||||
Socket s = null;
|
||||
do {
|
||||
s = queue.poll();
|
||||
if (s != null) {
|
||||
try {
|
||||
s.close();
|
||||
} catch (IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
} while (s != null);
|
||||
pool = null;
|
||||
}
|
||||
|
||||
public RedisConnection getConnection() {
|
||||
return new SRedisConnection(pool);
|
||||
}
|
||||
|
||||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
|
||||
return SRedisUtils.convertSRedisAccessException(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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.sredis;
|
||||
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.connection.util.AbstractSubscription;
|
||||
|
||||
import redis.client.RedisClient;
|
||||
|
||||
/**
|
||||
* Message subscription on top of SRP.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
class SRedisSubscription extends AbstractSubscription {
|
||||
|
||||
private final RedisClient client;
|
||||
|
||||
SRedisSubscription(MessageListener listener, RedisClient client) {
|
||||
super(listener);
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
protected void doClose() {
|
||||
client.unsubscribe(null);
|
||||
client.punsubscribe(null);
|
||||
}
|
||||
|
||||
|
||||
protected void doPsubscribe(byte[]... patterns) {
|
||||
client.psubscribe(patterns);
|
||||
}
|
||||
|
||||
|
||||
protected void doPUnsubscribe(boolean all, byte[]... patterns) {
|
||||
if (all) {
|
||||
client.punsubscribe(null);
|
||||
}
|
||||
else {
|
||||
client.punsubscribe(patterns);
|
||||
}
|
||||
}
|
||||
|
||||
protected void doSubscribe(byte[]... channels) {
|
||||
client.subscribe(channels);
|
||||
}
|
||||
|
||||
|
||||
protected void doUnsubscribe(boolean all, byte[]... channels) {
|
||||
if (all) {
|
||||
client.unsubscribe(null);
|
||||
}
|
||||
else {
|
||||
client.unsubscribe(channels);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Copyright 2011 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.sredis;
|
||||
|
||||
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;
|
||||
|
||||
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.RedisZSetCommands.Tuple;
|
||||
import org.springframework.data.redis.connection.SortParameters;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import redis.client.RedisException;
|
||||
import redis.reply.BulkReply;
|
||||
import redis.reply.MultiBulkReply;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
/**
|
||||
* Helper class featuring methods for SRedis connection handling, providing support for exception translation.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
abstract class SRedisUtils {
|
||||
|
||||
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.bytes, Charsets.UTF_8));
|
||||
try {
|
||||
info.load(stringReader);
|
||||
} catch (Exception ex) {
|
||||
throw new RedisSystemException("Cannot read Redis info", ex);
|
||||
} finally {
|
||||
stringReader.close();
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
static List<byte[]> toBytesList(Object[] byteArrays) {
|
||||
List<byte[]> list = new ArrayList<byte[]>(byteArrays.length);
|
||||
for (Object obj : byteArrays) {
|
||||
if (obj instanceof byte[])
|
||||
list.add((byte[]) obj);
|
||||
else
|
||||
throw new IllegalArgumentException("array contains more then just bytes" + obj);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
static <T> List<T> toList(T[] byteArrays) {
|
||||
return Arrays.asList(byteArrays);
|
||||
}
|
||||
|
||||
static Set<byte[]> toSet(Object[] 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 Double.valueOf(new String(bytes, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
static Long toLong(Object[] byteArrays) {
|
||||
return Long.valueOf(new String((byte[]) byteArrays[0], Charsets.UTF_8));
|
||||
}
|
||||
|
||||
static Set<Tuple> convertTuple(MultiBulkReply zrange) {
|
||||
Object[] byteArrays = zrange.byteArrays;
|
||||
Set<Tuple> tuples = new LinkedHashSet<Tuple>(byteArrays.length / 2 + 1);
|
||||
|
||||
for (int i = 0; i < byteArrays.length; i++) {
|
||||
byte[] value = (byte[]) byteArrays[i];
|
||||
i++;
|
||||
Double score = toDouble((byte[]) byteArrays[i]);
|
||||
tuples.add(new DefaultTuple(value, score));
|
||||
}
|
||||
|
||||
return tuples;
|
||||
}
|
||||
|
||||
static Map<byte[], byte[]> toMap(Object[] 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++], (byte[]) byteArrays[i]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
static byte[] limit(long offset, long count) {
|
||||
return ("LIMIT " + offset + " " + 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[]>();
|
||||
|
||||
if (params.getByPattern() != null) {
|
||||
arrays.add(BY);
|
||||
arrays.add(params.getByPattern());
|
||||
arrays.add(SPACE);
|
||||
}
|
||||
|
||||
if (params.getLimit() != null) {
|
||||
arrays.add(limit(params.getLimit().getStart(), params.getLimit().getCount()));
|
||||
arrays.add(SPACE);
|
||||
}
|
||||
|
||||
if (params.getGetPattern() != null) {
|
||||
byte[][] pattern = params.getGetPattern();
|
||||
for (byte[] bs : pattern) {
|
||||
arrays.add(GET);
|
||||
arrays.add(bs);
|
||||
arrays.add(SPACE);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.getOrder() != null) {
|
||||
arrays.add(params.getOrder().name().getBytes(Charsets.UTF_8));
|
||||
arrays.add(SPACE);
|
||||
}
|
||||
|
||||
if (params.isAlphabetic()) {
|
||||
arrays.add(ALPHA);
|
||||
}
|
||||
|
||||
if (sortKey != null) {
|
||||
arrays.add(STORE);
|
||||
arrays.add(sortKey);
|
||||
}
|
||||
|
||||
// concatenate array
|
||||
int size = 0;
|
||||
|
||||
for (byte[] bs : arrays) {
|
||||
size += 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Connection package for <a href="https://github.com/spullara/redis-protocol">spullara Redis Protocol</a> library.
|
||||
*/
|
||||
package org.springframework.data.redis.connection.sredis;
|
||||
|
||||
Reference in New Issue
Block a user