增加ConvertSmallString功能

This commit is contained in:
Redkale
2021-01-20 17:37:29 +08:00
parent 4e689855f4
commit d997d3a7bb
5 changed files with 85 additions and 7 deletions

View File

@@ -0,0 +1,27 @@
/*
* 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;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.3.0
*
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface ConvertSmallString {
}

View File

@@ -8,6 +8,7 @@ package org.redkale.convert;
import org.redkale.util.Creator; import org.redkale.util.Creator;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
import org.redkale.convert.ext.StringSimpledCoder;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
@@ -96,7 +97,13 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
if (factory.isConvertDisabled(field)) continue; if (factory.isConvertDisabled(field)) continue;
ref = factory.findRef(clazz, field); ref = factory.findRef(clazz, field);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Decodeable<R, ?> fieldCoder = factory.findFieldCoder(clazz, field.getName()); ConvertSmallString small = field.getAnnotation(ConvertSmallString.class);
Decodeable<R, ?> fieldCoder;
if (small != null && field.getType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
} else {
fieldCoder = factory.findFieldCoder(clazz, field.getName());
}
if (fieldCoder == null) { if (fieldCoder == null) {
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
fieldCoder = factory.loadDecoder(t); fieldCoder = factory.loadDecoder(t);
@@ -126,7 +133,13 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
ref = factory.findRef(clazz, method); ref = factory.findRef(clazz, method);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Decodeable<R, ?> fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method)); ConvertSmallString small = method.getAnnotation(ConvertSmallString.class);
Decodeable<R, ?> fieldCoder;
if (small != null && method.getReturnType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
} else {
fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
}
if (fieldCoder == null) { if (fieldCoder == null) {
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericParameterTypes()[0], this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericParameterTypes()[0], this.type), this.type);
fieldCoder = factory.loadDecoder(t); fieldCoder = factory.loadDecoder(t);

View File

@@ -7,6 +7,7 @@ package org.redkale.convert;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
import org.redkale.convert.ext.StringSimpledCoder;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
@@ -77,7 +78,13 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
if (factory.isConvertDisabled(field)) continue; if (factory.isConvertDisabled(field)) continue;
ref = factory.findRef(clazz, field); ref = factory.findRef(clazz, field);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Encodeable<W, ?> fieldCoder = factory.findFieldCoder(clazz, field.getName()); ConvertSmallString small = field.getAnnotation(ConvertSmallString.class);
Encodeable<W, ?> fieldCoder;
if (small != null && field.getType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
} else {
fieldCoder = factory.findFieldCoder(clazz, field.getName());
}
if (fieldCoder == null) { if (fieldCoder == null) {
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
fieldCoder = factory.loadEncoder(t); fieldCoder = factory.loadEncoder(t);
@@ -106,7 +113,13 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
} }
ref = factory.findRef(clazz, method); ref = factory.findRef(clazz, method);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Encodeable<W, ?> fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method)); ConvertSmallString small = method.getAnnotation(ConvertSmallString.class);
Encodeable<W, ?> fieldCoder;
if (small != null && method.getReturnType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
} else {
fieldCoder = factory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
}
if (fieldCoder == null) { if (fieldCoder == null) {
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type);
fieldCoder = factory.loadEncoder(t); fieldCoder = factory.loadEncoder(t);

View File

@@ -12,7 +12,9 @@ import org.redkale.convert.Writer;
/** /**
* String 的SimpledCoder实现 * String 的SimpledCoder实现
* *
* <p> 详情见: https://redkale.org * <p>
* 详情见: https://redkale.org
*
* @author zhangjx * @author zhangjx
* @param <R> Reader输入的子类型 * @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型 * @param <W> Writer输出的子类型
@@ -31,4 +33,19 @@ public final class StringSimpledCoder<R extends Reader, W extends Writer> extend
return in.readString(); return in.readString();
} }
public final static class SmallStringSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, String> {
public static final SmallStringSimpledCoder instance = new SmallStringSimpledCoder();
@Override
public void convertTo(W out, String value) {
out.writeSmallString(value);
}
@Override
public String convertFrom(R in) {
return in.readSmallString();
}
}
} }

View File

@@ -57,8 +57,8 @@ public abstract class AsyncConnection implements AutoCloseable {
this(bufferPool, bufferPool, sslContext, livingCounter, closedCounter); this(bufferPool, bufferPool, sslContext, livingCounter, closedCounter);
} }
protected AsyncConnection(Supplier<ByteBuffer> bufferSupplier, Consumer<ByteBuffer> bufferConsumer, SSLContext sslContext, protected AsyncConnection(Supplier<ByteBuffer> bufferSupplier, Consumer<ByteBuffer> bufferConsumer,
final AtomicLong livingCounter, final AtomicLong closedCounter) { SSLContext sslContext, final AtomicLong livingCounter, final AtomicLong closedCounter) {
Objects.requireNonNull(bufferSupplier); Objects.requireNonNull(bufferSupplier);
Objects.requireNonNull(bufferConsumer); Objects.requireNonNull(bufferConsumer);
this.bufferSupplier = bufferSupplier; this.bufferSupplier = bufferSupplier;
@@ -135,6 +135,14 @@ public abstract class AsyncConnection implements AutoCloseable {
this.readBuffer = buffer; this.readBuffer = buffer;
} }
public static class Message {
public String value;
public Message() {
}
}
public ByteBuffer pollReadBuffer() { public ByteBuffer pollReadBuffer() {
ByteBuffer rs = this.readBuffer; ByteBuffer rs = this.readBuffer;
if (rs != null) { if (rs != null) {