参考过不少Redis协议解析框架,不少是用栈或者状态机实现,这里先简单点用递归实现,解码器代码如下:
public class RespArrayDecoder implements RespDecoder { @Override public Object decode(ByteBuf buffer) { int lineEndIndex = CodecUtils.X.findLineEndIndex(buffer); if (-1 == lineEndIndex) { return null; } // 解析元素个数 Long length = (Long) DefaultRespCodec.DECODERS.get(ReplyType.INTEGER).decode(buffer); if (null == length) { return null; } // Null Array if (RespConstants.NEGATIVE_ONE.equals(length)) { return null; } // Array Empty List if (RespConstants.ZERO.equals(length)) { return Lists.newArrayList(); } List<Object> result = Lists.newArrayListWithCapacity((int) length.longValue()); // 递归 for (int i = 0; i < length; i++) { result.add(DefaultRespCodec.X.decode(buffer)); } return result; }}复制代码测试一下:
public static void main(String[] args) throws Exception { ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(); //*2rn$3rnfoorn$3rnbarrn buffer = ByteBufAllocator.DEFAULT.buffer(); buffer.writeBytes("*2".getBytes(RespConstants.UTF_8)); buffer.writeBytes(RespConstants.CRLF); buffer.writeBytes("$3".getBytes(RespConstants.UTF_8)); buffer.writeBytes(RespConstants.CRLF); buffer.writeBytes("foo".getBytes(RespConstants.UTF_8)); buffer.writeBytes(RespConstants.CRLF); buffer.writeBytes("$3".getBytes(RespConstants.UTF_8)); buffer.writeBytes(RespConstants.CRLF); buffer.writeBytes("bar".getBytes(RespConstants.UTF_8)); buffer.writeBytes(RespConstants.CRLF); List value = https://www.isolves.com/it/sjk/Redis/2019-10-15/RespCodec.X.decode(buffer); log.info("Decode result:{}", value);}// Decode result:[foo, bar]复制代码小结对RESP的内容和其编码解码的过程有相对深刻的认识后,就可以基于Netty编写Redis服务的编码解码模块,作为Netty入门的十分有意义的例子 。本文的最后一节只演示了RESP的解码部分,编码模块和更多细节会在另一篇用Netty实现Redis客户端的文章中展示 。
参考资料:
- Redis Protocol specification
希望你能读到这里,然后发现我:
- Github Page:www.throwable.club/2019/10/09/…
- Coding Page:throwable.coding.me/2019/10/09/…
本文涉及的所有代码:
public class RespConstants { public static final Charset ASCII = StandardCharsets.US_ASCII; public static final Charset UTF_8 = StandardCharsets.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 static final String EMPTY_STRING = ""; public static final Long ZERO = 0L; public static final Long NEGATIVE_ONE = -1L; public static final byte CR = (byte) 'r'; public static final byte LF = (byte) 'n'; public static final byte[] CRLF = "rn".getBytes(ASCII); public enum ReplyType { SIMPLE_STRING, ERROR, INTEGER, BULK_STRING, RESP_ARRAY }}public enum CodecUtils { X; public int findLineEndIndex(ByteBuf buffer) { int index = buffer.forEachByte(ByteProcessor.FIND_LF); return (index > 0 && buffer.getByte(index - 1) == 'r') ? index : -1; } public String readLine(ByteBuf buffer) { int lineEndIndex = findLineEndIndex(buffer); if (lineEndIndex > -1) { int lineStartIndex = buffer.readerIndex(); // 计算字节长度 int size = lineEndIndex - lineStartIndex - 1; byte[] bytes = new byte[size]; buffer.readBytes(bytes); // 重置读游标为rn之后的第一个字节 buffer.readerIndex(lineEndIndex + 1); buffer.markReaderIndex(); return new String(bytes, RespConstants.UTF_8); } return null; }}public interface RespCodec { RespCodec X = DefaultRespCodec.X; <IN, OUT> OUT decode(ByteBuf buffer); <IN, OUT> ByteBuf encode(IN in);}public enum DefaultRespCodec implements RespCodec { X; static final Map<ReplyType, RespDecoder> DECODERS = Maps.newConcurrentMap(); private static final RespDecoder DEFAULT_DECODER = new DefaultRespDecoder(); static { DECODERS.put(ReplyType.SIMPLE_STRING, new RespSimpleStringDecoder()); DECODERS.put(ReplyType.ERROR, new RespErrorDecoder()); DECODERS.put(ReplyType.INTEGER, new RespIntegerDecoder()); DECODERS.put(ReplyType.BULK_STRING, new RespBulkStringDecoder()); DECODERS.put(ReplyType.RESP_ARRAY, new RespArrayDecoder()); } @SuppressWarnings("unchecked") @Override public <IN, OUT> OUT decode(ByteBuf buffer) { return (OUT) DECODERS.getOrDefault(determineReplyType(buffer), DEFAULT_DECODER).decode(buffer); } private ReplyType determineReplyType(ByteBuf buffer) { byte firstByte = buffer.readByte(); ReplyType replyType; switch (firstByte) { case RespConstants.PLUS_BYTE: replyType = ReplyType.SIMPLE_STRING; break; case RespConstants.MINUS_BYTE: replyType = ReplyType.ERROR; break; case RespConstants.COLON_BYTE: replyType = ReplyType.INTEGER; break; case RespConstants.DOLLAR_BYTE: replyType = ReplyType.BULK_STRING; break; case RespConstants.ASTERISK_BYTE: replyType = ReplyType.RESP_ARRAY; break; default: { throw new IllegalArgumentException("first byte:" + firstByte); } } return replyType; } @Override public <IN, OUT> ByteBuf encode(IN in) { // TODO throw new UnsupportedOperationException("encode"); }}public interface RespDecoder<V> { V decode(ByteBuf buffer);}public class DefaultRespDecoder implements RespDecoder { @Override public Object decode(ByteBuf buffer) { throw new IllegalStateException("decoder"); }}public class LineStringDecoder implements RespDecoder<String> { @Override public String decode(ByteBuf buffer) { return CodecUtils.X.readLine(buffer); }}public class RespSimpleStringDecoder extends LineStringDecoder {}public class RespErrorDecoder extends LineStringDecoder {}public class RespIntegerDecoder implements RespDecoder<Long> { @Override public Long decode(ByteBuf buffer) { int lineEndIndex = CodecUtils.X.findLineEndIndex(buffer); // 没有行尾,异常 if (-1 == lineEndIndex) { return null; } long result = 0L; int lineStartIndex = buffer.readerIndex(); boolean negative = false; byte firstByte = buffer.getByte(lineStartIndex); // 负数 if (RespConstants.MINUS_BYTE == firstByte) { negative = true; } else { int digit = firstByte - '0'; result = result * 10 + digit; } for (int i = lineStartIndex + 1; i < (lineEndIndex - 1); i++) { byte value = https://www.isolves.com/it/sjk/Redis/2019-10-15/buffer.getByte(i); int digit = value - '0'; result = result * 10 + digit; } if (negative) { result = -result; } // 重置读游标为rn之后的第一个字节 buffer.readerIndex(lineEndIndex + 1); return result; }}public class RespBulkStringDecoder implements RespDecoder
推荐阅读
- 番荔枝释迦有什么区别
- 看完这些“秋天的第一杯奶茶”,手里的蜜雪冰城忽然就不香了!
- 茶就是幸福
- 蓝翔技校校长跨省打架 蓝翔技校跨省打架
- 汽车加油不是加满就是加200?学习起来,别再加错了
- 大盘、消费、医药、半导体科技板块全线大跌,有闲钱就加点仓吧!
- 阳气不到的地方就会生病
- 5种日用品该扔就扔,当心毁了健康!
- 一什么相声? 相声就是要逗乐能把观众逗乐的就是好的相声
- 选择安溪读茶校 升学就业两不愁