org.craft.atom.redis.DefaultRedis Maven / Gradle / Ivy
package org.craft.atom.redis;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Semaphore;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.craft.atom.redis.api.Redis;
import org.craft.atom.redis.api.RedisConnectionException;
import org.craft.atom.redis.api.RedisDataException;
import org.craft.atom.redis.api.RedisException;
import org.craft.atom.redis.api.RedisPubSub;
import org.craft.atom.redis.api.RedisTransaction;
import org.craft.atom.redis.api.Slowlog;
import org.craft.atom.redis.api.handler.RedisMonitorHandler;
import org.craft.atom.redis.api.handler.RedisPsubscribeHandler;
import org.craft.atom.redis.api.handler.RedisSubscribeHandler;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.BitOP;
import redis.clients.jedis.DebugParams;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisMonitor;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.ZParams.Aggregate;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
/**
* @author mindwind
* @version 1.0, Jun 15, 2013
*/
@EqualsAndHashCode(of = { "host", "port" })
@ToString(of = { "host", "port" })
@SuppressWarnings("unchecked")
public class DefaultRedis implements Redis {
private static final String OK = "OK";
private static final ThreadLocal THREAD_LOCAL_JEDIS = new ThreadLocal();
private String host;
private String password;
private int port = 6379;
private int timeout = 2000;
private int database = 0;
private Config poolConfig = poolConfig(100);
private volatile JedisPool pool;
// ~ ---------------------------------------------------------------------------------------------------------
public DefaultRedis(String host, int port) {
this.host = host;
this.port = port;
init();
}
public DefaultRedis(String host, int port, int timeout) {
this.host = host;
this.port = port;
this.timeout = timeout;
init();
}
public DefaultRedis(String host, int port, int timeout, int poolSize) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig(poolSize);
init();
}
public DefaultRedis(String host, int port, int timeout, int poolSize, String password) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig(poolSize);
this.password = password;
init();
}
public DefaultRedis(String host, int port, int timeout, int poolSize, String password, int database) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig(poolSize);
this.password = password;
this.database = database;
init();
}
public DefaultRedis(String host, int port, int timeout, Config poolConfig) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig;
init();
}
public DefaultRedis(String host, int port, int timeout, Config poolConfig, String password) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig;
this.password = password;
init();
}
public DefaultRedis(String host, int port, int timeout, Config poolConfig, String password, int database) {
this.host = host;
this.port = port;
this.timeout = timeout;
this.poolConfig = poolConfig;
this.password = password;
this.database = database;
init();
}
private Config poolConfig(int poolSize) {
if (poolSize <= 0) {
throw new IllegalArgumentException(String.format("Redis init must > 0", poolSize));
}
JedisPoolConfig jpc = new JedisPoolConfig();
jpc.setMaxActive(poolSize);
jpc.setMaxIdle(poolSize);
jpc.setMinIdle(0);
return jpc;
}
private void init() {
if (timeout < 0) {
throw new IllegalArgumentException(String.format("Redis init must >= 0", timeout));
}
pool = new JedisPool(poolConfig, host, port, timeout, password, database);
}
// ~ ---------------------------------------------------------------------------------------------------------
public String host() {
return host;
}
public int port() {
return port;
}
public String password() {
return password;
}
public int timeout() {
return timeout;
}
public int database() {
return database;
}
public Config config() {
return poolConfig;
}
// ~ --------------------------------------------------------------------------------------------------------- Keys
@Override
public Long del(String... keys) {
return (Long) executeCommand(CommandEnum.DEL, new Object[] { keys });
}
private Long del0(Jedis j, String... keys) {
return j.del(keys);
}
@Override
public byte[] dump(String key) {
return (byte[]) executeCommand(CommandEnum.DUMP, new Object[] { key });
}
private byte[] dump0(Jedis j, String key) {
return j.dump(key);
}
@Override
public Boolean exists(String key) {
return (Boolean) executeCommand(CommandEnum.EXISTS, new Object[] { key });
}
private Boolean exists0(Jedis j, String key) {
return j.exists(key);
}
@Override
public Long expire(String key, int seconds) {
return (Long) executeCommand(CommandEnum.EXPIRE, new Object[] { key, seconds });
}
private Long expire0(Jedis j, String key, int seconds) {
return j.expire(key, seconds);
}
@Override
public Long expireat(String key, long timestamp) {
return (Long) executeCommand(CommandEnum.EXPIREAT, new Object[] { key, timestamp });
}
private Long expireat0(Jedis j, String key, long timestamp) {
return j.expireAt(key, timestamp);
}
@Override
public Set keys(String pattern) {
return (Set) executeCommand(CommandEnum.KEYS, new Object[] { pattern });
}
private Set keys0(Jedis j, String pattern) {
return j.keys(pattern);
}
@Override
public String migrate(String host, int port, String key, int destinationdb, int timeout) {
return (String) executeCommand(CommandEnum.MIGRATE, new Object[] { host, port, key, destinationdb, timeout });
}
private String migrate0(Jedis j, String host, int port, String key, int destinationdb, int timeout) {
return j.migrate(host, port, key, destinationdb, timeout);
}
@Override
public Long move(String key, int db) {
return (Long) executeCommand(CommandEnum.MOVE, new Object[] { key, db });
}
private Long move0(Jedis j, String key, int db) {
return j.move(key, db);
}
@Override
public Long objectrefcount(String key) {
return (Long) executeCommand(CommandEnum.OBJECT_REFCOUNT, new Object[] { key });
}
private Long objectrefcount0(Jedis j, String key) {
return j.objectRefcount(key);
}
@Override
public String objectencoding(String key) {
return (String) executeCommand(CommandEnum.OBJECT_ENCODING, new Object[] { key });
}
private String objectencoding0(Jedis j, String key) {
return j.objectEncoding(key);
}
@Override
public Long objectidletime(String key) {
return (Long) executeCommand(CommandEnum.OBJECT_IDLETIME, new Object[] { key });
}
private Long objectidletime0(Jedis j, String key) {
return j.objectIdletime(key);
}
@Override
public Long persist(String key) {
return (Long) executeCommand(CommandEnum.PERSIST, new Object[] { key });
}
private Long persist0(Jedis j, String key) {
return j.persist(key);
}
@Override
public Long pexpire(String key, int milliseconds) {
return (Long) executeCommand(CommandEnum.PEXPIRE, new Object[] { key, milliseconds });
}
private Long pexpire0(Jedis j, String key, int milliseconds) {
return j.pexpire(key, milliseconds);
}
@Override
public Long pexpireat(String key, long millisecondstimestamp) {
return (Long) executeCommand(CommandEnum.PEXPIREAT, new Object[] { key, millisecondstimestamp });
}
private Long pexpireat0(Jedis j, String key, long millisecondstimestamp) {
return j.pexpireAt(key, millisecondstimestamp);
}
@Override
public Long pttl(String key) {
return (Long) executeCommand(CommandEnum.PTTL, new Object[] { key });
}
private Long pttl0(Jedis j, String key) {
return j.pttl(key);
}
@Override
public String randomkey() {
return (String) executeCommand(CommandEnum.RANDOMKEY, new Object[] {});
}
private String randomkey0(Jedis j) {
return j.randomKey();
}
@Override
public String rename(String key, String newkey) {
return (String) executeCommand(CommandEnum.RENAME, new Object[] { key, newkey });
}
private String rename0(Jedis j, String key, String newkey) {
return j.rename(key, newkey);
}
@Override
public Long renamenx(String key, String newkey) {
return (Long) executeCommand(CommandEnum.RENAMENX, new Object[] { key, newkey });
}
private Long renamenx0(Jedis j, String key, String newkey) {
return j.renamenx(key, newkey);
}
@Override
public String restore(String key, int ttl, byte[] serializedvalue) {
return (String) executeCommand(CommandEnum.RESTORE, new Object[] { key, ttl, serializedvalue });
}
private String restore0(Jedis j, String key, int ttl, byte[] serializedvalue) {
return j.restore(key, ttl, serializedvalue);
}
@Override
public List sort(String key) {
return (List) executeCommand(CommandEnum.SORT, new Object[] { key });
}
private List sort0(Jedis j, String key) {
return j.sort(key);
}
@Override
public List sort(String key, boolean desc) {
return (List) executeCommand(CommandEnum.SORT_DESC, new Object[] { key, desc });
}
private List sort_desc(Jedis j, String key, boolean desc) {
SortingParams sp = new SortingParams();
if (desc) {
sp.desc();
}
return j.sort(key, sp);
}
@Override
public List sort(String key, boolean alpha, boolean desc) {
return (List) executeCommand(CommandEnum.SORT_ALPHA_DESC, new Object[] { key, alpha, desc });
}
private List sort_alpha_desc(Jedis j, String key, boolean alpha, boolean desc) {
SortingParams sp = new SortingParams();
if (desc) {
sp.desc();
}
if (alpha) {
sp.alpha() ;
}
return j.sort(key, sp);
}
@Override
public List sort(String key, int offset, int count) {
return (List) executeCommand(CommandEnum.SORT_OFFSET_COUNT, new Object[] { key, offset, count });
}
private List sort_offset_count(Jedis j, String key, int offset, int count) {
return j.sort(key, new SortingParams().limit(offset, count));
}
@Override
public List sort(String key, int offset, int count, boolean alpha, boolean desc) {
return (List) executeCommand(CommandEnum.SORT_OFFSET_COUNT_ALPHA_DESC, new Object[] { key, offset, count, alpha, desc });
}
private List sort_offset_count_alpha_desc(Jedis j, String key, int offset, int count, boolean alpha, boolean desc) {
SortingParams sp = new SortingParams();
if (desc) {
sp.desc();
}
if (alpha) {
sp.alpha() ;
}
sp.limit(offset, count);
return j.sort(key, sp);
}
@Override
public List sort(String key, String bypattern, String... getpatterns) {
return (List) executeCommand(CommandEnum.SORT_BY_GET, new Object[] { key, bypattern, getpatterns });
}
private List sort_by_get(Jedis j, String key, String bypattern, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
return j.sort(key, sp);
}
@Override
public List sort(String key, String bypattern, boolean desc, String... getpatterns) {
return (List) executeCommand(CommandEnum.SORT_BY_DESC_GET, new Object[] { key, bypattern, desc, getpatterns });
}
private List sort_by_desc_get(Jedis j, String key, String bypattern, boolean desc, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
if (desc) {
sp.desc();
}
return j.sort(key, sp);
}
@Override
public List sort(String key, String bypattern, boolean alpha, boolean desc, String... getpatterns) {
return (List) executeCommand(CommandEnum.SORT_BY_ALPHA_DESC_GET, new Object[] { key, bypattern, alpha, desc, getpatterns });
}
private List sort_by_alpha_desc_get(Jedis j, String key, String bypattern, boolean alpha, boolean desc, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
if (alpha) {
sp.alpha();
}
if (desc) {
sp.desc();
}
return j.sort(key, sp);
}
@Override
public List sort(String key, String bypattern, int offset, int count, String... getpatterns) {
return (List) executeCommand(CommandEnum.SORT_BY_OFFSET_COUNT_GET, new Object[] { key, bypattern, offset, count, getpatterns });
}
private List sort_by_offset_count_get(Jedis j, String key, String bypattern, int offset, int count, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
sp.limit(offset, count);
return j.sort(key, sp);
}
@Override
public List sort(String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String... getpatterns) {
return (List) executeCommand(CommandEnum.SORT_BY_OFFSET_COUNT_ALPHA_DESC_GET, new Object[] { key, bypattern, offset, count, alpha, desc, getpatterns });
}
private List sort_by_offset_count_alpha_desc_get(Jedis j, String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
sp.limit(offset, count);
if (alpha) {
sp.alpha();
}
if (desc) {
sp.desc();
}
return j.sort(key, sp);
}
@Override
public Long sort(String key, String destination) {
return (Long) executeCommand(CommandEnum.SORT_DESTINATION, key, destination);
}
private Long sort_destination(Jedis j, String key, String destination) {
return j.sort(key, destination);
}
@Override
public Long sort(String key, boolean desc, String destination) {
return (Long) executeCommand(CommandEnum.SORT_DESC_DESTINATION, key, desc, destination);
}
private Long sort_desc_destination(Jedis j, String key, boolean desc, String destination) {
SortingParams sp = new SortingParams();
if (desc) {
sp.desc();
}
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, boolean alpha, boolean desc, String destination) {
return (Long) executeCommand(CommandEnum.SORT_ALPHA_DESC_DESTINATION, key, alpha, desc, destination);
}
private Long sort_alpha_desc_destination(Jedis j, String key, boolean alpha, boolean desc, String destination) {
SortingParams sp = new SortingParams();
if (desc) {
sp.desc();
}
if (alpha) {
sp.alpha();
}
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, int offset, int count, String destination) {
return (Long) executeCommand(CommandEnum.SORT_OFFSET_COUNT_DESTINATION, key, offset, count, destination);
}
private Long sort_offset_count_destination(Jedis j, String key, int offset, int count, String destination) {
SortingParams sp = new SortingParams();
sp.limit(offset, count);
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, int offset, int count, boolean alpha, boolean desc, String destination) {
return (Long) executeCommand(CommandEnum.SORT_OFFSET_COUNT_ALPHA_DESC_DESTINATION, key, offset, count, alpha, desc, destination);
}
private Long sort_offset_count_alpha_desc_destination(Jedis j, String key, int offset, int count, boolean alpha, boolean desc, String destination) {
SortingParams sp = new SortingParams();
sp.limit(offset, count);
if (desc) {
sp.desc();
}
if (alpha) {
sp.alpha();
}
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, String bypattern, String destination, String... getpatterns) {
return (Long) executeCommand(CommandEnum.SORT_BY_DESTINATION_GET, new Object[] { key, bypattern, destination, getpatterns });
}
private Long sort_by_destination_get(Jedis j, String key, String bypattern, String destination, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, String bypattern, boolean desc, String destination, String... getpatterns) {
return (Long) executeCommand(CommandEnum.SORT_BY_DESC_DESTINATION_GET, new Object[] { key, bypattern, desc, destination, getpatterns });
}
private Long sort_by_desc_destination_get(Jedis j, String key, String bypattern, boolean desc, String destination, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
if (desc) {
sp.desc();
}
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, String bypattern, boolean alpha, boolean desc, String destination, String... getpatterns) {
return (Long) executeCommand(CommandEnum.SORT_BY_ALPHA_DESC_DESTINATION_GET, new Object[] { key, bypattern, alpha, desc, destination, getpatterns });
}
private Long sort_by_alpha_desc_destination_get(Jedis j, String key, String bypattern, boolean alpha, boolean desc, String destination, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
if (alpha) {
sp.alpha();
}
if (desc) {
sp.desc();
}
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, String bypattern, int offset, int count, String destination, String... getpatterns) {
return (Long) executeCommand(CommandEnum.SORT_BY_OFFSET_COUNT_DESTINATION_GET, new Object[] { key, bypattern, offset, count, destination, getpatterns });
}
private Long sort_by_offset_count_destination_get(Jedis j, String key, String bypattern, int offset, int count, String destination, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
sp.limit(offset, count);
return j.sort(key, sp, destination);
}
@Override
public Long sort(String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String destination, String... getpatterns) {
return (Long) executeCommand(CommandEnum.SORT_BY_OFFSET_COUNT_ALPHA_DESC_DESTINATION_GET, new Object[] { key, bypattern, offset, count, alpha, desc, destination, getpatterns });
}
private Long sort_by_offset_count_alpha_desc_destination_get(Jedis j, String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String destination, String... getpatterns) {
SortingParams sp = new SortingParams();
sp.by(bypattern);
sp.get(getpatterns);
sp.limit(offset, count);
if (alpha) {
sp.alpha();
}
if (desc) {
sp.desc();
}
return j.sort(key, sp, destination);
}
@Override
public Long ttl(String key) {
return (Long) executeCommand(CommandEnum.TTL, new Object[] { key });
}
private Long ttl0(Jedis j, String key) {
return j.ttl(key);
}
@Override
public String type(String key) {
return (String) executeCommand(CommandEnum.TYPE, new Object[] { key });
}
private String type0(Jedis j, String key) {
return j.type(key);
}
// ~ ------------------------------------------------------------------------------------------------------ Strings
@Override
public Long append(String key, String value) {
return (Long) executeCommand(CommandEnum.APPEND, key, value);
}
private Long append0(Jedis j, String key, String value) {
return j.append(key, value);
}
@Override
public Long bitcount(String key) {
return (Long) executeCommand(CommandEnum.BITCOUNT, key);
}
private Long bitcount0(Jedis j, String key) {
return j.bitcount(key);
}
@Override
public Long bitcount(String key, long start, long end) {
return (Long) executeCommand(CommandEnum.BITCOUNT_START_END, key, start, end);
}
private Long bitcount0(Jedis j, String key, long start, long end) {
return j.bitcount(key, start, end);
}
@Override
public Long bitnot(String destkey, String key) {
return (Long) executeCommand(CommandEnum.BITNOT, destkey, key);
}
private Long bitnot0(Jedis j, String destkey, String key) {
return j.bitop(BitOP.NOT, destkey, key);
}
@Override
public Long bitand(String destkey, String... keys) {
return (Long) executeCommand(CommandEnum.BITAND, destkey, keys);
}
private Long bitand0(Jedis j, String destkey, String... keys) {
return j.bitop(BitOP.AND, destkey, keys);
}
@Override
public Long bitor(String destkey, String... keys) {
return (Long) executeCommand(CommandEnum.BITOR, destkey, keys);
}
private Long bitor0(Jedis j, String destkey, String... keys) {
return j.bitop(BitOP.OR, destkey, keys);
}
@Override
public Long bitxor(String destkey, String... keys) {
return (Long) executeCommand(CommandEnum.BITXOR, destkey, keys);
}
private Long bitxor0(Jedis j, String destkey, String... keys) {
return j.bitop(BitOP.XOR, destkey, keys);
}
@Override
public Long decr(String key) {
return (Long) executeCommand(CommandEnum.DECR, key);
}
private Long decr0(Jedis j, String key) {
return j.decr(key);
}
@Override
public Long decrby(String key, long decrement) {
return (Long) executeCommand(CommandEnum.DECRBY, key, decrement);
}
private Long decrby0(Jedis j, String key, long decrement) {
return j.decrBy(key, decrement);
}
@Override
public String get(String key) {
return (String) executeCommand(CommandEnum.GET, key);
}
private String get0(Jedis j, String key) {
return j.get(key);
}
@Override
public Boolean getbit(String key, long offset) {
return (Boolean) executeCommand(CommandEnum.GETBIT, key, offset);
}
private Boolean getbit0(Jedis j, String key, long offset) {
return j.getbit(key, offset);
}
@Override
public String getrange(String key, long start, long end) {
return (String) executeCommand(CommandEnum.GETRANGE, key, start, end);
}
private String getrange0(Jedis j, String key, long start, long end) {
return j.getrange(key, start, end);
}
@Override
public String getset(String key, String value) {
return (String) executeCommand(CommandEnum.GETSET, key, value);
}
private String getset0(Jedis j, String key, String value) {
return j.getSet(key, value);
}
@Override
public Long incr(String key) {
return (Long) executeCommand(CommandEnum.INCR, key);
}
private Long incr0(Jedis j, String key) {
return j.incr(key);
}
@Override
public Long incrby(String key, long increment) {
return (Long) executeCommand(CommandEnum.INCRBY, key, increment);
}
private Long incrby0(Jedis j, String key, long increment) {
return j.incrBy(key, increment);
}
@Override
public Double incrbyfloat(String key, double increment) {
return (Double) executeCommand(CommandEnum.INCRBYFLOAT, key, increment);
}
private Double incrbyfloat0(Jedis j, String key, double increment) {
return j.incrByFloat(key, increment);
}
@Override
public List mget(String... keys) {
return (List) executeCommand(CommandEnum.MGET, new Object[] { keys });
}
private List mget0(Jedis j, String... keys) {
return j.mget(keys);
}
@Override
public String mset(String... keysvalues) {
return (String) executeCommand(CommandEnum.MSET, new Object[] { keysvalues });
}
private String mset0(Jedis j, String... keysvalues) {
return j.mset(keysvalues);
}
@Override
public Long msetnx(String... keysvalues) {
return (Long) executeCommand(CommandEnum.MSETNX, new Object[] { keysvalues });
}
private Long msetnx0(Jedis j, String... keysvalues) {
return j.msetnx(keysvalues);
}
@Override
public String psetex(String key, int milliseconds, String value) {
return (String) executeCommand(CommandEnum.PSETEX, key, milliseconds, value);
}
private String psetex0(Jedis j, String key, int milliseconds, String value) {
return j.psetex(key, milliseconds, value);
}
@Override
public String set(String key, String value) {
return (String) executeCommand(CommandEnum.SET, key, value);
}
private String set0(Jedis j, String key, String value) {
return j.set(key, value);
}
@Override
public String setxx(String key, String value) {
return (String) executeCommand(CommandEnum.SETXX, key, value);
}
private String setxx0(Jedis j, String key, String value) {
return j.set(key, value, "XX");
}
@Override
public String setnxex(String key, String value, int seconds) {
return (String) executeCommand(CommandEnum.SETNXEX, key, value, seconds);
}
private String setnxex0(Jedis j, String key, String value, int seconds) {
return j.set(key, value, "NX", "EX", seconds);
}
@Override
public String setnxpx(String key, String value, int milliseconds) {
return (String) executeCommand(CommandEnum.SETNXPX, key, value, milliseconds);
}
private String setnxpx0(Jedis j, String key, String value, int milliseconds) {
return j.set(key, value, "NX", "PX", milliseconds);
}
@Override
public String setxxex(String key, String value, int seconds) {
return (String) executeCommand(CommandEnum.SETXXEX, key, value, seconds);
}
private String setxxex0(Jedis j, String key, String value, int seconds) {
return j.set(key, value, "XX", "EX", seconds);
}
@Override
public String setxxpx(String key, String value, int milliseconds) {
return (String) executeCommand(CommandEnum.SETXXPX, key, value, milliseconds);
}
private String setxxpx0(Jedis j, String key, String value, int milliseconds) {
return j.set(key, value, "XX", "PX", milliseconds);
}
@Override
public Boolean setbit(String key, long offset, boolean value) {
return (Boolean) executeCommand(CommandEnum.SETBIT, key, offset, value);
}
private Boolean setbit0(Jedis j, String key, long offset, boolean value) {
return j.setbit(key, offset, value);
}
@Override
public String setex(String key, int seconds, String value) {
return (String) executeCommand(CommandEnum.SETEX, key, seconds, value);
}
private String setex0(Jedis j, String key, int seconds, String value) {
return j.setex(key, seconds, value);
}
@Override
public Long setnx(String key, String value) {
return (Long) executeCommand(CommandEnum.SETNX, key, value);
}
private Long setnx0(Jedis j, String key, String value) {
return j.setnx(key, value);
}
@Override
public Long setrange(String key, long offset, String value) {
return (Long) executeCommand(CommandEnum.SETRANGE, key, offset, value);
}
private Long setrange0(Jedis j, String key, long offset, String value) {
return j.setrange(key, offset, value);
}
@Override
public Long strlen(String key) {
return (Long) executeCommand(CommandEnum.STRLEN, key);
}
private Long strlen0(Jedis j, String key) {
return j.strlen(key);
}
// ~ ------------------------------------------------------------------------------------------------------ Hashes
@Override
public Long hdel(String key, String... fields) {
return (Long) executeCommand(CommandEnum.HDEL, key, fields);
}
private Long hdel0(Jedis j, String key, String... fields) {
return j.hdel(key, fields);
}
@Override
public Boolean hexists(String key, String field) {
return (Boolean) executeCommand(CommandEnum.HEXISTS, key, field);
}
private Boolean hexists0(Jedis j, String key, String field) {
return j.hexists(key, field);
}
@Override
public String hget(String key, String field) {
return (String) executeCommand(CommandEnum.HGET, key, field);
}
private String hget0(Jedis j, String key, String field) {
return j.hget(key, field);
}
@Override
public Map hgetall(String key) {
return (Map) executeCommand(CommandEnum.HGETALL, key);
}
private Map hgetall0(Jedis j, String key) {
return j.hgetAll(key);
}
@Override
public Long hincrby(String key, String field, long increment) {
return (Long) executeCommand(CommandEnum.HINCRBY, key, field, increment);
}
private Long hincrby0(Jedis j, String key, String field, long increment) {
return j.hincrBy(key, field, increment);
}
@Override
public Double hincrbyfloat(String key, String field, double increment) {
return (Double) executeCommand(CommandEnum.HINCRBYFLOAT, key, field, increment);
}
private Double hincrbyfloat0(Jedis j, String key, String field, double increment) {
return j.hincrByFloat(key, field, increment);
}
@Override
public Set hkeys(String key) {
return (Set) executeCommand(CommandEnum.HKEYS, key);
}
private Set hkeys0(Jedis j, String key) {
return j.hkeys(key);
}
@Override
public Long hlen(String key) {
return (Long) executeCommand(CommandEnum.HLEN, key);
}
private Long hlen0(Jedis j, String key) {
return j.hlen(key);
}
@Override
public List hmget(String key, String... fields) {
return (List) executeCommand(CommandEnum.HMGET, key, fields);
}
private List hmget0(Jedis j, String key, String... fields) {
return j.hmget(key, fields);
}
@Override
public String hmset(String key, Map fieldvalues) {
return (String) executeCommand(CommandEnum.HMSET, key, fieldvalues);
}
private String hmset0(Jedis j, String key, Map fieldvalues) {
return j.hmset(key, fieldvalues);
}
@Override
public Long hset(String key, String field, String value) {
return (Long) executeCommand(CommandEnum.HSET, key, field, value);
}
private Long hset0(Jedis j, String key, String field, String value) {
return j.hset(key, field, value);
}
@Override
public Long hsetnx(String key, String field, String value) {
return (Long) executeCommand(CommandEnum.HSETNX, key, field, value);
}
private Long hsetnx0(Jedis j, String key, String field, String value) {
return j.hsetnx(key, field, value);
}
@Override
public List hvals(String key) {
return (List) executeCommand(CommandEnum.HVALS, key);
}
private List hvals0(Jedis j, String key) {
return j.hvals(key);
}
// ~ ------------------------------------------------------------------------------------------------------- Lists
@Override
public String blpop(String key) {
return blpop(key, 0);
}
@Override
public String blpop(String key, int timeout) {
Map map = blpop(0, key);
return map.get(key);
}
@Override
public Map blpop(String... keys) {
return blpop(0, keys);
}
@Override
public Map blpop(int timeout, String... keys) {
return (Map) executeCommand(CommandEnum.BLPOP, new Object[] { timeout, keys });
}
private Map blpop0(Jedis j, int timeout, String... keys) {
List l = j.blpop(timeout, keys);
return convert4bpop(l);
}
private Map convert4bpop(List l) {
Map map = new LinkedHashMap();
if (l == null) {
return map;
}
for (int i = 0; i < l.size(); i += 2) {
String key = l.get(i);
String value = l.get(i + 1);
map.put(key, value);
}
return map;
}
@Override
public String brpop(String key) {
return brpop(key, 0);
}
@Override
public String brpop(String key, int timeout) {
Map map = brpop(0, key);
return map.get(key);
}
@Override
public Map brpop(String... keys) {
return brpop(0, keys);
}
@Override
public Map brpop(int timeout, String... keys) {
return (Map) executeCommand(CommandEnum.BRPOP, timeout, keys);
}
private Map brpop0(Jedis j, int timeout, String... keys) {
List l = j.brpop(timeout, keys);
return convert4bpop(l);
}
@Override
public String brpoplpush(String source, String destination, int timeout) {
return (String) executeCommand(CommandEnum.BRPOPLPUSH, source, destination, timeout);
}
private String brpoplpush0(Jedis j, String source, String destination, int timeout) {
return j.brpoplpush(source, destination, timeout);
}
@Override
public String lindex(String key, long index) {
return (String) executeCommand(CommandEnum.LINDEX, key, index);
}
private String lindex0(Jedis j, String key, long index) {
return j.lindex(key, index);
}
@Override
public Long linsertbefore(String key, String pivot, String value) {
return (Long) executeCommand(CommandEnum.LINSERT_BEFORE, key, pivot, value);
}
private Long linsertbefore0(Jedis j, String key, String pivot, String value) {
return j.linsert(key, LIST_POSITION.BEFORE, pivot, value);
}
@Override
public Long linsertafter(String key, String pivot, String value) {
return (Long) executeCommand(CommandEnum.LINSERT_AFTER, key, pivot, value);
}
private Long linsertafter0(Jedis j, String key, String pivot, String value) {
return j.linsert(key, LIST_POSITION.AFTER, pivot, value);
}
@Override
public Long llen(String key) {
return (Long) executeCommand(CommandEnum.LLEN, key);
}
private Long llen0(Jedis j, String key) {
return j.llen(key);
}
@Override
public String lpop(String key) {
return (String) executeCommand(CommandEnum.LPOP, key);
}
private String lpop0(Jedis j, String key) {
return j.lpop(key);
}
@Override
public Long lpush(String key, String... values) {
return (Long) executeCommand(CommandEnum.LPUSH, key, values);
}
private Long lpush0(Jedis j, String key, String... values) {
return j.lpush(key, values);
}
@Override
public Long lpushx(String key, String value) {
return (Long) executeCommand(CommandEnum.LPUSHX, key, value);
}
private Long lpushx0(Jedis j, String key, String value) {
return j.lpushx(key, value);
}
@Override
public List lrange(String key, long start, long stop) {
return (List) executeCommand(CommandEnum.LRANGE, key, start, stop);
}
private List lrange0(Jedis j, String key, long start, long stop) {
return j.lrange(key, start, stop);
}
@Override
public Long lrem(String key, long count, String value) {
return (Long) executeCommand(CommandEnum.LREM, key, count, value);
}
private Long lrem0(Jedis j, String key, long count, String value) {
return j.lrem(key, count, value);
}
@Override
public String lset(String key, long index, String value) {
return (String) executeCommand(CommandEnum.LSET, key, index, value);
}
private String lset0(Jedis j, String key, long index, String value) {
return j.lset(key, index, value);
}
@Override
public String ltrim(String key, long start, long stop) {
return (String) executeCommand(CommandEnum.LTRIM, key, start, stop);
}
private String ltrim0(Jedis j, String key, long start, long stop) {
return j.ltrim(key, start, stop);
}
@Override
public String rpop(String key) {
return (String) executeCommand(CommandEnum.RPOP, key);
}
private String rpop0(Jedis j, String key) {
return j.rpop(key);
}
@Override
public String rpoplpush(String source, String destination) {
return (String) executeCommand(CommandEnum.RPOPLPUSH, source, destination);
}
private String rpoplpush0(Jedis j, String source, String destination) {
return j.rpoplpush(source, destination);
}
@Override
public Long rpush(String key, String... values) {
return (Long) executeCommand(CommandEnum.RPUSH, key, values);
}
private Long rpush0(Jedis j, String key, String... values) {
return j.rpush(key, values);
}
@Override
public Long rpushx(String key, String value) {
return (Long) executeCommand(CommandEnum.RPUSHX, key, value);
}
private Long rpushx0(Jedis j, String key, String value) {
return j.rpushx(key, value);
}
// ~ ------------------------------------------------------------------------------------------------------- Sets
@Override
public Long sadd(String key, String... members) {
return (Long) executeCommand(CommandEnum.SADD, new Object[] { key, members });
}
private Long sadd0(Jedis j, String key, String... members) {
return j.sadd(key, members);
}
@Override
public Long scard(String key) {
return (Long) executeCommand(CommandEnum.SCARD, key);
}
private Long scard0(Jedis j, String key) {
return j.scard(key);
}
@Override
public Set sdiff(String... keys) {
return (Set) executeCommand(CommandEnum.SDIFF, new Object[] { keys });
}
private Set sdiff0(Jedis j, String... keys) {
return j.sdiff(keys);
}
@Override
public Long sdiffstore(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.SDIFFSTORE, destination, keys);
}
private Long sdiffstore0(Jedis j, String destination, String... keys) {
return j.sdiffstore(destination, keys);
}
@Override
public Set sinter(String... keys) {
return (Set) executeCommand(CommandEnum.SINTER, new Object[] { keys });
}
private Set sinter0(Jedis j, String... keys) {
return j.sinter(keys);
}
@Override
public Long sinterstore(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.SINTERSTORE, destination, keys);
}
private Long sinterstore0(Jedis j, String destination, String... keys) {
return j.sinterstore(destination, keys);
}
@Override
public Boolean sismember(String key, String member) {
return (Boolean) executeCommand(CommandEnum.SISMEMBER, key, member);
}
private Boolean sismember0(Jedis j, String key, String member) {
return j.sismember(key, member);
}
@Override
public Set smembers(String key) {
return (Set) executeCommand(CommandEnum.SMEMBERS, key);
}
private Set smembers0(Jedis j, String key) {
return j.smembers(key);
}
@Override
public Long smove(String source, String destination, String member) {
return (Long) executeCommand(CommandEnum.SMOVE, source, destination, member);
}
private Long smove0(Jedis j, String source, String destination, String member) {
return j.smove(source, destination, member);
}
@Override
public String spop(String key) {
return (String) executeCommand(CommandEnum.SPOP, key);
}
private String spop0(Jedis j, String key) {
return j.spop(key);
}
@Override
public String srandmember(String key) {
List list = srandmember(key, 1);
if (list.isEmpty()) {
return null;
} else {
return list.iterator().next();
}
}
@Override
public List srandmember(String key, int count) {
return (List) executeCommand(CommandEnum.SRANDMEMBER, key, count);
}
private List srandmember0(Jedis j, String key, int count) {
return j.srandmember(key, count);
}
@Override
public Long srem(String key, String... members) {
return (Long) executeCommand(CommandEnum.SREM, key, members);
}
private Long srem0(Jedis j, String key, String... members) {
return j.srem(key, members);
}
@Override
public Set sunion(String... keys) {
return (Set) executeCommand(CommandEnum.SUNION, new Object[] { keys });
}
private Set sunion0(Jedis j, String... keys) {
return j.sunion(keys);
}
@Override
public Long sunionstore(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.SUNIONSTORE, destination, keys);
}
private Long sunionstore0(Jedis j, String destination, String... keys) {
return j.sunionstore(destination, keys);
}
// ~ ------------------------------------------------------------------------------------------------- Sorted Sets
@Override
public Long zadd(String key, double score, String member) {
Map scoremembers = new HashMap();
scoremembers.put(score, member);
return zadd(key, scoremembers);
}
@Override
public Long zadd(String key, Map scoremembers) {
return (Long) executeCommand(CommandEnum.ZADD, key, scoremembers);
}
private Long zadd0(Jedis j, String key, Map scoremembers) {
return j.zadd(key, scoremembers);
}
@Override
public Long zcard(String key) {
return (Long) executeCommand(CommandEnum.ZCARD, key);
}
private Long zcard0(Jedis j, String key) {
return j.zcard(key);
}
@Override
public Long zcount(String key, double min, double max) {
return (Long) executeCommand(CommandEnum.ZCOUNT, key, min, max);
}
private Long zcount0(Jedis j, String key, double min, double max) {
return j.zcount(key, min, max);
}
@Override
public Long zcount(String key, String min, String max) {
return (Long) executeCommand(CommandEnum.ZCOUNT_STRING, key, min, max);
}
private Long zcount0(Jedis j, String key, String min, String max) {
return j.zcount(key, min, max);
}
@Override
public Double zincrby(String key, double score, String member) {
return (Double) executeCommand(CommandEnum.ZINCRBY, key, score, member);
}
private Double zincrby0(Jedis j, String key, double score, String member) {
return j.zincrby(key, score, member);
}
@Override
public Long zinterstore(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE, destination, keys);
}
private Long zinterstore0(Jedis j, String destination, String... keys) {
return j.zinterstore(destination, keys);
}
@Override
public Long zinterstoremax(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE_MAX, destination, keys);
}
private Long zinterstoremax0(Jedis j, String destination, String... keys) {
return j.zinterstore(destination, new ZParams().aggregate(Aggregate.MAX), keys);
}
@Override
public Long zinterstoremin(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE_MIN, destination, keys);
}
private Long zinterstoremin0(Jedis j, String destination, String... keys) {
return j.zinterstore(destination, new ZParams().aggregate(Aggregate.MIN), keys);
}
@Override
public Long zinterstore(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE_WEIGHTS, destination, weightkeys);
}
private Long zinterstore_weights(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zinterstore(destination, new ZParams().weights(weights), keys);
}
private Object[] convert4zstore(Map weightkeys) {
int size = weightkeys.size();
String[] keys = new String[size];
int[] weights = new int[size];
List> list = new ArrayList>(weightkeys.entrySet());
for (int i = 0; i < size; i++) {
Entry entry = list.get(i);
keys[i] = entry.getKey();
weights[i] = entry.getValue();
}
return new Object[] { keys, weights };
}
@Override
public Long zinterstoremax(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE_WEIGHTS_MAX, destination, weightkeys);
}
private Long zinterstore_weights_max(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zinterstore(destination, new ZParams().weights(weights).aggregate(Aggregate.MAX), keys);
}
@Override
public Long zinterstoremin(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZINTERSTORE_WEIGHTS_MIN, destination, weightkeys);
}
private Long zinterstore_weights_min(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zinterstore(destination, new ZParams().weights(weights).aggregate(Aggregate.MIN), keys);
}
@Override
public Set zrange(String key, long start, long stop) {
return (Set) executeCommand(CommandEnum.ZRANGE, key, start, stop);
}
private Set zrange0(Jedis j, String key, long start, long stop) {
return j.zrange(key, start, stop);
}
@Override
public Map zrangewithscores(String key, long start, long stop) {
return (Map) executeCommand(CommandEnum.ZRANGE_WITHSCORES, key, start, stop);
}
private Map zrangewithscores0(Jedis j, String key, long start, long stop) {
Set set = j.zrangeWithScores(key, start, stop);
Map map = convert4zrangewithscores(set);
return map;
}
private Map convert4zrangewithscores(Set set) {
Map map = new LinkedHashMap(set.size());
for (Tuple tuple : set) {
map.put(tuple.getElement(), tuple.getScore());
}
return map;
}
@Override
public Set zrangebyscore(String key, double min, double max) {
return (Set) executeCommand(CommandEnum.ZRANGEBYSCORE, key, min, max);
}
private Set zrangebyscore0(Jedis j, String key, double min, double max) {
return j.zrangeByScore(key, min, max);
}
@Override
public Set zrangebyscore(String key, String min, String max) {
return (Set) executeCommand(CommandEnum.ZRANGEBYSCORE_STRING, key, min, max);
}
private Set zrangebyscore_string(Jedis j, String key, String min, String max) {
return j.zrangeByScore(key, min, max);
}
@Override
public Set zrangebyscore(String key, double min, double max, int offset, int count) {
return (Set) executeCommand(CommandEnum.ZRANGEBYSCORE_OFFSET_COUNT, key, min, max, offset, count);
}
private Set zrangebyscore_offset_count(Jedis j, String key, double min, double max, int offset, int count) {
return j.zrangeByScore(key, min, max, offset, count);
}
@Override
public Set zrangebyscore(String key, String min, String max, int offset, int count) {
return (Set) executeCommand(CommandEnum.ZRANGEBYSCORE_OFFSET_COUNT_STRING, key, min, max, offset, count);
}
private Set zrangebyscore_offset_count_string(Jedis j, String key, String min, String max, int offset, int count) {
return j.zrangeByScore(key, min, max, offset, count);
}
@Override
public Map zrangebyscorewithscores(String key, double min, double max) {
return (Map) executeCommand(CommandEnum.ZRANGEBYSCORE_WITHSCORES, key, min, max);
}
private Map zrangebyscorewithscores0(Jedis j, String key, double min, double max) {
Set set = j.zrangeByScoreWithScores(key, min, max);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrangebyscorewithscores(String key, String min, String max) {
return (Map) executeCommand(CommandEnum.ZRANGEBYSCORE_WITHSCORES_STRING, key, min, max);
}
private Map zrangebyscorewithscores_string(Jedis j, String key, String min, String max) {
Set set = j.zrangeByScoreWithScores(key, min, max);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrangebyscorewithscores(String key, double min, double max, int offset, int count) {
return (Map) executeCommand(CommandEnum.ZRANGEBYSCORE_WITHSCORES_OFFSET_COUNT, key, min, max, offset, count);
}
private Map zrangebyscorewithscores_offset_count(Jedis j, String key, double min, double max, int offset, int count) {
Set set = j.zrangeByScoreWithScores(key, min, max, offset, count);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrangebyscorewithscores(String key, String min, String max, int offset, int count) {
return (Map) executeCommand(CommandEnum.ZRANGEBYSCORE_WITHSCORES_OFFSET_COUNT_STRING, key, min, max, offset, count);
}
private Map zrangebyscorewithscores_offset_count_string(Jedis j, String key, String min, String max, int offset, int count) {
Set set = j.zrangeByScoreWithScores(key, min, max, offset, count);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Long zrank(String key, String member) {
return (Long) executeCommand(CommandEnum.ZRANK, key, member);
}
private Long zrank0(Jedis j, String key, String member) {
return j.zrank(key, member);
}
@Override
public Long zrem(String key, String... members) {
return (Long) executeCommand(CommandEnum.ZREM, key, members);
}
private Long zrem0(Jedis j, String key, String... members) {
return j.zrem(key, members);
}
@Override
public Long zremrangebyrank(String key, long start, long stop) {
return (Long) executeCommand(CommandEnum.ZREMRANGEBYRANK, key, start, stop);
}
private Long zremrangebyrank0(Jedis j, String key, long start, long stop) {
return j.zremrangeByRank(key, start, stop);
}
@Override
public Long zremrangebyscore(String key, double min, double max) {
return (Long) executeCommand(CommandEnum.ZREMRANGEBYSCORE, key, min, max);
}
private Long zremrangebyscore0(Jedis j, String key, double min, double max) {
return j.zremrangeByScore(key, min, max);
}
@Override
public Long zremrangebyscore(String key, String min, String max) {
return (Long) executeCommand(CommandEnum.ZREMRANGEBYSCORE_STRING, key, min, max);
}
private Long zremrangebyscore_string(Jedis j, String key, String min, String max) {
return j.zremrangeByScore(key, min, max);
}
@Override
public Set zrevrange(String key, long start, long stop) {
return (Set) executeCommand(CommandEnum.ZREVRANGE, key, start, stop);
}
private Set zrevrange0(Jedis j, String key, long start, long stop) {
return j.zrevrange(key, start, stop);
}
@Override
public Map zrevrangewithscores(String key, long start, long stop) {
return (Map) executeCommand(CommandEnum.ZREVRANGE_WITHSCORES, key, start, stop);
}
private Map zrevrangewithscores0(Jedis j, String key, long start, long stop) {
Set set = j.zrevrangeWithScores(key, start, stop);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Set zrevrangebyscore(String key, double max, double min) {
return (Set) executeCommand(CommandEnum.ZREVRANGEBYSCORE, key, max, min);
}
private Set zrevrangebyscore0(Jedis j, String key, double max, double min) {
return j.zrevrangeByScore(key, max, min);
}
@Override
public Set zrevrangebyscore(String key, String max, String min) {
return (Set) executeCommand(CommandEnum.ZREVRANGEBYSCORE_STRING, key, max, min);
}
private Set zrevrangebyscore_string(Jedis j, String key, String max, String min) {
return j.zrevrangeByScore(key, max, min);
}
@Override
public Set zrevrangebyscore(String key, double max, double min, int offset, int count) {
return (Set) executeCommand(CommandEnum.ZREVRANGEBYSCORE_OFFSET_COUNT, key, max, min, offset, count);
}
private Set zrevrangebyscore_offset_count(Jedis j, String key, double max, double min, int offset, int count) {
return j.zrevrangeByScore(key, max, min, offset, count);
}
@Override
public Set zrevrangebyscore(String key, String max, String min, int offset, int count) {
return (Set) executeCommand(CommandEnum.ZREVRANGEBYSCORE_OFFSET_COUNT_STRING, key, max, min, offset, count);
}
private Set zrevrangebyscore_offset_count_string(Jedis j, String key, String max, String min, int offset, int count) {
return j.zrevrangeByScore(key, max, min, offset, count);
}
@Override
public Map zrevrangebyscorewithscores(String key, double max, double min) {
return (Map) executeCommand(CommandEnum.ZREVRANGEBYSCORE_WITHSCORES, key, max, min);
}
private Map zrevrangebyscorewithscores0(Jedis j, String key, double max, double min) {
Set set = j.zrevrangeByScoreWithScores(key, max, min);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrevrangebyscorewithscores(String key, String max, String min) {
return (Map) executeCommand(CommandEnum.ZREVRANGEBYSCORE_WITHSCORES_STRING, key, max, min);
}
private Map zrevrangebyscorewithscores_string(Jedis j, String key, String max, String min) {
Set set = j.zrevrangeByScoreWithScores(key, max, min);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrevrangebyscorewithscores(String key, double max, double min, int offset, int count) {
return (Map) executeCommand(CommandEnum.ZREVRANGEBYSCORE_WITHSCORES_OFFSET_COUNT, key, max, min, offset, count);
}
private Map zrevrangebyscorewithscores_offset_count(Jedis j, String key, double max, double min, int offset, int count) {
Set set = j.zrevrangeByScoreWithScores(key, max, min, offset, count);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Map zrevrangebyscorewithscores(String key, String max, String min, int offset, int count) {
return (Map) executeCommand(CommandEnum.ZREVRANGEBYSCORE_WITHSCORES_OFFSET_COUNT_STRING, key, max, min, offset, count);
}
private Map zrevrangebyscorewithscores_offset_count_string(Jedis j, String key, String max, String min, int offset, int count) {
Set set = j.zrevrangeByScoreWithScores(key, max, min, offset, count);
Map map = convert4zrangewithscores(set);
return map;
}
@Override
public Long zrevrank(String key, String member) {
return (Long) executeCommand(CommandEnum.ZREVRANK, key, member);
}
private Long zrevrank0(Jedis j, String key, String member) {
return j.zrevrank(key, member);
}
@Override
public Double zscore(String key, String member) {
return (Double) executeCommand(CommandEnum.ZSCORE, key, member);
}
private Double zscore0(Jedis j, String key, String member) {
return j.zscore(key, member);
}
@Override
public Long zunionstore(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE, destination, keys);
}
private Long zunionstore0(Jedis j, String destination, String... keys) {
return j.zunionstore(destination, keys);
}
@Override
public Long zunionstoremax(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE_MAX, destination, keys);
}
private Long zunionstoremax0(Jedis j, String destination, String... keys) {
return j.zunionstore(destination, new ZParams().aggregate(Aggregate.MAX), keys);
}
@Override
public Long zunionstoremin(String destination, String... keys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE_MIN, destination, keys);
}
private Long zunionstoremin0(Jedis j, String destination, String... keys) {
return j.zunionstore(destination, new ZParams().aggregate(Aggregate.MIN), keys);
}
@Override
public Long zunionstore(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE_WEIGHTS, destination, weightkeys);
}
private Long zunionstore_weights(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zunionstore(destination, new ZParams().weights(weights), keys);
}
@Override
public Long zunionstoremax(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE_WEIGHTS_MAX, destination, weightkeys);
}
private Long zunionstore_weights_max(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zunionstore(destination, new ZParams().weights(weights).aggregate(Aggregate.MAX), keys);
}
@Override
public Long zunionstoremin(String destination, Map weightkeys) {
return (Long) executeCommand(CommandEnum.ZUNIONSTORE_WEIGHTS_MIN, destination, weightkeys);
}
private Long zunionstore_weights_min(Jedis j, String destination, Map weightkeys) {
Object[] objs = convert4zstore(weightkeys);
String[] keys = (String[]) objs[0];
int [] weights = (int[]) objs[1];
return j.zunionstore(destination, new ZParams().weights(weights).aggregate(Aggregate.MIN), keys);
}
// ~ ----------------------------------------------------------------------------------------------------- Pub/Sub
@Override
public RedisPubSub psubscribe(RedisPsubscribeHandler handler, String... patterns) {
return psubscribe0(handler, patterns);
}
private RedisPubSub psubscribe0(final RedisPsubscribeHandler handler, final String... patterns) {
final int permits = patterns.length;
final Semaphore s = new Semaphore(permits);
s.drainPermits();
final JedisPubSub jps = new JedisPubSubAdapter() {
@Override
public void onPMessage(String pattern, String channel, String message) {
handler.onMessage(pattern, channel, message);
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
s.release();
handler.onPsubscribe(pattern, subscribedChannels);
}
};
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Jedis j = null;
try {
j = jedis();
j.psubscribe(jps, patterns);
} catch (Exception e) {
RedisException re = handleException(e, j);
handler.onException(re);
} finally {
s.release(permits);
}
}
}, "redis-psubscribe-" + Thread.currentThread().getId());
t.start();
// wait all channels be subscribed
try { s.acquire(permits); } catch (InterruptedException e) {}
RedisPubSub rps = new DefaultRedisPubSub(jps);
return rps;
}
@Override
public Long publish(String channel, String message) {
return (Long) executeCommand(CommandEnum.PUBLISH, channel, message);
}
private Long publish0(Jedis j, String channel, String message) {
return j.publish(channel, message);
}
@Override
public void punsubscribe(RedisPubSub pubsub, String... patterns) {
executeCommand(CommandEnum.PUNSUBSCRIBE, new Object[] { pubsub, patterns });
}
private String punsubscribe0(DefaultRedisPubSub pubsub, String... patterns) {
pubsub.punsubscribe(patterns);
return OK;
}
@Override
public RedisPubSub subscribe(RedisSubscribeHandler handler, String... channels) {
return subscribe0(handler, channels);
}
private RedisPubSub subscribe0(final RedisSubscribeHandler handler, final String... channels) {
final int permits = channels.length;
final Semaphore s = new Semaphore(permits);
s.drainPermits();
final JedisPubSub jps = new JedisPubSubAdapter() {
@Override
public void onMessage(String channel, String message) {
handler.onMessage(channel, message);
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
s.release();
handler.onSubscribe(channel, subscribedChannels);
}
};
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Jedis j = null;
try {
j = jedis();
j.subscribe(jps, channels);
} catch (Exception e) {
RedisException re = handleException(e, j);
handler.onException(re);
} finally {
s.release(permits);
}
}
}, "redis-subscribe-" + Thread.currentThread().getId());
t.start();
// wait all channels be subscribed
try { s.acquire(permits); } catch (InterruptedException e) {}
RedisPubSub rps = new DefaultRedisPubSub(jps);
return rps;
}
@Override
public void unsubscribe(RedisPubSub pubsub, String... channels) {
executeCommand(CommandEnum.UNSUBSCRIBE, new Object[] { pubsub, channels });
}
private String unsubscribe0(DefaultRedisPubSub pubsub, String... channels) {
pubsub.unsubscribe(channels);
return OK;
}
// ~ ------------------------------------------------------------------------------------------------ Transactions
// Transaction command execution sequence.
// MULTI ... -> ... EXEC
// MULTI ... -> DISCARD
// WATCH -> MULTI ... -> ... EXEC
// WATCH -> UNWATCH -> MULTI ... -> ... EXEC
// WATCH -> MULTI ... -> UNWATCH -> ... EXEC
// WATCH ... -> ... MULTI ... -> ... EXEC
@Override
public String discard(RedisTransaction t) {
return (String) executeCommand(CommandEnum.DISCARD, new Object[] { t });
}
private String discard0(DefaultRedisTransaction t) {
try {
return t.discard();
} finally {
unbind();
}
}
@Override
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy