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



##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptTableMetaDataBuilder.java
##########
@@ -49,6 +60,73 @@
                 ? TableMetaDataLoader.load(dataSourceMap.get(dataSourceName), 
tableName, databaseType) : Optional.empty();
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final DatabaseType databaseType, final Map<String, DataSource> 
dataSourceMap, final DataNodes dataNodes,
+                                                     final EncryptRule rule, 
final ConfigurationProperties props, final ExecutorService executorService) 
throws SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
findDialectTableMetaDataLoader(databaseType);
+        Collection<String> loadTableNames = 
tableNames.stream().filter(tableName -> 
rule.findEncryptTable(tableName).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return loader.isPresent() ? loadByDialect(loader.get(), 
loadTableNames, dataSourceMap, dataNodes, executorService)
+                : loadByDefault(loadTableNames, dataSourceMap, dataNodes, 
rule, databaseType);
+    }
+    
+    private Map<String, TableMetaData> loadByDialect(final 
DialectTableMetaDataLoader dialectTableMetaDataLoader, final Collection<String> 
tableNames,
+                                                               final 
Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
+                                                               final 
ExecutorService executorService) throws SQLException {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        Map<String, Collection<String>> dataSourceTablesMap = 
getDataSourceTablesGroup(tableNames, dataSourceMap, dataNodes);
+        Collection<Future<Map<String, TableMetaData>>> futures = new 
LinkedList<>();
+        for (Map.Entry<String, Collection<String>> each : 
dataSourceTablesMap.entrySet()) {
+            futures.add(executorService.submit(() -> dialectTableMetaDataLoader
+                    .load(dataSourceMap.get(each.getKey()), each.getValue(), 
false)));
+        }
+        try {
+            for (Future<Map<String, TableMetaData>> each : futures) {
+                result.putAll(each.get());
+            }
+        } catch (final InterruptedException | ExecutionException ex) {
+            if (ex.getCause() instanceof SQLException) {
+                throw (SQLException) ex.getCause();
+            }
+            throw new ShardingSphereException(ex);
+        }
+        return result;
+    }
+    
+    private Map<String, Collection<String>> getDataSourceTablesGroup(final 
Collection<String> tableNames, final Map<String, DataSource> dataSourceMap, 
final DataNodes dataNodes) {
+        Map<String, Collection<String>> result = new LinkedHashMap<>();
+        for (String tableName : tableNames) {
+            String dataSourceName = 
dataNodes.getDataNodes(tableName).stream().map(DataNode::getDataSourceName).findFirst().orElseGet(()
 -> dataSourceMap.keySet().iterator().next());
+            Collection<String> collection = 
result.getOrDefault(dataSourceName, new LinkedList<>());
+            collection.add(tableName);
+            result.put(dataSourceName, collection);

Review comment:
       @tuichenchuxin Use `putIfAbsent` is better.

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptTableMetaDataBuilder.java
##########
@@ -49,6 +60,73 @@
                 ? TableMetaDataLoader.load(dataSourceMap.get(dataSourceName), 
tableName, databaseType) : Optional.empty();
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final DatabaseType databaseType, final Map<String, DataSource> 
dataSourceMap, final DataNodes dataNodes,
+                                                     final EncryptRule rule, 
final ConfigurationProperties props, final ExecutorService executorService) 
throws SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
findDialectTableMetaDataLoader(databaseType);
+        Collection<String> loadTableNames = 
tableNames.stream().filter(tableName -> 
rule.findEncryptTable(tableName).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return loader.isPresent() ? loadByDialect(loader.get(), 
loadTableNames, dataSourceMap, dataNodes, executorService)
+                : loadByDefault(loadTableNames, dataSourceMap, dataNodes, 
rule, databaseType);
+    }
+    
+    private Map<String, TableMetaData> loadByDialect(final 
DialectTableMetaDataLoader dialectTableMetaDataLoader, final Collection<String> 
tableNames,
+                                                               final 
Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
+                                                               final 
ExecutorService executorService) throws SQLException {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        Map<String, Collection<String>> dataSourceTablesMap = 
getDataSourceTablesGroup(tableNames, dataSourceMap, dataNodes);
+        Collection<Future<Map<String, TableMetaData>>> futures = new 
LinkedList<>();
+        for (Map.Entry<String, Collection<String>> each : 
dataSourceTablesMap.entrySet()) {

Review comment:
       @tuichenchuxin Please use static import for `Map.Entry`.

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptTableMetaDataBuilder.java
##########
@@ -49,6 +60,73 @@
                 ? TableMetaDataLoader.load(dataSourceMap.get(dataSourceName), 
tableName, databaseType) : Optional.empty();
     }
     
+    @Override
+    public Map<String, TableMetaData> load(final Collection<String> 
tableNames, final DatabaseType databaseType, final Map<String, DataSource> 
dataSourceMap, final DataNodes dataNodes,
+                                                     final EncryptRule rule, 
final ConfigurationProperties props, final ExecutorService executorService) 
throws SQLException {
+        Optional<DialectTableMetaDataLoader> loader = 
findDialectTableMetaDataLoader(databaseType);
+        Collection<String> loadTableNames = 
tableNames.stream().filter(tableName -> 
rule.findEncryptTable(tableName).isPresent()).collect(Collectors.toList());
+        if (loadTableNames.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return loader.isPresent() ? loadByDialect(loader.get(), 
loadTableNames, dataSourceMap, dataNodes, executorService)
+                : loadByDefault(loadTableNames, dataSourceMap, dataNodes, 
rule, databaseType);
+    }
+    
+    private Map<String, TableMetaData> loadByDialect(final 
DialectTableMetaDataLoader dialectTableMetaDataLoader, final Collection<String> 
tableNames,
+                                                               final 
Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
+                                                               final 
ExecutorService executorService) throws SQLException {
+        Map<String, TableMetaData> result = new LinkedHashMap<>();
+        Map<String, Collection<String>> dataSourceTablesMap = 
getDataSourceTablesGroup(tableNames, dataSourceMap, dataNodes);
+        Collection<Future<Map<String, TableMetaData>>> futures = new 
LinkedList<>();
+        for (Map.Entry<String, Collection<String>> each : 
dataSourceTablesMap.entrySet()) {
+            futures.add(executorService.submit(() -> dialectTableMetaDataLoader
+                    .load(dataSourceMap.get(each.getKey()), each.getValue(), 
false)));
+        }
+        try {
+            for (Future<Map<String, TableMetaData>> each : futures) {
+                result.putAll(each.get());
+            }
+        } catch (final InterruptedException | ExecutionException ex) {
+            if (ex.getCause() instanceof SQLException) {
+                throw (SQLException) ex.getCause();
+            }
+            throw new ShardingSphereException(ex);
+        }
+        return result;
+    }
+    
+    private Map<String, Collection<String>> getDataSourceTablesGroup(final 
Collection<String> tableNames, final Map<String, DataSource> dataSourceMap, 
final DataNodes dataNodes) {
+        Map<String, Collection<String>> result = new LinkedHashMap<>();
+        for (String tableName : tableNames) {
+            String dataSourceName = 
dataNodes.getDataNodes(tableName).stream().map(DataNode::getDataSourceName).findFirst().orElseGet(()
 -> dataSourceMap.keySet().iterator().next());
+            Collection<String> collection = 
result.getOrDefault(dataSourceName, new LinkedList<>());

Review comment:
       @tuichenchuxin `tableNames` is better.




-- 
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