Skip to content

Commit 96c11dc

Browse files
authored
Singleton Codecs (#197)
Singleton Codecs Motivation: A single instance of the Codec is sufficient for the entire application. Modification: Implement Singleton pattern for codecs. Result: Reduced memory consumption and improved performance. resolves #196
1 parent e01335f commit 96c11dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+297
-384
lines changed

src/main/java/io/asyncer/r2dbc/mysql/MySqlParameter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.asyncer.r2dbc.mysql.constant.MySqlType;
2020
import io.netty.buffer.ByteBuf;
21+
import io.netty.buffer.ByteBufAllocator;
2122
import org.reactivestreams.Publisher;
2223
import reactor.core.Disposable;
2324
import reactor.core.publisher.Mono;
@@ -50,9 +51,11 @@ default boolean isNull() {
5051
* If we don't support multiple times writing, it will be hard to understand and maybe make a confuse to
5152
* user.
5253
*
54+
* @param allocator the buffer allocator.
55+
*
5356
* @return the encoded binary buffer(s).
5457
*/
55-
Publisher<ByteBuf> publishBinary();
58+
Publisher<ByteBuf> publishBinary(ByteBufAllocator allocator);
5659

5760
/**
5861
* Text protocol encoding.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.asyncer.r2dbc.mysql.codec;
1818

1919
import io.asyncer.r2dbc.mysql.MySqlColumnMetadata;
20-
import io.netty.buffer.ByteBufAllocator;
2120

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

29-
protected final ByteBufAllocator allocator;
30-
3128
private final Class<? extends T> type;
3229

33-
AbstractClassedCodec(ByteBufAllocator allocator, Class<? extends T> type) {
34-
this.allocator = allocator;
30+
AbstractClassedCodec(Class<? extends T> type) {
3531
this.type = type;
3632
}
3733

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

43-
abstract protected boolean doCanDecode(MySqlColumnMetadata metadata);
39+
protected abstract boolean doCanDecode(MySqlColumnMetadata metadata);
4440
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@
2828
*/
2929
abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {
3030

31-
protected final ByteBufAllocator allocator;
32-
3331
private final Class<T> primitiveClass;
3432

3533
private final Class<T> boxedClass;
3634

37-
AbstractPrimitiveCodec(ByteBufAllocator allocator, Class<T> primitiveClass, Class<T> boxedClass) {
35+
AbstractPrimitiveCodec(Class<T> primitiveClass, Class<T> boxedClass) {
3836
require(primitiveClass.isPrimitive() && !boxedClass.isPrimitive(),
3937
"primitiveClass must be primitive and boxedClass must not be primitive");
4038

41-
this.allocator = allocator;
4239
this.primitiveClass = primitiveClass;
4340
this.boxedClass = boxedClass;
4441
}

src/main/java/io/asyncer/r2dbc/mysql/codec/BigDecimalCodec.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
*/
3333
final class BigDecimalCodec extends AbstractClassedCodec<BigDecimal> {
3434

35-
BigDecimalCodec(ByteBufAllocator allocator) {
36-
super(allocator, BigDecimal.class);
35+
static final BigDecimalCodec INSTANCE = new BigDecimalCodec();
36+
37+
private BigDecimalCodec() {
38+
super(BigDecimal.class);
3739
}
3840

3941
@Override
@@ -76,7 +78,7 @@ public boolean canEncode(Object value) {
7678

7779
@Override
7880
public MySqlParameter encode(Object value, CodecContext context) {
79-
return new BigDecimalMySqlParameter(allocator, (BigDecimal) value);
81+
return new BigDecimalMySqlParameter((BigDecimal) value);
8082
}
8183

8284
@Override
@@ -128,17 +130,14 @@ private static BigDecimal parseBigDecimal(ByteBuf buf) {
128130

129131
private static final class BigDecimalMySqlParameter extends AbstractMySqlParameter {
130132

131-
private final ByteBufAllocator allocator;
132-
133133
private final BigDecimal value;
134134

135-
private BigDecimalMySqlParameter(ByteBufAllocator allocator, BigDecimal value) {
136-
this.allocator = allocator;
135+
private BigDecimalMySqlParameter(BigDecimal value) {
137136
this.value = value;
138137
}
139138

140139
@Override
141-
public Mono<ByteBuf> publishBinary() {
140+
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
142141
return Mono.fromSupplier(() -> CodecUtils.encodeAscii(allocator, value.toString()));
143142
}
144143

src/main/java/io/asyncer/r2dbc/mysql/codec/BigIntegerCodec.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
*/
3434
final class BigIntegerCodec extends AbstractClassedCodec<BigInteger> {
3535

36-
BigIntegerCodec(ByteBufAllocator allocator) {
37-
super(allocator, BigInteger.class);
36+
static final BigIntegerCodec INSTANCE = new BigIntegerCodec();
37+
38+
private BigIntegerCodec() {
39+
super(BigInteger.class);
3840
}
3941

4042
@Override
@@ -84,10 +86,10 @@ public MySqlParameter encode(Object value, CodecContext context) {
8486
BigInteger i = (BigInteger) value;
8587

8688
if (i.bitLength() < Long.SIZE) {
87-
return LongCodec.encodeLong(allocator, i.longValue());
89+
return LongCodec.encodeLong(i.longValue());
8890
}
8991

90-
return new BigIntegerMySqlParameter(allocator, (BigInteger) value);
92+
return new BigIntegerMySqlParameter((BigInteger) value);
9193
}
9294

9395
@Override
@@ -140,17 +142,14 @@ private static BigInteger decimalBigInteger(ByteBuf buf) {
140142

141143
private static class BigIntegerMySqlParameter extends AbstractMySqlParameter {
142144

143-
private final ByteBufAllocator allocator;
144-
145145
private final BigInteger value;
146146

147-
private BigIntegerMySqlParameter(ByteBufAllocator allocator, BigInteger value) {
148-
this.allocator = allocator;
147+
private BigIntegerMySqlParameter(BigInteger value) {
149148
this.value = value;
150149
}
151150

152151
@Override
153-
public Mono<ByteBuf> publishBinary() {
152+
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
154153
return Mono.fromSupplier(() -> CodecUtils.encodeAscii(allocator, value.toString()));
155154
}
156155

src/main/java/io/asyncer/r2dbc/mysql/codec/BitSetCodec.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
*/
3535
final class BitSetCodec extends AbstractClassedCodec<BitSet> {
3636

37-
BitSetCodec(ByteBufAllocator allocator) {
38-
super(allocator, BitSet.class);
37+
static final BitSetCodec INSTANCE = new BitSetCodec();
38+
39+
private BitSetCodec() {
40+
super(BitSet.class);
3941
}
4042

4143
@Override
@@ -85,7 +87,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
8587
type = MySqlType.BIGINT;
8688
}
8789

88-
return new BitSetMySqlParameter(allocator, bits, type);
90+
return new BitSetMySqlParameter(bits, type);
8991
}
9092

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

110112
private static final class BitSetMySqlParameter extends AbstractMySqlParameter {
111113

112-
private final ByteBufAllocator allocator;
113-
114114
private final long value;
115115

116116
private final MySqlType type;
117117

118-
private BitSetMySqlParameter(ByteBufAllocator allocator, long value, MySqlType type) {
119-
this.allocator = allocator;
118+
private BitSetMySqlParameter(long value, MySqlType type) {
120119
this.value = value;
121120
this.type = type;
122121
}
123122

124123
@Override
125-
public Mono<ByteBuf> publishBinary() {
124+
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
126125
switch (type) {
127126
case TINYINT:
128127
return Mono.fromSupplier(() -> allocator.buffer(Byte.BYTES).writeByte((int) value));

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@
4242
*/
4343
final class BlobCodec implements MassiveCodec<Blob> {
4444

45-
private static final int MAX_MERGE = 1 << 14;
45+
static final BlobCodec INSTANCE = new BlobCodec();
4646

47-
private final ByteBufAllocator allocator;
47+
private static final int MAX_MERGE = 1 << 14;
4848

49-
BlobCodec(ByteBufAllocator allocator) {
50-
this.allocator = allocator;
49+
private BlobCodec() {
5150
}
5251

5352
@Override
@@ -76,7 +75,7 @@ public boolean canEncode(Object value) {
7675

7776
@Override
7877
public MySqlParameter encode(Object value, CodecContext context) {
79-
return new BlobMySqlParameter(allocator, (Blob) value);
78+
return new BlobMySqlParameter((Blob) value);
8079
}
8180

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

108107
private static final class BlobMySqlParameter extends AbstractLobMySqlParameter {
109108

110-
private final ByteBufAllocator allocator;
111-
112109
private final AtomicReference<Blob> blob;
113110

114-
private BlobMySqlParameter(ByteBufAllocator allocator, Blob blob) {
115-
this.allocator = allocator;
111+
private BlobMySqlParameter(Blob blob) {
116112
this.blob = new AtomicReference<>(blob);
117113
}
118114

119115
@Override
120-
public Flux<ByteBuf> publishBinary() {
116+
public Flux<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
121117
return Flux.defer(() -> {
122118
Blob blob = this.blob.getAndSet(null);
123119

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
*/
3030
final class BooleanCodec extends AbstractPrimitiveCodec<Boolean> {
3131

32-
BooleanCodec(ByteBufAllocator allocator) {
33-
super(allocator, Boolean.TYPE, Boolean.class);
32+
static final BooleanCodec INSTANCE = new BooleanCodec();
33+
34+
private BooleanCodec() {
35+
super(Boolean.TYPE, Boolean.class);
3436
}
3537

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

4749
@Override
4850
public MySqlParameter encode(Object value, CodecContext context) {
49-
return new BooleanMySqlParameter(allocator, (Boolean) value);
51+
return new BooleanMySqlParameter((Boolean) value);
5052
}
5153

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

5860
private static final class BooleanMySqlParameter extends AbstractMySqlParameter {
5961

60-
private final ByteBufAllocator allocator;
61-
6262
private final boolean value;
6363

64-
private BooleanMySqlParameter(ByteBufAllocator allocator, boolean value) {
65-
this.allocator = allocator;
64+
private BooleanMySqlParameter(boolean value) {
6665
this.value = value;
6766
}
6867

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

src/main/java/io/asyncer/r2dbc/mysql/codec/ByteArrayCodec.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
*/
3636
final class ByteArrayCodec extends AbstractClassedCodec<byte[]> {
3737

38-
ByteArrayCodec(ByteBufAllocator allocator) {
39-
super(allocator, byte[].class);
38+
static final ByteArrayCodec INSTANCE = new ByteArrayCodec();
39+
40+
private ByteArrayCodec() {
41+
super(byte[].class);
4042
}
4143

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

5759
@Override
5860
public MySqlParameter encode(Object value, CodecContext context) {
59-
return new ByteArrayMySqlParameter(allocator, (byte[]) value);
61+
return new ByteArrayMySqlParameter((byte[]) value);
6062
}
6163

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

8688
private static final class ByteArrayMySqlParameter extends AbstractMySqlParameter {
8789

88-
private final ByteBufAllocator allocator;
89-
9090
private final byte[] value;
9191

92-
private ByteArrayMySqlParameter(ByteBufAllocator allocator, byte[] value) {
93-
this.allocator = allocator;
92+
private ByteArrayMySqlParameter(byte[] value) {
9493
this.value = value;
9594
}
9695

9796
@Override
98-
public Mono<ByteBuf> publishBinary() {
97+
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
9998
return Mono.fromSupplier(() -> encodeBytes(allocator, value));
10099
}
101100

src/main/java/io/asyncer/r2dbc/mysql/codec/ByteBufferCodec.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
*/
3535
final class ByteBufferCodec extends AbstractClassedCodec<ByteBuffer> {
3636

37-
ByteBufferCodec(ByteBufAllocator allocator) {
38-
super(allocator, ByteBuffer.class);
37+
static final ByteBufferCodec INSTANCE = new ByteBufferCodec();
38+
39+
private ByteBufferCodec() {
40+
super(ByteBuffer.class);
3941
}
4042

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

5658
@Override
5759
public MySqlParameter encode(Object value, CodecContext context) {
58-
return new ByteBufferMySqlParameter(allocator, (ByteBuffer) value);
60+
return new ByteBufferMySqlParameter((ByteBuffer) value);
5961
}
6062

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

7173
private static final class ByteBufferMySqlParameter extends AbstractMySqlParameter {
7274

73-
private final ByteBufAllocator allocator;
74-
7575
private final ByteBuffer buffer;
7676

77-
private ByteBufferMySqlParameter(ByteBufAllocator allocator, ByteBuffer buffer) {
78-
this.allocator = allocator;
77+
private ByteBufferMySqlParameter(ByteBuffer buffer) {
7978
this.buffer = buffer;
8079
}
8180

8281
@Override
83-
public Mono<ByteBuf> publishBinary() {
82+
public Mono<ByteBuf> publishBinary(final ByteBufAllocator allocator) {
8483
return Mono.fromSupplier(() -> {
8584
if (!buffer.hasRemaining()) {
8685
// It is zero of var int, not terminal.

0 commit comments

Comments
 (0)