Skip to content

Commit 2371476

Browse files
committed
Add fast path for determining Codec
- Add `Codec.getMainClass()` for fast path, it allows users to extend `Codec` to change fast path - Add fast path to default codecs - Merge primitive type fast path into new fast path
1 parent 3e70b4a commit 2371476

21 files changed

+196
-109
lines changed

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/AbstractClassedCodec.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ public final boolean canDecode(MySqlReadableMetadata metadata, Class<?> target)
3636
return target.isAssignableFrom(this.type) && doCanDecode(metadata);
3737
}
3838

39+
@Override
40+
public final Class<? extends T> getMainClass() {
41+
return this.type;
42+
}
43+
3944
protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
4045
}

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/AbstractPrimitiveCodec.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {
4141

4242
@Override
4343
public final boolean canDecode(MySqlReadableMetadata metadata, Class<?> target) {
44-
return target.isAssignableFrom(boxedClass) && canPrimitiveDecode(metadata);
44+
return (target.isAssignableFrom(boxedClass) || target.equals(primitiveClass)) && doCanDecode(metadata);
4545
}
4646

4747
@Override
4848
public final Class<T> getPrimitiveClass() {
4949
return primitiveClass;
5050
}
51+
52+
@Override
53+
public final Class<? extends T> getMainClass() {
54+
return boxedClass;
55+
}
56+
57+
protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
5158
}

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/BlobCodec.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ final class BlobCodec implements MassiveCodec<Blob> {
4949
private BlobCodec() {
5050
}
5151

52+
@Override
53+
public Class<? extends Blob> getMainClass() {
54+
return Blob.class;
55+
}
56+
5257
@Override
5358
public Blob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
5459
CodecContext context) {

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/BooleanCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
5252
}
5353

5454
@Override
55-
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
55+
public boolean doCanDecode(MySqlReadableMetadata metadata) {
5656
MySqlType type = metadata.getType();
5757
return (type == MySqlType.BIT || type == MySqlType.TINYINT) &&
5858
Integer.valueOf(1).equals(metadata.getPrecision());

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/ByteCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
5252
}
5353

5454
@Override
55-
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
55+
public boolean doCanDecode(MySqlReadableMetadata metadata) {
5656
return metadata.getType().isNumeric();
5757
}
5858

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/ClobCodec.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ final class ClobCodec implements MassiveCodec<Clob> {
5252
private ClobCodec() {
5353
}
5454

55+
@Override
56+
public Class<? extends Clob> getMainClass() {
57+
return Clob.class;
58+
}
59+
5560
@Override
5661
public Clob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
5762
CodecContext context) {

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/Codec.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public interface Codec<T> {
4141
* @return the decoded result.
4242
*/
4343
@Nullable
44-
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
45-
CodecContext context);
44+
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary, CodecContext context);
4645

4746
/**
4847
* Checks if the field value can be decoded as specified {@link Class}.
@@ -69,4 +68,16 @@ T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean
6968
* @return encoded {@link MySqlParameter}.
7069
*/
7170
MySqlParameter encode(Object value, CodecContext context);
71+
72+
/**
73+
* Gets the main {@link Class} that is handled by this codec. It is used to fast path the codec lookup if it is not
74+
* {@code null}. If same main {@link Class} is handled by multiple codecs, the codec with the highest priority will
75+
* be used. The priority of the fast path is determined by its order in {@link Codecs}.
76+
*
77+
* @return the main {@link Class}, or {@code null} if it is not in fast path.
78+
*/
79+
@Nullable
80+
default Class<? extends T> getMainClass() {
81+
return null;
82+
}
7283
}

0 commit comments

Comments
 (0)