diff --git a/src/org/redkale/convert/bson/BsonByteBufferReader.java b/src/org/redkale/convert/bson/BsonByteBufferReader.java index 06600b90f..dea6b38c8 100644 --- a/src/org/redkale/convert/bson/BsonByteBufferReader.java +++ b/src/org/redkale/convert/bson/BsonByteBufferReader.java @@ -8,6 +8,7 @@ package org.redkale.convert.bson; import java.nio.*; import org.redkale.convert.*; import static org.redkale.convert.Reader.SIGN_NULL; +import org.redkale.convert.ext.ByteSimpledCoder; import org.redkale.util.*; /** @@ -53,6 +54,12 @@ public class BsonByteBufferReader extends BsonReader { 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); } @@ -70,6 +77,10 @@ public class BsonByteBufferReader extends BsonReader { 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); } //------------------------------------------------------------ diff --git a/src/org/redkale/convert/bson/BsonFactory.java b/src/org/redkale/convert/bson/BsonFactory.java index aac55a12c..9c9da8a3a 100644 --- a/src/org/redkale/convert/bson/BsonFactory.java +++ b/src/org/redkale/convert/bson/BsonFactory.java @@ -6,6 +6,7 @@ package org.redkale.convert.bson; import java.io.Serializable; +import java.lang.reflect.Type; import java.util.*; import java.util.stream.Stream; import org.redkale.convert.*; @@ -29,65 +30,15 @@ public final class BsonFactory extends ConvertFactory { static final Encodeable objectEncoder = instance.loadEncoder(Object.class); - static final Decodeable collectionBooleanDecoder = instance.loadDecoder(new TypeToken>() { + static final Decodeable skipArrayDecoder = new SkipArrayDecoder(instance, Object[].class); + + static final Decodeable skipCollectionDecoder = new SkipCollectionDecoder(instance, new TypeToken>() { }.getType()); - static final Decodeable collectionByteDecoder = instance.loadDecoder(new TypeToken>() { + static final Decodeable skipStreamDecoder = new SkipStreamDecoder(instance, new TypeToken>() { }.getType()); - static final Decodeable collectionShortDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionCharacterDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionIntegerDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionLongDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionFloatDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionDoubleDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionStringDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable collectionObjectDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringBooleanDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringByteDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringShortDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringCharacterDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringIntegerDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringLongDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringFloatDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringDoubleDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringStringDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); - - static final Decodeable mapStringObjectDecoder = instance.loadDecoder(new TypeToken>() { - }.getType()); + static final Decodeable skipMapDecoder = new SkipMapDecoder(instance, Map.class); static { instance.register(Serializable.class, objectDecoder); @@ -152,6 +103,10 @@ public final class BsonFactory extends ConvertFactory { return true; } + protected static byte typeEnum(final Type type) { + return typeEnum(TypeToken.typeToClass(type)); + } + protected static byte typeEnum(final Class type) { Objects.requireNonNull(type); byte typeval = 127; //字段的类型值 @@ -241,8 +196,17 @@ public final class BsonFactory extends ConvertFactory { return DoubleArraySimpledCoder.instance; case 29: return StringArraySimpledCoder.instance; - default: + case 81: + return skipArrayDecoder; + case 82: + return skipCollectionDecoder; + case 83: + return skipStreamDecoder; + case 84: + return skipMapDecoder; + case 127: return BsonFactory.objectDecoder; } + return null; } } diff --git a/src/org/redkale/convert/bson/BsonReader.java b/src/org/redkale/convert/bson/BsonReader.java index 06897eaae..fd7649e78 100644 --- a/src/org/redkale/convert/bson/BsonReader.java +++ b/src/org/redkale/convert/bson/BsonReader.java @@ -93,113 +93,27 @@ public class BsonReader extends Reader { final byte val = this.typeval; this.typeval = 0; switch (val) { - case 1: readBoolean(); + case 11: readBoolean(); break; - case 2: readByte(); + case 12: readByte(); break; - case 3: readShort(); + case 13: readShort(); break; - case 4: readChar(); + case 14: readChar(); break; - case 5: readInt(); + case 15: readInt(); break; - case 6: readLong(); + case 16: readLong(); break; - case 7: readFloat(); + case 17: readFloat(); break; - case 8: readDouble(); + case 18: readDouble(); break; - case 9: readString(); + case 19: readString(); break; - case 20: - BsonFactory.collectionObjectDecoder.convertFrom(this); - break; - case 21: - BsonFactory.collectionBooleanDecoder.convertFrom(this); - break; - case 22: - BsonFactory.collectionByteDecoder.convertFrom(this); - break; - case 23: - BsonFactory.collectionShortDecoder.convertFrom(this); - break; - case 24: - BsonFactory.collectionCharacterDecoder.convertFrom(this); - break; - case 25: - BsonFactory.collectionIntegerDecoder.convertFrom(this); - break; - case 26: - BsonFactory.collectionLongDecoder.convertFrom(this); - break; - case 27: - BsonFactory.collectionFloatDecoder.convertFrom(this); - break; - case 28: - BsonFactory.collectionDoubleDecoder.convertFrom(this); - break; - case 29: - BsonFactory.collectionStringDecoder.convertFrom(this); - break; - case 40: - BsonFactory.mapStringObjectDecoder.convertFrom(this); - break; - case 41: - BsonFactory.mapStringBooleanDecoder.convertFrom(this); - break; - case 42: - BsonFactory.mapStringByteDecoder.convertFrom(this); - break; - case 43: - BsonFactory.mapStringShortDecoder.convertFrom(this); - break; - case 44: - BsonFactory.mapStringCharacterDecoder.convertFrom(this); - break; - case 45: - BsonFactory.mapStringIntegerDecoder.convertFrom(this); - break; - case 46: - BsonFactory.mapStringLongDecoder.convertFrom(this); - break; - case 47: - BsonFactory.mapStringFloatDecoder.convertFrom(this); - break; - case 48: - BsonFactory.mapStringDoubleDecoder.convertFrom(this); - break; - case 49: - BsonFactory.mapStringStringDecoder.convertFrom(this); - break; - case 101: - BoolArraySimpledCoder.instance.convertFrom(this); - break; - case 102: - ByteArraySimpledCoder.instance.convertFrom(this); - break; - case 103: - ShortArraySimpledCoder.instance.convertFrom(this); - break; - case 104: - CharArraySimpledCoder.instance.convertFrom(this); - break; - case 105: - IntArraySimpledCoder.instance.convertFrom(this); - break; - case 106: - LongArraySimpledCoder.instance.convertFrom(this); - break; - case 107: - FloatArraySimpledCoder.instance.convertFrom(this); - break; - case 108: - DoubleArraySimpledCoder.instance.convertFrom(this); - break; - case 109: - StringArraySimpledCoder.instance.convertFrom(this); - break; - case 127: - BsonFactory.objectDecoder.convertFrom(this); + default: + Decodeable decoder = BsonFactory.typeEnum(val); + if (decoder != null) decoder.convertFrom(this); break; } } @@ -234,7 +148,14 @@ public class BsonReader extends Reader { public int readMapB(DeMember member, byte[] typevals, Decodeable keyDecoder, Decodeable valueDecoder) { short bt = readShort(); if (bt == Reader.SIGN_NULL) return bt; - return (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff); + 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; } @Override @@ -251,7 +172,10 @@ public class BsonReader extends Reader { short bt = readShort(); if (bt == Reader.SIGN_NULL) return bt; int rs = (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff); - if (componentDecoder == null || componentDecoder == ByteSimpledCoder.instance) return rs; //byte[] + if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) { + byte comval = readByte(); + if (typevals != null) typevals[0] = comval; + } return rs; } diff --git a/src/org/redkale/convert/bson/BsonWriter.java b/src/org/redkale/convert/bson/BsonWriter.java index 660b02e41..029dd5f21 100644 --- a/src/org/redkale/convert/bson/BsonWriter.java +++ b/src/org/redkale/convert/bson/BsonWriter.java @@ -7,6 +7,7 @@ package org.redkale.convert.bson; import java.nio.ByteBuffer; import org.redkale.convert.*; +import org.redkale.convert.ext.ByteSimpledCoder; import org.redkale.util.*; /** @@ -201,98 +202,7 @@ public class BsonWriter extends Writer { Attribute attribute = member.getAttribute(); writeByte(BsonReader.SIGN_HASNEXT); writeSmallString(attribute.field()); - byte typeval = 127; //字段的类型值 - final Class type = attribute.type(); - if (type == boolean.class || type == Boolean.class) { - typeval = 1; - } else if (type == byte.class || type == Byte.class) { - typeval = 2; - } else if (type == short.class || type == Short.class) { - typeval = 3; - } else if (type == char.class || type == Character.class) { - typeval = 4; - } else if (type == int.class || type == Integer.class) { - typeval = 5; - } else if (type == long.class || type == Long.class) { - typeval = 6; - } else if (type == float.class || type == Float.class) { - typeval = 7; - } else if (type == double.class || type == Double.class) { - typeval = 8; - } else if (type == String.class) { - typeval = 9; - } else if (type == boolean[].class || type == Boolean[].class) { - typeval = 101; - } else if (type == byte[].class || type == Byte[].class) { - typeval = 102; - } else if (type == short[].class || type == Short[].class) { - typeval = 103; - } else if (type == char[].class || type == Character[].class) { - typeval = 104; - } else if (type == int[].class || type == Integer[].class) { - typeval = 105; - } else if (type == long[].class || type == Long[].class) { - typeval = 106; - } else if (type == float[].class || type == Float[].class) { - typeval = 107; - } else if (type == double[].class || type == Double[].class) { - typeval = 108; - } else if (type == String[].class) { - typeval = 109; - } - if (typeval == 127 && member.getEncoder() instanceof CollectionEncoder) { - java.lang.reflect.Type comType = ((CollectionEncoder) member.getEncoder()).getComponentEncoder().getType(); - if (comType == Boolean.class) { - typeval = 21; - } else if (comType == Byte.class) { - typeval = 22; - } else if (comType == Short.class) { - typeval = 23; - } else if (comType == Character.class) { - typeval = 24; - } else if (comType == Integer.class) { - typeval = 25; - } else if (comType == Long.class) { - typeval = 26; - } else if (comType == Float.class) { - typeval = 27; - } else if (comType == Double.class) { - typeval = 28; - } else if (comType == String.class) { - typeval = 29; - } else { - typeval = 20; - } - } - if (typeval == 127 && member.getEncoder() instanceof ArrayEncoder) { - typeval = 20; - } - if (typeval == 127 && member.getEncoder() instanceof MapEncoder) { - java.lang.reflect.Type keyType = ((MapEncoder) member.getEncoder()).getKeyEncoder().getType(); - java.lang.reflect.Type valType = ((MapEncoder) member.getEncoder()).getValueEncoder().getType(); - if (keyType == String.class && valType == Boolean.class) { - typeval = 41; - } else if (keyType == String.class && valType == Byte.class) { - typeval = 42; - } else if (keyType == String.class && valType == Short.class) { - typeval = 43; - } else if (keyType == String.class && valType == Character.class) { - typeval = 44; - } else if (keyType == String.class && valType == Integer.class) { - typeval = 45; - } else if (keyType == String.class && valType == Long.class) { - typeval = 46; - } else if (keyType == String.class && valType == Float.class) { - typeval = 47; - } else if (keyType == String.class && valType == Double.class) { - typeval = 48; - } else if (keyType == String.class && valType == String.class) { - typeval = 49; - } else if (keyType == String.class) { - typeval = 40; - } - } - writeByte(typeval); + writeByte(BsonFactory.typeEnum(attribute.type())); } /** @@ -339,6 +249,9 @@ public class BsonWriter extends Writer { @Override public final int writeArrayB(int size, Encodeable componentEncoder, Object obj) { writeInt(size); + if (componentEncoder != null && componentEncoder != ByteSimpledCoder.instance) { + writeByte(BsonFactory.typeEnum(componentEncoder.getType())); + } return -1; } @@ -353,6 +266,8 @@ public class BsonWriter extends Writer { @Override public int writeMapB(int size, Encodeable keyEncoder, Encodeable valueEncoder, Object obj) { writeInt(size); + writeByte(BsonFactory.typeEnum(keyEncoder.getType())); + writeByte(BsonFactory.typeEnum(valueEncoder.getType())); return -1; } diff --git a/src/org/redkale/convert/bson/SkipArrayDecoder.java b/src/org/redkale/convert/bson/SkipArrayDecoder.java new file mode 100644 index 000000000..5efe46d80 --- /dev/null +++ b/src/org/redkale/convert/bson/SkipArrayDecoder.java @@ -0,0 +1,33 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert.bson; + +import java.lang.reflect.Type; +import org.redkale.convert.*; + +/** + * 数组的反序列化操作类
+ * 对象数组的反序列化,不包含int[]、long[]这样的primitive class数组。
+ * 支持一定程度的泛型。
+ * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @param 反解析的数组元素类型 + */ +public class SkipArrayDecoder extends ArrayDecoder { + + public SkipArrayDecoder(final ConvertFactory factory, final Type type) { + super(factory, type); + } + + @Override + protected Decodeable getComponentDecoder(Decodeable decoder, byte[] typevals) { + if (typevals != null) return BsonFactory.typeEnum(typevals[0]); + return decoder; + } +} diff --git a/src/org/redkale/convert/bson/SkipCollectionDecoder.java b/src/org/redkale/convert/bson/SkipCollectionDecoder.java new file mode 100644 index 000000000..0b74fe110 --- /dev/null +++ b/src/org/redkale/convert/bson/SkipCollectionDecoder.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert.bson; + +import java.lang.reflect.Type; +import org.redkale.convert.*; + +/** + * Collection的反序列化操作类
+ * 支持一定程度的泛型。
+ * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @param 反解析的集合元素类型 + */ +public class SkipCollectionDecoder extends CollectionDecoder { + + public SkipCollectionDecoder(final ConvertFactory factory, final Type type) { + super(factory, type); + } + + @Override + protected Decodeable getComponentDecoder(Decodeable decoder, byte[] typevals) { + if (typevals != null) return BsonFactory.typeEnum(typevals[0]); + return decoder; + } +} diff --git a/src/org/redkale/convert/bson/SkipMapDecoder.java b/src/org/redkale/convert/bson/SkipMapDecoder.java new file mode 100644 index 000000000..a0b352849 --- /dev/null +++ b/src/org/redkale/convert/bson/SkipMapDecoder.java @@ -0,0 +1,38 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert.bson; + +import java.lang.reflect.Type; +import org.redkale.convert.*; + +/** + * Map的反序列化操作类
+ * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @param Map key的数据类型 + * @param Map value的数据类型 + */ +public class SkipMapDecoder extends MapDecoder { + + public SkipMapDecoder(final ConvertFactory factory, final Type type) { + super(factory, type); + } + + @Override + protected Decodeable getKeyDecoder(Decodeable decoder, byte[] typevals) { + if (typevals != null) return BsonFactory.typeEnum(typevals[0]); + return decoder; + } + + @Override + protected Decodeable getValueDecoder(Decodeable decoder, byte[] typevals) { + if (typevals != null) return BsonFactory.typeEnum(typevals[1]); + return decoder; + } +} diff --git a/src/org/redkale/convert/bson/SkipStreamDecoder.java b/src/org/redkale/convert/bson/SkipStreamDecoder.java new file mode 100644 index 000000000..8a42ace7f --- /dev/null +++ b/src/org/redkale/convert/bson/SkipStreamDecoder.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert.bson; + +import java.lang.reflect.Type; +import org.redkale.convert.*; + +/** + * Stream的反序列化操作类
+ * 支持一定程度的泛型。
+ * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @param 反解析的集合元素类型 + */ +public class SkipStreamDecoder extends StreamDecoder { + + public SkipStreamDecoder(final ConvertFactory factory, final Type type) { + super(factory, type); + } + + @Override + protected Decodeable getComponentDecoder(Decodeable decoder, byte[] typevals) { + if (typevals != null) return BsonFactory.typeEnum(typevals[0]); + return decoder; + } +} diff --git a/src/org/redkale/util/Creator.java b/src/org/redkale/util/Creator.java index 7ce6cea05..563ee4c2a 100644 --- a/src/org/redkale/util/Creator.java +++ b/src/org/redkale/util/Creator.java @@ -11,6 +11,7 @@ import java.util.*; import java.util.AbstractMap.SimpleEntry; import java.util.concurrent.*; import java.util.logging.*; +import java.util.stream.Stream; import org.redkale.asm.*; import org.redkale.asm.Type; import static org.redkale.asm.Opcodes.*; @@ -81,6 +82,7 @@ public interface Creator { creatorCacheMap.put(ArrayList.class, (params) -> new ArrayList<>()); creatorCacheMap.put(HashMap.class, (params) -> new HashMap<>()); creatorCacheMap.put(HashSet.class, (params) -> new HashSet<>()); + creatorCacheMap.put(Stream.class, (params) -> new ArrayList<>().stream()); creatorCacheMap.put(ConcurrentHashMap.class, (params) -> new ConcurrentHashMap<>()); creatorCacheMap.put(CompletableFuture.class, (params) -> new CompletableFuture<>()); }