Skip to content

Commit

Permalink
Add fast path for determining Codec
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mirromutth committed Mar 18, 2024
1 parent 3e70b4a commit 2371476
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ public final boolean canDecode(MySqlReadableMetadata metadata, Class<?> target)
return target.isAssignableFrom(this.type) && doCanDecode(metadata);
}

@Override
public final Class<? extends T> getMainClass() {
return this.type;
}

protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {

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

@Override
public final Class<T> getPrimitiveClass() {
return primitiveClass;
}

@Override
public final Class<? extends T> getMainClass() {
return boxedClass;
}

protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class BlobCodec implements MassiveCodec<Blob> {
private BlobCodec() {
}

@Override
public Class<? extends Blob> getMainClass() {
return Blob.class;
}

@Override
public Blob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
}

@Override
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
public boolean doCanDecode(MySqlReadableMetadata metadata) {
MySqlType type = metadata.getType();
return (type == MySqlType.BIT || type == MySqlType.TINYINT) &&
Integer.valueOf(1).equals(metadata.getPrecision());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
}

@Override
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
public boolean doCanDecode(MySqlReadableMetadata metadata) {
return metadata.getType().isNumeric();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ final class ClobCodec implements MassiveCodec<Clob> {
private ClobCodec() {
}

@Override
public Class<? extends Clob> getMainClass() {
return Clob.class;
}

@Override
public Clob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context) {
Expand Down
15 changes: 13 additions & 2 deletions r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public interface Codec<T> {
* @return the decoded result.
*/
@Nullable
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context);
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary, CodecContext context);

/**
* Checks if the field value can be decoded as specified {@link Class}.
Expand All @@ -69,4 +68,16 @@ T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean
* @return encoded {@link MySqlParameter}.
*/
MySqlParameter encode(Object value, CodecContext context);

/**
* Gets the main {@link Class} that is handled by this codec. It is used to fast path the codec lookup if it is not
* {@code null}. If same main {@link Class} is handled by multiple codecs, the codec with the highest priority will
* be used. The priority of the fast path is determined by its order in {@link Codecs}.
*
* @return the main {@link Class}, or {@code null} if it is not in fast path.
*/
@Nullable
default Class<? extends T> getMainClass() {
return null;
}
}
Loading

0 comments on commit 2371476

Please sign in to comment.