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

[shardingsphere] branch master updated: Refactor PostgreSQLBindCompletePacket to singleton (#14819)

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

jianglongtao 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 f3a6c07  Refactor PostgreSQLBindCompletePacket to singleton (#14819)
f3a6c07 is described below

commit f3a6c07837f9a77ebdf367fcfebb36002e17961d
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Mon Jan 17 13:35:49 2022 +0800

    Refactor PostgreSQLBindCompletePacket to singleton (#14819)
---
 .../query/extended/bind/PostgreSQLBindCompletePacket.java  | 14 ++++++++++++++
 .../extended/bind/PostgreSQLBindCompletePacketTest.java    |  4 ++--
 .../query/extended/bind/OpenGaussComBatchBindExecutor.java |  2 +-
 .../PostgreSQLAggregatedBatchedInsertsCommandExecutor.java |  2 +-
 .../query/extended/bind/PostgreSQLComBindExecutor.java     |  2 +-
 .../query/extended/bind/PostgreSQLComBindExecutorTest.java |  9 ++++-----
 6 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacket.java
index 77cd7c1..6f8bea2 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacket.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind;
 
+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
 /**
  * Bind complete packet for PostgreSQL.
  */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class PostgreSQLBindCompletePacket implements PostgreSQLIdentifierPacket {
     
+    private static final PostgreSQLBindCompletePacket INSTANCE = new PostgreSQLBindCompletePacket();
+    
+    /**
+     * Get instance of {@link PostgreSQLBindCompletePacket}.
+     *
+     * @return instance of {@link PostgreSQLBindCompletePacket}
+     */
+    public static PostgreSQLBindCompletePacket 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/extended/bind/PostgreSQLBindCompletePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacketTest.java
index e0aa300..1074171 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLBindCompletePacketTest.java
@@ -39,14 +39,14 @@ public final class PostgreSQLBindCompletePacketTest {
     
     @Test
     public void assertWrite() {
-        PostgreSQLBindCompletePacket rowPacket = new PostgreSQLBindCompletePacket();
+        PostgreSQLBindCompletePacket rowPacket = PostgreSQLBindCompletePacket.getInstance();
         rowPacket.write(payload);
         assertThat(byteBuf.writerIndex(), is(0));
     }
     
     @Test
     public void assertGetMessageType() {
-        PostgreSQLBindCompletePacket rowPacket = new PostgreSQLBindCompletePacket();
+        PostgreSQLBindCompletePacket rowPacket = PostgreSQLBindCompletePacket.getInstance();
         assertThat(rowPacket.getIdentifier(), is(PostgreSQLMessagePacketType.BIND_COMPLETE));
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
index aa5d430..d79ccea 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
@@ -53,7 +53,7 @@ public final class OpenGaussComBatchBindExecutor implements QueryCommandExecutor
     public Collection<DatabasePacket<?>> execute() throws SQLException {
         PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), packet.getStatementId());
         updateCount = new PostgreSQLBatchedInsertsExecutor(connectionSession, preparedStatement, packet.readParameterSets(preparedStatement.getParameterTypes())).executeBatch();
-        return Collections.singletonList(new PostgreSQLBindCompletePacket());
+        return Collections.singletonList(PostgreSQLBindCompletePacket.getInstance());
     }
     
     @Override
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 f7e15d0..bc90012 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
@@ -58,7 +58,7 @@ public final class PostgreSQLAggregatedBatchedInsertsCommandExecutor implements
         int executePacketCount = executePacketCount();
         for (PostgreSQLCommandPacket each : packets) {
             if (each instanceof PostgreSQLComBindPacket) {
-                result.add(new PostgreSQLBindCompletePacket());
+                result.add(PostgreSQLBindCompletePacket.getInstance());
             }
             if (each instanceof PostgreSQLComDescribePacket) {
                 result.add(preparedStatement.describeRows().orElseGet(PostgreSQLNoDataPacket::getInstance));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
index b3aaff4..6804a02 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
@@ -49,6 +49,6 @@ public final class PostgreSQLComBindExecutor implements CommandExecutor {
         PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), packet.getStatementId());
         JDBCBackendConnection backendConnection = (JDBCBackendConnection) connectionSession.getBackendConnection();
         connectionContext.createPortal(packet.getPortal(), preparedStatement, packet.readParameters(preparedStatement.getParameterTypes()), packet.readResultFormats(), backendConnection).execute();
-        return Collections.singletonList(new PostgreSQLBindCompletePacket());
+        return Collections.singletonList(PostgreSQLBindCompletePacket.getInstance());
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
index b1a671e..d488344b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
@@ -39,7 +39,6 @@ import java.util.Collections;
 import java.util.Iterator;
 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.mockito.ArgumentMatchers.any;
@@ -85,7 +84,7 @@ public final class PostgreSQLComBindExecutorTest {
         PostgreSQLComBindExecutor executor = new PostgreSQLComBindExecutor(connectionContext, bindPacket, connectionSession);
         Collection<DatabasePacket<?>> actual = executor.execute();
         assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next(), is(instanceOf(PostgreSQLBindCompletePacket.class)));
+        assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
         verify(portal).execute();
     }
     
@@ -94,7 +93,7 @@ public final class PostgreSQLComBindExecutorTest {
         PostgreSQLComBindExecutor executor = new PostgreSQLComBindExecutor(connectionContext, bindPacket, connectionSession);
         Collection<DatabasePacket<?>> actual = executor.execute();
         assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next(), is(instanceOf(PostgreSQLBindCompletePacket.class)));
+        assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
         verify(portal).execute();
     }
     
@@ -104,7 +103,7 @@ public final class PostgreSQLComBindExecutorTest {
         Collection<DatabasePacket<?>> actual = executor.execute();
         assertThat(actual.size(), is(1));
         Iterator<DatabasePacket<?>> actualPackets = actual.iterator();
-        assertThat(actualPackets.next(), is(instanceOf(PostgreSQLBindCompletePacket.class)));
+        assertThat(actualPackets.next(), is(PostgreSQLBindCompletePacket.getInstance()));
         verify(portal).execute();
     }
     
@@ -113,7 +112,7 @@ public final class PostgreSQLComBindExecutorTest {
         PostgreSQLComBindExecutor executor = new PostgreSQLComBindExecutor(connectionContext, bindPacket, connectionSession);
         Collection<DatabasePacket<?>> actual = executor.execute();
         assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next(), is(instanceOf(PostgreSQLBindCompletePacket.class)));
+        assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
         verify(portal).execute();
     }
 }