Skip to content

Commit

Permalink
sd
Browse files Browse the repository at this point in the history
  • Loading branch information
jchrys committed Jan 14, 2024
1 parent 182eaf6 commit 51259ef
Show file tree
Hide file tree
Showing 54 changed files with 218 additions and 301 deletions.
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);
}
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/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
16 changes: 6 additions & 10 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ClobCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@
*/
final class ClobCodec implements MassiveCodec<Clob> {

static final ClobCodec INSTANCE = new ClobCodec();

/**
* It should less than {@link BlobCodec}'s, because we can only make a minimum estimate before writing the
* string.
*/
private static final int MAX_MERGE = 1 << 13;

private final ByteBufAllocator allocator;

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

@Override
Expand Down Expand Up @@ -79,25 +78,22 @@ public boolean canEncode(Object value) {

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

private static class ClobMySqlParameter extends AbstractLobMySqlParameter {

private final ByteBufAllocator allocator;

private final AtomicReference<Clob> clob;

private final CodecContext context;

private ClobMySqlParameter(ByteBufAllocator allocator, Clob clob, CodecContext context) {
this.allocator = allocator;
private ClobMySqlParameter(Clob clob, CodecContext context) {
this.clob = new AtomicReference<>(clob);
this.context = context;
}

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

Expand Down
38 changes: 19 additions & 19 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,38 +281,38 @@ private static Codec<?>[] defaultCodecs(ByteBufAllocator allocator) {
ShortCodec.INSTANCE,
IntegerCodec.INSTANCE,
LongCodec.INSTANCE,
new BigIntegerCodec(allocator),
BigIntegerCodec.INSTANCE,

new BigDecimalCodec(allocator), // Only all decimals
BigDecimalCodec.INSTANCE, // Only all decimals
FloatCodec.INSTANCE, // Decimal (precision < 7) or float
DoubleCodec.INSTANCE, // Decimal (precision < 16) or double or float

BooleanCodec.INSTANCE,
new BitSetCodec(allocator),
BitSetCodec.INSTANCE,

new ZonedDateTimeCodec(allocator),
new LocalDateTimeCodec(allocator),
new InstantCodec(allocator),
new OffsetDateTimeCodec(allocator),
ZonedDateTimeCodec.INSTANCE,
LocalDateTimeCodec.INSTANCE,
InstantCodec.INSTANCE,
OffsetDateTimeCodec.INSTANCE,

new LocalDateCodec(allocator),
LocalDateCodec.INSTANCE,

new LocalTimeCodec(allocator),
new DurationCodec(allocator),
new OffsetTimeCodec(allocator),
LocalTimeCodec.INSTANCE,
DurationCodec.INSTANCE,
OffsetTimeCodec.INSTANCE,

new YearCodec(allocator),
YearCodec.INSTANCE,

new StringCodec(allocator),
StringCodec.INSTANCE,

new EnumCodec(allocator),
new SetCodec(allocator),
EnumCodec.INSTANCE,
SetCodec.INSTANCE,

new ClobCodec(allocator),
new BlobCodec(allocator),
ClobCodec.INSTANCE,
BlobCodec.INSTANCE,

new ByteBufferCodec(allocator),
new ByteArrayCodec(allocator)
ByteBufferCodec.INSTANCE,
ByteArrayCodec.INSTANCE
};
}

Expand Down
Loading

0 comments on commit 51259ef

Please sign in to comment.