You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/01/17 03:50:09 UTC

[shardingsphere] branch master updated: Refactor PostgreSQLNoDataPacket to singleton (#14817)

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 bf35687  Refactor PostgreSQLNoDataPacket to singleton (#14817)
bf35687 is described below

commit bf35687f42324e110ef94d1aa100359eee7bda81
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Mon Jan 17 11:49:09 2022 +0800

    Refactor PostgreSQLNoDataPacket to singleton (#14817)
---
 .../packet/command/query/PostgreSQLNoDataPacket.java       | 14 ++++++++++++++
 .../packet/command/query/PostgreSQLNoDataPacketTest.java   |  2 +-
 .../PostgreSQLAggregatedBatchedInsertsCommandExecutor.java |  2 +-
 .../command/query/extended/PostgreSQLPortal.java           |  2 +-
 .../extended/describe/PostgreSQLComDescribeExecutor.java   |  4 ++--
 .../command/query/extended/PostgreSQLPortalTest.java       |  2 +-
 .../describe/PostgreSQLComDescribeExecutorTest.java        |  5 ++---
 7 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacket.java
index 356b6a9..9efecba 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacket.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query;
 
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierPacket;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
@@ -25,8 +27,20 @@ import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacket
 /**
  * No data packet for PostgreSQL.
  */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class PostgreSQLNoDataPacket implements PostgreSQLIdentifierPacket {
     
+    private static final PostgreSQLNoDataPacket INSTANCE = new PostgreSQLNoDataPacket();
+    
+    /**
+     * Get instance of {@link PostgreSQLNoDataPacket}.
+     *
+     * @return instance of {@link PostgreSQLNoDataPacket}
+     */
+    public static PostgreSQLNoDataPacket getInstance() {
+        return INSTANCE;
+    }
+    
     @Override
     public void write(final PostgreSQLPacketPayload payload) {
     }
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacketTest.java
index 31efe35..f043679 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLNoDataPacketTest.java
@@ -28,7 +28,7 @@ public final class PostgreSQLNoDataPacketTest {
     
     @Test
     public void assertIdentifier() {
-        PostgreSQLIdentifierTag actual = new PostgreSQLNoDataPacket().getIdentifier();
+        PostgreSQLIdentifierTag actual = PostgreSQLNoDataPacket.getInstance().getIdentifier();
         assertThat(actual, is(PostgreSQLMessagePacketType.NO_DATA));
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedInsertsCommandExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedInsertsCommandExecutor.java
index 03d22dc..f7e15d0 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedInsertsCommandExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedInsertsCommandExecutor.java
@@ -61,7 +61,7 @@ public final class PostgreSQLAggregatedBatchedInsertsCommandExecutor implements
                 result.add(new PostgreSQLBindCompletePacket());
             }
             if (each instanceof PostgreSQLComDescribePacket) {
-                result.add(preparedStatement.describeRows().orElseGet(PostgreSQLNoDataPacket::new));
+                result.add(preparedStatement.describeRows().orElseGet(PostgreSQLNoDataPacket::getInstance));
             }
             if (each instanceof PostgreSQLComExecutePacket) {
                 String tag = PostgreSQLCommand.valueOf(preparedStatement.getSqlStatement().getClass()).orElse(PostgreSQLCommand.INSERT).getTag();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortal.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortal.java
index db26ccc..34c8990 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortal.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortal.java
@@ -109,7 +109,7 @@ public final class PostgreSQLPortal {
             return createRowDescriptionPacket((QueryResponseHeader) responseHeader);
         }
         if (responseHeader instanceof UpdateResponseHeader) {
-            return new PostgreSQLNoDataPacket();
+            return PostgreSQLNoDataPacket.getInstance();
         }
         throw new UnsupportedOperationException("Failed to describe portal");
     }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
index 447cf6d..dca31da 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
@@ -114,7 +114,7 @@ public final class PostgreSQLComDescribeExecutor implements CommandExecutor {
     private void describeInsertStatementByShardingSphereMetaData(final PostgreSQLPreparedStatement preparedStatement) {
         if (!preparedStatement.describeRows().isPresent()) {
             // TODO Consider the SQL `insert into table (col) values ($1) returning id`
-            preparedStatement.setRowDescription(new PostgreSQLNoDataPacket());
+            preparedStatement.setRowDescription(PostgreSQLNoDataPacket.getInstance());
         }
         InsertStatement insertStatement = (InsertStatement) preparedStatement.getSqlStatement();
         if (0 == insertStatement.getParameterCount()) {
@@ -205,7 +205,7 @@ public final class PostgreSQLComDescribeExecutor implements CommandExecutor {
         }
         ResultSetMetaData resultSetMetaData = ps.getMetaData();
         if (null == resultSetMetaData) {
-            preparedStatement.setRowDescription(new PostgreSQLNoDataPacket());
+            preparedStatement.setRowDescription(PostgreSQLNoDataPacket.getInstance());
             return;
         }
         List<PostgreSQLColumnDescription> columnDescriptions = new ArrayList<>(resultSetMetaData.getColumnCount());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortalTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortalTest.java
index a407a1d..7dc6e97 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortalTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPortalTest.java
@@ -118,7 +118,7 @@ public final class PostgreSQLPortalTest {
     public void assertDescribeWithUpdateResponseHeader() {
         UpdateResponseHeader responseHeader = mock(UpdateResponseHeader.class);
         setResponseHeader(responseHeader);
-        assertTrue(portal.describe() instanceof PostgreSQLNoDataPacket);
+        assertThat(portal.describe(), is(PostgreSQLNoDataPacket.getInstance()));
     }
     
     @Test(expected = UnsupportedOperationException.class)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
index 8f7cdbe..3476967 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
@@ -66,7 +66,6 @@ import java.util.List;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.nullable;
@@ -158,7 +157,7 @@ public final class PostgreSQLComDescribeExecutorTest {
         verify(mockPayload).writeInt2(4);
         verify(mockPayload, times(2)).writeInt4(23);
         verify(mockPayload, times(2)).writeInt4(18);
-        assertTrue(actualPacketsIterator.next() instanceof PostgreSQLNoDataPacket);
+        assertThat(actualPacketsIterator.next(), is(PostgreSQLNoDataPacket.getInstance()));
     }
     
     @Test
@@ -185,7 +184,7 @@ public final class PostgreSQLComDescribeExecutorTest {
         verify(mockPayload).writeInt2(5);
         verify(mockPayload, times(2)).writeInt4(23);
         verify(mockPayload, times(3)).writeInt4(18);
-        assertTrue(actualPacketsIterator.next() instanceof PostgreSQLNoDataPacket);
+        assertThat(actualPacketsIterator.next(), is(PostgreSQLNoDataPacket.getInstance()));
     }
     
     @Test