Skip to content

Commit

Permalink
[CONJ-1145] compression of packet with sufficient length without havi…
Browse files Browse the repository at this point in the history
…ng good compression, avoid using unnecessary variables
  • Loading branch information
rusher committed Jan 12, 2024
1 parent e955360 commit 573eb3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Expand Up @@ -75,7 +75,6 @@ public class CompressPacketOutputStream extends AbstractPacketOutputStream {
private static final float MIN_COMPRESSION_RATIO = 0.9f;
private final byte[] header = new byte[7];
private final byte[] subHeader = new byte[4];
private boolean subHeaderIsGenerated = false;
private int maxPacketLength = MAX_PACKET_LENGTH;
private int compressSeqNo;
private byte[] remainingData = new byte[0];
Expand Down Expand Up @@ -162,7 +161,7 @@ protected void flushBuffer(boolean commandEnd) throws IOException {
subHeader[1] = (byte) (pos >>> 8);
subHeader[2] = (byte) (pos >>> 16);
subHeader[3] = (byte) this.seqNo++;
subHeaderIsGenerated = true;

deflater.write(subHeader, 0, 4);
deflater.write(buf, 0, uncompressSize - (remainingData.length + 4));
deflater.finish();
Expand Down Expand Up @@ -251,6 +250,9 @@ protected void flushBuffer(boolean commandEnd) throws IOException {
}
pos = 0;
return;
} else {
// reset sequence to previous value, since not sent because compression wasn't high enough
this.seqNo--;
}
}
}
Expand All @@ -277,10 +279,8 @@ protected void flushBuffer(boolean commandEnd) throws IOException {
subHeader[0] = (byte) pos;
subHeader[1] = (byte) (pos >>> 8);
subHeader[2] = (byte) (pos >>> 16);
// Avoid increasing the sequence of subHeader twice
if (!subHeaderIsGenerated){
subHeader[3] = (byte) this.seqNo++;
}
subHeader[3] = (byte) this.seqNo++;

out.write(subHeader, 0, 4);
out.write(buf, 0, uncompressSize - (remainingData.length + 4));
cmdLength += remainingData.length;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/mariadb/jdbc/ConnectionTest.java
Expand Up @@ -1092,4 +1092,14 @@ public void connectionAttributes() throws SQLException {
}
}
}

@Test
public void compressionWhenPacketIsNotCompressedEnough() throws SQLException {
try (Connection connection = setConnection("&useCompression=true&log")) {
try (Statement s = connection.createStatement()) {
String sql = "-- :: userIDaebcda5cb52dc741f20fe495327ddbfc8411cc53663eec3a5ffdb1f30626d39cc606822\nselect version();";
s.execute(sql);
}
}
}
}

0 comments on commit 573eb3d

Please sign in to comment.