Skip to content

Commit 0cfe1ee

Browse files
authored
MySQL codebase clean up (#588)
* simplify okpacket handling for MySQL Signed-off-by: Billy Yuan <billy112487983@gmail.com> * move sequenceId from codec to encoder Signed-off-by: Billy Yuan <billy112487983@gmail.com> * fix some warnings Signed-off-by: Billy Yuan <billy112487983@gmail.com> * MySQL packet payload does not need to be cleared after decoding Signed-off-by: Billy Yuan <billy112487983@gmail.com> * remove unnecessary check as the flag is always set Signed-off-by: Billy Yuan <billy112487983@gmail.com>
1 parent b170bdc commit 0cfe1ee

22 files changed

+67
-71
lines changed

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/AuthenticationCommandBaseCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected final void handleAuthMoreData(byte[] password, ByteBuf payload) {
5252
int nonScrambledPasswordPacketLength = password.length + 1;
5353
ByteBuf nonScrambledPasswordPacket = allocateBuffer(nonScrambledPasswordPacketLength + 4);
5454
nonScrambledPasswordPacket.writeMediumLE(nonScrambledPasswordPacketLength);
55-
nonScrambledPasswordPacket.writeByte(sequenceId);
55+
nonScrambledPasswordPacket.writeByte(encoder.sequenceId);
5656
nonScrambledPasswordPacket.writeBytes(password);
5757
nonScrambledPasswordPacket.writeByte(0x00); // end with a 0x00
5858
sendNonSplitPacket(nonScrambledPasswordPacket);
@@ -64,7 +64,7 @@ protected final void handleAuthMoreData(byte[] password, ByteBuf payload) {
6464
isWaitingForRsaPublicKey = true;
6565
ByteBuf rsaPublicKeyRequest = allocateBuffer(5);
6666
rsaPublicKeyRequest.writeMediumLE(1);
67-
rsaPublicKeyRequest.writeByte(sequenceId);
67+
rsaPublicKeyRequest.writeByte(encoder.sequenceId);
6868
rsaPublicKeyRequest.writeByte(AUTH_PUBLIC_KEY_REQUEST_FLAG);
6969
sendNonSplitPacket(rsaPublicKeyRequest);
7070
} else {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ChangeUserCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private void sendChangeUserCommand() {
8383
// encode packet header
8484
int packetStartIdx = packet.writerIndex();
8585
packet.writeMediumLE(0); // will set payload length later by calculation
86-
packet.writeByte(sequenceId);
86+
packet.writeByte(encoder.sequenceId);
8787

8888
// encode packet payload
8989
packet.writeByte(CommandType.COM_CHANGE_USER);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CloseConnectionCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void sendQuitCommand() {
3737
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
3838
// encode packet header
3939
packet.writeMediumLE(PAYLOAD_LENGTH);
40-
packet.writeByte(sequenceId);
40+
packet.writeByte(encoder.sequenceId);
4141

4242
// encode packet payload
4343
packet.writeByte(CommandType.COM_QUIT);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CloseStatementCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void sendCloseStatementCommand(MySQLPreparedStatement statement) {
4141
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
4242
// encode packet header
4343
packet.writeMediumLE(PAYLOAD_LENGTH);
44-
packet.writeByte(sequenceId);
44+
packet.writeByte(encoder.sequenceId);
4545

4646
// encode packet payload
4747
packet.writeByte(CommandType.COM_STMT_CLOSE);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CommandCodec.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ abstract class CommandCodec<R, C extends CommandBase<R>> {
3737
public Throwable failure;
3838
public R result;
3939
final C cmd;
40-
int sequenceId;
4140
MySQLEncoder encoder;
4241

4342
CommandCodec(C cmd) {
@@ -48,6 +47,7 @@ abstract class CommandCodec<R, C extends CommandBase<R>> {
4847

4948
void encode(MySQLEncoder encoder) {
5049
this.encoder = encoder;
50+
this.encoder.sequenceId = 0;
5151
}
5252

5353
ByteBuf allocateBuffer() {
@@ -76,21 +76,21 @@ private void sendSplitPacket(ByteBuf packet) {
7676
// send a packet with 0xFFFFFF length payload
7777
ByteBuf packetHeader = allocateBuffer(4);
7878
packetHeader.writeMediumLE(PACKET_PAYLOAD_LENGTH_LIMIT);
79-
packetHeader.writeByte(sequenceId++);
79+
packetHeader.writeByte(encoder.sequenceId++);
8080
encoder.chctx.write(packetHeader);
8181
encoder.chctx.write(payload.readRetainedSlice(PACKET_PAYLOAD_LENGTH_LIMIT));
8282
}
8383

8484
// send a packet with last part of the payload
8585
ByteBuf packetHeader = allocateBuffer(4);
8686
packetHeader.writeMediumLE(payload.readableBytes());
87-
packetHeader.writeByte(sequenceId++);
87+
packetHeader.writeByte(encoder.sequenceId++);
8888
encoder.chctx.write(packetHeader);
8989
encoder.chctx.writeAndFlush(payload);
9090
}
9191

9292
void sendNonSplitPacket(ByteBuf packet) {
93-
sequenceId++;
93+
encoder.sequenceId++;
9494
encoder.chctx.writeAndFlush(packet);
9595
}
9696

@@ -99,7 +99,7 @@ final void sendBytesAsPacket(byte[] payload) {
9999
ByteBuf packet = allocateBuffer(payloadLength + 4);
100100
// encode packet header
101101
packet.writeMediumLE(payloadLength);
102-
packet.writeByte(sequenceId);
102+
packet.writeByte(encoder.sequenceId);
103103

104104
// encode packet payload
105105
packet.writeBytes(payload);
@@ -123,41 +123,34 @@ void handleOkPacketOrErrorPacketPayload(ByteBuf payload) {
123123
void handleErrorPacketPayload(ByteBuf payload) {
124124
payload.skipBytes(1); // skip ERR packet header
125125
int errorCode = payload.readUnsignedShortLE();
126-
String sqlState = null;
127-
if ((encoder.clientCapabilitiesFlag & CapabilitiesFlag.CLIENT_PROTOCOL_41) != 0) {
128-
payload.skipBytes(1); // SQL state marker will always be #
129-
sqlState = BufferUtils.readFixedLengthString(payload, 5, StandardCharsets.UTF_8);
130-
}
126+
// CLIENT_PROTOCOL_41 capability flag will always be set
127+
payload.skipBytes(1); // SQL state marker will always be #
128+
String sqlState = BufferUtils.readFixedLengthString(payload, 5, StandardCharsets.UTF_8);
131129
String errorMessage = readRestOfPacketString(payload, StandardCharsets.UTF_8);
132130
completionHandler.handle(CommandResponse.failure(new MySQLException(errorMessage, errorCode, sqlState)));
133131
}
134132

135-
OkPacket decodeOkPacketPayload(ByteBuf payload, Charset charset) {
133+
// simplify the ok packet as those properties are actually not used for now
134+
OkPacket decodeOkPacketPayload(ByteBuf payload) {
136135
payload.skipBytes(1); // skip OK packet header
137136
long affectedRows = BufferUtils.readLengthEncodedInteger(payload);
138137
long lastInsertId = BufferUtils.readLengthEncodedInteger(payload);
139-
int serverStatusFlags = 0;
140-
int numberOfWarnings = 0;
141-
if ((encoder.clientCapabilitiesFlag & CapabilitiesFlag.CLIENT_PROTOCOL_41) != 0) {
142-
serverStatusFlags = payload.readUnsignedShortLE();
143-
numberOfWarnings = payload.readUnsignedShortLE();
144-
} else if ((encoder.clientCapabilitiesFlag & CapabilitiesFlag.CLIENT_TRANSACTIONS) != 0) {
145-
serverStatusFlags = payload.readUnsignedShortLE();
146-
}
147-
String statusInfo;
138+
int serverStatusFlags = payload.readUnsignedShortLE();
139+
// int numberOfWarnings = payload.readUnsignedShortLE();
140+
String statusInfo = null;
148141
String sessionStateInfo = null;
149-
if (payload.readableBytes() == 0) {
150-
// handle when OK packet does not contain server status info
151-
statusInfo = null;
152-
} else if ((encoder.clientCapabilitiesFlag & CapabilitiesFlag.CLIENT_SESSION_TRACK) != 0) {
153-
statusInfo = BufferUtils.readLengthEncodedString(payload, charset);
154-
if ((serverStatusFlags & ServerStatusFlags.SERVER_SESSION_STATE_CHANGED) != 0) {
155-
sessionStateInfo = BufferUtils.readLengthEncodedString(payload, charset);
156-
}
157-
} else {
158-
statusInfo = readRestOfPacketString(payload, charset);
159-
}
160-
return new OkPacket(affectedRows, lastInsertId, serverStatusFlags, numberOfWarnings, statusInfo, sessionStateInfo);
142+
// if (payload.readableBytes() == 0) {
143+
// // handle when OK packet does not contain server status info
144+
// statusInfo = null;
145+
// } else if ((encoder.clientCapabilitiesFlag & CapabilitiesFlag.CLIENT_SESSION_TRACK) != 0) {
146+
// statusInfo = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
147+
// if ((serverStatusFlags & ServerStatusFlags.SERVER_SESSION_STATE_CHANGED) != 0) {
148+
// sessionStateInfo = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
149+
// }
150+
// } else {
151+
// statusInfo = readRestOfPacketString(payload, charset);
152+
// }
153+
return new OkPacket(affectedRows, lastInsertId, serverStatusFlags, 0, statusInfo, sessionStateInfo);
161154
}
162155

163156
EofPacket decodeEofPacketPayload(ByteBuf payload) {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/DebugCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void sendDebugCommand() {
3737
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
3838
// encode packet header
3939
packet.writeMediumLE(PAYLOAD_LENGTH);
40-
packet.writeByte(sequenceId);
40+
packet.writeByte(encoder.sequenceId);
4141

4242
// encode packet payload
4343
packet.writeByte(CommandType.COM_DEBUG);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ExtendedBatchQueryCommandCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected boolean isDecodingCompleted(int serverStatusFlags) {
5757

5858
private void doExecuteBatch() {
5959
if (batchIdx < params.size()) {
60-
this.sequenceId = 0;
60+
encoder.sequenceId = 0;
6161
Tuple param = params.get(batchIdx);
6262
sendBatchStatementExecuteCommand(statement, param);
6363
batchIdx++;
@@ -69,7 +69,7 @@ private void sendBatchStatementExecuteCommand(MySQLPreparedStatement statement,
6969
// encode packet header
7070
int packetStartIdx = packet.writerIndex();
7171
packet.writeMediumLE(0); // will set payload length later by calculation
72-
packet.writeByte(sequenceId);
72+
packet.writeByte(encoder.sequenceId);
7373

7474
// encode packet payload
7575
packet.writeByte(CommandType.COM_STMT_EXECUTE);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ExtendedQueryCommandBaseCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected void handleInitPacket(ByteBuf payload) {
3333
// may receive ERR_Packet, OK_Packet, Binary Protocol Resultset
3434
int firstByte = payload.getUnsignedByte(payload.readerIndex());
3535
if (firstByte == OK_PACKET_HEADER) {
36-
OkPacket okPacket = decodeOkPacketPayload(payload, StandardCharsets.UTF_8);
36+
OkPacket okPacket = decodeOkPacketPayload(payload);
3737
handleSingleResultsetDecodingCompleted(okPacket.serverStatusFlags(), okPacket.affectedRows(), okPacket.lastInsertId());
3838
} else if (firstByte == ERROR_PACKET_HEADER) {
3939
handleErrorPacketPayload(payload);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ExtendedQueryCommandCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void decodePayload(ByteBuf payload, int payloadLength) {
8585
case HANDLING_ROW_DATA_OR_END_PACKET:
8686
handleResultsetColumnDefinitionsDecodingCompleted();
8787
// need to reset packet number so that we can send a fetch request
88-
this.sequenceId = 0;
88+
encoder.sequenceId = 0;
8989
// send fetch after cursor opened
9090
decoder = new RowResultDecoder<>(cmd.collector(), statement.rowDesc);
9191

@@ -107,7 +107,7 @@ private void sendStatementExecuteCommand(MySQLPreparedStatement statement, boole
107107
// encode packet header
108108
int packetStartIdx = packet.writerIndex();
109109
packet.writeMediumLE(0); // will set payload length later by calculation
110-
packet.writeByte(sequenceId);
110+
packet.writeByte(encoder.sequenceId);
111111

112112
// encode packet payload
113113
packet.writeByte(CommandType.COM_STMT_EXECUTE);
@@ -160,7 +160,7 @@ private void sendStatementFetchCommand(long statementId, int count) {
160160
// encode packet header
161161
int packetStartIdx = packet.writerIndex();
162162
packet.writeMediumLE(0); // will set payload length later by calculation
163-
packet.writeByte(sequenceId);
163+
packet.writeByte(encoder.sequenceId);
164164

165165
// encode packet payload
166166
packet.writeByte(CommandType.COM_STMT_FETCH);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/InitDbCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void sendInitDbCommand() {
3838
// encode packet header
3939
int packetStartIdx = packet.writerIndex();
4040
packet.writeMediumLE(0); // will set payload length later by calculation
41-
packet.writeByte(sequenceId);
41+
packet.writeByte(encoder.sequenceId);
4242

4343
// encode packet payload
4444
packet.writeByte(CommandType.COM_INIT_DB);

0 commit comments

Comments
 (0)