add support for java 1.5

This commit is contained in:
Jonathan Leibiusky
2010-11-21 19:53:43 -03:00
parent 71eb4c5b4a
commit 098de44a07
21 changed files with 485 additions and 451 deletions

View File

@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent> <parent>
<groupId>org.sonatype.oss</groupId> <groupId>org.sonatype.oss</groupId>
@@ -72,8 +73,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version> <version>2.0.2</version>
<configuration> <configuration>
<source>1.6</source> <source>1.5</source>
<target>1.6</target> <target>1.5</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@@ -14,6 +14,7 @@ import java.util.Map;
import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.Protocol.Keyword;
import redis.clients.util.SafeEncoder;
public class BinaryClient extends Connection { public class BinaryClient extends Connection {
public enum LIST_POSITION { public enum LIST_POSITION {
@@ -21,7 +22,7 @@ public class BinaryClient extends Connection {
public final byte[] raw; public final byte[] raw;
private LIST_POSITION() { private LIST_POSITION() {
raw = name().getBytes(Protocol.UTF8); raw = SafeEncoder.encode(name());
} }
} }

View File

@@ -12,6 +12,7 @@ import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.JedisByteHashMap; import redis.clients.util.JedisByteHashMap;
import redis.clients.util.SafeEncoder;
public class BinaryJedis implements BinaryJedisCommands { public class BinaryJedis implements BinaryJedisCommands {
protected Client client = null; protected Client client = null;
@@ -2317,8 +2318,8 @@ public class BinaryJedis implements BinaryJedisCommands {
Set<Tuple> set = new LinkedHashSet<Tuple>(); Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<byte[]> iterator = membersWithScores.iterator(); Iterator<byte[]> iterator = membersWithScores.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
set.add(new Tuple(iterator.next(), Double.valueOf(new String( set.add(new Tuple(iterator.next(), Double.valueOf(SafeEncoder
iterator.next(), Protocol.UTF8)))); .encode(iterator.next()))));
} }
return set; return set;
} }

View File

@@ -4,6 +4,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import redis.clients.util.SafeEncoder;
public class Client extends BinaryClient { public class Client extends BinaryClient {
public Client(final String host) { public Client(final String host) {
super(host); super(host);
@@ -14,82 +16,81 @@ public class Client extends BinaryClient {
} }
public void set(final String key, final String value) { public void set(final String key, final String value) {
set(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); set(SafeEncoder.encode(key), SafeEncoder.encode(value));
} }
public void get(final String key) { public void get(final String key) {
get(key.getBytes(Protocol.UTF8)); get(SafeEncoder.encode(key));
} }
public void exists(final String key) { public void exists(final String key) {
exists(key.getBytes(Protocol.UTF8)); exists(SafeEncoder.encode(key));
} }
public void del(final String... keys) { public void del(final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < keys.length; i++) { for (int i = 0; i < keys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
del(bkeys); del(bkeys);
} }
public void type(final String key) { public void type(final String key) {
type(key.getBytes(Protocol.UTF8)); type(SafeEncoder.encode(key));
} }
public void keys(final String pattern) { public void keys(final String pattern) {
keys(pattern.getBytes(Protocol.UTF8)); keys(SafeEncoder.encode(pattern));
} }
public void rename(final String oldkey, final String newkey) { public void rename(final String oldkey, final String newkey) {
rename(oldkey.getBytes(Protocol.UTF8), newkey.getBytes(Protocol.UTF8)); rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey));
} }
public void renamenx(final String oldkey, final String newkey) { public void renamenx(final String oldkey, final String newkey) {
renamenx(oldkey.getBytes(Protocol.UTF8), newkey.getBytes(Protocol.UTF8)); renamenx(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey));
} }
public void expire(final String key, final int seconds) { public void expire(final String key, final int seconds) {
expire(key.getBytes(Protocol.UTF8), seconds); expire(SafeEncoder.encode(key), seconds);
} }
public void expireAt(final String key, final long unixTime) { public void expireAt(final String key, final long unixTime) {
expireAt(key.getBytes(Protocol.UTF8), unixTime); expireAt(SafeEncoder.encode(key), unixTime);
} }
public void ttl(final String key) { public void ttl(final String key) {
ttl(key.getBytes(Protocol.UTF8)); ttl(SafeEncoder.encode(key));
} }
public void move(final String key, final int dbIndex) { public void move(final String key, final int dbIndex) {
move(key.getBytes(Protocol.UTF8), dbIndex); move(SafeEncoder.encode(key), dbIndex);
} }
public void getSet(final String key, final String value) { public void getSet(final String key, final String value) {
getSet(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); getSet(SafeEncoder.encode(key), SafeEncoder.encode(value));
} }
public void mget(final String... keys) { public void mget(final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
mget(bkeys); mget(bkeys);
} }
public void setnx(final String key, final String value) { public void setnx(final String key, final String value) {
setnx(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); setnx(SafeEncoder.encode(key), SafeEncoder.encode(value));
} }
public void setex(final String key, final int seconds, final String value) { public void setex(final String key, final int seconds, final String value) {
setex(key.getBytes(Protocol.UTF8), seconds, value setex(SafeEncoder.encode(key), seconds, SafeEncoder.encode(value));
.getBytes(Protocol.UTF8));
} }
public void mset(final String... keysvalues) { public void mset(final String... keysvalues) {
final byte[][] bkeysvalues = new byte[keysvalues.length][]; final byte[][] bkeysvalues = new byte[keysvalues.length][];
for (int i = 0; i < keysvalues.length; i++) { for (int i = 0; i < keysvalues.length; i++) {
bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]);
} }
mset(bkeysvalues); mset(bkeysvalues);
} }
@@ -97,175 +98,173 @@ public class Client extends BinaryClient {
public void msetnx(final String... keysvalues) { public void msetnx(final String... keysvalues) {
final byte[][] bkeysvalues = new byte[keysvalues.length][]; final byte[][] bkeysvalues = new byte[keysvalues.length][];
for (int i = 0; i < keysvalues.length; i++) { for (int i = 0; i < keysvalues.length; i++) {
bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]);
} }
msetnx(bkeysvalues); msetnx(bkeysvalues);
} }
public void decrBy(final String key, final int integer) { public void decrBy(final String key, final int integer) {
decrBy(key.getBytes(Protocol.UTF8), integer); decrBy(SafeEncoder.encode(key), integer);
} }
public void decr(final String key) { public void decr(final String key) {
decr(key.getBytes(Protocol.UTF8)); decr(SafeEncoder.encode(key));
} }
public void incrBy(final String key, final int integer) { public void incrBy(final String key, final int integer) {
incrBy(key.getBytes(Protocol.UTF8), integer); incrBy(SafeEncoder.encode(key), integer);
} }
public void incr(final String key) { public void incr(final String key) {
incr(key.getBytes(Protocol.UTF8)); incr(SafeEncoder.encode(key));
} }
public void append(final String key, final String value) { public void append(final String key, final String value) {
append(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); append(SafeEncoder.encode(key), SafeEncoder.encode(value));
} }
public void substr(final String key, final int start, final int end) { public void substr(final String key, final int start, final int end) {
substr(key.getBytes(Protocol.UTF8), start, end); substr(SafeEncoder.encode(key), start, end);
} }
public void hset(final String key, final String field, final String value) { public void hset(final String key, final String field, final String value) {
hset(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), value hset(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder
.getBytes(Protocol.UTF8)); .encode(value));
} }
public void hget(final String key, final String field) { public void hget(final String key, final String field) {
hget(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); hget(SafeEncoder.encode(key), SafeEncoder.encode(field));
} }
public void hsetnx(final String key, final String field, final String value) { public void hsetnx(final String key, final String field, final String value) {
hsetnx(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), hsetnx(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder
value.getBytes(Protocol.UTF8)); .encode(value));
} }
public void hmset(final String key, final Map<String, String> hash) { public void hmset(final String key, final Map<String, String> hash) {
final Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>(hash final Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>(hash
.size()); .size());
for (final Entry<String, String> entry : hash.entrySet()) { for (final Entry<String, String> entry : hash.entrySet()) {
bhash.put(entry.getKey().getBytes(Protocol.UTF8), entry.getValue() bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder
.getBytes(Protocol.UTF8)); .encode(entry.getValue()));
} }
hmset(key.getBytes(Protocol.UTF8), bhash); hmset(SafeEncoder.encode(key), bhash);
} }
public void hmget(final String key, final String... fields) { public void hmget(final String key, final String... fields) {
final byte[][] bfields = new byte[fields.length][]; final byte[][] bfields = new byte[fields.length][];
for (int i = 0; i < bfields.length; i++) { for (int i = 0; i < bfields.length; i++) {
bfields[i] = fields[i].getBytes(Protocol.UTF8); bfields[i] = SafeEncoder.encode(fields[i]);
} }
hmget(key.getBytes(Protocol.UTF8), bfields); hmget(SafeEncoder.encode(key), bfields);
} }
public void hincrBy(final String key, final String field, final int value) { public void hincrBy(final String key, final String field, final int value) {
hincrBy(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value);
value);
} }
public void hexists(final String key, final String field) { public void hexists(final String key, final String field) {
hexists(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); hexists(SafeEncoder.encode(key), SafeEncoder.encode(field));
} }
public void hdel(final String key, final String field) { public void hdel(final String key, final String field) {
hdel(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); hdel(SafeEncoder.encode(key), SafeEncoder.encode(field));
} }
public void hlen(final String key) { public void hlen(final String key) {
hlen(key.getBytes(Protocol.UTF8)); hlen(SafeEncoder.encode(key));
} }
public void hkeys(final String key) { public void hkeys(final String key) {
hkeys(key.getBytes(Protocol.UTF8)); hkeys(SafeEncoder.encode(key));
} }
public void hvals(final String key) { public void hvals(final String key) {
hvals(key.getBytes(Protocol.UTF8)); hvals(SafeEncoder.encode(key));
} }
public void hgetAll(final String key) { public void hgetAll(final String key) {
hgetAll(key.getBytes(Protocol.UTF8)); hgetAll(SafeEncoder.encode(key));
} }
public void rpush(final String key, final String string) { public void rpush(final String key, final String string) {
rpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); rpush(SafeEncoder.encode(key), SafeEncoder.encode(string));
} }
public void lpush(final String key, final String string) { public void lpush(final String key, final String string) {
lpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); lpush(SafeEncoder.encode(key), SafeEncoder.encode(string));
} }
public void llen(final String key) { public void llen(final String key) {
llen(key.getBytes(Protocol.UTF8)); llen(SafeEncoder.encode(key));
} }
public void lrange(final String key, final int start, final int end) { public void lrange(final String key, final int start, final int end) {
lrange(key.getBytes(Protocol.UTF8), start, end); lrange(SafeEncoder.encode(key), start, end);
} }
public void ltrim(final String key, final int start, final int end) { public void ltrim(final String key, final int start, final int end) {
ltrim(key.getBytes(Protocol.UTF8), start, end); ltrim(SafeEncoder.encode(key), start, end);
} }
public void lindex(final String key, final int index) { public void lindex(final String key, final int index) {
lindex(key.getBytes(Protocol.UTF8), index); lindex(SafeEncoder.encode(key), index);
} }
public void lset(final String key, final int index, final String value) { public void lset(final String key, final int index, final String value) {
lset(key.getBytes(Protocol.UTF8), index, value.getBytes(Protocol.UTF8)); lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value));
} }
public void lrem(final String key, int count, final String value) { public void lrem(final String key, int count, final String value) {
lrem(key.getBytes(Protocol.UTF8), count, value.getBytes(Protocol.UTF8)); lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value));
} }
public void lpop(final String key) { public void lpop(final String key) {
lpop(key.getBytes(Protocol.UTF8)); lpop(SafeEncoder.encode(key));
} }
public void rpop(final String key) { public void rpop(final String key) {
rpop(key.getBytes(Protocol.UTF8)); rpop(SafeEncoder.encode(key));
} }
public void rpoplpush(final String srckey, final String dstkey) { public void rpoplpush(final String srckey, final String dstkey) {
rpoplpush(srckey.getBytes(Protocol.UTF8), dstkey rpoplpush(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey));
.getBytes(Protocol.UTF8));
} }
public void sadd(final String key, final String member) { public void sadd(final String key, final String member) {
sadd(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); sadd(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void smembers(final String key) { public void smembers(final String key) {
smembers(key.getBytes(Protocol.UTF8)); smembers(SafeEncoder.encode(key));
} }
public void srem(final String key, final String member) { public void srem(final String key, final String member) {
srem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); srem(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void spop(final String key) { public void spop(final String key) {
spop(key.getBytes(Protocol.UTF8)); spop(SafeEncoder.encode(key));
} }
public void smove(final String srckey, final String dstkey, public void smove(final String srckey, final String dstkey,
final String member) { final String member) {
smove(srckey.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8), smove(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey),
member.getBytes(Protocol.UTF8)); SafeEncoder.encode(member));
} }
public void scard(final String key) { public void scard(final String key) {
scard(key.getBytes(Protocol.UTF8)); scard(SafeEncoder.encode(key));
} }
public void sismember(final String key, final String member) { public void sismember(final String key, final String member) {
sismember(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); sismember(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void sinter(final String... keys) { public void sinter(final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sinter(bkeys); sinter(bkeys);
} }
@@ -273,15 +272,15 @@ public class Client extends BinaryClient {
public void sinterstore(final String dstkey, final String... keys) { public void sinterstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sinterstore(dstkey.getBytes(Protocol.UTF8), bkeys); sinterstore(SafeEncoder.encode(dstkey), bkeys);
} }
public void sunion(final String... keys) { public void sunion(final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sunion(bkeys); sunion(bkeys);
} }
@@ -289,15 +288,15 @@ public class Client extends BinaryClient {
public void sunionstore(final String dstkey, final String... keys) { public void sunionstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sunionstore(dstkey.getBytes(Protocol.UTF8), bkeys); sunionstore(SafeEncoder.encode(dstkey), bkeys);
} }
public void sdiff(final String... keys) { public void sdiff(final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sdiff(bkeys); sdiff(bkeys);
} }
@@ -305,198 +304,197 @@ public class Client extends BinaryClient {
public void sdiffstore(final String dstkey, final String... keys) { public void sdiffstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][]; final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) { for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8); bkeys[i] = SafeEncoder.encode(keys[i]);
} }
sdiffstore(dstkey.getBytes(Protocol.UTF8), bkeys); sdiffstore(SafeEncoder.encode(dstkey), bkeys);
} }
public void srandmember(final String key) { public void srandmember(final String key) {
srandmember(key.getBytes(Protocol.UTF8)); srandmember(SafeEncoder.encode(key));
} }
public void zadd(final String key, final double score, final String member) { public void zadd(final String key, final double score, final String member) {
zadd(key.getBytes(Protocol.UTF8), score, member.getBytes(Protocol.UTF8)); zadd(SafeEncoder.encode(key), score, SafeEncoder.encode(member));
} }
public void zrange(final String key, final int start, final int end) { public void zrange(final String key, final int start, final int end) {
zrange(key.getBytes(Protocol.UTF8), start, end); zrange(SafeEncoder.encode(key), start, end);
} }
public void zrem(final String key, final String member) { public void zrem(final String key, final String member) {
zrem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); zrem(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void zincrby(final String key, final double score, public void zincrby(final String key, final double score,
final String member) { final String member) {
zincrby(key.getBytes(Protocol.UTF8), score, member zincrby(SafeEncoder.encode(key), score, SafeEncoder.encode(member));
.getBytes(Protocol.UTF8));
} }
public void zrank(final String key, final String member) { public void zrank(final String key, final String member) {
zrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); zrank(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void zrevrank(final String key, final String member) { public void zrevrank(final String key, final String member) {
zrevrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); zrevrank(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void zrevrange(final String key, final int start, final int end) { public void zrevrange(final String key, final int start, final int end) {
zrevrange(key.getBytes(Protocol.UTF8), start, end); zrevrange(SafeEncoder.encode(key), start, end);
} }
public void zrangeWithScores(final String key, final int start, public void zrangeWithScores(final String key, final int start,
final int end) { final int end) {
zrangeWithScores(key.getBytes(Protocol.UTF8), start, end); zrangeWithScores(SafeEncoder.encode(key), start, end);
} }
public void zrevrangeWithScores(final String key, final int start, public void zrevrangeWithScores(final String key, final int start,
final int end) { final int end) {
zrevrangeWithScores(key.getBytes(Protocol.UTF8), start, end); zrevrangeWithScores(SafeEncoder.encode(key), start, end);
} }
public void zcard(final String key) { public void zcard(final String key) {
zcard(key.getBytes(Protocol.UTF8)); zcard(SafeEncoder.encode(key));
} }
public void zscore(final String key, final String member) { public void zscore(final String key, final String member) {
zscore(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); zscore(SafeEncoder.encode(key), SafeEncoder.encode(member));
} }
public void watch(final String key) { public void watch(final String key) {
watch(key.getBytes(Protocol.UTF8)); watch(SafeEncoder.encode(key));
} }
public void sort(final String key) { public void sort(final String key) {
sort(key.getBytes(Protocol.UTF8)); sort(SafeEncoder.encode(key));
} }
public void sort(final String key, final SortingParams sortingParameters) { public void sort(final String key, final SortingParams sortingParameters) {
sort(key.getBytes(Protocol.UTF8), sortingParameters); sort(SafeEncoder.encode(key), sortingParameters);
} }
public void blpop(final String[] args) { public void blpop(final String[] args) {
final byte[][] bargs = new byte[args.length][]; final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < bargs.length; i++) { for (int i = 0; i < bargs.length; i++) {
bargs[i] = args[i].getBytes(Protocol.UTF8); bargs[i] = SafeEncoder.encode(args[i]);
} }
blpop(bargs); blpop(bargs);
} }
public void sort(final String key, final SortingParams sortingParameters, public void sort(final String key, final SortingParams sortingParameters,
final String dstkey) { final String dstkey) {
sort(key.getBytes(Protocol.UTF8), sortingParameters, dstkey sort(SafeEncoder.encode(key), sortingParameters, SafeEncoder
.getBytes(Protocol.UTF8)); .encode(dstkey));
} }
public void sort(final String key, final String dstkey) { public void sort(final String key, final String dstkey) {
sort(key.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8)); sort(SafeEncoder.encode(key), SafeEncoder.encode(dstkey));
} }
public void brpop(final String[] args) { public void brpop(final String[] args) {
final byte[][] bargs = new byte[args.length][]; final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < bargs.length; i++) { for (int i = 0; i < bargs.length; i++) {
bargs[i] = args[i].getBytes(Protocol.UTF8); bargs[i] = SafeEncoder.encode(args[i]);
} }
brpop(bargs); brpop(bargs);
} }
public void zcount(final String key, final double min, final double max) { public void zcount(final String key, final double min, final double max) {
zcount(key.getBytes(Protocol.UTF8), min, max); zcount(SafeEncoder.encode(key), min, max);
} }
public void zrangeByScore(final String key, final double min, public void zrangeByScore(final String key, final double min,
final double max) { final double max) {
zrangeByScore(key.getBytes(Protocol.UTF8), min, max); zrangeByScore(SafeEncoder.encode(key), min, max);
} }
public void zrangeByScore(final String key, final String min, public void zrangeByScore(final String key, final String min,
final String max) { final String max) {
zrangeByScore(key.getBytes(Protocol.UTF8), min.getBytes(Protocol.UTF8), zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min),
max.getBytes(Protocol.UTF8)); SafeEncoder.encode(max));
} }
public void zrangeByScore(final String key, final double min, public void zrangeByScore(final String key, final double min,
final double max, final int offset, int count) { final double max, final int offset, int count) {
zrangeByScore(key.getBytes(Protocol.UTF8), min, max, offset, count); zrangeByScore(SafeEncoder.encode(key), min, max, offset, count);
} }
public void zrangeByScoreWithScores(final String key, final double min, public void zrangeByScoreWithScores(final String key, final double min,
final double max) { final double max) {
zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max); zrangeByScoreWithScores(SafeEncoder.encode(key), min, max);
} }
public void zrangeByScoreWithScores(final String key, final double min, public void zrangeByScoreWithScores(final String key, final double min,
final double max, final int offset, final int count) { final double max, final int offset, final int count) {
zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max, offset, zrangeByScoreWithScores(SafeEncoder.encode(key), min, max, offset,
count); count);
} }
public void zremrangeByRank(final String key, final int start, final int end) { public void zremrangeByRank(final String key, final int start, final int end) {
zremrangeByRank(key.getBytes(Protocol.UTF8), start, end); zremrangeByRank(SafeEncoder.encode(key), start, end);
} }
public void zremrangeByScore(final String key, final double start, public void zremrangeByScore(final String key, final double start,
final double end) { final double end) {
zremrangeByScore(key.getBytes(Protocol.UTF8), start, end); zremrangeByScore(SafeEncoder.encode(key), start, end);
} }
public void zunionstore(final String dstkey, final String... sets) { public void zunionstore(final String dstkey, final String... sets) {
final byte[][] bsets = new byte[sets.length][]; final byte[][] bsets = new byte[sets.length][];
for (int i = 0; i < bsets.length; i++) { for (int i = 0; i < bsets.length; i++) {
bsets[i] = sets[i].getBytes(Protocol.UTF8); bsets[i] = SafeEncoder.encode(sets[i]);
} }
zunionstore(dstkey.getBytes(Protocol.UTF8), bsets); zunionstore(SafeEncoder.encode(dstkey), bsets);
} }
public void zunionstore(final String dstkey, final ZParams params, public void zunionstore(final String dstkey, final ZParams params,
final String... sets) { final String... sets) {
final byte[][] bsets = new byte[sets.length][]; final byte[][] bsets = new byte[sets.length][];
for (int i = 0; i < bsets.length; i++) { for (int i = 0; i < bsets.length; i++) {
bsets[i] = sets[i].getBytes(Protocol.UTF8); bsets[i] = SafeEncoder.encode(sets[i]);
} }
zunionstore(dstkey.getBytes(Protocol.UTF8), params, bsets); zunionstore(SafeEncoder.encode(dstkey), params, bsets);
} }
public void zinterstore(final String dstkey, final String... sets) { public void zinterstore(final String dstkey, final String... sets) {
final byte[][] bsets = new byte[sets.length][]; final byte[][] bsets = new byte[sets.length][];
for (int i = 0; i < bsets.length; i++) { for (int i = 0; i < bsets.length; i++) {
bsets[i] = sets[i].getBytes(Protocol.UTF8); bsets[i] = SafeEncoder.encode(sets[i]);
} }
zinterstore(dstkey.getBytes(Protocol.UTF8), bsets); zinterstore(SafeEncoder.encode(dstkey), bsets);
} }
public void zinterstore(final String dstkey, final ZParams params, public void zinterstore(final String dstkey, final ZParams params,
final String... sets) { final String... sets) {
final byte[][] bsets = new byte[sets.length][]; final byte[][] bsets = new byte[sets.length][];
for (int i = 0; i < bsets.length; i++) { for (int i = 0; i < bsets.length; i++) {
bsets[i] = sets[i].getBytes(Protocol.UTF8); bsets[i] = SafeEncoder.encode(sets[i]);
} }
zinterstore(dstkey.getBytes(Protocol.UTF8), params, bsets); zinterstore(SafeEncoder.encode(dstkey), params, bsets);
} }
public void strlen(final String key) { public void strlen(final String key) {
strlen(key.getBytes(Protocol.UTF8)); strlen(SafeEncoder.encode(key));
} }
public void lpushx(final String key, final String string) { public void lpushx(final String key, final String string) {
lpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); lpushx(SafeEncoder.encode(key), SafeEncoder.encode(string));
} }
public void persist(final String key) { public void persist(final String key) {
persist(key.getBytes(Protocol.UTF8)); persist(SafeEncoder.encode(key));
} }
public void rpushx(final String key, final String string) { public void rpushx(final String key, final String string) {
rpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); rpushx(SafeEncoder.encode(key), SafeEncoder.encode(string));
} }
public void echo(final String string) { public void echo(final String string) {
echo(string.getBytes(Protocol.UTF8)); echo(SafeEncoder.encode(string));
} }
public void linsert(final String key, final LIST_POSITION where, public void linsert(final String key, final LIST_POSITION where,
final String pivot, final String value) { final String pivot, final String value) {
linsert(key.getBytes(Protocol.UTF8), where, pivot linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot),
.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); SafeEncoder.encode(value));
} }
} }

View File

@@ -10,6 +10,7 @@ import java.util.List;
import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Protocol.Command;
import redis.clients.util.RedisInputStream; import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream; import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
public class Connection { public class Connection {
private String host; private String host;
@@ -53,7 +54,7 @@ public class Connection {
protected Connection sendCommand(final Command cmd, final String... args) { protected Connection sendCommand(final Command cmd, final String... args) {
final byte[][] bargs = new byte[args.length][]; final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
bargs[i] = args[i].getBytes(Protocol.UTF8); bargs[i] = SafeEncoder.encode(args[i]);
} }
return sendCommand(cmd, bargs); return sendCommand(cmd, bargs);
} }
@@ -144,14 +145,14 @@ public class Connection {
if (null == resp) { if (null == resp) {
return null; return null;
} else { } else {
return new String(resp, Protocol.UTF8); return SafeEncoder.encode(resp);
} }
} }
public String getBulkReply() { public String getBulkReply() {
final byte[] result = getBinaryBulkReply(); final byte[] result = getBinaryBulkReply();
if (null != result) { if (null != result) {
return new String(result, Protocol.UTF8); return SafeEncoder.encode(result);
} else { } else {
return null; return null;
} }
@@ -177,7 +178,7 @@ public class Connection {
if (barray == null) { if (barray == null) {
result.add(null); result.add(null);
} else { } else {
result.add(new String(barray, Protocol.UTF8)); result.add(SafeEncoder.encode(barray));
} }
} }
return result; return result;

View File

@@ -1,9 +1,16 @@
package redis.clients.jedis; package redis.clients.jedis;
import static redis.clients.jedis.Protocol.Keyword.MESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PMESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.PUNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.SUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static redis.clients.jedis.Protocol.Keyword.*; import redis.clients.util.SafeEncoder;
public abstract class JedisPubSub { public abstract class JedisPubSub {
private int subscribedChannels = 0; private int subscribedChannels = 0;
@@ -67,76 +74,54 @@ public abstract class JedisPubSub {
List<Object> reply = client.getObjectMultiBulkReply(); List<Object> reply = client.getObjectMultiBulkReply();
final Object firstObj = reply.get(0); final Object firstObj = reply.get(0);
if (!(firstObj instanceof byte[])) { if (!(firstObj instanceof byte[])) {
throw throw new JedisException("Unknown message type: " + firstObj);
new JedisException("Unknown message type: "+ firstObj);
} }
final byte[] resp = (byte[]) firstObj; final byte[] resp = (byte[]) firstObj;
if(Arrays.equals(SUBSCRIBE.raw, resp)) { if (Arrays.equals(SUBSCRIBE.raw, resp)) {
subscribedChannels = ((Integer) reply.get(2)).intValue(); subscribedChannels = ((Integer) reply.get(2)).intValue();
final byte[] bchannel = (byte[]) reply.get(1); final byte[] bchannel = (byte[]) reply.get(1);
final String strchannel = final String strchannel = (bchannel == null) ? null
(bchannel == null) ? : SafeEncoder.encode(bchannel);
null : onSubscribe(strchannel, subscribedChannels);
new String(bchannel, Protocol.UTF8);
onSubscribe(strchannel, subscribedChannels);
} else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) { } else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) {
subscribedChannels = ((Integer) reply.get(2)).intValue(); subscribedChannels = ((Integer) reply.get(2)).intValue();
final byte[] bchannel = (byte[]) reply.get(1); final byte[] bchannel = (byte[]) reply.get(1);
final String strchannel = final String strchannel = (bchannel == null) ? null
(bchannel == null) ? : SafeEncoder.encode(bchannel);
null : onUnsubscribe(strchannel, subscribedChannels);
new String(bchannel, Protocol.UTF8);
onUnsubscribe(strchannel, subscribedChannels);
} else if (Arrays.equals(MESSAGE.raw, resp)) { } else if (Arrays.equals(MESSAGE.raw, resp)) {
final byte[] bchannel = (byte[]) reply.get(1); final byte[] bchannel = (byte[]) reply.get(1);
final byte[] bmesg = (byte[]) reply.get(2); final byte[] bmesg = (byte[]) reply.get(2);
final String strchannel = final String strchannel = (bchannel == null) ? null
(bchannel == null) ? : SafeEncoder.encode(bchannel);
null : final String strmesg = (bmesg == null) ? null : SafeEncoder
new String(bchannel, Protocol.UTF8); .encode(bmesg);
final String strmesg =
(bmesg == null) ?
null :
new String(bmesg, Protocol.UTF8);
onMessage(strchannel, strmesg); onMessage(strchannel, strmesg);
} else if (Arrays.equals(PMESSAGE.raw, resp)) { } else if (Arrays.equals(PMESSAGE.raw, resp)) {
final byte[] bpattern = (byte[]) reply.get(1); final byte[] bpattern = (byte[]) reply.get(1);
final byte[] bchannel = (byte[]) reply.get(2); final byte[] bchannel = (byte[]) reply.get(2);
final byte[] bmesg = (byte[]) reply.get(3); final byte[] bmesg = (byte[]) reply.get(3);
final String strpattern = final String strpattern = (bpattern == null) ? null
(bpattern == null) ? : SafeEncoder.encode(bpattern);
null : final String strchannel = (bchannel == null) ? null
new String(bpattern, Protocol.UTF8); : SafeEncoder.encode(bchannel);
final String strchannel = final String strmesg = (bmesg == null) ? null : SafeEncoder
(bchannel == null) ? .encode(bmesg);
null : onPMessage(strpattern, strchannel, strmesg);
new String(bchannel, Protocol.UTF8);
final String strmesg =
(bmesg == null) ?
null :
new String(bmesg, Protocol.UTF8);
onPMessage(
strpattern,
strchannel,
strmesg);
} else if (Arrays.equals(PSUBSCRIBE.raw, resp)) { } else if (Arrays.equals(PSUBSCRIBE.raw, resp)) {
subscribedChannels = ((Integer) reply.get(2)).intValue(); subscribedChannels = ((Integer) reply.get(2)).intValue();
final byte[] bpattern = (byte[]) reply.get(1); final byte[] bpattern = (byte[]) reply.get(1);
final String strpattern = final String strpattern = (bpattern == null) ? null
(bpattern == null) ? : SafeEncoder.encode(bpattern);
null :
new String(bpattern, Protocol.UTF8);
onPSubscribe(strpattern, subscribedChannels); onPSubscribe(strpattern, subscribedChannels);
} else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) { } else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) {
subscribedChannels = ((Integer) reply.get(2)).intValue(); subscribedChannels = ((Integer) reply.get(2)).intValue();
final byte[] bpattern = (byte[]) reply.get(1); final byte[] bpattern = (byte[]) reply.get(1);
final String strpattern = final String strpattern = (bpattern == null) ? null
(bpattern == null) ? : SafeEncoder.encode(bpattern);
null :
new String(bpattern, Protocol.UTF8);
onPUnsubscribe(strpattern, subscribedChannels); onPUnsubscribe(strpattern, subscribedChannels);
} else { } else {
throw new JedisException("Unknown message type: "+ firstObj); throw new JedisException("Unknown message type: " + firstObj);
} }
} while (isSubscribed()); } while (isSubscribed());
} }

View File

@@ -1,19 +1,19 @@
package redis.clients.jedis; package redis.clients.jedis;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import redis.clients.util.RedisInputStream; import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream; import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
public final class Protocol { public final class Protocol {
public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_PORT = 6379;
public static final int DEFAULT_TIMEOUT = 2000; public static final int DEFAULT_TIMEOUT = 2000;
public static final Charset UTF8 = Charset.forName("UTF-8"); public static final String CHARSET = "UTF-8";
public static final byte DOLLAR_BYTE = '$'; public static final byte DOLLAR_BYTE = '$';
public static final byte ASTERISK_BYTE = '*'; public static final byte ASTERISK_BYTE = '*';
@@ -76,7 +76,7 @@ public final class Protocol {
} }
private byte[] processStatusCodeReply(final RedisInputStream is) { private byte[] processStatusCodeReply(final RedisInputStream is) {
return is.readLine().getBytes(UTF8); return SafeEncoder.encode(is.readLine());
} }
private byte[] processBulkReply(final RedisInputStream is) { private byte[] processBulkReply(final RedisInputStream is) {
@@ -122,15 +122,15 @@ public final class Protocol {
} }
public static final byte[] toByteArray(final int value) { public static final byte[] toByteArray(final int value) {
return String.valueOf(value).getBytes(Protocol.UTF8); return SafeEncoder.encode(String.valueOf(value));
} }
public static final byte[] toByteArray(final long value) { public static final byte[] toByteArray(final long value) {
return String.valueOf(value).getBytes(Protocol.UTF8); return SafeEncoder.encode(String.valueOf(value));
} }
public static final byte[] toByteArray(final double value) { public static final byte[] toByteArray(final double value) {
return String.valueOf(value).getBytes(Protocol.UTF8); return SafeEncoder.encode(String.valueOf(value));
} }
public static enum Command { public static enum Command {
@@ -139,7 +139,7 @@ public final class Protocol {
public final byte[] raw; public final byte[] raw;
Command() { Command() {
raw = this.name().getBytes(UTF8); raw = SafeEncoder.encode(this.name());
} }
} }
@@ -148,7 +148,7 @@ public final class Protocol {
public final byte[] raw; public final byte[] raw;
Keyword() { Keyword() {
raw = this.name().toLowerCase().getBytes(UTF8); raw = SafeEncoder.encode(this.name().toLowerCase());
} }
} }

View File

@@ -1,10 +1,20 @@
package redis.clients.jedis; package redis.clients.jedis;
import static redis.clients.jedis.Protocol.Keyword.ALPHA;
import static redis.clients.jedis.Protocol.Keyword.ASC;
import static redis.clients.jedis.Protocol.Keyword.BY;
import static redis.clients.jedis.Protocol.Keyword.DESC;
import static redis.clients.jedis.Protocol.Keyword.GET;
import static redis.clients.jedis.Protocol.Keyword.LIMIT;
import static redis.clients.jedis.Protocol.Keyword.NOSORT;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static redis.clients.jedis.Protocol.Keyword.*;
import redis.clients.util.SafeEncoder;
/** /**
* Builder Class for {@link Jedis#sort(String, SortingParams) SORT} Parameters. * Builder Class for {@link Jedis#sort(String, SortingParams) SORT} Parameters.
* *
@@ -26,7 +36,7 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams by(final String pattern) { public SortingParams by(final String pattern) {
return by(pattern.getBytes(Protocol.UTF8)); return by(SafeEncoder.encode(pattern));
} }
/** /**
@@ -43,9 +53,9 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams by(final byte[] pattern) { public SortingParams by(final byte[] pattern) {
params.add(BY.raw); params.add(BY.raw);
params.add(pattern); params.add(pattern);
return this; return this;
} }
/** /**
@@ -57,13 +67,13 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams nosort() { public SortingParams nosort() {
params.add(BY.raw); params.add(BY.raw);
params.add(NOSORT.raw); params.add(NOSORT.raw);
return this; return this;
} }
public Collection<byte[]> getParams() { public Collection<byte[]> getParams() {
return Collections.unmodifiableCollection(params); return Collections.unmodifiableCollection(params);
} }
/** /**
@@ -72,8 +82,8 @@ public class SortingParams {
* @return the sortingParams Object * @return the sortingParams Object
*/ */
public SortingParams desc() { public SortingParams desc() {
params.add(DESC.raw); params.add(DESC.raw);
return this; return this;
} }
/** /**
@@ -82,8 +92,8 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams asc() { public SortingParams asc() {
params.add(ASC.raw); params.add(ASC.raw);
return this; return this;
} }
/** /**
@@ -95,10 +105,10 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams limit(final int start, final int count) { public SortingParams limit(final int start, final int count) {
params.add(LIMIT.raw); params.add(LIMIT.raw);
params.add(Protocol.toByteArray(start)); params.add(Protocol.toByteArray(start));
params.add(Protocol.toByteArray(count)); params.add(Protocol.toByteArray(count));
return this; return this;
} }
/** /**
@@ -108,8 +118,8 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams alpha() { public SortingParams alpha() {
params.add(ALPHA.raw); params.add(ALPHA.raw);
return this; return this;
} }
/** /**
@@ -128,11 +138,11 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams get(String... patterns) { public SortingParams get(String... patterns) {
for (final String pattern : patterns) { for (final String pattern : patterns) {
params.add(GET.raw); params.add(GET.raw);
params.add(pattern.getBytes(Protocol.UTF8)); params.add(SafeEncoder.encode(pattern));
} }
return this; return this;
} }
/** /**
@@ -151,10 +161,10 @@ public class SortingParams {
* @return the SortingParams Object * @return the SortingParams Object
*/ */
public SortingParams get(byte[]... patterns) { public SortingParams get(byte[]... patterns) {
for (final byte[] pattern : patterns) { for (final byte[] pattern : patterns) {
params.add(GET.raw); params.add(GET.raw);
params.add(pattern); params.add(pattern);
} }
return this; return this;
} }
} }

View File

@@ -2,75 +2,77 @@ package redis.clients.jedis;
import java.util.Arrays; import java.util.Arrays;
import redis.clients.util.SafeEncoder;
public class Tuple { public class Tuple {
private byte[] element; private byte[] element;
private Double score; private Double score;
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result; result = prime * result;
if (null != element) { if (null != element) {
for(final byte b : element) { for (final byte b : element) {
result = prime * result + b; result = prime * result + b;
} }
} }
long temp; long temp;
temp = Double.doubleToLongBits(score); temp = Double.doubleToLongBits(score);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Tuple other = (Tuple) obj; Tuple other = (Tuple) obj;
if (element == null) { if (element == null) {
if (other.element != null) if (other.element != null)
return false; return false;
} else if (!Arrays.equals(element, other.element)) } else if (!Arrays.equals(element, other.element))
return false; return false;
if (Double.doubleToLongBits(score) != Double if (Double.doubleToLongBits(score) != Double
.doubleToLongBits(other.score)) .doubleToLongBits(other.score))
return false; return false;
return true; return true;
} }
public Tuple(String element, Double score) { public Tuple(String element, Double score) {
super(); super();
this.element = element.getBytes(Protocol.UTF8); this.element = SafeEncoder.encode(element);
this.score = score; this.score = score;
} }
public Tuple(byte[] element, Double score) { public Tuple(byte[] element, Double score) {
super(); super();
this.element = element; this.element = element;
this.score = score; this.score = score;
} }
public String getElement() { public String getElement() {
if(null != element) { if (null != element) {
return new String(element, Protocol.UTF8); return SafeEncoder.encode(element);
} else { } else {
return null; return null;
} }
} }
public byte[] getBinaryElement() { public byte[] getBinaryElement() {
return element; return element;
} }
public double getScore() { public double getScore() {
return score; return score;
} }
public String toString() { public String toString() {
return '['+Arrays.toString(element)+','+score+']'; return '[' + Arrays.toString(element) + ',' + score + ']';
} }
} }

View File

@@ -1,40 +1,44 @@
package redis.clients.jedis; package redis.clients.jedis;
import static redis.clients.jedis.Protocol.Keyword.AGGREGATE;
import static redis.clients.jedis.Protocol.Keyword.WEIGHTS;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static redis.clients.jedis.Protocol.Keyword.*; import redis.clients.util.SafeEncoder;
public class ZParams { public class ZParams {
public enum Aggregate { public enum Aggregate {
SUM, MIN, MAX; SUM, MIN, MAX;
public final byte[] raw; public final byte[] raw;
Aggregate() {
raw = name().getBytes(Protocol.UTF8); Aggregate() {
} raw = SafeEncoder.encode(name());
}
} }
private List<byte[]> params = new ArrayList<byte[]>(); private List<byte[]> params = new ArrayList<byte[]>();
public ZParams weights(final int... weights) { public ZParams weights(final int... weights) {
params.add(WEIGHTS.raw); params.add(WEIGHTS.raw);
for (final int weight : weights) { for (final int weight : weights) {
params.add(Protocol.toByteArray(weight)); params.add(Protocol.toByteArray(weight));
} }
return this; return this;
} }
public Collection<byte[]> getParams() { public Collection<byte[]> getParams() {
return Collections.unmodifiableCollection(params); return Collections.unmodifiableCollection(params);
} }
public ZParams aggregate(final Aggregate aggregate) { public ZParams aggregate(final Aggregate aggregate) {
params.add(AGGREGATE.raw); params.add(AGGREGATE.raw);
params.add(aggregate.raw); params.add(aggregate.raw);
return this; return this;
} }
} }

View File

@@ -3,38 +3,37 @@ package redis.clients.util;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import redis.clients.jedis.Protocol;
public interface Hashing { public interface Hashing {
public static final Hashing MURMUR_HASH = new MurmurHash(); public static final Hashing MURMUR_HASH = new MurmurHash();
public static final Hashing MD5 = new Hashing() { public static final Hashing MD5 = new Hashing() {
private MessageDigest md5 = null; // avoid recurring construction private MessageDigest md5 = null; // avoid recurring construction
public long hash(String key) {
return hash(key.getBytes(Protocol.UTF8));
}
public long hash(byte[] key) { public long hash(String key) {
if (md5 == null) { return hash(SafeEncoder.encode(key));
try { }
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"++++ no md5 algorythm found");
}
}
md5.reset(); public long hash(byte[] key) {
md5.update(key); if (md5 == null) {
byte[] bKey = md5.digest(); try {
long res = ((long) (bKey[3] & 0xFF) << 24) md5 = MessageDigest.getInstance("MD5");
| ((long) (bKey[2] & 0xFF) << 16) } catch (NoSuchAlgorithmException e) {
| ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF); throw new IllegalStateException(
return res; "++++ no md5 algorythm found");
} }
}
md5.reset();
md5.update(key);
byte[] bKey = md5.digest();
long res = ((long) (bKey[3] & 0xFF) << 24)
| ((long) (bKey[2] & 0xFF) << 16)
| ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF);
return res;
}
}; };
public long hash(String key); public long hash(String key);
public long hash(byte[] key); public long hash(byte[] key);
} }

View File

@@ -20,8 +20,6 @@ package redis.clients.util;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import redis.clients.jedis.Protocol;
/** /**
* This is a very fast, non-cryptographic hash suitable for general hash-based * This is a very fast, non-cryptographic hash suitable for general hash-based
* lookup. See http://murmurhash.googlepages.com/ for more details. * lookup. See http://murmurhash.googlepages.com/ for more details.
@@ -42,7 +40,7 @@ public class MurmurHash implements Hashing {
* @return The 32 bit hash of the bytes in question. * @return The 32 bit hash of the bytes in question.
*/ */
public static int hash(byte[] data, int seed) { public static int hash(byte[] data, int seed) {
return hash(ByteBuffer.wrap(data), seed); return hash(ByteBuffer.wrap(data), seed);
} }
/** /**
@@ -59,7 +57,7 @@ public class MurmurHash implements Hashing {
* @return The 32-bit hash of the data in question. * @return The 32-bit hash of the data in question.
*/ */
public static int hash(byte[] data, int offset, int length, int seed) { public static int hash(byte[] data, int offset, int length, int seed) {
return hash(ByteBuffer.wrap(data, offset, length), seed); return hash(ByteBuffer.wrap(data, offset, length), seed);
} }
/** /**
@@ -72,97 +70,97 @@ public class MurmurHash implements Hashing {
* @return The 32 bit murmur hash of the bytes in the buffer. * @return The 32 bit murmur hash of the bytes in the buffer.
*/ */
public static int hash(ByteBuffer buf, int seed) { public static int hash(ByteBuffer buf, int seed) {
// save byte order for later restoration // save byte order for later restoration
ByteOrder byteOrder = buf.order(); ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN); buf.order(ByteOrder.LITTLE_ENDIAN);
int m = 0x5bd1e995; int m = 0x5bd1e995;
int r = 24; int r = 24;
int h = seed ^ buf.remaining(); int h = seed ^ buf.remaining();
int k; int k;
while (buf.remaining() >= 4) { while (buf.remaining() >= 4) {
k = buf.getInt(); k = buf.getInt();
k *= m; k *= m;
k ^= k >>> r; k ^= k >>> r;
k *= m; k *= m;
h *= m; h *= m;
h ^= k; h ^= k;
} }
if (buf.remaining() > 0) { if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(4).order( ByteBuffer finish = ByteBuffer.allocate(4).order(
ByteOrder.LITTLE_ENDIAN); ByteOrder.LITTLE_ENDIAN);
// for big-endian version, use this first: // for big-endian version, use this first:
// finish.position(4-buf.remaining()); // finish.position(4-buf.remaining());
finish.put(buf).rewind(); finish.put(buf).rewind();
h ^= finish.getInt(); h ^= finish.getInt();
h *= m; h *= m;
} }
h ^= h >>> 13; h ^= h >>> 13;
h *= m; h *= m;
h ^= h >>> 15; h ^= h >>> 15;
buf.order(byteOrder); buf.order(byteOrder);
return h; return h;
} }
public static long hash64A(byte[] data, int seed) { public static long hash64A(byte[] data, int seed) {
return hash64A(ByteBuffer.wrap(data), seed); return hash64A(ByteBuffer.wrap(data), seed);
} }
public static long hash64A(byte[] data, int offset, int length, int seed) { public static long hash64A(byte[] data, int offset, int length, int seed) {
return hash64A(ByteBuffer.wrap(data, offset, length), seed); return hash64A(ByteBuffer.wrap(data, offset, length), seed);
} }
public static long hash64A(ByteBuffer buf, int seed) { public static long hash64A(ByteBuffer buf, int seed) {
ByteOrder byteOrder = buf.order(); ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN); buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L; long m = 0xc6a4a7935bd1e995L;
int r = 47; int r = 47;
long h = seed ^ (buf.remaining() * m); long h = seed ^ (buf.remaining() * m);
long k; long k;
while (buf.remaining() >= 8) { while (buf.remaining() >= 8) {
k = buf.getLong(); k = buf.getLong();
k *= m; k *= m;
k ^= k >>> r; k ^= k >>> r;
k *= m; k *= m;
h ^= k; h ^= k;
h *= m; h *= m;
} }
if (buf.remaining() > 0) { if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order( ByteBuffer finish = ByteBuffer.allocate(8).order(
ByteOrder.LITTLE_ENDIAN); ByteOrder.LITTLE_ENDIAN);
// for big-endian version, do this first: // for big-endian version, do this first:
// finish.position(8-buf.remaining()); // finish.position(8-buf.remaining());
finish.put(buf).rewind(); finish.put(buf).rewind();
h ^= finish.getLong(); h ^= finish.getLong();
h *= m; h *= m;
} }
h ^= h >>> r; h ^= h >>> r;
h *= m; h *= m;
h ^= h >>> r; h ^= h >>> r;
buf.order(byteOrder); buf.order(byteOrder);
return h; return h;
} }
public long hash(byte[] key) { public long hash(byte[] key) {
return hash64A(key, 0x1234ABCD); return hash64A(key, 0x1234ABCD);
} }
public long hash(String key) { public long hash(String key) {
return hash(key.getBytes(Protocol.UTF8)); return hash(SafeEncoder.encode(key));
} }
} }

View File

@@ -0,0 +1,28 @@
package redis.clients.util;
import java.io.UnsupportedEncodingException;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.Protocol;
/**
* The only reason to have this is to be able to compatible with java 1.5 :(
*
*/
public class SafeEncoder {
public static byte[] encode(final String str) {
try {
return str.getBytes(Protocol.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new JedisException(e);
}
}
public static String encode(final byte[] data) {
try {
return new String(data, Protocol.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new JedisException(e);
}
}
}

View File

@@ -2,8 +2,10 @@ package redis.clients.util;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -22,69 +24,73 @@ public class Sharded<R, S extends ShardInfo<R>> {
private Pattern tagPattern = null; private Pattern tagPattern = null;
// the tag is anything between {} // the tag is anything between {}
public static final Pattern DEFAULT_KEY_TAG_PATTERN = Pattern public static final Pattern DEFAULT_KEY_TAG_PATTERN = Pattern
.compile("\\{(.+?)\\}"); .compile("\\{(.+?)\\}");
public Sharded(List<S> shards) { public Sharded(List<S> shards) {
this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works
// with 64-bits not 128 // with 64-bits not 128
} }
public Sharded(List<S> shards, Hashing algo) { public Sharded(List<S> shards, Hashing algo) {
this.algo = algo; this.algo = algo;
initialize(shards); initialize(shards);
} }
public Sharded(List<S> shards, Pattern tagPattern) { public Sharded(List<S> shards, Pattern tagPattern) {
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
// as we works with // as we works with
// 64-bits not 128 // 64-bits not 128
} }
public Sharded(List<S> shards, Hashing algo, Pattern tagPattern) { public Sharded(List<S> shards, Hashing algo, Pattern tagPattern) {
this.algo = algo; this.algo = algo;
this.tagPattern = tagPattern; this.tagPattern = tagPattern;
initialize(shards); initialize(shards);
} }
private void initialize(List<S> shards) { private void initialize(List<S> shards) {
nodes = new TreeMap<Long, S>(); nodes = new TreeMap<Long, S>();
int totalWeight = 0; int totalWeight = 0;
for (ShardInfo<?> shard : shards) { for (ShardInfo<?> shard : shards) {
totalWeight += shard.getWeight(); totalWeight += shard.getWeight();
} }
long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize
// math in Long // math in Long
long floor = Long.MIN_VALUE; long floor = Long.MIN_VALUE;
for (int i = 0; i != shards.size(); ++i) { for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i); final S shardInfo = shards.get(i);
shardInfo.initResource(); shardInfo.initResource();
nodes.put(floor, shardInfo); nodes.put(floor, shardInfo);
floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to
// compensate // compensate
// 62 vs 64 // 62 vs 64
} }
} }
public R getShard(byte[] key) { public R getShard(byte[] key) {
return nodes return getShardInfo(key).getResource();
.floorEntry(algo.hash(key)) }
.getValue()
.getResource();
}
public R getShard(String key) { public R getShard(String key) {
return nodes return getShardInfo(key).getResource();
.floorEntry(algo.hash(getKeyTag(key))) }
.getValue()
.getResource(); private S getShardInfo(byte[] key) {
} Iterator<Entry<Long, S>> iterator = nodes.headMap(algo.hash(key))
.entrySet().iterator();
Entry<Long, S> next = iterator.next();
if (iterator.hasNext()) {
next = iterator.next();
}
return next.getValue();
}
public S getShardInfo(String key) { public S getShardInfo(String key) {
return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue(); return getShardInfo(SafeEncoder.encode(getKeyTag(key)));
} }
/** /**
@@ -97,15 +103,15 @@ public class Sharded<R, S extends ShardInfo<R>> {
* @return The tag if it exists, or the original key * @return The tag if it exists, or the original key
*/ */
public String getKeyTag(String key) { public String getKeyTag(String key) {
if (tagPattern != null) { if (tagPattern != null) {
Matcher m = tagPattern.matcher(key); Matcher m = tagPattern.matcher(key);
if (m.find()) if (m.find())
return m.group(1); return m.group(1);
} }
return key; return key;
} }
public Collection<S> getAllShards() { public Collection<S> getAllShards() {
return Collections.unmodifiableCollection(nodes.values()); return Collections.unmodifiableCollection(nodes.values());
} }
} }

View File

@@ -9,6 +9,7 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase; import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.util.SafeEncoder;
public class JedisTest extends JedisCommandTestBase { public class JedisTest extends JedisCommandTestBase {
@Test @Test
@@ -25,7 +26,7 @@ public class JedisTest extends JedisCommandTestBase {
bigdata[b] = (byte) ((byte) b % 255); bigdata[b] = (byte) ((byte) b % 255);
} }
Map<String, String> hash = new HashMap<String, String>(); Map<String, String> hash = new HashMap<String, String>();
hash.put("data", new String(bigdata, Protocol.UTF8)); hash.put("data", SafeEncoder.encode(bigdata));
String status = jedis.hmset("foo", hash); String status = jedis.hmset("foo", hash);
assertEquals("OK", status); assertEquals("OK", status);

View File

@@ -37,7 +37,7 @@ public class PipeliningTest extends Assert {
}); });
assertEquals(2, results.size()); assertEquals(2, results.size());
assertArrayEquals("OK".getBytes(Protocol.UTF8), (byte[])results.get(0)); assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[])results.get(0));
assertArrayEquals("bar".getBytes(Protocol.UTF8), (byte[])results.get(1)); assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[])results.get(1));
} }
} }

View File

@@ -14,6 +14,7 @@ import org.junit.Test;
import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol;
import redis.clients.util.RedisInputStream; import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream; import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
public class ProtocolTest extends JedisTestBase { public class ProtocolTest extends JedisTestBase {
@Test @Test
@@ -24,7 +25,7 @@ public class ProtocolTest extends JedisTestBase {
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
protocol.sendCommand(new RedisOutputStream(pos), Protocol.Command.GET, protocol.sendCommand(new RedisOutputStream(pos), Protocol.Command.GET,
"SOMEKEY".getBytes(Protocol.UTF8)); "SOMEKEY".getBytes(Protocol.CHARSET));
pos.close(); pos.close();
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n"; String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
@@ -43,7 +44,7 @@ public class ProtocolTest extends JedisTestBase {
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes()); InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
assertArrayEquals("foobar".getBytes(Protocol.UTF8), response); assertArrayEquals(SafeEncoder.encode("foobar"), response);
} }
@Test @Test
@@ -52,8 +53,8 @@ public class ProtocolTest extends JedisTestBase {
"$30\r\n012345678901234567890123456789\r\n".getBytes()); "$30\r\n012345678901234567890123456789\r\n".getBytes());
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
byte[] response = (byte[]) protocol.read(new RedisInputStream(fis)); byte[] response = (byte[]) protocol.read(new RedisInputStream(fis));
assertArrayEquals("012345678901234567890123456789" assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"),
.getBytes(Protocol.UTF8), response); response);
} }
@Test @Test
@@ -69,7 +70,7 @@ public class ProtocolTest extends JedisTestBase {
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
assertArrayEquals("OK".getBytes(Protocol.UTF8), response); assertArrayEquals(SafeEncoder.encode("OK"), response);
} }
@Test @Test
@@ -90,10 +91,10 @@ public class ProtocolTest extends JedisTestBase {
List<byte[]> response = (List<byte[]>) protocol List<byte[]> response = (List<byte[]>) protocol
.read(new RedisInputStream(is)); .read(new RedisInputStream(is));
List<byte[]> expected = new ArrayList<byte[]>(); List<byte[]> expected = new ArrayList<byte[]>();
expected.add("foo".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("foo"));
expected.add("bar".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("bar"));
expected.add("Hello".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("Hello"));
expected.add("World".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("World"));
assertEquals(expected, response); assertEquals(expected, response);
} }

View File

@@ -9,11 +9,11 @@ import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.Hashing; import redis.clients.util.Hashing;
import redis.clients.util.SafeEncoder;
public class ShardedJedisTest extends Assert { public class ShardedJedisTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers() private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
@@ -143,11 +143,11 @@ public class ShardedJedisTest extends Assert {
}); });
List<Object> expected = new ArrayList<Object>(2); List<Object> expected = new ArrayList<Object>(2);
expected.add("a".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("a"));
expected.add("b".getBytes(Protocol.UTF8)); expected.add(SafeEncoder.encode("b"));
assertEquals(2, results.size()); assertEquals(2, results.size());
assertArrayEquals("a".getBytes(Protocol.UTF8), (byte[]) results.get(0)); assertArrayEquals(SafeEncoder.encode("a"), (byte[]) results.get(0));
assertArrayEquals("b".getBytes(Protocol.UTF8), (byte[]) results.get(1)); assertArrayEquals(SafeEncoder.encode("b"), (byte[]) results.get(1));
} }
} }

View File

@@ -7,7 +7,7 @@ import java.util.Set;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.JedisException; import redis.clients.jedis.JedisException;
import redis.clients.jedis.Protocol; import redis.clients.util.SafeEncoder;
public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
@@ -447,8 +447,8 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
assertEquals("hello world", result); assertEquals("hello world", result);
// Binary // Binary
byte[] bresult = jedis.echo("hello world".getBytes(Protocol.UTF8)); byte[] bresult = jedis.echo(SafeEncoder.encode("hello world"));
assertArrayEquals("hello world".getBytes(Protocol.UTF8), bresult); assertArrayEquals(SafeEncoder.encode("hello world"), bresult);
} }
} }

View File

@@ -5,9 +5,9 @@ import java.util.Set;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.Tuple; import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams; import redis.clients.jedis.ZParams;
import redis.clients.util.SafeEncoder;
public class SortedSetCommandsTest extends JedisCommandTestBase { public class SortedSetCommandsTest extends JedisCommandTestBase {
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
@@ -359,7 +359,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
bscore = jedis.zscore(bfoo, bc); bscore = jedis.zscore(bfoo, bc);
assertEquals((Double) 0.1d, bscore); assertEquals((Double) 0.1d, bscore);
bscore = jedis.zscore(bfoo, "s".getBytes(Protocol.UTF8)); bscore = jedis.zscore(bfoo, SafeEncoder.encode("s"));
assertNull(bscore); assertNull(bscore);
} }
@@ -440,8 +440,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
assertEquals(bexpected, brange); assertEquals(bexpected, brange);
brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1); brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1);
Set<byte[]> brange2 = jedis.zrangeByScore(bfoo, "-inf" Set<byte[]> brange2 = jedis.zrangeByScore(bfoo, SafeEncoder
.getBytes(Protocol.UTF8), "(2".getBytes(Protocol.UTF8)); .encode("-inf"), SafeEncoder.encode("(2"));
assertEquals(bexpected, brange2); assertEquals(bexpected, brange2);
bexpected = new LinkedHashSet<byte[]>(); bexpected = new LinkedHashSet<byte[]>();
@@ -601,8 +601,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
jedis.zadd(bbar, 2, ba); jedis.zadd(bbar, 2, ba);
jedis.zadd(bbar, 2, bb); jedis.zadd(bbar, 2, bb);
int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bfoo, int bresult = jedis.zunionstore(SafeEncoder.encode("dst"), bfoo, bbar);
bbar);
assertEquals(2, bresult); assertEquals(2, bresult);
@@ -610,8 +609,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
bexpected.add(new Tuple(bb, new Double(4))); bexpected.add(new Tuple(bb, new Double(4)));
bexpected.add(new Tuple(ba, new Double(3))); bexpected.add(new Tuple(ba, new Double(3)));
assertEquals(bexpected, jedis.zrangeWithScores("dst" assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder
.getBytes(Protocol.UTF8), 0, 100)); .encode("dst"), 0, 100));
} }
@Test @Test
@@ -643,7 +642,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
ZParams bparams = new ZParams(); ZParams bparams = new ZParams();
bparams.weights(2, 2); bparams.weights(2, 2);
bparams.aggregate(ZParams.Aggregate.SUM); bparams.aggregate(ZParams.Aggregate.SUM);
int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bparams, int bresult = jedis.zunionstore(SafeEncoder.encode("dst"), bparams,
bfoo, bbar); bfoo, bbar);
assertEquals(2, bresult); assertEquals(2, bresult);
@@ -652,8 +651,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
bexpected.add(new Tuple(bb, new Double(8))); bexpected.add(new Tuple(bb, new Double(8)));
bexpected.add(new Tuple(ba, new Double(6))); bexpected.add(new Tuple(ba, new Double(6)));
assertEquals(bexpected, jedis.zrangeWithScores("dst" assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder
.getBytes(Protocol.UTF8), 0, 100)); .encode("dst"), 0, 100));
} }
@Test @Test
@@ -676,16 +675,15 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
jedis.zadd(bfoo, 2, bb); jedis.zadd(bfoo, 2, bb);
jedis.zadd(bbar, 2, ba); jedis.zadd(bbar, 2, ba);
int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bfoo, int bresult = jedis.zinterstore(SafeEncoder.encode("dst"), bfoo, bbar);
bbar);
assertEquals(1, bresult); assertEquals(1, bresult);
Set<Tuple> bexpected = new LinkedHashSet<Tuple>(); Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
bexpected.add(new Tuple(ba, new Double(3))); bexpected.add(new Tuple(ba, new Double(3)));
assertEquals(bexpected, jedis.zrangeWithScores("dst" assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder
.getBytes(Protocol.UTF8), 0, 100)); .encode("dst"), 0, 100));
} }
@Test @Test
@@ -714,7 +712,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
ZParams bparams = new ZParams(); ZParams bparams = new ZParams();
bparams.weights(2, 2); bparams.weights(2, 2);
bparams.aggregate(ZParams.Aggregate.SUM); bparams.aggregate(ZParams.Aggregate.SUM);
int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bparams, int bresult = jedis.zinterstore(SafeEncoder.encode("dst"), bparams,
bfoo, bbar); bfoo, bbar);
assertEquals(1, bresult); assertEquals(1, bresult);
@@ -722,7 +720,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
Set<Tuple> bexpected = new LinkedHashSet<Tuple>(); Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
bexpected.add(new Tuple(ba, new Double(6))); bexpected.add(new Tuple(ba, new Double(6)));
assertEquals(bexpected, jedis.zrangeWithScores("dst" assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder
.getBytes(Protocol.UTF8), 0, 100)); .encode("dst"), 0, 100));
} }
} }

View File

@@ -169,7 +169,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
t.set("mykey", val); t.set("mykey", val);
List<Object> resp = t.exec(); List<Object> resp = t.exec();
assertEquals(1, resp.size()); assertEquals(1, resp.size());
assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8), assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET),
(byte[]) resp.get(0)); (byte[]) resp.get(0));
// Binary // Binary
@@ -188,7 +188,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
t.set(bmykey, bval); t.set(bmykey, bval);
resp = t.exec(); resp = t.exec();
assertEquals(1, resp.size()); assertEquals(1, resp.size());
assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8), assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET),
(byte[]) resp.get(0)); (byte[]) resp.get(0));
} }