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

sunnianjun 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 04867b23b58 Refactor AlgorithmDistSQLConverter (#30596)
04867b23b58 is described below

commit 04867b23b5847a84ba38500f8c80cd76ab3d231e
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Mar 22 00:42:20 2024 +0800

    Refactor AlgorithmDistSQLConverter (#30596)
    
    * Refactor AlgorithmDistSQLConverter
    
    * Refactor AlgorithmDistSQLConverter
    
    * Add PropertiesUtils
---
 .../ral/convert/AlgorithmDistSQLConverter.java     | 41 +++++-----------------
 .../query/ral/convert/DistSQLScriptConstants.java  |  4 ---
 .../infra/util/props/PropertiesUtils.java}         | 31 ++++------------
 3 files changed, 15 insertions(+), 61 deletions(-)

diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
index 8524b04378e..555b1fedc3e 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
@@ -20,10 +20,7 @@ package 
org.apache.shardingsphere.distsql.handler.engine.query.ral.convert;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-
-import java.util.Iterator;
-import java.util.Properties;
-import java.util.TreeMap;
+import org.apache.shardingsphere.infra.util.props.PropertiesUtils;
 
 /**
  * Algorithm DistSQL converter.
@@ -31,6 +28,10 @@ import java.util.TreeMap;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class AlgorithmDistSQLConverter {
     
+    private static final String ALGORITHM_TYPE_WITHOUT_PROPS = 
"TYPE(NAME='%s')";
+    
+    private static final String ALGORITHM_TYPE_WITH_PROPS = "TYPE(NAME='%s', 
PROPERTIES(%s))";
+    
     /**
      * Get algorithm type.
      *
@@ -38,38 +39,14 @@ public final class AlgorithmDistSQLConverter {
      * @return algorithm type
      */
     public static String getAlgorithmType(final AlgorithmConfiguration 
algorithmConfig) {
-        StringBuilder result = new StringBuilder();
         if (null == algorithmConfig) {
-            return result.toString();
+            return "";
         }
+        StringBuilder result = new StringBuilder();
         String type = algorithmConfig.getType().toLowerCase();
         result.append(algorithmConfig.getProps().isEmpty()
-                ? 
String.format(DistSQLScriptConstants.ALGORITHM_TYPE_WITHOUT_PROPS, type)
-                : String.format(DistSQLScriptConstants.ALGORITHM_TYPE, type, 
getAlgorithmProperties(algorithmConfig.getProps())));
-        return result.toString();
-    }
-    
-    /**
-     * Get algorithm properties.
-     *
-     * @param props properties
-     * @return algorithm properties
-     */
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    public static String getAlgorithmProperties(final Properties props) {
-        StringBuilder result = new StringBuilder();
-        Iterator<String> iterator = new TreeMap(props).keySet().iterator();
-        while (iterator.hasNext()) {
-            String key = iterator.next();
-            Object value = props.get(key);
-            if (null == value) {
-                continue;
-            }
-            result.append(String.format(DistSQLScriptConstants.PROPERTY, key, 
value));
-            if (iterator.hasNext()) {
-                result.append(DistSQLScriptConstants.COMMA).append(' ');
-            }
-        }
+                ? String.format(ALGORITHM_TYPE_WITHOUT_PROPS, type)
+                : String.format(ALGORITHM_TYPE_WITH_PROPS, type, 
PropertiesUtils.toString(algorithmConfig.getProps())));
         return result.toString();
     }
 }
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/DistSQLScriptConstants.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/DistSQLScriptConstants.java
index bfea6bedfd7..7acbaa72cad 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/DistSQLScriptConstants.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/DistSQLScriptConstants.java
@@ -64,8 +64,4 @@ public final class DistSQLScriptConstants {
             + ")";
     
     public static final String PROPERTY = "'%s'='%s'";
-    
-    public static final String ALGORITHM_TYPE = "TYPE(NAME='%s', 
PROPERTIES(%s))";
-    
-    public static final String ALGORITHM_TYPE_WITHOUT_PROPS = 
"TYPE(NAME='%s')";
 }
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/PropertiesUtils.java
similarity index 56%
copy from 
infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
copy to 
infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/PropertiesUtils.java
index 8524b04378e..80fd978eecd 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/query/ral/convert/AlgorithmDistSQLConverter.java
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/PropertiesUtils.java
@@ -15,39 +15,20 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.distsql.handler.engine.query.ral.convert;
+package org.apache.shardingsphere.infra.util.props;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
 
 import java.util.Iterator;
 import java.util.Properties;
 import java.util.TreeMap;
 
 /**
- * Algorithm DistSQL converter.
+ * Properties utilities.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class AlgorithmDistSQLConverter {
-    
-    /**
-     * Get algorithm type.
-     *
-     * @param algorithmConfig algorithm configuration
-     * @return algorithm type
-     */
-    public static String getAlgorithmType(final AlgorithmConfiguration 
algorithmConfig) {
-        StringBuilder result = new StringBuilder();
-        if (null == algorithmConfig) {
-            return result.toString();
-        }
-        String type = algorithmConfig.getType().toLowerCase();
-        result.append(algorithmConfig.getProps().isEmpty()
-                ? 
String.format(DistSQLScriptConstants.ALGORITHM_TYPE_WITHOUT_PROPS, type)
-                : String.format(DistSQLScriptConstants.ALGORITHM_TYPE, type, 
getAlgorithmProperties(algorithmConfig.getProps())));
-        return result.toString();
-    }
+public final class PropertiesUtils {
     
     /**
      * Get algorithm properties.
@@ -56,7 +37,7 @@ public final class AlgorithmDistSQLConverter {
      * @return algorithm properties
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public static String getAlgorithmProperties(final Properties props) {
+    public static String toString(final Properties props) {
         StringBuilder result = new StringBuilder();
         Iterator<String> iterator = new TreeMap(props).keySet().iterator();
         while (iterator.hasNext()) {
@@ -65,9 +46,9 @@ public final class AlgorithmDistSQLConverter {
             if (null == value) {
                 continue;
             }
-            result.append(String.format(DistSQLScriptConstants.PROPERTY, key, 
value));
+            result.append(String.format("'%s'='%s'", key, value));
             if (iterator.hasNext()) {
-                result.append(DistSQLScriptConstants.COMMA).append(' ');
+                result.append(",").append(' ');
             }
         }
         return result.toString();

Reply via email to