移除BSON功能
This commit is contained in:
@@ -18,7 +18,6 @@ module org.redkale {
|
||||
exports org.redkale.cluster;
|
||||
exports org.redkale.cluster.spi;
|
||||
exports org.redkale.convert;
|
||||
exports org.redkale.convert.bson;
|
||||
exports org.redkale.convert.ext;
|
||||
exports org.redkale.convert.json;
|
||||
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.HttpLocalRpcClient;
|
||||
import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.inject.ResourceAnnotationLoader;
|
||||
@@ -304,19 +303,14 @@ public final class Application {
|
||||
|
||||
// 需要在加载properties初始化System.properties之后再注册
|
||||
this.resourceFactory.register(Environment.class, environment);
|
||||
this.resourceFactory.register(BsonFactory.root());
|
||||
this.resourceFactory.register(JsonFactory.root());
|
||||
this.resourceFactory.register(ProtobufFactory.root());
|
||||
this.resourceFactory.register(BsonFactory.root().getConvert());
|
||||
this.resourceFactory.register(JsonFactory.root().getConvert());
|
||||
this.resourceFactory.register(ProtobufFactory.root().getConvert());
|
||||
this.resourceFactory.register(
|
||||
"bsonconvert", Convert.class, BsonFactory.root().getConvert());
|
||||
this.resourceFactory.register(
|
||||
"jsonconvert", Convert.class, JsonFactory.root().getConvert());
|
||||
this.resourceFactory.register(
|
||||
"protobufconvert", Convert.class, ProtobufFactory.root().getConvert());
|
||||
BsonFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
||||
JsonFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
||||
ProtobufFactory.root().registerFieldFuncConsumer(resourceFactory::inject);
|
||||
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
package org.redkale.boot;
|
||||
|
||||
import java.util.List;
|
||||
import org.redkale.annotation.Serial;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.convert.Decodeable;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.persistence.Entity;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.util.Utility;
|
||||
import org.redkale.annotation.Serial;
|
||||
|
||||
/**
|
||||
* 执行一次Application.run提前获取所有动态类
|
||||
@@ -60,11 +60,11 @@ public class PrepareCompiler {
|
||||
// application.dataSources.forEach(source -> source.compile(clz));
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadEncoder(clz);
|
||||
ProtobufFactory.root().loadEncoder(clz);
|
||||
}
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadDecoder(clz);
|
||||
ProtobufFactory.root().loadDecoder(clz);
|
||||
}
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
@@ -81,11 +81,11 @@ public class PrepareCompiler {
|
||||
// application.dataSources.forEach(source -> source.compile(clz));
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadEncoder(clz);
|
||||
ProtobufFactory.root().loadEncoder(clz);
|
||||
}
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadDecoder(clz);
|
||||
ProtobufFactory.root().loadDecoder(clz);
|
||||
}
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
@@ -99,11 +99,11 @@ public class PrepareCompiler {
|
||||
try {
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadEncoder(clz);
|
||||
ProtobufFactory.root().loadEncoder(clz);
|
||||
}
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadDecoder(clz);
|
||||
ProtobufFactory.root().loadDecoder(clz);
|
||||
}
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
@@ -117,11 +117,11 @@ public class PrepareCompiler {
|
||||
try {
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadEncoder(clz);
|
||||
ProtobufFactory.root().loadEncoder(clz);
|
||||
}
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) {
|
||||
BsonFactory.root().loadDecoder(clz);
|
||||
ProtobufFactory.root().loadDecoder(clz);
|
||||
}
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { // JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
|
||||
@@ -8,7 +8,7 @@ package org.redkale.convert;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* 对不明类型的对象进行序列化; BSON序列化时将对象的类名写入Writer,JSON则不写入。
|
||||
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
|
||||
@@ -70,7 +70,7 @@ public @interface ConvertCoder {
|
||||
/**
|
||||
* 解析/序列化定制化的TYPE
|
||||
*
|
||||
* @return JSON or BSON or ALL
|
||||
* @return JSON or PROTOBUF or ALL
|
||||
*/
|
||||
ConvertType type() default ConvertType.ALL;
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public @interface ConvertColumn {
|
||||
/**
|
||||
* 解析/序列化定制化的TYPE
|
||||
*
|
||||
* @return JSON or BSON or ALL
|
||||
* @return JSON or PROTOBUF or ALL
|
||||
*/
|
||||
ConvertType type() default ConvertType.ALL;
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public @interface ConvertColumnHandler {
|
||||
/**
|
||||
* 解析/序列化定制化的TYPE
|
||||
*
|
||||
* @return JSON or BSON or ALL
|
||||
* @return JSON or PROTOBUF or ALL
|
||||
*/
|
||||
ConvertType type() default ConvertType.ALL;
|
||||
|
||||
|
||||
@@ -5,11 +5,10 @@
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 序列化时永久禁用该字段, 与ConvertColumn.ignore()的区别在于: ConvertDisabled不能通过ConvertEntity来解禁
|
||||
*
|
||||
@@ -24,7 +23,7 @@ public @interface ConvertDisabled {
|
||||
/**
|
||||
* 解析/序列化定制化的TYPE
|
||||
*
|
||||
* @return JSON or BSON or ALL
|
||||
* @return JSON or PROTOBUF or ALL
|
||||
*/
|
||||
ConvertType type() default ConvertType.ALL;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 用于类名的别名, 该值必须是全局唯一 <br>
|
||||
* 使用场景: 当BSON序列化为了不指定class可以使用@ConvertEntity来取个别名。 <br>
|
||||
* 使用场景: 当自定义序列化为了不指定class可以使用@ConvertEntity来取个别名。 <br>
|
||||
* 关联方法: {@link org.redkale.convert.Reader#readClassName()} 和 {@link org.redkale.convert.Writer#writeClassName(java.lang.String) } 。
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.*;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.ext.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
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)) {
|
||||
return ProtobufConvert.root();
|
||||
}
|
||||
if (type == ConvertType.BSON || type.contains(ConvertType.BSON)) {
|
||||
return BsonConvert.root();
|
||||
}
|
||||
|
||||
Iterator<ConvertProvider> it = ServiceLoader.load(ConvertProvider.class).iterator();
|
||||
RedkaleClassLoader.putServiceLoader(ConvertProvider.class);
|
||||
|
||||
@@ -14,10 +14,8 @@ package org.redkale.convert;
|
||||
*/
|
||||
public enum ConvertType {
|
||||
JSON(1),
|
||||
BSON(2),
|
||||
PROTOBUF(64),
|
||||
PROTOBUF_JSON(64 + 1),
|
||||
PROTOBUF_BSON(64 + 2),
|
||||
PROTOBUF(2),
|
||||
PROTOBUF_JSON(2 + 1),
|
||||
DIY(256),
|
||||
ALL(1023);
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ public abstract class Writer {
|
||||
public abstract void writeNull();
|
||||
|
||||
/**
|
||||
* 是否需要写入类名, BSON需要, JSON不需要
|
||||
* 是否需要写入类名, JSON不需要
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteArray toByteArray() {
|
||||
ByteArray array = new ByteArray();
|
||||
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
|
||||
protected int expand(int len) {
|
||||
int newcount = count + len;
|
||||
|
||||
@@ -657,7 +657,7 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
if (!(decoder instanceof ObjectDecoder)) {
|
||||
if (decoder == null) {
|
||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||
}
|
||||
T rs = (T) decoder.convertFrom(reader);
|
||||
|
||||
@@ -40,16 +40,17 @@ public class ProtobufReader extends Reader {
|
||||
setBytes(bytes);
|
||||
}
|
||||
|
||||
public final void setBytes(byte[] bytes) {
|
||||
public final ProtobufReader setBytes(byte[] bytes) {
|
||||
if (bytes == null) {
|
||||
this.position = 0;
|
||||
this.limit = 0;
|
||||
} else {
|
||||
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) {
|
||||
this.position = 0;
|
||||
this.limit = 0;
|
||||
@@ -58,6 +59,7 @@ public class ProtobufReader extends Reader {
|
||||
this.position = start - 1;
|
||||
this.limit = start + len;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void limit(int limit) {
|
||||
|
||||
@@ -106,6 +106,10 @@ public abstract class ProtobufWriter extends Writer {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
recycle();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
@@ -121,6 +125,8 @@ public abstract class ProtobufWriter extends Writer {
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract ByteArray toByteArray();
|
||||
|
||||
public abstract ProtobufWriter pollChild();
|
||||
|
||||
public abstract void offerChild(ProtobufWriter child);
|
||||
|
||||
@@ -33,8 +33,8 @@ public class MessageRecord implements Serializable {
|
||||
|
||||
public static final byte CTYPE_STRING = 1;
|
||||
|
||||
// Bson bytes
|
||||
public static final byte CTYPE_BSON = 2;
|
||||
// Protobuf bytes
|
||||
public static final byte CTYPE_PROTOBUF = 2;
|
||||
|
||||
// WebRequest
|
||||
public static final byte CTYPE_HTTP_REQUEST = 3;
|
||||
@@ -344,9 +344,9 @@ public class MessageRecord implements Serializable {
|
||||
sb.append(",\"respTopic\":\"").append(this.respTopic).append("\"");
|
||||
}
|
||||
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; //循环占位符
|
||||
// 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);
|
||||
// sb.append(",\"content\":").append(rs);
|
||||
// SncpHeader包含不确定长度的信息,故不能再直接偏移读取
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
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.util.ByteArray;
|
||||
|
||||
@@ -28,7 +28,7 @@ public class SncpMessageResponse extends SncpResponse {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish(final int retcode, final BsonWriter out) {
|
||||
public void finish(final int retcode, final ProtobufWriter out) {
|
||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||
if (out == null) {
|
||||
final ByteArray result = new ByteArray(headerSize).putPlaceholder(headerSize);
|
||||
@@ -36,7 +36,8 @@ public class SncpMessageResponse extends SncpResponse {
|
||||
messageClient
|
||||
.getProducer()
|
||||
.apply(messageClient.createMessageRecord(
|
||||
message.getSeqid(), MessageRecord.CTYPE_BSON, message.getRespTopic(), null, (byte[]) null));
|
||||
message.getSeqid(), MessageRecord.CTYPE_PROTOBUF, message.getRespTopic(), null, (byte[])
|
||||
null));
|
||||
return;
|
||||
}
|
||||
final ByteArray result = out.toByteArray();
|
||||
@@ -44,6 +45,10 @@ public class SncpMessageResponse extends SncpResponse {
|
||||
messageClient
|
||||
.getProducer()
|
||||
.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.logging.*;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import org.redkale.convert.bson.*;
|
||||
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.util.*;
|
||||
|
||||
@@ -48,8 +49,8 @@ public class Context {
|
||||
// 日志Logger
|
||||
protected final Logger logger;
|
||||
|
||||
// BSON操作工厂
|
||||
protected final BsonFactory bsonFactory;
|
||||
// Protobuf操作工厂
|
||||
protected final ProtobufFactory protobufFactory;
|
||||
|
||||
// JSON操作工厂
|
||||
protected final JsonFactory jsonFactory;
|
||||
@@ -138,7 +139,7 @@ public class Context {
|
||||
this.readTimeoutSeconds = readTimeoutSeconds;
|
||||
this.writeTimeoutSeconds = writeTimeoutSeconds;
|
||||
this.jsonFactory = JsonFactory.root();
|
||||
this.bsonFactory = BsonFactory.root();
|
||||
this.protobufFactory = ProtobufFactory.root();
|
||||
}
|
||||
|
||||
protected final void executeDispatch(Request request, Response response) {
|
||||
@@ -247,8 +248,8 @@ public class Context {
|
||||
return jsonFactory.getConvert();
|
||||
}
|
||||
|
||||
public BsonConvert getBsonConvert() {
|
||||
return bsonFactory.getConvert();
|
||||
public ProtobufConvert getProtobufConvert() {
|
||||
return protobufFactory.getConvert();
|
||||
}
|
||||
|
||||
public static class ContextConfig {
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import org.redkale.convert.ConvertDisabled;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.util.Creator;
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ public abstract class Request<C extends Context> {
|
||||
|
||||
protected final C context;
|
||||
|
||||
protected final BsonConvert bsonConvert;
|
||||
protected final ProtobufConvert protobufConvert;
|
||||
|
||||
protected final JsonConvert jsonConvert;
|
||||
|
||||
@@ -62,13 +62,13 @@ public abstract class Request<C extends Context> {
|
||||
|
||||
protected Request(C context) {
|
||||
this.context = context;
|
||||
this.bsonConvert = context.getBsonConvert();
|
||||
this.protobufConvert = context.getProtobufConvert();
|
||||
this.jsonConvert = context.getJsonConvert();
|
||||
}
|
||||
|
||||
protected Request(Request<C> request) {
|
||||
this.context = request.context;
|
||||
this.bsonConvert = request.bsonConvert;
|
||||
this.protobufConvert = request.protobufConvert;
|
||||
this.jsonConvert = request.jsonConvert;
|
||||
this.createTime = request.createTime;
|
||||
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.Opcodes.*;
|
||||
import org.redkale.asm.Type;
|
||||
import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.inject.Resourcable;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.mq.spi.MessageAgent;
|
||||
@@ -156,7 +155,7 @@ public abstract class Sncp {
|
||||
String resourceName,
|
||||
Class<T> resourceServiceType,
|
||||
Class<T> serviceImplClass,
|
||||
Convert convert,
|
||||
ProtobufConvert convert,
|
||||
SncpRpcGroups sncpRpcGroups,
|
||||
SncpClient sncpClient,
|
||||
MessageAgent messageAgent,
|
||||
@@ -930,7 +929,7 @@ public abstract class Sncp {
|
||||
name,
|
||||
getResourceType(serviceTypeOrImplClass),
|
||||
serviceTypeOrImplClass,
|
||||
BsonConvert.root(),
|
||||
ProtobufConvert.root(),
|
||||
sncpRpcGroups,
|
||||
client,
|
||||
agent,
|
||||
|
||||
@@ -43,7 +43,7 @@ public class SncpDispatcherServlet
|
||||
}
|
||||
}
|
||||
|
||||
public <T> SncpServlet removeSncpServlet(Service service) {
|
||||
public SncpServlet removeSncpServlet(Service service) {
|
||||
SncpServlet rs = null;
|
||||
updateLock.lock();
|
||||
try {
|
||||
|
||||
@@ -11,8 +11,9 @@ import java.nio.channels.CompletionHandler;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.convert.*;
|
||||
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.MessageClient;
|
||||
import org.redkale.mq.spi.MessageRecord;
|
||||
@@ -59,8 +60,8 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
// 非MQ模式下此字段才有值, 可能为null
|
||||
protected Set<InetSocketAddress> remoteAddresses;
|
||||
|
||||
// 默认值: BsonConvert.root()
|
||||
protected final Convert convert;
|
||||
// 默认值: ProtobufConvert.root()
|
||||
protected final ProtobufConvert convert;
|
||||
|
||||
// MQ模式下此字段才有值
|
||||
protected final String topic;
|
||||
@@ -75,7 +76,7 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
String resourceName,
|
||||
Class<S> resourceType,
|
||||
Class<S> serviceImplClass,
|
||||
Convert convert,
|
||||
ProtobufConvert convert,
|
||||
SncpRpcGroups sncpRpcGroups,
|
||||
SncpClient sncpClient,
|
||||
MessageAgent messageAgent,
|
||||
@@ -192,7 +193,7 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
request.writeTo(null, array);
|
||||
MessageRecord message = messageAgent
|
||||
.getSncpMessageClient()
|
||||
.createMessageRecord(MessageRecord.CTYPE_BSON, targetTopic, null, array.getBytes());
|
||||
.createMessageRecord(MessageRecord.CTYPE_PROTOBUF, targetTopic, null, array.getBytes());
|
||||
final String tt = targetTopic;
|
||||
message.localActionName(action.actionName());
|
||||
message.localParams(params);
|
||||
@@ -254,7 +255,7 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
}
|
||||
byte[] body = null;
|
||||
if (myParamTypes.length > 0) {
|
||||
Writer writer = convert.pollWriter();
|
||||
ProtobufWriter writer = convert.pollWriter();
|
||||
for (int i = 0; i < params.length; i++) { // service方法的参数
|
||||
convert.convertTo(
|
||||
writer,
|
||||
@@ -263,7 +264,7 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
: myParamTypes[i],
|
||||
params[i]);
|
||||
}
|
||||
body = ((ByteTuple) writer).toArray();
|
||||
body = writer.toByteArray().content();
|
||||
convert.offerWriter(writer);
|
||||
}
|
||||
final SncpClientRequest request = new SncpClientRequest();
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.bson.BsonReader;
|
||||
import org.redkale.convert.pb.ProtobufReader;
|
||||
import org.redkale.net.Request;
|
||||
import static org.redkale.net.client.ClientRequest.EMPTY_TRACEID;
|
||||
import org.redkale.util.*;
|
||||
@@ -36,7 +36,7 @@ public class SncpRequest extends Request<SncpContext> {
|
||||
|
||||
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;
|
||||
|
||||
@@ -80,15 +80,18 @@ public class SncpRequest extends Request<SncpContext> {
|
||||
}
|
||||
if (this.headerLength < SncpHeader.HEADER_SUBSIZE) {
|
||||
context.getLogger()
|
||||
.log(Level.WARNING,
|
||||
.log(
|
||||
Level.WARNING,
|
||||
"sncp header.length must more " + SncpHeader.HEADER_SUBSIZE + ", but "
|
||||
+ this.headerLength);
|
||||
return -1;
|
||||
}
|
||||
if (this.headerLength > context.getMaxHeader()) {
|
||||
context.getLogger()
|
||||
.log(Level.WARNING,
|
||||
"sncp header.length must lower " + context.getMaxHeader() + ", but " + this.headerLength);
|
||||
.log(
|
||||
Level.WARNING,
|
||||
"sncp header.length must lower " + context.getMaxHeader() + ", but "
|
||||
+ this.headerLength);
|
||||
return -1;
|
||||
}
|
||||
this.readState = READ_STATE_HEADER;
|
||||
@@ -199,7 +202,7 @@ public class SncpRequest extends Request<SncpContext> {
|
||||
}
|
||||
|
||||
public Convert getConvert() {
|
||||
return context.getBsonConvert();
|
||||
return context.getProtobufConvert();
|
||||
}
|
||||
|
||||
public Reader getReader() {
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
*/
|
||||
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.nio.channels.CompletionHandler;
|
||||
import java.util.concurrent.*;
|
||||
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 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.Traces;
|
||||
|
||||
@@ -36,7 +36,7 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
||||
|
||||
final int addrPort;
|
||||
|
||||
protected final BsonWriter writer = new BsonWriter();
|
||||
protected final ProtobufWriter writer = new ProtobufBytesWriter();
|
||||
|
||||
protected final CompletionHandler realHandler = new CompletionHandler() {
|
||||
@Override
|
||||
@@ -106,7 +106,7 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
||||
return super.recycle();
|
||||
}
|
||||
|
||||
public BsonWriter getBsonWriter() {
|
||||
public ProtobufWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
@@ -141,8 +141,8 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
||||
|
||||
public final void finishVoid() {
|
||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||
BsonWriter out = getBsonWriter();
|
||||
out.writePlaceholderTo(headerSize);
|
||||
ProtobufWriter out = getWriter();
|
||||
((ProtobufBytesWriter) out).writePlaceholderTo(headerSize);
|
||||
finish(0, out);
|
||||
}
|
||||
|
||||
@@ -170,17 +170,17 @@ public class SncpResponse extends Response<SncpContext, SncpRequest> {
|
||||
|
||||
public final void finish(final Type type, final Object result) {
|
||||
int headerSize = SncpHeader.calcHeaderSize(request);
|
||||
BsonWriter out = getBsonWriter();
|
||||
out.writePlaceholderTo(headerSize);
|
||||
ProtobufWriter out = getWriter();
|
||||
((ProtobufBytesWriter) out).writePlaceholderTo(headerSize);
|
||||
if (result != null || type != Void.class) {
|
||||
out.writeByte((byte) 0); // body的第一个字节为0,表示返回结果对象,而不是参数回调对象
|
||||
context.getBsonConvert().convertTo(out, type, result);
|
||||
context.getProtobufConvert().convertTo(out, type, result);
|
||||
}
|
||||
finish(0, out);
|
||||
}
|
||||
|
||||
// 调用此方法时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);
|
||||
if (out == null) {
|
||||
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.Type;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.util.*;
|
||||
@@ -764,18 +764,18 @@ public class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse>
|
||||
if (t.toString().startsWith("java.lang.")) {
|
||||
continue;
|
||||
}
|
||||
BsonFactory.root().loadDecoder(t);
|
||||
ProtobufFactory.root().loadDecoder(t);
|
||||
}
|
||||
if (originalReturnType != void.class && originalReturnType != Void.class) {
|
||||
if (boolReturnTypeFuture && method.getReturnType() != method.getGenericReturnType()) {
|
||||
java.lang.reflect.Type t =
|
||||
((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
|
||||
if (t != Void.class && t != java.lang.reflect.Type.class) {
|
||||
BsonFactory.root().loadEncoder(t);
|
||||
ProtobufFactory.root().loadEncoder(t);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
BsonFactory.root().loadEncoder(originalReturnType);
|
||||
ProtobufFactory.root().loadEncoder(originalReturnType);
|
||||
} catch (Exception e) {
|
||||
System.err.println(method);
|
||||
}
|
||||
|
||||
@@ -1,124 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
class Jdk21Inners {
|
||||
|
||||
static {
|
||||
// 加载时进行可用性判断
|
||||
Thread.currentThread().isVirtual();
|
||||
}
|
||||
|
||||
private Jdk21Inners() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public static Executor createExecutor() {
|
||||
return new VirtualExecutor();
|
||||
}
|
||||
|
||||
public static Function<String, ExecutorService> createPoolFunction() {
|
||||
return new VirtualPoolFunction();
|
||||
}
|
||||
|
||||
public static Function<Supplier, ThreadLocal> createThreadLocalFunction() {
|
||||
return new VirtualThreadLocal(() -> null);
|
||||
}
|
||||
|
||||
public static Function<String, ThreadFactory> createThreadFactoryFunction() {
|
||||
return new VirtualThreadFactory("");
|
||||
}
|
||||
|
||||
static class VirtualExecutor implements Executor {
|
||||
|
||||
@Override
|
||||
public void execute(Runnable t) {
|
||||
Thread.ofVirtual().name("Redkale-VirtualThread").start(t);
|
||||
}
|
||||
}
|
||||
|
||||
static class VirtualPoolFunction implements Function<String, ExecutorService> {
|
||||
|
||||
@Override
|
||||
public ExecutorService apply(String threadNameFormat) {
|
||||
final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||
final String threadName = String.format(threadNameFormat, "Virtual");
|
||||
return Executors.newThreadPerTaskExecutor(r -> {
|
||||
Thread t = factory.newThread(r);
|
||||
t.setName(threadName);
|
||||
return t;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static class VirtualThreadLocal<T> extends ThreadLocal<T> implements Function<Supplier<T>, ThreadLocal<T>> {
|
||||
|
||||
private final Supplier<T> supplier;
|
||||
|
||||
public VirtualThreadLocal(Supplier<T> supplier) {
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThreadLocal<T> apply(Supplier<T> supplier) {
|
||||
return new VirtualThreadLocal<>(supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T initialValue() {
|
||||
return supplier.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T value) {
|
||||
Thread t = Thread.currentThread();
|
||||
if (!t.isVirtual()) {
|
||||
super.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
Thread t = Thread.currentThread();
|
||||
return t.isVirtual() ? initialValue() : super.get();
|
||||
}
|
||||
}
|
||||
|
||||
static class VirtualThreadFactory implements ThreadFactory, Function<String, ThreadFactory> {
|
||||
|
||||
private final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||
|
||||
private final String name;
|
||||
|
||||
public VirtualThreadFactory(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThreadFactory apply(String name) {
|
||||
return new VirtualThreadFactory(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread t = factory.newThread(r);
|
||||
if (name != null) {
|
||||
t.setName(name);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
///*
|
||||
// * Copyright (c) 2016-2116 Redkale
|
||||
// * All rights reserved.
|
||||
// */
|
||||
//package org.redkale.util;
|
||||
//
|
||||
//import java.util.concurrent.Executor;
|
||||
//import java.util.concurrent.ExecutorService;
|
||||
//import java.util.concurrent.Executors;
|
||||
//import java.util.concurrent.ThreadFactory;
|
||||
//import java.util.function.Function;
|
||||
//import java.util.function.Supplier;
|
||||
//
|
||||
///**
|
||||
// *
|
||||
// * @author zhangjx
|
||||
// */
|
||||
//class Jdk21Inners {
|
||||
//
|
||||
// static {
|
||||
// // 加载时进行可用性判断
|
||||
// Thread.currentThread().isVirtual();
|
||||
// }
|
||||
//
|
||||
// private Jdk21Inners() {
|
||||
// // do nothing
|
||||
// }
|
||||
//
|
||||
// public static Executor createExecutor() {
|
||||
// return new VirtualExecutor();
|
||||
// }
|
||||
//
|
||||
// public static Function<String, ExecutorService> createPoolFunction() {
|
||||
// return new VirtualPoolFunction();
|
||||
// }
|
||||
//
|
||||
// public static Function<Supplier, ThreadLocal> createThreadLocalFunction() {
|
||||
// return new VirtualThreadLocal(() -> null);
|
||||
// }
|
||||
//
|
||||
// public static Function<String, ThreadFactory> createThreadFactoryFunction() {
|
||||
// return new VirtualThreadFactory("");
|
||||
// }
|
||||
//
|
||||
// static class VirtualExecutor implements Executor {
|
||||
//
|
||||
// @Override
|
||||
// public void execute(Runnable t) {
|
||||
// Thread.ofVirtual().name("Redkale-VirtualThread").start(t);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static class VirtualPoolFunction implements Function<String, ExecutorService> {
|
||||
//
|
||||
// @Override
|
||||
// public ExecutorService apply(String threadNameFormat) {
|
||||
// final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||
// final String threadName = String.format(threadNameFormat, "Virtual");
|
||||
// return Executors.newThreadPerTaskExecutor(r -> {
|
||||
// Thread t = factory.newThread(r);
|
||||
// t.setName(threadName);
|
||||
// return t;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static class VirtualThreadLocal<T> extends ThreadLocal<T> implements Function<Supplier<T>, ThreadLocal<T>> {
|
||||
//
|
||||
// private final Supplier<T> supplier;
|
||||
//
|
||||
// public VirtualThreadLocal(Supplier<T> supplier) {
|
||||
// this.supplier = supplier;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public ThreadLocal<T> apply(Supplier<T> supplier) {
|
||||
// return new VirtualThreadLocal<>(supplier);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// protected T initialValue() {
|
||||
// return supplier.get();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void set(T value) {
|
||||
// Thread t = Thread.currentThread();
|
||||
// if (!t.isVirtual()) {
|
||||
// super.set(value);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public T get() {
|
||||
// Thread t = Thread.currentThread();
|
||||
// return t.isVirtual() ? initialValue() : super.get();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static class VirtualThreadFactory implements ThreadFactory, Function<String, ThreadFactory> {
|
||||
//
|
||||
// private final ThreadFactory factory = Thread.ofVirtual().factory();
|
||||
//
|
||||
// private final String name;
|
||||
//
|
||||
// public VirtualThreadFactory(String name) {
|
||||
// this.name = name;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public ThreadFactory apply(String name) {
|
||||
// return new VirtualThreadFactory(name);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Thread newThread(Runnable r) {
|
||||
// Thread t = factory.newThread(r);
|
||||
// if (name != null) {
|
||||
// t.setName(name);
|
||||
// }
|
||||
// return t;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -42,7 +42,6 @@ public class RedkaleClassLoader extends URLClassLoader {
|
||||
"org.redkale.cluster",
|
||||
"org.redkale.cluster.spi",
|
||||
"org.redkale.convert",
|
||||
"org.redkale.convert.bson",
|
||||
"org.redkale.convert.ext",
|
||||
"org.redkale.convert.json",
|
||||
"org.redkale.convert.pb",
|
||||
|
||||
Reference in New Issue
Block a user