strongduanmu commented on a change in pull request #11767:
URL: https://github.com/apache/shardingsphere/pull/11767#discussion_r690050286



##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptTableMetaDataBuilder.java
##########
@@ -49,6 +57,31 @@
                 ? TableMetaDataLoader.load(dataSourceMap.get(dataSourceName), 
tableName, databaseType) : Optional.empty();
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final EncryptRule rule, final SchemaBuilderMaterials materials,
+                                           final ExecutorService 
executorService) throws SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
TableMetaDataLoader.findDialectTableMetaDataLoader(materials.getDatabaseType());
+        Collection<String> loadTableNames = tableNames.stream().filter(each -> 
rule.findEncryptTable(each).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        Map<String, Collection<String>> dataSourceTables = 
getTableGroup(loadTableNames, materials);
+        return loader.isPresent() ? TableMetaDataLoader.load(loader.get(), 
dataSourceTables, materials.getDataSourceMap(), executorService)
+                : TableMetaDataLoader.load(dataSourceTables, 
materials.getDatabaseType(), materials.getDataSourceMap());
+    }
+    
+    private Map<String, Collection<String>> getTableGroup(final 
Collection<String> tableNames, final SchemaBuilderMaterials materials) {
+        Map<String, Collection<String>> result = new LinkedHashMap<>();
+        DataNodes dataNodes = new DataNodes(materials.getRules());
+        for (String tableName : tableNames) {

Review comment:
       @tuichenchuxin modify `tableName` to `each`.

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
##########
@@ -82,6 +87,63 @@
         return Optional.of(actualTableMetaDataMap.values().iterator().next());
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials,
+                                           final ExecutorService 
executorService) throws SQLException {
+        Collection<String> loadTableNames = tableNames.stream().filter(each -> 
rule.findTableRule(each).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        boolean isCheckingMetaData = 
materials.getProps().getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED);
+        return isCheckingMetaData ? loadWithCheck(loadTableNames, rule, 
materials) : loadWithOutCheck(loadTableNames, rule, materials, executorService);
+    }
+    
+    private Map<String, TableMetaData> loadWithCheck(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials) {
+        int maxConnectionsSizePerQuery = 
materials.getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
+        Map<String, TableMetaData> result = new HashMap<>();
+        for (String tableName : tableNames) {

Review comment:
       @tuichenchuxin Same problem.

##########
File path: 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/loader/TableMetaDataLoader.java
##########
@@ -59,6 +69,26 @@
         }
     }
     
+    /**
+     * Load table meta data.
+     *
+     * @param dataSourceTable data source table name map
+     * @param databaseType database type
+     * @param dataSourceMap data source map
+     * @return table meta data map
+     * @throws SQLException SQL exception
+     */
+    public static Map<String, TableMetaData> load(final Map<String, 
Collection<String>> dataSourceTable, final DatabaseType databaseType,
+                                                  final Map<String, 
DataSource> dataSourceMap) throws SQLException {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        for (Entry<String, Collection<String>> each : 
dataSourceTable.entrySet()) {

Review comment:
       @tuichenchuxin `entry` is better.

##########
File path: 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/loader/TableMetaDataLoader.java
##########
@@ -59,6 +69,26 @@
         }
     }
     
+    /**
+     * Load table meta data.
+     *
+     * @param dataSourceTable data source table name map
+     * @param databaseType database type
+     * @param dataSourceMap data source map
+     * @return table meta data map
+     * @throws SQLException SQL exception
+     */
+    public static Map<String, TableMetaData> load(final Map<String, 
Collection<String>> dataSourceTable, final DatabaseType databaseType,
+                                                  final Map<String, 
DataSource> dataSourceMap) throws SQLException {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        for (Entry<String, Collection<String>> each : 
dataSourceTable.entrySet()) {
+            for (String tableName : each.getValue()) {

Review comment:
       @tuichenchuxin `each` is better.

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
##########
@@ -82,6 +87,63 @@
         return Optional.of(actualTableMetaDataMap.values().iterator().next());
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials,
+                                           final ExecutorService 
executorService) throws SQLException {
+        Collection<String> loadTableNames = tableNames.stream().filter(each -> 
rule.findTableRule(each).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        boolean isCheckingMetaData = 
materials.getProps().getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED);
+        return isCheckingMetaData ? loadWithCheck(loadTableNames, rule, 
materials) : loadWithOutCheck(loadTableNames, rule, materials, executorService);
+    }
+    
+    private Map<String, TableMetaData> loadWithCheck(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials) {
+        int maxConnectionsSizePerQuery = 
materials.getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
+        Map<String, TableMetaData> result = new HashMap<>();
+        for (String tableName : tableNames) {
+            TableRule tableRule = rule.getTableRule(tableName);
+            Map<String, TableMetaData> actualTableMetaDataMap = 
parallelLoadTables(materials.getDatabaseType(), materials.getDataSourceMap(),
+                    new DataNodes(materials.getRules()), tableName, 
maxConnectionsSizePerQuery);
+            if (actualTableMetaDataMap.isEmpty()) {
+                continue;
+            }
+            checkUniformed(tableRule.getLogicTable(), actualTableMetaDataMap, 
rule);
+            result.put(tableRule.getLogicTable(), 
actualTableMetaDataMap.values().iterator().next());
+        }
+        return result;
+    }
+    
+    private Map<String, TableMetaData> loadWithOutCheck(final 
Collection<String> tableNames, final ShardingRule rule,
+                                                        final 
SchemaBuilderMaterials materials, final ExecutorService executorService) throws 
SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
TableMetaDataLoader.findDialectTableMetaDataLoader(materials.getDatabaseType());
+        Map<String, Collection<String>> dataSourceTables = 
getTableGroup(tableNames, materials);
+        return loader.isPresent() ? new 
LinkedHashMap<>(getLogicTableName(TableMetaDataLoader.load(loader.get(), 
dataSourceTables, materials.getDataSourceMap(), executorService).values(), 
rule))

Review comment:
       @tuichenchuxin Is it better to get the results first and then decorate 
them?

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
##########
@@ -82,6 +87,63 @@
         return Optional.of(actualTableMetaDataMap.values().iterator().next());
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials,
+                                           final ExecutorService 
executorService) throws SQLException {
+        Collection<String> loadTableNames = tableNames.stream().filter(each -> 
rule.findTableRule(each).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        boolean isCheckingMetaData = 
materials.getProps().getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED);
+        return isCheckingMetaData ? loadWithCheck(loadTableNames, rule, 
materials) : loadWithOutCheck(loadTableNames, rule, materials, executorService);
+    }
+    
+    private Map<String, TableMetaData> loadWithCheck(final Collection<String> 
tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials) {
+        int maxConnectionsSizePerQuery = 
materials.getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
+        Map<String, TableMetaData> result = new HashMap<>();
+        for (String tableName : tableNames) {
+            TableRule tableRule = rule.getTableRule(tableName);
+            Map<String, TableMetaData> actualTableMetaDataMap = 
parallelLoadTables(materials.getDatabaseType(), materials.getDataSourceMap(),
+                    new DataNodes(materials.getRules()), tableName, 
maxConnectionsSizePerQuery);
+            if (actualTableMetaDataMap.isEmpty()) {
+                continue;
+            }
+            checkUniformed(tableRule.getLogicTable(), actualTableMetaDataMap, 
rule);
+            result.put(tableRule.getLogicTable(), 
actualTableMetaDataMap.values().iterator().next());
+        }
+        return result;
+    }
+    
+    private Map<String, TableMetaData> loadWithOutCheck(final 
Collection<String> tableNames, final ShardingRule rule,
+                                                        final 
SchemaBuilderMaterials materials, final ExecutorService executorService) throws 
SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
TableMetaDataLoader.findDialectTableMetaDataLoader(materials.getDatabaseType());
+        Map<String, Collection<String>> dataSourceTables = 
getTableGroup(tableNames, materials);
+        return loader.isPresent() ? new 
LinkedHashMap<>(getLogicTableName(TableMetaDataLoader.load(loader.get(), 
dataSourceTables, materials.getDataSourceMap(), executorService).values(), 
rule))
+                : new 
LinkedHashMap<>(getLogicTableName(TableMetaDataLoader.load(dataSourceTables, 
materials.getDatabaseType(), materials.getDataSourceMap()).values(), rule));
+    }
+    
+    private Map<String, Collection<String>> getTableGroup(final 
Collection<String> tableNames, final SchemaBuilderMaterials materials) {
+        DataNodes dataNodes = new DataNodes(materials.getRules());
+        Map<String, Collection<String>> result = new LinkedHashMap<>();
+        for (String tableName : tableNames) {
+            DataNode dataNode = 
dataNodes.getDataNodes(tableName).iterator().next();
+            Collection<String> tables = 
result.getOrDefault(dataNode.getDataSourceName(), new LinkedList<>());
+            tables.add(dataNode.getTableName());
+            result.putIfAbsent(dataNode.getDataSourceName(), tables);
+        }
+        return result;
+    }
+    
+    private Map<String, TableMetaData> getLogicTableName(final 
Collection<TableMetaData> tableMetaDatas, final ShardingRule rule) {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        for (TableMetaData each : tableMetaDatas) {
+            if (rule.findLogicTableByActualTable(each.getName()).isPresent()) {

Review comment:
       @tuichenchuxin Please avoid multiple calls `findLogicTableByActualTable` 
method, using `ifPresent`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to