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

@@ -14,6 +14,7 @@ import java.util.Map;
import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.util.SafeEncoder;
public class BinaryClient extends Connection {
public enum LIST_POSITION {
@@ -21,7 +22,7 @@ public class BinaryClient extends Connection {
public final byte[] raw;
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.util.JedisByteHashMap;
import redis.clients.util.SafeEncoder;
public class BinaryJedis implements BinaryJedisCommands {
protected Client client = null;
@@ -2317,8 +2318,8 @@ public class BinaryJedis implements BinaryJedisCommands {
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<byte[]> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set.add(new Tuple(iterator.next(), Double.valueOf(new String(
iterator.next(), Protocol.UTF8))));
set.add(new Tuple(iterator.next(), Double.valueOf(SafeEncoder
.encode(iterator.next()))));
}
return set;
}

View File

@@ -4,6 +4,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import redis.clients.util.SafeEncoder;
public class Client extends BinaryClient {
public Client(final String host) {
super(host);
@@ -14,82 +16,81 @@ public class Client extends BinaryClient {
}
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) {
get(key.getBytes(Protocol.UTF8));
get(SafeEncoder.encode(key));
}
public void exists(final String key) {
exists(key.getBytes(Protocol.UTF8));
exists(SafeEncoder.encode(key));
}
public void del(final String... keys) {
final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < keys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8);
bkeys[i] = SafeEncoder.encode(keys[i]);
}
del(bkeys);
}
public void type(final String key) {
type(key.getBytes(Protocol.UTF8));
type(SafeEncoder.encode(key));
}
public void keys(final String pattern) {
keys(pattern.getBytes(Protocol.UTF8));
keys(SafeEncoder.encode(pattern));
}
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) {
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) {
expire(key.getBytes(Protocol.UTF8), seconds);
expire(SafeEncoder.encode(key), seconds);
}
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) {
ttl(key.getBytes(Protocol.UTF8));
ttl(SafeEncoder.encode(key));
}
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) {
getSet(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8));
getSet(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
public void mget(final String... keys) {
final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8);
bkeys[i] = SafeEncoder.encode(keys[i]);
}
mget(bkeys);
}
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) {
setex(key.getBytes(Protocol.UTF8), seconds, value
.getBytes(Protocol.UTF8));
setex(SafeEncoder.encode(key), seconds, SafeEncoder.encode(value));
}
public void mset(final String... keysvalues) {
final byte[][] bkeysvalues = new byte[keysvalues.length][];
for (int i = 0; i < keysvalues.length; i++) {
bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8);
bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]);
}
mset(bkeysvalues);
}
@@ -97,175 +98,173 @@ public class Client extends BinaryClient {
public void msetnx(final String... keysvalues) {
final byte[][] bkeysvalues = new byte[keysvalues.length][];
for (int i = 0; i < keysvalues.length; i++) {
bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8);
bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]);
}
msetnx(bkeysvalues);
}
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) {
decr(key.getBytes(Protocol.UTF8));
decr(SafeEncoder.encode(key));
}
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) {
incr(key.getBytes(Protocol.UTF8));
incr(SafeEncoder.encode(key));
}
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) {
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) {
hset(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), value
.getBytes(Protocol.UTF8));
hset(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder
.encode(value));
}
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) {
hsetnx(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8),
value.getBytes(Protocol.UTF8));
hsetnx(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder
.encode(value));
}
public void hmset(final String key, final Map<String, String> hash) {
final Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>(hash
.size());
for (final Entry<String, String> entry : hash.entrySet()) {
bhash.put(entry.getKey().getBytes(Protocol.UTF8), entry.getValue()
.getBytes(Protocol.UTF8));
bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder
.encode(entry.getValue()));
}
hmset(key.getBytes(Protocol.UTF8), bhash);
hmset(SafeEncoder.encode(key), bhash);
}
public void hmget(final String key, final String... fields) {
final byte[][] bfields = new byte[fields.length][];
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) {
hincrBy(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8),
value);
hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value);
}
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) {
hdel(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8));
hdel(SafeEncoder.encode(key), SafeEncoder.encode(field));
}
public void hlen(final String key) {
hlen(key.getBytes(Protocol.UTF8));
hlen(SafeEncoder.encode(key));
}
public void hkeys(final String key) {
hkeys(key.getBytes(Protocol.UTF8));
hkeys(SafeEncoder.encode(key));
}
public void hvals(final String key) {
hvals(key.getBytes(Protocol.UTF8));
hvals(SafeEncoder.encode(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) {
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) {
lpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8));
lpush(SafeEncoder.encode(key), SafeEncoder.encode(string));
}
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) {
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) {
ltrim(key.getBytes(Protocol.UTF8), start, end);
ltrim(SafeEncoder.encode(key), start, end);
}
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) {
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) {
lrem(key.getBytes(Protocol.UTF8), count, value.getBytes(Protocol.UTF8));
lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value));
}
public void lpop(final String key) {
lpop(key.getBytes(Protocol.UTF8));
lpop(SafeEncoder.encode(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) {
rpoplpush(srckey.getBytes(Protocol.UTF8), dstkey
.getBytes(Protocol.UTF8));
rpoplpush(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey));
}
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) {
smembers(key.getBytes(Protocol.UTF8));
smembers(SafeEncoder.encode(key));
}
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) {
spop(key.getBytes(Protocol.UTF8));
spop(SafeEncoder.encode(key));
}
public void smove(final String srckey, final String dstkey,
final String member) {
smove(srckey.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8),
member.getBytes(Protocol.UTF8));
smove(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey),
SafeEncoder.encode(member));
}
public void scard(final String key) {
scard(key.getBytes(Protocol.UTF8));
scard(SafeEncoder.encode(key));
}
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) {
final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8);
bkeys[i] = SafeEncoder.encode(keys[i]);
}
sinter(bkeys);
}
@@ -273,15 +272,15 @@ public class Client extends BinaryClient {
public void sinterstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][];
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) {
final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8);
bkeys[i] = SafeEncoder.encode(keys[i]);
}
sunion(bkeys);
}
@@ -289,15 +288,15 @@ public class Client extends BinaryClient {
public void sunionstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][];
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) {
final byte[][] bkeys = new byte[keys.length][];
for (int i = 0; i < bkeys.length; i++) {
bkeys[i] = keys[i].getBytes(Protocol.UTF8);
bkeys[i] = SafeEncoder.encode(keys[i]);
}
sdiff(bkeys);
}
@@ -305,198 +304,197 @@ public class Client extends BinaryClient {
public void sdiffstore(final String dstkey, final String... keys) {
final byte[][] bkeys = new byte[keys.length][];
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) {
srandmember(key.getBytes(Protocol.UTF8));
srandmember(SafeEncoder.encode(key));
}
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) {
zrange(key.getBytes(Protocol.UTF8), start, end);
zrange(SafeEncoder.encode(key), start, end);
}
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,
final String member) {
zincrby(key.getBytes(Protocol.UTF8), score, member
.getBytes(Protocol.UTF8));
zincrby(SafeEncoder.encode(key), score, SafeEncoder.encode(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) {
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) {
zrevrange(key.getBytes(Protocol.UTF8), start, end);
zrevrange(SafeEncoder.encode(key), start, end);
}
public void zrangeWithScores(final String key, final int start,
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,
final int end) {
zrevrangeWithScores(key.getBytes(Protocol.UTF8), start, end);
zrevrangeWithScores(SafeEncoder.encode(key), start, end);
}
public void zcard(final String key) {
zcard(key.getBytes(Protocol.UTF8));
zcard(SafeEncoder.encode(key));
}
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) {
watch(key.getBytes(Protocol.UTF8));
watch(SafeEncoder.encode(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) {
sort(key.getBytes(Protocol.UTF8), sortingParameters);
sort(SafeEncoder.encode(key), sortingParameters);
}
public void blpop(final String[] args) {
final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < bargs.length; i++) {
bargs[i] = args[i].getBytes(Protocol.UTF8);
bargs[i] = SafeEncoder.encode(args[i]);
}
blpop(bargs);
}
public void sort(final String key, final SortingParams sortingParameters,
final String dstkey) {
sort(key.getBytes(Protocol.UTF8), sortingParameters, dstkey
.getBytes(Protocol.UTF8));
sort(SafeEncoder.encode(key), sortingParameters, SafeEncoder
.encode(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) {
final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < bargs.length; i++) {
bargs[i] = args[i].getBytes(Protocol.UTF8);
bargs[i] = SafeEncoder.encode(args[i]);
}
brpop(bargs);
}
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,
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,
final String max) {
zrangeByScore(key.getBytes(Protocol.UTF8), min.getBytes(Protocol.UTF8),
max.getBytes(Protocol.UTF8));
zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min),
SafeEncoder.encode(max));
}
public void zrangeByScore(final String key, final double min,
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,
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,
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);
}
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,
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) {
final byte[][] bsets = new byte[sets.length][];
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,
final String... sets) {
final byte[][] bsets = new byte[sets.length][];
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) {
final byte[][] bsets = new byte[sets.length][];
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,
final String... sets) {
final byte[][] bsets = new byte[sets.length][];
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) {
strlen(key.getBytes(Protocol.UTF8));
strlen(SafeEncoder.encode(key));
}
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) {
persist(key.getBytes(Protocol.UTF8));
persist(SafeEncoder.encode(key));
}
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) {
echo(string.getBytes(Protocol.UTF8));
echo(SafeEncoder.encode(string));
}
public void linsert(final String key, final LIST_POSITION where,
final String pivot, final String value) {
linsert(key.getBytes(Protocol.UTF8), where, pivot
.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8));
linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot),
SafeEncoder.encode(value));
}
}

View File

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

View File

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

View File

@@ -1,19 +1,19 @@
package redis.clients.jedis;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
public final class Protocol {
public static final int DEFAULT_PORT = 6379;
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 ASTERISK_BYTE = '*';
@@ -76,7 +76,7 @@ public final class Protocol {
}
private byte[] processStatusCodeReply(final RedisInputStream is) {
return is.readLine().getBytes(UTF8);
return SafeEncoder.encode(is.readLine());
}
private byte[] processBulkReply(final RedisInputStream is) {
@@ -122,15 +122,15 @@ public final class Protocol {
}
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) {
return String.valueOf(value).getBytes(Protocol.UTF8);
return SafeEncoder.encode(String.valueOf(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 {
@@ -139,7 +139,7 @@ public final class Protocol {
public final byte[] raw;
Command() {
raw = this.name().getBytes(UTF8);
raw = SafeEncoder.encode(this.name());
}
}
@@ -148,7 +148,7 @@ public final class Protocol {
public final byte[] raw;
Keyword() {
raw = this.name().toLowerCase().getBytes(UTF8);
raw = SafeEncoder.encode(this.name().toLowerCase());
}
}

View File

@@ -1,10 +1,20 @@
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.Collection;
import java.util.Collections;
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.
*
@@ -26,7 +36,7 @@ public class SortingParams {
* @return the SortingParams Object
*/
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
*/
public SortingParams by(final byte[] pattern) {
params.add(BY.raw);
params.add(pattern);
return this;
params.add(BY.raw);
params.add(pattern);
return this;
}
/**
@@ -57,13 +67,13 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams nosort() {
params.add(BY.raw);
params.add(NOSORT.raw);
return this;
params.add(BY.raw);
params.add(NOSORT.raw);
return this;
}
public Collection<byte[]> getParams() {
return Collections.unmodifiableCollection(params);
return Collections.unmodifiableCollection(params);
}
/**
@@ -72,8 +82,8 @@ public class SortingParams {
* @return the sortingParams Object
*/
public SortingParams desc() {
params.add(DESC.raw);
return this;
params.add(DESC.raw);
return this;
}
/**
@@ -82,8 +92,8 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams asc() {
params.add(ASC.raw);
return this;
params.add(ASC.raw);
return this;
}
/**
@@ -95,10 +105,10 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams limit(final int start, final int count) {
params.add(LIMIT.raw);
params.add(Protocol.toByteArray(start));
params.add(Protocol.toByteArray(count));
return this;
params.add(LIMIT.raw);
params.add(Protocol.toByteArray(start));
params.add(Protocol.toByteArray(count));
return this;
}
/**
@@ -108,8 +118,8 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams alpha() {
params.add(ALPHA.raw);
return this;
params.add(ALPHA.raw);
return this;
}
/**
@@ -128,11 +138,11 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams get(String... patterns) {
for (final String pattern : patterns) {
params.add(GET.raw);
params.add(pattern.getBytes(Protocol.UTF8));
}
return this;
for (final String pattern : patterns) {
params.add(GET.raw);
params.add(SafeEncoder.encode(pattern));
}
return this;
}
/**
@@ -151,10 +161,10 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams get(byte[]... patterns) {
for (final byte[] pattern : patterns) {
params.add(GET.raw);
params.add(pattern);
}
return this;
for (final byte[] pattern : patterns) {
params.add(GET.raw);
params.add(pattern);
}
return this;
}
}

View File

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

View File

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

View File

@@ -3,38 +3,37 @@ package redis.clients.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import redis.clients.jedis.Protocol;
public interface Hashing {
public static final Hashing MURMUR_HASH = new MurmurHash();
public static final Hashing MD5 = new Hashing() {
private MessageDigest md5 = null; // avoid recurring construction
public long hash(String key) {
return hash(key.getBytes(Protocol.UTF8));
}
private MessageDigest md5 = null; // avoid recurring construction
public long hash(byte[] key) {
if (md5 == null) {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"++++ no md5 algorythm found");
}
}
public long hash(String key) {
return hash(SafeEncoder.encode(key));
}
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(byte[] key) {
if (md5 == null) {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"++++ 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(byte[] key);
}

View File

@@ -20,8 +20,6 @@ package redis.clients.util;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import redis.clients.jedis.Protocol;
/**
* This is a very fast, non-cryptographic hash suitable for general hash-based
* 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.
*/
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.
*/
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.
*/
public static int hash(ByteBuffer buf, int seed) {
// save byte order for later restoration
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
// save byte order for later restoration
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
int m = 0x5bd1e995;
int r = 24;
int m = 0x5bd1e995;
int r = 24;
int h = seed ^ buf.remaining();
int h = seed ^ buf.remaining();
int k;
while (buf.remaining() >= 4) {
k = buf.getInt();
int k;
while (buf.remaining() >= 4) {
k = buf.getInt();
k *= m;
k ^= k >>> r;
k *= m;
k *= m;
k ^= k >>> r;
k *= m;
h *= m;
h ^= k;
}
h *= m;
h ^= k;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(4).order(
ByteOrder.LITTLE_ENDIAN);
// for big-endian version, use this first:
// finish.position(4-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getInt();
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(4).order(
ByteOrder.LITTLE_ENDIAN);
// for big-endian version, use this first:
// finish.position(4-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getInt();
h *= m;
}
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
buf.order(byteOrder);
return h;
buf.order(byteOrder);
return h;
}
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) {
return hash64A(ByteBuffer.wrap(data, offset, length), seed);
return hash64A(ByteBuffer.wrap(data, offset, length), seed);
}
public static long hash64A(ByteBuffer buf, int seed) {
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L;
int r = 47;
long m = 0xc6a4a7935bd1e995L;
int r = 47;
long h = seed ^ (buf.remaining() * m);
long h = seed ^ (buf.remaining() * m);
long k;
while (buf.remaining() >= 8) {
k = buf.getLong();
long k;
while (buf.remaining() >= 8) {
k = buf.getLong();
k *= m;
k ^= k >>> r;
k *= m;
k *= m;
k ^= k >>> r;
k *= m;
h ^= k;
h *= m;
}
h ^= k;
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order(
ByteOrder.LITTLE_ENDIAN);
// for big-endian version, do this first:
// finish.position(8-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order(
ByteOrder.LITTLE_ENDIAN);
// for big-endian version, do this first:
// finish.position(8-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
h ^= h >>> r;
h *= m;
h ^= h >>> r;
h ^= h >>> r;
h *= m;
h ^= h >>> r;
buf.order(byteOrder);
return h;
buf.order(byteOrder);
return h;
}
public long hash(byte[] key) {
return hash64A(key, 0x1234ABCD);
return hash64A(key, 0x1234ABCD);
}
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.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -22,69 +24,73 @@ public class Sharded<R, S extends ShardInfo<R>> {
private Pattern tagPattern = null;
// the tag is anything between {}
public static final Pattern DEFAULT_KEY_TAG_PATTERN = Pattern
.compile("\\{(.+?)\\}");
.compile("\\{(.+?)\\}");
public Sharded(List<S> shards) {
this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works
// with 64-bits not 128
this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works
// with 64-bits not 128
}
public Sharded(List<S> shards, Hashing algo) {
this.algo = algo;
initialize(shards);
this.algo = algo;
initialize(shards);
}
public Sharded(List<S> shards, Pattern tagPattern) {
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
// as we works with
// 64-bits not 128
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
// as we works with
// 64-bits not 128
}
public Sharded(List<S> shards, Hashing algo, Pattern tagPattern) {
this.algo = algo;
this.tagPattern = tagPattern;
initialize(shards);
this.algo = algo;
this.tagPattern = tagPattern;
initialize(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) {
totalWeight += shard.getWeight();
}
for (ShardInfo<?> shard : shards) {
totalWeight += shard.getWeight();
}
long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize
// math in Long
long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize
// math in Long
long floor = Long.MIN_VALUE;
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
shardInfo.initResource();
nodes.put(floor, shardInfo);
floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to
// compensate
// 62 vs 64
}
long floor = Long.MIN_VALUE;
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
shardInfo.initResource();
nodes.put(floor, shardInfo);
floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to
// compensate
// 62 vs 64
}
}
public R getShard(byte[] key) {
return nodes
.floorEntry(algo.hash(key))
.getValue()
.getResource();
}
public R getShard(byte[] key) {
return getShardInfo(key).getResource();
}
public R getShard(String key) {
return nodes
.floorEntry(algo.hash(getKeyTag(key)))
.getValue()
.getResource();
}
public R getShard(String key) {
return getShardInfo(key).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) {
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
*/
public String getKeyTag(String key) {
if (tagPattern != null) {
Matcher m = tagPattern.matcher(key);
if (m.find())
return m.group(1);
}
return key;
if (tagPattern != null) {
Matcher m = tagPattern.matcher(key);
if (m.find())
return m.group(1);
}
return key;
}
public Collection<S> getAllShards() {
return Collections.unmodifiableCollection(nodes.values());
return Collections.unmodifiableCollection(nodes.values());
}
}