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<>();
do {
K key = readKey(input);
int len = ByteBufUtils.readInt(input, valueSize());
Object value = readValue(key, input.readSlice(len));
map.put(key, (V) value);
int length = ByteBufUtils.readInt(input, valueSize());
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());
return map;
}

View File

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