Browse Source

Merge pull request #1091 in CORE/base-third from feature/10.0 to research/10.0

* commit '84a5d135b0aeb64b348157057da67c22acad556f':
  DEC-15495 redis命令支持替换
research/10.0
superman 4 years ago
parent
commit
1bbc7af5db
  1. 84
      fine-jedis/src/main/java/com/fr/third/redis/clients/jedis/Protocol.java

84
fine-jedis/src/main/java/com/fr/third/redis/clients/jedis/Protocol.java

@ -1,10 +1,5 @@
package com.fr.third.redis.clients.jedis; package com.fr.third.redis.clients.jedis;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.fr.third.redis.clients.jedis.commands.ProtocolCommand; import com.fr.third.redis.clients.jedis.commands.ProtocolCommand;
import com.fr.third.redis.clients.jedis.exceptions.JedisAskDataException; import com.fr.third.redis.clients.jedis.exceptions.JedisAskDataException;
import com.fr.third.redis.clients.jedis.exceptions.JedisBusyException; import com.fr.third.redis.clients.jedis.exceptions.JedisBusyException;
@ -17,6 +12,13 @@ import com.fr.third.redis.clients.jedis.util.RedisInputStream;
import com.fr.third.redis.clients.jedis.util.RedisOutputStream; import com.fr.third.redis.clients.jedis.util.RedisOutputStream;
import com.fr.third.redis.clients.jedis.util.SafeEncoder; import com.fr.third.redis.clients.jedis.util.SafeEncoder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public final class Protocol { public final class Protocol {
private static final String ASK_RESPONSE = "ASK"; private static final String ASK_RESPONSE = "ASK";
@ -79,13 +81,44 @@ public final class Protocol {
public static final byte[] POSITIVE_INFINITY_BYTES = "+inf".getBytes(); public static final byte[] POSITIVE_INFINITY_BYTES = "+inf".getBytes();
public static final byte[] NEGATIVE_INFINITY_BYTES = "-inf".getBytes(); public static final byte[] NEGATIVE_INFINITY_BYTES = "-inf".getBytes();
public static Map<ProtocolCommand, ProtocolCommand> commandMapping = new HashMap<ProtocolCommand, ProtocolCommand>();
public static volatile int commandMappingSize = 0;
public static synchronized void initCommandMapping(Map<String, String> mapping) {
commandMapping.clear();
for (Map.Entry<String, String> entry : mapping.entrySet()) {
ProtocolCommand defaultCommand = Command.fromName(entry.getKey());
if (defaultCommand != null) {
commandMapping.put(defaultCommand, new ProtocolCommand() {
@Override
public byte[] getRaw() {
return SafeEncoder.encode(entry.getValue());
}
});
}
}
commandMappingSize = commandMapping.size();
}
private Protocol() { private Protocol() {
// this prevent the class from instantiation // this prevent the class from instantiation
} }
public static void sendCommand(final RedisOutputStream os, final ProtocolCommand command, public static void sendCommand(final RedisOutputStream os, final ProtocolCommand command,
final byte[]... args) { final byte[]... args) {
sendCommand(os, command.getRaw(), args); ProtocolCommand realCommand = getRealCommand(command);
sendCommand(os, realCommand.getRaw(), args);
}
private static ProtocolCommand getRealCommand(ProtocolCommand command) {
if (commandMappingSize > 0 && commandMapping.containsKey(command)) {
return commandMapping.get(command);
}
return command;
} }
private static void sendCommand(final RedisOutputStream os, final byte[] command, private static void sendCommand(final RedisOutputStream os, final byte[] command,
@ -242,20 +275,20 @@ public final class Protocol {
} }
public static enum Command implements ProtocolCommand { public static enum Command implements ProtocolCommand {
PING, SET, GET, QUIT, EXISTS, DEL, UNLINK, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, PING, SET, GET, QUIT, EXISTS, DEL, UNLINK, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX,
DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, 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, 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, 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, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION,
SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK,
ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH,
SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBSUB, ZCOUNT, ZRANGEBYSCORE, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBSUB, ZCOUNT, ZRANGEBYSCORE,
ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, ZLEXCOUNT, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, ZLEXCOUNT,
ZRANGEBYLEX, ZREVRANGEBYLEX, ZREMRANGEBYLEX, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, ZRANGEBYLEX, ZREVRANGEBYLEX, ZREMRANGEBYLEX, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN,
INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH,
SETBIT, GETBIT, BITPOS, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SETBIT, GETBIT, BITPOS, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP,
SENTINEL, DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, SENTINEL, DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT,
SCAN, HSCAN, SSCAN, ZSCAN, WAIT, CLUSTER, ASKING, PFADD, PFCOUNT, PFMERGE, READONLY, GEOADD, GEODIST, SCAN, HSCAN, SSCAN, ZSCAN, WAIT, CLUSTER, ASKING, PFADD, PFCOUNT, PFMERGE, READONLY, GEOADD, GEODIST,
GEOHASH, GEOPOS, GEORADIUS, GEORADIUSBYMEMBER, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB; GEOHASH, GEOPOS, GEORADIUS, GEORADIUSBYMEMBER, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB;
private final byte[] raw; private final byte[] raw;
@ -268,6 +301,17 @@ public final class Protocol {
public byte[] getRaw() { public byte[] getRaw() {
return raw; return raw;
} }
public static ProtocolCommand fromName(String commandName) {
Command[] commands = values();
for (Command command : commands) {
if (command.name().equals(commandName)) {
return command;
}
}
return null;
}
} }
public static enum Keyword { public static enum Keyword {

Loading…
Cancel
Save