Skip to content

Commit

Permalink
[misc] correction on big buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Mar 18, 2016
1 parent 3b7cfb9 commit be23cbd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public void startPacket(int seqNo, boolean checkPacketLength) throws IOException
this.seqNo = seqNo;
this.compressSeqNo = seqNo;
this.checkPacketLength = checkPacketLength;
buffer.clear();
buffer.position(4);

}

/**
Expand Down Expand Up @@ -285,8 +288,6 @@ public void finishPacket() throws IOException {
buffer = firstBuffer;
}

buffer.clear();
buffer.position(4);
this.lastSeq = (useCompression) ? this.compressSeqNo : this.seqNo;
}

Expand Down Expand Up @@ -337,9 +338,9 @@ private void checkPacketMaxSize(int limit) throws MaxAllowedPacketException {
}

private void flushDirect() throws IOException {
buffer.flip();
buffer.flip(); //pos=0 lim=33554356
// the 4th first byte are reserved for first header.
int dataLength = buffer.remaining() - 4;
int dataLength = buffer.remaining() - 4; // 33554352

if (dataLength < maxPacketSize) {
//if only one packet, put array to socket
Expand All @@ -351,32 +352,35 @@ private void flushDirect() throws IOException {
outputStream.write(buffer.array(), 0, buffer.limit());
outputStream.flush();
} else {

//multiple packet. Send first one
buffer.put((byte) (maxPacketSize & 0xff))
.put((byte) (maxPacketSize >>> 8))
.put((byte) (maxPacketSize >>> 16))
.put((byte) seqNo++);

//pos=4 lim=33554356 maxPacketSize=16777215
outputStream.write(buffer.array(), 0, maxPacketSize + 4);
outputStream.flush();
buffer.position(maxPacketSize);

buffer.position(maxPacketSize + 4);
//pos=16777219 lim=33554356 maxPacketSize=16777215
while (buffer.remaining() > 0 ) {
int length = buffer.remaining();
int length = buffer.remaining(); // 16777137
buffer.position(buffer.position() - 4); //pos=16777215 lim=33554356 maxPacketSize=16777215
if (length > maxPacketSize) {
buffer.put((byte) (maxPacketSize & 0xff))
.put((byte) (maxPacketSize >>> 8))
.put((byte) (maxPacketSize >>> 16))
.put((byte) seqNo++);
outputStream.write(buffer.array(), buffer.position(), maxPacketSize);

outputStream.write(buffer.array(), buffer.position() - 4, maxPacketSize + 4);
outputStream.flush();
buffer.position(buffer.position() + maxPacketSize);
} else {
buffer.put((byte) (length & 0xff))
.put((byte) (length >>> 8))
.put((byte) (length >>> 16))
.put((byte) seqNo++);
outputStream.write(buffer.array(), buffer.position(), length);
outputStream.write(buffer.array(), buffer.position() - 4, length + 4);
outputStream.flush();
break;
}
Expand Down Expand Up @@ -851,7 +855,7 @@ public void sendTextPacket(String sql) throws IOException, QueryException {

System.arraycopy(sqlBytes, 0, packetBuffer, 5, maxPacketSize - 1);
int position = maxPacketSize - 1;
int positionDest = maxPacketSize - 1;
int positionDest = maxPacketSize + 4;

while (position < expectedPacketSize) {
int length = expectedPacketSize - position;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/mariadb/jdbc/BigQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ public void testError() throws SQLException {
}
}


}
4 changes: 2 additions & 2 deletions src/test/java/org/mariadb/jdbc/MultiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ public void rewriteBatchedMaxAllowedSizeTest() throws SQLException {
if (rs.next()) {
double maxAllowedPacket = rs.getInt(1);
// request will be INSERT INTO MultiTestt6 VALUES ...(1000000, 'testValue1000000'),(1000001, 'testValue1000001')"
// average additional part size will be 30 characters (",(1000001, 'testValue1000001')")
// average additional part size will be 30 characters (", (1500001, 'testValue1000001')")
// so there must be (8000000 * 30) / max_allowed_packet insert send
int totalInsertCommands = (int) Math.ceil((1500000 * 30) / maxAllowedPacket );
int totalInsertCommands = (int) Math.ceil((1500000 * 31) / maxAllowedPacket );
verifyInsertBehaviorBasedOnRewriteBatchedStatements(Boolean.TRUE, 1500000, totalInsertCommands);
} else {
fail();
Expand Down

0 comments on commit be23cbd

Please sign in to comment.