From 1401769f2eefccabe88b97e2b11fc4295c8b5b61 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 12 Nov 2010 10:23:18 +0200 Subject: [PATCH] String -> byte[] refactoring + changed Commands interfaces + updated Jredis implementation --- .../redis/connection/DefaultEntry.java | 4 +- .../redis/connection/DefaultTuple.java | 6 +- .../redis/connection/RedisCommands.java | 20 +- .../redis/connection/RedisHashCommands.java | 28 +- .../redis/connection/RedisListCommands.java | 26 +- .../redis/connection/RedisSetCommands.java | 28 +- .../redis/connection/RedisStringCommands.java | 29 +- .../redis/connection/RedisTxCommands.java | 2 +- .../redis/connection/RedisZSetCommands.java | 46 +-- .../connection/jedis/JedisConnection.java | 172 ++++----- .../connection/jredis/JredisConnection.java | 353 +++++++++--------- .../jredis/JredisConnectionFactory.java | 21 +- .../redis/connection/jredis/JredisUtils.java | 59 ++- 13 files changed, 439 insertions(+), 355 deletions(-) diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java index db174291e..db44fbbf2 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultEntry.java @@ -33,12 +33,12 @@ public class DefaultEntry implements Entry { } @Override - public String getField() { + public byte[] getField() { return null; } @Override - public String getValue() { + public byte[] getValue() { return null; } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultTuple.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultTuple.java index 62807b8d2..11d8d68e4 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultTuple.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/DefaultTuple.java @@ -25,7 +25,7 @@ import org.springframework.datastore.redis.connection.RedisZSetCommands.Tuple; public class DefaultTuple implements Tuple { private final Double score; - private final String value; + private final byte[] value; /** @@ -34,7 +34,7 @@ public class DefaultTuple implements Tuple { * @param value * @param score */ - public DefaultTuple(String value, Double score) { + public DefaultTuple(byte[] value, Double score) { this.score = score; this.value = value; } @@ -45,7 +45,7 @@ public class DefaultTuple implements Tuple { } @Override - public String getValue() { + public byte[] getValue() { return value; } } 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 74d284309..c09dd12c5 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 @@ -26,27 +26,27 @@ import java.util.Collection; public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands, RedisSetCommands, RedisZSetCommands, RedisHashCommands { - Boolean exists(String key); + Boolean exists(byte[] key); - Integer del(String... keys); + Integer del(byte[]... keys); - DataType type(String key); + DataType type(byte[] key); - Collection keys(String pattern); + Collection keys(String pattern); - String randomKey(); + byte[] randomKey(); - void rename(String oldName, String newName); + void rename(byte[] oldName, byte[] newName); - Boolean renameNX(String oldName, String newName); + Boolean renameNX(byte[] oldName, byte[] newName); Integer dbSize(); - Boolean expire(String key, int seconds); + Boolean expire(byte[] key, int seconds); - Boolean persist(String key); + Boolean persist(byte[] key); - Integer ttl(String key); + Integer ttl(byte[] key); void select(int dbIndex); diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java index e51cf5ddd..862ded3d5 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisHashCommands.java @@ -27,32 +27,32 @@ import java.util.Set; public interface RedisHashCommands { public interface Entry { - public String getField(); + public byte[] getField(); - public String getValue(); + public byte[] getValue(); } - Boolean hSet(String key, String field, String value); + Boolean hSet(byte[] key, byte[] field, byte[] value); - Boolean hSetNX(String key, String field, String value); + Boolean hSetNX(byte[] key, byte[] field, byte[] value); - String hGet(String key, String field); + byte[] hGet(byte[] key, byte[] field); - List hMGet(String key, String... fields); + List hMGet(byte[] key, byte[]... fields); - void hMSet(String key, String[] fields, String[] values); + void hMSet(byte[] key, byte[][] fields, byte[][] values); - Integer hIncrBy(String key, String field, int delta); + Integer hIncrBy(byte[] key, byte[] field, int delta); - Boolean hExists(String key, String field); + Boolean hExists(byte[] key, byte[] field); - Boolean hDel(String key, String field); + Boolean hDel(byte[] key, byte[] field); - Integer hLen(String key); + Integer hLen(byte[] key); - Set hKeys(String key); + Set hKeys(byte[] key); - List hVals(String key); + List hVals(byte[] key); - Set hGetAll(String key); + Set hGetAll(byte[] key); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java index bb2730005..0fdc343bf 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java @@ -25,29 +25,29 @@ import java.util.List; */ public interface RedisListCommands { - Integer rPush(String key, String value); + Integer rPush(byte[] key, byte[] value); - Integer lPush(String key, String value); + Integer lPush(byte[] key, byte[] value); - Integer lLen(String key); + Integer lLen(byte[] key); - List lRange(String key, int start, int end); + List lRange(byte[] key, int start, int end); - void lTrim(String key, int start, int end); + void lTrim(byte[] key, int start, int end); - String lIndex(String key, int index); + byte[] lIndex(byte[] key, int index); - void lSet(String key, int index, String value); + void lSet(byte[] key, int index, byte[] value); - Integer lRem(String key, int count, String value); + Integer lRem(byte[] key, int count, byte[] value); - String lPop(String key); + byte[] lPop(byte[] key); - String rPop(String key); + byte[] rPop(byte[] key); - List bLPop(int timeout, String... keys); + List bLPop(int timeout, byte[]... keys); - List bRPop(int timeout, String... keys); + List bRPop(int timeout, byte[]... keys); - String rPopLPush(String srcKey, String dstKey); + byte[] rPopLPush(byte[] srcKey, byte[] dstKey); } 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 302710d1e..244d0f3d8 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 @@ -25,31 +25,31 @@ import java.util.Set; */ public interface RedisSetCommands { - Boolean sAdd(String key, String value); + Boolean sAdd(byte[] key, byte[] value); - Boolean sRem(String key, String value); + Boolean sRem(byte[] key, byte[] value); - String sPop(String key); + byte[] sPop(byte[] key); - Boolean sMove(String srcKey, String destKey, String value); + Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value); - Integer sCard(String key); + Integer sCard(byte[] key); - Boolean sIsMember(String key, String value); + Boolean sIsMember(byte[] key, byte[] value); - Set sInter(String... keys); + Set sInter(byte[]... keys); - void sInterStore(String destKey, String... keys); + void sInterStore(byte[] destKey, byte[]... keys); - Set sUnion(String... keys); + Set sUnion(byte[]... keys); - void sUnionStore(String destKey, String... keys); + void sUnionStore(byte[] destKey, byte[]... keys); - Set sDiff(String... keys); + Set sDiff(byte[]... keys); - void sDiffStore(String destKey, String... keys); + void sDiffStore(byte[] destKey, byte[]... keys); - Set sMembers(String key); + Set sMembers(byte[] key); - String sRandMember(String key); + byte[] sRandMember(byte[] key); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisStringCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisStringCommands.java index 2f163d3e5..b5c56f1a2 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisStringCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisStringCommands.java @@ -17,6 +17,7 @@ package org.springframework.datastore.redis.connection; import java.util.List; +import java.util.Map; /** * String specific commands supported by Redis. @@ -25,31 +26,31 @@ import java.util.List; */ public interface RedisStringCommands { - void set(String key, String value); + void set(byte[] key, byte[] value); - String get(String key); + byte[] get(byte[] key); - String getSet(String key, String value); + byte[] getSet(byte[] key, byte[] value); - List mGet(String... keys); + List mGet(byte[]... keys); - Boolean setNX(String key, String value); + Boolean setNX(byte[] key, byte[] value); - void setEx(String key, int seconds, String value); + void setEx(byte[] key, int seconds, byte[] value); - void mSet(String[] keys, String[] values); + void mSet(Map tuple); - void mSetNX(String[] keys, String[] values); + void mSetNX(Map tuple); - Integer incr(String key); + Integer incr(byte[] key); - Integer incrBy(String key, int value); + Integer incrBy(byte[] key, int value); - Integer decr(String key); + Integer decr(byte[] key); - Integer decrBy(String key, int value); + Integer decrBy(byte[] key, int value); - Integer append(String key, String value); + Integer append(byte[] key, byte[] value); - String substr(String key, int start, int end); + byte[] substr(byte[] key, int start, int end); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisTxCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisTxCommands.java index 23ee193ae..db2592817 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisTxCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisTxCommands.java @@ -31,7 +31,7 @@ public interface RedisTxCommands { void discard(); - void watch(String... keys); + void watch(byte[]... keys); void unwatch(); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisZSetCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisZSetCommands.java index 1024c3169..93a11200d 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisZSetCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisZSetCommands.java @@ -31,52 +31,52 @@ public interface RedisZSetCommands { } public interface Tuple { - String getValue(); + byte[] getValue(); Double getScore(); } - Boolean zAdd(String key, double score, String value); + Boolean zAdd(byte[] key, double score, byte[] value); - Boolean zRem(String key, String value); + Boolean zRem(byte[] key, byte[] value); - Double zIncrBy(String key, double increment, String value); + Double zIncrBy(byte[] key, double increment, byte[] value); - Integer zRank(String key, String value); + Integer zRank(byte[] key, byte[] value); - Integer zRevRank(String key, String value); + Integer zRevRank(byte[] key, byte[] value); - Set zRange(String key, int start, int end); + Set zRange(byte[] key, int start, int end); - Set zRangeWithScore(String key, int start, int end); + Set zRangeWithScore(byte[] key, int start, int end); - Set zRevRange(String key, int start, int end); + Set zRevRange(byte[] key, int start, int end); - Set zRevRangeWithScore(String key, int start, int end); + Set zRevRangeWithScore(byte[] key, int start, int end); - Set zRangeByScore(String key, double min, double max); + Set zRangeByScore(byte[] key, double min, double max); - Set zRangeByScoreWithScore(String key, double min, double max); + Set zRangeByScoreWithScore(byte[] key, double min, double max); - Set zRangeByScore(String key, double min, double max, int offset, int count); + Set zRangeByScore(byte[] key, double min, double max, int offset, int count); - Set zRangeByScoreWithScore(String key, double min, double max, int offset, int count); + Set zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count); - Integer zCount(String key, double min, double max); + Integer zCount(byte[] key, double min, double max); - Integer zCard(String key); + Integer zCard(byte[] key); - Double zScore(String key, String value); + Double zScore(byte[] key, byte[] value); - Integer zRemRange(String key, int start, int end); + Integer zRemRange(byte[] key, int start, int end); - Integer zRemRangeByScore(String key, double min, double max); + Integer zRemRangeByScore(byte[] key, double min, double max); - Integer zUnionStore(String destKey, String... sets); + Integer zUnionStore(byte[] destKey, byte[]... sets); - Integer zUnionStore(String destKey, Aggregate aggregate, int[] weights, String... sets); + Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets); - Integer zInterStore(String destKey, String... sets); + Integer zInterStore(byte[] destKey, byte[]... sets); - Integer zInterStore(String destKey, Aggregate aggregate, int[] weights, String... sets); + Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets); } \ No newline at end of file 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 b398c8b63..42f5cc523 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 @@ -119,7 +119,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer del(String... keys) { + public Integer del(byte[]... keys) { try { if (isQueueing()) { transaction.del(keys); @@ -150,7 +150,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean exists(String key) { + public Boolean exists(byte[] key) { try { if (isQueueing()) { transaction.exists(key); @@ -163,7 +163,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean expire(String key, int seconds) { + public Boolean expire(byte[] key, int seconds) { try { if (isQueueing()) { transaction.expire(key, seconds); @@ -176,7 +176,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Collection keys(String pattern) { + public Collection keys(String pattern) { try { if (isQueueing()) { transaction.keys(pattern); @@ -198,7 +198,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean persist(String key) { + public Boolean persist(byte[] key) { try { if (isQueueing()) { client.persist(key); @@ -211,7 +211,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String randomKey() { + public byte[] randomKey() { try { if (isQueueing()) { transaction.randomKey(); @@ -224,7 +224,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void rename(String oldName, String newName) { + public void rename(byte[] oldName, byte[] newName) { try { if (isQueueing()) { transaction.rename(oldName, newName); @@ -236,7 +236,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean renameNX(String oldName, String newName) { + public Boolean renameNX(byte[] oldName, byte[] newName) { try { if (isQueueing()) { transaction.renamenx(oldName, newName); @@ -261,7 +261,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer ttl(String key) { + public Integer ttl(byte[] key) { try { if (isQueueing()) { transaction.ttl(key); @@ -274,7 +274,7 @@ public class JedisConnection implements RedisConnection { } @Override - public DataType type(String key) { + public DataType type(byte[] key) { try { if (isQueueing()) { transaction.type(key); @@ -296,7 +296,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void watch(String... keys) { + public void watch(byte[]... keys) { if (isQueueing()) { // ignore (as watch not allowed in multi) return; @@ -316,7 +316,7 @@ public class JedisConnection implements RedisConnection { // @Override - public String get(String key) { + public byte[] get(byte[] key) { try { if (isQueueing()) { transaction.get(key); @@ -330,7 +330,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void set(String key, String value) { + public void set(byte[] key, byte[] value) { try { jedis.set(key, value); } catch (Exception ex) { @@ -340,7 +340,7 @@ public class JedisConnection implements RedisConnection { @Override - public String getSet(String key, String value) { + public byte[] getSet(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.getSet(key, value); @@ -353,7 +353,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer append(String key, String value) { + public Integer append(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.append(key, value); @@ -366,7 +366,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List mGet(String... keys) { + public List mGet(byte[]... keys) { try { if (isQueueing()) { transaction.mget(keys); @@ -379,7 +379,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void mSet(String[] keys, String[] values) { + public void mSet(byte[][] keys, byte[][] values) { try { if (isQueueing()) { transaction.mset(JedisUtils.arrange(keys, values)); @@ -391,7 +391,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void mSetNX(String[] keys, String[] values) { + public void mSetNX(byte[][] keys, byte[][] values) { try { if (isQueueing()) { transaction.msetnx(JedisUtils.arrange(keys, values)); @@ -403,7 +403,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void setEx(String key, int time, String value) { + public void setEx(byte[] key, int time, byte[] value) { try { if (isQueueing()) { transaction.setex(key, time, value); @@ -415,7 +415,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean setNX(String key, String value) { + public Boolean setNX(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.setnx(key, value); @@ -427,7 +427,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String substr(String key, int start, int end) { + public byte[] substr(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.substr(key, start, end); @@ -440,7 +440,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer decr(String key) { + public Integer decr(byte[] key) { try { if (isQueueing()) { transaction.decr(key); @@ -453,7 +453,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer decrBy(String key, int value) { + public Integer decrBy(byte[] key, int value) { try { if (isQueueing()) { transaction.decrBy(key, value); @@ -466,7 +466,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer incr(String key) { + public Integer incr(byte[] key) { try { if (isQueueing()) { transaction.incr(key); @@ -479,7 +479,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer incrBy(String key, int value) { + public Integer incrBy(byte[] key, int value) { try { if (isQueueing()) { transaction.incrBy(key, value); @@ -497,7 +497,7 @@ public class JedisConnection implements RedisConnection { @Override - public Integer lPush(String key, String value) { + public Integer lPush(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.lpush(key, value); @@ -510,7 +510,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer rPush(String key, String value) { + public Integer rPush(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.rpush(key, value); @@ -523,7 +523,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List bLPop(int timeout, String... keys) { + public List bLPop(int timeout, byte[]... keys) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -535,7 +535,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List bRPop(int timeout, String... keys) { + public List bRPop(int timeout, byte[]... keys) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -547,7 +547,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String lIndex(String key, int index) { + public byte[] lIndex(byte[] key, int index) { try { if (isQueueing()) { transaction.lindex(key, index); @@ -560,7 +560,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer lLen(String key) { + public Integer lLen(byte[] key) { try { if (isQueueing()) { transaction.llen(key); @@ -573,7 +573,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String lPop(String key) { + public byte[] lPop(byte[] key) { try { if (isQueueing()) { transaction.lpop(key); @@ -586,7 +586,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List lRange(String key, int start, int end) { + public List lRange(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.lrange(key, start, end); @@ -599,7 +599,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer lRem(String key, int count, String value) { + public Integer lRem(byte[] key, int count, byte[] value) { try { if (isQueueing()) { transaction.lrem(key, count, value); @@ -612,7 +612,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void lSet(String key, int index, String value) { + public void lSet(byte[] key, int index, byte[] value) { try { if (isQueueing()) { transaction.lset(key, index, value); @@ -624,7 +624,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void lTrim(String key, int start, int end) { + public void lTrim(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.ltrim(key, start, end); @@ -636,7 +636,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String rPop(String key) { + public byte[] rPop(byte[] key) { try { if (isQueueing()) { transaction.rpop(key); @@ -649,7 +649,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String rPopLPush(String srcKey, String dstKey) { + public byte[] rPopLPush(byte[] srcKey, byte[] dstKey) { try { if (isQueueing()) { transaction.rpoplpush(srcKey, dstKey); @@ -667,7 +667,7 @@ public class JedisConnection implements RedisConnection { // @Override - public Boolean sAdd(String key, String value) { + public Boolean sAdd(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.sadd(key, value); @@ -680,7 +680,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer sCard(String key) { + public Integer sCard(byte[] key) { try { if (isQueueing()) { transaction.scard(key); @@ -693,7 +693,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set sDiff(String... keys) { + public Set sDiff(byte[]... keys) { try { if (isQueueing()) { transaction.sdiff(keys); @@ -706,7 +706,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void sDiffStore(String destKey, String... keys) { + public void sDiffStore(byte[] destKey, byte[]... keys) { try { if (isQueueing()) { transaction.sdiffstore(destKey, keys); @@ -718,7 +718,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set sInter(String... keys) { + public Set sInter(byte[]... keys) { try { if (isQueueing()) { transaction.sinter(keys); @@ -731,7 +731,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void sInterStore(String destKey, String... keys) { + public void sInterStore(byte[] destKey, byte[]... keys) { try { if (isQueueing()) { transaction.sinterstore(destKey, keys); @@ -743,7 +743,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean sIsMember(String key, String value) { + public Boolean sIsMember(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.sismember(key, value); @@ -756,7 +756,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set sMembers(String key) { + public Set sMembers(byte[] key) { try { if (isQueueing()) { transaction.smembers(key); @@ -769,7 +769,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean sMove(String srcKey, String destKey, String value) { + public Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value) { try { if (isQueueing()) { transaction.smove(srcKey, destKey, value); @@ -782,7 +782,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String sPop(String key) { + public byte[] sPop(byte[] key) { try { if (isQueueing()) { transaction.spop(key); @@ -795,7 +795,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String sRandMember(String key) { + public byte[] sRandMember(byte[] key) { try { if (isQueueing()) { transaction.srandmember(key); @@ -808,7 +808,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean sRem(String key, String value) { + public Boolean sRem(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.srem(key, value); @@ -821,7 +821,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set sUnion(String... keys) { + public Set sUnion(byte[]... keys) { try { if (isQueueing()) { transaction.sunion(keys); @@ -834,7 +834,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void sUnionStore(String destKey, String... keys) { + public void sUnionStore(byte[] destKey, byte[]... keys) { try { if (isQueueing()) { transaction.sunionstore(destKey, keys); @@ -850,7 +850,7 @@ public class JedisConnection implements RedisConnection { // @Override - public Boolean zAdd(String key, double score, String value) { + public Boolean zAdd(byte[] key, double score, byte[] value) { try { if (isQueueing()) { transaction.zadd(key, score, value); @@ -863,7 +863,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zCard(String key) { + public Integer zCard(byte[] key) { try { if (isQueueing()) { transaction.zcard(key); @@ -876,7 +876,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zCount(String key, double min, double max) { + public Integer zCount(byte[] key, double min, double max) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -888,7 +888,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Double zIncrBy(String key, double increment, String value) { + public Double zIncrBy(byte[] key, double increment, byte[] value) { try { if (isQueueing()) { transaction.zincrby(key, increment, value); @@ -901,7 +901,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zInterStore(String destKey, Aggregate aggregate, int[] weights, String... sets) { + public Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -915,7 +915,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zInterStore(String destKey, String... sets) { + public Integer zInterStore(byte[] destKey, byte[]... sets) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -927,7 +927,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRange(String key, int start, int end) { + public Set zRange(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.zrange(key, start, end); @@ -940,7 +940,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRangeWithScore(String key, int start, int end) { + public Set zRangeWithScore(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.zrangeWithScores(key, start, end); @@ -953,7 +953,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRangeByScore(String key, double min, double max) { + public Set zRangeByScore(byte[] key, double min, double max) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -965,7 +965,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRangeByScoreWithScore(String key, double min, double max) { + public Set zRangeByScoreWithScore(byte[] key, double min, double max) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -977,7 +977,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRevRangeWithScore(String key, int start, int end) { + public Set zRevRangeWithScore(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.zrangeWithScores(key, start, end); @@ -990,7 +990,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRangeByScore(String key, double min, double max, int offset, int count) { + public Set zRangeByScore(byte[] key, double min, double max, int offset, int count) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1002,7 +1002,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRangeByScoreWithScore(String key, double min, double max, int offset, int count) { + public Set zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1014,7 +1014,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zRank(String key, String value) { + public Integer zRank(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.zrank(key, value); @@ -1027,7 +1027,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean zRem(String key, String value) { + public Boolean zRem(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.zrem(key, value); @@ -1040,7 +1040,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zRemRange(String key, int start, int end) { + public Integer zRemRange(byte[] key, int start, int end) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1052,7 +1052,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zRemRangeByScore(String key, double min, double max) { + public Integer zRemRangeByScore(byte[] key, double min, double max) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1064,7 +1064,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set zRevRange(String key, int start, int end) { + public Set zRevRange(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.zrevrange(key, start, end); @@ -1077,7 +1077,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zRevRank(String key, String value) { + public Integer zRevRank(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.zrevrank(key, value); @@ -1090,7 +1090,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Double zScore(String key, String value) { + public Double zScore(byte[] key, byte[] value) { try { if (isQueueing()) { transaction.zscore(key, value); @@ -1103,7 +1103,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zUnionStore(String destKey, Aggregate aggregate, int[] weights, String... sets) { + public Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1117,7 +1117,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer zUnionStore(String destKey, String... sets) { + public Integer zUnionStore(byte[] destKey, byte[]... sets) { try { if (isQueueing()) { throw new UnsupportedOperationException(); @@ -1133,7 +1133,7 @@ public class JedisConnection implements RedisConnection { // @Override - public Boolean hSet(String key, String field, String value) { + public Boolean hSet(byte[] key, byte[] field, byte[] value) { try { if (isQueueing()) { transaction.hset(key, field, value); @@ -1146,7 +1146,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean hSetNX(String key, String field, String value) { + public Boolean hSetNX(byte[] key, byte[] field, byte[] value) { try { if (isQueueing()) { transaction.hsetnx(key, field, value); @@ -1159,7 +1159,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean hDel(String key, String field) { + public Boolean hDel(byte[] key, byte[] field) { try { if (isQueueing()) { transaction.hdel(key, field); @@ -1172,7 +1172,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Boolean hExists(String key, String field) { + public Boolean hExists(byte[] key, byte[] field) { try { if (isQueueing()) { transaction.hexists(key, field); @@ -1185,7 +1185,7 @@ public class JedisConnection implements RedisConnection { } @Override - public String hGet(String key, String field) { + public byte[] hGet(byte[] key, byte[] field) { try { if (isQueueing()) { transaction.hget(key, field); @@ -1198,7 +1198,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set hGetAll(String key) { + public Set hGetAll(byte[] key) { try { if (isQueueing()) { transaction.hgetAll(key); @@ -1211,7 +1211,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer hIncrBy(String key, String field, int delta) { + public Integer hIncrBy(byte[] key, byte[] field, int delta) { try { if (isQueueing()) { transaction.hincrBy(key, field, delta); @@ -1224,7 +1224,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Set hKeys(String key) { + public Set hKeys(byte[] key) { try { if (isQueueing()) { transaction.hkeys(key); @@ -1237,7 +1237,7 @@ public class JedisConnection implements RedisConnection { } @Override - public Integer hLen(String key) { + public Integer hLen(byte[] key) { try { if (isQueueing()) { transaction.hlen(key); @@ -1250,7 +1250,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List hMGet(String key, String... fields) { + public List hMGet(byte[] key, byte[]... fields) { try { if (isQueueing()) { transaction.hmget(key, fields); @@ -1263,7 +1263,7 @@ public class JedisConnection implements RedisConnection { } @Override - public void hMSet(String key, String[] fields, String[] values) { + public void hMSet(byte[] key, byte[][] fields, byte[][] values) { Map param = JedisUtils.convert(fields, values); try { if (isQueueing()) { @@ -1276,7 +1276,7 @@ public class JedisConnection implements RedisConnection { } @Override - public List hVals(String key) { + public List hVals(byte[] key) { try { if (isQueueing()) { transaction.hvals(key); diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java index 74539f385..98782ba85 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.jredis.JRedis; @@ -30,14 +31,19 @@ import org.springframework.datastore.redis.connection.DataType; import org.springframework.datastore.redis.connection.RedisConnection; /** + * JRedis based implementation. + * * @author Costin Leau */ public class JredisConnection implements RedisConnection { private final JRedis jredis; - public JredisConnection(JRedis jredis) { + private final String charset; + + public JredisConnection(JRedis jredis, String charset) { this.jredis = jredis; + this.charset = charset; } protected DataAccessException convertJedisAccessException(Exception ex) { @@ -78,9 +84,9 @@ public class JredisConnection implements RedisConnection { } @Override - public Integer del(String... keys) { + public Integer del(byte[]... keys) { try { - return Integer.valueOf((int) jredis.del(keys)); + return Integer.valueOf((int) jredis.del(JredisUtils.convertMultiple(charset, keys))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -101,27 +107,27 @@ public class JredisConnection implements RedisConnection { } @Override - public Boolean exists(String key) { + public Boolean exists(byte[] key) { try { - return jredis.exists(key); + return jredis.exists(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean expire(String key, int seconds) { + public Boolean expire(byte[] key, int seconds) { try { - return jredis.expire(key, seconds); + return jredis.expire(JredisUtils.convert(charset, key), seconds); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Collection keys(String pattern) { + public Collection keys(String pattern) { try { - return jredis.keys(pattern); + return JredisUtils.convert(charset, jredis.keys(pattern)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -133,32 +139,32 @@ public class JredisConnection implements RedisConnection { } @Override - public Boolean persist(String key) { + public Boolean persist(byte[] key) { throw new UnsupportedOperationException(); } @Override - public String randomKey() { + public byte[] randomKey() { try { - return jredis.randomkey(); + return JredisUtils.convert(charset, jredis.randomkey()); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void rename(String oldName, String newName) { + public void rename(byte[] oldName, byte[] newName) { try { - jredis.rename(oldName, newName); + jredis.rename(JredisUtils.convert(charset, oldName), JredisUtils.convert(charset, newName)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean renameNX(String oldName, String newName) { + public Boolean renameNX(byte[] oldName, byte[] newName) { try { - return jredis.renamenx(oldName, newName); + return jredis.renamenx(JredisUtils.convert(charset, oldName), JredisUtils.convert(charset, newName)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -170,18 +176,18 @@ public class JredisConnection implements RedisConnection { } @Override - public Integer ttl(String key) { + public Integer ttl(byte[] key) { try { - return Integer.valueOf((int) jredis.ttl(key)); + return Integer.valueOf((int) jredis.ttl(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public DataType type(String key) { + public DataType type(byte[] key) { try { - return JredisUtils.convertDataType(jredis.type(key)); + return JredisUtils.convertDataType(jredis.type(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -193,7 +199,7 @@ public class JredisConnection implements RedisConnection { } @Override - public void watch(String... keys) { + public void watch(byte[]... keys) { throw new UnsupportedOperationException(); } @@ -202,123 +208,122 @@ public class JredisConnection implements RedisConnection { // @Override - public String get(String key) { + public byte[] get(byte[] key) { try { - return JredisUtils.convertToString(jredis.get(key)); + return jredis.get(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void set(String key, String value) { + public void set(byte[] key, byte[] value) { try { - jredis.set(key, value); + jredis.set(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String getSet(String key, String value) { + public byte[] getSet(byte[] key, byte[] value) { try { - return JredisUtils.convertToString(jredis.getset(key, value)); - } catch (RedisException ex) { - throw JredisUtils.convertJredisAccessException(ex); - } - } - - - @Override - public Integer append(String key, String value) { - try { - return Integer.valueOf((int) jredis.append(key, value)); + return jredis.getset(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public List mGet(String... keys) { + public Integer append(byte[] key, byte[] value) { try { - return JredisUtils.convertToStringCollection(jredis.mget(keys), List.class); + return Integer.valueOf((int) jredis.append(JredisUtils.convert(charset, key), value)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void mSet(String[] keys, String[] values) { + public List mGet(byte[]... keys) { try { - jredis.mset(JredisUtils.convert(keys, values)); + return jredis.mget(JredisUtils.convertMultiple(charset, keys)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void mSetNX(String[] keys, String[] values) { + public void mSet(Map tuple) { try { - jredis.msetnx(JredisUtils.convert(keys, values)); + jredis.mset(JredisUtils.convert(charset, tuple)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void setEx(String key, int seconds, String value) { + public void mSetNX(Map tuple) { + try { + jredis.msetnx(JredisUtils.convert(charset, tuple)); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public void setEx(byte[] key, int seconds, byte[] value) { throw new UnsupportedOperationException(); } @Override - public Boolean setNX(String key, String value) { + public Boolean setNX(byte[] key, byte[] value) { try { - return jredis.setnx(key, value); + return jredis.setnx(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String substr(String key, int start, int end) { + public byte[] substr(byte[] key, int start, int end) { try { - return JredisUtils.convertToString(jredis.substr(key, (long) start, (long) end)); + return jredis.substr(JredisUtils.convert(charset, key), (long) start, (long) end); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer decr(String key) { + public Integer decr(byte[] key) { try { - return (int) jredis.decr(key); + return (int) jredis.decr(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer decrBy(String key, int value) { + public Integer decrBy(byte[] key, int value) { try { - return (int) jredis.decrby(key, value); + return (int) jredis.decrby(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer incr(String key) { + public Integer incr(byte[] key) { try { - return (int) jredis.incr(key); + return (int) jredis.incr(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer incrBy(String key, int value) { + public Integer incrBy(byte[] key, int value) { try { - return (int) jredis.incrby(key, value); + return (int) jredis.incrby(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -329,46 +334,46 @@ public class JredisConnection implements RedisConnection { // @Override - public List bLPop(int timeout, String... keys) { + public List bLPop(int timeout, byte[]... keys) { throw new UnsupportedOperationException(); } @Override - public List bRPop(int timeout, String... keys) { + public List bRPop(int timeout, byte[]... keys) { throw new UnsupportedOperationException(); } @Override - public String lIndex(String key, int index) { + public byte[] lIndex(byte[] key, int index) { try { - return JredisUtils.convertToString(jredis.lindex(key, (long) index)); + return jredis.lindex(JredisUtils.convert(charset, key), (long) index); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer lLen(String key) { + public Integer lLen(byte[] key) { try { - return Integer.valueOf((int) jredis.llen(key)); + return Integer.valueOf((int) jredis.llen(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String lPop(String key) { + public byte[] lPop(byte[] key) { try { - return JredisUtils.convertToString(jredis.lpop(key)); + return jredis.lpop(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer lPush(String key, String value) { + public Integer lPush(byte[] key, byte[] value) { try { - jredis.lpush(key, value); + jredis.lpush(JredisUtils.convert(charset, key), value); return null; } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); @@ -376,66 +381,65 @@ public class JredisConnection implements RedisConnection { } @Override - public List lRange(String key, int start, int end) { + public List lRange(byte[] key, int start, int end) { try { - List lrange = jredis.lrange(key, start, end); + List lrange = jredis.lrange(JredisUtils.convert(charset, key), start, end); - return JredisUtils.convertToStringCollection(lrange, List.class); + return lrange; } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer lRem(String key, int count, String value) { + public Integer lRem(byte[] key, int count, byte[] value) { try { - Integer.valueOf((int) jredis.lrem(key, value, count)); - return null; + return Integer.valueOf((int) jredis.lrem(JredisUtils.convert(charset, key), value, count)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void lSet(String key, int index, String value) { + public void lSet(byte[] key, int index, byte[] value) { try { - jredis.lset(key, index, value); + jredis.lset(JredisUtils.convert(charset, key), index, value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void lTrim(String key, int start, int end) { + public void lTrim(byte[] key, int start, int end) { try { - jredis.ltrim(key, start, end); + jredis.ltrim(JredisUtils.convert(charset, key), start, end); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String rPop(String key) { + public byte[] rPop(byte[] key) { try { - return JredisUtils.convertToString(jredis.rpop(key)); + return jredis.rpop(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String rPopLPush(String srcKey, String dstKey) { + public byte[] rPopLPush(byte[] srcKey, byte[] dstKey) { try { - return JredisUtils.convertToString(jredis.rpoplpush(srcKey, dstKey)); + return jredis.rpoplpush(JredisUtils.convert(charset, srcKey), JredisUtils.convert(charset, dstKey)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer rPush(String key, String value) { + public Integer rPush(byte[] key, byte[] value) { try { - jredis.rpush(key, value); + jredis.rpush(JredisUtils.convert(charset, key), value); return null; } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); @@ -447,27 +451,27 @@ public class JredisConnection implements RedisConnection { // @Override - public Boolean sAdd(String key, String value) { + public Boolean sAdd(byte[] key, byte[] value) { try { - return jredis.sadd(key, value); + return jredis.sadd(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer sCard(String key) { + public Integer sCard(byte[] key) { try { - return Integer.valueOf((int) jredis.scard(key)); + return Integer.valueOf((int) jredis.scard(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set sDiff(String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public Set sDiff(byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { List result = jredis.sdiff(set1, sets); @@ -478,9 +482,9 @@ public class JredisConnection implements RedisConnection { } @Override - public void sDiffStore(String destKey, String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public void sDiffStore(byte[] destKey, byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { jredis.sdiffstore(set1, sets); @@ -490,9 +494,9 @@ public class JredisConnection implements RedisConnection { } @Override - public Set sInter(String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public Set sInter(byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { List result = jredis.sinter(set1, sets); @@ -503,9 +507,9 @@ public class JredisConnection implements RedisConnection { } @Override - public void sInterStore(String destKey, String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public void sInterStore(byte[] destKey, byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { jredis.sinterstore(set1, sets); @@ -515,76 +519,75 @@ public class JredisConnection implements RedisConnection { } @Override - public Boolean sIsMember(String key, String value) { + public Boolean sIsMember(byte[] key, byte[] value) { try { - return jredis.sismember(key, value); + return jredis.sismember(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set sMembers(String key) { + public Set sMembers(byte[] key) { try { - return JredisUtils.convertToStringCollection(jredis.smembers(key), Set.class); + return new LinkedHashSet(jredis.smembers(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean sMove(String srcKey, String destKey, String value) { + public Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value) { try { - return jredis.smove(srcKey, destKey, value); + return jredis.smove(JredisUtils.convert(charset, srcKey), JredisUtils.convert(charset, destKey), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String sPop(String key) { + public byte[] sPop(byte[] key) { try { - return JredisUtils.convertToString(jredis.spop(key)); + return jredis.spop(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String sRandMember(String key) { + public byte[] sRandMember(byte[] key) { try { - return JredisUtils.convertToString(jredis.srandmember(key)); + return jredis.srandmember(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean sRem(String key, String value) { + public Boolean sRem(byte[] key, byte[] value) { try { - return jredis.srem(key, value); + return jredis.srem(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set sUnion(String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public Set sUnion(byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { - List result = jredis.sunion(set1, sets); - return JredisUtils.convertToStringCollection(result, Set.class); + return new LinkedHashSet(jredis.sunion(set1, sets)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public void sUnionStore(String destKey, String... keys) { - String set1 = keys[0]; - String[] sets = Arrays.copyOfRange(keys, 1, keys.length); + public void sUnionStore(byte[] destKey, byte[]... keys) { + String set1 = JredisUtils.convert(charset, keys[0]); + String[] sets = JredisUtils.convertMultiple(charset, Arrays.copyOfRange(keys, 1, keys.length)); try { jredis.sunionstore(set1, sets); @@ -599,153 +602,156 @@ public class JredisConnection implements RedisConnection { // @Override - public Boolean zAdd(String key, double score, String value) { + public Boolean zAdd(byte[] key, double score, byte[] value) { try { - return jredis.zadd(key, score, value); + return jredis.zadd(JredisUtils.convert(charset, key), score, value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer zCard(String key) { + public Integer zCard(byte[] key) { try { - return Integer.valueOf((int) jredis.zcard(key)); + return Integer.valueOf((int) jredis.zcard(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer zCount(String key, double min, double max) { + public Integer zCount(byte[] key, double min, double max) { try { - return Integer.valueOf((int) jredis.zcount(key, min, max)); + return Integer.valueOf((int) jredis.zcount(JredisUtils.convert(charset, key), min, max)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Double zIncrBy(String key, double increment, String value) { + public Double zIncrBy(byte[] key, double increment, byte[] value) { try { - return jredis.zincrby(key, increment, value); + return jredis.zincrby(JredisUtils.convert(charset, key), increment, value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer zInterStore(String destKey, Aggregate aggregate, int[] weights, String... sets) { + public Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { throw new UnsupportedOperationException(); } @Override - public Integer zInterStore(String destKey, String... sets) { + public Integer zInterStore(byte[] destKey, byte[]... sets) { throw new UnsupportedOperationException(); } @Override - public Set zRange(String key, int start, int end) { + public Set zRange(byte[] key, int start, int end) { try { - return JredisUtils.convertToStringCollection(jredis.zrange(key, (long) start, (long) end), Set.class); + return JredisUtils.convertToStringCollection(jredis.zrange(JredisUtils.convert(charset, key), (long) start, + (long) end), Set.class); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set zRangeWithScore(String key, int start, int end) { + public Set zRangeWithScore(byte[] key, int start, int end) { throw new UnsupportedOperationException(); } @Override - public Set zRangeByScore(String key, double min, double max) { + public Set zRangeByScore(byte[] key, double min, double max) { try { - return JredisUtils.convertToStringCollection(jredis.zrangebyscore(key, min, max), Set.class); + return JredisUtils.convertToStringCollection(jredis.zrangebyscore(JredisUtils.convert(charset, key), min, + max), Set.class); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set zRangeByScoreWithScore(String key, double min, double max) { + public Set zRangeByScoreWithScore(byte[] key, double min, double max) { throw new UnsupportedOperationException(); } @Override - public Set zRangeByScore(String key, double min, double max, int offset, int count) { + public Set zRangeByScore(byte[] key, double min, double max, int offset, int count) { throw new UnsupportedOperationException(); } @Override - public Set zRangeByScoreWithScore(String key, double min, double max, int offset, int count) { + public Set zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count) { throw new UnsupportedOperationException(); } @Override - public Integer zRank(String key, String value) { + public Integer zRank(byte[] key, byte[] value) { try { - return Integer.valueOf((int) jredis.zrank(key, value)); + return Integer.valueOf((int) jredis.zrank(JredisUtils.convert(charset, key), value)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean zRem(String key, String value) { + public Boolean zRem(byte[] key, byte[] value) { try { - return jredis.zrem(key, value); + return jredis.zrem(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer zRemRange(String key, int start, int end) { + public Integer zRemRange(byte[] key, int start, int end) { try { - return Integer.valueOf((int) jredis.zremrangebyrank(key, start, end)); + return Integer.valueOf((int) jredis.zremrangebyrank(JredisUtils.convert(charset, key), start, end)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer zRemRangeByScore(String key, double min, double max) { + public Integer zRemRangeByScore(byte[] key, double min, double max) { try { - return Integer.valueOf((int) jredis.zremrangebyscore(key, min, max)); + return Integer.valueOf((int) jredis.zremrangebyscore(JredisUtils.convert(charset, key), min, max)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set zRevRange(String key, int start, int end) { + public Set zRevRange(byte[] key, int start, int end) { try { - return JredisUtils.convertToStringCollection(jredis.zrevrange(key, start, end), Set.class); + return JredisUtils.convertToStringCollection( + jredis.zrevrange(JredisUtils.convert(charset, key), start, end), Set.class); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set zRevRangeWithScore(String key, int start, int end) { + public Set zRevRangeWithScore(byte[] key, int start, int end) { throw new UnsupportedOperationException(); } @Override - public Integer zRevRank(String key, String value) { + public Integer zRevRank(byte[] key, byte[] value) { try { - return Integer.valueOf((int) jredis.zrevrank(key, value)); + return Integer.valueOf((int) jredis.zrevrank(JredisUtils.convert(charset, key), value)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Double zScore(String key, String value) { + public Double zScore(byte[] key, byte[] value) { try { - return jredis.zscore(key, value); + return jredis.zscore(JredisUtils.convert(charset, key), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -757,102 +763,103 @@ public class JredisConnection implements RedisConnection { // @Override - public Integer zUnionStore(String destKey, Aggregate aggregate, int[] weights, String... sets) { + public Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { throw new UnsupportedOperationException(); } @Override - public Integer zUnionStore(String destKey, String... sets) { + public Integer zUnionStore(byte[] destKey, byte[]... sets) { throw new UnsupportedOperationException(); } @Override - public Boolean hDel(String key, String field) { + public Boolean hDel(byte[] key, byte[] field) { try { - return jredis.hdel(key, field); + return jredis.hdel(JredisUtils.convert(charset, key), JredisUtils.convert(charset, field)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean hExists(String key, String field) { + public Boolean hExists(byte[] key, byte[] field) { try { - return jredis.hexists(key, field); + return jredis.hexists(JredisUtils.convert(charset, key), JredisUtils.convert(charset, field)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public String hGet(String key, String field) { + public byte[] hGet(byte[] key, byte[] field) { try { - return JredisUtils.convertToString(jredis.hget(key, field)); + return jredis.hget(JredisUtils.convert(charset, key), JredisUtils.convert(charset, field)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Set hGetAll(String key) { + public Set hGetAll(byte[] key) { try { - return JredisUtils.convert(jredis.hgetall(key)); + return JredisUtils.convert(jredis.hgetall(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer hIncrBy(String key, String field, int delta) { + public Integer hIncrBy(byte[] key, byte[] field, int delta) { throw new UnsupportedOperationException(); } @Override - public Set hKeys(String key) { + public Set hKeys(byte[] key) { try { - return new LinkedHashSet(jredis.hkeys(key)); + return new LinkedHashSet(JredisUtils.convert(charset, + jredis.hkeys(JredisUtils.convert(charset, key)))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Integer hLen(String key) { + public Integer hLen(byte[] key) { try { - return Integer.valueOf((int) jredis.hlen(key)); + return Integer.valueOf((int) jredis.hlen(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public List hMGet(String key, String... fields) { + public List hMGet(byte[] key, byte[]... fields) { throw new UnsupportedOperationException(); } @Override - public void hMSet(String key, String[] fields, String[] values) { + public void hMSet(byte[] key, byte[][] fields, byte[][] values) { throw new UnsupportedOperationException(); } @Override - public Boolean hSet(String key, String field, String value) { + public Boolean hSet(byte[] key, byte[] field, byte[] value) { try { - return jredis.hset(key, field, value); + return jredis.hset(JredisUtils.convert(charset, key), JredisUtils.convert(charset, field), value); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } } @Override - public Boolean hSetNX(String key, String field, String value) { + public Boolean hSetNX(byte[] key, byte[] field, byte[] value) { throw new UnsupportedOperationException(); } @Override - public List hVals(String key) { + public List hVals(byte[] key) { try { - return JredisUtils.convertToStringCollection(jredis.hvals(key), List.class); + return jredis.hvals(JredisUtils.convert(charset, key)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java index 8d4d8b6a0..a8e6c697a 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java @@ -47,6 +47,8 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean // taken from JRedis code private int poolSize = 5; + + private String charset = "ISO-8859-1"; /** * Constructs a new JredisConnectionFactory instance. @@ -116,7 +118,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean @Override public RedisConnection getConnection() { - return new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec))); + return new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec)), charset); } @@ -174,4 +176,21 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean this.poolSize = poolSize; usePool = true; } + + + /** + * + * @return + */ + public String getCharset() { + return charset; + } + + + /** + * @param charset + */ + public void setCharset(String charset) { + this.charset = charset; + } } \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java index c6ec31283..9c33d58c9 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java @@ -16,6 +16,7 @@ package org.springframework.datastore.redis.connection.jredis; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -43,10 +44,30 @@ public abstract class JredisUtils { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } - static String convertToString(byte[] bytes) { + static String convert(byte[] bytes) { return new String(bytes); } + static String convert(String encoding, byte[] bytes) { + try { + return new String(bytes, encoding); + } catch (UnsupportedEncodingException ex) { + throw new RuntimeException(ex); + } + } + + static String[] convertMultiple(String encoding, byte[]... bytes) { + String[] result = new String[bytes.length]; + try { + for (int i = 0; i < bytes.length; i++) { + result[i] = new String(bytes[i], encoding); + } + } catch (UnsupportedEncodingException ex) { + throw new RuntimeException(ex); + } + return result; + } + static > T convertToStringCollection(List bytes, Class collectionType) { Collection col = (List.class.isAssignableFrom(collectionType) ? new ArrayList(bytes.size()) @@ -93,4 +114,40 @@ public abstract class JredisUtils { } return result; } + + static Collection convert(String charset, List keys) { + Collection list = new ArrayList(keys.size()); + + try { + for (String string : keys) { + list.add(string.getBytes(charset)); + } + } catch (UnsupportedEncodingException ex) { + throw new RuntimeException(ex); + } + + return list; + } + + static byte[] convert(String charset, String string) { + try { + return string.getBytes(charset); + } catch (UnsupportedEncodingException ex) { + throw new RuntimeException(ex); + } + } + + static Map convert(String encoding, Map tuple) { + Map result = new LinkedHashMap(tuple.size()); + try { + + for (Map.Entry entry : tuple.entrySet()) { + result.put(new String(entry.getKey(), encoding), entry.getValue()); + } + } catch (UnsupportedEncodingException ex) { + throw new RuntimeException(ex); + } + + return result; + } } \ No newline at end of file