From 8cf8a2c03782be9350e8b8feab52194b58ab07f3 Mon Sep 17 00:00:00 2001 From: redkale Date: Mon, 30 Sep 2024 08:59:17 +0800 Subject: [PATCH] bson --- docs/convert.md | 10 ++--- .../org/redkale/convert/StreamDecoder.java | 4 ++ .../org/redkale/convert/bson/BsonConvert.java | 10 ++--- .../org/redkale/convert/bson/BsonFactory.java | 18 ++++----- .../org/redkale/convert/bson/BsonReader.java | 40 +++++-------------- 5 files changed, 31 insertions(+), 51 deletions(-) diff --git a/docs/convert.md b/docs/convert.md index 369b39170..07b75ea66 100644 --- a/docs/convert.md +++ b/docs/convert.md @@ -450,7 +450,7 @@ public class RestConvertService extends AbstractService {     1). 基本数据类型: 直接转换成byte[] -    2). SmallString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。 +    2). StandardString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。     3). String: length(4 bytes) + byte[](utf8); @@ -458,9 +458,9 @@ public class RestConvertService extends AbstractService {     5). Object: -      1. realclass (SmallString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名) +      1. realclass (StandardString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名) -      2. 空字符串(SmallString) +      2. 空字符串(StandardString)       3. SIGN_OBJECTB 标记位,值固定为0xBB (short) @@ -468,9 +468,9 @@ public class RestConvertService extends AbstractService {         4.1 SIGN_HASNEXT 标记位,值固定为1 (byte) -        4.2 字段类型; 1-9为基本类型&字符串; 101-109为基本类型&字符串的数组; 127为Object +        4.2 字段类型; 11-19为基本类型&字符串; 21-29为基本类型&字符串的数组; 127为Object -        4.3 字段名 (SmallString) +        4.3 字段名 (StandardString)         4.4 字段的值Object diff --git a/src/main/java/org/redkale/convert/StreamDecoder.java b/src/main/java/org/redkale/convert/StreamDecoder.java index 6c7edbdee..a1a61611b 100644 --- a/src/main/java/org/redkale/convert/StreamDecoder.java +++ b/src/main/java/org/redkale/convert/StreamDecoder.java @@ -43,6 +43,10 @@ public class StreamDecoder implements Decodeable * BSON协议格式: * 1) 基本数据类型: 直接转换成byte[] - * 2) SmallString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。 + * 2) StandardString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。 * 3) String: length(4 bytes) + byte[](utf8); * 4) 数组: length(4 bytes) + byte[]... * 5) Object: - * 1、 realclass (SmallString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名) - * 2、 空字符串(SmallString) + * 1、 realclass (StandardString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名) + * 2、 空字符串(StandardString) * 3、 SIGN_OBJECTB 标记位,值固定为0xBB (short) * 4、 循环字段值: * 4.1 SIGN_HASNEXT 标记位,值固定为1 (byte) - * 4.2 字段类型; 1-9为基本类型和字符串; 101-109为基本类型和字符串的数组; 127为Object - * 4.3 字段名 (SmallString) + * 4.2 字段类型; 11-19为基本类型和字符串; 21-29为基本类型和字符串的数组; 127为Object + * 4.3 字段名 (StandardString) * 4.4 字段的值Object * 5、 SIGN_NONEXT 标记位,值固定为0 (byte) * 6、 SIGN_OBJECTE 标记位,值固定为0xEE (short) diff --git a/src/main/java/org/redkale/convert/bson/BsonFactory.java b/src/main/java/org/redkale/convert/bson/BsonFactory.java index b443e9edd..f55e75d7c 100644 --- a/src/main/java/org/redkale/convert/bson/BsonFactory.java +++ b/src/main/java/org/redkale/convert/bson/BsonFactory.java @@ -36,15 +36,13 @@ public final class BsonFactory extends ConvertFactory { static final Encodeable objectEncoder = instance.loadEncoder(Object.class); - static final Decodeable skipArrayDecoder = new BsonArrayDecoder(instance, Object[].class); + static final Decodeable arrayDecoder = new BsonArrayDecoder(instance, Object[].class); - static final Decodeable skipCollectionDecoder = - new BsonCollectionDecoder(instance, new TypeToken>() {}.getType()); + static final Decodeable collectionDecoder = new BsonCollectionDecoder(instance, Collection.class); - static final Decodeable skipStreamDecoder = - new BsonStreamDecoder(instance, new TypeToken>() {}.getType()); + static final Decodeable streamDecoder = new BsonStreamDecoder(instance, Stream.class); - static final Decodeable skipMapDecoder = new BsonMapDecoder(instance, Map.class); + static final Decodeable mapDecoder = new BsonMapDecoder(instance, Map.class); static { instance.register(Serializable.class, objectDecoder); @@ -249,13 +247,13 @@ public final class BsonFactory extends ConvertFactory { case 29: return StringArraySimpledCoder.instance; case 81: - return skipArrayDecoder; + return arrayDecoder; case 82: - return skipCollectionDecoder; + return collectionDecoder; case 83: - return skipStreamDecoder; + return streamDecoder; case 84: - return skipMapDecoder; + return mapDecoder; case 127: return BsonFactory.objectDecoder; default: diff --git a/src/main/java/org/redkale/convert/bson/BsonReader.java b/src/main/java/org/redkale/convert/bson/BsonReader.java index 92d97980d..b5bd4a507 100644 --- a/src/main/java/org/redkale/convert/bson/BsonReader.java +++ b/src/main/java/org/redkale/convert/bson/BsonReader.java @@ -9,7 +9,6 @@ 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; import org.redkale.util.*; /** @@ -211,11 +210,7 @@ public class BsonReader extends Reader { return bt; } short lt = readShort(); - if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) { - this.arrayItemTypeEnum = readByte(); - } else { - this.arrayItemTypeEnum = 0; - } + this.arrayItemTypeEnum = readByte(); return (bt & 0xffff) << 16 | (lt & 0xffff); } @@ -273,34 +268,17 @@ public class BsonReader extends Reader { @Override public final byte[] readByteArray() { - int len = readArrayB(null); - this.arrayItemTypeEnum = 0; - if (len == Reader.SIGN_NULL) { + short bt = readShort(); + if (bt == Reader.SIGN_NULL) { return null; } - if (len == Reader.SIGN_VARIABLE) { - int size = 0; - byte[] data = new byte[8]; - while (hasNext()) { - if (size >= data.length) { - byte[] newdata = new byte[data.length + 4]; - System.arraycopy(data, 0, newdata, 0, size); - data = newdata; - } - data[size++] = readByte(); - } - readArrayE(); - byte[] newdata = new byte[size]; - System.arraycopy(data, 0, newdata, 0, size); - return newdata; - } else { - byte[] values = new byte[len]; - for (int i = 0; i < values.length; i++) { - values[i] = readByte(); - } - readArrayE(); - return values; + short lt = readShort(); + int len = (bt & 0xffff) << 16 | (lt & 0xffff); + byte[] values = new byte[len]; + for (int i = 0; i < values.length; i++) { + values[i] = readByte(); } + return values; } @Override