1.优化String类型写入性能;2.提升MapConverter容错性

master
剑器近 2021-07-23 09:29:41 +08:00
parent 15943a2e55
commit 71be789c62
2 changed files with 19 additions and 10 deletions

View File

@ -27,9 +27,16 @@ public abstract class MapConverter<K, V> extends PrepareLoadStrategy implements
Map<K, V> map = new TreeMap<>(); Map<K, V> map = new TreeMap<>();
do { do {
K key = readKey(input); K key = readKey(input);
int len = ByteBufUtils.readInt(input, valueSize()); int length = ByteBufUtils.readInt(input, valueSize());
Object value = readValue(key, input.readSlice(len));
map.put(key, (V) value); if (input.isReadable(length)) {
int writerIndex = input.writerIndex();
input.writerIndex(input.readerIndex() + length);
map.put(key, (V) readValue(key, input));
input.writerIndex(writerIndex);
} else {
map.put(key, (V) readValue(key, input));
}
} while (input.isReadable()); } while (input.isReadable());
return map; return map;
} }

View File

@ -8,6 +8,7 @@ import io.netty.util.internal.StringUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
@ -58,24 +59,25 @@ public class StringSchema {
@Override @Override
public void writeTo(ByteBuf output, int length, String value) { public void writeTo(ByteBuf output, int length, String value) {
byte[] bytes = value.getBytes(charset); ByteBuffer buffer = charset.encode(value);
if (length > 0) { if (length > 0) {
int srcPos = length - bytes.length; int srcPos = length - buffer.limit();
if (srcPos > 0) { if (srcPos > 0) {
byte[] pads = new byte[srcPos]; byte[] pads = new byte[srcPos];
if (pad != 0x00) if (pad != 0x00)
Arrays.fill(pads, pad); Arrays.fill(pads, pad);
output.writeBytes(pads); output.writeBytes(pads);
output.writeBytes(bytes); output.writeBytes(buffer);
} else if (srcPos < 0) { } else if (srcPos < 0) {
output.writeBytes(bytes, -srcPos, length); buffer.position(-srcPos);
log.info("字符长度超出限制: 长度[{}],数据长度[{}],{}", length, bytes.length, value); output.writeBytes(buffer);
log.info("字符长度超出限制: 长度[{}],数据长度[{}],{}", length, buffer.limit(), value);
} else { } else {
output.writeBytes(bytes); output.writeBytes(buffer);
} }
} else { } else {
output.writeBytes(bytes); output.writeBytes(buffer);
} }
} }
} }