This commit is contained in:
redkale
2024-09-29 12:12:21 +08:00
parent b736cbafc6
commit f42ff13824
12 changed files with 129 additions and 106 deletions

View File

@@ -17,6 +17,7 @@ import java.util.concurrent.locks.*;
* @author zhangjx
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
* @param <T> T
*/
public class OptionalCoder<R extends Reader, W extends Writer, T> extends SimpledCoder<R, W, Optional<T>> {
@@ -109,4 +110,9 @@ public class OptionalCoder<R extends Reader, W extends Writer, T> extends Simple
}
return Optional.ofNullable(this.decoder.convertFrom(in));
}
@Override
public Class getType() {
return Optional.class;
}
}

View File

@@ -5,6 +5,8 @@
*/
package org.redkale.convert;
import org.redkale.annotation.Nullable;
/**
* 反序列化的数据读取流
*
@@ -93,7 +95,7 @@ public abstract class Reader {
* @param componentDecoder Decodeable
* @return 返回数组的长度
*/
public abstract int readArrayB(Decodeable componentDecoder);
public abstract int readArrayB(@Nullable Decodeable componentDecoder);
/** 读取数组的尾端 */
public abstract void readArrayE();

View File

@@ -36,6 +36,9 @@ public abstract class SimpledCoder<R extends Reader, W extends Writer, T>
if (type == null) {
Type[] ts = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
type = ts[ts.length - 1];
if (!(type instanceof Class)) {
throw new ConvertException(type + " is not class");
}
}
return (Class<T>) type;
}

View File

@@ -28,12 +28,11 @@ public class BsonArrayDecoder<T> extends ArrayDecoder<BsonReader, T> {
@Override
public T[] convertFrom(BsonReader in) {
this.checkInited();
byte[] typevals = new byte[1];
int len = in.readArrayB(typevals, this.componentDecoder);
int len = in.readArrayB(this.componentDecoder);
if (len == Reader.SIGN_NULL) {
return null;
}
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
final List<T> result = new ArrayList();
// 固定长度
for (int i = 0; i < len; i++) {

View File

@@ -5,12 +5,9 @@
*/
package org.redkale.convert.bson;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.redkale.convert.*;
import static org.redkale.convert.Reader.SIGN_NULL;
import org.redkale.convert.ext.ByteSimpledCoder;
/**
* 以ByteBuffer为数据载体的BsonReader
@@ -48,44 +45,6 @@ public class BsonByteBufferReader extends BsonReader {
return currentBuffer.get(currentBuffer.position());
}
@Override
public int readMapB(byte[] typevals, Decodeable keyDecoder, Decodeable valueDecoder) {
short bt = readShort();
if (bt == Reader.SIGN_NULL) {
return bt;
}
short lt = readShort();
byte kt = readByte();
byte vt = readByte();
if (typevals != null) {
typevals[0] = kt;
typevals[1] = vt;
}
return (bt & 0xffff) << 16 | (lt & 0xffff);
}
/**
* 判断下一个非空白字节是否为[
*
* @param typevals byte[]
* @param componentDecoder Decodeable
* @return 数组长度或 SIGN_NULL
*/
@Override
public final int readArrayB(byte[] typevals, Decodeable componentDecoder) {
short bt = readShort();
if (bt == Reader.SIGN_NULL) {
return bt;
}
short lt = readShort();
if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) {
byte comval = readByte();
if (typevals != null) {
typevals[0] = comval;
}
}
return (bt & 0xffff) << 16 | (lt & 0xffff);
}
// ------------------------------------------------------------
@Override

View File

@@ -27,12 +27,11 @@ public class BsonCollectionDecoder<T> extends CollectionDecoder<BsonReader, T> {
@Override
public Collection<T> convertFrom(BsonReader in) {
this.checkInited();
byte[] typevals = new byte[1];
int len = in.readArrayB(typevals, componentDecoder);
int len = in.readArrayB(componentDecoder);
if (len == Reader.SIGN_NULL) {
return null;
}
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
final Collection<T> result = this.creator.create();
// 固定长度
for (int i = 0; i < len; i++) {

View File

@@ -10,6 +10,7 @@ import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.function.*;
import org.redkale.annotation.Nullable;
import org.redkale.convert.*;
import org.redkale.util.*;
@@ -48,10 +49,16 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
private final ThreadLocal<BsonWriter> writerPool = Utility.withInitialThreadLocal(BsonWriter::new);
private final Consumer<BsonWriter> writerConsumer = w -> offerWriter(w);
private final Consumer<BsonWriter> writerConsumer = this::offerWriter;
private final ThreadLocal<BsonReader> readerPool = Utility.withInitialThreadLocal(BsonReader::new);
@Nullable
private Encodeable lastEncodeable;
@Nullable
private Decodeable lastDecodeable;
protected BsonConvert(ConvertFactory<BsonReader, BsonWriter> factory, int features) {
super(factory, features);
}
@@ -166,8 +173,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
return null;
}
final BsonReader in = new BsonReader(bytes, offset, len);
@SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(in);
Decodeable decoder = this.lastDecodeable;
if (decoder == null || decoder.getType() != type) {
decoder = factory.loadDecoder(type);
this.lastDecodeable = decoder;
}
T rs = (T) decoder.convertFrom(in);
return rs;
}
@@ -185,7 +196,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
if (type == null || Utility.isEmpty(buffers)) {
return null;
}
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers));
Decodeable decoder = this.lastDecodeable;
if (decoder == null || decoder.getType() != type) {
decoder = factory.loadDecoder(type);
this.lastDecodeable = decoder;
}
return (T) decoder.convertFrom(new BsonByteBufferReader(buffers));
}
@Override
@@ -194,8 +210,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
if (type == null) {
return null;
}
@SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(reader);
Decodeable decoder = this.lastDecodeable;
if (decoder == null || decoder.getType() != type) {
decoder = factory.loadDecoder(type);
this.lastDecodeable = decoder;
}
T rs = (T) decoder.convertFrom(reader);
return rs;
}
@@ -209,8 +229,14 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
offerWriter(out);
return result;
}
final Type t = type == null ? value.getClass() : type;
Encodeable encoder = this.lastEncodeable;
if (encoder == null || encoder.getType() != t) {
encoder = factory.loadEncoder(t);
this.lastEncodeable = encoder;
}
final BsonWriter writer = pollWriter();
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
encoder.convertTo(writer, value);
byte[] result = writer.toArray();
offerWriter(writer);
return result;
@@ -227,7 +253,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
if (type == null && value == null) {
writer.writeNull();
} else {
factory.loadEncoder(type).convertTo(writer, value);
Encodeable encoder = this.lastEncodeable;
if (encoder == null || encoder.getType() != type) {
encoder = factory.loadEncoder(type);
this.lastEncodeable = encoder;
}
encoder.convertTo(writer, value);
}
writer.completed(handler, writerConsumer);
}
@@ -239,6 +270,11 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
if (type == null && value == null) {
writer.writeNull();
} else {
Encodeable encoder = this.lastEncodeable;
if (encoder == null || encoder.getType() != type) {
encoder = factory.loadEncoder(type);
this.lastEncodeable = encoder;
}
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
}
writer.directTo(array);
@@ -273,7 +309,13 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
if (type == null && value == null) { // 必须判断type==null
writer.writeNull();
} else {
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
final Type t = type == null ? value.getClass() : type;
Encodeable encoder = this.lastEncodeable;
if (encoder == null || encoder.getType() != t) {
encoder = factory.loadEncoder(t);
this.lastEncodeable = encoder;
}
encoder.convertTo(writer, value);
}
}
@@ -282,7 +324,13 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
return null;
}
final BsonWriter writer = writerPool.get().withFeatures(features);
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
final Type t = type == null ? value.getClass() : type;
Encodeable encoder = this.lastEncodeable;
if (encoder == null || encoder.getType() != t) {
encoder = factory.loadEncoder(t);
this.lastEncodeable = encoder;
}
encoder.convertTo(writer, value);
return writer;
}
}

View File

@@ -27,13 +27,12 @@ public class BsonMapDecoder<K, V> extends MapDecoder<BsonReader, K, V> {
@Override
public Map<K, V> convertFrom(BsonReader in) {
this.checkInited();
byte[] typevals = new byte[2];
int len = in.readMapB(typevals, this.keyDecoder, this.valueDecoder);
int len = in.readMapB(this.keyDecoder, this.valueDecoder);
if (len == Reader.SIGN_NULL) {
return null;
}
Decodeable<BsonReader, K> kdecoder = BsonFactory.typeEnum(typevals[0]);
Decodeable<BsonReader, V> vdecoder = BsonFactory.typeEnum(typevals[1]);
Decodeable<BsonReader, K> kdecoder = BsonFactory.typeEnum(in.readMapKeyTypeEnum());
Decodeable<BsonReader, V> vdecoder = BsonFactory.typeEnum(in.readmapValueTypeEnum());
final Map<K, V> result = this.creator.create();
// 固定长度
for (int i = 0; i < len; i++) {

View File

@@ -6,6 +6,7 @@
package org.redkale.convert.bson;
import java.nio.charset.StandardCharsets;
import org.redkale.annotation.Nullable;
import org.redkale.convert.*;
import static org.redkale.convert.Reader.SIGN_NULL;
import org.redkale.convert.ext.ByteSimpledCoder;
@@ -32,7 +33,13 @@ public class BsonReader extends Reader {
public static final byte VERBOSE_YES = 2;
protected byte typeval; // 字段的类型值 对应 BsonWriter.writeField
protected byte fieldTypeEnum; // 字段的类型值 对应 BsonWriter.writeField
protected byte arrayItemTypeEnum;
protected byte mapKeyTypeEnum;
protected byte mapValueTypeEnum;
protected int position = -1;
@@ -40,10 +47,6 @@ public class BsonReader extends Reader {
public BsonReader() {}
public static ObjectPool<BsonReader> createPool(int max) {
return ObjectPool.createSafePool(max, (Object... params) -> new BsonReader(), null, (t) -> t.recycle());
}
public BsonReader(byte[] bytes) {
setBytes(bytes, 0, bytes.length);
}
@@ -79,7 +82,10 @@ public class BsonReader extends Reader {
protected boolean recycle() {
this.position = -1;
this.typeval = 0;
this.fieldTypeEnum = 0;
this.arrayItemTypeEnum = 0;
this.mapKeyTypeEnum = 0;
this.mapValueTypeEnum = 0;
// this.limit = -1;
this.content = null;
return true;
@@ -94,11 +100,11 @@ public class BsonReader extends Reader {
@Override
@SuppressWarnings("unchecked")
public final void skipValue() {
if (typeval == 0) {
final byte val = this.fieldTypeEnum;
if (val == 0) {
return;
}
final byte val = this.typeval;
this.typeval = 0;
this.fieldTypeEnum = 0;
switch (val) {
case 11:
readBoolean();
@@ -165,54 +171,57 @@ public class BsonReader extends Reader {
return this.content[this.position];
}
@Override
public int readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
return readMapB(null, keyDecoder, valueDecoder);
public final byte readMapKeyTypeEnum() {
return mapKeyTypeEnum;
}
public int readMapB(byte[] typevals, Decodeable keyDecoder, Decodeable valueDecoder) {
public final byte readmapValueTypeEnum() {
return mapValueTypeEnum;
}
@Override
public final int readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
short bt = readShort();
if (bt == Reader.SIGN_NULL) {
this.mapKeyTypeEnum = 0;
this.mapValueTypeEnum = 0;
return bt;
}
int rs = (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff);
byte kt = readByte();
byte vt = readByte();
if (typevals != null) {
typevals[0] = kt;
typevals[1] = vt;
}
return rs;
short lt = readShort();
this.mapKeyTypeEnum = readByte();
this.mapValueTypeEnum = readByte();
return (bt & 0xffff) << 16 | (lt & 0xffff);
}
@Override
public final void readMapE() {
// do nothing
this.mapKeyTypeEnum = 0;
this.mapValueTypeEnum = 0;
}
public final byte readArrayItemTypeEnum() {
return arrayItemTypeEnum;
}
@Override
public int readArrayB(Decodeable componentDecoder) {
return readArrayB(null, componentDecoder);
}
public int readArrayB(byte[] typevals, Decodeable componentDecoder) { // componentDecoder可能为null
public final int readArrayB(@Nullable Decodeable componentDecoder) {
short bt = readShort();
if (bt == Reader.SIGN_NULL) {
this.arrayItemTypeEnum = 0;
return bt;
}
int rs = (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff);
short lt = readShort();
if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) {
byte comval = readByte();
if (typevals != null) {
typevals[0] = comval;
}
this.arrayItemTypeEnum = readByte();
} else {
this.arrayItemTypeEnum = 0;
}
return rs;
return (bt & 0xffff) << 16 | (lt & 0xffff);
}
@Override
public final void readArrayE() {
// do nothing
this.arrayItemTypeEnum = 0;
}
/** 判断下一个非空白字节是否: */
@@ -232,7 +241,7 @@ public class BsonReader extends Reader {
* @return 是否存在
*/
@Override
public boolean hasNext() {
public final boolean hasNext() {
byte b = readByte();
if (b == SIGN_HASNEXT) {
return true;
@@ -247,7 +256,7 @@ public class BsonReader extends Reader {
@Override
public final DeMember readFieldName(final DeMemberInfo memberInfo) {
final String exceptedField = readSmallString();
this.typeval = readByte();
this.fieldTypeEnum = readByte();
return memberInfo.getMemberByField(exceptedField);
}
@@ -264,7 +273,8 @@ public class BsonReader extends Reader {
@Override
public final byte[] readByteArray() {
int len = readArrayB(null, null);
int len = readArrayB(null);
this.arrayItemTypeEnum = 0;
if (len == Reader.SIGN_NULL) {
return null;
}

View File

@@ -28,12 +28,11 @@ public class BsonStreamDecoder<T> extends StreamDecoder<BsonReader, T> {
@Override
public Stream<T> convertFrom(BsonReader in) {
this.checkInited();
byte[] typevals = new byte[1];
int len = in.readArrayB(typevals, componentDecoder);
int len = in.readArrayB(componentDecoder);
if (len == Reader.SIGN_NULL) {
return null;
}
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
final List<T> result = new ArrayList();
// 固定长度
for (int i = 0; i < len; i++) {

View File

@@ -27,10 +27,6 @@ public class BsonWriter extends Writer implements ByteTuple {
protected int count;
public static ObjectPool<BsonWriter> createPool(int max) {
return ObjectPool.createSafePool(max, (Object... params) -> new BsonWriter(), null, (t) -> t.recycle());
}
@Override
public byte[] content() {
return content;

View File

@@ -11,6 +11,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.function.*;
import org.redkale.annotation.Nullable;
import org.redkale.convert.*;
import org.redkale.service.RetResult;
import org.redkale.util.*;
@@ -37,8 +38,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
@Nullable
private Encodeable lastEncodeable;
@Nullable
private Decodeable lastDecodeable;
protected JsonConvert(JsonFactory factory, int features) {