-
Notifications
You must be signed in to change notification settings - Fork 513
compatiable ror bitmap rdb type #1079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
1406f24
04a3858
beb548b
758d1ad
e807c09
61e3910
22abb62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,11 @@ | |
| import com.ctrip.xpipe.redis.core.redis.operation.RedisOp; | ||
| import com.ctrip.xpipe.redis.core.redis.operation.RedisOpParser; | ||
| import com.ctrip.xpipe.redis.core.redis.operation.RedisOpType; | ||
| import com.ctrip.xpipe.redis.core.redis.operation.op.RedisOpMultiKVs; | ||
| import com.ctrip.xpipe.redis.core.redis.operation.op.RedisOpMultiSubKey; | ||
| import com.ctrip.xpipe.tuple.Pair; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.*; | ||
|
|
||
| /** | ||
| * @author tb | ||
|
|
@@ -24,12 +22,28 @@ public class RedisOpWithSubKeysParser extends AbstractRedisOpParser implements R | |
| private Integer kvNum; | ||
| private boolean kvReverse; | ||
|
|
||
| private byte[] NX_BYTES = new byte[]{'N','X'}; | ||
| private byte[] XX_BYTES = new byte[]{'X','X'}; | ||
| private byte[] GT_BYTES = new byte[]{'G','T'}; | ||
| private byte[] LT_BYTES = new byte[]{'L','T'}; | ||
| private byte[] CH_BYTES = new byte[]{'C','H'}; | ||
| private byte[] INCR_BYTES = new byte[]{'I','N','C','R'}; | ||
| private static final Set<ByteBuffer> NON_KEY_COMMANDS = new HashSet<>(); | ||
| private static final Set<ByteBuffer> NON_KEY_COMMANDS_WITH_ARGS = new HashSet<>(); | ||
|
|
||
| private static final byte[] FIELDS_BYTES = "FIELDS".getBytes(); | ||
| private static final byte[] FIELDS_BYTES_LOWER = "fields".getBytes(); | ||
|
|
||
| static { | ||
| // 无参数关键字 | ||
| String[] noArgCmds = { | ||
| "NX", "nx", "XX", "xx", "GT", "gt", "LT", "lt", "CH", "ch", | ||
| "INCR", "incr", "KEEPTTL", "keepttl", "FNX", "fnx", "FXX", "fxx", "PERSIST", "persist" | ||
| }; | ||
| for (String cmd : noArgCmds) { | ||
| NON_KEY_COMMANDS.add(ByteBuffer.wrap(cmd.getBytes())); | ||
| } | ||
| // 带一个参数的关键字 | ||
| String[] oneArgCmds = { "EX", "ex", "PX", "px", "EXAT", "exat", "PXAT", "pxat" }; | ||
| for (String cmd : oneArgCmds) { | ||
| NON_KEY_COMMANDS_WITH_ARGS.add(ByteBuffer.wrap(cmd.getBytes())); | ||
| NON_KEY_COMMANDS.add(ByteBuffer.wrap(cmd.getBytes())); // 也在无参数集合中,因为需要先识别 | ||
| } | ||
| } | ||
|
|
||
| public RedisOpWithSubKeysParser(RedisOpType redisOpType, int keyStartIndex, int kvNum,boolean kvReverse) { | ||
| this.redisOpType = redisOpType; | ||
|
|
@@ -43,6 +57,13 @@ public RedisOp parse(byte[][] args) { | |
| Pair<RedisOpType, byte[][]> pair = redisOpType.transfer(redisOpType, args); | ||
| args = pair.getValue(); | ||
|
|
||
| if(usesFieldsSyntax(redisOpType)) { | ||
| return parseFieldsCommand(redisOpType,args); | ||
| } | ||
| return parseGeneralCommands(args, pair); | ||
| } | ||
|
|
||
| private RedisOpMultiSubKey parseGeneralCommands(byte[][] args, Pair<RedisOpType, byte[][]> pair) { | ||
| // 计算容量 - 主键 + 子键数量 | ||
| int subKeyCount = (args.length - keyStartIndex-1) / kvNum; | ||
| int capacity = 1 + subKeyCount; | ||
|
|
@@ -76,7 +97,7 @@ public RedisOp parse(byte[][] args) { | |
| subKeys.add(subKey); | ||
| } | ||
|
|
||
| return new RedisOpMultiSubKey(pair.getKey(), args, key,subKeys); | ||
| return new RedisOpMultiSubKey(pair.getKey(), args, key, subKeys); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -85,8 +106,45 @@ public int getOrder() { | |
| } | ||
|
|
||
| private boolean nonKey(byte[] args){ | ||
| return Arrays.equals(NX_BYTES,args) || Arrays.equals(XX_BYTES,args) | ||
| || Arrays.equals(GT_BYTES,args) || Arrays.equals(LT_BYTES,args) | ||
| || Arrays.equals(CH_BYTES,args) || Arrays.equals(INCR_BYTES,args); | ||
| return NON_KEY_COMMANDS.contains(ByteBuffer.wrap(args)); | ||
| } | ||
|
|
||
| private boolean usesFieldsSyntax(RedisOpType opType) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 放到OpType定义里? |
||
| return opType == RedisOpType.HSETEX || opType == RedisOpType.HEXPIRE | ||
| || opType == RedisOpType.HEXPIREAT || opType == RedisOpType.HGETEX | ||
| || opType == RedisOpType.HGETDEL || opType == RedisOpType.HPEXPIRE | ||
| || opType == RedisOpType.HPEXPIREAT || opType == RedisOpType.HPERSIST; | ||
| } | ||
|
|
||
| private RedisOp parseFieldsCommand(RedisOpType opType, byte[][] args) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和parseGeneralCommands的关系? |
||
| int idx = keyStartIndex; | ||
| RedisKey key = new RedisKey(args[idx++]); | ||
|
|
||
| // 跳过 FIELDS 之前的所有可选标志 | ||
| while (idx < args.length) { | ||
| ByteBuffer tokenBuf = ByteBuffer.wrap(args[idx]); | ||
| if (NON_KEY_COMMANDS.contains(tokenBuf)) { | ||
| idx++; | ||
| if (NON_KEY_COMMANDS_WITH_ARGS.contains(tokenBuf) && idx < args.length) { | ||
| idx++; // 跳过标志的参数 | ||
| } | ||
| } else if (Arrays.equals(args[idx], FIELDS_BYTES) || Arrays.equals(args[idx], FIELDS_BYTES_LOWER)) { | ||
| idx++; | ||
| break; | ||
| } else { | ||
| idx++; | ||
| } | ||
| } | ||
|
|
||
| if (idx >= args.length) throw new IllegalArgumentException("Missing numfields"); | ||
| int numFields = Integer.parseInt(new String(args[idx++])); | ||
| List<RedisKey> subKeys = new ArrayList<>(numFields); | ||
|
|
||
| for (int i = 0; i < numFields; i++) { | ||
| if (idx >= args.length) throw new IllegalArgumentException("Incomplete field list"); | ||
| subKeys.add(new RedisKey(args[idx])); | ||
| idx += kvNum; | ||
| } | ||
| return new RedisOpMultiSubKey(opType, args, key, subKeys); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,7 +90,8 @@ enum RdbType { | |
| HASH_ZIPLIST(RdbConstant.REDIS_RDB_TYPE_HASH_ZIPLIST, false, RdbHashZipListParser::new), | ||
| LIST_QUICKLIST(RdbConstant.REDIS_RDB_TYPE_LIST_QUICKLIST, false, RdbQuickListParser::new), | ||
| STREAM_LISTPACKS(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS, false, RdbStreamListpacksParser::new), | ||
| // BITMAP(RdbConstant.REDIS_RDB_TYPE_BITMAP, false, RdbBitmapParser::new), | ||
| BITMAP(RdbConstant.REDIS_RDB_TYPE_BITMAP, false, RdbBitmapParser::new), | ||
| BITMAP_9(9,RdbConstant.REDIS_RDB_TYPE_BITMAP_9, false, RdbBitmapParser::new), | ||
| CRDT(RdbConstant.REDIS_RDB_TYPE_CRDT, false, DefaultRdbCrdtParser::new), | ||
| // MODULE_AUX(RdbConstant.REDIS_RDB_OP_CODE_MODULE_AUX), | ||
| IDLE(RdbConstant.REDIS_RDB_OP_CODE_IDLE, true, RdbIdleParser::new), | ||
|
|
@@ -132,15 +133,25 @@ enum RdbType { | |
| STREAM_LISTPACKS_3(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS_3, false, RdbStreamListpacks3Parser::new), | ||
| STREAM_LISTPACKS_4(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS_4, false, RdbStreamListpacks4Parser::new); | ||
|
|
||
| private int version; | ||
| private short code; | ||
|
|
||
| private boolean rdbOp; | ||
|
|
||
| private Function<RdbParseContext, RdbParser> parserConstructor; | ||
|
|
||
| private static final Map<Short, RdbType> types = new HashMap<>(); | ||
| private static final Map<Integer,Map<Short, RdbType>> versionTypes = new HashMap<>(); | ||
|
|
||
| RdbType(short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor) { | ||
| this.version = 0; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 复用RdbType(int version,short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor)? |
||
| this.code = code; | ||
| this.rdbOp = rdbOp; | ||
| this.parserConstructor = parserConstructor; | ||
| } | ||
|
|
||
| RdbType(int version,short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor) { | ||
| this.version = version; | ||
| this.code = code; | ||
| this.rdbOp = rdbOp; | ||
| this.parserConstructor = parserConstructor; | ||
|
|
@@ -163,14 +174,28 @@ public RdbParser<?> makeParser(RdbParseContext parserManager) { | |
|
|
||
| static { | ||
| for (RdbType rdbType : values()) { | ||
| types.put(rdbType.code, rdbType); | ||
| if(rdbType.version != 0) { | ||
| Map<Short, RdbType> versionMap = versionTypes.computeIfAbsent(rdbType.version, (version) -> new HashMap<>()); | ||
| versionMap.put(rdbType.code, rdbType); | ||
| }else { | ||
| types.put(rdbType.code, rdbType); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static RdbType findByCode(short code) { | ||
| return types.get(code); | ||
| } | ||
|
|
||
| public static RdbType findByCode(int version,short code) { | ||
| Map<Short,RdbType> versionMap = versionTypes.getOrDefault(version,types); | ||
| RdbType rdbType = versionMap.get(code); | ||
| if(rdbType == null){ | ||
| return findByCode(code); | ||
| } | ||
| return rdbType; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| String cSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
扫一下8.0注册了getkeyrequests_proc的命令,如果都符合可以用这种写法;否则可以参考下Redis的写法,对于特殊的key获取定义方法注册到命令定义枚举