From 2a847020e1698f876351cbbc5b6bc88b52e2b4a7 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 8 Nov 2010 21:33:45 +0200 Subject: [PATCH] + add set commands and jedis implementation --- .../redis/connection/RedisCommands.java | 2 +- .../redis/connection/RedisSetCommands.java | 29 +++ .../connection/jedis/JedisConnection.java | 189 ++++++++++++++++++ .../redis/connection/jedis/JedisUtils.java | 4 + 4 files changed, 223 insertions(+), 1 deletion(-) diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java index f0f0e7502..fb4d98ca0 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java @@ -23,7 +23,7 @@ import java.util.Collection; * * @author Costin Leau */ -public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands { +public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands, RedisSetCommands { Boolean exists(String key); diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisSetCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisSetCommands.java index a59bfec91..302710d1e 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisSetCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisSetCommands.java @@ -16,6 +16,8 @@ package org.springframework.datastore.redis.connection; +import java.util.Set; + /** * Set-specific commands supported by Redis. * @@ -23,4 +25,31 @@ package org.springframework.datastore.redis.connection; */ public interface RedisSetCommands { + Boolean sAdd(String key, String value); + + Boolean sRem(String key, String value); + + String sPop(String key); + + Boolean sMove(String srcKey, String destKey, String value); + + Integer sCard(String key); + + Boolean sIsMember(String key, String value); + + Set sInter(String... keys); + + void sInterStore(String destKey, String... keys); + + Set sUnion(String... keys); + + void sUnionStore(String destKey, String... keys); + + Set sDiff(String... keys); + + void sDiffStore(String destKey, String... keys); + + Set sMembers(String key); + + String sRandMember(String key); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java index 9aea02464..0da23aeaf 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.Collection; import java.util.List; +import java.util.Set; import org.springframework.dao.DataAccessException; import org.springframework.datastore.keyvalue.UncategorizedKeyvalueStoreException; @@ -415,6 +416,10 @@ public class JedisConnection implements RedisConnection { } } + // + // List operations + // + @Override public Integer lPush(String key, String value) { @@ -580,4 +585,188 @@ public class JedisConnection implements RedisConnection { throw convertJedisAccessException(ex); } } + + + // + // Set operations + // + + @Override + public Boolean sAdd(String key, String value) { + try { + if (isQueueing()) { + transaction.sadd(key, value); + return null; + } + return (jedis.sadd(key, value) == 1); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Integer sCard(String key) { + try { + if (isQueueing()) { + transaction.scard(key); + return null; + } + return jedis.scard(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Set sDiff(String... keys) { + try { + if (isQueueing()) { + transaction.sdiff(keys); + return null; + } + return jedis.sdiff(keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void sDiffStore(String destKey, String... keys) { + try { + if (isQueueing()) { + transaction.sdiffstore(destKey, keys); + } + jedis.sdiffstore(destKey, keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Set sInter(String... keys) { + try { + if (isQueueing()) { + transaction.sinter(keys); + return null; + } + return jedis.sinter(keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void sInterStore(String destKey, String... keys) { + try { + if (isQueueing()) { + transaction.sinterstore(destKey, keys); + } + jedis.sinterstore(destKey, keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Boolean sIsMember(String key, String value) { + try { + if (isQueueing()) { + transaction.sismember(key, value); + return null; + } + return JedisUtils.convertCodeReply(jedis.sismember(key, value)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Set sMembers(String key) { + try { + if (isQueueing()) { + transaction.smembers(key); + return null; + } + return jedis.smembers(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Boolean sMove(String srcKey, String destKey, String value) { + try { + if (isQueueing()) { + transaction.smove(srcKey, destKey, value); + return null; + } + return JedisUtils.convertCodeReply(jedis.smove(srcKey, destKey, value)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String sPop(String key) { + try { + if (isQueueing()) { + transaction.spop(key); + return null; + } + return jedis.spop(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String sRandMember(String key) { + try { + if (isQueueing()) { + transaction.srandmember(key); + return null; + } + return jedis.srandmember(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Boolean sRem(String key, String value) { + try { + if (isQueueing()) { + transaction.srem(key, value); + return null; + } + return JedisUtils.convertCodeReply(jedis.srem(key, value)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Set sUnion(String... keys) { + try { + if (isQueueing()) { + transaction.sunion(keys); + return null; + } + return jedis.sunion(keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void sUnionStore(String destKey, String... keys) { + try { + if (isQueueing()) { + transaction.sunionstore(destKey, keys); + } + jedis.sunionstore(destKey, keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } } \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java index 054ceb6c4..d09769bfc 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisUtils.java @@ -63,4 +63,8 @@ public abstract class JedisUtils { static boolean isStatusOk(String status) { return status != null && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status)); } + + static Boolean convertCodeReply(Integer code) { + return (code != null ? code == 1 : null); + } }