strongduanmu commented on a change in pull request #11345: URL: https://github.com/apache/shardingsphere/pull/11345#discussion_r677928958
########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderWithDefaultLoader.java ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.metadata.schema.builder; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.shardingsphere.infra.database.type.DatabaseType; +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.exception.ShardingSphereException; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.ColumnMetaDataLoader; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.adapter.MetaDataLoaderConnectionAdapter; +import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData; +import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.stream.Collectors; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class SchemaBuilderWithDefaultLoader { Review comment: @Beyondeclipse Please add final. ########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java ########## @@ -76,109 +69,87 @@ Map<String, TableMetaData> actualTableMetaMap = buildActualTableMetaDataMap(materials); Map<String, TableMetaData> logicTableMetaMap = buildLogicTableMetaDataMap(materials, actualTableMetaMap); Map<TableMetaData, TableMetaData> tableMetaDataMap = new HashMap<>(actualTableMetaMap.size(), 1); - for (Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { + for (Map.Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { tableMetaDataMap.put(entry.getValue(), logicTableMetaMap.getOrDefault(entry.getKey(), entry.getValue())); } return tableMetaDataMap; } - + private static Map<String, TableMetaData> buildActualTableMetaDataMap(final SchemaBuilderMaterials materials) throws SQLException { - Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1); - appendRemainTables(materials, result); - for (ShardingSphereRule rule : materials.getRules()) { - if (rule instanceof TableContainedRule) { - for (String table : ((TableContainedRule) rule).getTables()) { - if (!result.containsKey(table)) { - TableMetaDataBuilder.load(table, materials).map(optional -> result.put(table, optional)); - } - } - } - } - return result; - } - - private static void appendRemainTables(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Optional<DialectTableMetaDataLoader> dialectLoader = findDialectTableMetaDataLoader(materials); + Map<String, Collection<DataNode>> logicTable2DataNodes = getLogicTable2DataNodes(materials); + + Optional<DialectTableMetaDataLoader> dialectLoader = SchemaBuilderWithDialectLoader.findDialectTableMetaDataLoader(materials); + Map<String, TableMetaData> result; if (dialectLoader.isPresent()) { - appendDialectRemainTables(dialectLoader.get(), materials, tables); - return; - } - appendDefaultRemainTables(materials, tables); - } - - private static Map<String, TableMetaData> buildLogicTableMetaDataMap(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1); - for (ShardingSphereRule rule : materials.getRules()) { - if (rule instanceof TableContainedRule) { - for (String table : ((TableContainedRule) rule).getTables()) { - if (tables.containsKey(table)) { - TableMetaData metaData = TableMetaDataBuilder.decorate(table, tables.get(table), materials.getRules()); - result.put(table, metaData); - } - } - } + result = SchemaBuilderWithDialectLoader.build(dialectLoader.get(), EXECUTOR_SERVICE, materials, logicTable2DataNodes); + } else { + result = SchemaBuilderWithDefaultLoader.build(EXECUTOR_SERVICE, materials, logicTable2DataNodes); } return result; } - - private static Optional<DialectTableMetaDataLoader> findDialectTableMetaDataLoader(final SchemaBuilderMaterials materials) { - for (DialectTableMetaDataLoader each : ShardingSphereServiceLoader.getSingletonServiceInstances(DialectTableMetaDataLoader.class)) { - if (each.getDatabaseType().equals(materials.getDatabaseType().getName())) { - return Optional.of(each); - } + + private static Map<String, Collection<DataNode>> getLogicTable2DataNodes(final SchemaBuilderMaterials materials) { + List<DataNodeContainedRule> dataNodeContainedRuleList = materials.getRules().stream() + .filter(each -> each instanceof DataNodeContainedRule) + .map(each -> (DataNodeContainedRule) each) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(dataNodeContainedRuleList)) { + return Collections.emptyMap(); } - return Optional.empty(); - } - - private static void appendDialectRemainTables(final DialectTableMetaDataLoader dialectLoader, final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Collection<Future<Map<String, TableMetaData>>> futures = new LinkedList<>(); - Collection<String> existedTables = getExistedTables(materials.getRules(), tables); - for (DataSource each : materials.getDataSourceMap().values()) { - futures.add(EXECUTOR_SERVICE.submit(() -> dialectLoader.load(each, existedTables))); + Map<String, Collection<DataNode>> logicTable2DataNodes = dataNodeContainedRuleList.stream() + .flatMap(each -> each.getAllDataNodes().entrySet().stream()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> { + Collection<DataNode> result = new LinkedList<>(a); + result.addAll(b); + return result; + })); + + Map<String, Collection<String>> dataSourceContainedMap = materials.getRules().stream() + .filter(each -> each instanceof DataSourceContainedRule) + .flatMap(each -> ((DataSourceContainedRule) each).getDataSourceMapper().entrySet().stream()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> { + Collection<String> result = new ArrayList<>(a.size() + b.size()); + result.addAll(a); + result.addAll(b); + return result; + })); + if (MapUtils.isEmpty(dataSourceContainedMap)) { + return logicTable2DataNodes; } - for (Future<Map<String, TableMetaData>> each : futures) { - try { - tables.putAll(each.get()); - } catch (final InterruptedException | ExecutionException ex) { - if (ex.getCause() instanceof SQLException) { - throw (SQLException) ex.getCause(); + + Map<String, Collection<DataNode>> replaceResult = new HashMap<>(logicTable2DataNodes.size(), 1); + for (Map.Entry<String, Collection<DataNode>> entry : logicTable2DataNodes.entrySet()) { + Collection<DataNode> dataNodes = entry.getValue(); + Collection<DataNode> newDataNodeList = new LinkedList<>(); + for (DataNode each : dataNodes) { + Collection<String> toReplaceDataSourceNames = dataSourceContainedMap.get(each.getDataSourceName()); + if (CollectionUtils.isEmpty(toReplaceDataSourceNames)) { + newDataNodeList.add(each); + } else { + for (String newDataSourceName : toReplaceDataSourceNames) { + newDataNodeList.add(new DataNode(newDataSourceName, each.getTableName())); Review comment: @Beyondeclipse Why we need to replace dataSource name? ########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderWithDialectLoader.java ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.metadata.schema.builder; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.exception.ShardingSphereException; +import org.apache.shardingsphere.infra.metadata.schema.builder.spi.DialectTableMetaDataLoader; +import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.stream.Collectors; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class SchemaBuilderWithDialectLoader { Review comment: @Beyondeclipse Please add final. ########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java ########## @@ -76,109 +69,87 @@ Map<String, TableMetaData> actualTableMetaMap = buildActualTableMetaDataMap(materials); Map<String, TableMetaData> logicTableMetaMap = buildLogicTableMetaDataMap(materials, actualTableMetaMap); Map<TableMetaData, TableMetaData> tableMetaDataMap = new HashMap<>(actualTableMetaMap.size(), 1); - for (Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { + for (Map.Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { tableMetaDataMap.put(entry.getValue(), logicTableMetaMap.getOrDefault(entry.getKey(), entry.getValue())); } return tableMetaDataMap; } - + private static Map<String, TableMetaData> buildActualTableMetaDataMap(final SchemaBuilderMaterials materials) throws SQLException { - Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1); - appendRemainTables(materials, result); - for (ShardingSphereRule rule : materials.getRules()) { - if (rule instanceof TableContainedRule) { - for (String table : ((TableContainedRule) rule).getTables()) { - if (!result.containsKey(table)) { - TableMetaDataBuilder.load(table, materials).map(optional -> result.put(table, optional)); - } - } - } - } - return result; - } - - private static void appendRemainTables(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Optional<DialectTableMetaDataLoader> dialectLoader = findDialectTableMetaDataLoader(materials); + Map<String, Collection<DataNode>> logicTable2DataNodes = getLogicTable2DataNodes(materials); + + Optional<DialectTableMetaDataLoader> dialectLoader = SchemaBuilderWithDialectLoader.findDialectTableMetaDataLoader(materials); + Map<String, TableMetaData> result; if (dialectLoader.isPresent()) { - appendDialectRemainTables(dialectLoader.get(), materials, tables); - return; - } - appendDefaultRemainTables(materials, tables); - } - - private static Map<String, TableMetaData> buildLogicTableMetaDataMap(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1); - for (ShardingSphereRule rule : materials.getRules()) { - if (rule instanceof TableContainedRule) { - for (String table : ((TableContainedRule) rule).getTables()) { - if (tables.containsKey(table)) { - TableMetaData metaData = TableMetaDataBuilder.decorate(table, tables.get(table), materials.getRules()); - result.put(table, metaData); - } - } - } + result = SchemaBuilderWithDialectLoader.build(dialectLoader.get(), EXECUTOR_SERVICE, materials, logicTable2DataNodes); + } else { + result = SchemaBuilderWithDefaultLoader.build(EXECUTOR_SERVICE, materials, logicTable2DataNodes); } return result; } - - private static Optional<DialectTableMetaDataLoader> findDialectTableMetaDataLoader(final SchemaBuilderMaterials materials) { - for (DialectTableMetaDataLoader each : ShardingSphereServiceLoader.getSingletonServiceInstances(DialectTableMetaDataLoader.class)) { - if (each.getDatabaseType().equals(materials.getDatabaseType().getName())) { - return Optional.of(each); - } + + private static Map<String, Collection<DataNode>> getLogicTable2DataNodes(final SchemaBuilderMaterials materials) { + List<DataNodeContainedRule> dataNodeContainedRuleList = materials.getRules().stream() + .filter(each -> each instanceof DataNodeContainedRule) + .map(each -> (DataNodeContainedRule) each) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(dataNodeContainedRuleList)) { + return Collections.emptyMap(); } - return Optional.empty(); - } - - private static void appendDialectRemainTables(final DialectTableMetaDataLoader dialectLoader, final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Collection<Future<Map<String, TableMetaData>>> futures = new LinkedList<>(); - Collection<String> existedTables = getExistedTables(materials.getRules(), tables); - for (DataSource each : materials.getDataSourceMap().values()) { - futures.add(EXECUTOR_SERVICE.submit(() -> dialectLoader.load(each, existedTables))); + Map<String, Collection<DataNode>> logicTable2DataNodes = dataNodeContainedRuleList.stream() Review comment: @Beyondeclipse Can we extract this two param `logicTable2DataNodes`, `dataSourceContainedMap` to a private method? ########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java ########## @@ -76,109 +69,87 @@ Map<String, TableMetaData> actualTableMetaMap = buildActualTableMetaDataMap(materials); Map<String, TableMetaData> logicTableMetaMap = buildLogicTableMetaDataMap(materials, actualTableMetaMap); Map<TableMetaData, TableMetaData> tableMetaDataMap = new HashMap<>(actualTableMetaMap.size(), 1); - for (Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { + for (Map.Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) { tableMetaDataMap.put(entry.getValue(), logicTableMetaMap.getOrDefault(entry.getKey(), entry.getValue())); } return tableMetaDataMap; } - + private static Map<String, TableMetaData> buildActualTableMetaDataMap(final SchemaBuilderMaterials materials) throws SQLException { - Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1); - appendRemainTables(materials, result); - for (ShardingSphereRule rule : materials.getRules()) { - if (rule instanceof TableContainedRule) { - for (String table : ((TableContainedRule) rule).getTables()) { - if (!result.containsKey(table)) { - TableMetaDataBuilder.load(table, materials).map(optional -> result.put(table, optional)); - } - } - } - } - return result; - } - - private static void appendRemainTables(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException { - Optional<DialectTableMetaDataLoader> dialectLoader = findDialectTableMetaDataLoader(materials); + Map<String, Collection<DataNode>> logicTable2DataNodes = getLogicTable2DataNodes(materials); + Review comment: @Beyondeclipse Remove useless black line. ########## File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderWithDefaultLoader.java ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.metadata.schema.builder; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.shardingsphere.infra.database.type.DatabaseType; +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.exception.ShardingSphereException; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.ColumnMetaDataLoader; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.adapter.MetaDataLoaderConnectionAdapter; +import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData; +import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.stream.Collectors; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class SchemaBuilderWithDefaultLoader { + + /** + * build table meta data with default loader. Review comment: @Beyondeclipse Capitalize the first letter in java doc. -- 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]
