- removed some of the old code

This commit is contained in:
Costin Leau
2010-11-05 11:59:23 +02:00
parent 414c0a7025
commit ef792bac1a
17 changed files with 0 additions and 1765 deletions

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
/**
* Common base class for RedisClient implementations
* @author Mark Pollack
*
*/
public abstract class AbstractRedisClient implements RedisClient {
protected final Log logger = LogFactory.getLog(this.getClass());
public static final String DEFAULT_CHARSET = "UTF-8";
private volatile String defaultCharset = DEFAULT_CHARSET;
/**
* Specify the default charset to use when converting to or from text-based
* Message body content. If not specified, the charset will be "UTF-8".
*/
public void setDefaultCharset(String defaultCharset) {
this.defaultCharset = (defaultCharset != null) ? defaultCharset : DEFAULT_CHARSET;
}
public String getDefaultCharset() {
return defaultCharset;
}
protected byte[] stringToByte(String string) throws InvalidDataAccessApiUsageException {
try {
return string.getBytes(this.defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new InvalidDataAccessApiUsageException(defaultCharset
+ " encoding not supported.", e);
}
}
protected String byteToString(byte[] value) throws InvalidDataAccessApiUsageException {
try {
return new String(value, defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new InvalidDataAccessApiUsageException(defaultCharset
+ " encoding not supported.", e);
}
}
}

View File

@@ -1,102 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.datastore.redis.support.RedisPersistenceExceptionTranslator;
/**
* Common base class for RedisClientFactories
* @author Mark Pollack
*
*/
public abstract class AbstractRedisClientFactory implements RedisClientFactory {
protected final Log logger = LogFactory.getLog(getClass());
private String hostName;
private int port;
private String password;
public int getPort() {
return port;
}
protected void setPort(int port) {
this.port = port;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHostName() {
return hostName;
}
protected void setHostName(String hostName) {
this.hostName = hostName;
}
public RedisClient createClient() {
return doGetClient();
}
public abstract RedisClient doGetClient();
public abstract RedisPersistenceExceptionTranslator getExceptionTranslator();
protected String getDefaultHostName() {
String temp;
try {
InetAddress localMachine = InetAddress.getLocalHost();
temp = localMachine.getHostName();
logger.debug("Using hostname [" + temp + "] for hostname.");
}
catch (UnknownHostException e) {
logger.warn("Could not get host name, using 'localhost' as default value", e);
temp = "localhost";
}
return temp;
}
/*
public void closeClient() {
if (logger.isDebugEnabled()) {
logger.debug("Closing Redis Client: " + this.client);
}
try {
client.close();
}
catch (Throwable ex) {
logger.debug("Could not close Redis Client", ex);
}
}*/
}

View File

@@ -1,42 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DefaultServerOperations implements ServerOperations {
protected final Log logger = LogFactory.getLog(getClass());
private RedisOperations redisOperations;
public DefaultServerOperations(RedisOperations redisOperations) {
this.redisOperations = redisOperations;
}
public Map<String, String> getServerInfo() {
return redisOperations.execute(new RedisCallback<Map<String,String>>() {
public Map<String, String> doInRedis(RedisClient redisClient)
throws Exception {
return redisClient.info();
}
});
}
}

View File

@@ -1,93 +0,0 @@
package org.springframework.datastore.redis.core;
import java.util.Set;
public class DefaultSetOperations implements SetOperations {
private RedisTemplate redisTemplate;
public DefaultSetOperations(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public boolean add(final String key, final String member) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisClient redisClient) throws Exception {
return (redisClient.sadd(key, member) == 0) ? true : false;
}
});
}
public Set<String> getAll(final String key) {
return redisTemplate.execute(new RedisCallback<Set<String>>() {
public Set<String> doInRedis(RedisClient redisClient) throws Exception {
return redisClient.smembers(key);
}
});
}
public boolean remove(String key, String member) {
// TODO Auto-generated method stub
return false;
}
public boolean removeRandom(String key) {
// TODO Auto-generated method stub
return false;
}
public boolean moveBetweenSets(String srckey, String dstkey, String member) {
// TODO Auto-generated method stub
return false;
}
public int size(String key) {
// TODO Auto-generated method stub
return 0;
}
public boolean contains(String key, String member) {
// TODO Auto-generated method stub
return false;
}
public Set<String> getIntersectionOfSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeIntersectionOfSets(final String dstkey, final String... keys) {
redisTemplate.execute(new RedisCallback<Void>() {
public Void doInRedis(RedisClient redisClient) throws Exception {
redisClient.sinterstore(dstkey, keys);
return null;
}
});
}
public Set<String> getUnionOfSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeUnionOfSets(String dstkey, String... keys) {
// TODO Auto-generated method stub
}
public Set<String> getDifferenceBetweenSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeDifferenceBetweenSets(String dstkey, String... keys) {
// TODO Auto-generated method stub
}
public String getRandom(String key) {
// TODO Auto-generated method stub
return null;
}
}

View File

@@ -1,29 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
/**
* List operations with 'friendly' names instead of using Redis command names for methods.
*
* May also include List specific helper methods from redis recipies.
* @author Mark Pollack
*
*/
public interface ListOperations {
//ListRecipies getListRecipies();
}

View File

@@ -1,69 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import com.sun.xml.internal.bind.v2.TODO;
/**
* Base class for {@link RedisTemplate} and
* other Redis-accessing DAO helpers, defining common properties such as
* RedisClientFactory.
*
* @author Mark Pollack
*
*/
public class RedisAccessor implements InitializingBean {
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private volatile RedisClientFactory redisClientFactory;
/**
* Set the RedisClientFactory to use for obtaining Redis {@link RedisClient clients}.
*/
public void setRedisClientFactory(RedisClientFactory redisClientFactory) {
this.redisClientFactory = redisClientFactory;
}
/**
* Return the RedisClientFactory that this accessor uses for obtaining
* Redis {@link RedisClient Clients}.
*/
public RedisClientFactory getRedisClientFactory() {
return this.redisClientFactory;
}
/**
* Create a Redis Client
* @return the new Redis Client
* @throws TODO
*/
protected RedisClient createClient() {
return this.redisClientFactory.createClient();
}
public void afterPropertiesSet() {
Assert.notNull(getRedisClientFactory(), "RedisClientfactory is required");
}
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
/**
* Basic callback for use in RedisTemplate
* @author Mark Pollack
*
* @param <T> TODO
*/
public interface RedisCallback<T> {
/**
* Execute any number of operations against the supplied RedisClient
* {@link RedicClient}, possibly returning a result.
*/
T doInRedis(RedisClient redisClient) throws Exception;
}

View File

@@ -1,220 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* An interface that is a one to one mapping to Redis commands to method names
* that is portable across various Redis driver libraries.
*
* @author Mark Pollack
*
*/
public interface RedisClient {
// Connection Management
void disconnect() throws IOException;
// Database control commands
String save();
String bgsave();
String bgrewriteaof();
Integer lastsave();
String shutdown();
Map<String, String> info();
//bulk reply callback - monitor
String slaveof(String host, int port);
String slaveofNoOne();
String select(int index);
String flushDb();
String flushAll();
Integer move(String key, int dbIndex);
String auth(String password);
Integer dbSize();
// Note: JRedis and the SMA client do not return the response code for set, would probably have to catch exception.
// Commands operating on string values "StringOperations" or "Operations"
/**
* Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1 GB).
* <p>Time complexity: O(1)</p>
* <p>Corresponds to Redis command "SET key value"</p>
* @see <a href="http://code.google.com/p/redis/wiki/SetCommand">setCommand</a>
* @param key key whose associated value is to be returned
* @param value value to be associated with the specified key
*/
void set(String key, String value);
void set(String key, byte[] value);
String get(String key);
byte[] getAsBytes(String key);
String getSet(String key, String value);
List<String> mget(String... keys);
//TODO mgetAsBytes? Best to have byte[] overloads somewhere else?
/**
* SETNX works exactly like SET with the only difference that if the key already exists no operation is performed.
* SETNX actually means "SET if Not eXists".
* <p>Time complexity: O(1)</p>
* <p>Corresponds to command "SETNX key value"</p>
* @see <a href="http://code.google.com/p/redis/wiki/SetnxCommand">SetnxCommand</a>
* @param key key whose associated value is to be set
* @param value value to be associated with the specified key
* @return 1 if the key was set, 0 if the key was not set
*/
Integer setnx(String key, String value);
/**
* The command is exactly equivalent to the following group of commands:
* <p>SET key value
* EXPIRE key time
* </p>
* <p>Time complexity: O(1)</p>
* @see <a href="http://code.google.com/p/redis/wiki/SetexCommand">SetexCommand</a>
* @param key key whose associated value is to be set
* @param seconds timeout on the specified key. After the timeout the key will automatically be deleted by the server
* @param value timeout in seconds
* @return Status reply code, OK is success
*/
String setex(String key, int seconds, String value);
/**
* Set the the respective keys to the respective values.
* <p>Time complexity: O(1) to set every key</p>
* <p>Corresponds to the command "MSET key1 value1 key2 value2 ... keyN valueN"</p>
* @see <a href="http://code.google.com/p/redis/wiki/MsetCommand">MsetCommand</a>
* @param keysvalues key value sequence
* @return OK as MSET can't fail.
*/
//TODO Consider Map<string,string> here or in template? Map<string, byte> ?
String mset(String... keysvalues);
Integer msetnx(String... keysvalues);
Integer incrBy(String key, int increment);
Integer incr(String key);
Integer decr(String key);
Integer decrBy(String key, int decrement);
//TODO incrementByOne,decrementByOne in template
/**
* If the key already exists and is a string, this command appends the provided value at the
* end of the string. If the key does not exist it is created and set as an empty string,
* so APPEND will be very similar to SET in this special case.
* @see <a href="http://code.google.com/p/redis/wiki/AppendCommand">AppendCommand</a>
* @param key key whose associated value is to be appended
* @param value value to be appended to end of current value associated with the specified key
* @return the total length of the string after the append operation.
*/
Integer append(String key, String value);
String substr(String key, int start, int end);
// Commands operating on all value types "KeySpaceOperations"
Integer exists(String key);
Integer del(String... keys);
String type(String key);
List<String> keys(String pattern);
String randomKey();
String rename(String oldkey, String newkey);
Integer renamenx(String oldkey, String newkey);
Integer expire(String key, int seconds);
Integer expireAt(String key, long unixTime);
Integer ttl(String key);
Integer persist(String key);
// Probably not possible to abstract at this level across different providers....
// T sendCommand(String commandName, ReplyTypeMapper mapper, String... commandArgs);
// Commands operating on Sets
Integer sadd(String key, String member);
Set<String> smembers(String key);
Integer srem(String key, String member);
String spop(String key);
Integer smove(String srckey, String dstkey, String member);
Integer scard(String key);
Integer sismember(String key, String member);
Set<String> sinter(String... keys);
Integer sinterstore(String dstkey, String... keys);
Set<String> sunion(String... keys);
Integer sunionstore(String dstkey, String... keys);
Set<String> sdiff(String... keys);
Integer sdiffstore(String dstkey, String... keys);
String srandmember(String key);
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import org.springframework.datastore.redis.support.RedisPersistenceExceptionTranslator;
/**
* An interface based ConnectionFactory for creating {@link RedisClient}s.
*
* @author Mark Pollack
*
*/
public interface RedisClientFactory {
RedisClient createClient();
void setPassword(String password);
RedisPersistenceExceptionTranslator getExceptionTranslator();
}

View File

@@ -1,42 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.util.List;
import org.springframework.dao.DataAccessException;
/**
* Interface specifying a set of Redis operations.
* Implemented by {@link RedisTemplate}.
*
* @author Mark Pollack
*
*/
public interface RedisOperations extends KeyValueOperations {
<T> T execute(RedisCallback<T> action) throws DataAccessException;
ServerOperations getServerOperations();
ListOperations getListOperations();
SetOperations getSetOperations();
}

View File

@@ -1,292 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.datastore.redis.support.RedisUtils;
import org.springframework.datastore.redis.support.converter.DefaultRedisConverter;
import org.springframework.datastore.redis.support.converter.RedisConverter;
import org.springframework.util.Assert;
/**
* <b>This is the central class in the Redis core package.</b>
* It simplifies the use of Redis and helps to avoid common errors.
*
* @author Mark Pollack
*
*/
public class RedisTemplate extends RedisAccessor implements RedisOperations {
// TODO perform validation to see if value size > 1GB
// TODO perform validation to see if key contains space, newline or whitespace.
// TODO warning on key size being large > 1024 bytes?
private RedisConverter redisConverter = new DefaultRedisConverter();
private ServerOperations serverOperations;
public RedisTemplate() {
initDefaults();
}
public RedisTemplate(RedisClientFactory redisClientFactory) {
this();
this.setRedisClientFactory(redisClientFactory);
afterPropertiesSet();
}
public void setRedisConverter(RedisConverter redisConverter) {
this.redisConverter = redisConverter;
}
protected void initDefaults() {
serverOperations = new DefaultServerOperations(this);
}
public <T> T execute(RedisCallback<T> action) {
Assert.notNull(action, "Callback object must not be null");
RedisClient clientToClose = null;
try {
RedisClient clientToUse = null; //ConnectionFactoryUtils.doGetTransacxtionChannel(getConnectionFactory, this.transactionResourceFactory);
if (clientToUse == null) {
clientToClose = createClient();
clientToUse = clientToClose;
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on Redis Client: " + clientToUse);
}
return action.doInRedis(clientToUse);
}
catch (Exception e) {
throw convertRedisAccessException(e);
} finally {
RedisUtils.closeClient(clientToClose);
}
}
protected DataAccessException convertRedisAccessException(Exception ex) {
//TODO
return null;
}
public ServerOperations getServerOperations() {
return serverOperations;
}
public ListOperations getListOperations() {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
// Key Value Operations
public String get(final String key) {
return execute(new RedisCallback<String>() {
public String doInRedis(RedisClient redisClient) throws Exception {
return redisClient.get(key);
}
});
}
public byte[] getAsBytes(final String key) {
return execute(new RedisCallback<byte[]>() {
public byte[] doInRedis(RedisClient redisClient) throws Exception {
return redisClient.getAsBytes(key);
}
});
}
public <T> T getAndConvert(String key, Class<T> requiredType) {
//TODO deserializer exceptions need to be under DAO exception hierarchy.
Object object = redisConverter.deserialize(getAsBytes(key));
if (requiredType != null && object != null && !requiredType.isAssignableFrom(object.getClass())) {
throw new DataRetrievalFailureException("Can not assign from " + requiredType + " to " + object.getClass());
}
return (T) object;
}
public String getAndSet(String key, String value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public byte[] getAndSetBytes(String key, byte[] value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public <T> T getAndSetObject(String key, T value, Class<T> requiredType) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void set(final String key, final String value) {
execute(new RedisCallback<Void>() {
public Void doInRedis(RedisClient redisClient) throws Exception {
redisClient.set(key, value);
return null;
}
});
}
public void set(String key, String value, long expiryInMillis) {
// TODO Auto-generated method stub
}
public void setAsBytes(final String key, final byte[] value) {
execute(new RedisCallback<Void>() {
public Void doInRedis(RedisClient redisClient) throws Exception {
redisClient.set(key, value);
return null;
}
});
}
public void setAsBytes(String key, byte[] value, long expiryInMillis) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setIfKeyNonExistent(String key, String value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setIfKeyNonExistent(String key, byte[] value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setMultiple(Map<String, String> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setMultipleAsBytes(Map<String, byte[]> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setMultipleAsBytesIfKeysNonExistent(
Map<String, byte[]> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void setMultipleIfKeysNonExistent(Map<String, String> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public int append(String key, String value) {
throw new RuntimeException("unimplemented");
}
public void convertAndSet(String key, Object value) {
setAsBytes(key, this.redisConverter.serialize(value));
}
public void convertAndSet(String key, Object value, long expiryInMillis) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public void convertAndSetIfKeyNonExistent(String key, Object value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public <T> void convertAndSetMultiple(Map<String, T> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public <T> void convertAndSetMultipleIfKeysNonExistent(
Map<String, T> keysAndValues) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public int decrement(String key) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public int decrementBy(String key, int value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public List<String> getValues(List<String> keys) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public int increment(String key) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public int incrementBy(String key, int value) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public String subString(String key, int fromIndex, int toIndex) {
// TODO Auto-generated method stub
throw new RuntimeException("unimplemented");
}
public <T> List<T> getAndConvertValues(List<String> keys,
Class<T> requiredType) {
// TODO Auto-generated method stub
return null;
}
public String getSubString(String key, int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
public boolean containsKey(String key) {
// TODO Auto-generated method stub
return false;
}
public boolean deleteKeys(final String... keys) {
return execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisClient redisClient) throws Exception {
Integer intVal = redisClient.del(keys);
return (intVal == 0) ? false : true;
}
});
}
public SetOperations getSetOperations() {
return new DefaultSetOperations(this);
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core;
import java.util.Map;
/**
* Server operations for Redis
*
* @author Mark Pollack
*
*/
public interface ServerOperations {
// Connection handling
/**
* Calls the Redis 'info' command that returns different information and statistics about the server.
* The reply is parsed into a Map for easy programmatic access.
* Corresponds to the Redis InfoCommand INFO
* @see <a href="http://code.google.com/p/redis/wiki/InfoCommand">InfoCommand</a>
* @return
*/
Map<String,String> getServerInfo();
// TODO Commands Monitor, SlaveOf, Config
}

View File

@@ -1,35 +0,0 @@
package org.springframework.datastore.redis.core;
import java.util.Set;
public interface SetOperations {
boolean add(String key, String member);
Set<String> getAll(String key);
boolean remove(String key, String member);
boolean removeRandom(String key);
boolean moveBetweenSets(String srckey, String dstkey, String member);
int size(String key);
boolean contains(String key, String member);
Set<String> getIntersectionOfSets(String... keys);
void storeIntersectionOfSets(String dstkey, String... keys);
Set<String> getUnionOfSets(String... keys);
void storeUnionOfSets(String dstkey, String... keys);
Set<String> getDifferenceBetweenSets(String... keys);
void storeDifferenceBetweenSets(String dstkey, String... keys);
String getRandom(String key);
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core.jredis;
import org.jredis.ri.alphazero.JRedisClient;
/**
* Basic callback for use in JRedisClient
* @author Mark Pollack
*
* @param <T> TODO
*/
public interface JRedisClientCallback<T> {
/**
* Execute any number of operations against the supplied RedisClient
* {@link RedicClient}, possibly returning a result.
*/
T doInJRedis(JRedisClient jredisClient) throws Exception;
}

View File

@@ -1,111 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core.jredis;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.jredis.ClientRuntimeException;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.redis.core.AbstractRedisClientFactory;
import org.springframework.datastore.redis.core.RedisClient;
import org.springframework.datastore.redis.support.RedisPersistenceExceptionTranslator;
public class JRedisClientFactory extends AbstractRedisClientFactory {
public static final String DEFAULT_CHARSET = "UTF-8";
private volatile String defaultCharset = DEFAULT_CHARSET;
private RedisPersistenceExceptionTranslator exceptionTranslator;
private ConnectionSpec connectionSpec;
public JRedisClientFactory() {
setHostName(getDefaultHostName());
exceptionTranslator = new JRedisPersistenceExceptionTranslator();
}
public JRedisClientFactory(ConnectionSpec connectionSpec) {
this.connectionSpec = connectionSpec;
}
@Override
public RedisClient doGetClient() {
JRedisClient jredis;
if (connectionSpec == null) {
connectionSpec = DefaultConnectionSpec.newSpec();
InetAddress address;
try {
address = InetAddress.getByName(getHostName());
} catch (UnknownHostException e) {
throw new ClientRuntimeException("unknown host: "
+ getHostName(), e);
}
connectionSpec.setAddress(address);
if (getPort() != 0) {
connectionSpec.setPort(getPort());
}
if (getPassword() != null) {
connectionSpec.setCredentials(stringToByte(getPassword()));
}
}
jredis = new JRedisClient(connectionSpec);
return new JRedisSpringClient(jredis, getExceptionTranslator());
}
protected byte[] stringToByte(String string) throws InvalidDataAccessApiUsageException {
try {
return string.getBytes(this.defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new InvalidDataAccessApiUsageException(defaultCharset
+ " encoding not supported.", e);
}
}
/**
* Specify the default charset to use when converting to or from text-based
* Message body content. If not specified, the charset will be "UTF-8".
*/
public void setDefaultCharset(String defaultCharset) {
this.defaultCharset = (defaultCharset != null) ? defaultCharset : DEFAULT_CHARSET;
}
public String getDefaultCharset() {
return defaultCharset;
}
@Override
public RedisPersistenceExceptionTranslator getExceptionTranslator() {
return exceptionTranslator;
}
public void setExceptionTranslator(
RedisPersistenceExceptionTranslator exceptionTranslator) {
this.exceptionTranslator = exceptionTranslator;
}
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core.jredis;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.redis.support.RedisPersistenceExceptionTranslator;
public class JRedisPersistenceExceptionTranslator implements RedisPersistenceExceptionTranslator {
public DataAccessException translateException(Exception ex) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
}

View File

@@ -1,494 +0,0 @@
/*
* Copyright 2010 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.datastore.redis.core.jredis;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.support.DefaultCodec;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.datastore.redis.core.AbstractRedisClient;
import org.springframework.datastore.redis.support.RedisPersistenceExceptionTranslator;
import org.springframework.util.Assert;
/**
* JRedis implementation of RedisClient. Name has 'Spring' in it to avoid naming
* conflict with classes in JRedis itself.
*
* @author Mark Pollack
*
*/
public class JRedisSpringClient extends AbstractRedisClient {
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private JRedisClient _jredisClient;
private RedisPersistenceExceptionTranslator exceptionTranslator;
public JRedisSpringClient(JRedisClient jredisClient,
RedisPersistenceExceptionTranslator exceptionTransator) {
this._jredisClient = jredisClient;
this.exceptionTranslator = exceptionTransator;
this.setDefaultCharset(DefaultCodec.SUPPORTED_CHARSET_NAME);
}
protected Integer convertToInteger(long longTime) {
if (longTime < Integer.MIN_VALUE
|| longTime > Integer.MAX_VALUE) {
throw new DataRetrievalFailureException(
longTime
+ " cannot be cast to int without changing its value.");
}
return (int) longTime;
}
public <T> T execute(JRedisClientCallback<T> action) {
Assert.notNull(action, "Callback object must not be null");
// TODO jredisClient resource mgmt.
try {
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JRedisClient : "
+ _jredisClient);
}
return action.doInJRedis(_jredisClient);
} catch (Exception e) {
throw convertJRedisAccessException(e);
}
}
protected DataAccessException convertJRedisAccessException(Exception ex) {
return exceptionTranslator.translateException(ex);
}
public void disconnect() {
// TODO look at disconnect exception translation
execute(new JRedisClientCallback<Object>() {
public Object doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.quit();
return null;
}
});
}
public String get(final String key) {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
return byteToString(jredisClient.get(key));
}
});
}
public byte[] getAsBytes(final String key) {
return execute(new JRedisClientCallback<byte[]>() {
public byte[] doInJRedis(JRedisClient jredisClient)
throws Exception {
return jredisClient.get(key);
}
});
}
public void set(final String key, final String value) {
execute(new JRedisClientCallback<Object>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.set(key, value);
return null;
}
});
}
public void set(final String key, final byte[] value) {
execute(new JRedisClientCallback<Object>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.set(key, value);
return null;
}
});
}
public String save() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.save();
return "OK";
}
});
}
public String bgsave() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.bgsave();
return "Background saving started";
}
});
}
public String bgrewriteaof() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.bgrewriteaof();
return "Background append only file rewriting started";
}
});
}
public Integer lastsave() {
return execute(new JRedisClientCallback<Integer>() {
public Integer doInJRedis(JRedisClient jredisClient)
throws Exception {
long longTime = jredisClient.lastsave();
// odd that JRedis return long when the Redis command spec says
// int.
return convertToInteger(longTime);
}
});
}
public String shutdown() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
throw new UnsupportedOperationException("JRedis does not implement SHUTDOWN command");
}
});
}
public Map<String, String> info() {
return execute(new JRedisClientCallback<Map<String, String>>() {
public Map<String, String> doInJRedis(JRedisClient jredisClient)
throws Exception {
return jredisClient.info();
}
});
}
public String slaveof(final String host, final int port) {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.slaveof(host,port);
return "TODO - EXTRACT CORRECT STRING RESPONSE FROM REDIS FOR COMMAND SLAVEOF";
}
});
}
public String slaveofNoOne() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.slaveofnone();
return "TODO - EXTRACT CORRECT STRING RESPONSE FROM REDIS FOR COMMAND SLAVEOF NO ONE";
}
});
}
public String select(int index) {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
throw new UnsupportedOperationException("JRedis does not implement SELECT command");
}
});
}
public String flushDb() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.flushdb();
//TODO why does flushdb() return JRedis interface?
return "OK";
}
});
}
public String flushAll() {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
jredisClient.flushall();
//TODO why does flushdb() return JRedis interface?
return "OK";
}
});
}
public Integer move(final String key, final int dbIndex) {
return execute(new JRedisClientCallback<Integer>() {
public Integer doInJRedis(JRedisClient jredisClient)
throws Exception {
return jredisClient.move(key, dbIndex) ? 1 : 0;
}
});
}
public String auth(String password) {
return execute(new JRedisClientCallback<String>() {
public String doInJRedis(JRedisClient jredisClient)
throws Exception {
throw new UnsupportedOperationException("JRedis does not implement AUTH command");
}
});
}
public Integer dbSize() {
return execute(new JRedisClientCallback<Integer>() {
public Integer doInJRedis(JRedisClient jredisClient)
throws Exception {
return convertToInteger(jredisClient.dbsize());
}
});
}
public String getSet(String key, String value) {
// TODO Auto-generated method stub
return null;
}
public List<String> mget(String... keys) {
// TODO Auto-generated method stub
return null;
}
public Integer setnx(String key, String value) {
// TODO Auto-generated method stub
return null;
}
public String setex(String key, int seconds, String value) {
// TODO Auto-generated method stub
return null;
}
public String mset(String... keysvalues) {
// TODO Auto-generated method stub
return null;
}
public Integer msetnx(String... keysvalues) {
// TODO Auto-generated method stub
return null;
}
public Integer incrBy(String key, int increment) {
// TODO Auto-generated method stub
return null;
}
public Integer incr(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer decr(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer decrBy(String key, int decrement) {
// TODO Auto-generated method stub
return null;
}
public Integer append(String key, String value) {
// TODO Auto-generated method stub
return null;
}
public String substr(String key, int start, int end) {
// TODO Auto-generated method stub
return null;
}
public Integer exists(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer del(String... keys) {
// TODO Auto-generated method stub
return null;
}
public String type(String key) {
// TODO Auto-generated method stub
return null;
}
public List<String> keys(String pattern) {
// TODO Auto-generated method stub
return null;
}
public String randomKey() {
// TODO Auto-generated method stub
return null;
}
public String rename(String oldkey, String newkey) {
// TODO Auto-generated method stub
return null;
}
public Integer renamenx(String oldkey, String newkey) {
// TODO Auto-generated method stub
return null;
}
public Integer expire(String key, int seconds) {
// TODO Auto-generated method stub
return null;
}
public Integer expireAt(String key, long unixTime) {
// TODO Auto-generated method stub
return null;
}
public Integer ttl(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer persist(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer sadd(String key, String member) {
// TODO Auto-generated method stub
return null;
}
public Set<String> smembers(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer srem(String key, String member) {
// TODO Auto-generated method stub
return null;
}
public String spop(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer smove(String srckey, String dstkey, String member) {
// TODO Auto-generated method stub
return null;
}
public Integer scard(String key) {
// TODO Auto-generated method stub
return null;
}
public Integer sismember(String key, String member) {
// TODO Auto-generated method stub
return null;
}
public Set<String> sinter(String... keys) {
// TODO Auto-generated method stub
return null;
}
public Integer sinterstore(String dstkey, String... keys) {
// TODO Auto-generated method stub
return null;
}
public Set<String> sunion(String... keys) {
// TODO Auto-generated method stub
return null;
}
public Integer sunionstore(String dstkey, String... keys) {
// TODO Auto-generated method stub
return null;
}
public Set<String> sdiff(String... keys) {
// TODO Auto-generated method stub
return null;
}
public Integer sdiffstore(String dstkey, String... keys) {
// TODO Auto-generated method stub
return null;
}
public String srandmember(String key) {
// TODO Auto-generated method stub
return null;
}
}