first draft for SPRP driver
This commit is contained in:
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