DATAKV-46

+ add initial Rjc connection/connection factory support
This commit is contained in:
Costin Leau
2011-03-16 09:49:07 +02:00
parent cd0dcf9b13
commit c385bc4d7c
7 changed files with 1011 additions and 17 deletions

View File

@@ -16,8 +16,10 @@
<spring.range>"[3.0.0, 4.0.0)"</spring.range>
<jredis.ver>03122010</jredis.ver>
<jedis.ver>1.5.2</jedis.ver>
<rjc.ver>0.6.2</rjc.ver>
<jedis.range>"[1.0.0,2.0.0)"</jedis.range>
<jackson.range>"[1.6, 2.0.0)"</jackson.range>
<rjc.range>"[0.6.2, 0.6.2]"</rjc.range>
</properties>
<dependencies>
@@ -135,27 +137,18 @@
<scope>compile</scope>
</dependency>
<!--
<dependency>
<groupId>org.jredis</groupId>
<artifactId>jredis-core-api</artifactId>
<version>${jredis.ver}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jredis</groupId>
<artifactId>jredis-core-ri</artifactId>
<version>${jredis.ver}</version>
<scope>compile</scope>
</dependency>
-->
<dependency>
<groupId>org.jredis</groupId>
<artifactId>jredis-anthonylauzon</artifactId>
<version>${jredis.ver}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.idevlab</groupId>
<artifactId>rjc</artifactId>
<version>${rjc.ver}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@@ -34,7 +34,7 @@ import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
/**
* Connection factory using creating <a href="http://github.com/xetorthio/jedis">Jedis</a> based connections.
* Connection factory creating <a href="http://github.com/xetorthio/jedis">Jedis</a> based connections.
*
* @author Costin Leau
*/

View File

@@ -81,7 +81,11 @@ public class JredisConnection implements RedisConnection {
// don't actually close the connection
// if a pool is used
if (!isPool) {
jredis.quit();
try {
jredis.quit();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
}
}

View File

@@ -0,0 +1,709 @@
/*
* 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.keyvalue.redis.connection.rjc;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.idevlab.rjc.RedisException;
import org.idevlab.rjc.Session;
import org.idevlab.rjc.SessionFactoryImpl;
import org.springframework.dao.DataAccessException;
import org.springframework.data.keyvalue.UncategorizedKeyvalueStoreException;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.connection.MessageListener;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.SortParameters;
import org.springframework.data.keyvalue.redis.connection.Subscription;
/**
* {@code RedisConnection} implementation on top of <a href="http://github.com/e-mzungu/rjc">rjc</a> library.
*
* @author Costin Leau
*/
public class RjcConnection implements RedisConnection {
private final int dbIndex;
private final Session session;
private boolean isClosed = false;
public RjcConnection(org.idevlab.rjc.ds.RedisConnection connection, int dbIndex) {
session = new SessionFactoryImpl(new SingleDataSource(connection)).create();
this.dbIndex = dbIndex;
// select the db
if (dbIndex > 0) {
select(dbIndex);
}
}
protected DataAccessException convertRjcAccessException(Exception ex) {
if (ex instanceof RedisException) {
return RjcUtils.convertRjcAccessException((RedisException) ex);
}
return new UncategorizedKeyvalueStoreException("Unknown rjc exception", ex);
}
@Override
public void close() throws DataAccessException {
isClosed = true;
try {
session.close();
} catch (Exception ex) {
throw convertRjcAccessException(ex);
}
}
@Override
public boolean isClosed() {
return isClosed;
}
@Override
public Session getNativeConnection() {
return session;
}
@Override
public List<byte[]> closePipeline() {
throw new UnsupportedOperationException();
}
@Override
public boolean isPipelined() {
throw new UnsupportedOperationException();
}
@Override
public boolean isQueueing() {
throw new UnsupportedOperationException();
}
@Override
public void openPipeline() {
throw new UnsupportedOperationException();
}
@Override
public Long del(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public byte[] echo(byte[] message) {
throw new UnsupportedOperationException();
}
@Override
public Boolean exists(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Boolean expire(byte[] key, long seconds) {
throw new UnsupportedOperationException();
}
@Override
public Boolean expireAt(byte[] key, long unixTime) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> keys(byte[] pattern) {
throw new UnsupportedOperationException();
}
@Override
public Boolean persist(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public String ping() {
throw new UnsupportedOperationException();
}
@Override
public byte[] randomKey() {
throw new UnsupportedOperationException();
}
@Override
public void rename(byte[] oldName, byte[] newName) {
throw new UnsupportedOperationException();
}
@Override
public Boolean renameNX(byte[] oldName, byte[] newName) {
throw new UnsupportedOperationException();
}
@Override
public void select(int dbIndex) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> sort(byte[] key, SortParameters params) {
throw new UnsupportedOperationException();
}
@Override
public Long sort(byte[] key, SortParameters params, byte[] storeKey) {
throw new UnsupportedOperationException();
}
@Override
public Long ttl(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public DataType type(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public void discard() {
throw new UnsupportedOperationException();
}
@Override
public List<Object> exec() {
throw new UnsupportedOperationException();
}
@Override
public void multi() {
throw new UnsupportedOperationException();
}
@Override
public void unwatch() {
throw new UnsupportedOperationException();
}
@Override
public void watch(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public Long append(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long decr(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long decrBy(byte[] key, long value) {
throw new UnsupportedOperationException();
}
@Override
public byte[] get(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Boolean getBit(byte[] key, long offset) {
throw new UnsupportedOperationException();
}
@Override
public byte[] getRange(byte[] key, int begin, int end) {
throw new UnsupportedOperationException();
}
@Override
public byte[] getSet(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long incr(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long incrBy(byte[] key, long value) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> mGet(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public void mSet(Map<byte[], byte[]> tuple) {
throw new UnsupportedOperationException();
}
@Override
public void mSetNX(Map<byte[], byte[]> tuple) {
throw new UnsupportedOperationException();
}
@Override
public void set(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public void setBit(byte[] key, long offset, boolean value) {
throw new UnsupportedOperationException();
}
@Override
public void setEx(byte[] key, long seconds, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Boolean setNX(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public void setRange(byte[] key, int begin, int end) {
throw new UnsupportedOperationException();
}
@Override
public Long strLen(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> bLPop(int timeout, byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> bRPop(int timeout, byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public byte[] bRPopLPush(int timeout, byte[] srcKey, byte[] dstKey) {
throw new UnsupportedOperationException();
}
@Override
public byte[] lIndex(byte[] key, long index) {
throw new UnsupportedOperationException();
}
@Override
public Long lInsert(byte[] key, Position where, byte[] pivot, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long lLen(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public byte[] lPop(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long lPush(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long lPushX(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> lRange(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Long lRem(byte[] key, long count, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public void lSet(byte[] key, long index, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public void lTrim(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public byte[] rPop(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public byte[] rPopLPush(byte[] srcKey, byte[] dstKey) {
throw new UnsupportedOperationException();
}
@Override
public Long rPush(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long rPushX(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Boolean sAdd(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long sCard(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> sDiff(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public void sDiffStore(byte[] destKey, byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> sInter(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public void sInterStore(byte[] destKey, byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public Boolean sIsMember(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> sMembers(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public byte[] sPop(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public byte[] sRandMember(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Boolean sRem(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> sUnion(byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public void sUnionStore(byte[] destKey, byte[]... keys) {
throw new UnsupportedOperationException();
}
@Override
public Boolean zAdd(byte[] key, double score, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long zCard(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long zCount(byte[] key, double min, double max) {
throw new UnsupportedOperationException();
}
@Override
public Double zIncrBy(byte[] key, double increment, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long zInterStore(byte[] destKey, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> zRange(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, long offset, long count) {
throw new UnsupportedOperationException();
}
@Override
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max) {
throw new UnsupportedOperationException();
}
@Override
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, long offset, long count) {
throw new UnsupportedOperationException();
}
@Override
public Set<Tuple> zRangeWithScore(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Long zRank(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Boolean zRem(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long zRemRange(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Long zRemRangeByScore(byte[] key, double min, double max) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> zRevRange(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Set<Tuple> zRevRangeWithScore(byte[] key, long begin, long end) {
throw new UnsupportedOperationException();
}
@Override
public Long zRevRank(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Double zScore(byte[] key, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Long zUnionStore(byte[] destKey, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Boolean hDel(byte[] key, byte[] field) {
throw new UnsupportedOperationException();
}
@Override
public Boolean hExists(byte[] key, byte[] field) {
throw new UnsupportedOperationException();
}
@Override
public byte[] hGet(byte[] key, byte[] field) {
throw new UnsupportedOperationException();
}
@Override
public Map<byte[], byte[]> hGetAll(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long hIncrBy(byte[] key, byte[] field, long delta) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> hKeys(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public Long hLen(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> hMGet(byte[] key, byte[]... fields) {
throw new UnsupportedOperationException();
}
@Override
public void hMSet(byte[] key, Map<byte[], byte[]> hashes) {
throw new UnsupportedOperationException();
}
@Override
public Boolean hSet(byte[] key, byte[] field, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public Boolean hSetNX(byte[] key, byte[] field, byte[] value) {
throw new UnsupportedOperationException();
}
@Override
public List<byte[]> hVals(byte[] key) {
throw new UnsupportedOperationException();
}
@Override
public void bgSave() {
throw new UnsupportedOperationException();
}
@Override
public void bgWriteAof() {
throw new UnsupportedOperationException();
}
@Override
public Long dbSize() {
throw new UnsupportedOperationException();
}
@Override
public void flushAll() {
throw new UnsupportedOperationException();
}
@Override
public void flushDb() {
throw new UnsupportedOperationException();
}
@Override
public List<String> getConfig(String pattern) {
throw new UnsupportedOperationException();
}
@Override
public Properties info() {
throw new UnsupportedOperationException();
}
@Override
public Long lastSave() {
throw new UnsupportedOperationException();
}
@Override
public void resetConfigStats() {
throw new UnsupportedOperationException();
}
@Override
public void save() {
throw new UnsupportedOperationException();
}
@Override
public void setConfig(String param, String value) {
throw new UnsupportedOperationException();
}
@Override
public void shutdown() {
throw new UnsupportedOperationException();
}
@Override
public Subscription getSubscription() {
throw new UnsupportedOperationException();
}
@Override
public boolean isSubscribed() {
throw new UnsupportedOperationException();
}
@Override
public void pSubscribe(MessageListener listener, byte[]... patterns) {
throw new UnsupportedOperationException();
}
@Override
public Long publish(byte[] channel, byte[] message) {
throw new UnsupportedOperationException();
}
@Override
public void subscribe(MessageListener listener, byte[]... channels) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,209 @@
/*
* 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.keyvalue.redis.connection.rjc;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.idevlab.rjc.ds.DataSource;
import org.idevlab.rjc.ds.PoolableDataSource;
import org.idevlab.rjc.ds.SimpleDataSource;
import org.idevlab.rjc.protocol.Protocol;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.util.Assert;
/**
* Connection factory creating <a href="http://github.com/e-mzungu/rjc/">rjc</a> based connections.
*
* @author Costin Leau
*/
public class RjcConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
private final static Log log = LogFactory.getLog(JedisConnectionFactory.class);
private String hostName = "localhost";
private int port = Protocol.DEFAULT_PORT;
private int timeout = Protocol.DEFAULT_TIMEOUT;
private String password;
private boolean usePool = true;
private int dbIndex = 0;
private DataSource dataSource;
/**
* Constructs a new <code>RjcConnectionFactory</code> instance
* with default settings (default connection pooling, no shard information).
*/
public RjcConnectionFactory() {
}
public void afterPropertiesSet() {
if (usePool) {
PoolableDataSource pool = new PoolableDataSource();
pool.setHost(hostName);
pool.setPort(port);
pool.setPassword(password);
pool.setTimeout(timeout);
pool.init();
dataSource = pool;
}
else {
dataSource = new SimpleDataSource(hostName, port, timeout, password);
}
}
public void destroy() {
if (usePool && dataSource != null) {
try {
((PoolableDataSource) dataSource).close();
} catch (Exception ex) {
log.warn("Cannot properly close Rjc pool", ex);
}
dataSource = null;
}
}
@Override
public RedisConnection getConnection() {
return postProcessConnection(new RjcConnection(dataSource.getConnection(), usePool, dbIndex));
}
/**
* 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 RjcConnection postProcessConnection(RjcConnection connection) {
return connection;
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return RjcUtils.convertRjcAccessException(ex);
}
/**
* Returns the Redis hostName.
*
* @return Returns the hostName
*/
public String getHostName() {
return hostName;
}
/**
* Sets the Redis hostName.
*
* @param hostName The hostName to set.
*/
public void setHostName(String hostName) {
this.hostName = hostName;
}
/**
* 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 port used to connect to the Redis instance.
*
* @return Redis port.
*/
public int getPort() {
return port;
}
/**
* Sets the port used to connect to the Redis instance.
*
* @param port Redis port
*/
public void setPort(int port) {
this.port = port;
}
/**
* Returns the timeout.
*
* @return Returns the timeout
*/
public int getTimeout() {
return timeout;
}
/**
* @param timeout The timeout to set.
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
}
/**
* Indicates the use of a connection pool.
*
* @return Returns the use of connection pooling.
*/
public boolean getUsePool() {
return usePool;
}
/**
* Turns on or off the use of connection pooling.
*
* @param usePool The usePool to set.
*/
public void setUsePool(boolean usePool) {
this.usePool = usePool;
}
/**
* 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;
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.keyvalue.redis.connection.rjc;
import org.idevlab.rjc.RedisException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.keyvalue.redis.UncategorizedRedisException;
/**
* Helper class featuring methods for RJC connection handling, providing support for exception translation.
*
* @author Costin Leau
*/
public abstract class RjcUtils {
public static DataAccessException convertRjcAccessException(RuntimeException ex) {
if (ex instanceof RedisException) {
return convertRjcAccessException((RedisException) ex);
}
return new UncategorizedRedisException("Unknown exception", ex);
}
public static DataAccessException convertRjcAccessException(RedisException ex) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.keyvalue.redis.connection.rjc;
import org.idevlab.rjc.ds.DataSource;
import org.idevlab.rjc.ds.RedisConnection;
/**
* Basic data source that always returns the same connection.
*
* @author Costin Leau
*/
class SingleDataSource implements DataSource {
private final RedisConnection connection;
SingleDataSource(RedisConnection connection) {
this.connection = connection;
}
@Override
public RedisConnection getConnection() {
return connection;
}
}