Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton Codecs #197

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/io/asyncer/r2dbc/mysql/MySqlParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.asyncer.r2dbc.mysql.constant.MySqlType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.reactivestreams.Publisher;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -50,9 +51,11 @@ default boolean isNull() {
* If we don't support multiple times writing, it will be hard to understand and maybe make a confuse to
* user.
*
* @param allocator the buffer allocator.
*
* @return the encoded binary buffer(s).
jchrys marked this conversation as resolved.
Show resolved Hide resolved
*/
Publisher<ByteBuf> publishBinary();
Publisher<ByteBuf> publishBinary(ByteBufAllocator allocator);

/**
* Text protocol encoding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.asyncer.r2dbc.mysql.codec;

import io.asyncer.r2dbc.mysql.MySqlColumnMetadata;
import io.netty.buffer.ByteBufAllocator;

/**
* Codec for classed type when field bytes less or equals than {@link Integer#MAX_VALUE}.
Expand All @@ -26,12 +25,9 @@
*/
abstract class AbstractClassedCodec<T> implements Codec<T> {

protected final ByteBufAllocator allocator;

private final Class<? extends T> type;

AbstractClassedCodec(ByteBufAllocator allocator, Class<? extends T> type) {
this.allocator = allocator;
AbstractClassedCodec(Class<? extends T> type) {
this.type = type;
}

Expand All @@ -40,5 +36,5 @@ public final boolean canDecode(MySqlColumnMetadata metadata, Class<?> target) {
return target.isAssignableFrom(this.type) && doCanDecode(metadata);
}

abstract protected boolean doCanDecode(MySqlColumnMetadata metadata);
protected abstract boolean doCanDecode(MySqlColumnMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@
*/
abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {

protected final ByteBufAllocator allocator;

private final Class<T> primitiveClass;

private final Class<T> boxedClass;

AbstractPrimitiveCodec(ByteBufAllocator allocator, Class<T> primitiveClass, Class<T> boxedClass) {
AbstractPrimitiveCodec(Class<T> primitiveClass, Class<T> boxedClass) {
require(primitiveClass.isPrimitive() && !boxedClass.isPrimitive(),
"primitiveClass must be primitive and boxedClass must not be primitive");

this.allocator = allocator;
this.primitiveClass = primitiveClass;
this.boxedClass = boxedClass;
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BigDecimalCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
*/
final class BigDecimalCodec extends AbstractClassedCodec<BigDecimal> {

BigDecimalCodec(ByteBufAllocator allocator) {
super(allocator, BigDecimal.class);
static final BigDecimalCodec INSTANCE = new BigDecimalCodec();

private BigDecimalCodec() {
super(BigDecimal.class);
}

@Override
Expand Down Expand Up @@ -76,7 +78,7 @@ public boolean canEncode(Object value) {

@Override
public MySqlParameter encode(Object value, CodecContext context) {
return new BigDecimalMySqlParameter(allocator, (BigDecimal) value);
return new BigDecimalMySqlParameter((BigDecimal) value);
}

@Override
Expand Down Expand Up @@ -128,17 +130,14 @@ private static BigDecimal parseBigDecimal(ByteBuf buf) {

private static final class BigDecimalMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final BigDecimal value;

private BigDecimalMySqlParameter(ByteBufAllocator allocator, BigDecimal value) {
this.allocator = allocator;
private BigDecimalMySqlParameter(BigDecimal value) {
this.value = value;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Mono.fromSupplier(() -> CodecUtils.encodeAscii(allocator, value.toString()));
}

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BigIntegerCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
*/
final class BigIntegerCodec extends AbstractClassedCodec<BigInteger> {

BigIntegerCodec(ByteBufAllocator allocator) {
super(allocator, BigInteger.class);
static final BigIntegerCodec INSTANCE = new BigIntegerCodec();

private BigIntegerCodec() {
super(BigInteger.class);
}

@Override
Expand Down Expand Up @@ -84,10 +86,10 @@ public MySqlParameter encode(Object value, CodecContext context) {
BigInteger i = (BigInteger) value;

if (i.bitLength() < Long.SIZE) {
return LongCodec.encodeLong(allocator, i.longValue());
return LongCodec.encodeLong(i.longValue());
}

return new BigIntegerMySqlParameter(allocator, (BigInteger) value);
return new BigIntegerMySqlParameter((BigInteger) value);
}

@Override
Expand Down Expand Up @@ -140,17 +142,14 @@ private static BigInteger decimalBigInteger(ByteBuf buf) {

private static class BigIntegerMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final BigInteger value;

private BigIntegerMySqlParameter(ByteBufAllocator allocator, BigInteger value) {
this.allocator = allocator;
private BigIntegerMySqlParameter(BigInteger value) {
this.value = value;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Mono.fromSupplier(() -> CodecUtils.encodeAscii(allocator, value.toString()));
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BitSetCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
*/
final class BitSetCodec extends AbstractClassedCodec<BitSet> {

BitSetCodec(ByteBufAllocator allocator) {
super(allocator, BitSet.class);
static final BitSetCodec INSTANCE = new BitSetCodec();

private BitSetCodec() {
super(BitSet.class);
}

@Override
Expand Down Expand Up @@ -85,7 +87,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
type = MySqlType.BIGINT;
}

return new BitSetMySqlParameter(allocator, bits, type);
return new BitSetMySqlParameter(bits, type);
}

@Override
Expand All @@ -109,20 +111,17 @@ private static byte[] reverse(byte[] bytes) {

private static final class BitSetMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final long value;

private final MySqlType type;

private BitSetMySqlParameter(ByteBufAllocator allocator, long value, MySqlType type) {
this.allocator = allocator;
private BitSetMySqlParameter(long value, MySqlType type) {
this.value = value;
this.type = type;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
switch (type) {
case TINYINT:
return Mono.fromSupplier(() -> allocator.buffer(Byte.BYTES).writeByte((int) value));
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BlobCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@
*/
final class BlobCodec implements MassiveCodec<Blob> {

private static final int MAX_MERGE = 1 << 14;
static final BlobCodec INSTANCE = new BlobCodec();

private final ByteBufAllocator allocator;
private static final int MAX_MERGE = 1 << 14;

BlobCodec(ByteBufAllocator allocator) {
this.allocator = allocator;
private BlobCodec() {
}

@Override
Expand Down Expand Up @@ -76,7 +75,7 @@ public boolean canEncode(Object value) {

@Override
public MySqlParameter encode(Object value, CodecContext context) {
return new BlobMySqlParameter(allocator, (Blob) value);
return new BlobMySqlParameter((Blob) value);
}

static List<ByteBuf> toList(List<ByteBuf> buffers) {
Expand Down Expand Up @@ -107,17 +106,14 @@ static void releaseAll(List<ByteBuf> buffers, ByteBuf lastBuf) {

private static final class BlobMySqlParameter extends AbstractLobMySqlParameter {

private final ByteBufAllocator allocator;

private final AtomicReference<Blob> blob;

private BlobMySqlParameter(ByteBufAllocator allocator, Blob blob) {
this.allocator = allocator;
private BlobMySqlParameter(Blob blob) {
this.blob = new AtomicReference<>(blob);
}

@Override
public Flux<ByteBuf> publishBinary() {
public Flux<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Flux.defer(() -> {
Blob blob = this.blob.getAndSet(null);

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BooleanCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
*/
final class BooleanCodec extends AbstractPrimitiveCodec<Boolean> {

BooleanCodec(ByteBufAllocator allocator) {
super(allocator, Boolean.TYPE, Boolean.class);
static final BooleanCodec INSTANCE = new BooleanCodec();

private BooleanCodec() {
super(Boolean.TYPE, Boolean.class);
}

@Override
Expand All @@ -46,7 +48,7 @@ public boolean canEncode(Object value) {

@Override
public MySqlParameter encode(Object value, CodecContext context) {
return new BooleanMySqlParameter(allocator, (Boolean) value);
return new BooleanMySqlParameter((Boolean) value);
}

@Override
Expand All @@ -57,17 +59,14 @@ public boolean canPrimitiveDecode(MySqlColumnMetadata metadata) {

private static final class BooleanMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final boolean value;

private BooleanMySqlParameter(ByteBufAllocator allocator, boolean value) {
this.allocator = allocator;
private BooleanMySqlParameter(boolean value) {
this.value = value;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Mono.fromSupplier(() -> allocator.buffer(Byte.BYTES).writeByte(value ? 1 : 0));
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ByteArrayCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
*/
final class ByteArrayCodec extends AbstractClassedCodec<byte[]> {

ByteArrayCodec(ByteBufAllocator allocator) {
super(allocator, byte[].class);
static final ByteArrayCodec INSTANCE = new ByteArrayCodec();

private ByteArrayCodec() {
super(byte[].class);
}

@Override
Expand All @@ -56,7 +58,7 @@ public boolean canEncode(Object value) {

@Override
public MySqlParameter encode(Object value, CodecContext context) {
return new ByteArrayMySqlParameter(allocator, (byte[]) value);
return new ByteArrayMySqlParameter((byte[]) value);
}

@Override
Expand Down Expand Up @@ -85,17 +87,14 @@ static ByteBuf encodeBytes(ByteBufAllocator alloc, byte[] value) {

private static final class ByteArrayMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final byte[] value;

private ByteArrayMySqlParameter(ByteBufAllocator allocator, byte[] value) {
this.allocator = allocator;
private ByteArrayMySqlParameter(byte[] value) {
this.value = value;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Mono.fromSupplier(() -> encodeBytes(allocator, value));
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ByteBufferCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
*/
final class ByteBufferCodec extends AbstractClassedCodec<ByteBuffer> {

ByteBufferCodec(ByteBufAllocator allocator) {
super(allocator, ByteBuffer.class);
static final ByteBufferCodec INSTANCE = new ByteBufferCodec();

private ByteBufferCodec() {
super(ByteBuffer.class);
}

@Override
Expand All @@ -55,7 +57,7 @@ public ByteBuffer decode(ByteBuf value, MySqlColumnMetadata metadata, Class<?> t

@Override
public MySqlParameter encode(Object value, CodecContext context) {
return new ByteBufferMySqlParameter(allocator, (ByteBuffer) value);
return new ByteBufferMySqlParameter((ByteBuffer) value);
}

@Override
Expand All @@ -70,17 +72,14 @@ protected boolean doCanDecode(MySqlColumnMetadata metadata) {

private static final class ByteBufferMySqlParameter extends AbstractMySqlParameter {

private final ByteBufAllocator allocator;

private final ByteBuffer buffer;

private ByteBufferMySqlParameter(ByteBufAllocator allocator, ByteBuffer buffer) {
this.allocator = allocator;
private ByteBufferMySqlParameter(ByteBuffer buffer) {
this.buffer = buffer;
}

@Override
public Mono<ByteBuf> publishBinary() {
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
return Mono.fromSupplier(() -> {
if (!buffer.hasRemaining()) {
// It is zero of var int, not terminal.
Expand Down
Loading