You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2023/05/31 15:32:42 UTC

[shardingsphere] branch master updated: Fix sonar issue on CDCSchemaTableUtils (#25970)

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

chengzhang 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 457326ca483 Fix sonar issue on CDCSchemaTableUtils (#25970)
457326ca483 is described below

commit 457326ca483be9eb4828168785183e66f9b65523
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed May 31 23:32:34 2023 +0800

    Fix sonar issue on CDCSchemaTableUtils (#25970)
---
 .../pipeline/cdc/util/CDCSchemaTableUtils.java     | 59 +++++++++++++---------
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java b/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
index 34373ece4eb..a6db39787e1 100644
--- a/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
+++ b/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
@@ -34,7 +34,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Set;
 
 /**
@@ -53,30 +52,14 @@ public final class CDCSchemaTableUtils {
     public static Map<String, Set<String>> parseTableExpressionWithSchema(final ShardingSphereDatabase database, final Collection<SchemaTable> schemaTables) {
         Map<String, Set<String>> result = new HashMap<>();
         Collection<String> systemSchemas = database.getProtocolType().getSystemSchemas();
-        Optional<SchemaTable> allSchemaTablesOptional = schemaTables.stream().filter(each -> "*".equals(each.getTable()) && ("*".equals(each.getSchema()) || each.getSchema().isEmpty())).findFirst();
-        if (allSchemaTablesOptional.isPresent()) {
-            for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
-                if (systemSchemas.contains(entry.getKey())) {
-                    continue;
-                }
-                entry.getValue().getAllTableNames().forEach(tableName -> result.computeIfAbsent(entry.getKey(), ignored -> new HashSet<>()).add(tableName));
-            }
-            return result;
+        if (schemaTables.stream().anyMatch(each -> "*".equals(each.getTable()) && ("*".equals(each.getSchema()) || each.getSchema().isEmpty()))) {
+            return parseTableExpressionWithAllTables(database, systemSchemas);
         }
         for (SchemaTable each : schemaTables) {
             if ("*".equals(each.getSchema())) {
-                for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
-                    if (systemSchemas.contains(entry.getKey())) {
-                        continue;
-                    }
-                    entry.getValue().getAllTableNames().stream().filter(tableName -> tableName.equals(each.getTable())).findFirst()
-                            .ifPresent(tableName -> result.computeIfAbsent(entry.getKey(), ignored -> new HashSet<>()).add(tableName));
-                }
+                result.putAll(parseTableExpressionWithAllSchema(database, systemSchemas, each));
             } else if ("*".equals(each.getTable())) {
-                String schemaName = each.getSchema().isEmpty() ? getDefaultSchema(database.getProtocolType()) : each.getSchema();
-                ShardingSphereSchema schema = database.getSchema(schemaName);
-                ShardingSpherePreconditions.checkNotNull(schema, () -> new SchemaNotFoundException(each.getSchema()));
-                schema.getAllTableNames().forEach(tableName -> result.computeIfAbsent(schemaName, ignored -> new HashSet<>()).add(tableName));
+                result.putAll(parseTableExpressionWithAllTable(database, each));
             } else {
                 result.computeIfAbsent(each.getSchema(), ignored -> new HashSet<>()).add(each.getTable());
             }
@@ -84,6 +67,37 @@ public final class CDCSchemaTableUtils {
         return result;
     }
     
+    private static Map<String, Set<String>> parseTableExpressionWithAllTables(final ShardingSphereDatabase database, final Collection<String> systemSchemas) {
+        Map<String, Set<String>> result = new HashMap<>(database.getSchemas().size(), 1);
+        for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
+            if (!systemSchemas.contains(entry.getKey())) {
+                entry.getValue().getAllTableNames().forEach(tableName -> result.computeIfAbsent(entry.getKey(), ignored -> new HashSet<>()).add(tableName));
+            }
+            
+        }
+        return result;
+    }
+    
+    private static Map<String, Set<String>> parseTableExpressionWithAllSchema(final ShardingSphereDatabase database, final Collection<String> systemSchemas, final SchemaTable table) {
+        Map<String, Set<String>> result = new HashMap<>();
+        for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
+            if (!systemSchemas.contains(entry.getKey())) {
+                entry.getValue().getAllTableNames().stream().filter(tableName -> tableName.equals(table.getTable())).findFirst()
+                        .ifPresent(tableName -> result.computeIfAbsent(entry.getKey(), ignored -> new HashSet<>()).add(tableName));
+            }
+        }
+        return result;
+    }
+    
+    private static Map<String, Set<String>> parseTableExpressionWithAllTable(final ShardingSphereDatabase database, final SchemaTable each) {
+        Map<String, Set<String>> result = new HashMap<>();
+        String schemaName = each.getSchema().isEmpty() ? getDefaultSchema(database.getProtocolType()) : each.getSchema();
+        ShardingSphereSchema schema = database.getSchema(schemaName);
+        ShardingSpherePreconditions.checkNotNull(schema, () -> new SchemaNotFoundException(each.getSchema()));
+        schema.getAllTableNames().forEach(tableName -> result.computeIfAbsent(schemaName, ignored -> new HashSet<>()).add(tableName));
+        return result;
+    }
+    
     private static String getDefaultSchema(final DatabaseType databaseType) {
         if (!(databaseType instanceof SchemaSupportedDatabaseType)) {
             return null;
@@ -99,9 +113,8 @@ public final class CDCSchemaTableUtils {
      * @return parsed table names
      */
     public static Collection<String> parseTableExpressionWithoutSchema(final ShardingSphereDatabase database, final List<String> tableNames) {
-        Optional<String> allTablesOptional = tableNames.stream().filter("*"::equals).findFirst();
         ShardingSphereSchema schema = database.getSchema(database.getName());
         Set<String> allTableNames = null == schema ? Collections.emptySet() : new HashSet<>(schema.getAllTableNames());
-        return allTablesOptional.isPresent() ? allTableNames : new HashSet<>(tableNames);
+        return tableNames.stream().anyMatch("*"::equals) ? allTableNames : new HashSet<>(tableNames);
     }
 }