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 4cc516e  Add cache for PostgreSQLCommand (#11303)
4cc516e is described below

commit 4cc516ef3ccd8d9abcebf9b3de69a4af1f586cf6
Author: 吴伟杰 <[email protected]>
AuthorDate: Tue Jul 13 12:50:03 2021 +0800

    Add cache for PostgreSQLCommand (#11303)
    
    * Add cache for PostgreSQLCommand
    
    * Rename CACHE to COMPUTED_CLASSES
---
 .../proxy/frontend/postgresql/command/query/PostgreSQLCommand.java  | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
index f91e623..839595c 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
@@ -33,7 +33,9 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.RollbackSta
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Map;
 import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * PostgreSQL command.
@@ -51,6 +53,8 @@ public enum PostgreSQLCommand {
     ROLLBACK(RollbackStatement.class),
     SET(SetStatement.class);
     
+    private static final Map<Class<? extends SQLStatement>, 
Optional<PostgreSQLCommand>> COMPUTED_CLASSES = new ConcurrentHashMap<>(16, 1);
+    
     private final Collection<Class<? extends SQLStatement>> 
sqlStatementClasses;
     
     @SafeVarargs
@@ -65,7 +69,7 @@ public enum PostgreSQLCommand {
      * @return PostgreSQL command
      */
     public static Optional<PostgreSQLCommand> valueOf(final Class<? extends 
SQLStatement> sqlStatementClass) {
-        return Arrays.stream(PostgreSQLCommand.values()).filter(each -> 
matches(sqlStatementClass, each)).findAny();
+        return COMPUTED_CLASSES.computeIfAbsent(sqlStatementClass, target -> 
Arrays.stream(PostgreSQLCommand.values()).filter(each -> matches(target, 
each)).findAny());
     }
     
     private static boolean matches(final Class<? extends SQLStatement> 
sqlStatementClass, final PostgreSQLCommand postgreSQLCommand) {

Reply via email to