移除BSON功能
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# 序列化
|
# 序列化
|
||||||
  Convert提供Java对象的序列化与反序列化功能。支持JSON(JavaScript Object Notation)、BSON(Binary Stream Object Notation)、PROTOBUF三种格式化。 三种格式使用方式完全一样,其性能都大幅度超过其他JSON框架。同时JSON内置于HTTP服务中,BSON也是SNCP协议数据序列化的基础。
|
  Convert提供Java对象的序列化与反序列化功能。支持JSON(JavaScript Object Notation)、PROTOBUF两种格式化。 两种格式使用方式完全一样,其性能都大幅度超过其他JSON框架。同时JSON内置于HTTP服务中,PROTOBUF也是SNCP协议数据序列化的基础。
|
||||||
## 基本API
|
## 基本API
|
||||||
  JSON序列化其操作类主要是JsonConvert,配置类主要是JsonFactory、ConvertColumn。JsonFactory采用同ClassLoader类似的双亲委托方式设计。
|
  JSON序列化其操作类主要是JsonConvert,配置类主要是JsonFactory、ConvertColumn。JsonFactory采用同ClassLoader类似的双亲委托方式设计。
|
||||||
JsonConvert 序列化encode方法:
|
JsonConvert 序列化encode方法:
|
||||||
@@ -81,16 +81,13 @@ JsonConvert 反序列化decode方法:
|
|||||||
System.out.println(childConvert.convertTo(user2));
|
System.out.println(childConvert.convertTo(user2));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
  在Redkale里存在默认的JsonConvert、BsonConvert、ProtobufConvert对象。 只需在所有Service、Servlet中增加依赖注入资源。
|
  在Redkale里存在默认的JsonConvert、ProtobufConvert对象。 只需在所有Service、Servlet中增加依赖注入资源。
|
||||||
```java
|
```java
|
||||||
public class XXXService implements Service {
|
public class XXXService implements Service {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private JsonConvert jsonConvert;
|
private JsonConvert jsonConvert;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private BsonConvert bsonConvert;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ProtobufConvert protobufConvert;
|
private ProtobufConvert protobufConvert;
|
||||||
}
|
}
|
||||||
@@ -100,9 +97,6 @@ public class XXXServlet extends HttpServlet {
|
|||||||
@Resource
|
@Resource
|
||||||
private JsonConvert jsonConvert;
|
private JsonConvert jsonConvert;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private BsonConvert bsonConvert;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ProtobufConvert protobufConvert;
|
private ProtobufConvert protobufConvert;
|
||||||
|
|
||||||
@@ -269,7 +263,7 @@ public class FileSimpleCoder<R extends Reader, W extends Writer> extends Simpled
|
|||||||
|
|
||||||
JsonFactory.root().register(File.class, FileSimpleCoder.instance);
|
JsonFactory.root().register(File.class, FileSimpleCoder.instance);
|
||||||
|
|
||||||
BsonFactory.root().register(File.class, FileSimpleCoder.instance);
|
ProtobufFactory.root().register(File.class, FileSimpleCoder.instance);
|
||||||
```
|
```
|
||||||
    2. 通过JavaBean类自定义静态方法自动加载:
|
    2. 通过JavaBean类自定义静态方法自动加载:
|
||||||
```java
|
```java
|
||||||
@@ -293,9 +287,8 @@ public class InnerCoderEntity {
|
|||||||
* 1) 方法名可以随意。
|
* 1) 方法名可以随意。
|
||||||
* 2) 方法必须是static
|
* 2) 方法必须是static
|
||||||
* 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。
|
* 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。
|
||||||
* —3.1) 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON,BSON,PROTOBUF。
|
* —3.1) 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON,PROTOBUF。
|
||||||
* —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
* —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
||||||
* —3.3) 参数类型为org.redkale.convert.bson.BsonFactory 表示仅适合BSON。
|
|
||||||
* —3.3) 参数类型为org.redkale.convert.pb.ProtobufFactory 表示仅适合PROTOBUF。
|
* —3.3) 参数类型为org.redkale.convert.pb.ProtobufFactory 表示仅适合PROTOBUF。
|
||||||
* 4)方法的返回类型必须是 Decodeable/Encodeable/SimpledCoder
|
* 4)方法的返回类型必须是 Decodeable/Encodeable/SimpledCoder
|
||||||
* 若返回类型不是SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
* 若返回类型不是SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
||||||
@@ -445,36 +438,3 @@ public class RestConvertService extends AbstractService {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## BSON数据格式
|
|
||||||
  BSON类似Java自带的Serializable, 其格式如下:
|
|
||||||
|
|
||||||
    1). 基本数据类型: 直接转换成byte[]
|
|
||||||
|
|
||||||
    2). StandardString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。
|
|
||||||
|
|
||||||
    3). String: length(4 bytes) + byte[](utf8);
|
|
||||||
|
|
||||||
    4). 数组: length(4 bytes) + byte[]...
|
|
||||||
|
|
||||||
    5). Object:
|
|
||||||
|
|
||||||
      1. realclass (StandardString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名)
|
|
||||||
|
|
||||||
      2. 空字符串(StandardString)
|
|
||||||
|
|
||||||
      3. SIGN_OBJECTB 标记位,值固定为0xBB (short)
|
|
||||||
|
|
||||||
      4. 循环字段值:
|
|
||||||
|
|
||||||
        4.1 SIGN_HASNEXT 标记位,值固定为1 (byte)
|
|
||||||
|
|
||||||
        4.2 字段类型; 11-19为基本类型&字符串; 21-29为基本类型&字符串的数组; 127为Object
|
|
||||||
|
|
||||||
        4.3 字段名 (StandardString)
|
|
||||||
|
|
||||||
        4.4 字段的值Object
|
|
||||||
|
|
||||||
      5. SIGN_NONEXT 标记位,值固定为0 (byte)
|
|
||||||
|
|
||||||
       6. SIGN_OBJECTE 标记位,值固定为0xEE (short)
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ module org.redkale {
|
|||||||
exports org.redkale.cluster;
|
exports org.redkale.cluster;
|
||||||
exports org.redkale.cluster.spi;
|
exports org.redkale.cluster.spi;
|
||||||
exports org.redkale.convert;
|
exports org.redkale.convert;
|
||||||
exports org.redkale.convert.bson;
|
|
||||||
exports org.redkale.convert.ext;
|
exports org.redkale.convert.ext;
|
||||||
exports org.redkale.convert.json;
|
exports org.redkale.convert.json;
|
||||||
exports org.redkale.convert.pb;
|
exports org.redkale.convert.pb;
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import org.redkale.cluster.spi.ClusterModuleEngine;
|
|||||||
import org.redkale.cluster.spi.HttpClusterRpcClient;
|
import org.redkale.cluster.spi.HttpClusterRpcClient;
|
||||||
import org.redkale.cluster.spi.HttpLocalRpcClient;
|
import org.redkale.cluster.spi.HttpLocalRpcClient;
|
||||||
import org.redkale.convert.Convert;
|
import org.redkale.convert.Convert;
|
||||||
import org.redkale.convert.bson.BsonFactory;
|
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
import org.redkale.convert.pb.ProtobufFactory;
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.inject.ResourceAnnotationLoader;
|
import org.redkale.inject.ResourceAnnotationLoader;
|
||||||
@@ -304,19 +303,14 @@ public final class Application {
|
|||||||
|
|
||||||
// 需要在加载properties初始化System.properties之后再注册
|
// 需要在加载properties初始化System.properties之后再注册
|
||||||
this.resourceFactory.register(Environment.class, environment);
|
this.resourceFactory.register(Environment.class, environment);
|
||||||
this.resourceFactory.register(BsonFactory.root());
|
|
||||||
this.resourceFactory.register(JsonFactory.root());
|
this.resourceFactory.register(JsonFactory.root());
|
||||||
this.resourceFactory.register(ProtobufFactory.root());
|
this.resourceFactory.register(ProtobufFactory.root());
|
||||||
this.resourceFactory.register(BsonFactory.root().getConvert());
|
|
||||||
this.resourceFactory.register(JsonFactory.root().getConvert());
|
this.resourceFactory.register(JsonFactory.root().getConvert());
|
||||||
this.resourceFactory.register(ProtobufFactory.root().getConvert());
|
this.resourceFactory.register(ProtobufFactory.root().getConvert());
|
||||||
this.resourceFactory.register(
|
|
||||||
"bsonconvert", Convert.class, BsonFactory.root().getConvert());
|
|
||||||
this.resourceFactory.register(
|
this.resourceFactory.register(
|
||||||
"jsonconvert", Convert.class, JsonFactory.root().getConvert());
|
"jsonconvert", Convert.class, JsonFactory.root().getConvert());
|
||||||
this.resourceFactory.register(
|
this.resourceFactory.register(
|
||||||
"protobufconvert", Convert.class, ProtobufFactory.root().getConvert());
|
"protobufconvert", Convert.class, ProtobufFactory.root().getConvert());
|
||||||
BsonFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
|
||||||
JsonFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
JsonFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
||||||
ProtobufFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
ProtobufFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,14 @@
|
|||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.redkale.annotation.Serial;
|
||||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||||
import org.redkale.convert.Decodeable;
|
import org.redkale.convert.Decodeable;
|
||||||
import org.redkale.convert.bson.BsonFactory;
|
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.persistence.Entity;
|
import org.redkale.persistence.Entity;
|
||||||
import org.redkale.source.*;
|
import org.redkale.source.*;
|
||||||
import org.redkale.util.Utility;
|
import org.redkale.util.Utility;
|
||||||
import org.redkale.annotation.Serial;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行一次Application.run提前获取所有动态类
|
* 执行一次Application.run提前获取所有动态类
|
||||||
@@ -60,11 +60,11 @@ public class PrepareCompiler {
|
|||||||
// application.dataSources.forEach(source -> source.compile(clz));
|
// application.dataSources.forEach(source -> source.compile(clz));
|
||||||
JsonFactory.root().loadEncoder(clz);
|
JsonFactory.root().loadEncoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadEncoder(clz);
|
ProtobufFactory.root().loadEncoder(clz);
|
||||||
}
|
}
|
||||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadDecoder(clz);
|
ProtobufFactory.root().loadDecoder(clz);
|
||||||
}
|
}
|
||||||
decoder.convertFrom(new JsonReader("{}"));
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
@@ -81,11 +81,11 @@ public class PrepareCompiler {
|
|||||||
// application.dataSources.forEach(source -> source.compile(clz));
|
// application.dataSources.forEach(source -> source.compile(clz));
|
||||||
JsonFactory.root().loadEncoder(clz);
|
JsonFactory.root().loadEncoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadEncoder(clz);
|
ProtobufFactory.root().loadEncoder(clz);
|
||||||
}
|
}
|
||||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadDecoder(clz);
|
ProtobufFactory.root().loadDecoder(clz);
|
||||||
}
|
}
|
||||||
decoder.convertFrom(new JsonReader("{}"));
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
@@ -99,11 +99,11 @@ public class PrepareCompiler {
|
|||||||
try {
|
try {
|
||||||
JsonFactory.root().loadEncoder(clz);
|
JsonFactory.root().loadEncoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadEncoder(clz);
|
ProtobufFactory.root().loadEncoder(clz);
|
||||||
}
|
}
|
||||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadDecoder(clz);
|
ProtobufFactory.root().loadDecoder(clz);
|
||||||
}
|
}
|
||||||
decoder.convertFrom(new JsonReader("{}"));
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
@@ -117,11 +117,11 @@ public class PrepareCompiler {
|
|||||||
try {
|
try {
|
||||||
JsonFactory.root().loadEncoder(clz);
|
JsonFactory.root().loadEncoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadEncoder(clz);
|
ProtobufFactory.root().loadEncoder(clz);
|
||||||
}
|
}
|
||||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
if (hasSncp) {
|
if (hasSncp) {
|
||||||
BsonFactory.root().loadDecoder(clz);
|
ProtobufFactory.root().loadDecoder(clz);
|
||||||
}
|
}
|
||||||
decoder.convertFrom(new JsonReader("{}"));
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ package org.redkale.convert;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对不明类型的对象进行序列化; BSON序列化时将对象的类名写入Writer,JSON则不写入。
|
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public @interface ConvertCoder {
|
|||||||
/**
|
/**
|
||||||
* 解析/序列化定制化的TYPE
|
* 解析/序列化定制化的TYPE
|
||||||
*
|
*
|
||||||
* @return JSON or BSON or ALL
|
* @return JSON or PROTOBUF or ALL
|
||||||
*/
|
*/
|
||||||
ConvertType type() default ConvertType.ALL;
|
ConvertType type() default ConvertType.ALL;
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public @interface ConvertColumn {
|
|||||||
/**
|
/**
|
||||||
* 解析/序列化定制化的TYPE
|
* 解析/序列化定制化的TYPE
|
||||||
*
|
*
|
||||||
* @return JSON or BSON or ALL
|
* @return JSON or PROTOBUF or ALL
|
||||||
*/
|
*/
|
||||||
ConvertType type() default ConvertType.ALL;
|
ConvertType type() default ConvertType.ALL;
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public @interface ConvertColumnHandler {
|
|||||||
/**
|
/**
|
||||||
* 解析/序列化定制化的TYPE
|
* 解析/序列化定制化的TYPE
|
||||||
*
|
*
|
||||||
* @return JSON or BSON or ALL
|
* @return JSON or PROTOBUF or ALL
|
||||||
*/
|
*/
|
||||||
ConvertType type() default ConvertType.ALL;
|
ConvertType type() default ConvertType.ALL;
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.convert;
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
import static java.lang.annotation.ElementType.*;
|
import static java.lang.annotation.ElementType.*;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序列化时永久禁用该字段, 与ConvertColumn.ignore()的区别在于: ConvertDisabled不能通过ConvertEntity来解禁
|
* 序列化时永久禁用该字段, 与ConvertColumn.ignore()的区别在于: ConvertDisabled不能通过ConvertEntity来解禁
|
||||||
*
|
*
|
||||||
@@ -24,7 +23,7 @@ public @interface ConvertDisabled {
|
|||||||
/**
|
/**
|
||||||
* 解析/序列化定制化的TYPE
|
* 解析/序列化定制化的TYPE
|
||||||
*
|
*
|
||||||
* @return JSON or BSON or ALL
|
* @return JSON or PROTOBUF or ALL
|
||||||
*/
|
*/
|
||||||
ConvertType type() default ConvertType.ALL;
|
ConvertType type() default ConvertType.ALL;
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于类名的别名, 该值必须是全局唯一 <br>
|
* 用于类名的别名, 该值必须是全局唯一 <br>
|
||||||
* 使用场景: 当BSON序列化为了不指定class可以使用@ConvertEntity来取个别名。 <br>
|
* 使用场景: 当自定义序列化为了不指定class可以使用@ConvertEntity来取个别名。 <br>
|
||||||
* 关联方法: {@link org.redkale.convert.Reader#readClassName()} 和 {@link org.redkale.convert.Writer#writeClassName(java.lang.String) } 。
|
* 关联方法: {@link org.redkale.convert.Reader#readClassName()} 和 {@link org.redkale.convert.Writer#writeClassName(java.lang.String) } 。
|
||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import java.util.function.BiFunction;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.ext.*;
|
import org.redkale.convert.ext.*;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.convert.pb.ProtobufConvert;
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
@@ -223,9 +222,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
if (type == ConvertType.PROTOBUF || type.contains(ConvertType.PROTOBUF)) {
|
if (type == ConvertType.PROTOBUF || type.contains(ConvertType.PROTOBUF)) {
|
||||||
return ProtobufConvert.root();
|
return ProtobufConvert.root();
|
||||||
}
|
}
|
||||||
if (type == ConvertType.BSON || type.contains(ConvertType.BSON)) {
|
|
||||||
return BsonConvert.root();
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<ConvertProvider> it = ServiceLoader.load(ConvertProvider.class).iterator();
|
Iterator<ConvertProvider> it = ServiceLoader.load(ConvertProvider.class).iterator();
|
||||||
RedkaleClassLoader.putServiceLoader(ConvertProvider.class);
|
RedkaleClassLoader.putServiceLoader(ConvertProvider.class);
|
||||||
|
|||||||
@@ -14,10 +14,8 @@ package org.redkale.convert;
|
|||||||
*/
|
*/
|
||||||
public enum ConvertType {
|
public enum ConvertType {
|
||||||
JSON(1),
|
JSON(1),
|
||||||
BSON(2),
|
PROTOBUF(2),
|
||||||
PROTOBUF(64),
|
PROTOBUF_JSON(2 + 1),
|
||||||
PROTOBUF_JSON(64 + 1),
|
|
||||||
PROTOBUF_BSON(64 + 2),
|
|
||||||
DIY(256),
|
DIY(256),
|
||||||
ALL(1023);
|
ALL(1023);
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public abstract class Writer {
|
|||||||
public abstract void writeNull();
|
public abstract void writeNull();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否需要写入类名, BSON需要, JSON不需要
|
* 是否需要写入类名, JSON不需要
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.*;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数组的反序列化操作类 <br>
|
|
||||||
* 对象数组的反序列化,不包含int[]、long[]这样的primitive class数组。 <br>
|
|
||||||
* 支持一定程度的泛型。 <br>
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <T> 反解析的数组元素类型
|
|
||||||
*/
|
|
||||||
public class BsonArrayDecoder<T> extends ArrayDecoder<BsonReader, T> {
|
|
||||||
|
|
||||||
private final boolean skip;
|
|
||||||
|
|
||||||
public BsonArrayDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
|
||||||
super(factory, type);
|
|
||||||
this.skip = skip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T[] convertFrom(BsonReader in) {
|
|
||||||
this.checkInited();
|
|
||||||
int len = in.readArrayB(this.componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
|
||||||
if (skip) {
|
|
||||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
|
||||||
}
|
|
||||||
final List<T> result = new ArrayList();
|
|
||||||
// 固定长度
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
result.add(itemDecoder.convertFrom(in));
|
|
||||||
}
|
|
||||||
in.readArrayE();
|
|
||||||
T[] rs = this.componentArrayFunction.apply(result.size());
|
|
||||||
return result.toArray(rs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.nio.ByteBuffer;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 以ByteBuffer为数据载体的BsonReader
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public class BsonByteBufferReader extends BsonReader {
|
|
||||||
|
|
||||||
private ByteBuffer[] buffers;
|
|
||||||
|
|
||||||
private int currentIndex = 0;
|
|
||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
|
||||||
|
|
||||||
protected BsonByteBufferReader() {}
|
|
||||||
|
|
||||||
protected BsonByteBufferReader(ByteBuffer... buffers) {
|
|
||||||
this.buffers = buffers;
|
|
||||||
this.currentBuffer = buffers[currentIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean recycle() {
|
|
||||||
super.recycle(); // this.position 初始化值为-1
|
|
||||||
this.currentIndex = 0;
|
|
||||||
this.currentBuffer = null;
|
|
||||||
this.buffers = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected byte currentByte() {
|
|
||||||
return currentBuffer.get(currentBuffer.position());
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final boolean readBoolean() {
|
|
||||||
return readByte() == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte readByte() {
|
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
|
||||||
this.position++;
|
|
||||||
return this.currentBuffer.get();
|
|
||||||
}
|
|
||||||
for (; ; ) {
|
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
|
||||||
this.position++;
|
|
||||||
return this.currentBuffer.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final char readChar() {
|
|
||||||
if (this.currentBuffer != null) {
|
|
||||||
int remain = this.currentBuffer.remaining();
|
|
||||||
if (remain >= 2) {
|
|
||||||
this.position += 2;
|
|
||||||
return this.currentBuffer.getChar();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (char) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final short readShort() {
|
|
||||||
if (this.currentBuffer != null) {
|
|
||||||
int remain = this.currentBuffer.remaining();
|
|
||||||
if (remain >= 2) {
|
|
||||||
this.position += 2;
|
|
||||||
return this.currentBuffer.getShort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (short) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int readInt() {
|
|
||||||
if (this.currentBuffer != null) {
|
|
||||||
int remain = this.currentBuffer.remaining();
|
|
||||||
if (remain >= 4) {
|
|
||||||
this.position += 4;
|
|
||||||
return this.currentBuffer.getInt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ((readByte() & 0xff) << 24)
|
|
||||||
| ((readByte() & 0xff) << 16)
|
|
||||||
| ((readByte() & 0xff) << 8)
|
|
||||||
| (readByte() & 0xff);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final long readLong() {
|
|
||||||
if (this.currentBuffer != null) {
|
|
||||||
int remain = this.currentBuffer.remaining();
|
|
||||||
if (remain >= 8) {
|
|
||||||
this.position += 8;
|
|
||||||
return this.currentBuffer.getLong();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ((((long) readByte() & 0xff) << 56)
|
|
||||||
| (((long) readByte() & 0xff) << 48)
|
|
||||||
| (((long) readByte() & 0xff) << 40)
|
|
||||||
| (((long) readByte() & 0xff) << 32)
|
|
||||||
| (((long) readByte() & 0xff) << 24)
|
|
||||||
| (((long) readByte() & 0xff) << 16)
|
|
||||||
| (((long) readByte() & 0xff) << 8)
|
|
||||||
| (((long) readByte() & 0xff)));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected byte[] read(final int len) {
|
|
||||||
byte[] bs = new byte[len];
|
|
||||||
read(bs, 0);
|
|
||||||
return bs;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void read(final byte[] bs, final int pos) {
|
|
||||||
int remain = this.currentBuffer.remaining();
|
|
||||||
if (remain < 1) {
|
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
|
||||||
read(bs, pos);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int len = bs.length - pos;
|
|
||||||
if (remain >= len) {
|
|
||||||
this.position += len;
|
|
||||||
this.currentBuffer.get(bs, pos, len);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.currentBuffer.get(bs, pos, remain);
|
|
||||||
this.position += remain;
|
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
|
||||||
read(bs, pos + remain);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readStandardString() {
|
|
||||||
int len = 0xff & readByte();
|
|
||||||
if (len == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return new String(read(len));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readString() {
|
|
||||||
int len = readInt();
|
|
||||||
if (len == SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (len == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return new String(read(len), StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.nio.ByteBuffer;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
import org.redkale.util.ByteArray;
|
|
||||||
import org.redkale.util.Utility;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 以ByteBuffer为数据载体的BsonWriter
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public class BsonByteBufferWriter extends BsonWriter {
|
|
||||||
|
|
||||||
private final Supplier<ByteBuffer> supplier;
|
|
||||||
|
|
||||||
private ByteBuffer[] buffers;
|
|
||||||
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
public BsonByteBufferWriter(Supplier<ByteBuffer> supplier) {
|
|
||||||
this(0, supplier);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BsonByteBufferWriter(int features, Supplier<ByteBuffer> supplier) {
|
|
||||||
super((byte[]) null);
|
|
||||||
this.features = features;
|
|
||||||
this.supplier = supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer[] toBuffers() {
|
|
||||||
if (buffers == null) {
|
|
||||||
return new ByteBuffer[0];
|
|
||||||
}
|
|
||||||
for (int i = index; i < this.buffers.length; i++) {
|
|
||||||
ByteBuffer buf = this.buffers[i];
|
|
||||||
if (buf.position() != 0) {
|
|
||||||
buf.flip();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.buffers;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteArray toByteArray() {
|
|
||||||
ByteArray array = new ByteArray();
|
|
||||||
if (buffers != null) {
|
|
||||||
for (ByteBuffer buf : toBuffers()) {
|
|
||||||
array.put(buf);
|
|
||||||
buf.flip();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return Objects.toString(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int expand(final int byteLength) {
|
|
||||||
if (this.buffers == null) {
|
|
||||||
this.index = 0;
|
|
||||||
this.buffers = new ByteBuffer[] {supplier.get()};
|
|
||||||
}
|
|
||||||
ByteBuffer buffer = this.buffers[index];
|
|
||||||
if (!buffer.hasRemaining()) {
|
|
||||||
buffer.flip();
|
|
||||||
buffer = supplier.get();
|
|
||||||
this.buffers = Utility.append(this.buffers, buffer);
|
|
||||||
this.index++;
|
|
||||||
}
|
|
||||||
int len = buffer.remaining();
|
|
||||||
int size = 0;
|
|
||||||
while (len < byteLength) {
|
|
||||||
buffer = supplier.get();
|
|
||||||
this.buffers = Utility.append(this.buffers, buffer);
|
|
||||||
len += buffer.remaining();
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeTo(final byte[] chs, final int start, final int len) {
|
|
||||||
if (expand(len) == 0) {
|
|
||||||
this.buffers[index].put(chs, start, len);
|
|
||||||
} else {
|
|
||||||
ByteBuffer buffer = this.buffers[index];
|
|
||||||
final int end = start + len;
|
|
||||||
int remain = len; // 还剩多少没有写
|
|
||||||
while (remain > 0) {
|
|
||||||
final int br = buffer.remaining();
|
|
||||||
if (remain > br) { // 一个buffer写不完
|
|
||||||
buffer.put(chs, end - remain, br);
|
|
||||||
buffer = nextByteBuffer();
|
|
||||||
remain -= br;
|
|
||||||
} else {
|
|
||||||
buffer.put(chs, end - remain, remain);
|
|
||||||
remain = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.count += len;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ByteBuffer nextByteBuffer() {
|
|
||||||
this.buffers[this.index].flip();
|
|
||||||
return this.buffers[++this.index];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeTo(final byte ch) {
|
|
||||||
expand(1);
|
|
||||||
this.buffers[index].put(ch);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean recycle() {
|
|
||||||
super.recycle();
|
|
||||||
this.index = 0;
|
|
||||||
this.specificObjectType = null;
|
|
||||||
this.buffers = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final byte[] toArray() {
|
|
||||||
return toByteArray().getBytes();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final byte[] content() {
|
|
||||||
throw new UnsupportedOperationException("Not supported yet."); // 无需实现
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int offset() {
|
|
||||||
throw new UnsupportedOperationException("Not supported yet."); // 无需实现
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int length() {
|
|
||||||
throw new UnsupportedOperationException("Not supported yet."); // 无需实现
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.Collection;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Collection的反序列化操作类 <br>
|
|
||||||
* 支持一定程度的泛型。 <br>
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <T> 反解析的集合元素类型
|
|
||||||
*/
|
|
||||||
public class BsonCollectionDecoder<T> extends CollectionDecoder<BsonReader, T> {
|
|
||||||
|
|
||||||
private final boolean skip;
|
|
||||||
|
|
||||||
public BsonCollectionDecoder(final ConvertFactory factory, final Type type, boolean skip) {
|
|
||||||
super(factory, type);
|
|
||||||
this.skip = skip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<T> convertFrom(BsonReader in) {
|
|
||||||
this.checkInited();
|
|
||||||
int len = in.readArrayB(componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
|
||||||
if (skip) {
|
|
||||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
|
||||||
}
|
|
||||||
final Collection<T> result = this.creator.create();
|
|
||||||
// 固定长度
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
result.add(itemDecoder.convertFrom(in));
|
|
||||||
}
|
|
||||||
in.readArrayE();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,336 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io.*;
|
|
||||||
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.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* <blockquote>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* BSON协议格式:
|
|
||||||
* 1) 基本数据类型: 直接转换成byte[]
|
|
||||||
* 2) StandardString(无特殊字符且长度小于256的字符串): length(1 byte) + byte[](utf8); 通常用于类名、字段名、枚举。
|
|
||||||
* 3) String: length(4 bytes) + byte[](utf8);
|
|
||||||
* 4) 数组: length(4 bytes) + byte[]...
|
|
||||||
* 5) Object:
|
|
||||||
* 1、 realclass (StandardString) (如果指定格式化的class与实体对象的class不一致才会有该值, 该值可以使用@ConvertEntity给其取个别名)
|
|
||||||
* 2、 空字符串(StandardString)
|
|
||||||
* 3、 SIGN_OBJECTB 标记位,值固定为0xBB (short)
|
|
||||||
* 4、 循环字段值:
|
|
||||||
* 4.1 SIGN_HASNEXT 标记位,值固定为1 (byte)
|
|
||||||
* 4.2 字段类型; 11-19为基本类型和字符串; 21-29为基本类型和字符串的数组; 127为Object
|
|
||||||
* 4.3 字段名 (StandardString)
|
|
||||||
* 4.4 字段的值Object
|
|
||||||
* 5、 SIGN_NONEXT 标记位,值固定为0 (byte)
|
|
||||||
* 6、 SIGN_OBJECTE 标记位,值固定为0xEE (short)
|
|
||||||
*
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* </blockquote>
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|
||||||
|
|
||||||
private final ThreadLocal<BsonWriter> writerPool = Utility.withInitialThreadLocal(BsonWriter::new);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory getFactory() {
|
|
||||||
return (BsonFactory) factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BsonConvert root() {
|
|
||||||
return BsonFactory.root().getConvert();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc) {
|
|
||||||
return newConvert(objFieldFunc, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc, BiFunction mapFieldFunc) {
|
|
||||||
return newConvert(objFieldFunc, mapFieldFunc, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonConvert newConvert(
|
|
||||||
final BiFunction<Attribute, Object, Object> objFieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
|
||||||
return newConvert(objFieldFunc, null, objExtFunc);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonConvert newConvert(
|
|
||||||
final BiFunction<Attribute, Object, Object> fieldFunc,
|
|
||||||
BiFunction mapFieldFunc,
|
|
||||||
Function<Object, ConvertField[]> objExtFunc) {
|
|
||||||
return new BsonConvert(getFactory(), features) {
|
|
||||||
@Override
|
|
||||||
protected <S extends BsonWriter> S configWrite(S writer) {
|
|
||||||
return fieldFunc(writer, fieldFunc, mapFieldFunc, objExtFunc);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------ reader -----------------------------------------------------------
|
|
||||||
public BsonReader pollReader(final ByteBuffer... buffers) {
|
|
||||||
return new BsonByteBufferReader(buffers);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonReader pollReader(final InputStream in) {
|
|
||||||
return new BsonStreamReader(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonReader pollReader() {
|
|
||||||
BsonReader reader = readerPool.get();
|
|
||||||
if (reader == null) {
|
|
||||||
reader = new BsonReader();
|
|
||||||
} else {
|
|
||||||
readerPool.set(null);
|
|
||||||
}
|
|
||||||
return reader;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void offerReader(final BsonReader in) {
|
|
||||||
if (in != null) {
|
|
||||||
in.recycle();
|
|
||||||
readerPool.set(in);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------ writer -----------------------------------------------------------
|
|
||||||
public BsonByteBufferWriter pollWriter(final Supplier<ByteBuffer> supplier) {
|
|
||||||
return configWrite(new BsonByteBufferWriter(features, supplier));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BsonWriter pollWriter(final OutputStream out) {
|
|
||||||
return configWrite(new BsonStreamWriter(features, out));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonWriter pollWriter() {
|
|
||||||
BsonWriter writer = writerPool.get();
|
|
||||||
if (writer == null) {
|
|
||||||
writer = new BsonWriter();
|
|
||||||
} else {
|
|
||||||
writerPool.set(null);
|
|
||||||
}
|
|
||||||
return configWrite(writer.withFeatures(features));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void offerWriter(final BsonWriter out) {
|
|
||||||
if (out != null) {
|
|
||||||
out.recycle();
|
|
||||||
writerPool.set(out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------ convertFrom -----------------------------------------------------------
|
|
||||||
@Override
|
|
||||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
|
||||||
if (bytes == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return convertFrom(type, bytes, 0, bytes.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int len) {
|
|
||||||
if (type == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final BsonReader in = new BsonReader(bytes, offset, len);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T convertFrom(final Type type, final InputStream in) {
|
|
||||||
if (type == null || in == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return (T) factory.loadDecoder(type).convertFrom(new BsonStreamReader(in));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
|
||||||
if (type == null || Utility.isEmpty(buffers)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
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
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T convertFrom(final Type type, final BsonReader reader) {
|
|
||||||
if (type == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------ convertTo -----------------------------------------------------------
|
|
||||||
@Override
|
|
||||||
public byte[] convertTo(final Type type, final Object value) {
|
|
||||||
if (type == null && value == null) {
|
|
||||||
final BsonWriter out = pollWriter();
|
|
||||||
out.writeNull();
|
|
||||||
byte[] result = out.toArray();
|
|
||||||
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();
|
|
||||||
encoder.convertTo(writer, value);
|
|
||||||
byte[] result = writer.toArray();
|
|
||||||
offerWriter(writer);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] convertToBytes(final Type type, final Object value) {
|
|
||||||
return convertTo(type, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler) {
|
|
||||||
final BsonWriter writer = pollWriter();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
encoder.convertTo(writer, value);
|
|
||||||
}
|
|
||||||
writer.completed(handler, writerConsumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convertToBytes(final ByteArray array, final Type type, final Object value) {
|
|
||||||
Objects.requireNonNull(array);
|
|
||||||
final BsonWriter writer = configWrite(new BsonWriter(array).withFeatures(features));
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void convertTo(final OutputStream out, final Object value) {
|
|
||||||
convertTo(out, (Type) null, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void convertTo(final OutputStream out, final Type type, final Object value) {
|
|
||||||
if (type == null && value == null) {
|
|
||||||
pollWriter(out).writeNull();
|
|
||||||
} else {
|
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(pollWriter(out), value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
|
|
||||||
Objects.requireNonNull(supplier);
|
|
||||||
BsonByteBufferWriter writer = pollWriter(supplier);
|
|
||||||
if (type == null && value == null) {
|
|
||||||
writer.writeNull();
|
|
||||||
} else {
|
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
|
||||||
}
|
|
||||||
return writer.toBuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convertTo(final BsonWriter writer, final Type type, final Object value) {
|
|
||||||
if (type == null && value == null) { // 必须判断type==null
|
|
||||||
writer.writeNull();
|
|
||||||
} else {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonWriter convertToWriter(final Type type, final Object value) {
|
|
||||||
if (value == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final BsonWriter writer = writerPool.get().withFeatures(features);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,267 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io.Serializable;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
import org.redkale.convert.ext.*;
|
|
||||||
import org.redkale.util.TypeToken;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BSON的ConvertFactory
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
|
||||||
|
|
||||||
private static final BsonFactory instance = new BsonFactory(
|
|
||||||
null,
|
|
||||||
getSystemPropertyInt("redkale.convert.bson.tiny", "redkale.convert.tiny", true, Convert.FEATURE_TINY)
|
|
||||||
| getSystemPropertyInt(
|
|
||||||
"redkale.convert.bson.nullable",
|
|
||||||
"redkale.convert.nullable",
|
|
||||||
false,
|
|
||||||
Convert.FEATURE_NULLABLE));
|
|
||||||
|
|
||||||
static final Decodeable objectDecoder = instance.loadDecoder(Object.class);
|
|
||||||
|
|
||||||
static final Encodeable objectEncoder = instance.loadEncoder(Object.class);
|
|
||||||
|
|
||||||
// only for BsonRead.skipValue
|
|
||||||
static final Decodeable skipArrayDecoder = new BsonArrayDecoder(instance, Object[].class, true);
|
|
||||||
|
|
||||||
// only for BsonRead.skipValue
|
|
||||||
static final Decodeable skipCollectionDecoder = new BsonCollectionDecoder(instance, Collection.class, true);
|
|
||||||
|
|
||||||
// only for BsonRead.skipValue
|
|
||||||
static final Decodeable skipStreamDecoder = new BsonStreamDecoder(instance, Stream.class, true);
|
|
||||||
|
|
||||||
// only for BsonRead.skipValue
|
|
||||||
static final Decodeable skipMapDecoder = new BsonMapDecoder(instance, Map.class, true);
|
|
||||||
|
|
||||||
static {
|
|
||||||
instance.register(Serializable.class, objectDecoder);
|
|
||||||
instance.register(Serializable.class, objectEncoder);
|
|
||||||
|
|
||||||
// instance.register(AnyValue.class, instance.loadDecoder(SimpleAnyValue.class));
|
|
||||||
// instance.register(AnyValue.class, instance.loadEncoder(SimpleAnyValue.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
private BsonFactory(BsonFactory parent, int features) {
|
|
||||||
super(parent, features);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory withFeatures(int features) {
|
|
||||||
return super.withFeatures(features);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory addFeature(int feature) {
|
|
||||||
return super.addFeature(feature);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory removeFeature(int feature) {
|
|
||||||
return super.removeFeature(feature);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory withTinyFeature(boolean tiny) {
|
|
||||||
return super.withTinyFeature(tiny);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory withNullableFeature(boolean nullable) {
|
|
||||||
return super.withNullableFeature(nullable);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory skipAllIgnore(final boolean skipIgnore) {
|
|
||||||
this.registerSkipAllIgnore(skipIgnore);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ConvertFactory rootFactory() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BsonFactory root() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BsonFactory create() {
|
|
||||||
return new BsonFactory(null, instance.features);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final BsonConvert getConvert() {
|
|
||||||
if (convert == null) {
|
|
||||||
convert = new BsonConvert(this, features);
|
|
||||||
}
|
|
||||||
return (BsonConvert) convert;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory createChild() {
|
|
||||||
return new BsonFactory(this, features);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BsonFactory createChild(int features) {
|
|
||||||
return new BsonFactory(this, features);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected <E> Decodeable<BsonReader, E> createArrayDecoder(Type type) {
|
|
||||||
return new BsonArrayDecoder(this, type, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected <E> Decodeable<BsonReader, E> createCollectionDecoder(Type type) {
|
|
||||||
return new BsonCollectionDecoder(this, type, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected <E> Decodeable<BsonReader, E> createStreamDecoder(Type type) {
|
|
||||||
return new BsonStreamDecoder(this, type, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected <E> Decodeable<BsonReader, E> createMapDecoder(Type type) {
|
|
||||||
return new BsonMapDecoder(this, type, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConvertType getConvertType() {
|
|
||||||
return ConvertType.BSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isReversible() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isFieldSort() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static byte typeEnum(final Type type) {
|
|
||||||
Objects.requireNonNull(type);
|
|
||||||
Class clazz = TypeToken.typeToClass(type);
|
|
||||||
byte typeval = 127; // 字段的类型值
|
|
||||||
if (clazz == boolean.class || clazz == Boolean.class || clazz == AtomicBoolean.class) {
|
|
||||||
typeval = 11;
|
|
||||||
} else if (clazz == byte.class || clazz == Byte.class) {
|
|
||||||
typeval = 12;
|
|
||||||
} else if (clazz == short.class || clazz == Short.class) {
|
|
||||||
typeval = 13;
|
|
||||||
} else if (clazz == char.class || clazz == Character.class) {
|
|
||||||
typeval = 14;
|
|
||||||
} else if (clazz == int.class || clazz == Integer.class || clazz == AtomicInteger.class) {
|
|
||||||
typeval = 15;
|
|
||||||
} else if (clazz == long.class || clazz == Long.class || clazz == AtomicLong.class) {
|
|
||||||
typeval = 16;
|
|
||||||
} else if (clazz == float.class || clazz == Float.class) {
|
|
||||||
typeval = 17;
|
|
||||||
} else if (clazz == double.class || clazz == Double.class) {
|
|
||||||
typeval = 18;
|
|
||||||
} else if (clazz == String.class) {
|
|
||||||
typeval = 19;
|
|
||||||
} else if (clazz == boolean[].class || clazz == Boolean[].class || clazz == AtomicBoolean[].class) {
|
|
||||||
typeval = 21;
|
|
||||||
} else if (clazz == byte[].class || clazz == Byte[].class) {
|
|
||||||
typeval = 22;
|
|
||||||
} else if (clazz == short[].class || clazz == Short[].class) {
|
|
||||||
typeval = 23;
|
|
||||||
} else if (clazz == char[].class || clazz == Character[].class) {
|
|
||||||
typeval = 24;
|
|
||||||
} else if (clazz == int[].class || clazz == Integer[].class || clazz == AtomicInteger[].class) {
|
|
||||||
typeval = 25;
|
|
||||||
} else if (clazz == long[].class || clazz == Long[].class || clazz == AtomicLong[].class) {
|
|
||||||
typeval = 26;
|
|
||||||
} else if (clazz == float[].class || clazz == Float[].class) {
|
|
||||||
typeval = 27;
|
|
||||||
} else if (clazz == double[].class || clazz == Double[].class) {
|
|
||||||
typeval = 28;
|
|
||||||
} else if (clazz == String[].class) {
|
|
||||||
typeval = 29;
|
|
||||||
} else if (clazz.isArray()) {
|
|
||||||
typeval = 81;
|
|
||||||
} else if (Collection.class.isAssignableFrom(clazz)) {
|
|
||||||
typeval = 82;
|
|
||||||
} else if (Stream.class.isAssignableFrom(clazz)) {
|
|
||||||
typeval = 83;
|
|
||||||
} else if (Map.class.isAssignableFrom(clazz)) {
|
|
||||||
typeval = 84;
|
|
||||||
}
|
|
||||||
return typeval;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static Decodeable skipTypeEnum(final byte typeval) {
|
|
||||||
switch (typeval) {
|
|
||||||
case 11:
|
|
||||||
return BoolSimpledCoder.instance;
|
|
||||||
case 12:
|
|
||||||
return ByteSimpledCoder.instance;
|
|
||||||
case 13:
|
|
||||||
return ShortSimpledCoder.instance;
|
|
||||||
case 14:
|
|
||||||
return CharSimpledCoder.instance;
|
|
||||||
case 15:
|
|
||||||
return IntSimpledCoder.instance;
|
|
||||||
case 16:
|
|
||||||
return LongSimpledCoder.instance;
|
|
||||||
case 17:
|
|
||||||
return FloatSimpledCoder.instance;
|
|
||||||
case 18:
|
|
||||||
return DoubleSimpledCoder.instance;
|
|
||||||
case 19:
|
|
||||||
return StringSimpledCoder.instance;
|
|
||||||
case 21:
|
|
||||||
return BoolArraySimpledCoder.instance;
|
|
||||||
case 22:
|
|
||||||
return ByteArraySimpledCoder.instance;
|
|
||||||
case 23:
|
|
||||||
return ShortArraySimpledCoder.instance;
|
|
||||||
case 24:
|
|
||||||
return CharArraySimpledCoder.instance;
|
|
||||||
case 25:
|
|
||||||
return IntArraySimpledCoder.instance;
|
|
||||||
case 26:
|
|
||||||
return LongArraySimpledCoder.instance;
|
|
||||||
case 27:
|
|
||||||
return FloatArraySimpledCoder.instance;
|
|
||||||
case 28:
|
|
||||||
return DoubleArraySimpledCoder.instance;
|
|
||||||
case 29:
|
|
||||||
return StringArraySimpledCoder.instance;
|
|
||||||
case 81:
|
|
||||||
return skipArrayDecoder;
|
|
||||||
case 82:
|
|
||||||
return skipCollectionDecoder;
|
|
||||||
case 83:
|
|
||||||
return skipStreamDecoder;
|
|
||||||
case 84:
|
|
||||||
return skipMapDecoder;
|
|
||||||
case 127:
|
|
||||||
return objectDecoder;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.Map;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Map的反序列化操作类 <br>
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <K> Map key的数据类型
|
|
||||||
* @param <V> Map value的数据类型
|
|
||||||
*/
|
|
||||||
public class BsonMapDecoder<K, V> extends MapDecoder<BsonReader, K, V> {
|
|
||||||
|
|
||||||
private final boolean skip;
|
|
||||||
|
|
||||||
public BsonMapDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
|
||||||
super(factory, type);
|
|
||||||
this.skip = skip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<K, V> convertFrom(BsonReader in) {
|
|
||||||
this.checkInited();
|
|
||||||
int len = in.readMapB(this.keyDecoder, this.valueDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable<BsonReader, K> kdecoder = this.keyDecoder;
|
|
||||||
Decodeable<BsonReader, V> vdecoder = this.valueDecoder;
|
|
||||||
if (skip) {
|
|
||||||
kdecoder = BsonFactory.skipTypeEnum(in.readMapKeyTypeEnum());
|
|
||||||
vdecoder = BsonFactory.skipTypeEnum(in.readmapValueTypeEnum());
|
|
||||||
}
|
|
||||||
final Map<K, V> result = this.creator.create();
|
|
||||||
// 固定长度
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
K key = kdecoder.convertFrom(in);
|
|
||||||
in.readBlank();
|
|
||||||
V value = vdecoder.convertFrom(in);
|
|
||||||
result.put(key, value);
|
|
||||||
}
|
|
||||||
in.readMapE();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,325 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.nio.charset.StandardCharsets;
|
|
||||||
import org.redkale.annotation.Nullable;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
|
||||||
import org.redkale.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BSON数据源
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public class BsonReader extends Reader {
|
|
||||||
|
|
||||||
public static final short SIGN_OBJECTB = (short) 0xBB;
|
|
||||||
|
|
||||||
public static final short SIGN_OBJECTE = (short) 0xEE;
|
|
||||||
|
|
||||||
public static final byte SIGN_HASNEXT = 1;
|
|
||||||
|
|
||||||
public static final byte SIGN_NONEXT = 0;
|
|
||||||
|
|
||||||
public static final byte VERBOSE_NO = 1;
|
|
||||||
|
|
||||||
public static final byte VERBOSE_YES = 2;
|
|
||||||
|
|
||||||
protected byte fieldTypeEnum; // 字段的类型值 对应 BsonWriter.writeField
|
|
||||||
|
|
||||||
protected byte arrayItemTypeEnum;
|
|
||||||
|
|
||||||
protected byte mapKeyTypeEnum;
|
|
||||||
|
|
||||||
protected byte mapValueTypeEnum;
|
|
||||||
|
|
||||||
protected int position = -1;
|
|
||||||
|
|
||||||
private byte[] content;
|
|
||||||
|
|
||||||
public BsonReader() {}
|
|
||||||
|
|
||||||
public BsonReader(byte[] bytes) {
|
|
||||||
setBytes(bytes, 0, bytes.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonReader(byte[] bytes, int start, int len) {
|
|
||||||
setBytes(bytes, start, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void prepare(byte[] bytes) {
|
|
||||||
setBytes(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final BsonReader setBytes(byte[] bytes) {
|
|
||||||
if (bytes == null) {
|
|
||||||
this.position = 0;
|
|
||||||
} else {
|
|
||||||
setBytes(bytes, 0, bytes.length);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final BsonReader setBytes(byte[] bytes, int start, int len) {
|
|
||||||
if (bytes == null) {
|
|
||||||
this.position = 0;
|
|
||||||
} else {
|
|
||||||
this.content = bytes;
|
|
||||||
this.position = start - 1;
|
|
||||||
// this.limit = start + len - 1;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean recycle() {
|
|
||||||
this.position = -1;
|
|
||||||
this.fieldTypeEnum = 0;
|
|
||||||
this.arrayItemTypeEnum = 0;
|
|
||||||
this.mapKeyTypeEnum = 0;
|
|
||||||
this.mapValueTypeEnum = 0;
|
|
||||||
// this.limit = -1;
|
|
||||||
this.content = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonReader clear() {
|
|
||||||
recycle();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 跳过属性的值 */
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public final void skipValue() {
|
|
||||||
final byte val = this.fieldTypeEnum;
|
|
||||||
if (val == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.fieldTypeEnum = 0;
|
|
||||||
Decodeable decoder = BsonFactory.skipTypeEnum(val);
|
|
||||||
decoder.convertFrom(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readObjectB(final Class clazz) {
|
|
||||||
final String newcls = readClassName();
|
|
||||||
if (Utility.isNotEmpty(newcls)) {
|
|
||||||
return newcls;
|
|
||||||
}
|
|
||||||
short bt = readShort();
|
|
||||||
if (bt == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (bt != SIGN_OBJECTB) {
|
|
||||||
throw new ConvertException("a bson object must begin with " + (SIGN_OBJECTB) + " (position = " + position
|
|
||||||
+ ") but '" + currentByte() + "'");
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void readObjectE(final Class clazz) {
|
|
||||||
if (readShort() != SIGN_OBJECTE) {
|
|
||||||
throw new ConvertException("a bson object must end with " + (SIGN_OBJECTE) + " (position = " + position
|
|
||||||
+ ") but '" + currentByte() + "'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected byte currentByte() {
|
|
||||||
return this.content[this.position];
|
|
||||||
}
|
|
||||||
|
|
||||||
public final byte readMapKeyTypeEnum() {
|
|
||||||
return mapKeyTypeEnum;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
short lt = readShort();
|
|
||||||
this.mapKeyTypeEnum = readByte();
|
|
||||||
this.mapValueTypeEnum = readByte();
|
|
||||||
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void readMapE() {
|
|
||||||
this.mapKeyTypeEnum = 0;
|
|
||||||
this.mapValueTypeEnum = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final byte readArrayItemTypeEnum() {
|
|
||||||
return arrayItemTypeEnum;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int readArrayB(@Nullable Decodeable componentDecoder) {
|
|
||||||
short bt = readShort();
|
|
||||||
if (bt == Reader.SIGN_NULL) {
|
|
||||||
this.arrayItemTypeEnum = 0;
|
|
||||||
return bt;
|
|
||||||
}
|
|
||||||
short lt = readShort();
|
|
||||||
this.arrayItemTypeEnum = readByte();
|
|
||||||
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void readArrayE() {
|
|
||||||
this.arrayItemTypeEnum = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 判断下一个非空白字节是否: */
|
|
||||||
@Override
|
|
||||||
public final void readBlank() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int position() {
|
|
||||||
return this.position;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断对象是否存在下一个属性或者数组是否存在下一个元素
|
|
||||||
*
|
|
||||||
* @return 是否存在
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public final boolean hasNext() {
|
|
||||||
byte b = readByte();
|
|
||||||
if (b == SIGN_HASNEXT) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (b != SIGN_NONEXT) {
|
|
||||||
throw new ConvertException("hasNext option must be (" + (SIGN_HASNEXT) + " or " + (SIGN_NONEXT) + ") but '"
|
|
||||||
+ b + "' at position(" + this.position + ")");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final DeMember readField(final DeMemberInfo memberInfo) {
|
|
||||||
final String exceptedField = readStandardString();
|
|
||||||
this.fieldTypeEnum = readByte();
|
|
||||||
return memberInfo.getMemberByField(exceptedField);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
@Override
|
|
||||||
public boolean readBoolean() {
|
|
||||||
return content[++this.position] == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte readByte() {
|
|
||||||
return content[++this.position];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final byte[] readByteArray() {
|
|
||||||
short bt = readShort();
|
|
||||||
if (bt == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
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
|
|
||||||
public char readChar() {
|
|
||||||
return (char) ((0xff00 & (content[++this.position] << 8)) | (0xff & content[++this.position]));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public short readShort() {
|
|
||||||
return (short) ((0xff00 & (content[++this.position] << 8)) | (0xff & content[++this.position]));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int readInt() {
|
|
||||||
return ((content[++this.position] & 0xff) << 24)
|
|
||||||
| ((content[++this.position] & 0xff) << 16)
|
|
||||||
| ((content[++this.position] & 0xff) << 8)
|
|
||||||
| (content[++this.position] & 0xff);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long readLong() {
|
|
||||||
return ((((long) content[++this.position] & 0xff) << 56)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 48)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 40)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 32)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 24)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 16)
|
|
||||||
| (((long) content[++this.position] & 0xff) << 8)
|
|
||||||
| ((long) content[++this.position] & 0xff));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final float readFloat() {
|
|
||||||
return Float.intBitsToFloat(readInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final double readDouble() {
|
|
||||||
return Double.longBitsToDouble(readLong());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readClassName() {
|
|
||||||
return readStandardString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readStandardString() {
|
|
||||||
int len = 0xff & readByte();
|
|
||||||
if (len == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
String value = new String(content, ++this.position, len);
|
|
||||||
this.position += len - 1; // 上一行已经++this.position,所以此处要-1
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readString() {
|
|
||||||
int len = readInt();
|
|
||||||
if (len == SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (len == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
String value = new String(content, ++this.position, len, StandardCharsets.UTF_8);
|
|
||||||
this.position += len - 1; // 上一行已经++this.position,所以此处要-1
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ValueType readType() {
|
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 org.redkale.convert.SimpledCoder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <T> 序列化/反解析的数据类型
|
|
||||||
*/
|
|
||||||
public abstract class BsonSimpledCoder<T> extends SimpledCoder<BsonReader, BsonWriter, T> {}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.*;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stream的反序列化操作类 <br>
|
|
||||||
* 支持一定程度的泛型。 <br>
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <T> 反解析的集合元素类型
|
|
||||||
*/
|
|
||||||
public class BsonStreamDecoder<T> extends StreamDecoder<BsonReader, T> {
|
|
||||||
|
|
||||||
private final boolean skip;
|
|
||||||
|
|
||||||
public BsonStreamDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
|
||||||
super(factory, type);
|
|
||||||
this.skip = skip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Stream<T> convertFrom(BsonReader in) {
|
|
||||||
this.checkInited();
|
|
||||||
int len = in.readArrayB(componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
|
||||||
if (skip) {
|
|
||||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
|
||||||
}
|
|
||||||
final List<T> result = new ArrayList();
|
|
||||||
// 固定长度
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
result.add(itemDecoder.convertFrom(in));
|
|
||||||
}
|
|
||||||
in.readArrayE();
|
|
||||||
return result.stream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io.*;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
class BsonStreamReader extends BsonByteBufferReader {
|
|
||||||
|
|
||||||
private InputStream in;
|
|
||||||
|
|
||||||
private byte currByte;
|
|
||||||
|
|
||||||
protected BsonStreamReader(InputStream in) {
|
|
||||||
super();
|
|
||||||
this.in = in instanceof BufferedInputStream ? in : new BufferedInputStream(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean recycle() {
|
|
||||||
super.recycle(); // this.position 初始化值为-1
|
|
||||||
this.in = null;
|
|
||||||
this.currByte = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte readByte() {
|
|
||||||
try {
|
|
||||||
byte b = (currByte = (byte) in.read());
|
|
||||||
this.position++;
|
|
||||||
return b;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ConvertException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected byte currentByte() {
|
|
||||||
return currByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected byte[] read(final int len) {
|
|
||||||
byte[] bs = new byte[len];
|
|
||||||
try {
|
|
||||||
in.read(bs);
|
|
||||||
this.position += len;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ConvertException(e);
|
|
||||||
}
|
|
||||||
return bs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io.*;
|
|
||||||
import org.redkale.convert.ConvertException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
class BsonStreamWriter extends BsonByteBufferWriter {
|
|
||||||
|
|
||||||
private OutputStream out;
|
|
||||||
|
|
||||||
protected BsonStreamWriter(int features, OutputStream out) {
|
|
||||||
super(features, null);
|
|
||||||
this.out = out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean recycle() {
|
|
||||||
super.recycle();
|
|
||||||
this.out = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeTo(final byte[] chs, final int start, final int len) {
|
|
||||||
try {
|
|
||||||
out.write(chs, start, len);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ConvertException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeTo(final byte ch) {
|
|
||||||
try {
|
|
||||||
out.write((byte) ch);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ConvertException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,344 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.nio.ByteBuffer;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
|
||||||
import org.redkale.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public class BsonWriter extends Writer implements ByteTuple {
|
|
||||||
|
|
||||||
private static final int DEFAULT_SIZE = Integer.getInteger(
|
|
||||||
"redkale.convert.bson.writer.buffer.defsize",
|
|
||||||
Integer.getInteger("redkale.convert.writer.buffer.defsize", 1024));
|
|
||||||
|
|
||||||
private byte[] content;
|
|
||||||
|
|
||||||
protected int count;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] content() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int offset() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int length() {
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 直接获取全部数据, 实际数据需要根据count长度来截取
|
|
||||||
*
|
|
||||||
* @return byte[]
|
|
||||||
*/
|
|
||||||
public byte[] directBytes() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将本对象的内容引用复制给array
|
|
||||||
*
|
|
||||||
* @param array ByteArray
|
|
||||||
*/
|
|
||||||
public void directTo(ByteArray array) {
|
|
||||||
array.directFrom(content, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void completed(ConvertBytesHandler handler, Consumer<BsonWriter> callback) {
|
|
||||||
handler.completed(content, 0, count, callback, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ByteArray toByteArray() {
|
|
||||||
return new ByteArray(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ByteBuffer[] toBuffers() {
|
|
||||||
return new ByteBuffer[] {ByteBuffer.wrap(content, 0, count)};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BsonWriter(byte[] bs) {
|
|
||||||
this.content = bs == null ? new byte[0] : bs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonWriter() {
|
|
||||||
this(DEFAULT_SIZE);
|
|
||||||
this.features = BsonFactory.root().getFeatures();
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonWriter(int size) {
|
|
||||||
this.content = new byte[size > 128 ? size : 128];
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonWriter(ByteArray array) {
|
|
||||||
this.content = array.content();
|
|
||||||
this.count = array.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final BsonWriter withFeatures(int features) {
|
|
||||||
super.withFeatures(features);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* 扩充指定长度的缓冲区
|
|
||||||
*
|
|
||||||
* @param len 扩容长度
|
|
||||||
* @return 固定0
|
|
||||||
*/
|
|
||||||
protected int expand(int len) {
|
|
||||||
int newcount = count + len;
|
|
||||||
if (newcount > content.length) {
|
|
||||||
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
|
|
||||||
System.arraycopy(content, 0, newdata, 0, count);
|
|
||||||
this.content = newdata;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeTo(final byte ch) {
|
|
||||||
expand(1);
|
|
||||||
content[count++] = ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 类似writeTo(new byte[length])
|
|
||||||
public void writePlaceholderTo(final int length) {
|
|
||||||
expand(length);
|
|
||||||
count += length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void writeTo(final byte... chs) {
|
|
||||||
writeTo(chs, 0, chs.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeTo(final byte[] chs, final int start, final int len) {
|
|
||||||
expand(len);
|
|
||||||
System.arraycopy(chs, start, content, count, len);
|
|
||||||
count += len;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean recycle() {
|
|
||||||
super.recycle();
|
|
||||||
this.count = 0;
|
|
||||||
this.specificObjectType = null;
|
|
||||||
if (this.content != null && this.content.length > DEFAULT_SIZE) {
|
|
||||||
this.content = new byte[DEFAULT_SIZE];
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonWriter clear() {
|
|
||||||
recycle();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.getClass().getSimpleName() + "[count=" + this.count + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
public final int count() {
|
|
||||||
return this.count;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeBoolean(boolean value) {
|
|
||||||
writeTo(value ? (byte) 1 : (byte) 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeByte(byte value) {
|
|
||||||
writeTo(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeByteArray(byte[] values) {
|
|
||||||
if (values == null) {
|
|
||||||
writeNull();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
writeArrayB(values.length, null, values);
|
|
||||||
boolean flag = false;
|
|
||||||
for (byte v : values) {
|
|
||||||
if (flag) {
|
|
||||||
writeArrayMark();
|
|
||||||
}
|
|
||||||
writeByte(v);
|
|
||||||
flag = true;
|
|
||||||
}
|
|
||||||
writeArrayE();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeChar(final char value) {
|
|
||||||
writeTo((byte) ((value & 0xFF00) >> 8), (byte) (value & 0xFF));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeShort(short value) {
|
|
||||||
writeTo((byte) (value >> 8), (byte) value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeInt(int value) {
|
|
||||||
writeTo((byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeLong(long value) {
|
|
||||||
writeTo(
|
|
||||||
(byte) (value >> 56),
|
|
||||||
(byte) (value >> 48),
|
|
||||||
(byte) (value >> 40),
|
|
||||||
(byte) (value >> 32),
|
|
||||||
(byte) (value >> 24),
|
|
||||||
(byte) (value >> 16),
|
|
||||||
(byte) (value >> 8),
|
|
||||||
(byte) value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeFloat(float value) {
|
|
||||||
writeInt(Float.floatToIntBits(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeDouble(double value) {
|
|
||||||
writeLong(Double.doubleToLongBits(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final boolean needWriteClassName() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeClassName(String clazz) {
|
|
||||||
writeStandardString(clazz == null ? "" : clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeObjectB(Object obj) {
|
|
||||||
super.writeObjectB(obj);
|
|
||||||
writeStandardString("");
|
|
||||||
writeShort(BsonReader.SIGN_OBJECTB);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeObjectE(Object obj) {
|
|
||||||
writeByte(BsonReader.SIGN_NONEXT);
|
|
||||||
writeShort(BsonReader.SIGN_OBJECTE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeField(EnMember member, String fieldName, Type fieldType, int fieldPos) {
|
|
||||||
writeByte(BsonReader.SIGN_HASNEXT);
|
|
||||||
writeStandardString(fieldName);
|
|
||||||
writeByte(BsonFactory.typeEnum(fieldType));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 对于类的字段名、枚举值这些长度一般不超过255且不会出现双字节字符的字符串采用writeSmallString处理, readSmallString用于读取
|
|
||||||
*
|
|
||||||
* @param value String值
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public final void writeStandardString(String value) {
|
|
||||||
if (value.isEmpty()) {
|
|
||||||
writeTo((byte) 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
char[] chars = Utility.charArray(value);
|
|
||||||
if (chars.length > 255) {
|
|
||||||
throw new ConvertException("'" + value + "' have very long length");
|
|
||||||
}
|
|
||||||
byte[] bytes = new byte[chars.length + 1];
|
|
||||||
bytes[0] = (byte) chars.length;
|
|
||||||
for (int i = 0; i < chars.length; i++) {
|
|
||||||
if (chars[i] > Byte.MAX_VALUE) {
|
|
||||||
throw new ConvertException("'" + value + "' have double-word");
|
|
||||||
}
|
|
||||||
bytes[i + 1] = (byte) chars[i];
|
|
||||||
}
|
|
||||||
writeTo(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeString(String value) {
|
|
||||||
if (value == null) {
|
|
||||||
writeInt(Reader.SIGN_NULL);
|
|
||||||
return;
|
|
||||||
} else if (value.isEmpty()) {
|
|
||||||
writeInt(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
byte[] bytes = Utility.encodeUTF8(value);
|
|
||||||
writeInt(bytes.length);
|
|
||||||
writeTo(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeWrapper(StringWrapper value) {
|
|
||||||
this.writeString(value == null ? null : value.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeNull() {
|
|
||||||
writeShort(Reader.SIGN_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeArrayB(int size, Encodeable componentEncoder, Object obj) {
|
|
||||||
writeInt(size);
|
|
||||||
if (componentEncoder != null && componentEncoder != ByteSimpledCoder.instance) {
|
|
||||||
writeByte(BsonFactory.typeEnum(componentEncoder.getType()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeArrayMark() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeArrayE() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeMapB(int size, Encodeable keyEncoder, Encodeable valueEncoder, Object obj) {
|
|
||||||
writeInt(size);
|
|
||||||
writeByte(BsonFactory.typeEnum(keyEncoder.getType()));
|
|
||||||
writeByte(BsonFactory.typeEnum(valueEncoder.getType()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeMapMark() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeMapE() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
/** 提供BSON的序列化和反解析功能 */
|
|
||||||
package org.redkale.convert.bson;
|
|
||||||
@@ -75,6 +75,7 @@ public class ProtobufByteBufferWriter extends ProtobufWriter {
|
|||||||
return this.buffers;
|
return this.buffers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ByteArray toByteArray() {
|
public ByteArray toByteArray() {
|
||||||
ByteArray array = new ByteArray();
|
ByteArray array = new ByteArray();
|
||||||
if (buffers != null) {
|
if (buffers != null) {
|
||||||
|
|||||||
@@ -169,6 +169,17 @@ public class ProtobufBytesWriter extends ProtobufWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ByteArray toByteArray() {
|
||||||
|
return new ByteArray(toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 类似writeTo(new byte[length])
|
||||||
|
public void writePlaceholderTo(final int length) {
|
||||||
|
expand(length);
|
||||||
|
count += length;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int expand(int len) {
|
protected int expand(int len) {
|
||||||
int newcount = count + len;
|
int newcount = count + len;
|
||||||
|
|||||||
@@ -657,7 +657,7 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
|||||||
decoder = factory.loadDecoder(type);
|
decoder = factory.loadDecoder(type);
|
||||||
this.lastDecodeable = decoder;
|
this.lastDecodeable = decoder;
|
||||||
}
|
}
|
||||||
if (!(decoder instanceof ObjectDecoder)) {
|
if (decoder == null) {
|
||||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||||
}
|
}
|
||||||
T rs = (T) decoder.convertFrom(reader);
|
T rs = (T) decoder.convertFrom(reader);
|
||||||
|
|||||||
@@ -40,16 +40,17 @@ public class ProtobufReader extends Reader {
|
|||||||
setBytes(bytes);
|
setBytes(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void setBytes(byte[] bytes) {
|
public final ProtobufReader setBytes(byte[] bytes) {
|
||||||
if (bytes == null) {
|
if (bytes == null) {
|
||||||
this.position = 0;
|
this.position = 0;
|
||||||
this.limit = 0;
|
this.limit = 0;
|
||||||
} else {
|
} else {
|
||||||
setBytes(bytes, 0, bytes.length);
|
setBytes(bytes, 0, bytes.length);
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void setBytes(byte[] bytes, int start, int len) {
|
public final ProtobufReader setBytes(byte[] bytes, int start, int len) {
|
||||||
if (bytes == null) {
|
if (bytes == null) {
|
||||||
this.position = 0;
|
this.position = 0;
|
||||||
this.limit = 0;
|
this.limit = 0;
|
||||||
@@ -58,6 +59,7 @@ public class ProtobufReader extends Reader {
|
|||||||
this.position = start - 1;
|
this.position = start - 1;
|
||||||
this.limit = start + len;
|
this.limit = start + len;
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void limit(int limit) {
|
public void limit(int limit) {
|
||||||
|
|||||||
@@ -106,6 +106,10 @@ public abstract class ProtobufWriter extends Writer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
recycle();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
super.recycle();
|
super.recycle();
|
||||||
@@ -121,6 +125,8 @@ public abstract class ProtobufWriter extends Writer {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract ByteArray toByteArray();
|
||||||
|
|
||||||
public abstract ProtobufWriter pollChild();
|
public abstract ProtobufWriter pollChild();
|
||||||
|
|
||||||
public abstract void offerChild(ProtobufWriter child);
|
public abstract void offerChild(ProtobufWriter child);
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ public class MessageRecord implements Serializable {
|
|||||||
|
|
||||||
public static final byte CTYPE_STRING = 1;
|
public static final byte CTYPE_STRING = 1;
|
||||||
|
|
||||||
// Bson bytes
|
// Protobuf bytes
|
||||||
public static final byte CTYPE_BSON = 2;
|
public static final byte CTYPE_PROTOBUF = 2;
|
||||||
|
|
||||||
// WebRequest
|
// WebRequest
|
||||||
public static final byte CTYPE_HTTP_REQUEST = 3;
|
public static final byte CTYPE_HTTP_REQUEST = 3;
|
||||||
@@ -344,9 +344,9 @@ public class MessageRecord implements Serializable {
|
|||||||
sb.append(",\"respTopic\":\"").append(this.respTopic).append("\"");
|
sb.append(",\"respTopic\":\"").append(this.respTopic).append("\"");
|
||||||
}
|
}
|
||||||
if (this.content != null) {
|
if (this.content != null) {
|
||||||
if (this.ctype == CTYPE_BSON && this.content.length > SncpHeader.HEADER_SUBSIZE) {
|
if (this.ctype == CTYPE_PROTOBUF && this.content.length > SncpHeader.HEADER_SUBSIZE) {
|
||||||
// int offset = new ByteArray(this.content).getChar(0) + 1; //循环占位符
|
// int offset = new ByteArray(this.content).getChar(0) + 1; //循环占位符
|
||||||
// Object rs = BsonConvert.root().convertFrom(Object.class, this.content, offset, this.content.length -
|
// Object rs = ProtobufConvert.root().convertFrom(Object.class, this.content, offset, this.content.length -
|
||||||
// offset);
|
// offset);
|
||||||
// sb.append(",\"content\":").append(rs);
|
// sb.append(",\"content\":").append(rs);
|
||||||
// SncpHeader包含不确定长度的信息,故不能再直接偏移读取
|
// SncpHeader包含不确定长度的信息,故不能再直接偏移读取
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.mq.spi;
|
package org.redkale.mq.spi;
|
||||||
|
|
||||||
import org.redkale.convert.bson.BsonWriter;
|
import org.redkale.convert.pb.ProtobufWriter;
|
||||||
import org.redkale.net.sncp.*;
|
import org.redkale.net.sncp.*;
|
||||||
import org.redkale.util.ByteArray;
|
import org.redkale.util.ByteArray;
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ public class SncpMessageResponse extends SncpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void finish(final int retcode, final BsonWriter out) {
|
public void finish(final int retcode, final ProtobufWriter out) {
|
||||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||||
if (out == null) {
|
if (out == null) {
|
||||||
final ByteArray result = new ByteArray(headerSize).putPlaceholder(headerSize);
|
final ByteArray result = new ByteArray(headerSize).putPlaceholder(headerSize);
|
||||||
@@ -36,7 +36,8 @@ public class SncpMessageResponse extends SncpResponse {
|
|||||||
messageClient
|
messageClient
|
||||||
.getProducer()
|
.getProducer()
|
||||||
.apply(messageClient.createMessageRecord(
|
.apply(messageClient.createMessageRecord(
|
||||||
message.getSeqid(), MessageRecord.CTYPE_BSON, message.getRespTopic(), null, (byte[]) null));
|
message.getSeqid(), MessageRecord.CTYPE_PROTOBUF, message.getRespTopic(), null, (byte[])
|
||||||
|
null));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ByteArray result = out.toByteArray();
|
final ByteArray result = out.toByteArray();
|
||||||
@@ -44,6 +45,10 @@ public class SncpMessageResponse extends SncpResponse {
|
|||||||
messageClient
|
messageClient
|
||||||
.getProducer()
|
.getProducer()
|
||||||
.apply(messageClient.createMessageRecord(
|
.apply(messageClient.createMessageRecord(
|
||||||
message.getSeqid(), MessageRecord.CTYPE_BSON, message.getRespTopic(), null, result.getBytes()));
|
message.getSeqid(),
|
||||||
|
MessageRecord.CTYPE_PROTOBUF,
|
||||||
|
message.getRespTopic(),
|
||||||
|
null,
|
||||||
|
result.getBytes()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ import java.nio.charset.*;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import org.redkale.convert.bson.*;
|
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -48,8 +49,8 @@ public class Context {
|
|||||||
// 日志Logger
|
// 日志Logger
|
||||||
protected final Logger logger;
|
protected final Logger logger;
|
||||||
|
|
||||||
// BSON操作工厂
|
// Protobuf操作工厂
|
||||||
protected final BsonFactory bsonFactory;
|
protected final ProtobufFactory protobufFactory;
|
||||||
|
|
||||||
// JSON操作工厂
|
// JSON操作工厂
|
||||||
protected final JsonFactory jsonFactory;
|
protected final JsonFactory jsonFactory;
|
||||||
@@ -138,7 +139,7 @@ public class Context {
|
|||||||
this.readTimeoutSeconds = readTimeoutSeconds;
|
this.readTimeoutSeconds = readTimeoutSeconds;
|
||||||
this.writeTimeoutSeconds = writeTimeoutSeconds;
|
this.writeTimeoutSeconds = writeTimeoutSeconds;
|
||||||
this.jsonFactory = JsonFactory.root();
|
this.jsonFactory = JsonFactory.root();
|
||||||
this.bsonFactory = BsonFactory.root();
|
this.protobufFactory = ProtobufFactory.root();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void executeDispatch(Request request, Response response) {
|
protected final void executeDispatch(Request request, Response response) {
|
||||||
@@ -247,8 +248,8 @@ public class Context {
|
|||||||
return jsonFactory.getConvert();
|
return jsonFactory.getConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BsonConvert getBsonConvert() {
|
public ProtobufConvert getProtobufConvert() {
|
||||||
return bsonFactory.getConvert();
|
return protobufFactory.getConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ContextConfig {
|
public static class ContextConfig {
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import java.nio.ByteBuffer;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import org.redkale.convert.ConvertDisabled;
|
import org.redkale.convert.ConvertDisabled;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.util.Creator;
|
import org.redkale.util.Creator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +27,7 @@ public abstract class Request<C extends Context> {
|
|||||||
|
|
||||||
protected final C context;
|
protected final C context;
|
||||||
|
|
||||||
protected final BsonConvert bsonConvert;
|
protected final ProtobufConvert protobufConvert;
|
||||||
|
|
||||||
protected final JsonConvert jsonConvert;
|
protected final JsonConvert jsonConvert;
|
||||||
|
|
||||||
@@ -62,13 +62,13 @@ public abstract class Request<C extends Context> {
|
|||||||
|
|
||||||
protected Request(C context) {
|
protected Request(C context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.bsonConvert = context.getBsonConvert();
|
this.protobufConvert = context.getProtobufConvert();
|
||||||
this.jsonConvert = context.getJsonConvert();
|
this.jsonConvert = context.getJsonConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Request(Request<C> request) {
|
protected Request(Request<C> request) {
|
||||||
this.context = request.context;
|
this.context = request.context;
|
||||||
this.bsonConvert = request.bsonConvert;
|
this.protobufConvert = request.protobufConvert;
|
||||||
this.jsonConvert = request.jsonConvert;
|
this.jsonConvert = request.jsonConvert;
|
||||||
this.createTime = request.createTime;
|
this.createTime = request.createTime;
|
||||||
this.keepAlive = request.keepAlive;
|
this.keepAlive = request.keepAlive;
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ import org.redkale.asm.*;
|
|||||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||||
import static org.redkale.asm.Opcodes.*;
|
import static org.redkale.asm.Opcodes.*;
|
||||||
import org.redkale.asm.Type;
|
import org.redkale.asm.Type;
|
||||||
import org.redkale.convert.Convert;
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.inject.Resourcable;
|
import org.redkale.inject.Resourcable;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.mq.spi.MessageAgent;
|
import org.redkale.mq.spi.MessageAgent;
|
||||||
@@ -156,7 +155,7 @@ public abstract class Sncp {
|
|||||||
String resourceName,
|
String resourceName,
|
||||||
Class<T> resourceServiceType,
|
Class<T> resourceServiceType,
|
||||||
Class<T> serviceImplClass,
|
Class<T> serviceImplClass,
|
||||||
Convert convert,
|
ProtobufConvert convert,
|
||||||
SncpRpcGroups sncpRpcGroups,
|
SncpRpcGroups sncpRpcGroups,
|
||||||
SncpClient sncpClient,
|
SncpClient sncpClient,
|
||||||
MessageAgent messageAgent,
|
MessageAgent messageAgent,
|
||||||
@@ -930,7 +929,7 @@ public abstract class Sncp {
|
|||||||
name,
|
name,
|
||||||
getResourceType(serviceTypeOrImplClass),
|
getResourceType(serviceTypeOrImplClass),
|
||||||
serviceTypeOrImplClass,
|
serviceTypeOrImplClass,
|
||||||
BsonConvert.root(),
|
ProtobufConvert.root(),
|
||||||
sncpRpcGroups,
|
sncpRpcGroups,
|
||||||
client,
|
client,
|
||||||
agent,
|
agent,
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class SncpDispatcherServlet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> SncpServlet removeSncpServlet(Service service) {
|
public SncpServlet removeSncpServlet(Service service) {
|
||||||
SncpServlet rs = null;
|
SncpServlet rs = null;
|
||||||
updateLock.lock();
|
updateLock.lock();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ import java.nio.channels.CompletionHandler;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import org.redkale.convert.*;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufWriter;
|
||||||
import org.redkale.mq.spi.MessageAgent;
|
import org.redkale.mq.spi.MessageAgent;
|
||||||
import org.redkale.mq.spi.MessageClient;
|
import org.redkale.mq.spi.MessageClient;
|
||||||
import org.redkale.mq.spi.MessageRecord;
|
import org.redkale.mq.spi.MessageRecord;
|
||||||
@@ -59,8 +60,8 @@ public class SncpRemoteInfo<S extends Service> {
|
|||||||
// 非MQ模式下此字段才有值, 可能为null
|
// 非MQ模式下此字段才有值, 可能为null
|
||||||
protected Set<InetSocketAddress> remoteAddresses;
|
protected Set<InetSocketAddress> remoteAddresses;
|
||||||
|
|
||||||
// 默认值: BsonConvert.root()
|
// 默认值: ProtobufConvert.root()
|
||||||
protected final Convert convert;
|
protected final ProtobufConvert convert;
|
||||||
|
|
||||||
// MQ模式下此字段才有值
|
// MQ模式下此字段才有值
|
||||||
protected final String topic;
|
protected final String topic;
|
||||||
@@ -75,7 +76,7 @@ public class SncpRemoteInfo<S extends Service> {
|
|||||||
String resourceName,
|
String resourceName,
|
||||||
Class<S> resourceType,
|
Class<S> resourceType,
|
||||||
Class<S> serviceImplClass,
|
Class<S> serviceImplClass,
|
||||||
Convert convert,
|
ProtobufConvert convert,
|
||||||
SncpRpcGroups sncpRpcGroups,
|
SncpRpcGroups sncpRpcGroups,
|
||||||
SncpClient sncpClient,
|
SncpClient sncpClient,
|
||||||
MessageAgent messageAgent,
|
MessageAgent messageAgent,
|
||||||
@@ -192,7 +193,7 @@ public class SncpRemoteInfo<S extends Service> {
|
|||||||
request.writeTo(null, array);
|
request.writeTo(null, array);
|
||||||
MessageRecord message = messageAgent
|
MessageRecord message = messageAgent
|
||||||
.getSncpMessageClient()
|
.getSncpMessageClient()
|
||||||
.createMessageRecord(MessageRecord.CTYPE_BSON, targetTopic, null, array.getBytes());
|
.createMessageRecord(MessageRecord.CTYPE_PROTOBUF, targetTopic, null, array.getBytes());
|
||||||
final String tt = targetTopic;
|
final String tt = targetTopic;
|
||||||
message.localActionName(action.actionName());
|
message.localActionName(action.actionName());
|
||||||
message.localParams(params);
|
message.localParams(params);
|
||||||
@@ -254,7 +255,7 @@ public class SncpRemoteInfo<S extends Service> {
|
|||||||
}
|
}
|
||||||
byte[] body = null;
|
byte[] body = null;
|
||||||
if (myParamTypes.length > 0) {
|
if (myParamTypes.length > 0) {
|
||||||
Writer writer = convert.pollWriter();
|
ProtobufWriter writer = convert.pollWriter();
|
||||||
for (int i = 0; i < params.length; i++) { // service方法的参数
|
for (int i = 0; i < params.length; i++) { // service方法的参数
|
||||||
convert.convertTo(
|
convert.convertTo(
|
||||||
writer,
|
writer,
|
||||||
@@ -263,7 +264,7 @@ public class SncpRemoteInfo<S extends Service> {
|
|||||||
: myParamTypes[i],
|
: myParamTypes[i],
|
||||||
params[i]);
|
params[i]);
|
||||||
}
|
}
|
||||||
body = ((ByteTuple) writer).toArray();
|
body = writer.toByteArray().content();
|
||||||
convert.offerWriter(writer);
|
convert.offerWriter(writer);
|
||||||
}
|
}
|
||||||
final SncpClientRequest request = new SncpClientRequest();
|
final SncpClientRequest request = new SncpClientRequest();
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.util.Objects;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.convert.bson.BsonReader;
|
import org.redkale.convert.pb.ProtobufReader;
|
||||||
import org.redkale.net.Request;
|
import org.redkale.net.Request;
|
||||||
import static org.redkale.net.client.ClientRequest.EMPTY_TRACEID;
|
import static org.redkale.net.client.ClientRequest.EMPTY_TRACEID;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
@@ -36,7 +36,7 @@ public class SncpRequest extends Request<SncpContext> {
|
|||||||
|
|
||||||
private static final Function<String, ByteArray> tbaFunc = s -> new ByteArray();
|
private static final Function<String, ByteArray> tbaFunc = s -> new ByteArray();
|
||||||
|
|
||||||
protected final BsonReader reader = new BsonReader();
|
protected final ProtobufReader reader = new ProtobufReader();
|
||||||
|
|
||||||
protected int readState = READ_STATE_ROUTE;
|
protected int readState = READ_STATE_ROUTE;
|
||||||
|
|
||||||
@@ -80,15 +80,18 @@ public class SncpRequest extends Request<SncpContext> {
|
|||||||
}
|
}
|
||||||
if (this.headerLength < SncpHeader.HEADER_SUBSIZE) {
|
if (this.headerLength < SncpHeader.HEADER_SUBSIZE) {
|
||||||
context.getLogger()
|
context.getLogger()
|
||||||
.log(Level.WARNING,
|
.log(
|
||||||
|
Level.WARNING,
|
||||||
"sncp header.length must more " + SncpHeader.HEADER_SUBSIZE + ", but "
|
"sncp header.length must more " + SncpHeader.HEADER_SUBSIZE + ", but "
|
||||||
+ this.headerLength);
|
+ this.headerLength);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (this.headerLength > context.getMaxHeader()) {
|
if (this.headerLength > context.getMaxHeader()) {
|
||||||
context.getLogger()
|
context.getLogger()
|
||||||
.log(Level.WARNING,
|
.log(
|
||||||
"sncp header.length must lower " + context.getMaxHeader() + ", but " + this.headerLength);
|
Level.WARNING,
|
||||||
|
"sncp header.length must lower " + context.getMaxHeader() + ", but "
|
||||||
|
+ this.headerLength);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
this.readState = READ_STATE_HEADER;
|
this.readState = READ_STATE_HEADER;
|
||||||
@@ -199,7 +202,7 @@ public class SncpRequest extends Request<SncpContext> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Convert getConvert() {
|
public Convert getConvert() {
|
||||||
return context.getBsonConvert();
|
return context.getProtobufConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reader getReader() {
|
public Reader getReader() {
|
||||||
|
|||||||
@@ -5,15 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.net.sncp;
|
package org.redkale.net.sncp;
|
||||||
|
|
||||||
import static org.redkale.net.sncp.SncpHeader.KEEPALIVE_OFF;
|
|
||||||
import static org.redkale.net.sncp.SncpHeader.KEEPALIVE_ON;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.nio.channels.CompletionHandler;
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import org.redkale.annotation.ClassDepends;
|
import org.redkale.annotation.ClassDepends;
|
||||||
import org.redkale.convert.bson.BsonWriter;
|
import org.redkale.convert.pb.ProtobufBytesWriter;
|
||||||
|
import org.redkale.convert.pb.ProtobufWriter;
|
||||||
import org.redkale.net.Response;
|
import org.redkale.net.Response;
|
||||||
|
import static org.redkale.net.sncp.SncpHeader.KEEPALIVE_OFF;
|
||||||
|
import static org.redkale.net.sncp.SncpHeader.KEEPALIVE_ON;
|
||||||
import org.redkale.util.ByteArray;
|
import org.redkale.util.ByteArray;
|
||||||
import org.redkale.util.Traces;
|
import org.redkale.util.Traces;
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
|||||||
|
|
||||||
final int addrPort;
|
final int addrPort;
|
||||||
|
|
||||||
protected final BsonWriter writer = new BsonWriter();
|
protected final ProtobufWriter writer = new ProtobufBytesWriter();
|
||||||
|
|
||||||
protected final CompletionHandler realHandler = new CompletionHandler() {
|
protected final CompletionHandler realHandler = new CompletionHandler() {
|
||||||
@Override
|
@Override
|
||||||
@@ -106,7 +106,7 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
|||||||
return super.recycle();
|
return super.recycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BsonWriter getBsonWriter() {
|
public ProtobufWriter getWriter() {
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,8 +141,8 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
|||||||
|
|
||||||
public final void finishVoid() {
|
public final void finishVoid() {
|
||||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||||
BsonWriter out = getBsonWriter();
|
ProtobufWriter out = getWriter();
|
||||||
out.writePlaceholderTo(headerSize);
|
((ProtobufBytesWriter) out).writePlaceholderTo(headerSize);
|
||||||
finish(0, out);
|
finish(0, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,17 +170,17 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
|||||||
|
|
||||||
public final void finish(final Type type, final Object result) {
|
public final void finish(final Type type, final Object result) {
|
||||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||||
BsonWriter out = getBsonWriter();
|
ProtobufWriter out = getWriter();
|
||||||
out.writePlaceholderTo(headerSize);
|
((ProtobufBytesWriter) out).writePlaceholderTo(headerSize);
|
||||||
if (result != null || type != Void.class) {
|
if (result != null || type != Void.class) {
|
||||||
out.writeByte((byte) 0); // body的第一个字节为0,表示返回结果对象,而不是参数回调对象
|
out.writeByte((byte) 0); // body的第一个字节为0,表示返回结果对象,而不是参数回调对象
|
||||||
context.getBsonConvert().convertTo(out, type, result);
|
context.getProtobufConvert().convertTo(out, type, result);
|
||||||
}
|
}
|
||||||
finish(0, out);
|
finish(0, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用此方法时out已写入SncpHeader的占位空间
|
// 调用此方法时out已写入SncpHeader的占位空间
|
||||||
public void finish(final int retcode, final BsonWriter out) {
|
public void finish(final int retcode, final ProtobufWriter out) {
|
||||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||||
if (out == null) {
|
if (out == null) {
|
||||||
final ByteArray array = new ByteArray(headerSize).putPlaceholder(headerSize);
|
final ByteArray array = new ByteArray(headerSize).putPlaceholder(headerSize);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import org.redkale.annotation.NonBlocking;
|
|||||||
import org.redkale.asm.*;
|
import org.redkale.asm.*;
|
||||||
import org.redkale.asm.Type;
|
import org.redkale.asm.Type;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.convert.bson.BsonFactory;
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
import org.redkale.service.Service;
|
import org.redkale.service.Service;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
@@ -764,18 +764,18 @@ public class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse>
|
|||||||
if (t.toString().startsWith("java.lang.")) {
|
if (t.toString().startsWith("java.lang.")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
BsonFactory.root().loadDecoder(t);
|
ProtobufFactory.root().loadDecoder(t);
|
||||||
}
|
}
|
||||||
if (originalReturnType != void.class && originalReturnType != Void.class) {
|
if (originalReturnType != void.class && originalReturnType != Void.class) {
|
||||||
if (boolReturnTypeFuture && method.getReturnType() != method.getGenericReturnType()) {
|
if (boolReturnTypeFuture && method.getReturnType() != method.getGenericReturnType()) {
|
||||||
java.lang.reflect.Type t =
|
java.lang.reflect.Type t =
|
||||||
((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
|
((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
|
||||||
if (t != Void.class && t != java.lang.reflect.Type.class) {
|
if (t != Void.class && t != java.lang.reflect.Type.class) {
|
||||||
BsonFactory.root().loadEncoder(t);
|
ProtobufFactory.root().loadEncoder(t);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
BsonFactory.root().loadEncoder(originalReturnType);
|
ProtobufFactory.root().loadEncoder(originalReturnType);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(method);
|
System.err.println(method);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,124 +1,124 @@
|
|||||||
/*
|
///*
|
||||||
* Copyright (c) 2016-2116 Redkale
|
// * Copyright (c) 2016-2116 Redkale
|
||||||
* All rights reserved.
|
// * All rights reserved.
|
||||||
*/
|
// */
|
||||||
package org.redkale.util;
|
//package org.redkale.util;
|
||||||
|
//
|
||||||
import java.util.concurrent.Executor;
|
//import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.ExecutorService;
|
//import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
//import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ThreadFactory;
|
//import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.function.Function;
|
//import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
//import java.util.function.Supplier;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
*
|
// *
|
||||||
* @author zhangjx
|
// * @author zhangjx
|
||||||
*/
|
// */
|
||||||
class Jdk21Inners {
|
//class Jdk21Inners {
|
||||||
|
//
|
||||||
static {
|
// static {
|
||||||
// 加载时进行可用性判断
|
// // 加载时进行可用性判断
|
||||||
Thread.currentThread().isVirtual();
|
// Thread.currentThread().isVirtual();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private Jdk21Inners() {
|
// private Jdk21Inners() {
|
||||||
// do nothing
|
// // do nothing
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static Executor createExecutor() {
|
// public static Executor createExecutor() {
|
||||||
return new VirtualExecutor();
|
// return new VirtualExecutor();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static Function<String, ExecutorService> createPoolFunction() {
|
// public static Function<String, ExecutorService> createPoolFunction() {
|
||||||
return new VirtualPoolFunction();
|
// return new VirtualPoolFunction();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static Function<Supplier, ThreadLocal> createThreadLocalFunction() {
|
// public static Function<Supplier, ThreadLocal> createThreadLocalFunction() {
|
||||||
return new VirtualThreadLocal(() -> null);
|
// return new VirtualThreadLocal(() -> null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static Function<String, ThreadFactory> createThreadFactoryFunction() {
|
// public static Function<String, ThreadFactory> createThreadFactoryFunction() {
|
||||||
return new VirtualThreadFactory("");
|
// return new VirtualThreadFactory("");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
static class VirtualExecutor implements Executor {
|
// static class VirtualExecutor implements Executor {
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void execute(Runnable t) {
|
// public void execute(Runnable t) {
|
||||||
Thread.ofVirtual().name("Redkale-VirtualThread").start(t);
|
// Thread.ofVirtual().name("Redkale-VirtualThread").start(t);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
static class VirtualPoolFunction implements Function<String, ExecutorService> {
|
// static class VirtualPoolFunction implements Function<String, ExecutorService> {
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public ExecutorService apply(String threadNameFormat) {
|
// public ExecutorService apply(String threadNameFormat) {
|
||||||
final ThreadFactory factory = Thread.ofVirtual().factory();
|
// final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||||
final String threadName = String.format(threadNameFormat, "Virtual");
|
// final String threadName = String.format(threadNameFormat, "Virtual");
|
||||||
return Executors.newThreadPerTaskExecutor(r -> {
|
// return Executors.newThreadPerTaskExecutor(r -> {
|
||||||
Thread t = factory.newThread(r);
|
// Thread t = factory.newThread(r);
|
||||||
t.setName(threadName);
|
// t.setName(threadName);
|
||||||
return t;
|
// return t;
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
static class VirtualThreadLocal<T> extends ThreadLocal<T> implements Function<Supplier<T>, ThreadLocal<T>> {
|
// static class VirtualThreadLocal<T> extends ThreadLocal<T> implements Function<Supplier<T>, ThreadLocal<T>> {
|
||||||
|
//
|
||||||
private final Supplier<T> supplier;
|
// private final Supplier<T> supplier;
|
||||||
|
//
|
||||||
public VirtualThreadLocal(Supplier<T> supplier) {
|
// public VirtualThreadLocal(Supplier<T> supplier) {
|
||||||
this.supplier = supplier;
|
// this.supplier = supplier;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public ThreadLocal<T> apply(Supplier<T> supplier) {
|
// public ThreadLocal<T> apply(Supplier<T> supplier) {
|
||||||
return new VirtualThreadLocal<>(supplier);
|
// return new VirtualThreadLocal<>(supplier);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
protected T initialValue() {
|
// protected T initialValue() {
|
||||||
return supplier.get();
|
// return supplier.get();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void set(T value) {
|
// public void set(T value) {
|
||||||
Thread t = Thread.currentThread();
|
// Thread t = Thread.currentThread();
|
||||||
if (!t.isVirtual()) {
|
// if (!t.isVirtual()) {
|
||||||
super.set(value);
|
// super.set(value);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public T get() {
|
// public T get() {
|
||||||
Thread t = Thread.currentThread();
|
// Thread t = Thread.currentThread();
|
||||||
return t.isVirtual() ? initialValue() : super.get();
|
// return t.isVirtual() ? initialValue() : super.get();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
static class VirtualThreadFactory implements ThreadFactory, Function<String, ThreadFactory> {
|
// static class VirtualThreadFactory implements ThreadFactory, Function<String, ThreadFactory> {
|
||||||
|
//
|
||||||
private final ThreadFactory factory = Thread.ofVirtual().factory();
|
// private final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||||
|
//
|
||||||
private final String name;
|
// private final String name;
|
||||||
|
//
|
||||||
public VirtualThreadFactory(String name) {
|
// public VirtualThreadFactory(String name) {
|
||||||
this.name = name;
|
// this.name = name;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public ThreadFactory apply(String name) {
|
// public ThreadFactory apply(String name) {
|
||||||
return new VirtualThreadFactory(name);
|
// return new VirtualThreadFactory(name);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public Thread newThread(Runnable r) {
|
// public Thread newThread(Runnable r) {
|
||||||
Thread t = factory.newThread(r);
|
// Thread t = factory.newThread(r);
|
||||||
if (name != null) {
|
// if (name != null) {
|
||||||
t.setName(name);
|
// t.setName(name);
|
||||||
}
|
// }
|
||||||
return t;
|
// return t;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
"org.redkale.cluster",
|
"org.redkale.cluster",
|
||||||
"org.redkale.cluster.spi",
|
"org.redkale.cluster.spi",
|
||||||
"org.redkale.convert",
|
"org.redkale.convert",
|
||||||
"org.redkale.convert.bson",
|
|
||||||
"org.redkale.convert.ext",
|
"org.redkale.convert.ext",
|
||||||
"org.redkale.convert.json",
|
"org.redkale.convert.json",
|
||||||
"org.redkale.convert.pb",
|
"org.redkale.convert.pb",
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ import java.math.BigInteger;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.redkale.convert.ConvertCoder;
|
import org.redkale.convert.ConvertCoder;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
import org.redkale.convert.ConvertColumn;
|
||||||
import org.redkale.convert.ext.BigIntegerSimpledCoder.BigIntegerHexJsonSimpledCoder;
|
import org.redkale.convert.ext.BigIntegerSimpledCoder.BigIntegerHexJsonSimpledCoder;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
|
|
||||||
/** @author zhangjx */
|
/** @author zhangjx */
|
||||||
public class ConvertCoderTest {
|
public class ConvertCoderTest {
|
||||||
@@ -44,37 +45,47 @@ public class ConvertCoderTest {
|
|||||||
BigMessage msg12 = convert.convertFrom(BigMessage.class, json);
|
BigMessage msg12 = convert.convertFrom(BigMessage.class, json);
|
||||||
Assertions.assertEquals(convert.convertTo(msg12), json);
|
Assertions.assertEquals(convert.convertTo(msg12), json);
|
||||||
|
|
||||||
byte[] bs1 = BsonConvert.root().convertTo(msg);
|
byte[] bs1 = ProtobufConvert.root().convertTo(msg);
|
||||||
byte[] bs2 = BsonConvert.root().convertTo(msg2);
|
byte[] bs2 = ProtobufConvert.root().convertTo(msg2);
|
||||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BigMessage {
|
public static class BigMessage {
|
||||||
|
|
||||||
|
@ConvertColumn(index = 1)
|
||||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||||
public BigInteger big;
|
public BigInteger big;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 2)
|
||||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||||
public BigInteger big2;
|
public BigInteger big2;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 3)
|
||||||
public BigInteger big3;
|
public BigInteger big3;
|
||||||
|
|
||||||
public int num1;
|
@ConvertColumn(index = 4)
|
||||||
|
|
||||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||||
public Map<String, BigInteger> map;
|
public Map<String, BigInteger> map;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 5)
|
||||||
|
public int num1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BigMessage2 {
|
public static class BigMessage2 {
|
||||||
|
|
||||||
|
@ConvertColumn(index = 1)
|
||||||
public BigInteger big;
|
public BigInteger big;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 2)
|
||||||
public BigInteger big2;
|
public BigInteger big2;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 3)
|
||||||
public BigInteger big3;
|
public BigInteger big3;
|
||||||
|
|
||||||
public int num1;
|
@ConvertColumn(index = 4)
|
||||||
|
|
||||||
public Map<String, BigInteger> map;
|
public Map<String, BigInteger> map;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 5)
|
||||||
|
public int num1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.redkale.convert.ConvertColumn;
|
import org.redkale.convert.ConvertColumn;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.convert.pb.ProtobufConvert;
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.util.TypeToken;
|
import org.redkale.util.TypeToken;
|
||||||
@@ -39,9 +38,6 @@ public class GenericEntityTest {
|
|||||||
test.runPb1();
|
test.runPb1();
|
||||||
test.runPb2();
|
test.runPb2();
|
||||||
test.runPb3();
|
test.runPb3();
|
||||||
test.runBson1();
|
|
||||||
test.runBson2();
|
|
||||||
test.runBson3();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -123,51 +119,6 @@ public class GenericEntityTest {
|
|||||||
Assertions.assertArrayEquals(bs, bs2);
|
Assertions.assertArrayEquals(bs, bs2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runBson1() throws Exception {
|
|
||||||
System.out.println("-------------------- runBson1 ---------------------------------");
|
|
||||||
BsonConvert convert = BsonConvert.root();
|
|
||||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
|
||||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
|
||||||
Utility.println("bson", bs);
|
|
||||||
String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
|
||||||
Assertions.assertEquals(JSON, rs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runBson2() throws Exception {
|
|
||||||
System.out.println("-------------------- runBson2 ---------------------------------");
|
|
||||||
BsonConvert convert = BsonConvert.root();
|
|
||||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
|
||||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
|
||||||
Utility.println("bson1 ", bs);
|
|
||||||
ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
|
||||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
|
||||||
Assertions.assertEquals(JSON, rs.toString());
|
|
||||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
|
||||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
|
||||||
byte[] bs2 = ConvertHelper.toBytes(buffers);
|
|
||||||
Utility.println("bson2 ", bs2);
|
|
||||||
Assertions.assertArrayEquals(bs, bs2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runBson3() throws Exception {
|
|
||||||
System.out.println("-------------------- runBson3 ---------------------------------");
|
|
||||||
BsonConvert convert = BsonConvert.root();
|
|
||||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
|
||||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
|
||||||
Utility.println("bson1 ", bs);
|
|
||||||
InputStream in = ConvertHelper.createInputStream(bs);
|
|
||||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
|
||||||
Assertions.assertEquals(JSON, rs.toString());
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
convert.convertTo(out, ENTITY_TYPE, rs);
|
|
||||||
byte[] bs2 = out.toByteArray();
|
|
||||||
Utility.println("bson2 ", bs2);
|
|
||||||
Assertions.assertArrayEquals(bs, bs2);
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] createBytes() {
|
private byte[] createBytes() {
|
||||||
return JSON.getBytes(StandardCharsets.UTF_8);
|
return JSON.getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,16 @@
|
|||||||
package org.redkale.test.convert;
|
package org.redkale.test.convert;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.redkale.convert.ConvertColumn;
|
||||||
import org.redkale.convert.DeMember;
|
import org.redkale.convert.DeMember;
|
||||||
import org.redkale.convert.DeMemberInfo;
|
import org.redkale.convert.DeMemberInfo;
|
||||||
import org.redkale.convert.EnMember;
|
import org.redkale.convert.EnMember;
|
||||||
import org.redkale.convert.Reader;
|
import org.redkale.convert.Reader;
|
||||||
import org.redkale.convert.SimpledCoder;
|
import org.redkale.convert.SimpledCoder;
|
||||||
import org.redkale.convert.Writer;
|
import org.redkale.convert.Writer;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.bson.BsonFactory;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.util.Utility;
|
import org.redkale.util.Utility;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,7 +35,7 @@ public class InnerCoderEntityTest {
|
|||||||
System.out.println(json);
|
System.out.println(json);
|
||||||
System.out.println(convert.convertFrom(InnerCoderEntity.class, json).toString());
|
System.out.println(convert.convertFrom(InnerCoderEntity.class, json).toString());
|
||||||
|
|
||||||
final BsonConvert convert2 = BsonFactory.root().getConvert();
|
final ProtobufConvert convert2 = ProtobufFactory.root().getConvert();
|
||||||
byte[] bs = convert2.convertTo(InnerCoderEntity.class, null);
|
byte[] bs = convert2.convertTo(InnerCoderEntity.class, null);
|
||||||
Utility.println("--", bs);
|
Utility.println("--", bs);
|
||||||
InnerCoderEntity r = convert2.convertFrom(InnerCoderEntity.class, bs);
|
InnerCoderEntity r = convert2.convertFrom(InnerCoderEntity.class, bs);
|
||||||
@@ -42,11 +43,12 @@ public class InnerCoderEntityTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class InnerCoderEntity {
|
public static class InnerCoderEntity {
|
||||||
|
@ConvertColumn(index = 1)
|
||||||
private final String val;
|
|
||||||
|
|
||||||
private final int id;
|
private final int id;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 2)
|
||||||
|
private final String val;
|
||||||
|
|
||||||
private InnerCoderEntity(int id, String value) {
|
private InnerCoderEntity(int id, String value) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.val = value;
|
this.val = value;
|
||||||
@@ -58,8 +60,8 @@ public class InnerCoderEntityTest {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 该方法提供给Convert组件自动加载。 1) 方法名可以随意。 2) 方法必须是static 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。 —3.1)
|
* 该方法提供给Convert组件自动加载。 1) 方法名可以随意。 2) 方法必须是static 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。 —3.1)
|
||||||
* 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON和BSON。 —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
* 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON和PROTOBUF。 —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
||||||
* —3.3) 参数类型为org.redkale.convert.bson.BsonFactory 表示仅适合BSON。
|
* —3.3) 参数类型为org.redkale.convert.pb.ProtobufFactory 表示仅适合PROTOBUF。
|
||||||
* 4)方法的返回类型必须是org.redkale.convert.Decodeable/org.redkale.convert.Encodeable/org.redkale.convert.SimpledCoder
|
* 4)方法的返回类型必须是org.redkale.convert.Decodeable/org.redkale.convert.Encodeable/org.redkale.convert.SimpledCoder
|
||||||
* 若返回类型不是org.redkale.convert.SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
* 若返回类型不是org.redkale.convert.SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,288 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.test.convert.bson;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.*;
|
|
||||||
import org.junit.jupiter.api.*;
|
|
||||||
import org.redkale.annotation.ConstructorParameters;
|
|
||||||
import org.redkale.convert.bson.*;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
|
||||||
import org.redkale.persistence.*;
|
|
||||||
import org.redkale.test.convert.SimpleChildEntity;
|
|
||||||
import org.redkale.test.convert.SimpleEntity;
|
|
||||||
import org.redkale.util.*;
|
|
||||||
|
|
||||||
/** @author zhangjx */
|
|
||||||
public class BsonMainTest {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
|
||||||
BsonMainTest test = new BsonMainTest();
|
|
||||||
test.run1();
|
|
||||||
test.run2();
|
|
||||||
test.run3();
|
|
||||||
test.run4();
|
|
||||||
test.run5();
|
|
||||||
test.run6();
|
|
||||||
test.run7();
|
|
||||||
test.run8();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run1() throws Throwable {
|
|
||||||
Serializable[] sers = new Serializable[] {"aaa", 4};
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
byte[] bytes = convert.convertTo(sers);
|
|
||||||
Utility.println("---", bytes);
|
|
||||||
byte[] checks = new byte[] {
|
|
||||||
0x00, 0x00, 0x00, 0x02, 0x7f, 0x01, 0x41, 0x00, 0x00, 0x00, 0x03, 0x61, 0x61, 0x61, 0x01, 0x69, 0x00, 0x00,
|
|
||||||
0x00, 0x04
|
|
||||||
};
|
|
||||||
Assertions.assertArrayEquals(checks, bytes);
|
|
||||||
Serializable[] a = convert.convertFrom(Serializable[].class, bytes);
|
|
||||||
Assertions.assertEquals("[aaa, 4]", Arrays.toString(a));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run2() throws Exception {
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
|
||||||
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
|
|
||||||
System.out.println("长度: " + bytes.length);
|
|
||||||
Assertions.assertEquals(258, bytes.length);
|
|
||||||
BsonByteBufferWriter writer = convert.pollWriter(() -> ByteBuffer.allocate(1));
|
|
||||||
convert.convertTo(writer, SimpleEntity.class, entry);
|
|
||||||
ByteBuffer[] buffers = writer.toBuffers();
|
|
||||||
int len = 0;
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
for (ByteBuffer b : buffers) {
|
|
||||||
len += b.remaining();
|
|
||||||
byte[] ts = new byte[b.remaining()];
|
|
||||||
b.get(ts);
|
|
||||||
out.write(ts);
|
|
||||||
b.flip();
|
|
||||||
}
|
|
||||||
System.out.println("长度: " + len);
|
|
||||||
Assertions.assertEquals(258, len);
|
|
||||||
SimpleChildEntity entry2 = convert.convertFrom(SimpleChildEntity.class, buffers);
|
|
||||||
System.out.println(entry);
|
|
||||||
Assertions.assertEquals(entry.toString(), entry2.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run3() throws Exception {
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
|
||||||
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
|
|
||||||
Utility.println(null, bytes);
|
|
||||||
System.out.println(JsonConvert.root().convertTo(entry));
|
|
||||||
SimpleEntity rs = convert.convertFrom(SimpleEntity.class, bytes);
|
|
||||||
Assertions.assertEquals(JsonConvert.root().convertTo(entry), rs.toString());
|
|
||||||
|
|
||||||
ComplextEntity bean = new ComplextEntity();
|
|
||||||
byte[] bytes2 = convert.convertTo(Object.class, bean);
|
|
||||||
final int len = bytes2.length;
|
|
||||||
BsonByteBufferWriter writer = convert.pollWriter(() -> ByteBuffer.allocate(len / 2));
|
|
||||||
convert.convertTo(writer, bean);
|
|
||||||
bytes2 = writer.toByteArray().getBytes();
|
|
||||||
System.out.println(convert.convertFrom(ComplextEntity.class, bytes2).toString());
|
|
||||||
Assertions.assertEquals(
|
|
||||||
"{\"chname\":\"\",\"flag\":true,\"userid\":0}",
|
|
||||||
convert.convertFrom(ComplextEntity.class, bytes2).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run4() throws Exception {
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
convert.convertTo(out, SimpleEntity.class, entry);
|
|
||||||
byte[] bytes = out.toByteArray();
|
|
||||||
Utility.println(null, bytes);
|
|
||||||
SimpleEntity rs = convert.convertFrom(SimpleEntity.class, new ByteArrayInputStream(bytes));
|
|
||||||
System.out.println(rs.toString());
|
|
||||||
Assertions.assertEquals(JsonConvert.root().convertTo(entry), rs.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run5() throws Exception {
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
|
|
||||||
LinkedHashMap map = new LinkedHashMap();
|
|
||||||
map.put("1", 1);
|
|
||||||
map.put("2", "a2");
|
|
||||||
byte[] bs = convert.convertTo(Object.class, map);
|
|
||||||
Object mapobj = convert.convertFrom(Object.class, bs);
|
|
||||||
System.out.println(mapobj);
|
|
||||||
Assertions.assertEquals("{1=1, 2=a2}", mapobj.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run6() throws Exception {
|
|
||||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
|
||||||
|
|
||||||
Optional<String> val = Optional.ofNullable("haha");
|
|
||||||
byte[] bs = convert.convertTo(val);
|
|
||||||
Object obj = convert.convertFrom(Optional.class, bs);
|
|
||||||
System.out.println(obj);
|
|
||||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
|
||||||
bs = convert.convertTo(Object.class, val);
|
|
||||||
obj = convert.convertFrom(Object.class, bs);
|
|
||||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
|
||||||
bs = convert.convertTo(new TypeToken<Optional<String>>() {}.getType(), val);
|
|
||||||
obj = convert.convertFrom(new TypeToken<Optional<String>>() {}.getType(), bs);
|
|
||||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
|
||||||
System.out.println(JsonConvert.root().convertTo(val));
|
|
||||||
Assertions.assertEquals("\"haha\"", JsonConvert.root().convertTo(val));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run7() throws Throwable {
|
|
||||||
Two two = new Two();
|
|
||||||
two.setKey("key111");
|
|
||||||
two.setCode(12345);
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
list.add("haha");
|
|
||||||
two.setList(list);
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put("222", "333");
|
|
||||||
two.setStringMap(map);
|
|
||||||
|
|
||||||
List<ConvertRecord> records = new ArrayList<>();
|
|
||||||
records.add(ConvertRecord.createDefault());
|
|
||||||
two.setRecords(records);
|
|
||||||
|
|
||||||
Map<String, ConvertRecord> rmap = new HashMap<>();
|
|
||||||
rmap.put("222", ConvertRecord.createDefault());
|
|
||||||
two.setRecordMap(rmap);
|
|
||||||
|
|
||||||
byte[] bs = BsonFactory.root().getConvert().convertTo(two);
|
|
||||||
|
|
||||||
One one = BsonFactory.root().getConvert().convertFrom(One.class, bs);
|
|
||||||
System.out.println(one);
|
|
||||||
Assertions.assertEquals(
|
|
||||||
"{\"bytes\":[3,4,5],\"code\":12345,\"ints\":[3000,4000,5000],\"key\":\"key111\"}", one.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void run8() throws Exception {
|
|
||||||
final JsonConvert jsonConvert = JsonConvert.root();
|
|
||||||
final BsonConvert bsonConvert = BsonFactory.root().getConvert();
|
|
||||||
ConstructorArgsEntity bean = new ConstructorArgsEntity(12345678, "哈哈");
|
|
||||||
bean.setCreatetime(12345678901L);
|
|
||||||
String json = jsonConvert.convertTo(bean);
|
|
||||||
System.out.println(json);
|
|
||||||
Assertions.assertEquals("{\"createtime\":12345678901,\"name\":\"哈哈\",\"userid\":12345678}", json);
|
|
||||||
Assertions.assertEquals(
|
|
||||||
jsonConvert.convertFrom(ConstructorArgsEntity.class, json).toString(), json);
|
|
||||||
byte[] bytes = bsonConvert.convertTo(bean);
|
|
||||||
Assertions.assertEquals(
|
|
||||||
bsonConvert.convertFrom(ConstructorArgsEntity.class, bytes).toString(), json);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ComplextEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
private int userid;
|
|
||||||
|
|
||||||
private String chname = "";
|
|
||||||
|
|
||||||
@Transient
|
|
||||||
private boolean flag = true;
|
|
||||||
|
|
||||||
@Transient
|
|
||||||
private List<SimpleChildEntity> children;
|
|
||||||
|
|
||||||
@Transient
|
|
||||||
private SimpleEntity user;
|
|
||||||
|
|
||||||
public int getUserid() {
|
|
||||||
return userid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserid(int userid) {
|
|
||||||
this.userid = userid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getChname() {
|
|
||||||
return chname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setChname(String chname) {
|
|
||||||
this.chname = chname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isFlag() {
|
|
||||||
return flag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFlag(boolean flag) {
|
|
||||||
this.flag = flag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SimpleChildEntity> getChildren() {
|
|
||||||
return children;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setChildren(List<SimpleChildEntity> children) {
|
|
||||||
this.children = children;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SimpleEntity getUser() {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUser(SimpleEntity user) {
|
|
||||||
this.user = user;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return JsonConvert.root().convertTo(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ConstructorArgsEntity {
|
|
||||||
|
|
||||||
private final int userid;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private long createtime;
|
|
||||||
|
|
||||||
@ConstructorParameters({"userid", "name"})
|
|
||||||
public ConstructorArgsEntity(int userid, String name) {
|
|
||||||
this.userid = userid;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getUserid() {
|
|
||||||
return userid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getCreatetime() {
|
|
||||||
return createtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCreatetime(long createtime) {
|
|
||||||
this.createtime = createtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return JsonConvert.root().convertTo(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,155 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.test.convert.bson;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/** @author redkale */
|
|
||||||
public class ConvertRecord {
|
|
||||||
|
|
||||||
private String aname;
|
|
||||||
|
|
||||||
private String desc = "";
|
|
||||||
|
|
||||||
private int id = (int) System.currentTimeMillis();
|
|
||||||
|
|
||||||
private int[] integers;
|
|
||||||
|
|
||||||
private long[] longs;
|
|
||||||
|
|
||||||
private List<String> strings;
|
|
||||||
|
|
||||||
private Map<String, Integer> map;
|
|
||||||
|
|
||||||
public static ConvertRecord createDefault() {
|
|
||||||
ConvertRecord v = new ConvertRecord();
|
|
||||||
v.setAname("this is name\n \"test");
|
|
||||||
v.setId(1000000001);
|
|
||||||
v.setIntegers(new int[] {12, 34, 56, 78, 90, 123, 456, 789});
|
|
||||||
v.setLongs(new long[] {
|
|
||||||
10000012L, 10000034L, 10000056L, 10000078L, -10000090L, -100000123L, -100000456L, -100000789L
|
|
||||||
});
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
list.add("str_a");
|
|
||||||
list.add("str_b");
|
|
||||||
list.add("str_c");
|
|
||||||
v.setStrings(list);
|
|
||||||
Map<String, Integer> map = new HashMap<>();
|
|
||||||
map.put("key_a", 111);
|
|
||||||
map.put("key_b", 222);
|
|
||||||
map.put("key_c", 333);
|
|
||||||
v.setMap(map);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
final ConvertRecord entry = ConvertRecord.createDefault();
|
|
||||||
run(ConvertRecord.class, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> void run(final Class<T> type, final T entry) throws Exception {
|
|
||||||
/**
|
|
||||||
* final org.redkale.convert.json.JsonConvert convert =
|
|
||||||
* org.redkale.convert.json.JsonFactory.root().getConvert(); final String entryString =
|
|
||||||
* convert.convertTo(entry); convert.convertFrom(type, entryString); System.out.println("redkale-convert: " +
|
|
||||||
* convert.convertTo(entry));
|
|
||||||
*
|
|
||||||
* <p>com.alibaba.fastjson.JSON.parseObject(entryString, type); System.out.println("fastjson 1.2.7: " +
|
|
||||||
* com.alibaba.fastjson.JSON.toJSONString(entry));
|
|
||||||
*
|
|
||||||
* <p>final com.fasterxml.jackson.databind.ObjectMapper mapper = new
|
|
||||||
* com.fasterxml.jackson.databind.ObjectMapper(); mapper.readValue(entryString, type);
|
|
||||||
* System.out.println("jackson 2.7.2: " + mapper.writeValueAsString(entry));
|
|
||||||
*
|
|
||||||
* <p>final com.google.gson.Gson gson = new com.google.gson.Gson(); gson.fromJson(entryString, type);
|
|
||||||
* System.out.println("google-gson 2.4: " + gson.toJson(entry));
|
|
||||||
*
|
|
||||||
* <p>System.out.println("------------------------------------------------"); System.out.println("组件 序列化耗时(ms)
|
|
||||||
* 反序列化耗时(ms)"); final int count = 10_0000; long s = System.currentTimeMillis(); for (int i = 0; i < count; i++)
|
|
||||||
* { convert.convertTo(entry); } long e = System.currentTimeMillis() - s; System.out.print("redkale-convert " +
|
|
||||||
* e);
|
|
||||||
*
|
|
||||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { convert.convertFrom(type, entryString);
|
|
||||||
* } e = System.currentTimeMillis() - s; System.out.println("\t " + e);
|
|
||||||
*
|
|
||||||
* <p>//---------------------------------------------------------------------------- s =
|
|
||||||
* System.currentTimeMillis(); for (int i = 0; i < count; i++) { com.alibaba.fastjson.JSON.toJSONString(entry);
|
|
||||||
* } e = System.currentTimeMillis() - s; System.out.print("fastjson 1.2.7 " + e);
|
|
||||||
*
|
|
||||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) {
|
|
||||||
* com.alibaba.fastjson.JSON.parseObject(entryString, type); } e = System.currentTimeMillis() - s;
|
|
||||||
* System.out.println("\t " + e); //----------------------------------------------------------------------------
|
|
||||||
* s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { mapper.writeValueAsString(entry); } e =
|
|
||||||
* System.currentTimeMillis() - s; System.out.print("jackson 2.7.2 " + e);
|
|
||||||
*
|
|
||||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { mapper.readValue(entryString, type); } e
|
|
||||||
* = System.currentTimeMillis() - s; System.out.println("\t " + e);
|
|
||||||
* //---------------------------------------------------------------------------- s =
|
|
||||||
* System.currentTimeMillis(); for (int i = 0; i < count; i++) { gson.toJson(entry); } e =
|
|
||||||
* System.currentTimeMillis() - s; System.out.print("google-gson 2.4 " + e);
|
|
||||||
*
|
|
||||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { gson.fromJson(entryString, type); } e =
|
|
||||||
* System.currentTimeMillis() - s; System.out.println("\t " + e);
|
|
||||||
* //----------------------------------------------------------------------------
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAname() {
|
|
||||||
return aname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAname(String aname) {
|
|
||||||
this.aname = aname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDesc() {
|
|
||||||
return desc;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDesc(String desc) {
|
|
||||||
this.desc = desc;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getIntegers() {
|
|
||||||
return integers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIntegers(int[] integers) {
|
|
||||||
this.integers = integers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long[] getLongs() {
|
|
||||||
return longs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLongs(long[] longs) {
|
|
||||||
this.longs = longs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getStrings() {
|
|
||||||
return strings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStrings(List<String> strings) {
|
|
||||||
this.strings = strings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Integer> getMap() {
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMap(Map<String, Integer> map) {
|
|
||||||
this.map = map;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.test.convert.bson;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
|
||||||
import org.redkale.util.Utility;
|
|
||||||
|
|
||||||
/** @author zhangjx */
|
|
||||||
public class One {
|
|
||||||
|
|
||||||
protected String key;
|
|
||||||
|
|
||||||
protected int code;
|
|
||||||
|
|
||||||
protected byte[] bytes = new byte[] {3, 4, 5};
|
|
||||||
|
|
||||||
protected int[] ints = new int[] {3000, 4000, 5000};
|
|
||||||
|
|
||||||
public One(int code) {
|
|
||||||
this.code = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getKey() {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setKey(String key) {
|
|
||||||
this.key = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCode(int code) {
|
|
||||||
this.code = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getBytes() {
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBytes(byte[] bytes) {
|
|
||||||
this.bytes = bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getInts() {
|
|
||||||
return ints;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setInts(int[] ints) {
|
|
||||||
this.ints = ints;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return JsonConvert.root().convertTo(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
|
||||||
int count = 100_0000;
|
|
||||||
One one = new One(234);
|
|
||||||
one.bytes = new byte[] {1, 2, 3};
|
|
||||||
one.key = "哈哈";
|
|
||||||
|
|
||||||
System.out.println(Arrays.toString(Utility.encodeUTF8(JsonConvert.root().convertTo(one))));
|
|
||||||
System.out.println(Arrays.toString(JsonConvert.root().convertToBytes(one)));
|
|
||||||
long s = System.currentTimeMillis();
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
JsonConvert.root().convertTo(one).getBytes();
|
|
||||||
}
|
|
||||||
long e = System.currentTimeMillis() - s;
|
|
||||||
|
|
||||||
long s2 = System.currentTimeMillis();
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
JsonConvert.root().convertToBytes(one);
|
|
||||||
}
|
|
||||||
long e2 = System.currentTimeMillis() - s2;
|
|
||||||
System.out.println(e);
|
|
||||||
System.out.println(e2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.test.convert.bson;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/** @author zhangjx */
|
|
||||||
public class Two extends One {
|
|
||||||
|
|
||||||
public Two() {
|
|
||||||
super(90100119);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected List<String> list;
|
|
||||||
|
|
||||||
protected Map<String, String> stringMap;
|
|
||||||
|
|
||||||
protected List<ConvertRecord> records;
|
|
||||||
|
|
||||||
protected Map<String, ConvertRecord> recordMap;
|
|
||||||
|
|
||||||
public Map<String, String> getStringMap() {
|
|
||||||
return stringMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStringMap(Map<String, String> stringMap) {
|
|
||||||
this.stringMap = stringMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
String ip;
|
|
||||||
|
|
||||||
public String getIp() {
|
|
||||||
return ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIp(String ip) {
|
|
||||||
this.ip = ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getList() {
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setList(List<String> list) {
|
|
||||||
this.list = list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ConvertRecord> getRecords() {
|
|
||||||
return records;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRecords(List<ConvertRecord> records) {
|
|
||||||
this.records = records;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, ConvertRecord> getRecordMap() {
|
|
||||||
return recordMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRecordMap(Map<String, ConvertRecord> recordMap) {
|
|
||||||
this.recordMap = recordMap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,10 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.convert.media;
|
package org.redkale.test.convert.media;
|
||||||
|
|
||||||
import org.redkale.test.convert.bson.ConvertRecord;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
import org.redkale.test.convert.*;
|
|
||||||
|
|
||||||
/** @author redkale */
|
/** @author redkale */
|
||||||
public class MediaContent implements java.io.Serializable {
|
public class MediaContent implements java.io.Serializable {
|
||||||
@@ -26,7 +24,6 @@ public class MediaContent implements java.io.Serializable {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final MediaContent entry = MediaContent.createDefault();
|
final MediaContent entry = MediaContent.createDefault();
|
||||||
ConvertRecord.run(MediaContent.class, entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MediaContent createDefault() {
|
public static MediaContent createDefault() {
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import java.net.Socket;
|
|||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.net.AsyncIOGroup;
|
import org.redkale.net.AsyncIOGroup;
|
||||||
import org.redkale.net.http.HttpServer;
|
import org.redkale.net.http.HttpServer;
|
||||||
@@ -40,7 +40,7 @@ public class RestConvertTest {
|
|||||||
asyncGroup.start();
|
asyncGroup.start();
|
||||||
final ResourceFactory resFactory = ResourceFactory.create();
|
final ResourceFactory resFactory = ResourceFactory.create();
|
||||||
resFactory.register(JsonConvert.root());
|
resFactory.register(JsonConvert.root());
|
||||||
resFactory.register(BsonConvert.root());
|
resFactory.register(ProtobufConvert.root());
|
||||||
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(application);
|
method.invoke(application);
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import java.lang.reflect.Method;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.net.AsyncIOGroup;
|
import org.redkale.net.AsyncIOGroup;
|
||||||
import org.redkale.net.http.*;
|
import org.redkale.net.http.*;
|
||||||
@@ -34,7 +34,7 @@ public class RestSleepTest {
|
|||||||
asyncGroup.start();
|
asyncGroup.start();
|
||||||
final ResourceFactory resFactory = ResourceFactory.create();
|
final ResourceFactory resFactory = ResourceFactory.create();
|
||||||
resFactory.register(JsonConvert.root());
|
resFactory.register(JsonConvert.root());
|
||||||
resFactory.register(BsonConvert.root());
|
resFactory.register(ProtobufConvert.root());
|
||||||
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(application);
|
method.invoke(application);
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ import java.util.concurrent.atomic.*;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import org.redkale.annotation.Resource;
|
import org.redkale.annotation.Resource;
|
||||||
import org.redkale.boot.*;
|
import org.redkale.boot.*;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.net.AsyncIOGroup;
|
import org.redkale.net.AsyncIOGroup;
|
||||||
import org.redkale.net.client.ClientAddress;
|
import org.redkale.net.client.ClientAddress;
|
||||||
@@ -44,7 +44,7 @@ public class ABMainService implements Service {
|
|||||||
new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100);
|
new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100);
|
||||||
final ResourceFactory resFactory = ResourceFactory.create();
|
final ResourceFactory resFactory = ResourceFactory.create();
|
||||||
resFactory.register(JsonConvert.root());
|
resFactory.register(JsonConvert.root());
|
||||||
resFactory.register(BsonConvert.root());
|
resFactory.register(ProtobufConvert.root());
|
||||||
final SncpRpcGroups rpcGroups = application.getSncpRpcGroups();
|
final SncpRpcGroups rpcGroups = application.getSncpRpcGroups();
|
||||||
rpcGroups.computeIfAbsent("g77", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5577));
|
rpcGroups.computeIfAbsent("g77", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5577));
|
||||||
rpcGroups.computeIfAbsent("g88", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5588));
|
rpcGroups.computeIfAbsent("g88", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5588));
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.net.AsyncIOGroup;
|
import org.redkale.net.AsyncIOGroup;
|
||||||
import org.redkale.net.WorkThread;
|
import org.redkale.net.WorkThread;
|
||||||
@@ -35,7 +35,7 @@ public class SncpSleepTest {
|
|||||||
final ResourceFactory resFactory = ResourceFactory.create();
|
final ResourceFactory resFactory = ResourceFactory.create();
|
||||||
resFactory.register(Application.RESNAME_APP_EXECUTOR, ExecutorService.class, workExecutor);
|
resFactory.register(Application.RESNAME_APP_EXECUTOR, ExecutorService.class, workExecutor);
|
||||||
resFactory.register(JsonConvert.root());
|
resFactory.register(JsonConvert.root());
|
||||||
resFactory.register(BsonConvert.root());
|
resFactory.register(ProtobufConvert.root());
|
||||||
|
|
||||||
// ------------------------ 初始化 CService ------------------------------------
|
// ------------------------ 初始化 CService ------------------------------------
|
||||||
SncpSleepService service = Sncp.createSimpleLocalService(SncpSleepService.class, resFactory);
|
SncpSleepService service = Sncp.createSimpleLocalService(SncpSleepService.class, resFactory);
|
||||||
@@ -64,6 +64,6 @@ public class SncpSleepTest {
|
|||||||
System.out.println("耗时: " + e + " ms");
|
System.out.println("耗时: " + e + " ms");
|
||||||
server.shutdown();
|
server.shutdown();
|
||||||
workExecutor.shutdown();
|
workExecutor.shutdown();
|
||||||
Assertions.assertTrue(e < 600);
|
Assertions.assertTrue(e < 660);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import java.util.concurrent.*;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.redkale.boot.*;
|
import org.redkale.boot.*;
|
||||||
import org.redkale.convert.bson.*;
|
import org.redkale.convert.pb.ProtobufConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
import org.redkale.net.client.ClientAddress;
|
import org.redkale.net.client.ClientAddress;
|
||||||
@@ -52,7 +53,7 @@ public class SncpTest {
|
|||||||
application = Application.create(true);
|
application = Application.create(true);
|
||||||
rpcGroups = application.getSncpRpcGroups();
|
rpcGroups = application.getSncpRpcGroups();
|
||||||
factory = application.getResourceFactory();
|
factory = application.getResourceFactory();
|
||||||
factory.register("", BsonConvert.class, BsonFactory.root().getConvert());
|
factory.register("", ProtobufConvert.class, ProtobufFactory.root().getConvert());
|
||||||
factory.register("", Application.class, application);
|
factory.register("", Application.class, application);
|
||||||
|
|
||||||
if (System.getProperty("client") == null) {
|
if (System.getProperty("client") == null) {
|
||||||
|
|||||||
@@ -5,8 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.sncp;
|
package org.redkale.test.sncp;
|
||||||
|
|
||||||
import org.redkale.convert.bson.BsonFactory;
|
import org.redkale.convert.ConvertColumn;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.convert.pb.ProtobufFactory;
|
||||||
import org.redkale.persistence.Id;
|
import org.redkale.persistence.Id;
|
||||||
import org.redkale.source.FilterBean;
|
import org.redkale.source.FilterBean;
|
||||||
import org.redkale.util.Utility;
|
import org.redkale.util.Utility;
|
||||||
@@ -15,17 +16,19 @@ import org.redkale.util.Utility;
|
|||||||
public class SncpTestBean implements FilterBean {
|
public class SncpTestBean implements FilterBean {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ConvertColumn(index = 1)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
|
@ConvertColumn(index = 2)
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
String json = "{\"content\":\"数据: 01\",\"id\":1}";
|
String json = "{\"content\":\"数据: 01\",\"id\":1}";
|
||||||
SncpTestBean bean = JsonConvert.root().convertFrom(SncpTestBean.class, json);
|
SncpTestBean bean = JsonConvert.root().convertFrom(SncpTestBean.class, json);
|
||||||
System.out.println(bean);
|
System.out.println(bean);
|
||||||
byte[] bs = BsonFactory.root().getConvert().convertTo(bean);
|
byte[] bs = ProtobufFactory.root().getConvert().convertTo(bean);
|
||||||
Utility.println("---------", bs);
|
Utility.println("---------", bs);
|
||||||
System.out.println(BsonFactory.root()
|
System.out.println(ProtobufFactory.root()
|
||||||
.getConvert()
|
.getConvert()
|
||||||
.convertFrom(SncpTestBean.class, bs)
|
.convertFrom(SncpTestBean.class, bs)
|
||||||
.toString());
|
.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user