支持数值型数组

master
剑器近 2020-11-11 17:22:14 +08:00
parent 514edd4796
commit 723640a000
8 changed files with 315 additions and 156 deletions

View File

@ -34,19 +34,13 @@ public abstract class FieldFactory {
Schema fieldSchema;
switch (dataType) {
case BYTE:
fieldSchema = IntSchema.Int8.INSTANCE;
break;
case WORD:
fieldSchema = IntSchema.Int16.INSTANCE;
break;
case DWORD:
if (Integer.TYPE.isAssignableFrom(typeClass) || Integer.class.isAssignableFrom(typeClass))
fieldSchema = IntSchema.Int32.INSTANCE;
else
fieldSchema = LongSchema.Long32.INSTANCE;
break;
case QWORD:
fieldSchema = LongSchema.Long64.INSTANCE;
if (typeClass.isArray())
fieldSchema = ArraySchema.getSchema(dataType);
else
fieldSchema = NumberSchema.getSchema(dataType, typeClass);
break;
case BCD8421:
if (LocalDateTime.class.isAssignableFrom(typeClass))
@ -60,7 +54,7 @@ public abstract class FieldFactory {
else if (ByteBuffer.class.isAssignableFrom(typeClass))
fieldSchema = ByteBufferSchema.INSTANCE;
else
fieldSchema = ByteArraySchema.INSTANCE;
fieldSchema = ArraySchema.ByteArraySchema.INSTANCE;
break;
case STRING:
fieldSchema = StringSchema.Chars.getInstance(field.pad(), field.charset());

View File

@ -1,7 +1,7 @@
package io.github.yezhihao.protostar;
import io.github.yezhihao.protostar.schema.ByteArraySchema;
import io.github.yezhihao.protostar.schema.IntSchema;
import io.github.yezhihao.protostar.schema.ArraySchema;
import io.github.yezhihao.protostar.schema.NumberSchema;
import java.util.HashMap;
import java.util.Map;
@ -36,16 +36,16 @@ public abstract class PrepareLoadStrategy extends IdStrategy {
public PrepareLoadStrategy addSchema(Object key, DataType dataType) {
switch (dataType) {
case BYTE:
this.typeIdMapping.put(key, IntSchema.Int8.INSTANCE);
this.typeIdMapping.put(key, NumberSchema.Int8.INSTANCE);
break;
case WORD:
this.typeIdMapping.put(key, IntSchema.Int16.INSTANCE);
this.typeIdMapping.put(key, NumberSchema.Int16.INSTANCE);
break;
case DWORD:
this.typeIdMapping.put(key, IntSchema.Int32.INSTANCE);
this.typeIdMapping.put(key, NumberSchema.Int32.INSTANCE);
break;
case BYTES:
this.typeIdMapping.put(key, ByteArraySchema.INSTANCE);
this.typeIdMapping.put(key, ArraySchema.ByteArraySchema.INSTANCE);
break;
default:
throw new RuntimeException("不支持的类型转换");

View File

@ -0,0 +1,185 @@
package io.github.yezhihao.protostar.schema;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.Schema;
import io.netty.buffer.ByteBuf;
public class ArraySchema {
public static Schema getSchema(DataType dataType) {
Schema schema;
switch (dataType) {
case BYTE:
schema = ByteArraySchema.INSTANCE;
break;
case WORD:
schema = ShortArraySchema.INSTANCE;
break;
case DWORD:
schema = IntArraySchema.INSTANCE;
break;
case QWORD:
schema = LongArraySchema.INSTANCE;
break;
default:
throw new RuntimeException("不支持的类型转换");
}
return schema;
}
public static class ByteArraySchema implements Schema<byte[]> {
public static final Schema INSTANCE = new ByteArraySchema();
private ByteArraySchema() {
}
@Override
public byte[] readFrom(ByteBuf input) {
byte[] array = new byte[input.readableBytes()];
input.readBytes(array);
return array;
}
@Override
public byte[] readFrom(ByteBuf input, int length) {
if (length < 0)
length = input.readableBytes();
byte[] array = new byte[length];
input.readBytes(array);
return array;
}
@Override
public void writeTo(ByteBuf output, byte[] array) {
output.writeBytes(array);
}
@Override
public void writeTo(ByteBuf output, int length, byte[] array) {
output.writeBytes(array, 0, length);
}
}
public static class ShortArraySchema implements Schema<short[]> {
public static final Schema INSTANCE = new ShortArraySchema();
private ShortArraySchema() {
}
@Override
public short[] readFrom(ByteBuf input) {
int total = input.readableBytes() >> 1;
short[] array = new short[total];
for (int i = 0; i < total; i++)
array[i] = input.readShort();
return array;
}
@Override
public short[] readFrom(ByteBuf input, int length) {
if (length < 0)
length = input.readableBytes();
int total = length >> 1;
short[] array = new short[total];
for (int i = 0; i < total; i++)
array[i] = input.readShort();
return array;
}
@Override
public void writeTo(ByteBuf output, short[] array) {
for (int i = 0; i < array.length; i++) {
output.writeShort(array[i]);
}
}
@Override
public void writeTo(ByteBuf output, int length, short[] array) {
for (int i = 0, total = length >> 1; i < total; i++) {
output.writeShort(array[i]);
}
}
}
public static class IntArraySchema implements Schema<int[]> {
public static final Schema INSTANCE = new IntArraySchema();
private IntArraySchema() {
}
@Override
public int[] readFrom(ByteBuf input) {
int total = input.readableBytes() >> 2;
int[] array = new int[total];
for (int i = 0; i < total; i++)
array[i] = input.readInt();
return array;
}
@Override
public int[] readFrom(ByteBuf input, int length) {
if (length < 0)
length = input.readableBytes();
int total = length >> 2;
int[] array = new int[total];
for (int i = 0; i < total; i++)
array[i] = input.readInt();
return array;
}
@Override
public void writeTo(ByteBuf output, int[] array) {
for (int i = 0; i < array.length; i++) {
output.writeInt(array[i]);
}
}
@Override
public void writeTo(ByteBuf output, int length, int[] array) {
for (int i = 0, total = length >> 2; i < total; i++) {
output.writeInt(array[i]);
}
}
}
public static class LongArraySchema implements Schema<long[]> {
public static final Schema INSTANCE = new LongArraySchema();
private LongArraySchema() {
}
@Override
public long[] readFrom(ByteBuf input) {
int total = input.readableBytes() >> 3;
long[] array = new long[total];
for (int i = 0; i < total; i++)
array[i] = input.readLong();
return array;
}
@Override
public long[] readFrom(ByteBuf input, int length) {
if (length < 0)
length = input.readableBytes();
int total = length >> 3;
long[] array = new long[total];
for (int i = 0; i < total; i++)
array[i] = input.readLong();
return array;
}
@Override
public void writeTo(ByteBuf output, long[] array) {
for (int i = 0; i < array.length; i++) {
output.writeLong(array[i]);
}
}
@Override
public void writeTo(ByteBuf output, int length, long[] array) {
for (int i = 0, total = length >> 3; i < total; i++) {
output.writeLong(array[i]);
}
}
}
}

View File

@ -1,38 +0,0 @@
package io.github.yezhihao.protostar.schema;
import io.netty.buffer.ByteBuf;
import io.github.yezhihao.protostar.Schema;
public class ByteArraySchema implements Schema<byte[]> {
public static final Schema INSTANCE = new ByteArraySchema();
private ByteArraySchema() {
}
@Override
public byte[] readFrom(ByteBuf input) {
byte[] message = new byte[input.readableBytes()];
input.readBytes(message);
return message;
}
@Override
public byte[] readFrom(ByteBuf input, int length) {
if (length < 0)
length = input.readableBytes();
byte[] bytes = new byte[length];
input.readBytes(bytes);
return bytes;
}
@Override
public void writeTo(ByteBuf output, byte[] value) {
output.writeBytes(value);
}
@Override
public void writeTo(ByteBuf output, int length, byte[] message) {
output.writeBytes(message, 0, length);
}
}

View File

@ -1,58 +0,0 @@
package io.github.yezhihao.protostar.schema;
import io.netty.buffer.ByteBuf;
import io.github.yezhihao.protostar.Schema;
public class IntSchema {
public static class Int8 implements Schema<Integer> {
public static final Schema INSTANCE = new Int8();
private Int8() {
}
@Override
public Integer readFrom(ByteBuf input) {
return (int) input.readUnsignedByte();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeByte(value);
}
}
public static class Int16 implements Schema<Integer> {
public static final Schema INSTANCE = new Int16();
private Int16() {
}
@Override
public Integer readFrom(ByteBuf input) {
return input.readUnsignedShort();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeShort(value);
}
}
public static class Int32 implements Schema<Integer> {
public static final Schema INSTANCE = new Int32();
private Int32() {
}
@Override
public Integer readFrom(ByteBuf input) {
return (int) input.readUnsignedInt();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeInt(value);
}
}
}

View File

@ -1,41 +0,0 @@
package io.github.yezhihao.protostar.schema;
import io.netty.buffer.ByteBuf;
import io.github.yezhihao.protostar.Schema;
public class LongSchema {
public static class Long32 implements Schema<Long> {
public static final Schema INSTANCE = new Long32();
private Long32() {
}
@Override
public Long readFrom(ByteBuf input) {
return input.readUnsignedInt();
}
@Override
public void writeTo(ByteBuf output, Long value) {
output.writeInt(value.intValue());
}
}
public static class Long64 implements Schema<Long> {
public static final Schema INSTANCE = new Long64();
private Long64() {
}
@Override
public Long readFrom(ByteBuf input) {
return input.readLong();
}
@Override
public void writeTo(ByteBuf output, Long value) {
output.writeLong(value.intValue());
}
}
}

View File

@ -0,0 +1,117 @@
package io.github.yezhihao.protostar.schema;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.Schema;
import io.netty.buffer.ByteBuf;
public class NumberSchema {
public static Schema getSchema(DataType dataType, Class<?> typeClass) {
Schema schema;
switch (dataType) {
case BYTE:
schema = Int8.INSTANCE;
break;
case WORD:
schema = Int16.INSTANCE;
break;
case DWORD:
if (Integer.TYPE.isAssignableFrom(typeClass) || Integer.class.isAssignableFrom(typeClass))
schema = NumberSchema.Int32.INSTANCE;
else
schema = NumberSchema.Long32.INSTANCE;
break;
case QWORD:
schema = Long64.INSTANCE;
break;
default:
throw new RuntimeException("不支持的类型转换");
}
return schema;
}
public static class Int8 implements Schema<Integer> {
public static final Schema INSTANCE = new Int8();
private Int8() {
}
@Override
public Integer readFrom(ByteBuf input) {
return (int) input.readUnsignedByte();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeByte(value);
}
}
public static class Int16 implements Schema<Integer> {
public static final Schema INSTANCE = new Int16();
private Int16() {
}
@Override
public Integer readFrom(ByteBuf input) {
return input.readUnsignedShort();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeShort(value);
}
}
public static class Int32 implements Schema<Integer> {
public static final Schema INSTANCE = new Int32();
private Int32() {
}
@Override
public Integer readFrom(ByteBuf input) {
return input.readInt();
}
@Override
public void writeTo(ByteBuf output, Integer value) {
output.writeInt(value);
}
}
public static class Long32 implements Schema<Long> {
public static final Schema INSTANCE = new Long32();
private Long32() {
}
@Override
public Long readFrom(ByteBuf input) {
return input.readUnsignedInt();
}
@Override
public void writeTo(ByteBuf output, Long value) {
output.writeInt(value.intValue());
}
}
public static class Long64 implements Schema<Long> {
public static final Schema INSTANCE = new Long64();
private Long64() {
}
@Override
public Long readFrom(ByteBuf input) {
return input.readLong();
}
@Override
public void writeTo(ByteBuf output, Long value) {
output.writeLong(value.intValue());
}
}
}

View File

@ -2,7 +2,7 @@ package io.github.yezhihao.protostar.convert;
import io.github.yezhihao.protostar.IdStrategy;
import io.github.yezhihao.protostar.PrepareLoadStrategy;
import io.github.yezhihao.protostar.schema.IntSchema;
import io.github.yezhihao.protostar.schema.NumberSchema;
import io.github.yezhihao.protostar.schema.StringSchema;
public class AttributeType extends PrepareLoadStrategy {
@ -12,7 +12,7 @@ public class AttributeType extends PrepareLoadStrategy {
@Override
protected void addSchemas(PrepareLoadStrategy schemaRegistry) {
schemaRegistry
.addSchema(1, IntSchema.Int32.INSTANCE)
.addSchema(1, NumberSchema.Int32.INSTANCE)
.addSchema(2, StringSchema.Chars.getInstance((byte) 0, "GBK"))
.addSchema(3, Attr1.class)