Merge branch 'binaryAPI' of git://github.com/yaourt/jedis
Conflicts: src/main/java/redis/clients/jedis/Connection.java src/main/java/redis/clients/jedis/Jedis.java src/main/java/redis/clients/jedis/ShardedJedis.java
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -54,7 +54,7 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.1</version>
|
||||
<type>jar</type>
|
||||
<scope>compile</scope>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
629
src/main/java/redis/clients/jedis/BinaryClient.java
Normal file
629
src/main/java/redis/clients/jedis/BinaryClient.java
Normal file
@@ -0,0 +1,629 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import static redis.clients.jedis.Protocol.toByteArray;
|
||||
import static redis.clients.jedis.Protocol.Command.*;
|
||||
import static redis.clients.jedis.Protocol.Keyword.LIMIT;
|
||||
import static redis.clients.jedis.Protocol.Keyword.NO;
|
||||
import static redis.clients.jedis.Protocol.Keyword.ONE;
|
||||
import static redis.clients.jedis.Protocol.Keyword.STORE;
|
||||
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import redis.clients.jedis.Protocol.Command;
|
||||
import redis.clients.jedis.Protocol.Keyword;
|
||||
|
||||
public class BinaryClient extends Connection {
|
||||
public enum LIST_POSITION {
|
||||
BEFORE, AFTER;
|
||||
public final byte[] raw;
|
||||
|
||||
private LIST_POSITION() {
|
||||
raw = name().getBytes(Protocol.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInMulti;
|
||||
|
||||
public boolean isInMulti() {
|
||||
return isInMulti;
|
||||
}
|
||||
|
||||
public BinaryClient(final String host) {
|
||||
super(host);
|
||||
}
|
||||
|
||||
public BinaryClient(final String host, final int port) {
|
||||
super(host, port);
|
||||
}
|
||||
|
||||
public void ping() {
|
||||
sendCommand(PING);
|
||||
}
|
||||
|
||||
public void set(final byte[] key, final byte[] value) {
|
||||
sendCommand(Command.SET, key, value);
|
||||
}
|
||||
|
||||
public void get(final byte[] key) {
|
||||
sendCommand(Command.GET, key);
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
sendCommand(QUIT);
|
||||
}
|
||||
|
||||
public void exists(final byte[] key) {
|
||||
sendCommand(EXISTS, key);
|
||||
}
|
||||
|
||||
public void del(final byte[]... keys) {
|
||||
sendCommand(DEL, keys);
|
||||
}
|
||||
|
||||
public void type(final byte[] key) {
|
||||
sendCommand(TYPE, key);
|
||||
}
|
||||
|
||||
public void flushDB() {
|
||||
sendCommand(FLUSHDB);
|
||||
}
|
||||
|
||||
public void keys(final byte[] pattern) {
|
||||
sendCommand(KEYS, pattern);
|
||||
}
|
||||
|
||||
public void randomKey() {
|
||||
sendCommand(RANDOMKEY);
|
||||
}
|
||||
|
||||
public void rename(final byte[] oldkey, final byte[] newkey) {
|
||||
sendCommand(RENAME, oldkey, newkey);
|
||||
}
|
||||
|
||||
public void renamenx(final byte[] oldkey, final byte[] newkey) {
|
||||
sendCommand(RENAMENX, oldkey, newkey);
|
||||
}
|
||||
|
||||
public void dbSize() {
|
||||
sendCommand(DBSIZE);
|
||||
}
|
||||
|
||||
public void expire(final byte[] key, final int seconds) {
|
||||
sendCommand(EXPIRE, key, toByteArray(seconds));
|
||||
}
|
||||
|
||||
public void expireAt(final byte[] key, final long unixTime) {
|
||||
sendCommand(EXPIREAT, key, toByteArray(unixTime));
|
||||
}
|
||||
|
||||
public void ttl(final byte[] key) {
|
||||
sendCommand(TTL, key);
|
||||
}
|
||||
|
||||
public void select(final int index) {
|
||||
sendCommand(SELECT, toByteArray(index));
|
||||
}
|
||||
|
||||
public void move(final byte[] key, final int dbIndex) {
|
||||
sendCommand(MOVE, key, toByteArray(dbIndex));
|
||||
}
|
||||
|
||||
public void flushAll() {
|
||||
sendCommand(FLUSHALL);
|
||||
}
|
||||
|
||||
public void getSet(final byte[] key, final byte[] value) {
|
||||
sendCommand(GETSET, key, value);
|
||||
}
|
||||
|
||||
public void mget(final byte[]... keys) {
|
||||
sendCommand(MGET, keys);
|
||||
}
|
||||
|
||||
public void setnx(final byte[] key, final byte[] value) {
|
||||
sendCommand(SETNX, key, value);
|
||||
}
|
||||
|
||||
public void setex(final byte[] key, final int seconds, final byte[] value) {
|
||||
sendCommand(SETEX, key, toByteArray(seconds), value);
|
||||
}
|
||||
|
||||
public void mset(final byte[]... keysvalues) {
|
||||
sendCommand(MSET, keysvalues);
|
||||
}
|
||||
|
||||
public void msetnx(final byte[]... keysvalues) {
|
||||
sendCommand(MSETNX, keysvalues);
|
||||
}
|
||||
|
||||
public void decrBy(final byte[] key, final int integer) {
|
||||
sendCommand(DECRBY, key, toByteArray(integer));
|
||||
}
|
||||
|
||||
public void decr(final byte[] key) {
|
||||
sendCommand(DECR, key);
|
||||
}
|
||||
|
||||
public void incrBy(final byte[] key, final int integer) {
|
||||
sendCommand(INCRBY, key, toByteArray(integer));
|
||||
}
|
||||
|
||||
public void incr(final byte[] key) {
|
||||
sendCommand(INCR, key);
|
||||
}
|
||||
|
||||
public void append(final byte[] key, final byte[] value) {
|
||||
sendCommand(APPEND, key, value);
|
||||
}
|
||||
|
||||
public void substr(final byte[] key, final int start, final int end) {
|
||||
sendCommand(SUBSTR, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void hset(final byte[] key, final byte[] field, final byte[] value) {
|
||||
sendCommand(HSET, key, field, value);
|
||||
}
|
||||
|
||||
public void hget(final byte[] key, final byte[] field) {
|
||||
sendCommand(HGET, key, field);
|
||||
}
|
||||
|
||||
public void hsetnx(final byte[] key, final byte[] field, final byte[] value) {
|
||||
sendCommand(HSETNX, key, field, value);
|
||||
}
|
||||
|
||||
public void hmset(final byte[] key, final Map<byte[], byte[]> hash) {
|
||||
final List<byte[]> params = new ArrayList<byte[]>();
|
||||
params.add(key);
|
||||
|
||||
for (final byte[] field : hash.keySet()) {
|
||||
params.add(field);
|
||||
params.add(hash.get(field));
|
||||
}
|
||||
sendCommand(HMSET, params.toArray(new byte[params.size()][]));
|
||||
}
|
||||
|
||||
public void hmget(final byte[] key, final byte[]... fields) {
|
||||
final byte[][] params = new byte[fields.length + 1][];
|
||||
params[0] = key;
|
||||
System.arraycopy(fields, 0, params, 1, fields.length);
|
||||
sendCommand(HMGET, params);
|
||||
}
|
||||
|
||||
public void hincrBy(final byte[] key, final byte[] field, final int value) {
|
||||
sendCommand(HINCRBY, key, field, toByteArray(value));
|
||||
}
|
||||
|
||||
public void hexists(final byte[] key, final byte[] field) {
|
||||
sendCommand(HEXISTS, key, field);
|
||||
}
|
||||
|
||||
public void hdel(final byte[] key, final byte[] field) {
|
||||
sendCommand(HDEL, key, field);
|
||||
}
|
||||
|
||||
public void hlen(final byte[] key) {
|
||||
sendCommand(HLEN, key);
|
||||
}
|
||||
|
||||
public void hkeys(final byte[] key) {
|
||||
sendCommand(HKEYS, key);
|
||||
}
|
||||
|
||||
public void hvals(final byte[] key) {
|
||||
sendCommand(HVALS, key);
|
||||
}
|
||||
|
||||
public void hgetAll(final byte[] key) {
|
||||
sendCommand(HGETALL, key);
|
||||
}
|
||||
|
||||
public void rpush(final byte[] key, final byte[] string) {
|
||||
sendCommand(RPUSH, key, string);
|
||||
}
|
||||
|
||||
public void lpush(final byte[] key, final byte[] string) {
|
||||
sendCommand(LPUSH, key, string);
|
||||
}
|
||||
|
||||
public void llen(final byte[] key) {
|
||||
sendCommand(LLEN, key);
|
||||
}
|
||||
|
||||
public void lrange(final byte[] key, final int start, final int end) {
|
||||
sendCommand(LRANGE, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void ltrim(final byte[] key, final int start, final int end) {
|
||||
sendCommand(LTRIM, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void lindex(final byte[] key, final int index) {
|
||||
sendCommand(LINDEX, key, toByteArray(index));
|
||||
}
|
||||
|
||||
public void lset(final byte[] key, final int index, final byte[] value) {
|
||||
sendCommand(LSET, key, toByteArray(index), value);
|
||||
}
|
||||
|
||||
public void lrem(final byte[] key, int count, final byte[] value) {
|
||||
sendCommand(LREM, key, toByteArray(count), value);
|
||||
}
|
||||
|
||||
public void lpop(final byte[] key) {
|
||||
sendCommand(LPOP, key);
|
||||
}
|
||||
|
||||
public void rpop(final byte[] key) {
|
||||
sendCommand(RPOP, key);
|
||||
}
|
||||
|
||||
public void rpoplpush(final byte[] srckey, final byte[] dstkey) {
|
||||
sendCommand(RPOPLPUSH, srckey, dstkey);
|
||||
}
|
||||
|
||||
public void sadd(final byte[] key, final byte[] member) {
|
||||
sendCommand(SADD, key, member);
|
||||
}
|
||||
|
||||
public void smembers(final byte[] key) {
|
||||
sendCommand(SMEMBERS, key);
|
||||
}
|
||||
|
||||
public void srem(final byte[] key, final byte[] member) {
|
||||
sendCommand(SREM, key, member);
|
||||
}
|
||||
|
||||
public void spop(final byte[] key) {
|
||||
sendCommand(SPOP, key);
|
||||
}
|
||||
|
||||
public void smove(final byte[] srckey, final byte[] dstkey,
|
||||
final byte[] member) {
|
||||
sendCommand(SMOVE, srckey, dstkey, member);
|
||||
}
|
||||
|
||||
public void scard(final byte[] key) {
|
||||
sendCommand(SCARD, key);
|
||||
}
|
||||
|
||||
public void sismember(final byte[] key, final byte[] member) {
|
||||
sendCommand(SISMEMBER, key, member);
|
||||
}
|
||||
|
||||
public void sinter(final byte[]... keys) {
|
||||
sendCommand(SINTER, keys);
|
||||
}
|
||||
|
||||
public void sinterstore(final byte[] dstkey, final byte[]... keys) {
|
||||
final byte[][] params = new byte[keys.length + 1][];
|
||||
params[0] = dstkey;
|
||||
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||
sendCommand(SINTERSTORE, params);
|
||||
}
|
||||
|
||||
public void sunion(final byte[]... keys) {
|
||||
sendCommand(SUNION, keys);
|
||||
}
|
||||
|
||||
public void sunionstore(final byte[] dstkey, final byte[]... keys) {
|
||||
byte[][] params = new byte[keys.length + 1][];
|
||||
params[0] = dstkey;
|
||||
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||
sendCommand(SUNIONSTORE, params);
|
||||
}
|
||||
|
||||
public void sdiff(final byte[]... keys) {
|
||||
sendCommand(SDIFF, keys);
|
||||
}
|
||||
|
||||
public void sdiffstore(final byte[] dstkey, final byte[]... keys) {
|
||||
byte[][] params = new byte[keys.length + 1][];
|
||||
params[0] = dstkey;
|
||||
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||
sendCommand(SDIFFSTORE, params);
|
||||
}
|
||||
|
||||
public void srandmember(final byte[] key) {
|
||||
sendCommand(SRANDMEMBER, key);
|
||||
}
|
||||
|
||||
public void zadd(final byte[] key, final double score, final byte[] member) {
|
||||
sendCommand(ZADD, key, toByteArray(score), member);
|
||||
}
|
||||
|
||||
public void zrange(final byte[] key, final int start, final int end) {
|
||||
sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void zrem(final byte[] key, final byte[] member) {
|
||||
sendCommand(ZREM, key, member);
|
||||
}
|
||||
|
||||
public void zincrby(final byte[] key, final double score,
|
||||
final byte[] member) {
|
||||
sendCommand(ZINCRBY, key, toByteArray(score), member);
|
||||
}
|
||||
|
||||
public void zrank(final byte[] key, final byte[] member) {
|
||||
sendCommand(ZRANK, key, member);
|
||||
}
|
||||
|
||||
public void zrevrank(final byte[] key, final byte[] member) {
|
||||
sendCommand(ZREVRANK, key, member);
|
||||
}
|
||||
|
||||
public void zrevrange(final byte[] key, final int start, final int end) {
|
||||
sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void zrangeWithScores(final byte[] key, final int start,
|
||||
final int end) {
|
||||
sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end),
|
||||
WITHSCORES.raw);
|
||||
}
|
||||
|
||||
public void zrevrangeWithScores(final byte[] key, final int start,
|
||||
final int end) {
|
||||
sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end),
|
||||
WITHSCORES.raw);
|
||||
}
|
||||
|
||||
public void zcard(final byte[] key) {
|
||||
sendCommand(ZCARD, key);
|
||||
}
|
||||
|
||||
public void zscore(final byte[] key, final byte[] member) {
|
||||
sendCommand(ZSCORE, key, member);
|
||||
}
|
||||
|
||||
public void multi() {
|
||||
sendCommand(MULTI);
|
||||
isInMulti = true;
|
||||
}
|
||||
|
||||
public void discard() {
|
||||
sendCommand(DISCARD);
|
||||
isInMulti = false;
|
||||
}
|
||||
|
||||
public void exec() {
|
||||
sendCommand(EXEC);
|
||||
isInMulti = false;
|
||||
}
|
||||
|
||||
public void watch(final byte[] key) {
|
||||
sendCommand(WATCH, key);
|
||||
}
|
||||
|
||||
public void unwatch() {
|
||||
sendCommand(UNWATCH);
|
||||
}
|
||||
|
||||
public void sort(final byte[] key) {
|
||||
sendCommand(SORT, key);
|
||||
}
|
||||
|
||||
public void sort(final byte[] key, final SortingParams sortingParameters) {
|
||||
final List<byte[]> args = new ArrayList<byte[]>();
|
||||
args.add(key);
|
||||
args.addAll(sortingParameters.getParams());
|
||||
sendCommand(SORT, args.toArray(new byte[args.size()][]));
|
||||
}
|
||||
|
||||
public void blpop(final byte[][] args) {
|
||||
sendCommand(BLPOP, args);
|
||||
}
|
||||
|
||||
public void sort(final byte[] key, final SortingParams sortingParameters,
|
||||
final byte[] dstkey) {
|
||||
final List<byte[]> args = new ArrayList<byte[]>();
|
||||
args.add(key);
|
||||
args.addAll(sortingParameters.getParams());
|
||||
args.add(STORE.raw);
|
||||
args.add(dstkey);
|
||||
sendCommand(SORT, args.toArray(new byte[args.size()][]));
|
||||
}
|
||||
|
||||
public void sort(final byte[] key, final byte[] dstkey) {
|
||||
sendCommand(SORT, key, STORE.raw, dstkey);
|
||||
}
|
||||
|
||||
public void brpop(final byte[][] args) {
|
||||
sendCommand(BRPOP, args);
|
||||
}
|
||||
|
||||
public void auth(final String password) {
|
||||
sendCommand(AUTH, password);
|
||||
}
|
||||
|
||||
public void subscribe(final String... channels) {
|
||||
sendCommand(SUBSCRIBE, channels);
|
||||
}
|
||||
|
||||
public void publish(final String channel, final String message) {
|
||||
sendCommand(PUBLISH, channel, message);
|
||||
}
|
||||
|
||||
public void unsubscribe() {
|
||||
sendCommand(UNSUBSCRIBE);
|
||||
}
|
||||
|
||||
public void unsubscribe(final String... channels) {
|
||||
sendCommand(UNSUBSCRIBE, channels);
|
||||
}
|
||||
|
||||
public void psubscribe(final String[] patterns) {
|
||||
sendCommand(PSUBSCRIBE, patterns);
|
||||
}
|
||||
|
||||
public void punsubscribe() {
|
||||
sendCommand(PUNSUBSCRIBE);
|
||||
}
|
||||
|
||||
public void punsubscribe(final String... patterns) {
|
||||
sendCommand(PUNSUBSCRIBE, patterns);
|
||||
}
|
||||
|
||||
public void zcount(final byte[] key, final double min, final double max) {
|
||||
sendCommand(ZCOUNT, key, toByteArray(min), toByteArray(max));
|
||||
}
|
||||
|
||||
public void zrangeByScore(final byte[] key, final double min,
|
||||
final double max) {
|
||||
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max));
|
||||
}
|
||||
|
||||
public void zrangeByScore(final byte[] key, final byte[] min,
|
||||
final byte[] max) {
|
||||
sendCommand(ZRANGEBYSCORE, key, min, max);
|
||||
}
|
||||
|
||||
public void zrangeByScore(final byte[] key, final double min,
|
||||
final double max, final int offset, int count) {
|
||||
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max),
|
||||
LIMIT.raw, toByteArray(offset), toByteArray(count));
|
||||
}
|
||||
|
||||
public void zrangeByScoreWithScores(final byte[] key, final double min,
|
||||
final double max) {
|
||||
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max),
|
||||
WITHSCORES.raw);
|
||||
}
|
||||
|
||||
public void zrangeByScoreWithScores(final byte[] key, final double min,
|
||||
final double max, final int offset, final int count) {
|
||||
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max),
|
||||
LIMIT.raw, toByteArray(offset), toByteArray(count),
|
||||
WITHSCORES.raw);
|
||||
}
|
||||
|
||||
public void zremrangeByRank(final byte[] key, final int start, final int end) {
|
||||
sendCommand(ZREMRANGEBYRANK, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void zremrangeByScore(final byte[] key, final double start,
|
||||
final double end) {
|
||||
sendCommand(ZREMRANGEBYSCORE, key, toByteArray(start), toByteArray(end));
|
||||
}
|
||||
|
||||
public void zunionstore(final byte[] dstkey, final byte[]... sets) {
|
||||
final byte[][] params = new byte[sets.length + 2][];
|
||||
params[0] = dstkey;
|
||||
params[1] = toByteArray(sets.length);
|
||||
System.arraycopy(sets, 0, params, 2, sets.length);
|
||||
sendCommand(ZUNIONSTORE, params);
|
||||
}
|
||||
|
||||
public void zunionstore(final byte[] dstkey, final ZParams params,
|
||||
final byte[]... sets) {
|
||||
final List<byte[]> args = new ArrayList<byte[]>();
|
||||
args.add(dstkey);
|
||||
args.add(Protocol.toByteArray(sets.length));
|
||||
for (final byte[] set : sets) {
|
||||
args.add(set);
|
||||
}
|
||||
args.addAll(params.getParams());
|
||||
sendCommand(ZUNIONSTORE, args.toArray(new byte[args.size()][]));
|
||||
}
|
||||
|
||||
public void zinterstore(final byte[] dstkey, final byte[]... sets) {
|
||||
final byte[][] params = new byte[sets.length + 2][];
|
||||
params[0] = dstkey;
|
||||
params[1] = Protocol.toByteArray(sets.length);
|
||||
System.arraycopy(sets, 0, params, 2, sets.length);
|
||||
sendCommand(ZINTERSTORE, params);
|
||||
}
|
||||
|
||||
public void zinterstore(final byte[] dstkey, final ZParams params,
|
||||
final byte[]... sets) {
|
||||
final List<byte[]> args = new ArrayList<byte[]>();
|
||||
args.add(dstkey);
|
||||
args.add(Protocol.toByteArray(sets.length));
|
||||
for (final byte[] set : sets) {
|
||||
args.add(set);
|
||||
}
|
||||
args.addAll(params.getParams());
|
||||
sendCommand(ZINTERSTORE, args.toArray(new byte[args.size()][]));
|
||||
}
|
||||
|
||||
public void save() {
|
||||
sendCommand(SAVE);
|
||||
}
|
||||
|
||||
public void bgsave() {
|
||||
sendCommand(BGSAVE);
|
||||
}
|
||||
|
||||
public void bgrewriteaof() {
|
||||
sendCommand(BGREWRITEAOF);
|
||||
}
|
||||
|
||||
public void lastsave() {
|
||||
sendCommand(LASTSAVE);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
sendCommand(SHUTDOWN);
|
||||
}
|
||||
|
||||
public void info() {
|
||||
sendCommand(INFO);
|
||||
}
|
||||
|
||||
public void monitor() {
|
||||
sendCommand(MONITOR);
|
||||
}
|
||||
|
||||
public void slaveof(final String host, final int port) {
|
||||
sendCommand(SLAVEOF, host, String.valueOf(port));
|
||||
}
|
||||
|
||||
public void slaveofNoOne() {
|
||||
sendCommand(SLAVEOF, NO.raw, ONE.raw);
|
||||
}
|
||||
|
||||
public void configGet(final String pattern) {
|
||||
sendCommand(CONFIG, Keyword.GET.name(), pattern);
|
||||
}
|
||||
|
||||
public void configSet(final String parameter, final String value) {
|
||||
sendCommand(CONFIG, Keyword.SET.name(), parameter, value);
|
||||
}
|
||||
|
||||
public void strlen(final byte[] key) {
|
||||
sendCommand(STRLEN, key);
|
||||
}
|
||||
|
||||
public void sync() {
|
||||
sendCommand(SYNC);
|
||||
}
|
||||
|
||||
public void lpushx(final byte[] key, final byte[] string) {
|
||||
sendCommand(LPUSHX, key, string);
|
||||
}
|
||||
|
||||
public void persist(final byte[] key) {
|
||||
sendCommand(PERSIST, key);
|
||||
}
|
||||
|
||||
public void rpushx(final byte[] key, final byte[] string) {
|
||||
sendCommand(RPUSHX, key, string);
|
||||
}
|
||||
|
||||
public void echo(final byte[] string) {
|
||||
sendCommand(ECHO, string);
|
||||
}
|
||||
|
||||
public void linsert(final byte[] key, final LIST_POSITION where,
|
||||
final byte[] pivot, final byte[] value) {
|
||||
sendCommand(LINSERT, key, where.raw, pivot, value);
|
||||
}
|
||||
|
||||
public void debug(final DebugParams params) {
|
||||
sendCommand(DEBUG, params.getCommand());
|
||||
}
|
||||
}
|
||||
2877
src/main/java/redis/clients/jedis/BinaryJedis.java
Normal file
2877
src/main/java/redis/clients/jedis/BinaryJedis.java
Normal file
File diff suppressed because it is too large
Load Diff
159
src/main/java/redis/clients/jedis/BinaryJedisCommands.java
Normal file
159
src/main/java/redis/clients/jedis/BinaryJedisCommands.java
Normal file
@@ -0,0 +1,159 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||
|
||||
/**
|
||||
* Common interface for sharded and non-sharded BinaryJedis
|
||||
*/
|
||||
public interface BinaryJedisCommands {
|
||||
String set(byte[] key, byte[] value);
|
||||
|
||||
byte[] get(byte[] key);
|
||||
|
||||
Integer exists(byte[] key);
|
||||
|
||||
String type(byte[] key);
|
||||
|
||||
Integer expire(byte[] key, int seconds);
|
||||
|
||||
Integer expireAt(byte[] key, long unixTime);
|
||||
|
||||
Integer ttl(byte[] key);
|
||||
|
||||
byte[] getSet(byte[] key, byte[] value);
|
||||
|
||||
Integer setnx(byte[] key, byte[] value);
|
||||
|
||||
String setex(byte[] key, int seconds, byte[] value);
|
||||
|
||||
Integer decrBy(byte[] key, int integer);
|
||||
|
||||
Integer decr(byte[] key);
|
||||
|
||||
Integer incrBy(byte[] key, int integer);
|
||||
|
||||
Integer incr(byte[] key);
|
||||
|
||||
Integer append(byte[] key, byte[] value);
|
||||
|
||||
byte[] substr(byte[] key, int start, int end);
|
||||
|
||||
Integer hset(byte[] key, byte[] field, byte[] value);
|
||||
|
||||
byte[] hget(byte[] key, byte[] field);
|
||||
|
||||
Integer hsetnx(byte[] key, byte[] field, byte[] value);
|
||||
|
||||
String hmset(byte[] key, Map<byte[], byte[]> hash);
|
||||
|
||||
List<byte[]> hmget(byte[] key, byte[]... fields);
|
||||
|
||||
Integer hincrBy(byte[] key, byte[] field, int value);
|
||||
|
||||
Integer hexists(byte[] key, byte[] field);
|
||||
|
||||
Integer hdel(byte[] key, byte[] field);
|
||||
|
||||
Integer hlen(byte[] key);
|
||||
|
||||
Set<byte[]> hkeys(byte[] key);
|
||||
|
||||
Collection<byte[]> hvals(byte[] key);
|
||||
|
||||
Map<byte[], byte[]> hgetAll(byte[] key);
|
||||
|
||||
Integer rpush(byte[] key, byte[] string);
|
||||
|
||||
Integer lpush(byte[] key, byte[] string);
|
||||
|
||||
Integer llen(byte[] key);
|
||||
|
||||
List<byte[]> lrange(byte[] key, int start, int end);
|
||||
|
||||
String ltrim(byte[] key, int start, int end);
|
||||
|
||||
byte[] lindex(byte[] key, int index);
|
||||
|
||||
String lset(byte[] key, int index, byte[] value);
|
||||
|
||||
Integer lrem(byte[] key, int count, byte[] value);
|
||||
|
||||
byte[] lpop(byte[] key);
|
||||
|
||||
byte[] rpop(byte[] key);
|
||||
|
||||
Integer sadd(byte[] key, byte[] member);
|
||||
|
||||
Set<byte[]> smembers(byte[] key);
|
||||
|
||||
Integer srem(byte[] key, byte[] member);
|
||||
|
||||
byte[] spop(byte[] key);
|
||||
|
||||
Integer scard(byte[] key);
|
||||
|
||||
Integer sismember(byte[] key, byte[] member);
|
||||
|
||||
byte[] srandmember(byte[] key);
|
||||
|
||||
Integer zadd(byte[] key, double score, byte[] member);
|
||||
|
||||
Set<byte[]> zrange(byte[] key, int start, int end);
|
||||
|
||||
Integer zrem(byte[] key, byte[] member);
|
||||
|
||||
Double zincrby(byte[] key, double score, byte[] member);
|
||||
|
||||
Integer zrank(byte[] key, byte[] member);
|
||||
|
||||
Integer zrevrank(byte[] key, byte[] member);
|
||||
|
||||
Set<byte[]> zrevrange(byte[] key, int start, int end);
|
||||
|
||||
Set<Tuple> zrangeWithScores(byte[] key, int start, int end);
|
||||
|
||||
Set<Tuple> zrevrangeWithScores(byte[] key, int start, int end);
|
||||
|
||||
Integer zcard(byte[] key);
|
||||
|
||||
Double zscore(byte[] key, byte[] member);
|
||||
|
||||
List<byte[]> sort(byte[] key);
|
||||
|
||||
List<byte[]> sort(byte[] key, SortingParams sortingParameters);
|
||||
|
||||
Integer zcount(byte[] key, double min, double max);
|
||||
|
||||
Set<byte[]> zrangeByScore(byte[] key, double min, double max);
|
||||
|
||||
Set<byte[]> zrangeByScore(
|
||||
byte[] key,
|
||||
double min,
|
||||
double max,
|
||||
int offset,
|
||||
int count);
|
||||
|
||||
Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max);
|
||||
|
||||
Set<Tuple> zrangeByScoreWithScores(
|
||||
byte[] key,
|
||||
double min,
|
||||
double max,
|
||||
int offset,
|
||||
int count);
|
||||
|
||||
Integer zremrangeByRank(byte[] key, int start, int end);
|
||||
|
||||
Integer zremrangeByScore(byte[] key, double start, double end);
|
||||
|
||||
Integer linsert(
|
||||
byte[] key,
|
||||
LIST_POSITION where,
|
||||
byte[] pivot,
|
||||
byte[] value);
|
||||
}
|
||||
381
src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Normal file
381
src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Normal file
@@ -0,0 +1,381 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||
import redis.clients.util.Hashing;
|
||||
import redis.clients.util.Sharded;
|
||||
|
||||
public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
|
||||
implements BinaryJedisCommands {
|
||||
public BinaryShardedJedis(List<JedisShardInfo> shards) {
|
||||
super(shards);
|
||||
}
|
||||
|
||||
public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
|
||||
super(shards, algo);
|
||||
}
|
||||
|
||||
public BinaryShardedJedis(List<JedisShardInfo> shards, Pattern keyTagPattern) {
|
||||
super(shards, keyTagPattern);
|
||||
}
|
||||
|
||||
public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo,
|
||||
Pattern keyTagPattern) {
|
||||
super(shards, algo, keyTagPattern);
|
||||
}
|
||||
|
||||
public void disconnect() throws IOException {
|
||||
for (JedisShardInfo jedis : getAllShards()) {
|
||||
jedis.getResource().disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
protected Jedis create(JedisShardInfo shard) {
|
||||
return new Jedis(shard);
|
||||
}
|
||||
|
||||
public String set(byte[] key, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.set(key, value);
|
||||
}
|
||||
|
||||
public byte[] get(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.get(key);
|
||||
}
|
||||
|
||||
public Integer exists(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.exists(key);
|
||||
}
|
||||
|
||||
public String type(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.type(key);
|
||||
}
|
||||
|
||||
public Integer expire(byte[] key, int seconds) {
|
||||
Jedis j = getShard(key);
|
||||
return j.expire(key, seconds);
|
||||
}
|
||||
|
||||
public Integer expireAt(byte[] key, long unixTime) {
|
||||
Jedis j = getShard(key);
|
||||
return j.expireAt(key, unixTime);
|
||||
}
|
||||
|
||||
public Integer ttl(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.ttl(key);
|
||||
}
|
||||
|
||||
public byte[] getSet(byte[] key, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.getSet(key, value);
|
||||
}
|
||||
|
||||
public Integer setnx(byte[] key, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.setnx(key, value);
|
||||
}
|
||||
|
||||
public String setex(byte[] key, int seconds, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.setex(key, seconds, value);
|
||||
}
|
||||
|
||||
public Integer decrBy(byte[] key, int integer) {
|
||||
Jedis j = getShard(key);
|
||||
return j.decrBy(key, integer);
|
||||
}
|
||||
|
||||
public Integer decr(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.decr(key);
|
||||
}
|
||||
|
||||
public Integer incrBy(byte[] key, int integer) {
|
||||
Jedis j = getShard(key);
|
||||
return j.incrBy(key, integer);
|
||||
}
|
||||
|
||||
public Integer incr(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.incr(key);
|
||||
}
|
||||
|
||||
public Integer append(byte[] key, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.append(key, value);
|
||||
}
|
||||
|
||||
public byte[] substr(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.substr(key, start, end);
|
||||
}
|
||||
|
||||
public Integer hset(byte[] key, byte[] field, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hset(key, field, value);
|
||||
}
|
||||
|
||||
public byte[] hget(byte[] key, byte[] field) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hget(key, field);
|
||||
}
|
||||
|
||||
public Integer hsetnx(byte[] key, byte[] field, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hsetnx(key, field, value);
|
||||
}
|
||||
|
||||
public String hmset(byte[] key, Map<byte[], byte[]> hash) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hmset(key, hash);
|
||||
}
|
||||
|
||||
public List<byte[]> hmget(byte[] key, byte[]... fields) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hmget(key, fields);
|
||||
}
|
||||
|
||||
public Integer hincrBy(byte[] key, byte[] field, int value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hincrBy(key, field, value);
|
||||
}
|
||||
|
||||
public Integer hexists(byte[] key, byte[] field) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hexists(key, field);
|
||||
}
|
||||
|
||||
public Integer hdel(byte[] key, byte[] field) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hdel(key, field);
|
||||
}
|
||||
|
||||
public Integer hlen(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hlen(key);
|
||||
}
|
||||
|
||||
public Set<byte[]> hkeys(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hkeys(key);
|
||||
}
|
||||
|
||||
public Collection<byte[]> hvals(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hvals(key);
|
||||
}
|
||||
|
||||
public Map<byte[], byte[]> hgetAll(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hgetAll(key);
|
||||
}
|
||||
|
||||
public Integer rpush(byte[] key, byte[] string) {
|
||||
Jedis j = getShard(key);
|
||||
return j.rpush(key, string);
|
||||
}
|
||||
|
||||
public Integer lpush(byte[] key, byte[] string) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lpush(key, string);
|
||||
}
|
||||
|
||||
public Integer llen(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.llen(key);
|
||||
}
|
||||
|
||||
public List<byte[]> lrange(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lrange(key, start, end);
|
||||
}
|
||||
|
||||
public String ltrim(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.ltrim(key, start, end);
|
||||
}
|
||||
|
||||
public byte[] lindex(byte[] key, int index) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lindex(key, index);
|
||||
}
|
||||
|
||||
public String lset(byte[] key, int index, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lset(key, index, value);
|
||||
}
|
||||
|
||||
public Integer lrem(byte[] key, int count, byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lrem(key, count, value);
|
||||
}
|
||||
|
||||
public byte[] lpop(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.lpop(key);
|
||||
}
|
||||
|
||||
public byte[] rpop(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.rpop(key);
|
||||
}
|
||||
|
||||
public Integer sadd(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.sadd(key, member);
|
||||
}
|
||||
|
||||
public Set<byte[]> smembers(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.smembers(key);
|
||||
}
|
||||
|
||||
public Integer srem(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.srem(key, member);
|
||||
}
|
||||
|
||||
public byte[] spop(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.spop(key);
|
||||
}
|
||||
|
||||
public Integer scard(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.scard(key);
|
||||
}
|
||||
|
||||
public Integer sismember(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.sismember(key, member);
|
||||
}
|
||||
|
||||
public byte[] srandmember(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.srandmember(key);
|
||||
}
|
||||
|
||||
public Integer zadd(byte[] key, double score, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zadd(key, score, member);
|
||||
}
|
||||
|
||||
public Set<byte[]> zrange(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrange(key, start, end);
|
||||
}
|
||||
|
||||
public Integer zrem(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrem(key, member);
|
||||
}
|
||||
|
||||
public Double zincrby(byte[] key, double score, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zincrby(key, score, member);
|
||||
}
|
||||
|
||||
public Integer zrank(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrank(key, member);
|
||||
}
|
||||
|
||||
public Integer zrevrank(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrevrank(key, member);
|
||||
}
|
||||
|
||||
public Set<byte[]> zrevrange(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrevrange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeWithScores(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeWithScores(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrevrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Integer zcard(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zcard(key);
|
||||
}
|
||||
|
||||
public Double zscore(byte[] key, byte[] member) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zscore(key, member);
|
||||
}
|
||||
|
||||
public List<byte[]> sort(byte[] key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.sort(key);
|
||||
}
|
||||
|
||||
public List<byte[]> sort(byte[] key, SortingParams sortingParameters) {
|
||||
Jedis j = getShard(key);
|
||||
return j.sort(key, sortingParameters);
|
||||
}
|
||||
|
||||
public Integer zcount(byte[] key, double min, double max) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zcount(key, min, max);
|
||||
}
|
||||
|
||||
public Set<byte[]> zrangeByScore(byte[] key, double min, double max) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Set<byte[]> zrangeByScore(byte[] key, double min, double max,
|
||||
int offset, int count) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrangeByScore(key, min, max, offset, count);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrangeByScoreWithScores(key, min, max);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeByScoreWithScores(byte[] key, double min,
|
||||
double max, int offset, int count) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zrangeByScoreWithScores(key, min, max, offset, count);
|
||||
}
|
||||
|
||||
public Integer zremrangeByRank(byte[] key, int start, int end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zremrangeByRank(key, start, end);
|
||||
}
|
||||
|
||||
public Integer zremrangeByScore(byte[] key, double start, double end) {
|
||||
Jedis j = getShard(key);
|
||||
return j.zremrangeByScore(key, start, end);
|
||||
}
|
||||
|
||||
public Integer linsert(byte[] key, LIST_POSITION where, byte[] pivot,
|
||||
byte[] value) {
|
||||
Jedis j = getShard(key);
|
||||
return j.linsert(key, where, pivot, value);
|
||||
}
|
||||
|
||||
public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) {
|
||||
shardedJedisPipeline.setShardedJedis(this);
|
||||
shardedJedisPipeline.execute();
|
||||
return shardedJedisPipeline.getResults();
|
||||
}
|
||||
}
|
||||
430
src/main/java/redis/clients/jedis/BinaryTransaction.java
Normal file
430
src/main/java/redis/clients/jedis/BinaryTransaction.java
Normal file
@@ -0,0 +1,430 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BinaryTransaction {
|
||||
protected Client client = null;
|
||||
|
||||
public BinaryTransaction() {
|
||||
}
|
||||
|
||||
public BinaryTransaction(final Client client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
client.ping();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String set(final byte[] key, final byte[] value) {
|
||||
client.set(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String get(final byte[] key) {
|
||||
client.get(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String exists(final byte[] key) {
|
||||
client.exists(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String del(final byte[]... keys) {
|
||||
client.del(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String type(final byte[] key) {
|
||||
client.type(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String flushDB() {
|
||||
client.flushDB();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String keys(final byte[] pattern) {
|
||||
client.keys(pattern);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public byte[] randomBinaryKey() {
|
||||
client.randomKey();
|
||||
return client.getBinaryBulkReply();
|
||||
}
|
||||
|
||||
public String rename(final byte[] oldkey, final byte[] newkey) {
|
||||
client.rename(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String renamenx(final byte[] oldkey, final byte[] newkey) {
|
||||
client.renamenx(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String dbSize() {
|
||||
client.dbSize();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expire(final byte[] key, final int seconds) {
|
||||
client.expire(key, seconds);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expireAt(final byte[] key, final long unixTime) {
|
||||
client.expireAt(key, unixTime);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ttl(final byte[] key) {
|
||||
client.ttl(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String select(final int index) {
|
||||
client.select(index);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String move(final byte[] key, final int dbIndex) {
|
||||
client.move(key, dbIndex);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String flushAll() {
|
||||
client.flushAll();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String getSet(final byte[] key, final byte[] value) {
|
||||
client.getSet(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mget(final byte[]... keys) {
|
||||
client.mget(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setnx(final byte[] key, final byte[] value) {
|
||||
client.setnx(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setex(final byte[] key, final int seconds, final byte[] value) {
|
||||
client.setex(key, seconds, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mset(final byte[]... keysvalues) {
|
||||
client.mset(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String msetnx(final byte[]... keysvalues) {
|
||||
client.msetnx(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decrBy(final byte[] key, final int integer) {
|
||||
client.decrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decr(final byte[] key) {
|
||||
client.decr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incrBy(final byte[] key, final int integer) {
|
||||
client.incrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incr(final byte[] key) {
|
||||
client.incr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String append(final byte[] key, final byte[] value) {
|
||||
client.append(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String substr(final byte[] key, final int start, final int end) {
|
||||
client.substr(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hset(final byte[] key, final byte[] field, final byte[] value) {
|
||||
client.hset(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hget(final byte[] key, final byte[] field) {
|
||||
client.hget(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hsetnx(final byte[] key, final byte[] field,
|
||||
final byte[] value) {
|
||||
client.hsetnx(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmset(final byte[] key, final Map<byte[], byte[]> hash) {
|
||||
client.hmset(key, hash);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmget(final byte[] key, final byte[]... fields) {
|
||||
client.hmget(key, fields);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hincrBy(final byte[] key, final byte[] field, final int value) {
|
||||
client.hincrBy(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hexists(final byte[] key, final byte[] field) {
|
||||
client.hexists(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hdel(final byte[] key, final byte[] field) {
|
||||
client.hdel(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hlen(final byte[] key) {
|
||||
client.hlen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hkeys(final byte[] key) {
|
||||
client.hkeys(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hvals(final byte[] key) {
|
||||
client.hvals(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hgetAll(final byte[] key) {
|
||||
client.hgetAll(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpush(final byte[] key, final byte[] string) {
|
||||
client.rpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpush(final byte[] key, final byte[] string) {
|
||||
client.lpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String llen(final byte[] key) {
|
||||
client.llen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrange(final byte[] key, final int start, final int end) {
|
||||
client.lrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ltrim(final byte[] key, final int start, final int end) {
|
||||
client.ltrim(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lindex(final byte[] key, final int index) {
|
||||
client.lindex(key, index);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lset(final byte[] key, final int index, final byte[] value) {
|
||||
client.lset(key, index, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrem(final byte[] key, final int count, final byte[] value) {
|
||||
client.lrem(key, count, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpop(final byte[] key) {
|
||||
client.lpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpop(final byte[] key) {
|
||||
client.rpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpoplpush(final byte[] srckey, final byte[] dstkey) {
|
||||
client.rpoplpush(srckey, dstkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sadd(final byte[] key, final byte[] member) {
|
||||
client.sadd(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smembers(final byte[] key) {
|
||||
client.smembers(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srem(final byte[] key, final byte[] member) {
|
||||
client.srem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String spop(final byte[] key) {
|
||||
client.spop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smove(final byte[] srckey, final byte[] dstkey,
|
||||
final byte[] member) {
|
||||
client.smove(srckey, dstkey, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String scard(final byte[] key) {
|
||||
client.scard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sismember(final byte[] key, final byte[] member) {
|
||||
client.sismember(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinter(final byte[]... keys) {
|
||||
client.sinter(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinterstore(final byte[] dstkey, final byte[]... keys) {
|
||||
client.sinterstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunion(final byte[]... keys) {
|
||||
client.sunion(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunionstore(final byte[] dstkey, final byte[]... keys) {
|
||||
client.sunionstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiff(final byte[]... keys) {
|
||||
client.sdiff(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiffstore(final byte[] dstkey, final byte[]... keys) {
|
||||
client.sdiffstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srandmember(final byte[] key) {
|
||||
client.srandmember(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zadd(final byte[] key, final double score, final byte[] member) {
|
||||
client.zadd(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrange(final byte[] key, final int start, final int end) {
|
||||
client.zrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrem(final byte[] key, final byte[] member) {
|
||||
client.zrem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zincrby(final byte[] key, final double score,
|
||||
final byte[] member) {
|
||||
client.zincrby(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrank(final byte[] key, final byte[] member) {
|
||||
client.zrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrank(final byte[] key, final byte[] member) {
|
||||
client.zrevrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrange(final byte[] key, final int start, final int end) {
|
||||
client.zrevrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrangeWithScores(final byte[] key, final int start,
|
||||
final int end) {
|
||||
client.zrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrangeWithScores(final byte[] key, final int start,
|
||||
final int end) {
|
||||
client.zrevrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zcard(final byte[] key) {
|
||||
client.zcard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zscore(final byte[] key, final byte[] member) {
|
||||
client.zscore(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public List<Object> exec() {
|
||||
client.exec();
|
||||
|
||||
return client.getObjectMultiBulkReply();
|
||||
}
|
||||
|
||||
public String sort(final byte[] key) {
|
||||
client.sort(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sort(final byte[] key, final SortingParams sortingParameters) {
|
||||
client.sort(key, sortingParameters);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public void discard() {
|
||||
client.discard();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@ import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import redis.clients.jedis.Protocol.Command;
|
||||
import redis.clients.util.RedisInputStream;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
|
||||
@@ -24,7 +25,7 @@ public class Connection {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
public void setTimeout(final int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@@ -44,18 +45,46 @@ public class Connection {
|
||||
}
|
||||
}
|
||||
|
||||
public Connection(String host) {
|
||||
public Connection(final String host) {
|
||||
super();
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
protected Connection sendCommand(String name, String... args) {
|
||||
protocol.sendCommand(outputStream, name, args);
|
||||
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);
|
||||
}
|
||||
return sendCommand(cmd, bargs);
|
||||
}
|
||||
|
||||
protected Connection sendCommand(final Command cmd, final byte[]... args) {
|
||||
try {
|
||||
connect();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
} catch (IOException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
}
|
||||
protocol.sendCommand(outputStream, cmd, args);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Connection(String host, int port) {
|
||||
protected Connection sendCommand(final Command cmd) {
|
||||
try {
|
||||
connect();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
} catch (IOException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
}
|
||||
protocol.sendCommand(outputStream, cmd, new byte[0][]);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Connection(final String host, final int port) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
@@ -65,7 +94,7 @@ public class Connection {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
public void setHost(final String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
@@ -73,7 +102,7 @@ public class Connection {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
public void setPort(final int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@@ -111,12 +140,26 @@ public class Connection {
|
||||
|
||||
protected String getStatusCodeReply() {
|
||||
pipelinedCommands--;
|
||||
return (String) protocol.read(inputStream);
|
||||
final byte[] resp = (byte[]) protocol.read(inputStream);
|
||||
if (null == resp) {
|
||||
return null;
|
||||
} else {
|
||||
return new String(resp, Protocol.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
public String getBulkReply() {
|
||||
final byte[] result = getBinaryBulkReply();
|
||||
if (null != result) {
|
||||
return new String(result, Protocol.UTF8);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getBinaryBulkReply() {
|
||||
pipelinedCommands--;
|
||||
return (String) protocol.read(inputStream);
|
||||
return (byte[]) protocol.read(inputStream);
|
||||
}
|
||||
|
||||
public Integer getIntegerReply() {
|
||||
@@ -124,10 +167,26 @@ public class Connection {
|
||||
return (Integer) protocol.read(inputStream);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getMultiBulkReply() {
|
||||
final List<byte[]> bresult = getBinaryMultiBulkReply();
|
||||
if (null == bresult) {
|
||||
return null;
|
||||
}
|
||||
final ArrayList<String> result = new ArrayList<String>(bresult.size());
|
||||
for (final byte[] barray : bresult) {
|
||||
if (barray == null) {
|
||||
result.add(null);
|
||||
} else {
|
||||
result.add(new String(barray, Protocol.UTF8));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<byte[]> getBinaryMultiBulkReply() {
|
||||
pipelinedCommands--;
|
||||
return (List<String>) protocol.read(inputStream);
|
||||
return (List<byte[]>) protocol.read(inputStream);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -58,9 +59,9 @@ public interface JedisCommands {
|
||||
|
||||
Integer hlen(String key);
|
||||
|
||||
List<String> hkeys(String key);
|
||||
Set<String> hkeys(String key);
|
||||
|
||||
List<String> hvals(String key);
|
||||
Collection<String> hvals(String key);
|
||||
|
||||
Map<String, String> hgetAll(String key);
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static redis.clients.jedis.Protocol.Keyword.*;
|
||||
|
||||
public abstract class JedisPubSub {
|
||||
private int subscribedChannels = 0;
|
||||
private Client client;
|
||||
@@ -62,26 +65,78 @@ public abstract class JedisPubSub {
|
||||
private void process(Client client) {
|
||||
do {
|
||||
List<Object> reply = client.getObjectMultiBulkReply();
|
||||
if (reply.get(0).equals("subscribe")) {
|
||||
final Object firstObj = reply.get(0);
|
||||
if (!(firstObj instanceof byte[])) {
|
||||
throw
|
||||
new JedisException("Unknown message type: "+ firstObj);
|
||||
}
|
||||
final byte[] resp = (byte[]) firstObj;
|
||||
if(Arrays.equals(SUBSCRIBE.raw, resp)) {
|
||||
subscribedChannels = ((Integer) reply.get(2)).intValue();
|
||||
onSubscribe((String) reply.get(1), subscribedChannels);
|
||||
} else if (reply.get(0).equals("unsubscribe")) {
|
||||
final byte[] bchannel = (byte[]) reply.get(1);
|
||||
final String strchannel =
|
||||
(bchannel == null) ?
|
||||
null :
|
||||
new String(bchannel, Protocol.UTF8);
|
||||
onSubscribe(strchannel, subscribedChannels);
|
||||
} else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) {
|
||||
subscribedChannels = ((Integer) reply.get(2)).intValue();
|
||||
onUnsubscribe((String) reply.get(1), subscribedChannels);
|
||||
} else if (reply.get(0).equals("message")) {
|
||||
onMessage((String) reply.get(1), (String) reply.get(2));
|
||||
} else if (reply.get(0).equals("pmessage")) {
|
||||
onPMessage((String) reply.get(1), (String) reply.get(2),
|
||||
(String) reply.get(3));
|
||||
} else if (reply.get(0).equals("psubscribe")) {
|
||||
final byte[] bchannel = (byte[]) reply.get(1);
|
||||
final String strchannel =
|
||||
(bchannel == null) ?
|
||||
null :
|
||||
new String(bchannel, Protocol.UTF8);
|
||||
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);
|
||||
onMessage(strchannel, strmesg);
|
||||
} else if (Arrays.equals(PMESSAGE.raw, resp)) {
|
||||
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);
|
||||
} else if (Arrays.equals(PSUBSCRIBE.raw, resp)) {
|
||||
subscribedChannels = ((Integer) reply.get(2)).intValue();
|
||||
onPSubscribe((String) reply.get(1), subscribedChannels);
|
||||
} else if (reply.get(0).equals("punsubscribe")) {
|
||||
final byte[] bpattern = (byte[]) reply.get(1);
|
||||
final String strpattern =
|
||||
(bpattern == null) ?
|
||||
null :
|
||||
new String(bpattern, Protocol.UTF8);
|
||||
onPSubscribe(strpattern, subscribedChannels);
|
||||
} else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) {
|
||||
subscribedChannels = ((Integer) reply.get(2)).intValue();
|
||||
onPUnsubscribe((String) reply.get(1), subscribedChannels);
|
||||
final byte[] bpattern = (byte[]) reply.get(1);
|
||||
final String strpattern =
|
||||
(bpattern == null) ?
|
||||
null :
|
||||
new String(bpattern, Protocol.UTF8);
|
||||
onPUnsubscribe(strpattern, subscribedChannels);
|
||||
} else {
|
||||
throw new JedisException("Unknown message type: "
|
||||
+ reply.get(0));
|
||||
throw new JedisException("Unknown message type: "+ firstObj);
|
||||
}
|
||||
} while (isSubscribed());
|
||||
}
|
||||
|
||||
@@ -1,41 +1,45 @@
|
||||
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 java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static redis.clients.util.RedisOutputStream.CHARSET;
|
||||
|
||||
public final class Protocol {
|
||||
|
||||
public static final int DEFAULT_PORT = 6379;
|
||||
|
||||
public static final Charset UTF8 = Charset.forName("UTF-8");
|
||||
|
||||
public static final byte DOLLAR_BYTE = '$';
|
||||
public static final byte ASTERISK_BYTE = '*';
|
||||
public static final byte PLUS_BYTE = '+';
|
||||
public static final byte MINUS_BYTE = '-';
|
||||
public static final byte COLON_BYTE = ':';
|
||||
|
||||
public void sendCommand(RedisOutputStream os, String name, String... args) {
|
||||
public void sendCommand(final RedisOutputStream os, final Command command,
|
||||
final byte[]... args) {
|
||||
sendCommand(os, command.raw, args);
|
||||
}
|
||||
|
||||
private void sendCommand(final RedisOutputStream os, final byte[] command,
|
||||
final byte[]... args) {
|
||||
try {
|
||||
os.write(ASTERISK_BYTE);
|
||||
os.writeIntCrLf(args.length + 1);
|
||||
os.write(DOLLAR_BYTE);
|
||||
os.writeIntCrLf(name.length());
|
||||
os.writeAsciiCrLf(name);
|
||||
os.writeIntCrLf(command.length);
|
||||
os.write(command);
|
||||
os.writeCrLf();
|
||||
|
||||
for (String str : args) {
|
||||
for (final byte[] arg : args) {
|
||||
os.write(DOLLAR_BYTE);
|
||||
final int size = RedisOutputStream.utf8Length(str);
|
||||
os.writeIntCrLf(size);
|
||||
if (size == str.length())
|
||||
os.writeAsciiCrLf(str);
|
||||
else {
|
||||
os.writeUtf8CrLf(str);
|
||||
}
|
||||
os.writeIntCrLf(arg.length);
|
||||
os.write(arg);
|
||||
os.writeCrLf();
|
||||
}
|
||||
os.flush();
|
||||
} catch (IOException e) {
|
||||
@@ -43,12 +47,12 @@ public final class Protocol {
|
||||
}
|
||||
}
|
||||
|
||||
private void processError(RedisInputStream is) {
|
||||
private void processError(final RedisInputStream is) {
|
||||
String message = is.readLine();
|
||||
throw new JedisException(message);
|
||||
}
|
||||
|
||||
private Object process(RedisInputStream is) {
|
||||
private Object process(final RedisInputStream is) {
|
||||
try {
|
||||
byte b = is.readByte();
|
||||
if (b == MINUS_BYTE) {
|
||||
@@ -70,11 +74,11 @@ public final class Protocol {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String processStatusCodeReply(RedisInputStream is) {
|
||||
return is.readLine();
|
||||
private byte[] processStatusCodeReply(final RedisInputStream is) {
|
||||
return is.readLine().getBytes(UTF8);
|
||||
}
|
||||
|
||||
private String processBulkReply(RedisInputStream is) {
|
||||
private byte[] processBulkReply(final RedisInputStream is) {
|
||||
int len = Integer.parseInt(is.readLine());
|
||||
if (len == -1) {
|
||||
return null;
|
||||
@@ -92,15 +96,15 @@ public final class Protocol {
|
||||
throw new JedisException(e);
|
||||
}
|
||||
|
||||
return new String(read, CHARSET);
|
||||
return read;
|
||||
}
|
||||
|
||||
private Integer processInteger(RedisInputStream is) {
|
||||
private Integer processInteger(final RedisInputStream is) {
|
||||
String num = is.readLine();
|
||||
return Integer.valueOf(num);
|
||||
}
|
||||
|
||||
private List<Object> processMultiBulkReply(RedisInputStream is) {
|
||||
private List<Object> processMultiBulkReply(final RedisInputStream is) {
|
||||
int num = Integer.parseInt(is.readLine());
|
||||
if (num == -1) {
|
||||
return null;
|
||||
@@ -112,7 +116,39 @@ public final class Protocol {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Object read(RedisInputStream is) {
|
||||
public Object read(final RedisInputStream is) {
|
||||
return process(is);
|
||||
}
|
||||
|
||||
public static final byte[] toByteArray(final int value) {
|
||||
return String.valueOf(value).getBytes(Protocol.UTF8);
|
||||
}
|
||||
|
||||
public static final byte[] toByteArray(final long value) {
|
||||
return String.valueOf(value).getBytes(Protocol.UTF8);
|
||||
}
|
||||
|
||||
public static final byte[] toByteArray(final double value) {
|
||||
return String.valueOf(value).getBytes(Protocol.UTF8);
|
||||
}
|
||||
|
||||
public static enum Command {
|
||||
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG;
|
||||
|
||||
public final byte[] raw;
|
||||
|
||||
Command() {
|
||||
raw = this.name().getBytes(UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Keyword {
|
||||
AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES;
|
||||
public final byte[] raw;
|
||||
|
||||
Keyword() {
|
||||
raw = this.name().toLowerCase().getBytes(UTF8);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,16 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import redis.clients.jedis.Client.LIST_POSITION;
|
||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||
import redis.clients.util.Hashing;
|
||||
import redis.clients.util.Sharded;
|
||||
|
||||
public class ShardedJedis extends Sharded<Jedis, JedisShardInfo> implements
|
||||
JedisCommands {
|
||||
public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
|
||||
public ShardedJedis(List<JedisShardInfo> shards) {
|
||||
super(shards);
|
||||
}
|
||||
@@ -161,12 +160,12 @@ public class ShardedJedis extends Sharded<Jedis, JedisShardInfo> implements
|
||||
return j.hlen(key);
|
||||
}
|
||||
|
||||
public List<String> hkeys(String key) {
|
||||
public Set<String> hkeys(String key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hkeys(key);
|
||||
}
|
||||
|
||||
public List<String> hvals(String key) {
|
||||
public Collection<String> hvals(String key) {
|
||||
Jedis j = getShard(key);
|
||||
return j.hvals(key);
|
||||
}
|
||||
@@ -368,10 +367,4 @@ public class ShardedJedis extends Sharded<Jedis, JedisShardInfo> implements
|
||||
Jedis j = getShard(key);
|
||||
return j.linsert(key, where, pivot, value);
|
||||
}
|
||||
|
||||
public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) {
|
||||
shardedJedisPipeline.setShardedJedis(this);
|
||||
shardedJedisPipeline.execute();
|
||||
return shardedJedisPipeline.getResults();
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import redis.clients.jedis.Client.LIST_POSITION;
|
||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||
|
||||
public abstract class ShardedJedisPipeline {
|
||||
private ShardedJedis jedis;
|
||||
private BinaryShardedJedis jedis;
|
||||
private List<FutureResult> results = new ArrayList<FutureResult>();
|
||||
|
||||
private class FutureResult {
|
||||
@@ -22,7 +22,7 @@ public abstract class ShardedJedisPipeline {
|
||||
}
|
||||
}
|
||||
|
||||
public void setShardedJedis(ShardedJedis jedis) {
|
||||
public void setShardedJedis(BinaryShardedJedis jedis) {
|
||||
this.jedis = jedis;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static redis.clients.jedis.Protocol.Keyword.*;
|
||||
/**
|
||||
* Builder Class for {@link Jedis#sort(String, SortingParams) SORT} Parameters.
|
||||
*
|
||||
*/
|
||||
public class SortingParams {
|
||||
private List<String> params = new ArrayList<String>();
|
||||
private List<byte[]> params = new ArrayList<byte[]>();
|
||||
|
||||
/**
|
||||
* Sort by weight in keys.
|
||||
@@ -25,8 +25,25 @@ public class SortingParams {
|
||||
* @param pattern
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams by(String pattern) {
|
||||
params.add("BY");
|
||||
public SortingParams by(final String pattern) {
|
||||
return by(pattern.getBytes(Protocol.UTF8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort by weight in keys.
|
||||
* <p>
|
||||
* Takes a pattern that is used in order to generate the key names of the
|
||||
* weights used for sorting. Weight key names are obtained substituting the
|
||||
* first occurrence of * with the actual value of the elements on the list.
|
||||
* <p>
|
||||
* The pattern for a normal key/value pair is "keyname*" and for a value in
|
||||
* a hash "keyname*->fieldname".
|
||||
*
|
||||
* @param pattern
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams by(final byte[] pattern) {
|
||||
params.add(BY.raw);
|
||||
params.add(pattern);
|
||||
return this;
|
||||
}
|
||||
@@ -40,11 +57,12 @@ public class SortingParams {
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams nosort() {
|
||||
params.add("BY nosort");
|
||||
params.add(BY.raw);
|
||||
params.add(NOSORT.raw);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Collection<String> getParams() {
|
||||
public Collection<byte[]> getParams() {
|
||||
return Collections.unmodifiableCollection(params);
|
||||
}
|
||||
|
||||
@@ -54,7 +72,7 @@ public class SortingParams {
|
||||
* @return the sortingParams Object
|
||||
*/
|
||||
public SortingParams desc() {
|
||||
params.add("DESC");
|
||||
params.add(DESC.raw);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -64,7 +82,7 @@ public class SortingParams {
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams asc() {
|
||||
params.add("ASC");
|
||||
params.add(ASC.raw);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -76,10 +94,10 @@ public class SortingParams {
|
||||
* @param count
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams limit(int start, int count) {
|
||||
params.add("LIMIT");
|
||||
params.add(String.valueOf(start));
|
||||
params.add(String.valueOf(count));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -90,7 +108,7 @@ public class SortingParams {
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams alpha() {
|
||||
params.add("ALPHA");
|
||||
params.add(ALPHA.raw);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -110,8 +128,31 @@ public class SortingParams {
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams get(String... patterns) {
|
||||
for (String pattern : patterns) {
|
||||
params.add("GET");
|
||||
for (final String pattern : patterns) {
|
||||
params.add(GET.raw);
|
||||
params.add(pattern.getBytes(Protocol.UTF8));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieving external keys from the result of the search.
|
||||
* <p>
|
||||
* Takes a pattern that is used in order to generate the key names of the
|
||||
* result of sorting. The key names are obtained substituting the first
|
||||
* occurrence of * with the actual value of the elements on the list.
|
||||
* <p>
|
||||
* The pattern for a normal key/value pair is "keyname*" and for a value in
|
||||
* a hash "keyname*->fieldname".
|
||||
* <p>
|
||||
* To get the list itself use the char # as pattern.
|
||||
*
|
||||
* @param patterns
|
||||
* @return the SortingParams Object
|
||||
*/
|
||||
public SortingParams get(byte[]... patterns) {
|
||||
for (final byte[] pattern : patterns) {
|
||||
params.add(GET.raw);
|
||||
params.add(pattern);
|
||||
}
|
||||
return this;
|
||||
|
||||
@@ -1,425 +1,392 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Transaction {
|
||||
protected Client client = null;
|
||||
|
||||
public Transaction() {
|
||||
}
|
||||
|
||||
public Transaction(Client client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
client.ping();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String set(String key, String value) {
|
||||
client.set(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
client.sendCommand("GET", key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String exists(String key) {
|
||||
client.exists(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String del(String... keys) {
|
||||
client.del(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String type(String key) {
|
||||
client.type(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String flushDB() {
|
||||
client.flushDB();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String keys(String pattern) {
|
||||
client.keys(pattern);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String randomKey() {
|
||||
client.randomKey();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rename(String oldkey, String newkey) {
|
||||
client.rename(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String renamenx(String oldkey, String newkey) {
|
||||
client.renamenx(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String dbSize() {
|
||||
client.dbSize();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expire(String key, int seconds) {
|
||||
client.expire(key, seconds);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expireAt(String key, long unixTime) {
|
||||
client.expireAt(key, unixTime);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ttl(String key) {
|
||||
client.ttl(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String select(int index) {
|
||||
client.select(index);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String move(String key, int dbIndex) {
|
||||
client.move(key, dbIndex);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String flushAll() {
|
||||
client.flushAll();
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String getSet(String key, String value) {
|
||||
client.getSet(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mget(String... keys) {
|
||||
client.mget(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setnx(String key, String value) {
|
||||
client.setnx(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setex(String key, int seconds, String value) {
|
||||
client.setex(key, seconds, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mset(String... keysvalues) {
|
||||
client.mset(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String msetnx(String... keysvalues) {
|
||||
client.msetnx(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decrBy(String key, int integer) {
|
||||
client.decrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decr(String key) {
|
||||
client.decr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incrBy(String key, int integer) {
|
||||
client.incrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incr(String key) {
|
||||
client.incr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String append(String key, String value) {
|
||||
client.append(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String substr(String key, int start, int end) {
|
||||
client.substr(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hset(String key, String field, String value) {
|
||||
client.hset(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hget(String key, String field) {
|
||||
client.hget(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hsetnx(String key, String field, String value) {
|
||||
client.hsetnx(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmset(String key, Map<String, String> hash) {
|
||||
client.hmset(key, hash);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmget(String key, String... fields) {
|
||||
client.hmget(key, fields);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hincrBy(String key, String field, int value) {
|
||||
client.hincrBy(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hexists(String key, String field) {
|
||||
client.hexists(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hdel(String key, String field) {
|
||||
client.hdel(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hlen(String key) {
|
||||
client.hlen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hkeys(String key) {
|
||||
client.hkeys(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hvals(String key) {
|
||||
client.hvals(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hgetAll(String key) {
|
||||
client.hgetAll(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpush(String key, String string) {
|
||||
client.rpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpush(String key, String string) {
|
||||
client.lpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String llen(String key) {
|
||||
client.llen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrange(String key, int start, int end) {
|
||||
client.lrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ltrim(String key, int start, int end) {
|
||||
client.ltrim(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lindex(String key, int index) {
|
||||
client.lindex(key, index);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lset(String key, int index, String value) {
|
||||
client.lset(key, index, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrem(String key, int count, String value) {
|
||||
client.lrem(key, count, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpop(String key) {
|
||||
client.lpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpop(String key) {
|
||||
client.rpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpoplpush(String srckey, String dstkey) {
|
||||
client.rpoplpush(srckey, dstkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sadd(String key, String member) {
|
||||
client.sadd(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smembers(String key) {
|
||||
client.smembers(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srem(String key, String member) {
|
||||
client.srem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String spop(String key) {
|
||||
client.spop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smove(String srckey, String dstkey, String member) {
|
||||
client.smove(srckey, dstkey, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String scard(String key) {
|
||||
client.scard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sismember(String key, String member) {
|
||||
client.sismember(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinter(String... keys) {
|
||||
client.sinter(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinterstore(String dstkey, String... keys) {
|
||||
client.sinterstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunion(String... keys) {
|
||||
client.sunion(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunionstore(String dstkey, String... keys) {
|
||||
client.sunionstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiff(String... keys) {
|
||||
client.sdiff(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiffstore(String dstkey, String... keys) {
|
||||
client.sdiffstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srandmember(String key) {
|
||||
client.srandmember(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zadd(String key, double score, String member) {
|
||||
client.zadd(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrange(String key, int start, int end) {
|
||||
client.zrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrem(String key, String member) {
|
||||
client.zrem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zincrby(String key, double score, String member) {
|
||||
client.zincrby(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrank(String key, String member) {
|
||||
client.zrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrank(String key, String member) {
|
||||
client.zrevrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrange(String key, int start, int end) {
|
||||
client.zrevrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrangeWithScores(String key, int start, int end) {
|
||||
client.zrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrangeWithScores(String key, int start, int end) {
|
||||
client.zrevrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zcard(String key) {
|
||||
client.zcard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zscore(String key, String member) {
|
||||
client.zscore(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public List<Object> exec() {
|
||||
client.exec();
|
||||
|
||||
return client.getObjectMultiBulkReply();
|
||||
}
|
||||
|
||||
public String sort(String key) {
|
||||
client.sort(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sort(String key, SortingParams sortingParameters) {
|
||||
client.sort(key, sortingParameters);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public void discard() {
|
||||
client.discard();
|
||||
}
|
||||
public class Transaction extends BinaryTransaction {
|
||||
public Transaction() {
|
||||
}
|
||||
|
||||
public Transaction(final Client client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
public String set(final String key, final String value) {
|
||||
client.set(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String get(final String key) {
|
||||
client.get(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String exists(final String key) {
|
||||
client.exists(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String del(final String... keys) {
|
||||
client.del(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String type(final String key) {
|
||||
client.type(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String keys(final String pattern) {
|
||||
client.keys(pattern);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String randomKey() {
|
||||
client.randomKey();
|
||||
return client.getBulkReply();
|
||||
}
|
||||
|
||||
public String rename(final String oldkey, final String newkey) {
|
||||
client.rename(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String renamenx(final String oldkey, final String newkey) {
|
||||
client.renamenx(oldkey, newkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expire(final String key, final int seconds) {
|
||||
client.expire(key, seconds);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String expireAt(final String key, final long unixTime) {
|
||||
client.expireAt(key, unixTime);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ttl(final String key) {
|
||||
client.ttl(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String move(final String key, final int dbIndex) {
|
||||
client.move(key, dbIndex);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String getSet(final String key, final String value) {
|
||||
client.getSet(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mget(final String... keys) {
|
||||
client.mget(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setnx(final String key, final String value) {
|
||||
client.setnx(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String setex(final String key, final int seconds, final String value) {
|
||||
client.setex(key, seconds, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mset(final String... keysvalues) {
|
||||
client.mset(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String msetnx(final String... keysvalues) {
|
||||
client.msetnx(keysvalues);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decrBy(final String key, final int integer) {
|
||||
client.decrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String decr(final String key) {
|
||||
client.decr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incrBy(final String key, final int integer) {
|
||||
client.incrBy(key, integer);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String incr(final String key) {
|
||||
client.incr(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String append(final String key, final String value) {
|
||||
client.append(key, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String substr(final String key, final int start, final int end) {
|
||||
client.substr(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hset(final String key, final String field, final String value) {
|
||||
client.hset(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hget(final String key, final String field) {
|
||||
client.hget(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hsetnx(final String key, final String field,
|
||||
final String value) {
|
||||
client.hsetnx(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmset(final String key, final Map<String, String> hash) {
|
||||
client.hmset(key, hash);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hmget(final String key, final String... fields) {
|
||||
client.hmget(key, fields);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hincrBy(final String key, final String field, final int value) {
|
||||
client.hincrBy(key, field, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hexists(final String key, final String field) {
|
||||
client.hexists(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hdel(final String key, final String field) {
|
||||
client.hdel(key, field);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hlen(final String key) {
|
||||
client.hlen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hkeys(final String key) {
|
||||
client.hkeys(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hvals(final String key) {
|
||||
client.hvals(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String hgetAll(final String key) {
|
||||
client.hgetAll(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpush(final String key, final String string) {
|
||||
client.rpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpush(final String key, final String string) {
|
||||
client.lpush(key, string);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String llen(final String key) {
|
||||
client.llen(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrange(final String key, final int start, final int end) {
|
||||
client.lrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ltrim(String key, final int start, final int end) {
|
||||
client.ltrim(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lindex(final String key, final int index) {
|
||||
client.lindex(key, index);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lset(final String key, final int index, final String value) {
|
||||
client.lset(key, index, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lrem(final String key, final int count, final String value) {
|
||||
client.lrem(key, count, value);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String lpop(final String key) {
|
||||
client.lpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpop(final String key) {
|
||||
client.rpop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String rpoplpush(final String srckey, final String dstkey) {
|
||||
client.rpoplpush(srckey, dstkey);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sadd(final String key, final String member) {
|
||||
client.sadd(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smembers(final String key) {
|
||||
client.smembers(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srem(final String key, final String member) {
|
||||
client.srem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String spop(final String key) {
|
||||
client.spop(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String smove(final String srckey, final String dstkey,
|
||||
final String member) {
|
||||
client.smove(srckey, dstkey, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String scard(final String key) {
|
||||
client.scard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sismember(final String key, final String member) {
|
||||
client.sismember(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinter(final String... keys) {
|
||||
client.sinter(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sinterstore(final String dstkey, final String... keys) {
|
||||
client.sinterstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunion(final String... keys) {
|
||||
client.sunion(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sunionstore(final String dstkey, final String... keys) {
|
||||
client.sunionstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiff(final String... keys) {
|
||||
client.sdiff(keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sdiffstore(final String dstkey, final String... keys) {
|
||||
client.sdiffstore(dstkey, keys);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String srandmember(final String key) {
|
||||
client.srandmember(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zadd(final String key, final double score, final String member) {
|
||||
client.zadd(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrange(final String key, final int start, final int end) {
|
||||
client.zrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrem(final String key, final String member) {
|
||||
client.zrem(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zincrby(final String key, final double score,
|
||||
final String member) {
|
||||
client.zincrby(key, score, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrank(final String key, final String member) {
|
||||
client.zrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrank(final String key, final String member) {
|
||||
client.zrevrank(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrange(final String key, final int start, final int end) {
|
||||
client.zrevrange(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrangeWithScores(final String key, final int start,
|
||||
final int end) {
|
||||
client.zrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zrevrangeWithScores(final String key, final int start,
|
||||
final int end) {
|
||||
client.zrevrangeWithScores(key, start, end);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zcard(final String key) {
|
||||
client.zcard(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String zscore(final String key, final String member) {
|
||||
client.zscore(key, member);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sort(final String key) {
|
||||
client.sort(key);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String sort(final String key, final SortingParams sortingParameters) {
|
||||
client.sort(key, sortingParameters);
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,21 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Tuple {
|
||||
private String element;
|
||||
private byte[] element;
|
||||
private Double score;
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((element == null) ? 0 : element.hashCode());
|
||||
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));
|
||||
@@ -27,7 +34,7 @@ public class Tuple {
|
||||
if (element == null) {
|
||||
if (other.element != null)
|
||||
return false;
|
||||
} else if (!element.equals(other.element))
|
||||
} else if (!Arrays.equals(element, other.element))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(score) != Double
|
||||
.doubleToLongBits(other.score))
|
||||
@@ -37,15 +44,33 @@ public class Tuple {
|
||||
|
||||
public Tuple(String element, Double score) {
|
||||
super();
|
||||
this.element = element;
|
||||
this.element = element.getBytes(Protocol.UTF8);
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public Tuple(byte[] element, Double score) {
|
||||
super();
|
||||
this.element = element;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String getElement() {
|
||||
return element;
|
||||
if(null != element) {
|
||||
return new String(element, Protocol.UTF8);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getBinaryElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return '['+Arrays.toString(element)+','+score+']';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,29 +5,36 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static redis.clients.jedis.Protocol.Keyword.*;
|
||||
|
||||
public class ZParams {
|
||||
public enum Aggregate {
|
||||
SUM, MIN, MAX
|
||||
SUM, MIN, MAX;
|
||||
|
||||
public final byte[] raw;
|
||||
Aggregate() {
|
||||
raw = name().getBytes(Protocol.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> params = new ArrayList<String>();
|
||||
private List<byte[]> params = new ArrayList<byte[]>();
|
||||
|
||||
public ZParams weights(int... weights) {
|
||||
params.add("WEIGHTS");
|
||||
for (int weight : weights) {
|
||||
params.add(String.valueOf(weight));
|
||||
public ZParams weights(final int... weights) {
|
||||
params.add(WEIGHTS.raw);
|
||||
for (final int weight : weights) {
|
||||
params.add(Protocol.toByteArray(weight));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Collection<String> getParams() {
|
||||
return Collections.unmodifiableCollection(params);
|
||||
public Collection<byte[]> getParams() {
|
||||
return Collections.unmodifiableCollection(params);
|
||||
}
|
||||
|
||||
public ZParams aggregate(Aggregate aggregate) {
|
||||
params.add("AGGREGATE");
|
||||
params.add(aggregate.name());
|
||||
public ZParams aggregate(final Aggregate aggregate) {
|
||||
params.add(AGGREGATE.raw);
|
||||
params.add(aggregate.raw);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,19 @@ 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));
|
||||
}
|
||||
|
||||
public long hash(byte[] key) {
|
||||
if (md5 == null) {
|
||||
try {
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
@@ -20,7 +26,7 @@ public interface Hashing {
|
||||
}
|
||||
|
||||
md5.reset();
|
||||
md5.update(key.getBytes());
|
||||
md5.update(key);
|
||||
byte[] bKey = md5.digest();
|
||||
long res = ((long) (bKey[3] & 0xFF) << 24)
|
||||
| ((long) (bKey[2] & 0xFF) << 16)
|
||||
@@ -30,4 +36,5 @@ public interface Hashing {
|
||||
};
|
||||
|
||||
public long hash(String key);
|
||||
public long hash(byte[] key);
|
||||
}
|
||||
148
src/main/java/redis/clients/util/JedisByteHashMap.java
Normal file
148
src/main/java/redis/clients/util/JedisByteHashMap.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package redis.clients.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class JedisByteHashMap implements Map<byte[], byte[]>, Cloneable,
|
||||
Serializable {
|
||||
private static final long serialVersionUID = -6971431362627219416L;
|
||||
private Map<ByteArrayWrapper, byte[]> internalMap = new HashMap<ByteArrayWrapper, byte[]>();
|
||||
|
||||
public void clear() {
|
||||
internalMap.clear();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
if (key instanceof byte[])
|
||||
return internalMap.containsKey(new ByteArrayWrapper((byte[]) key));
|
||||
return internalMap.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
return internalMap.containsValue(value);
|
||||
}
|
||||
|
||||
public Set<java.util.Map.Entry<byte[], byte[]>> entrySet() {
|
||||
Iterator<java.util.Map.Entry<ByteArrayWrapper, byte[]>> iterator = internalMap
|
||||
.entrySet().iterator();
|
||||
HashSet<Entry<byte[], byte[]>> hashSet = new HashSet<java.util.Map.Entry<byte[], byte[]>>();
|
||||
while (iterator.hasNext()) {
|
||||
Entry<ByteArrayWrapper, byte[]> entry = iterator.next();
|
||||
hashSet.add(new JedisByteEntry(entry.getKey().data, entry
|
||||
.getValue()));
|
||||
}
|
||||
return hashSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] get(Object key) {
|
||||
if (key instanceof byte[])
|
||||
return internalMap.get(new ByteArrayWrapper((byte[]) key));
|
||||
return internalMap.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return internalMap.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<byte[]> keySet() {
|
||||
Set<byte[]> keySet = new HashSet<byte[]>();
|
||||
Iterator<ByteArrayWrapper> iterator = internalMap.keySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
keySet.add(iterator.next().data);
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] put(byte[] key, byte[] value) {
|
||||
return internalMap.put(new ByteArrayWrapper(key), value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void putAll(Map<? extends byte[], ? extends byte[]> m) {
|
||||
Iterator<?> iterator = m.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Entry<? extends byte[], ? extends byte[]> next = (Entry<? extends byte[], ? extends byte[]>) iterator
|
||||
.next();
|
||||
internalMap.put(new ByteArrayWrapper(next.getKey()), next
|
||||
.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] remove(Object key) {
|
||||
if (key instanceof byte[])
|
||||
return internalMap.remove(new ByteArrayWrapper((byte[]) key));
|
||||
return internalMap.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return internalMap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<byte[]> values() {
|
||||
return internalMap.values();
|
||||
}
|
||||
|
||||
private final class ByteArrayWrapper {
|
||||
private final byte[] data;
|
||||
|
||||
public ByteArrayWrapper(byte[] data) {
|
||||
if (data == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof ByteArrayWrapper)) {
|
||||
return false;
|
||||
}
|
||||
return Arrays.equals(data, ((ByteArrayWrapper) other).data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(data);
|
||||
}
|
||||
}
|
||||
|
||||
private final class JedisByteEntry implements Entry<byte[], byte[]> {
|
||||
private byte[] value;
|
||||
private byte[] key;
|
||||
|
||||
public JedisByteEntry(byte[] key, byte[] value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public byte[] getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] setValue(byte[] value) {
|
||||
this.value = value;
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ 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.
|
||||
@@ -156,7 +158,11 @@ public class MurmurHash implements Hashing {
|
||||
return h;
|
||||
}
|
||||
|
||||
public long hash(byte[] key) {
|
||||
return hash64A(key, 0x1234ABCD);
|
||||
}
|
||||
|
||||
public long hash(String key) {
|
||||
return hash64A(key.getBytes(), 0x1234ABCD);
|
||||
return hash(key.getBytes(Protocol.UTF8));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package redis.clients.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* The class implements a buffered output stream without synchronization
|
||||
@@ -12,13 +11,12 @@ public final class RedisOutputStream extends FilterOutputStream {
|
||||
protected final byte buf[];
|
||||
|
||||
protected int count;
|
||||
public static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
public RedisOutputStream(OutputStream out) {
|
||||
public RedisOutputStream(final OutputStream out) {
|
||||
this(out, 8192);
|
||||
}
|
||||
|
||||
public RedisOutputStream(OutputStream out, int size) {
|
||||
public RedisOutputStream(final OutputStream out, final int size) {
|
||||
super(out);
|
||||
if (size <= 0) {
|
||||
throw new IllegalArgumentException("Buffer size <= 0");
|
||||
@@ -33,14 +31,18 @@ public final class RedisOutputStream extends FilterOutputStream {
|
||||
}
|
||||
}
|
||||
|
||||
public void write(int b) throws IOException {
|
||||
buf[count++] = (byte) b;
|
||||
public void write(final byte b) throws IOException {
|
||||
buf[count++] = b;
|
||||
if (count == buf.length) {
|
||||
flushBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
public void write(final byte[] b) throws IOException {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
public void write(byte b[], int off, int len) throws IOException {
|
||||
public void write(final byte b[], final int off, final int len) throws IOException {
|
||||
if (len >= buf.length) {
|
||||
flushBuffer();
|
||||
out.write(b, off, len);
|
||||
@@ -54,7 +56,7 @@ public final class RedisOutputStream extends FilterOutputStream {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeAsciiCrLf(String in) throws IOException {
|
||||
public void writeAsciiCrLf(final String in) throws IOException {
|
||||
final int size = in.length();
|
||||
|
||||
for (int i = 0; i != size; ++i) {
|
||||
@@ -67,11 +69,11 @@ public final class RedisOutputStream extends FilterOutputStream {
|
||||
writeCrLf();
|
||||
}
|
||||
|
||||
public static boolean isSurrogate(char ch) {
|
||||
public static boolean isSurrogate(final char ch) {
|
||||
return ch >= Character.MIN_SURROGATE && ch <= Character.MAX_SURROGATE;
|
||||
}
|
||||
|
||||
public static int utf8Length (String str) {
|
||||
public static int utf8Length (final String str) {
|
||||
int strLen = str.length(), utfLen = 0;
|
||||
for(int i = 0; i != strLen; ++i) {
|
||||
char c = str.charAt(i);
|
||||
@@ -98,7 +100,7 @@ public final class RedisOutputStream extends FilterOutputStream {
|
||||
buf[count++] = '\n';
|
||||
}
|
||||
|
||||
public void writeUtf8CrLf(String str) throws IOException {
|
||||
public void writeUtf8CrLf(final String str) throws IOException {
|
||||
int strLen = str.length();
|
||||
|
||||
int i;
|
||||
|
||||
@@ -69,10 +69,19 @@ public class Sharded<R, S extends ShardInfo<R>> {
|
||||
}
|
||||
}
|
||||
|
||||
public R getShard(String key) {
|
||||
return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue()
|
||||
.getResource();
|
||||
}
|
||||
public R getShard(byte[] key) {
|
||||
return nodes
|
||||
.floorEntry(algo.hash(key))
|
||||
.getValue()
|
||||
.getResource();
|
||||
}
|
||||
|
||||
public R getShard(String key) {
|
||||
return nodes
|
||||
.floorEntry(algo.hash(getKeyTag(key)))
|
||||
.getValue()
|
||||
.getResource();
|
||||
}
|
||||
|
||||
public S getShardInfo(String key) {
|
||||
return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue();
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
@@ -13,58 +12,61 @@ import java.util.Set;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
import redis.clients.jedis.Transaction;
|
||||
import redis.clients.jedis.Protocol.Command;
|
||||
|
||||
public class JedisNewCommandsCheckTest extends Assert {
|
||||
@Test
|
||||
public void checkJedisIsUpdated() throws IOException {
|
||||
String[] commands = getAvailableCommands();
|
||||
Set<String> implementedCommands = getImplementedCommands();
|
||||
String[] commands = getAvailableCommands();
|
||||
Set<String> implementedCommands = getImplementedCommands();
|
||||
|
||||
Set<String> missingCommands = new HashSet<String>();
|
||||
for (String command : commands) {
|
||||
if (!implementedCommands.contains(command.trim())) {
|
||||
missingCommands.add(command);
|
||||
}
|
||||
}
|
||||
Set<String> missingCommands = new HashSet<String>();
|
||||
for (String command : commands) {
|
||||
if (!implementedCommands.contains(command.trim())) {
|
||||
missingCommands.add(command);
|
||||
}
|
||||
}
|
||||
|
||||
if (!missingCommands.isEmpty()) {
|
||||
fail("There are missing commands: " + missingCommands.toString());
|
||||
}
|
||||
if (!missingCommands.isEmpty()) {
|
||||
fail("There are missing commands: " + missingCommands.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getImplementedCommands() {
|
||||
Method[] methods = Jedis.class.getDeclaredMethods();
|
||||
Set<String> implementedCommands = new HashSet<String>();
|
||||
for (Method method : methods) {
|
||||
implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
}
|
||||
|
||||
methods = JedisPubSub.class.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
}
|
||||
|
||||
methods = Transaction.class.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
}
|
||||
implementedCommands.add("config");
|
||||
return implementedCommands;
|
||||
// Method[] methods = Jedis.class.getDeclaredMethods();
|
||||
// Set<String> implementedCommands = new HashSet<String>();
|
||||
// for (Method method : methods) {
|
||||
// implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
// }
|
||||
//
|
||||
// methods = JedisPubSub.class.getDeclaredMethods();
|
||||
// for (Method method : methods) {
|
||||
// implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
// }
|
||||
//
|
||||
// methods = Transaction.class.getDeclaredMethods();
|
||||
// for (Method method : methods) {
|
||||
// implementedCommands.add(method.getName().trim().toLowerCase());
|
||||
// }
|
||||
// implementedCommands.add("config");
|
||||
// return implementedCommands;
|
||||
Set<String> implementedCommands = new HashSet<String>();
|
||||
for (Command cmd : Command.values()) {
|
||||
implementedCommands.add(cmd.name().toLowerCase());
|
||||
}
|
||||
return implementedCommands;
|
||||
}
|
||||
|
||||
private String[] getAvailableCommands() throws MalformedURLException,
|
||||
IOException {
|
||||
URL url = new URL("http://dimaion.com/redis/master");
|
||||
InputStream openStream = url.openStream();
|
||||
DataInputStream dis = new DataInputStream(new BufferedInputStream(
|
||||
openStream));
|
||||
byte[] all = new byte[dis.available()];
|
||||
dis.readFully(all);
|
||||
String commandList = new String(all);
|
||||
String[] commands = commandList.split("\n");
|
||||
return commands;
|
||||
IOException {
|
||||
URL url = new URL("http://dimaion.com/redis/master");
|
||||
InputStream openStream = url.openStream();
|
||||
DataInputStream dis = new DataInputStream(new BufferedInputStream(
|
||||
openStream));
|
||||
byte[] all = new byte[dis.available()];
|
||||
dis.readFully(all);
|
||||
String commandList = new String(all);
|
||||
String[] commands = commandList.split("\n");
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,36 +9,35 @@ import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisShardInfo;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
|
||||
public class JedisTest extends JedisCommandTestBase {
|
||||
@Test
|
||||
public void useWithoutConnecting() {
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.auth("foobared");
|
||||
jedis.dbSize();
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.auth("foobared");
|
||||
jedis.dbSize();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkBinaryData() {
|
||||
byte[] bigdata = new byte[1777];
|
||||
for (int b = 0; b < bigdata.length; b++) {
|
||||
bigdata[b] = (byte) ((byte) b % 255);
|
||||
}
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("data", new String(bigdata, RedisOutputStream.CHARSET));
|
||||
byte[] bigdata = new byte[1777];
|
||||
for (int b = 0; b < bigdata.length; b++) {
|
||||
bigdata[b] = (byte) ((byte) b % 255);
|
||||
}
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("data", new String(bigdata, Protocol.UTF8));
|
||||
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectWithShardInfo() {
|
||||
JedisShardInfo shardInfo = new JedisShardInfo("localhost",
|
||||
Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
JedisShardInfo shardInfo = new JedisShardInfo("localhost",
|
||||
Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
}
|
||||
}
|
||||
|
||||
25
src/test/java/redis/clients/jedis/tests/JedisTestBase.java
Normal file
25
src/test/java/redis/clients/jedis/tests/JedisTestBase.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public abstract class JedisTestBase extends Assert {
|
||||
protected void assertEquals(List<byte[]> expected, List<byte[]> actual) {
|
||||
assertEquals(expected.size(), actual.size());
|
||||
for (int n = 0; n < expected.size(); n++) {
|
||||
assertArrayEquals(expected.get(n), actual.get(n));
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertEquals(Set<byte[]> expected, Set<byte[]> actual) {
|
||||
assertEquals(expected.size(), actual.size());
|
||||
Iterator<byte[]> iterator = expected.iterator();
|
||||
Iterator<byte[]> iterator2 = actual.iterator();
|
||||
while (iterator.hasNext() || iterator2.hasNext()) {
|
||||
assertArrayEquals(iterator.next(), iterator2.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,14 @@ import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPipeline;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
||||
|
||||
public class PipeliningTest extends Assert {
|
||||
@@ -36,7 +37,7 @@ public class PipeliningTest extends Assert {
|
||||
});
|
||||
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("OK", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
assertArrayEquals("OK".getBytes(Protocol.UTF8), (byte[])results.get(0));
|
||||
assertArrayEquals("bar".getBytes(Protocol.UTF8), (byte[])results.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,119 +9,102 @@ import java.io.PipedOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.util.RedisInputStream;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
|
||||
public class ProtocolTest extends Assert {
|
||||
public class ProtocolTest extends JedisTestBase {
|
||||
@Test
|
||||
public void buildACommand() throws IOException {
|
||||
PipedInputStream pis = new PipedInputStream();
|
||||
BufferedInputStream bis = new BufferedInputStream(pis);
|
||||
PipedOutputStream pos = new PipedOutputStream(pis);
|
||||
PipedInputStream pis = new PipedInputStream();
|
||||
BufferedInputStream bis = new BufferedInputStream(pis);
|
||||
PipedOutputStream pos = new PipedOutputStream(pis);
|
||||
|
||||
Protocol protocol = new Protocol();
|
||||
protocol.sendCommand(new RedisOutputStream(pos), "GET", "SOMEKEY");
|
||||
Protocol protocol = new Protocol();
|
||||
protocol.sendCommand(new RedisOutputStream(pos), Protocol.Command.GET,
|
||||
"SOMEKEY".getBytes(Protocol.UTF8));
|
||||
|
||||
pos.close();
|
||||
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
|
||||
pos.close();
|
||||
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
|
||||
|
||||
int b;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((b = bis.read()) != -1) {
|
||||
sb.append((char) b);
|
||||
}
|
||||
int b;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((b = bis.read()) != -1) {
|
||||
sb.append((char) b);
|
||||
}
|
||||
|
||||
assertEquals(expectedCommand, sb.toString());
|
||||
assertEquals(expectedCommand, sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bulkReply() {
|
||||
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = (String) protocol.read(new RedisInputStream(is));
|
||||
assertEquals("foobar", response);
|
||||
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
|
||||
assertArrayEquals("foobar".getBytes(Protocol.UTF8), response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fragmentedBulkReply() {
|
||||
FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream(
|
||||
"$30\r\n012345678901234567890123456789\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = (String) protocol.read(new RedisInputStream(fis));
|
||||
assertEquals("012345678901234567890123456789", response);
|
||||
FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream(
|
||||
"$30\r\n012345678901234567890123456789\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
byte[] response = (byte[]) protocol.read(new RedisInputStream(fis));
|
||||
assertArrayEquals("012345678901234567890123456789"
|
||||
.getBytes(Protocol.UTF8), response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullBulkReply() {
|
||||
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = (String) protocol.read(new RedisInputStream(is));
|
||||
assertEquals(null, response);
|
||||
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = (String) protocol.read(new RedisInputStream(is));
|
||||
assertEquals(null, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleLineReply() {
|
||||
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = (String) protocol.read(new RedisInputStream(is));
|
||||
assertEquals("OK", response);
|
||||
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
|
||||
assertArrayEquals("OK".getBytes(Protocol.UTF8), response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void integerReply() {
|
||||
InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
int response = (Integer) protocol.read(new RedisInputStream(is));
|
||||
assertEquals(123, response);
|
||||
InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
int response = (Integer) protocol.read(new RedisInputStream(is));
|
||||
assertEquals(123, response);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void multiBulkReply() {
|
||||
InputStream is = new ByteArrayInputStream(
|
||||
"*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n"
|
||||
.getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
List<String> response = (List<String>) (List<?>) protocol
|
||||
.read(new RedisInputStream(is));
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("foo");
|
||||
expected.add("bar");
|
||||
expected.add("Hello");
|
||||
expected.add("World");
|
||||
InputStream is = new ByteArrayInputStream(
|
||||
"*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n"
|
||||
.getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
List<byte[]> response = (List<byte[]>) protocol
|
||||
.read(new RedisInputStream(is));
|
||||
List<byte[]> expected = new ArrayList<byte[]>();
|
||||
expected.add("foo".getBytes(Protocol.UTF8));
|
||||
expected.add("bar".getBytes(Protocol.UTF8));
|
||||
expected.add("Hello".getBytes(Protocol.UTF8));
|
||||
expected.add("World".getBytes(Protocol.UTF8));
|
||||
|
||||
assertEquals(expected, response);
|
||||
|
||||
is = new ByteArrayInputStream(
|
||||
"*4\r\n$3\r\nfoo\r\n+OK\r\n:1000\r\n*2\r\n$3\r\nfoo\r\n$3\r\nbar"
|
||||
.getBytes());
|
||||
protocol = new Protocol();
|
||||
List<Object> response2 = (List<Object>) protocol
|
||||
.read(new RedisInputStream(is));
|
||||
List<Object> expected2 = new ArrayList<Object>();
|
||||
expected2.add("foo");
|
||||
expected2.add("OK");
|
||||
expected2.add(1000);
|
||||
List<Object> sub = new ArrayList<Object>();
|
||||
sub.add("foo");
|
||||
sub.add("bar");
|
||||
expected2.add(sub);
|
||||
|
||||
assertEquals(expected2, response2);
|
||||
assertEquals(expected, response);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void nullMultiBulkReply() {
|
||||
InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
List<String> response = (List<String>) protocol
|
||||
.read(new RedisInputStream(is));
|
||||
assertNull(response);
|
||||
InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
List<String> response = (List<String>) protocol
|
||||
.read(new RedisInputStream(is));
|
||||
assertNull(response);
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class ShardedJedisPoolTest extends Assert {
|
||||
@Test
|
||||
public void checkConnectionWithDefaultPort() throws TimeoutException {
|
||||
ShardedJedisPool pool = new ShardedJedisPool(shards);
|
||||
pool.setResourcesNumber(10);
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
ShardedJedis jedis = pool.getResource(200);
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisShardInfo;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.ShardedJedis;
|
||||
import redis.clients.jedis.ShardedJedisPipeline;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
||||
@@ -141,7 +142,12 @@ public class ShardedJedisTest extends Assert {
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals("a", results.get(0));
|
||||
assertEquals("b", results.get(1));
|
||||
List<Object> expected = new ArrayList<Object>(2);
|
||||
expected.add("a".getBytes(Protocol.UTF8));
|
||||
expected.add("b".getBytes(Protocol.UTF8));
|
||||
|
||||
assertEquals(2, results.size());
|
||||
assertArrayEquals("a".getBytes(Protocol.UTF8), (byte[]) results.get(0));
|
||||
assertArrayEquals("b".getBytes(Protocol.UTF8), (byte[]) results.get(1));
|
||||
}
|
||||
}
|
||||
@@ -1,238 +1,454 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.JedisException;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bfoo1 = { 0x01, 0x02, 0x03, 0x04, 0x0A };
|
||||
final byte[] bfoo2 = { 0x01, 0x02, 0x03, 0x04, 0x0B };
|
||||
final byte[] bfoo3 = { 0x01, 0x02, 0x03, 0x04, 0x0C };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A };
|
||||
final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B };
|
||||
final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C };
|
||||
|
||||
final byte[] bfoobar = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bfoostar = { 0x01, 0x02, 0x03, 0x04, '*' };
|
||||
final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' };
|
||||
|
||||
@Test
|
||||
public void ping() {
|
||||
String status = jedis.ping();
|
||||
assertEquals("PONG", status);
|
||||
String status = jedis.ping();
|
||||
assertEquals("PONG", status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exists() {
|
||||
String status = jedis.set("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
String status = jedis.set("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
|
||||
int reply = jedis.exists("foo");
|
||||
assertEquals(1, reply);
|
||||
status = jedis.set(bfoo, bbar);
|
||||
assertEquals("OK", status);
|
||||
|
||||
reply = jedis.del("foo");
|
||||
assertEquals(1, reply);
|
||||
int reply = jedis.exists("foo");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.exists("foo");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists(bfoo);
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del("foo");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del(bfoo);
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.exists("foo");
|
||||
assertEquals(0, reply);
|
||||
|
||||
reply = jedis.exists(bfoo);
|
||||
assertEquals(0, reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() {
|
||||
jedis.set("foo1", "bar1");
|
||||
jedis.set("foo2", "bar2");
|
||||
jedis.set("foo3", "bar3");
|
||||
jedis.set("foo1", "bar1");
|
||||
jedis.set("foo2", "bar2");
|
||||
jedis.set("foo3", "bar3");
|
||||
|
||||
int reply = jedis.del("foo1", "foo2", "foo3");
|
||||
assertEquals(3, reply);
|
||||
int reply = jedis.del("foo1", "foo2", "foo3");
|
||||
assertEquals(3, reply);
|
||||
|
||||
reply = jedis.exists("foo1");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo2");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo3");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo1");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo2");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo3");
|
||||
assertEquals(0, reply);
|
||||
|
||||
jedis.set("foo1", "bar1");
|
||||
jedis.set("foo1", "bar1");
|
||||
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(1, reply);
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(0, reply);
|
||||
|
||||
// Binary ...
|
||||
jedis.set(bfoo1, bbar1);
|
||||
jedis.set(bfoo2, bbar2);
|
||||
jedis.set(bfoo3, bbar3);
|
||||
|
||||
reply = jedis.del(bfoo1, bfoo2, bfoo3);
|
||||
assertEquals(3, reply);
|
||||
|
||||
reply = jedis.exists(bfoo1);
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists(bfoo2);
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists(bfoo3);
|
||||
assertEquals(0, reply);
|
||||
|
||||
jedis.set(bfoo1, bbar1);
|
||||
|
||||
reply = jedis.del(bfoo1, bfoo2);
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del(bfoo1, bfoo2);
|
||||
assertEquals(0, reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void type() {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.type("foo");
|
||||
assertEquals("string", status);
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.type("foo");
|
||||
assertEquals("string", status);
|
||||
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
status = jedis.type(bfoo);
|
||||
assertEquals("string", status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keys() {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.set("foobar", "bar");
|
||||
jedis.set("foo", "bar");
|
||||
jedis.set("foobar", "bar");
|
||||
|
||||
List<String> keys = jedis.keys("foo*");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("foo");
|
||||
expected.add("foobar");
|
||||
assertEquals(expected, keys);
|
||||
Set<String> keys = jedis.keys("foo*");
|
||||
Set<String> expected = new HashSet<String>();
|
||||
expected.add("foo");
|
||||
expected.add("foobar");
|
||||
assertEquals(expected, keys);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
keys = jedis.keys("bar*");
|
||||
expected = new HashSet<String>();
|
||||
keys = jedis.keys("bar*");
|
||||
|
||||
assertEquals(expected, keys);
|
||||
assertEquals(expected, keys);
|
||||
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
jedis.set(bfoobar, bbar);
|
||||
|
||||
Set<byte[]> bkeys = jedis.keys(bfoostar);
|
||||
assertEquals(2, bkeys.size());
|
||||
assertTrue(setContains(bkeys, bfoo));
|
||||
assertTrue(setContains(bkeys, bfoobar));
|
||||
|
||||
bkeys = jedis.keys(bbarstar);
|
||||
|
||||
assertEquals(0, bkeys.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomKey() {
|
||||
assertEquals(null, jedis.randomKey());
|
||||
assertEquals(null, jedis.randomKey());
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
jedis.set("foo", "bar");
|
||||
|
||||
assertEquals("foo", jedis.randomKey());
|
||||
assertEquals("foo", jedis.randomKey());
|
||||
|
||||
jedis.set("bar", "foo");
|
||||
jedis.set("bar", "foo");
|
||||
|
||||
String randomkey = jedis.randomKey();
|
||||
assertTrue(randomkey.equals("foo") || randomkey.equals("bar"));
|
||||
|
||||
// Binary
|
||||
jedis.del("foo");
|
||||
jedis.del("bar");
|
||||
assertEquals(null, jedis.randomKey());
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
|
||||
assertArrayEquals(bfoo, jedis.randomBinaryKey());
|
||||
|
||||
jedis.set(bbar, bfoo);
|
||||
|
||||
byte[] randomBkey = jedis.randomBinaryKey();
|
||||
assertTrue(Arrays.equals(randomBkey, bfoo)
|
||||
|| Arrays.equals(randomBkey, bbar));
|
||||
|
||||
String randomkey = jedis.randomKey();
|
||||
assertTrue(randomkey.equals("foo") || randomkey.equals("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rename() {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.rename("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.rename("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
|
||||
String value = jedis.get("foo");
|
||||
assertEquals(null, value);
|
||||
String value = jedis.get("foo");
|
||||
assertEquals(null, value);
|
||||
|
||||
value = jedis.get("bar");
|
||||
assertEquals("bar", value);
|
||||
value = jedis.get("bar");
|
||||
assertEquals("bar", value);
|
||||
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
String bstatus = jedis.rename(bfoo, bbar);
|
||||
assertEquals("OK", bstatus);
|
||||
|
||||
byte[] bvalue = jedis.get(bfoo);
|
||||
assertEquals(null, bvalue);
|
||||
|
||||
bvalue = jedis.get(bbar);
|
||||
assertArrayEquals(bbar, bvalue);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
@Test
|
||||
public void renameOldAndNewAreTheSame() {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.rename("foo", "foo");
|
||||
try {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.rename("foo", "foo");
|
||||
fail("JedisException expected");
|
||||
} catch (final JedisException e) {
|
||||
}
|
||||
|
||||
// Binary
|
||||
try {
|
||||
jedis.set(bfoo, bbar);
|
||||
jedis.rename(bfoo, bfoo);
|
||||
fail("JedisException expected");
|
||||
} catch (final JedisException e) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renamenx() {
|
||||
jedis.set("foo", "bar");
|
||||
int status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(1, status);
|
||||
jedis.set("foo", "bar");
|
||||
int status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(1, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
int bstatus = jedis.renamenx(bfoo, bbar);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
bstatus = jedis.renamenx(bfoo, bbar);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dbSize() {
|
||||
int size = jedis.dbSize();
|
||||
assertEquals(0, size);
|
||||
int size = jedis.dbSize();
|
||||
assertEquals(0, size);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
size = jedis.dbSize();
|
||||
assertEquals(1, size);
|
||||
jedis.set("foo", "bar");
|
||||
size = jedis.dbSize();
|
||||
assertEquals(1, size);
|
||||
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
size = jedis.dbSize();
|
||||
assertEquals(2, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expire() {
|
||||
int status = jedis.expire("foo", 20);
|
||||
assertEquals(0, status);
|
||||
int status = jedis.expire("foo", 20);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.expire("foo", 20);
|
||||
assertEquals(1, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.expire(bfoo, 20);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
bstatus = jedis.expire(bfoo, 20);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.expire("foo", 20);
|
||||
assertEquals(1, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expireAt() {
|
||||
long unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
long unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
|
||||
int status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(0, status);
|
||||
int status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(1, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.expireAt(bfoo, unixTime);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
bstatus = jedis.expireAt(bfoo, unixTime);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(1, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ttl() {
|
||||
int ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
int ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
jedis.set("foo", "bar");
|
||||
ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
|
||||
jedis.expire("foo", 20);
|
||||
ttl = jedis.ttl("foo");
|
||||
assertTrue(ttl >= 0 && ttl <= 20);
|
||||
|
||||
// Binary
|
||||
int bttl = jedis.ttl(bfoo);
|
||||
assertEquals(-1, bttl);
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
bttl = jedis.ttl(bfoo);
|
||||
assertEquals(-1, bttl);
|
||||
|
||||
jedis.expire(bfoo, 20);
|
||||
bttl = jedis.ttl(bfoo);
|
||||
assertTrue(bttl >= 0 && bttl <= 20);
|
||||
|
||||
jedis.expire("foo", 20);
|
||||
ttl = jedis.ttl("foo");
|
||||
assertTrue(ttl >= 0 && ttl <= 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select() {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.select(1);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
status = jedis.select(0);
|
||||
assertEquals("OK", status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.select(1);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
status = jedis.select(0);
|
||||
assertEquals("OK", status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
// Binary
|
||||
jedis.set(bfoo, bbar);
|
||||
String bstatus = jedis.select(1);
|
||||
assertEquals("OK", bstatus);
|
||||
assertEquals(null, jedis.get(bfoo));
|
||||
bstatus = jedis.select(0);
|
||||
assertEquals("OK", bstatus);
|
||||
assertArrayEquals(bbar, jedis.get(bfoo));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void move() {
|
||||
int status = jedis.move("foo", 1);
|
||||
assertEquals(0, status);
|
||||
int status = jedis.move("foo", 1);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.move("foo", 1);
|
||||
assertEquals(1, status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.move("foo", 1);
|
||||
assertEquals(1, status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
|
||||
jedis.select(1);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
|
||||
// Binary
|
||||
jedis.select(0);
|
||||
int bstatus = jedis.move(bfoo, 1);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.set(bfoo, bbar);
|
||||
bstatus = jedis.move(bfoo, 1);
|
||||
assertEquals(1, bstatus);
|
||||
assertEquals(null, jedis.get(bfoo));
|
||||
|
||||
jedis.select(1);
|
||||
assertArrayEquals(bbar, jedis.get(bfoo));
|
||||
|
||||
jedis.select(1);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flushDB() {
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushDB();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushDB();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.del("bar");
|
||||
|
||||
// Binary
|
||||
jedis.select(0);
|
||||
jedis.set(bfoo, bbar);
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set(bbar, bfoo);
|
||||
jedis.move(bbar, 1);
|
||||
String bstatus = jedis.flushDB();
|
||||
assertEquals("OK", bstatus);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flushAll() {
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushAll();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushAll();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
|
||||
// Binary
|
||||
jedis.select(0);
|
||||
jedis.set(bfoo, bbar);
|
||||
assertEquals(1, jedis.dbSize().intValue());
|
||||
jedis.set(bbar, bfoo);
|
||||
jedis.move(bbar, 1);
|
||||
String bstatus = jedis.flushAll();
|
||||
assertEquals("OK", bstatus);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
jedis.select(1);
|
||||
assertEquals(0, jedis.dbSize().intValue());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void persist() {
|
||||
jedis.setex("foo", 60 * 60, "bar");
|
||||
assertTrue(jedis.ttl("foo") > 0);
|
||||
int status = jedis.persist("foo");
|
||||
assertEquals(1, status);
|
||||
assertEquals(-1, jedis.ttl("foo").intValue());
|
||||
jedis.setex("foo", 60 * 60, "bar");
|
||||
assertTrue(jedis.ttl("foo") > 0);
|
||||
int status = jedis.persist("foo");
|
||||
assertEquals(1, status);
|
||||
assertEquals(-1, jedis.ttl("foo").intValue());
|
||||
|
||||
// Binary
|
||||
jedis.setex(bfoo, 60 * 60, bbar);
|
||||
assertTrue(jedis.ttl(bfoo) > 0);
|
||||
int bstatus = jedis.persist(bfoo);
|
||||
assertEquals(1, bstatus);
|
||||
assertEquals(-1, jedis.ttl(bfoo).intValue());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void echo() {
|
||||
String result = jedis.echo("hello world");
|
||||
assertEquals("hello world", result);
|
||||
String result = jedis.echo("hello world");
|
||||
assertEquals("hello world", result);
|
||||
|
||||
// Binary
|
||||
byte[] bresult = jedis.echo("hello world".getBytes(Protocol.UTF8));
|
||||
assertArrayEquals("hello world".getBytes(Protocol.UTF8), bresult);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.JedisException;
|
||||
import redis.clients.jedis.Protocol.Keyword;
|
||||
|
||||
public class BinaryValuesCommandsTest extends JedisCommandTestBase {
|
||||
byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
byte[] binaryValue;
|
||||
|
||||
@Before
|
||||
public void startUp() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int n = 0; n < 1000; n++) {
|
||||
sb.append("A");
|
||||
}
|
||||
|
||||
binaryValue = sb.toString().getBytes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAndGet() {
|
||||
String status = jedis.set(bfoo, binaryValue);
|
||||
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
|
||||
|
||||
byte[] value = jedis.get(bfoo);
|
||||
assertTrue(Arrays.equals(binaryValue, value));
|
||||
|
||||
assertNull(jedis.get(bbar));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSet() {
|
||||
byte[] value = jedis.getSet(bfoo, binaryValue);
|
||||
assertNull(value);
|
||||
value = jedis.get(bfoo);
|
||||
assertTrue(Arrays.equals(binaryValue, value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mget() {
|
||||
List<byte[]> values = jedis.mget(bfoo, bbar);
|
||||
List<byte[]> expected = new ArrayList<byte[]>();
|
||||
expected.add(null);
|
||||
expected.add(null);
|
||||
|
||||
assertEquals(expected, values);
|
||||
|
||||
jedis.set(bfoo, binaryValue);
|
||||
|
||||
expected = new ArrayList<byte[]>();
|
||||
expected.add(binaryValue);
|
||||
expected.add(null);
|
||||
values = jedis.mget(bfoo, bbar);
|
||||
|
||||
assertEquals(expected, values);
|
||||
|
||||
jedis.set(bbar, bfoo);
|
||||
|
||||
expected = new ArrayList<byte[]>();
|
||||
expected.add(binaryValue);
|
||||
expected.add(bfoo);
|
||||
values = jedis.mget(bfoo, bbar);
|
||||
|
||||
assertEquals(expected, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setnx() {
|
||||
int status = jedis.setnx(bfoo, binaryValue);
|
||||
assertEquals(1, status);
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
|
||||
status = jedis.setnx(bfoo, bbar);
|
||||
assertEquals(0, status);
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setex() {
|
||||
String status = jedis.setex(bfoo, 20, binaryValue);
|
||||
assertEquals(Keyword.OK.name(), status);
|
||||
int ttl = jedis.ttl(bfoo);
|
||||
assertTrue(ttl > 0 && ttl <= 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mset() {
|
||||
String status = jedis.mset(bfoo, binaryValue, bbar, bfoo);
|
||||
assertEquals(Keyword.OK.name(), status);
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
assertTrue(Arrays.equals(bfoo, jedis.get(bbar)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void msetnx() {
|
||||
int status = jedis.msetnx(bfoo, binaryValue, bbar, bfoo);
|
||||
assertEquals(1, status);
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
assertTrue(Arrays.equals(bfoo, jedis.get(bbar)));
|
||||
|
||||
status = jedis.msetnx(bfoo, bbar, "bar2".getBytes(), "foo2".getBytes());
|
||||
assertEquals(0, status);
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
assertTrue(Arrays.equals(bfoo, jedis.get(bbar)));
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void incrWrongValue() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
jedis.incr(bfoo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incr() {
|
||||
int value = jedis.incr(bfoo);
|
||||
assertEquals(1, value);
|
||||
value = jedis.incr(bfoo);
|
||||
assertEquals(2, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void incrByWrongValue() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
jedis.incrBy(bfoo, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incrBy() {
|
||||
int value = jedis.incrBy(bfoo, 2);
|
||||
assertEquals(2, value);
|
||||
value = jedis.incrBy(bfoo, 2);
|
||||
assertEquals(4, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void decrWrongValue() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
jedis.decr(bfoo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decr() {
|
||||
int value = jedis.decr(bfoo);
|
||||
assertEquals(-1, value);
|
||||
value = jedis.decr(bfoo);
|
||||
assertEquals(-2, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void decrByWrongValue() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
jedis.decrBy(bfoo, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decrBy() {
|
||||
int value = jedis.decrBy(bfoo, 2);
|
||||
assertEquals(-2, value);
|
||||
value = jedis.decrBy(bfoo, 2);
|
||||
assertEquals(-4, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void append() {
|
||||
byte[] first512 = new byte[512];
|
||||
System.arraycopy(binaryValue, 0, first512, 0, 512);
|
||||
int value = jedis.append(bfoo, first512);
|
||||
assertEquals(512, value);
|
||||
assertTrue(Arrays.equals(first512, jedis.get(bfoo)));
|
||||
|
||||
byte[] rest = new byte[binaryValue.length - 512];
|
||||
System.arraycopy(binaryValue, 512, rest, 0, binaryValue.length - 512);
|
||||
value = jedis.append(bfoo, rest);
|
||||
assertEquals(binaryValue.length, value);
|
||||
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void substr() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
|
||||
byte[] first512 = new byte[512];
|
||||
System.arraycopy(binaryValue, 0, first512, 0, 512);
|
||||
byte[] rfirst512 = jedis.substr(bfoo, 0, 511);
|
||||
assertTrue(Arrays.equals(first512, rfirst512));
|
||||
|
||||
byte[] last512 = new byte[512];
|
||||
System
|
||||
.arraycopy(binaryValue, binaryValue.length - 512, last512, 0,
|
||||
512);
|
||||
assertTrue(Arrays.equals(last512, jedis.substr(bfoo, -512, -1)));
|
||||
|
||||
assertTrue(Arrays.equals(binaryValue, jedis.substr(bfoo, 0, -1)));
|
||||
|
||||
assertTrue(Arrays.equals(last512, jedis.substr(bfoo,
|
||||
binaryValue.length - 512, 100000)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strlen() {
|
||||
jedis.set(bfoo, binaryValue);
|
||||
assertEquals(binaryValue.length, jedis.strlen(bfoo).intValue());
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,9 @@ public class ControlCommandsTest extends JedisCommandTestBase {
|
||||
String status = jedis.bgsave();
|
||||
assertEquals("Background saving started", status);
|
||||
} catch (JedisException e) {
|
||||
assertEquals("ERR Background save already in progress", e
|
||||
.getMessage());
|
||||
assertTrue(
|
||||
"ERR Background save already in progress"
|
||||
.equalsIgnoreCase(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,155 +3,287 @@ package redis.clients.jedis.tests.commands;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashesCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C };
|
||||
|
||||
@Test
|
||||
public void hset() {
|
||||
int status = jedis.hset("foo", "bar", "car");
|
||||
assertEquals(1, status);
|
||||
status = jedis.hset("foo", "bar", "foo");
|
||||
assertEquals(0, status);
|
||||
int status = jedis.hset("foo", "bar", "car");
|
||||
assertEquals(1, status);
|
||||
status = jedis.hset("foo", "bar", "foo");
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.hset(bfoo, bbar, bcar);
|
||||
assertEquals(1, bstatus);
|
||||
bstatus = jedis.hset(bfoo, bbar, bfoo);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hget() {
|
||||
jedis.hset("foo", "bar", "car");
|
||||
assertEquals(null, jedis.hget("bar", "foo"));
|
||||
assertEquals(null, jedis.hget("foo", "car"));
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
jedis.hset("foo", "bar", "car");
|
||||
assertEquals(null, jedis.hget("bar", "foo"));
|
||||
assertEquals(null, jedis.hget("foo", "car"));
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
|
||||
// Binary
|
||||
jedis.hset(bfoo, bbar, bcar);
|
||||
assertEquals(null, jedis.hget(bbar, bfoo));
|
||||
assertEquals(null, jedis.hget(bfoo, bcar));
|
||||
assertArrayEquals(bcar, jedis.hget(bfoo, bbar));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hsetnx() {
|
||||
int status = jedis.hsetnx("foo", "bar", "car");
|
||||
assertEquals(1, status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
int status = jedis.hsetnx("foo", "bar", "car");
|
||||
assertEquals(1, status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
|
||||
status = jedis.hsetnx("foo", "bar", "foo");
|
||||
assertEquals(0, status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
status = jedis.hsetnx("foo", "bar", "foo");
|
||||
assertEquals(0, status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
|
||||
status = jedis.hsetnx("foo", "car", "bar");
|
||||
assertEquals(1, status);
|
||||
assertEquals("bar", jedis.hget("foo", "car"));
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.hsetnx(bfoo, bbar, bcar);
|
||||
assertEquals(1, bstatus);
|
||||
assertArrayEquals(bcar, jedis.hget(bfoo, bbar));
|
||||
|
||||
bstatus = jedis.hsetnx(bfoo, bbar, bfoo);
|
||||
assertEquals(0, bstatus);
|
||||
assertArrayEquals(bcar, jedis.hget(bfoo, bbar));
|
||||
|
||||
bstatus = jedis.hsetnx(bfoo, bcar, bbar);
|
||||
assertEquals(1, bstatus);
|
||||
assertArrayEquals(bbar, jedis.hget(bfoo, bcar));
|
||||
|
||||
status = jedis.hsetnx("foo", "car", "bar");
|
||||
assertEquals(1, status);
|
||||
assertEquals("bar", jedis.hget("foo", "car"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmset() {
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
assertEquals("bar", jedis.hget("foo", "car"));
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals("car", jedis.hget("foo", "bar"));
|
||||
assertEquals("bar", jedis.hget("foo", "car"));
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
String bstatus = jedis.hmset(bfoo, bhash);
|
||||
assertEquals("OK", bstatus);
|
||||
assertArrayEquals(bcar, jedis.hget(bfoo, bbar));
|
||||
assertArrayEquals(bbar, jedis.hget(bfoo, bcar));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmget() {
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
List<String> values = jedis.hmget("foo", "bar", "car", "foo");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("car");
|
||||
expected.add("bar");
|
||||
expected.add(null);
|
||||
List<String> values = jedis.hmget("foo", "bar", "car", "foo");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("car");
|
||||
expected.add("bar");
|
||||
expected.add(null);
|
||||
|
||||
assertEquals(expected, values);
|
||||
assertEquals(expected, values);
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
List<byte[]> bvalues = jedis.hmget(bfoo, bbar, bcar, bfoo);
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bcar);
|
||||
bexpected.add(bbar);
|
||||
bexpected.add(null);
|
||||
|
||||
assertEquals(bexpected, bvalues);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hincrBy() {
|
||||
int value = jedis.hincrBy("foo", "bar", 1);
|
||||
assertEquals(1, value);
|
||||
value = jedis.hincrBy("foo", "bar", -1);
|
||||
assertEquals(0, value);
|
||||
value = jedis.hincrBy("foo", "bar", -10);
|
||||
assertEquals(-10, value);
|
||||
int value = jedis.hincrBy("foo", "bar", 1);
|
||||
assertEquals(1, value);
|
||||
value = jedis.hincrBy("foo", "bar", -1);
|
||||
assertEquals(0, value);
|
||||
value = jedis.hincrBy("foo", "bar", -10);
|
||||
assertEquals(-10, value);
|
||||
|
||||
// Binary
|
||||
int bvalue = jedis.hincrBy(bfoo, bbar, 1);
|
||||
assertEquals(1, bvalue);
|
||||
bvalue = jedis.hincrBy(bfoo, bbar, -1);
|
||||
assertEquals(0, bvalue);
|
||||
bvalue = jedis.hincrBy(bfoo, bbar, -10);
|
||||
assertEquals(-10, bvalue);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexists() {
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
assertEquals(0, jedis.hexists("bar", "foo").intValue());
|
||||
assertEquals(0, jedis.hexists("foo", "foo").intValue());
|
||||
assertEquals(1, jedis.hexists("foo", "bar").intValue());
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
assertEquals(0, jedis.hexists(bbar, bfoo).intValue());
|
||||
assertEquals(0, jedis.hexists(bfoo, bfoo).intValue());
|
||||
assertEquals(1, jedis.hexists(bfoo, bbar).intValue());
|
||||
|
||||
assertEquals(0, jedis.hexists("bar", "foo").intValue());
|
||||
assertEquals(0, jedis.hexists("foo", "foo").intValue());
|
||||
assertEquals(1, jedis.hexists("foo", "bar").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hdel() {
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
assertEquals(0, jedis.hdel("bar", "foo").intValue());
|
||||
assertEquals(0, jedis.hdel("foo", "foo").intValue());
|
||||
assertEquals(1, jedis.hdel("foo", "bar").intValue());
|
||||
assertEquals(null, jedis.hget("foo", "bar"));
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
assertEquals(0, jedis.hdel(bbar, bfoo).intValue());
|
||||
assertEquals(0, jedis.hdel(bfoo, bfoo).intValue());
|
||||
assertEquals(1, jedis.hdel(bfoo, bbar).intValue());
|
||||
assertEquals(null, jedis.hget(bfoo, bbar));
|
||||
|
||||
assertEquals(0, jedis.hdel("bar", "foo").intValue());
|
||||
assertEquals(0, jedis.hdel("foo", "foo").intValue());
|
||||
assertEquals(1, jedis.hdel("foo", "bar").intValue());
|
||||
assertEquals(null, jedis.hget("foo", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hlen() {
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
assertEquals(0, jedis.hlen("bar").intValue());
|
||||
assertEquals(2, jedis.hlen("foo").intValue());
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
assertEquals(0, jedis.hlen(bbar).intValue());
|
||||
assertEquals(2, jedis.hlen(bfoo).intValue());
|
||||
|
||||
assertEquals(0, jedis.hlen("bar").intValue());
|
||||
assertEquals(2, jedis.hlen("foo").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hkeys() {
|
||||
Map<String, String> hash = new LinkedHashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new LinkedHashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
List<String> keys = jedis.hkeys("foo");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("bar");
|
||||
expected.add("car");
|
||||
assertEquals(expected, keys);
|
||||
Set<String> keys = jedis.hkeys("foo");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("bar");
|
||||
expected.add("car");
|
||||
assertEquals(expected, keys);
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new LinkedHashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
Set<byte[]> bkeys = jedis.hkeys(bfoo);
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bbar);
|
||||
bexpected.add(bcar);
|
||||
assertEquals(bexpected, bkeys);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hvals() {
|
||||
Map<String, String> hash = new LinkedHashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
Map<String, String> hash = new LinkedHashMap<String, String>();
|
||||
hash.put("bar", "car");
|
||||
hash.put("car", "bar");
|
||||
jedis.hmset("foo", hash);
|
||||
|
||||
List<String> vals = jedis.hvals("foo");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("car");
|
||||
expected.add("bar");
|
||||
assertEquals(expected, vals);
|
||||
List<String> vals = jedis.hvals("foo");
|
||||
assertEquals(2, vals.size());
|
||||
assertTrue(vals.contains("bar"));
|
||||
assertTrue(vals.contains("car"));
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bhash = new LinkedHashMap<byte[], byte[]>();
|
||||
bhash.put(bbar, bcar);
|
||||
bhash.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bhash);
|
||||
|
||||
List<byte[]> bvals = jedis.hvals(bfoo);
|
||||
|
||||
assertEquals(2, bvals.size());
|
||||
assertTrue(arrayContains(bvals, bbar));
|
||||
assertTrue(arrayContains(bvals, bcar));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hgetAll() {
|
||||
Map<String, String> h = new HashMap<String, String>();
|
||||
h.put("bar", "car");
|
||||
h.put("car", "bar");
|
||||
jedis.hmset("foo", h);
|
||||
Map<String, String> h = new HashMap<String, String>();
|
||||
h.put("bar", "car");
|
||||
h.put("car", "bar");
|
||||
jedis.hmset("foo", h);
|
||||
|
||||
Map<String, String> hash = jedis.hgetAll("foo");
|
||||
Map<String, String> expected = new HashMap<String, String>();
|
||||
expected.put("bar", "car");
|
||||
expected.put("car", "bar");
|
||||
assertEquals(expected, hash);
|
||||
Map<String, String> hash = jedis.hgetAll("foo");
|
||||
assertEquals(2, hash.size());
|
||||
assertEquals("car", hash.get("bar"));
|
||||
assertEquals("bar", hash.get("car"));
|
||||
|
||||
// Binary
|
||||
Map<byte[], byte[]> bh = new HashMap<byte[], byte[]>();
|
||||
bh.put(bbar, bcar);
|
||||
bh.put(bcar, bbar);
|
||||
jedis.hmset(bfoo, bh);
|
||||
Map<byte[], byte[]> bhash = jedis.hgetAll(bfoo);
|
||||
|
||||
assertEquals(2, bhash.size());
|
||||
assertArrayEquals(bcar, bhash.get(bbar));
|
||||
assertArrayEquals(bbar, bhash.get(bcar));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,43 +2,85 @@ package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||
import redis.clients.jedis.tests.JedisTestBase;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
||||
|
||||
public abstract class JedisCommandTestBase extends Assert {
|
||||
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||
public abstract class JedisCommandTestBase extends JedisTestBase {
|
||||
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||
|
||||
protected Jedis jedis;
|
||||
protected Jedis jedis;
|
||||
|
||||
public JedisCommandTestBase() {
|
||||
super();
|
||||
}
|
||||
public JedisCommandTestBase() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis(hnp.host, hnp.port, 500);
|
||||
jedis.connect();
|
||||
jedis.auth("foobared");
|
||||
jedis.flushAll();
|
||||
}
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis(hnp.host, hnp.port, 500);
|
||||
jedis.connect();
|
||||
jedis.auth("foobared");
|
||||
jedis.flushAll();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.disconnect();
|
||||
}
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.disconnect();
|
||||
}
|
||||
|
||||
protected Jedis createJedis() throws UnknownHostException, IOException {
|
||||
Jedis j = new Jedis(hnp.host, hnp.port);
|
||||
j.connect();
|
||||
j.auth("foobared");
|
||||
j.flushAll();
|
||||
return j;
|
||||
}
|
||||
protected Jedis createJedis() throws UnknownHostException, IOException {
|
||||
Jedis j = new Jedis(hnp.host, hnp.port);
|
||||
j.connect();
|
||||
j.auth("foobared");
|
||||
j.flushAll();
|
||||
return j;
|
||||
}
|
||||
|
||||
protected void assertEquals(List<byte[]> expected, List<byte[]> actual) {
|
||||
assertEquals(expected.size(), actual.size());
|
||||
for (int n = 0; n < expected.size(); n++) {
|
||||
assertArrayEquals(expected.get(n), actual.get(n));
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertEquals(Set<byte[]> expected, Set<byte[]> actual) {
|
||||
assertEquals(expected.size(), actual.size());
|
||||
Iterator<byte[]> iterator = expected.iterator();
|
||||
Iterator<byte[]> iterator2 = actual.iterator();
|
||||
while (iterator.hasNext() || iterator2.hasNext()) {
|
||||
assertArrayEquals(iterator.next(), iterator2.next());
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean arrayContains(List<byte[]> array, byte[] expected) {
|
||||
for (byte[] a : array) {
|
||||
try {
|
||||
assertArrayEquals(a, expected);
|
||||
return true;
|
||||
} catch (AssertionError e) {
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean setContains(Set<byte[]> set, byte[] expected) {
|
||||
for (byte[] a : set) {
|
||||
try {
|
||||
assertArrayEquals(a, expected);
|
||||
return true;
|
||||
} catch (AssertionError e) {
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -10,287 +10,572 @@ import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
|
||||
public class ListCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C };
|
||||
final byte[] bA = { 0x0A };
|
||||
final byte[] bB = { 0x0B };
|
||||
final byte[] bC = { 0x0C };
|
||||
final byte[] b1 = { 0x01 };
|
||||
final byte[] b2 = { 0x02 };
|
||||
final byte[] b3 = { 0x03 };
|
||||
final byte[] bhello = { 0x04, 0x02 };
|
||||
final byte[] bx = { 0x02, 0x04 };
|
||||
final byte[] bdst = { 0x11, 0x12, 0x13, 0x14 };
|
||||
|
||||
@Test
|
||||
public void rpush() {
|
||||
int size = jedis.rpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.rpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
int size = jedis.rpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.rpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
|
||||
// Binary
|
||||
int bsize = jedis.rpush(bfoo, bbar);
|
||||
assertEquals(1, bsize);
|
||||
bsize = jedis.rpush(bfoo, bfoo);
|
||||
assertEquals(2, bsize);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lpush() {
|
||||
int size = jedis.lpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.lpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
int size = jedis.lpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.lpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
|
||||
// Binary
|
||||
int bsize = jedis.lpush(bfoo, bbar);
|
||||
assertEquals(1, bsize);
|
||||
bsize = jedis.lpush(bfoo, bfoo);
|
||||
assertEquals(2, bsize);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void llen() {
|
||||
assertEquals(0, jedis.llen("foo").intValue());
|
||||
jedis.lpush("foo", "bar");
|
||||
jedis.lpush("foo", "car");
|
||||
assertEquals(2, jedis.llen("foo").intValue());
|
||||
assertEquals(0, jedis.llen("foo").intValue());
|
||||
jedis.lpush("foo", "bar");
|
||||
jedis.lpush("foo", "car");
|
||||
assertEquals(2, jedis.llen("foo").intValue());
|
||||
|
||||
// Binary
|
||||
assertEquals(0, jedis.llen(bfoo).intValue());
|
||||
jedis.lpush(bfoo, bbar);
|
||||
jedis.lpush(bfoo, bcar);
|
||||
assertEquals(2, jedis.llen(bfoo).intValue());
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
@Test
|
||||
public void llenNotOnList() {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.llen("foo");
|
||||
try {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.llen("foo");
|
||||
fail("JedisException expected");
|
||||
} catch (final JedisException e) {
|
||||
}
|
||||
|
||||
// Binary
|
||||
try {
|
||||
jedis.set(bfoo, bbar);
|
||||
jedis.llen(bfoo);
|
||||
fail("JedisException expected");
|
||||
} catch (final JedisException e) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lrange() {
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
List<String> range = jedis.lrange("foo", 0, 2);
|
||||
assertEquals(expected, range);
|
||||
List<String> range = jedis.lrange("foo", 0, 2);
|
||||
assertEquals(expected, range);
|
||||
|
||||
range = jedis.lrange("foo", 0, 20);
|
||||
assertEquals(expected, range);
|
||||
range = jedis.lrange("foo", 0, 20);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
expected = new ArrayList<String>();
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
range = jedis.lrange("foo", 1, 2);
|
||||
assertEquals(expected, range);
|
||||
range = jedis.lrange("foo", 1, 2);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
range = jedis.lrange("foo", 2, 1);
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.rpush(bfoo, bA);
|
||||
jedis.rpush(bfoo, bB);
|
||||
jedis.rpush(bfoo, bC);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bA);
|
||||
bexpected.add(bB);
|
||||
bexpected.add(bC);
|
||||
|
||||
List<byte[]> brange = jedis.lrange(bfoo, 0, 2);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
brange = jedis.lrange(bfoo, 0, 20);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bB);
|
||||
bexpected.add(bC);
|
||||
|
||||
brange = jedis.lrange(bfoo, 1, 2);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected = new ArrayList<byte[]>();
|
||||
brange = jedis.lrange(bfoo, 2, 1);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
range = jedis.lrange("foo", 2, 1);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ltrim() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
String status = jedis.ltrim("foo", 0, 1);
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
String status = jedis.ltrim("foo", 0, 1);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
|
||||
assertEquals("OK", status);
|
||||
assertEquals(2, jedis.llen("foo").intValue());
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b3);
|
||||
String bstatus = jedis.ltrim(bfoo, 0, 1);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b3);
|
||||
bexpected.add(b2);
|
||||
|
||||
assertEquals("OK", bstatus);
|
||||
assertEquals(2, jedis.llen(bfoo).intValue());
|
||||
assertEquals(bexpected, jedis.lrange(bfoo, 0, 100));
|
||||
|
||||
assertEquals("OK", status);
|
||||
assertEquals(2, jedis.llen("foo").intValue());
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lindex() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("bar");
|
||||
expected.add("1");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("bar");
|
||||
expected.add("1");
|
||||
|
||||
String status = jedis.lset("foo", 1, "bar");
|
||||
String status = jedis.lset("foo", 1, "bar");
|
||||
|
||||
assertEquals("OK", status);
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 100));
|
||||
assertEquals("OK", status);
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b3);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b3);
|
||||
bexpected.add(bbar);
|
||||
bexpected.add(b1);
|
||||
|
||||
String bstatus = jedis.lset(bfoo, 1, bbar);
|
||||
|
||||
assertEquals("OK", bstatus);
|
||||
assertEquals(bexpected, jedis.lrange(bfoo, 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lset() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
|
||||
assertEquals("3", jedis.lindex("foo", 0));
|
||||
assertEquals(null, jedis.lindex("foo", 100));
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b3);
|
||||
|
||||
assertArrayEquals(b3, jedis.lindex(bfoo, 0));
|
||||
assertEquals(null, jedis.lindex(bfoo, 100));
|
||||
|
||||
assertEquals("3", jedis.lindex("foo", 0));
|
||||
assertEquals(null, jedis.lindex("foo", 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lrem() {
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "x");
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "c");
|
||||
jedis.lpush("foo", "b");
|
||||
jedis.lpush("foo", "a");
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "x");
|
||||
jedis.lpush("foo", "hello");
|
||||
jedis.lpush("foo", "c");
|
||||
jedis.lpush("foo", "b");
|
||||
jedis.lpush("foo", "a");
|
||||
|
||||
int count = jedis.lrem("foo", -2, "hello");
|
||||
int count = jedis.lrem("foo", -2, "hello");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
expected.add("hello");
|
||||
expected.add("x");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
expected.add("hello");
|
||||
expected.add("x");
|
||||
|
||||
assertEquals(2, count);
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
assertEquals(0, jedis.lrem("bar", 100, "foo").intValue());
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, bhello);
|
||||
jedis.lpush(bfoo, bhello);
|
||||
jedis.lpush(bfoo, bx);
|
||||
jedis.lpush(bfoo, bhello);
|
||||
jedis.lpush(bfoo, bC);
|
||||
jedis.lpush(bfoo, bB);
|
||||
jedis.lpush(bfoo, bA);
|
||||
|
||||
int bcount = jedis.lrem(bfoo, -2, bhello);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bA);
|
||||
bexpected.add(bB);
|
||||
bexpected.add(bC);
|
||||
bexpected.add(bhello);
|
||||
bexpected.add(bx);
|
||||
|
||||
assertEquals(2, bcount);
|
||||
assertEquals(bexpected, jedis.lrange(bfoo, 0, 1000));
|
||||
assertEquals(0, jedis.lrem(bbar, 100, bfoo).intValue());
|
||||
|
||||
assertEquals(2, count);
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
assertEquals(0, jedis.lrem("bar", 100, "foo").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lpop() {
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
|
||||
String element = jedis.lpop("foo");
|
||||
assertEquals("a", element);
|
||||
String element = jedis.lpop("foo");
|
||||
assertEquals("a", element);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
jedis.lpop("foo");
|
||||
jedis.lpop("foo");
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
jedis.lpop("foo");
|
||||
jedis.lpop("foo");
|
||||
|
||||
element = jedis.lpop("foo");
|
||||
assertEquals(null, element);
|
||||
|
||||
// Binary
|
||||
jedis.rpush(bfoo, bA);
|
||||
jedis.rpush(bfoo, bB);
|
||||
jedis.rpush(bfoo, bC);
|
||||
|
||||
byte[] belement = jedis.lpop(bfoo);
|
||||
assertArrayEquals(bA, belement);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bB);
|
||||
bexpected.add(bC);
|
||||
|
||||
assertEquals(bexpected, jedis.lrange(bfoo, 0, 1000));
|
||||
jedis.lpop(bfoo);
|
||||
jedis.lpop(bfoo);
|
||||
|
||||
belement = jedis.lpop(bfoo);
|
||||
assertEquals(null, belement);
|
||||
|
||||
element = jedis.lpop("foo");
|
||||
assertEquals(null, element);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rpop() {
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
|
||||
String element = jedis.rpop("foo");
|
||||
assertEquals("c", element);
|
||||
String element = jedis.rpop("foo");
|
||||
assertEquals("c", element);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
jedis.rpop("foo");
|
||||
jedis.rpop("foo");
|
||||
assertEquals(expected, jedis.lrange("foo", 0, 1000));
|
||||
jedis.rpop("foo");
|
||||
jedis.rpop("foo");
|
||||
|
||||
element = jedis.rpop("foo");
|
||||
assertEquals(null, element);
|
||||
|
||||
// Binary
|
||||
jedis.rpush(bfoo, bA);
|
||||
jedis.rpush(bfoo, bB);
|
||||
jedis.rpush(bfoo, bC);
|
||||
|
||||
byte[] belement = jedis.rpop(bfoo);
|
||||
assertArrayEquals(bC, belement);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bA);
|
||||
bexpected.add(bB);
|
||||
|
||||
assertEquals(bexpected, jedis.lrange(bfoo, 0, 1000));
|
||||
jedis.rpop(bfoo);
|
||||
jedis.rpop(bfoo);
|
||||
|
||||
belement = jedis.rpop(bfoo);
|
||||
assertEquals(null, belement);
|
||||
|
||||
element = jedis.rpop("foo");
|
||||
assertEquals(null, element);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rpoplpush() {
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
|
||||
jedis.rpush("dst", "foo");
|
||||
jedis.rpush("dst", "bar");
|
||||
jedis.rpush("dst", "foo");
|
||||
jedis.rpush("dst", "bar");
|
||||
|
||||
String element = jedis.rpoplpush("foo", "dst");
|
||||
String element = jedis.rpoplpush("foo", "dst");
|
||||
|
||||
assertEquals("c", element);
|
||||
assertEquals("c", element);
|
||||
|
||||
List<String> srcExpected = new ArrayList<String>();
|
||||
srcExpected.add("a");
|
||||
srcExpected.add("b");
|
||||
List<String> srcExpected = new ArrayList<String>();
|
||||
srcExpected.add("a");
|
||||
srcExpected.add("b");
|
||||
|
||||
List<String> dstExpected = new ArrayList<String>();
|
||||
dstExpected.add("c");
|
||||
dstExpected.add("foo");
|
||||
dstExpected.add("bar");
|
||||
List<String> dstExpected = new ArrayList<String>();
|
||||
dstExpected.add("c");
|
||||
dstExpected.add("foo");
|
||||
dstExpected.add("bar");
|
||||
|
||||
assertEquals(srcExpected, jedis.lrange("foo", 0, 1000));
|
||||
assertEquals(dstExpected, jedis.lrange("dst", 0, 1000));
|
||||
|
||||
// Binary
|
||||
jedis.rpush(bfoo, bA);
|
||||
jedis.rpush(bfoo, bB);
|
||||
jedis.rpush(bfoo, bC);
|
||||
|
||||
jedis.rpush(bdst, bfoo);
|
||||
jedis.rpush(bdst, bbar);
|
||||
|
||||
byte[] belement = jedis.rpoplpush(bfoo, bdst);
|
||||
|
||||
assertArrayEquals(bC, belement);
|
||||
|
||||
List<byte[]> bsrcExpected = new ArrayList<byte[]>();
|
||||
bsrcExpected.add(bA);
|
||||
bsrcExpected.add(bB);
|
||||
|
||||
List<byte[]> bdstExpected = new ArrayList<byte[]>();
|
||||
bdstExpected.add(bC);
|
||||
bdstExpected.add(bfoo);
|
||||
bdstExpected.add(bbar);
|
||||
|
||||
assertEquals(bsrcExpected, jedis.lrange(bfoo, 0, 1000));
|
||||
assertEquals(bdstExpected, jedis.lrange(bdst, 0, 1000));
|
||||
|
||||
assertEquals(srcExpected, jedis.lrange("foo", 0, 1000));
|
||||
assertEquals(dstExpected, jedis.lrange("dst", 0, 1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blpop() throws InterruptedException {
|
||||
List<String> result = jedis.blpop(1, "foo");
|
||||
assertNull(result);
|
||||
List<String> result = jedis.blpop(1, "foo");
|
||||
assertNull(result);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush("foo", "bar");
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush("foo", "bar");
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
result = jedis.blpop(1, "foo");
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.get(0));
|
||||
assertEquals("bar", result.get(1));
|
||||
|
||||
// Binary
|
||||
List<byte[]> bresult = jedis.blpop(1, bfoo);
|
||||
assertNull(bresult);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush(bfoo, bbar);
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
bresult = jedis.blpop(1, bfoo);
|
||||
assertNotNull(bresult);
|
||||
assertEquals(2, bresult.size());
|
||||
assertArrayEquals(bfoo, bresult.get(0));
|
||||
assertArrayEquals(bbar, bresult.get(1));
|
||||
|
||||
result = jedis.blpop(1, "foo");
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.get(0));
|
||||
assertEquals("bar", result.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void brpop() throws InterruptedException {
|
||||
List<String> result = jedis.brpop(1, "foo");
|
||||
assertNull(result);
|
||||
List<String> result = jedis.brpop(1, "foo");
|
||||
assertNull(result);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush("foo", "bar");
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush("foo", "bar");
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
result = jedis.brpop(1, "foo");
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.get(0));
|
||||
assertEquals("bar", result.get(1));
|
||||
|
||||
// Binary
|
||||
List<byte[]> bresult = jedis.brpop(1, bfoo);
|
||||
assertNull(bresult);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
j.lpush(bfoo, bbar);
|
||||
j.disconnect();
|
||||
} catch (Exception ex) {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
bresult = jedis.brpop(1, bfoo);
|
||||
assertNotNull(bresult);
|
||||
assertEquals(2, bresult.size());
|
||||
assertArrayEquals(bfoo, bresult.get(0));
|
||||
assertArrayEquals(bbar, bresult.get(1));
|
||||
|
||||
result = jedis.brpop(1, "foo");
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.get(0));
|
||||
assertEquals("bar", result.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lpushx() {
|
||||
int status = jedis.lpushx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
int status = jedis.lpushx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.lpushx("foo", "b");
|
||||
assertEquals(2, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.lpushx(bfoo, bbar);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.lpush(bfoo, bA);
|
||||
bstatus = jedis.lpushx(bfoo, bB);
|
||||
assertEquals(2, bstatus);
|
||||
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.lpushx("foo", "b");
|
||||
assertEquals(2, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rpushx() {
|
||||
int status = jedis.rpushx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
int status = jedis.rpushx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.rpushx("foo", "b");
|
||||
assertEquals(2, status);
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.rpushx("foo", "b");
|
||||
assertEquals(2, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.rpushx(bfoo, bbar);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.lpush(bfoo, bA);
|
||||
bstatus = jedis.rpushx(bfoo, bB);
|
||||
assertEquals(2, bstatus);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void linsert() {
|
||||
int status = jedis.linsert("foo", Client.LIST_POSITION.BEFORE, "bar",
|
||||
"car");
|
||||
assertEquals(0, status);
|
||||
int status = jedis.linsert("foo", Client.LIST_POSITION.BEFORE, "bar",
|
||||
"car");
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.linsert("foo", Client.LIST_POSITION.AFTER, "a", "b");
|
||||
assertEquals(2, status);
|
||||
jedis.lpush("foo", "a");
|
||||
status = jedis.linsert("foo", Client.LIST_POSITION.AFTER, "a", "b");
|
||||
assertEquals(2, status);
|
||||
|
||||
List<String> actual = jedis.lrange("foo", 0, 100);
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
List<String> actual = jedis.lrange("foo", 0, 100);
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(expected, actual);
|
||||
assertEquals(expected, actual);
|
||||
|
||||
status = jedis
|
||||
.linsert("foo", Client.LIST_POSITION.BEFORE, "bar", "car");
|
||||
assertEquals(-1, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.linsert(bfoo, Client.LIST_POSITION.BEFORE, bbar,
|
||||
bcar);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
jedis.lpush(bfoo, bA);
|
||||
bstatus = jedis.linsert(bfoo, Client.LIST_POSITION.AFTER, bA, bB);
|
||||
assertEquals(2, bstatus);
|
||||
|
||||
List<byte[]> bactual = jedis.lrange(bfoo, 0, 100);
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bA);
|
||||
bexpected.add(bB);
|
||||
|
||||
assertEquals(bexpected, bactual);
|
||||
|
||||
bstatus = jedis.linsert(bfoo, Client.LIST_POSITION.BEFORE, bbar, bcar);
|
||||
assertEquals(-1, bstatus);
|
||||
|
||||
status = jedis
|
||||
.linsert("foo", Client.LIST_POSITION.BEFORE, "bar", "car");
|
||||
assertEquals(-1, status);
|
||||
}
|
||||
}
|
||||
@@ -1,236 +1,458 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C };
|
||||
final byte[] ba = { 0x0A };
|
||||
final byte[] bb = { 0x0B };
|
||||
final byte[] bc = { 0x0C };
|
||||
final byte[] bd = { 0x0D };
|
||||
final byte[] bx = { 0x42 };
|
||||
|
||||
@Test
|
||||
public void sadd() {
|
||||
int status = jedis.sadd("foo", "a");
|
||||
assertEquals(1, status);
|
||||
int status = jedis.sadd("foo", "a");
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.sadd("foo", "a");
|
||||
assertEquals(0, status);
|
||||
|
||||
int bstatus = jedis.sadd(bfoo, ba);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
bstatus = jedis.sadd(bfoo, ba);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
status = jedis.sadd("foo", "a");
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void smembers() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
|
||||
Set<String> members = jedis.smembers("foo");
|
||||
Set<String> members = jedis.smembers("foo");
|
||||
|
||||
assertEquals(expected, members);
|
||||
assertEquals(expected, members);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(ba);
|
||||
|
||||
Set<byte[]> bmembers = jedis.smembers(bfoo);
|
||||
|
||||
assertEquals(bexpected, bmembers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void srem() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
int status = jedis.srem("foo", "a");
|
||||
int status = jedis.srem("foo", "a");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(1, status);
|
||||
assertEquals(expected, jedis.smembers("foo"));
|
||||
assertEquals(1, status);
|
||||
assertEquals(expected, jedis.smembers("foo"));
|
||||
|
||||
status = jedis.srem("foo", "bar");
|
||||
status = jedis.srem("foo", "bar");
|
||||
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
int bstatus = jedis.srem(bfoo, ba);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
|
||||
assertEquals(1, bstatus);
|
||||
assertEquals(bexpected, jedis.smembers(bfoo));
|
||||
|
||||
bstatus = jedis.srem(bfoo, bbar);
|
||||
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spop() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
String member = jedis.spop("foo");
|
||||
String member = jedis.spop("foo");
|
||||
|
||||
assertTrue("a".equals(member) || "b".equals(member));
|
||||
assertEquals(1, jedis.smembers("foo").size());
|
||||
assertTrue("a".equals(member) || "b".equals(member));
|
||||
assertEquals(1, jedis.smembers("foo").size());
|
||||
|
||||
member = jedis.spop("bar");
|
||||
assertNull(member);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
byte[] bmember = jedis.spop(bfoo);
|
||||
|
||||
assertTrue(Arrays.equals(ba, bmember) || Arrays.equals(bb, bmember));
|
||||
assertEquals(1, jedis.smembers(bfoo).size());
|
||||
|
||||
bmember = jedis.spop(bbar);
|
||||
assertNull(bmember);
|
||||
|
||||
member = jedis.spop("bar");
|
||||
assertNull(member);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void smove() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
int status = jedis.smove("foo", "bar", "a");
|
||||
int status = jedis.smove("foo", "bar", "a");
|
||||
|
||||
Set<String> expectedSrc = new LinkedHashSet<String>();
|
||||
expectedSrc.add("b");
|
||||
Set<String> expectedSrc = new LinkedHashSet<String>();
|
||||
expectedSrc.add("b");
|
||||
|
||||
Set<String> expectedDst = new LinkedHashSet<String>();
|
||||
expectedDst.add("c");
|
||||
expectedDst.add("a");
|
||||
Set<String> expectedDst = new LinkedHashSet<String>();
|
||||
expectedDst.add("c");
|
||||
expectedDst.add("a");
|
||||
|
||||
assertEquals(status, 1);
|
||||
assertEquals(expectedSrc, jedis.smembers("foo"));
|
||||
assertEquals(expectedDst, jedis.smembers("bar"));
|
||||
assertEquals(status, 1);
|
||||
assertEquals(expectedSrc, jedis.smembers("foo"));
|
||||
assertEquals(expectedDst, jedis.smembers("bar"));
|
||||
|
||||
status = jedis.smove("foo", "bar", "a");
|
||||
|
||||
assertEquals(status, 0);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
int bstatus = jedis.smove(bfoo, bbar, ba);
|
||||
|
||||
Set<byte[]> bexpectedSrc = new LinkedHashSet<byte[]>();
|
||||
bexpectedSrc.add(bb);
|
||||
|
||||
Set<byte[]> bexpectedDst = new LinkedHashSet<byte[]>();
|
||||
bexpectedDst.add(bc);
|
||||
bexpectedDst.add(ba);
|
||||
|
||||
assertEquals(bstatus, 1);
|
||||
assertEquals(bexpectedSrc, jedis.smembers(bfoo));
|
||||
assertEquals(bexpectedDst, jedis.smembers(bbar));
|
||||
|
||||
bstatus = jedis.smove(bfoo, bbar, ba);
|
||||
assertEquals(bstatus, 0);
|
||||
|
||||
status = jedis.smove("foo", "bar", "a");
|
||||
assertEquals(status, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scard() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
int card = jedis.scard("foo");
|
||||
int card = jedis.scard("foo");
|
||||
|
||||
assertEquals(2, card);
|
||||
assertEquals(2, card);
|
||||
|
||||
card = jedis.scard("bar");
|
||||
assertEquals(0, card);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
int bcard = jedis.scard(bfoo);
|
||||
|
||||
assertEquals(2, bcard);
|
||||
|
||||
bcard = jedis.scard(bbar);
|
||||
assertEquals(0, bcard);
|
||||
|
||||
card = jedis.scard("bar");
|
||||
assertEquals(0, card);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sismember() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
int status = jedis.sismember("foo", "a");
|
||||
assertEquals(1, status);
|
||||
int status = jedis.sismember("foo", "a");
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.sismember("foo", "c");
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
int bstatus = jedis.sismember(bfoo, ba);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
bstatus = jedis.sismember(bfoo, bc);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
status = jedis.sismember("foo", "c");
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sinter() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
|
||||
Set<String> intersection = jedis.sinter("foo", "bar");
|
||||
assertEquals(expected, intersection);
|
||||
Set<String> intersection = jedis.sinter("foo", "bar");
|
||||
assertEquals(expected, intersection);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
jedis.sadd(bbar, bb);
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
|
||||
Set<byte[]> bintersection = jedis.sinter(bfoo, bbar);
|
||||
assertEquals(bexpected, bintersection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sinterstore() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
|
||||
int status = jedis.sinterstore("car", "foo", "bar");
|
||||
assertEquals(1, status);
|
||||
int status = jedis.sinterstore("car", "foo", "bar");
|
||||
assertEquals(1, status);
|
||||
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
jedis.sadd(bbar, bb);
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
|
||||
int bstatus = jedis.sinterstore(bcar, bfoo, bbar);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
assertEquals(bexpected, jedis.smembers(bcar));
|
||||
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sunion() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
Set<String> union = jedis.sunion("foo", "bar");
|
||||
assertEquals(expected, union);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
jedis.sadd(bbar, bb);
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(bc);
|
||||
bexpected.add(ba);
|
||||
|
||||
Set<byte[]> bunion = jedis.sunion(bfoo, bbar);
|
||||
assertEquals(bexpected, bunion);
|
||||
|
||||
Set<String> union = jedis.sunion("foo", "bar");
|
||||
assertEquals(expected, union);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sunionstore() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "b");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
int status = jedis.sunionstore("car", "foo", "bar");
|
||||
assertEquals(3, status);
|
||||
int status = jedis.sunionstore("car", "foo", "bar");
|
||||
assertEquals(3, status);
|
||||
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
jedis.sadd(bbar, bb);
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(bc);
|
||||
bexpected.add(ba);
|
||||
|
||||
int bstatus = jedis.sunionstore(bcar, bfoo, bbar);
|
||||
assertEquals(3, bstatus);
|
||||
|
||||
assertEquals(bexpected, jedis.smembers(bcar));
|
||||
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sdiff() {
|
||||
jedis.sadd("foo", "x");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "c");
|
||||
jedis.sadd("foo", "x");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "c");
|
||||
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
jedis.sadd("car", "a");
|
||||
jedis.sadd("car", "d");
|
||||
jedis.sadd("car", "a");
|
||||
jedis.sadd("car", "d");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("x");
|
||||
expected.add("b");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("x");
|
||||
expected.add("b");
|
||||
|
||||
Set<String> diff = jedis.sdiff("foo", "bar", "car");
|
||||
assertEquals(expected, diff);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, bx);
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
jedis.sadd(bfoo, bc);
|
||||
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
jedis.sadd(bcar, ba);
|
||||
jedis.sadd(bcar, bd);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(bx);
|
||||
|
||||
Set<byte[]> bdiff = jedis.sdiff(bfoo, bbar, bcar);
|
||||
assertEquals(bexpected, bdiff);
|
||||
|
||||
Set<String> diff = jedis.sdiff("foo", "bar", "car");
|
||||
assertEquals(expected, diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sdiffstore() {
|
||||
jedis.sadd("foo", "x");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "c");
|
||||
jedis.sadd("foo", "x");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "c");
|
||||
|
||||
jedis.sadd("bar", "c");
|
||||
jedis.sadd("bar", "c");
|
||||
|
||||
jedis.sadd("car", "a");
|
||||
jedis.sadd("car", "d");
|
||||
jedis.sadd("car", "a");
|
||||
jedis.sadd("car", "d");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("d");
|
||||
expected.add("a");
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("d");
|
||||
expected.add("a");
|
||||
|
||||
int status = jedis.sdiffstore("tar", "foo", "bar", "car");
|
||||
assertEquals(2, status);
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, bx);
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
jedis.sadd(bfoo, bc);
|
||||
|
||||
jedis.sadd(bbar, bc);
|
||||
|
||||
jedis.sadd(bcar, ba);
|
||||
jedis.sadd(bcar, bd);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bd);
|
||||
bexpected.add(ba);
|
||||
|
||||
int bstatus = jedis.sdiffstore("tar".getBytes(), bfoo, bbar, bcar);
|
||||
assertEquals(2, bstatus);
|
||||
assertEquals(bexpected, jedis.smembers(bcar));
|
||||
|
||||
int status = jedis.sdiffstore("tar", "foo", "bar", "car");
|
||||
assertEquals(2, status);
|
||||
assertEquals(expected, jedis.smembers("car"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void srandmember() {
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
jedis.sadd("foo", "a");
|
||||
jedis.sadd("foo", "b");
|
||||
|
||||
String member = jedis.srandmember("foo");
|
||||
String member = jedis.srandmember("foo");
|
||||
|
||||
assertTrue("a".equals(member) || "b".equals(member));
|
||||
assertEquals(2, jedis.smembers("foo").size());
|
||||
assertTrue("a".equals(member) || "b".equals(member));
|
||||
assertEquals(2, jedis.smembers("foo").size());
|
||||
|
||||
member = jedis.srandmember("bar");
|
||||
assertNull(member);
|
||||
|
||||
// Binary
|
||||
jedis.sadd(bfoo, ba);
|
||||
jedis.sadd(bfoo, bb);
|
||||
|
||||
byte[] bmember = jedis.srandmember(bfoo);
|
||||
|
||||
assertTrue(Arrays.equals(ba, bmember) || Arrays.equals(bb, bmember));
|
||||
assertEquals(2, jedis.smembers(bfoo).size());
|
||||
|
||||
bmember = jedis.srandmember(bbar);
|
||||
assertNull(bmember);
|
||||
|
||||
member = jedis.srandmember("bar");
|
||||
assertNull(member);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,10 +5,18 @@ import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.Tuple;
|
||||
import redis.clients.jedis.ZParams;
|
||||
|
||||
public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C };
|
||||
final byte[] ba = { 0x0A };
|
||||
final byte[] bb = { 0x0B };
|
||||
final byte[] bc = { 0x0C };
|
||||
|
||||
@Test
|
||||
public void zadd() {
|
||||
int status = jedis.zadd("foo", 1d, "a");
|
||||
@@ -22,6 +30,20 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
status = jedis.zadd("foo", 2d, "a");
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
int bstatus = jedis.zadd(bfoo, 1d, ba);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
bstatus = jedis.zadd(bfoo, 10d, bb);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
bstatus = jedis.zadd(bfoo, 0.1d, bc);
|
||||
assertEquals(1, bstatus);
|
||||
|
||||
bstatus = jedis.zadd(bfoo, 2d, ba);
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -41,6 +63,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("b");
|
||||
range = jedis.zrange("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bc);
|
||||
bexpected.add(ba);
|
||||
|
||||
Set<byte[]> brange = jedis.zrange(bfoo, 0, 1);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected.add(bb);
|
||||
brange = jedis.zrange(bfoo, 0, 100);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,6 +100,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("c");
|
||||
range = jedis.zrevrange("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(ba);
|
||||
|
||||
Set<byte[]> brange = jedis.zrevrange(bfoo, 0, 1);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected.add(bc);
|
||||
brange = jedis.zrevrange(bfoo, 0, 100);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,6 +136,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
status = jedis.zrem("foo", "bar");
|
||||
|
||||
assertEquals(0, status);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 2d, bb);
|
||||
|
||||
int bstatus = jedis.zrem(bfoo, ba);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
|
||||
assertEquals(1, bstatus);
|
||||
assertEquals(bexpected, jedis.zrange(bfoo, 0, 100));
|
||||
|
||||
bstatus = jedis.zrem(bfoo, bbar);
|
||||
|
||||
assertEquals(0, bstatus);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,8 +166,22 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(3d, score);
|
||||
assertEquals(3d, score, 0);
|
||||
assertEquals(expected, jedis.zrange("foo", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 2d, bb);
|
||||
|
||||
double bscore = jedis.zincrby(bfoo, 2d, ba);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
bexpected.add(ba);
|
||||
|
||||
assertEquals(3d, bscore, 0);
|
||||
assertEquals(bexpected, jedis.zrange(bfoo, 0, 100));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,6 +197,20 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
rank = jedis.zrank("car", "b");
|
||||
assertNull(rank);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 2d, bb);
|
||||
|
||||
Integer brank = jedis.zrank(bfoo, ba);
|
||||
assertEquals(0, brank.intValue());
|
||||
|
||||
brank = jedis.zrank(bfoo, bb);
|
||||
assertEquals(1, brank.intValue());
|
||||
|
||||
brank = jedis.zrank(bcar, bb);
|
||||
assertNull(brank);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,6 +223,17 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
rank = jedis.zrevrank("foo", "b");
|
||||
assertEquals(0, rank);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 2d, bb);
|
||||
|
||||
int brank = jedis.zrevrank(bfoo, ba);
|
||||
assertEquals(1, brank);
|
||||
|
||||
brank = jedis.zrevrank(bfoo, bb);
|
||||
assertEquals(0, brank);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,6 +253,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("b", 10d));
|
||||
range = jedis.zrangeWithScores("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bc, 0.1d));
|
||||
bexpected.add(new Tuple(ba, 2d));
|
||||
|
||||
Set<Tuple> brange = jedis.zrangeWithScores(bfoo, 0, 1);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected.add(new Tuple(bb, 10d));
|
||||
brange = jedis.zrangeWithScores(bfoo, 0, 100);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -158,6 +290,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("c", 0.1d));
|
||||
range = jedis.zrevrangeWithScores("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bb, 10d));
|
||||
bexpected.add(new Tuple(ba, 2d));
|
||||
|
||||
Set<Tuple> brange = jedis.zrevrangeWithScores(bfoo, 0, 1);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
bexpected.add(new Tuple(bc, 0.1d));
|
||||
brange = jedis.zrevrangeWithScores(bfoo, 0, 100);
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -169,6 +319,16 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
int size = jedis.zcard("foo");
|
||||
assertEquals(3, size);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
int bsize = jedis.zcard(bfoo);
|
||||
assertEquals(3, bsize);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -179,13 +339,29 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
Double score = jedis.zscore("foo", "b");
|
||||
assertEquals(10d, score);
|
||||
assertEquals((Double) 10d, score);
|
||||
|
||||
score = jedis.zscore("foo", "c");
|
||||
assertEquals(0.1d, score);
|
||||
assertEquals((Double) 0.1d, score);
|
||||
|
||||
score = jedis.zscore("foo", "s");
|
||||
assertNull(score);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Double bscore = jedis.zscore(bfoo, bb);
|
||||
assertEquals((Double) 10d, bscore);
|
||||
|
||||
bscore = jedis.zscore(bfoo, bc);
|
||||
assertEquals((Double) 0.1d, bscore);
|
||||
|
||||
bscore = jedis.zscore(bfoo, "s".getBytes(Protocol.UTF8));
|
||||
assertNull(bscore);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -198,6 +374,17 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
int result = jedis.zcount("foo", 0.01d, 2.1d);
|
||||
|
||||
assertEquals(2, result);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
int bresult = jedis.zcount(bfoo, 0.01d, 2.1d);
|
||||
|
||||
assertEquals(2, bresult);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -230,6 +417,38 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("a");
|
||||
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<byte[]> brange = jedis.zrangeByScore(bfoo, 0d, 2d);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bc);
|
||||
bexpected.add(ba);
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
brange = jedis.zrangeByScore(bfoo, 0d, 2d, 0, 1);
|
||||
|
||||
bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bc);
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1);
|
||||
Set<byte[]> brange2 = jedis.zrangeByScore(bfoo, "-inf"
|
||||
.getBytes(Protocol.UTF8), "(2".getBytes(Protocol.UTF8));
|
||||
assertEquals(bexpected, brange2);
|
||||
|
||||
bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(ba);
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -260,6 +479,36 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("a", 2d));
|
||||
|
||||
assertEquals(expected, range);
|
||||
|
||||
// Binary
|
||||
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
Set<Tuple> brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bc, 0.1d));
|
||||
bexpected.add(new Tuple(ba, 2d));
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 0, 1);
|
||||
|
||||
bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bc, 0.1d));
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 1, 1);
|
||||
|
||||
bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(ba, 2d));
|
||||
|
||||
assertEquals(bexpected, brange);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -278,6 +527,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(expected, jedis.zrange("foo", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
int bresult = jedis.zremrangeByRank(bfoo, 0, 0);
|
||||
|
||||
assertEquals(1, bresult);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(ba);
|
||||
bexpected.add(bb);
|
||||
|
||||
assertEquals(bexpected, jedis.zrange(bfoo, 0, 100));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -295,6 +561,21 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(expected, jedis.zrange("foo", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1d, ba);
|
||||
jedis.zadd(bfoo, 10d, bb);
|
||||
jedis.zadd(bfoo, 0.1d, bc);
|
||||
jedis.zadd(bfoo, 2d, ba);
|
||||
|
||||
int bresult = jedis.zremrangeByScore(bfoo, 0, 2);
|
||||
|
||||
assertEquals(2, bresult);
|
||||
|
||||
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
|
||||
bexpected.add(bb);
|
||||
|
||||
assertEquals(bexpected, jedis.zrange(bfoo, 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -313,6 +594,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("a", new Double(3)));
|
||||
|
||||
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1, ba);
|
||||
jedis.zadd(bfoo, 2, bb);
|
||||
jedis.zadd(bbar, 2, ba);
|
||||
jedis.zadd(bbar, 2, bb);
|
||||
|
||||
int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bfoo,
|
||||
bbar);
|
||||
|
||||
assertEquals(2, bresult);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bb, new Double(4)));
|
||||
bexpected.add(new Tuple(ba, new Double(3)));
|
||||
|
||||
assertEquals(bexpected, jedis.zrangeWithScores("dst"
|
||||
.getBytes(Protocol.UTF8), 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -334,6 +633,27 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("a", new Double(6)));
|
||||
|
||||
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1, ba);
|
||||
jedis.zadd(bfoo, 2, bb);
|
||||
jedis.zadd(bbar, 2, ba);
|
||||
jedis.zadd(bbar, 2, bb);
|
||||
|
||||
ZParams bparams = new ZParams();
|
||||
bparams.weights(2, 2);
|
||||
bparams.aggregate(ZParams.Aggregate.SUM);
|
||||
int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bparams,
|
||||
bfoo, bbar);
|
||||
|
||||
assertEquals(2, bresult);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(bb, new Double(8)));
|
||||
bexpected.add(new Tuple(ba, new Double(6)));
|
||||
|
||||
assertEquals(bexpected, jedis.zrangeWithScores("dst"
|
||||
.getBytes(Protocol.UTF8), 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -350,6 +670,22 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("a", new Double(3)));
|
||||
|
||||
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1, ba);
|
||||
jedis.zadd(bfoo, 2, bb);
|
||||
jedis.zadd(bbar, 2, ba);
|
||||
|
||||
int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bfoo,
|
||||
bbar);
|
||||
|
||||
assertEquals(1, bresult);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(ba, new Double(3)));
|
||||
|
||||
assertEquals(bexpected, jedis.zrangeWithScores("dst"
|
||||
.getBytes(Protocol.UTF8), 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -369,5 +705,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
expected.add(new Tuple("a", new Double(6)));
|
||||
|
||||
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
|
||||
|
||||
// Binary
|
||||
jedis.zadd(bfoo, 1, ba);
|
||||
jedis.zadd(bfoo, 2, bb);
|
||||
jedis.zadd(bbar, 2, ba);
|
||||
|
||||
ZParams bparams = new ZParams();
|
||||
bparams.weights(2, 2);
|
||||
bparams.aggregate(ZParams.Aggregate.SUM);
|
||||
int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bparams,
|
||||
bfoo, bbar);
|
||||
|
||||
assertEquals(1, bresult);
|
||||
|
||||
Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
|
||||
bexpected.add(new Tuple(ba, new Double(6)));
|
||||
|
||||
assertEquals(bexpected, jedis.zrangeWithScores("dst"
|
||||
.getBytes(Protocol.UTF8), 0, 100));
|
||||
}
|
||||
}
|
||||
@@ -8,147 +8,294 @@ import org.junit.Test;
|
||||
import redis.clients.jedis.SortingParams;
|
||||
|
||||
public class SortingCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, '1' };
|
||||
final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, '2' };
|
||||
final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, '3' };
|
||||
final byte[] bbar10 = { 0x05, 0x06, 0x07, 0x08, '1', '0' };
|
||||
final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' };
|
||||
final byte[] bcar1 = { 0x0A, 0x0B, 0x0C, 0x0D, '1' };
|
||||
final byte[] bcar2 = { 0x0A, 0x0B, 0x0C, 0x0D, '2' };
|
||||
final byte[] bcar10 = { 0x0A, 0x0B, 0x0C, 0x0D, '1', '0' };
|
||||
final byte[] bcarstar = { 0x0A, 0x0B, 0x0C, 0x0D, '*' };
|
||||
final byte[] b1 = { '1' };
|
||||
final byte[] b2 = { '2' };
|
||||
final byte[] b3 = { '3' };
|
||||
final byte[] b10 = { '1', '0' };
|
||||
|
||||
@Test
|
||||
public void sort() {
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "1");
|
||||
|
||||
List<String> result = jedis.sort("foo");
|
||||
List<String> result = jedis.sort("foo");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("3");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("3");
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b3);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b1);
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b1);
|
||||
bexpected.add(b2);
|
||||
bexpected.add(b3);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortBy() {
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "1");
|
||||
|
||||
jedis.set("bar1", "3");
|
||||
jedis.set("bar2", "2");
|
||||
jedis.set("bar3", "1");
|
||||
jedis.set("bar1", "3");
|
||||
jedis.set("bar2", "2");
|
||||
jedis.set("bar3", "1");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.by("bar*");
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.by("bar*");
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
expected.add("1");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
expected.add("1");
|
||||
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b3);
|
||||
jedis.lpush(bfoo, b1);
|
||||
|
||||
jedis.set(bbar1, b3);
|
||||
jedis.set(bbar2, b2);
|
||||
jedis.set(bbar3, b1);
|
||||
|
||||
SortingParams bsp = new SortingParams();
|
||||
bsp.by(bbarstar);
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo, bsp);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b3);
|
||||
bexpected.add(b2);
|
||||
bexpected.add(b1);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortDesc() {
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "1");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.desc();
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.desc();
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
expected.add("1");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
expected.add("1");
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b3);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b1);
|
||||
|
||||
SortingParams bsp = new SortingParams();
|
||||
bsp.desc();
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo, bsp);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b3);
|
||||
bexpected.add(b2);
|
||||
bexpected.add(b1);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortLimit() {
|
||||
for (int n = 10; n > 0; n--) {
|
||||
jedis.lpush("foo", String.valueOf(n));
|
||||
}
|
||||
for (int n = 10; n > 0; n--) {
|
||||
jedis.lpush("foo", String.valueOf(n));
|
||||
}
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.limit(0, 3);
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.limit(0, 3);
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("3");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("3");
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.rpush(bfoo, new byte[] { (byte) '4' });
|
||||
jedis.rpush(bfoo, new byte[] { (byte) '3' });
|
||||
jedis.rpush(bfoo, new byte[] { (byte) '2' });
|
||||
jedis.rpush(bfoo, new byte[] { (byte) '1' });
|
||||
|
||||
SortingParams bsp = new SortingParams();
|
||||
bsp.limit(0, 3);
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo, bsp);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b1);
|
||||
bexpected.add(b2);
|
||||
bexpected.add(b3);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortAlpha() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.alpha();
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.alpha();
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("10");
|
||||
expected.add("2");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("10");
|
||||
expected.add("2");
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b10);
|
||||
|
||||
SortingParams bsp = new SortingParams();
|
||||
bsp.alpha();
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo, bsp);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b1);
|
||||
bexpected.add(b10);
|
||||
bexpected.add(b2);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortGet() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
|
||||
jedis.set("bar1", "bar1");
|
||||
jedis.set("bar2", "bar2");
|
||||
jedis.set("bar10", "bar10");
|
||||
jedis.set("bar1", "bar1");
|
||||
jedis.set("bar2", "bar2");
|
||||
jedis.set("bar10", "bar10");
|
||||
|
||||
jedis.set("car1", "car1");
|
||||
jedis.set("car2", "car2");
|
||||
jedis.set("car10", "car10");
|
||||
jedis.set("car1", "car1");
|
||||
jedis.set("car2", "car2");
|
||||
jedis.set("car10", "car10");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.get("car*", "bar*");
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.get("car*", "bar*");
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("car1");
|
||||
expected.add("bar1");
|
||||
expected.add("car2");
|
||||
expected.add("bar2");
|
||||
expected.add("car10");
|
||||
expected.add("bar10");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("car1");
|
||||
expected.add("bar1");
|
||||
expected.add("car2");
|
||||
expected.add("bar2");
|
||||
expected.add("car10");
|
||||
expected.add("bar10");
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(expected, result);
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b10);
|
||||
|
||||
jedis.set(bbar1, bbar1);
|
||||
jedis.set(bbar2, bbar2);
|
||||
jedis.set(bbar10, bbar10);
|
||||
|
||||
jedis.set(bcar1, bcar1);
|
||||
jedis.set(bcar2, bcar2);
|
||||
jedis.set(bcar10, bcar10);
|
||||
|
||||
SortingParams bsp = new SortingParams();
|
||||
bsp.get(bcarstar, bbarstar);
|
||||
|
||||
List<byte[]> bresult = jedis.sort(bfoo, bsp);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(bcar1);
|
||||
bexpected.add(bbar1);
|
||||
bexpected.add(bcar2);
|
||||
bexpected.add(bbar2);
|
||||
bexpected.add(bcar10);
|
||||
bexpected.add(bbar10);
|
||||
|
||||
assertEquals(bexpected, bresult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortStore() {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
|
||||
int result = jedis.sort("foo", "result");
|
||||
int result = jedis.sort("foo", "result");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("10");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("10");
|
||||
|
||||
assertEquals(3, result);
|
||||
assertEquals(expected, jedis.lrange("result", 0, 1000));
|
||||
assertEquals(3, result);
|
||||
assertEquals(expected, jedis.lrange("result", 0, 1000));
|
||||
|
||||
// Binary
|
||||
jedis.lpush(bfoo, b1);
|
||||
jedis.lpush(bfoo, b2);
|
||||
jedis.lpush(bfoo, b10);
|
||||
|
||||
byte[] bkresult = new byte[] { 0X09, 0x0A, 0x0B, 0x0C };
|
||||
int bresult = jedis.sort(bfoo, bkresult);
|
||||
|
||||
List<byte[]> bexpected = new ArrayList<byte[]>();
|
||||
bexpected.add(b1);
|
||||
bexpected.add(b2);
|
||||
bexpected.add(b10);
|
||||
|
||||
assertEquals(3, bresult);
|
||||
assertEquals(bexpected, jedis.lrange(bkresult, 0, 1000));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package redis.clients.jedis.tests.commands;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -10,105 +11,190 @@ import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.Transaction;
|
||||
import redis.clients.jedis.TransactionBlock;
|
||||
import redis.clients.jedis.Protocol.Keyword;
|
||||
|
||||
public class TransactionCommandsTest extends JedisCommandTestBase {
|
||||
Jedis nj;
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] ba = { 0x0A };
|
||||
final byte[] bb = { 0x0B };
|
||||
|
||||
nj = new Jedis(hnp.host, hnp.port, 500);
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.flushAll();
|
||||
}
|
||||
final byte[] bmykey = { 0x42, 0x02, 0x03, 0x04 };
|
||||
|
||||
Jedis nj;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
nj = new Jedis(hnp.host, hnp.port, 500);
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.flushAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multi() {
|
||||
Transaction trans = jedis.multi();
|
||||
Transaction trans = jedis.multi();
|
||||
|
||||
String status = trans.sadd("foo", "a");
|
||||
assertEquals("QUEUED", status);
|
||||
String status = trans.sadd("foo", "a");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = trans.sadd("foo", "b");
|
||||
assertEquals("QUEUED", status);
|
||||
status = trans.sadd("foo", "b");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = trans.scard("foo");
|
||||
assertEquals("QUEUED", status);
|
||||
status = trans.scard("foo");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
List<Object> response = trans.exec();
|
||||
List<Object> response = trans.exec();
|
||||
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
|
||||
// Binary
|
||||
trans = jedis.multi();
|
||||
|
||||
status = trans.sadd(bfoo, ba);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = trans.sadd(bfoo, bb);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = trans.scard(bfoo);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
response = trans.exec();
|
||||
|
||||
expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiBlock() {
|
||||
List<Object> response = jedis.multi(new TransactionBlock() {
|
||||
public void execute() {
|
||||
String status = sadd("foo", "a");
|
||||
assertEquals("QUEUED", status);
|
||||
List<Object> response = jedis.multi(new TransactionBlock() {
|
||||
public void execute() {
|
||||
String status = sadd("foo", "a");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = sadd("foo", "b");
|
||||
assertEquals("QUEUED", status);
|
||||
status = sadd("foo", "b");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = scard("foo");
|
||||
assertEquals("QUEUED", status);
|
||||
}
|
||||
});
|
||||
status = scard("foo");
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
}
|
||||
});
|
||||
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
|
||||
// Binary
|
||||
response = jedis.multi(new TransactionBlock() {
|
||||
public void execute() {
|
||||
String status = sadd(bfoo, ba);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = sadd(bfoo, bb);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
|
||||
status = scard(bfoo);
|
||||
assertEquals(Keyword.QUEUED.name(), status);
|
||||
}
|
||||
});
|
||||
|
||||
expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void watch() throws UnknownHostException, IOException {
|
||||
jedis.watch("mykey");
|
||||
Transaction t = jedis.multi();
|
||||
jedis.watch("mykey");
|
||||
Transaction t = jedis.multi();
|
||||
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set("mykey", "bar");
|
||||
nj.disconnect();
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set("mykey", "bar");
|
||||
nj.disconnect();
|
||||
|
||||
t.set("mykey", "foo");
|
||||
List<Object> resp = t.exec();
|
||||
assertEquals(null, resp);
|
||||
assertEquals("bar", jedis.get("mykey"));
|
||||
t.set("mykey", "foo");
|
||||
List<Object> resp = t.exec();
|
||||
assertEquals(null, resp);
|
||||
assertEquals("bar", jedis.get("mykey"));
|
||||
|
||||
// Binary
|
||||
jedis.watch(bmykey);
|
||||
t = jedis.multi();
|
||||
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set(bmykey, bbar);
|
||||
nj.disconnect();
|
||||
|
||||
t.set(bmykey, bfoo);
|
||||
resp = t.exec();
|
||||
assertEquals(null, resp);
|
||||
assertTrue(Arrays.equals(bbar, jedis.get(bmykey)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unwatch() throws UnknownHostException, IOException {
|
||||
jedis.watch("mykey");
|
||||
String val = jedis.get("mykey");
|
||||
val = "foo";
|
||||
String status = jedis.unwatch();
|
||||
assertEquals("OK", status);
|
||||
Transaction t = jedis.multi();
|
||||
jedis.watch("mykey");
|
||||
String val = jedis.get("mykey");
|
||||
val = "foo";
|
||||
String status = jedis.unwatch();
|
||||
assertEquals("OK", status);
|
||||
Transaction t = jedis.multi();
|
||||
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set("mykey", "bar");
|
||||
nj.disconnect();
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set("mykey", "bar");
|
||||
nj.disconnect();
|
||||
|
||||
t.set("mykey", val);
|
||||
List<Object> resp = t.exec();
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add("OK");
|
||||
assertEquals(expected, resp);
|
||||
t.set("mykey", val);
|
||||
List<Object> resp = t.exec();
|
||||
assertEquals(1, resp.size());
|
||||
assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8),
|
||||
(byte[]) resp.get(0));
|
||||
|
||||
// Binary
|
||||
jedis.watch(bmykey);
|
||||
byte[] bval = jedis.get(bmykey);
|
||||
bval = bfoo;
|
||||
status = jedis.unwatch();
|
||||
assertEquals(Keyword.OK.name(), status);
|
||||
t = jedis.multi();
|
||||
|
||||
nj.connect();
|
||||
nj.auth("foobared");
|
||||
nj.set(bmykey, bbar);
|
||||
nj.disconnect();
|
||||
|
||||
t.set(bmykey, bval);
|
||||
resp = t.exec();
|
||||
assertEquals(1, resp.size());
|
||||
assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8),
|
||||
(byte[]) resp.get(0));
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void validateWhenInMulti() {
|
||||
jedis.multi();
|
||||
jedis.ping();
|
||||
jedis.multi();
|
||||
jedis.ping();
|
||||
}
|
||||
}
|
||||
BIN
src/test/resources/MySimpson.png
Normal file
BIN
src/test/resources/MySimpson.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
Reference in New Issue
Block a user