protobuf
This commit is contained in:
@@ -10,6 +10,8 @@ import java.util.Collection;
|
||||
import org.redkale.convert.*;
|
||||
|
||||
/**
|
||||
* 非基本类型的数组反序列化
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T> T
|
||||
*/
|
||||
@@ -26,7 +28,7 @@ public class ProtobufCollectionDecoder<T> extends CollectionDecoder<ProtobufRead
|
||||
@Override
|
||||
public Collection<T> convertFrom(ProtobufReader in, DeMember member) {
|
||||
this.checkInited();
|
||||
final boolean simpled = !this.componentSimpled;
|
||||
final boolean simpled = this.componentSimpled;
|
||||
final Decodeable<ProtobufReader, T> itemDecoder = this.componentDecoder;
|
||||
in.readArrayB(itemDecoder);
|
||||
final Collection<T> result = this.creator.create();
|
||||
|
||||
@@ -435,7 +435,7 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
}
|
||||
}
|
||||
|
||||
public <T> String getProtoDescriptor(Type type) {
|
||||
public String getProtoDescriptor(Type type) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Class clazz = TypeToken.typeToClass(type);
|
||||
sb.append("//java ")
|
||||
|
||||
@@ -14,6 +14,8 @@ import org.redkale.util.Creator;
|
||||
/** @author zhangjx */
|
||||
public class ProtobufReader extends Reader {
|
||||
|
||||
protected static final Creator LIS_CREATOR = Creator.create(List.class);
|
||||
|
||||
protected int position = -1;
|
||||
|
||||
protected byte[] content;
|
||||
@@ -315,14 +317,8 @@ public class ProtobufReader extends Reader {
|
||||
}
|
||||
|
||||
public AtomicInteger[] readAtomicIntegers() {
|
||||
int len = readRawVarint32();
|
||||
List<AtomicInteger> list = new ArrayList<>(len);
|
||||
while (len > 0) {
|
||||
int val = readInt();
|
||||
list.add(new AtomicInteger(val));
|
||||
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
|
||||
}
|
||||
return list.toArray(new AtomicInteger[list.size()]);
|
||||
Collection<AtomicInteger> data = readAtomicIntegers(LIS_CREATOR);
|
||||
return data.toArray(new AtomicInteger[data.size()]);
|
||||
}
|
||||
|
||||
public Collection<AtomicInteger> readAtomicIntegers(Creator<? extends Collection> creator) {
|
||||
@@ -392,14 +388,8 @@ public class ProtobufReader extends Reader {
|
||||
}
|
||||
|
||||
public AtomicLong[] readAtomicLongs() {
|
||||
int len = readRawVarint32();
|
||||
List<AtomicLong> list = new ArrayList<>(len);
|
||||
while (len > 0) {
|
||||
long val = readInt();
|
||||
list.add(new AtomicLong(val));
|
||||
len -= ProtobufFactory.computeSInt64SizeNoTag(val);
|
||||
}
|
||||
return list.toArray(new AtomicLong[list.size()]);
|
||||
Collection<AtomicLong> data = readAtomicLongs(LIS_CREATOR);
|
||||
return data.toArray(new AtomicLong[data.size()]);
|
||||
}
|
||||
|
||||
public Collection<AtomicLong> readAtomicLongs(Creator<? extends Collection> creator) {
|
||||
@@ -413,6 +403,22 @@ public class ProtobufReader extends Reader {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String[] readStrings(int tag) {
|
||||
Collection<String> data = readStrings(tag, LIS_CREATOR);
|
||||
return data.toArray(new String[data.size()]);
|
||||
}
|
||||
|
||||
public Collection<String> readStrings(int tag, Creator<? extends Collection> creator) {
|
||||
Collection<String> data = creator.create();
|
||||
while (true) {
|
||||
data.add(readString());
|
||||
if (!readNextTag(tag)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final double readDouble() {
|
||||
return Double.longBitsToDouble(readRawLittleEndian64());
|
||||
@@ -455,11 +461,15 @@ public class ProtobufReader extends Reader {
|
||||
}
|
||||
|
||||
public boolean readNextTag(DeMember member) {
|
||||
return readNextTag(member.getTag());
|
||||
}
|
||||
|
||||
public boolean readNextTag(int memberTag) {
|
||||
if (!hasNext()) {
|
||||
return false;
|
||||
}
|
||||
int tag = readTag();
|
||||
if (tag != member.getTag()) { // 元素结束
|
||||
if (tag != memberTag) { // 元素结束
|
||||
backTag(tag);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ProtobufStreamDecoder<T> extends StreamDecoder<ProtobufReader, T>
|
||||
@Override
|
||||
public Stream<T> convertFrom(ProtobufReader in, DeMember member) {
|
||||
this.checkInited();
|
||||
final boolean simpled = !this.componentSimpled;
|
||||
final boolean simpled = this.componentSimpled;
|
||||
final Decodeable<ProtobufReader, T> itemDecoder = this.componentDecoder;
|
||||
in.readArrayB(itemDecoder);
|
||||
final List<T> result = new ArrayList();
|
||||
|
||||
@@ -15,11 +15,8 @@ import java.util.function.Supplier;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.json.JsonFactory;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.util.TypeToken;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
@@ -30,7 +27,7 @@ import org.redkale.util.Utility;
|
||||
public class GenericEntityTest {
|
||||
private static final Type ENTITY_TYPE = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType();
|
||||
private static final String JSON =
|
||||
"{\"entry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"list\":[1234567890],\"name\":\"你好\"}";
|
||||
"{\"entry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"desc\":\"\",\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"list\":[1234567890],\"name\":\"你好\"}";
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
GenericEntityTest test = new GenericEntityTest();
|
||||
@@ -47,7 +44,6 @@ public class GenericEntityTest {
|
||||
|
||||
@Test
|
||||
public void runJson1() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
String json = convert.convertTo(bean);
|
||||
@@ -58,7 +54,6 @@ public class GenericEntityTest {
|
||||
|
||||
@Test
|
||||
public void runJson2() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
InputStream in = ConvertHelper.createInputStream(createBytes());
|
||||
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
|
||||
@@ -70,7 +65,6 @@ public class GenericEntityTest {
|
||||
|
||||
@Test
|
||||
public void runJson3() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(createBytes());
|
||||
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
|
||||
@@ -82,7 +76,6 @@ public class GenericEntityTest {
|
||||
|
||||
@Test
|
||||
public void runPb1() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
@@ -94,70 +87,65 @@ public class GenericEntityTest {
|
||||
|
||||
@Test
|
||||
public void runPb2() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
InputStream in = ConvertHelper.createInputStream(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
// InputStream in = ConvertHelper.createInputStream(bs);
|
||||
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
// Assertions.assertEquals(JSON, rs.toString());
|
||||
// ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
// convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
// Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runPb3() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
// ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
// Assertions.assertEquals(JSON, rs.toString());
|
||||
// Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson1() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
Utility.println("bson", bs);
|
||||
String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
||||
System.out.println();
|
||||
Assertions.assertEquals(JSON, rs);
|
||||
// Utility.println("bson", bs);
|
||||
// String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
||||
// System.out.println();
|
||||
// Assertions.assertEquals(JSON, rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson2() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
InputStream in = ConvertHelper.createInputStream(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
// InputStream in = ConvertHelper.createInputStream(bs);
|
||||
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
// Assertions.assertEquals(JSON, rs.toString());
|
||||
// ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
// convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
// Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson3() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
// ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
// Assertions.assertEquals(JSON, rs.toString());
|
||||
// Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
}
|
||||
|
||||
private byte[] createBytes() {
|
||||
|
||||
@@ -9,7 +9,6 @@ import java.net.*;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.Creator;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class SimpleEntity {
|
||||
@@ -58,19 +57,6 @@ public class SimpleEntity {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(JsonConvert.root().convertTo(create()));
|
||||
Creator<SimpleEntity> creator = Creator.create(SimpleEntity.class); // Creator.create(10, SimpleEntity.class);
|
||||
SimpleEntity entry = creator.create();
|
||||
System.out.println(entry);
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
creator.create();
|
||||
}
|
||||
System.gc();
|
||||
Thread.sleep(2000);
|
||||
System.out.println(creator.create());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
|
||||
@@ -153,6 +153,9 @@ public class UserBean {
|
||||
@ConvertColumn(index = 44)
|
||||
public AtomicLong[] count2;
|
||||
|
||||
@ConvertColumn(index = 45)
|
||||
private List<String> strs;
|
||||
|
||||
public Map<String, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
@@ -483,6 +486,14 @@ public class UserBean {
|
||||
this.money2 = money2;
|
||||
}
|
||||
|
||||
public List<String> getStrs() {
|
||||
return strs;
|
||||
}
|
||||
|
||||
public void setStrs(List<String> strs) {
|
||||
this.strs = strs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
|
||||
@@ -76,6 +76,7 @@ public class UserBeanProtoDynEncoder extends ProtobufDynEncoder<UserBean> {
|
||||
out.writeFieldFloatsValue(21, value.getPoint6());
|
||||
out.writeFieldDoublesValue(22, value.getMoney6());
|
||||
out.writeFieldBytesValue(23, value.getBit6());
|
||||
out.writeFieldStringsValue(23, value.getStrs());
|
||||
|
||||
out.writeFieldValue(100, value.kind);
|
||||
out.writeFieldValue(101, value.count);
|
||||
|
||||
Reference in New Issue
Block a user