This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b59c01  Avoid duplicate memory allocations in PostgreSQL protocol 
(#15137)
6b59c01 is described below

commit 6b59c010075de77c2000cb15dcca54e6d424aeec
Author: 吴伟杰 <[email protected]>
AuthorDate: Fri Jan 28 13:21:04 2022 +0800

    Avoid duplicate memory allocations in PostgreSQL protocol (#15137)
---
 .../command/query/PostgreSQLDataRowPacket.java     | 13 ++++----
 .../{simple => }/PostgreSQLDataRowPacketTest.java  | 36 ++++++++++++++++++----
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
index 440fe85..b9ebeeb 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
@@ -53,12 +53,12 @@ public final class PostgreSQLDataRowPacket implements 
PostgreSQLIdentifierPacket
     }
     
     private void writeBinaryValue(final PostgreSQLPacketPayload payload, final 
BinaryCell each) {
-        PostgreSQLBinaryProtocolValue binaryProtocolValue = 
PostgreSQLBinaryProtocolValueFactory.getBinaryProtocolValue(each.getColumnType());
         Object value = each.getData();
         if (null == value) {
             payload.writeInt4(0xFFFFFFFF);
             return;
         }
+        PostgreSQLBinaryProtocolValue binaryProtocolValue = 
PostgreSQLBinaryProtocolValueFactory.getBinaryProtocolValue(each.getColumnType());
         payload.writeInt4(binaryProtocolValue.getColumnLength(value));
         binaryProtocolValue.write(payload, value);
     }
@@ -72,16 +72,17 @@ public final class PostgreSQLDataRowPacket implements 
PostgreSQLIdentifierPacket
         } else if (each instanceof SQLXML) {
             writeSQLXMLData(payload, each);
         } else {
-            String columnData = each.toString();
-            payload.writeInt4(columnData.getBytes().length);
-            payload.writeStringEOF(columnData);
+            byte[] columnData = each.toString().getBytes(payload.getCharset());
+            payload.writeInt4(columnData.length);
+            payload.writeBytes(columnData);
         }
     }
     
     private void writeSQLXMLData(final PostgreSQLPacketPayload payload, final 
Object data) {
         try {
-            payload.writeInt4(((SQLXML) data).getString().getBytes().length);
-            payload.writeStringEOF(((SQLXML) data).getString());
+            byte[] dataBytes = ((SQLXML) 
data).getString().getBytes(payload.getCharset());
+            payload.writeInt4(dataBytes.length);
+            payload.writeBytes(dataBytes);
         } catch (final SQLException ex) {
             throw new RuntimeException(ex.getMessage());
         }
diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/simple/PostgreSQLDataRowPacketTest.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
similarity index 74%
rename from 
shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/simple/PostgreSQLDataRowPacketTest.java
rename to 
shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
index 88010eb..619e3b2 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/simple/PostgreSQLDataRowPacketTest.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
@@ -15,17 +15,19 @@
  * limitations under the License.
  */
 
-package 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.simple;
+package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query;
 
 import org.apache.shardingsphere.db.protocol.binary.BinaryCell;
-import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLDataRowPacket;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
+import 
org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
 import 
org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
+import java.nio.charset.StandardCharsets;
 import java.sql.SQLException;
 import java.sql.SQLXML;
 import java.util.Collections;
@@ -46,6 +48,11 @@ public final class PostgreSQLDataRowPacketTest {
     @Mock
     private SQLXML sqlxml;
     
+    @Before
+    public void setup() {
+        when(payload.getCharset()).thenReturn(StandardCharsets.UTF_8);
+    }
+    
     @Test
     public void assertWriteWithNull() {
         PostgreSQLDataRowPacket actual = new 
PostgreSQLDataRowPacket(Collections.singletonList(null));
@@ -66,8 +73,9 @@ public final class PostgreSQLDataRowPacketTest {
         when(sqlxml.getString()).thenReturn("value");
         PostgreSQLDataRowPacket actual = new 
PostgreSQLDataRowPacket(Collections.singletonList(sqlxml));
         actual.write(payload);
-        verify(payload).writeInt4("value".getBytes().length);
-        verify(payload).writeStringEOF("value");
+        byte[] valueBytes = "value".getBytes(StandardCharsets.UTF_8);
+        verify(payload).writeInt4(valueBytes.length);
+        verify(payload).writeBytes(valueBytes);
     }
     
     @Test
@@ -75,8 +83,9 @@ public final class PostgreSQLDataRowPacketTest {
         PostgreSQLDataRowPacket actual = new 
PostgreSQLDataRowPacket(Collections.singletonList("value"));
         assertThat(actual.getData(), is(Collections.singletonList("value")));
         actual.write(payload);
-        verify(payload).writeInt4("value".getBytes().length);
-        verify(payload).writeStringEOF("value");
+        byte[] valueBytes = "value".getBytes(StandardCharsets.UTF_8);
+        verify(payload).writeInt4(valueBytes.length);
+        verify(payload).writeBytes(valueBytes);
     }
     
     @Test(expected = RuntimeException.class)
@@ -94,4 +103,19 @@ public final class PostgreSQLDataRowPacketTest {
         verify(payload).writeInt2(1);
         verify(payload).writeInt4(0xFFFFFFFF);
     }
+    
+    @Test
+    public void assertWriteBinaryInt4() {
+        final int value = 12345678;
+        PostgreSQLDataRowPacket actual = new 
PostgreSQLDataRowPacket(Collections.singletonList(new 
BinaryCell(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4, value)));
+        actual.write(payload);
+        verify(payload).writeInt2(1);
+        verify(payload).writeInt4(4);
+        verify(payload).writeInt4(value);
+    }
+    
+    @Test
+    public void assertGetIdentifier() {
+        assertThat(new 
PostgreSQLDataRowPacket(Collections.emptyList()).getIdentifier(), 
is(PostgreSQLMessagePacketType.DATA_ROW));
+    }
 }

Reply via email to