You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/08/06 08:57:11 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #11345: Improve loading TableMetaData for DialectTableMetaDataLoader

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



##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
##########
@@ -148,9 +155,27 @@ private static void appendDialectRemainTables(final DialectTableMetaDataLoader d
         }
     }
     
+    private static void putTablesForFutures(final Map<String, TableMetaData> tableMetaMap, final Map<String, DataNode> logicTableFirstDataNodeMap,
+            final Map<String, TableMetaData> tables) {
+        Map<String, String> firstActualLogicTableMap = logicTableFirstDataNodeMap.entrySet().stream()
+                .collect(Collectors.toMap(entry -> entry.getValue().getTableName(), Map.Entry::getKey));
+        for (Map.Entry<String, TableMetaData> entry : tableMetaMap.entrySet()) {

Review comment:
       Same problem.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
##########
@@ -76,7 +77,7 @@
         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()) {

Review comment:
       @Beyondeclipse Please use static import for Entry.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {
+    
+    /**
+     * Get mapping between logic-table name and data nodes.
+     * @param materials schema builder materials

Review comment:
       @Beyondeclipse Please keep a blank line.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {

Review comment:
       @Beyondeclipse Add javadoc for this class.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
##########
@@ -131,14 +132,20 @@ private static void appendRemainTables(final SchemaBuilderMaterials materials, f
     }
     
     private static void appendDialectRemainTables(final DialectTableMetaDataLoader dialectLoader, final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException {
+    
+        Map<String, Collection<DataNode>> logicTableDataNodesMap = SchemaBuilderHelper.getLogicTableDataNodesMap(materials);
+        Map<String, DataNode> logicTableFirstDataNodeMap = SchemaBuilderHelper.getLogicTableFirstDataNodeMap(logicTableDataNodesMap);
+        
+        Map<String, Collection<String>> datasourceExcludeTablesMap = SchemaBuilderHelper.getDatasourceExcludeTablesMap(logicTableDataNodesMap, logicTableFirstDataNodeMap);

Review comment:
       @Beyondeclipse dataSourceExcludeTablesMap is better.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
##########
@@ -148,9 +155,27 @@ private static void appendDialectRemainTables(final DialectTableMetaDataLoader d
         }
     }
     
+    private static void putTablesForFutures(final Map<String, TableMetaData> tableMetaMap, final Map<String, DataNode> logicTableFirstDataNodeMap,
+            final Map<String, TableMetaData> tables) {
+        Map<String, String> firstActualLogicTableMap = logicTableFirstDataNodeMap.entrySet().stream()
+                .collect(Collectors.toMap(entry -> entry.getValue().getTableName(), Map.Entry::getKey));
+        for (Map.Entry<String, TableMetaData> entry : tableMetaMap.entrySet()) {
+            String actualTableName = entry.getKey();
+            String logicTableName = firstActualLogicTableMap.get(actualTableName);
+            if (null != logicTableName && !logicTableName.equals(actualTableName)) {
+                if (!tables.containsKey(logicTableName)) {
+                    TableMetaData copyTableMetaData = entry.getValue();
+                    tables.put(logicTableName, new TableMetaData(logicTableName, copyTableMetaData.getColumns().values(), copyTableMetaData.getIndexes().values()));
+                }
+            } else {
+                tables.put(actualTableName, entry.getValue());
+            }
+        }
+    }
+    
     private static void appendDefaultRemainTables(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) throws SQLException {
         Collection<String> existedTableNames = getExistedTables(materials.getRules(), tables);
-        for (Entry<String, DataSource> entry : materials.getDataSourceMap().entrySet()) {
+        for (Map.Entry<String, DataSource> entry : materials.getDataSourceMap().entrySet()) {

Review comment:
       Same problem.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {
+    
+    /**
+     * Get mapping between logic-table name and data nodes.
+     * @param materials schema builder materials
+     * @return logic-table to data nodes map
+     */
+    public static Map<String, Collection<DataNode>> getLogicTableDataNodesMap(final SchemaBuilderMaterials materials) {
+        Map<String, Collection<DataNode>> logicTableDataNodesMap = materials.getRules().stream()
+                .filter(each -> each instanceof DataNodeContainedRule)
+                .flatMap(each -> ((DataNodeContainedRule) each).getAllDataNodes().entrySet().stream())
+                .filter(entry -> entry.getValue().size() > 1)
+                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Review comment:
       Same problem.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
##########
@@ -148,9 +155,27 @@ private static void appendDialectRemainTables(final DialectTableMetaDataLoader d
         }
     }
     
+    private static void putTablesForFutures(final Map<String, TableMetaData> tableMetaMap, final Map<String, DataNode> logicTableFirstDataNodeMap,
+            final Map<String, TableMetaData> tables) {
+        Map<String, String> firstActualLogicTableMap = logicTableFirstDataNodeMap.entrySet().stream()
+                .collect(Collectors.toMap(entry -> entry.getValue().getTableName(), Map.Entry::getKey));

Review comment:
       @Beyondeclipse Please add mergeFunction param for `Collectors.toMap()`.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {
+    
+    /**
+     * Get mapping between logic-table name and data nodes.
+     * @param materials schema builder materials
+     * @return logic-table to data nodes map
+     */
+    public static Map<String, Collection<DataNode>> getLogicTableDataNodesMap(final SchemaBuilderMaterials materials) {
+        Map<String, Collection<DataNode>> logicTableDataNodesMap = materials.getRules().stream()
+                .filter(each -> each instanceof DataNodeContainedRule)
+                .flatMap(each -> ((DataNodeContainedRule) each).getAllDataNodes().entrySet().stream())
+                .filter(entry -> entry.getValue().size() > 1)
+                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+        if (MapUtils.isEmpty(logicTableDataNodesMap)) {
+            return Collections.emptyMap();
+        }
+        Map<String, Collection<String>> logicActualdataSourceMap = getLogicActualDataSourceMap(materials);
+        if (MapUtils.isNotEmpty(logicActualdataSourceMap)) {
+            logicTableDataNodesMap = replaceLogicDataNodeWithActualDataNode(logicTableDataNodesMap, logicActualdataSourceMap);
+        }
+        return logicTableDataNodesMap;
+    }
+    
+    private static Map<String, Collection<String>> getLogicActualDataSourceMap(final SchemaBuilderMaterials materials) {
+        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 = (a instanceof LinkedList) ? a : new LinkedList<>(a);
+                    result.addAll(b);
+                    return result;
+                }));
+        return dataSourceContainedMap;
+    }
+    
+    private static Map<String, Collection<DataNode>> replaceLogicDataNodeWithActualDataNode(
+            final Map<String, Collection<DataNode>> logicTableDataNodesMap,
+            final Map<String, Collection<String>> logicActualdataSourceMap) {
+        Map<String, Collection<DataNode>> replaceResult = new HashMap<>(logicTableDataNodesMap.size(), 1);
+        for (Map.Entry<String, Collection<DataNode>> entry : logicTableDataNodesMap.entrySet()) {
+            Collection<DataNode> dataNodes = entry.getValue();
+            Collection<DataNode> actualDataNodeList = new LinkedList<>();
+            for (DataNode each : dataNodes) {
+                Collection<String> actualDataSourceNameList = logicActualdataSourceMap.get(each.getDataSourceName());
+                if (CollectionUtils.isEmpty(actualDataSourceNameList)) {
+                    actualDataNodeList.add(each);
+                } else {
+                    for (String actualDataSourceName : actualDataSourceNameList) {
+                        actualDataNodeList.add(new DataNode(actualDataSourceName, each.getTableName()));
+                    }
+                }
+            }
+            replaceResult.put(entry.getKey(), actualDataNodeList);
+        }
+        return replaceResult;
+    }
+    
+    /**
+     * Get mapping between logic-table name and the first data node of 'logicTableDataNodesMap'.
+     * @param logicTableDataNodesMap data nodes map
+     * @return logic-table to first data node map
+     */
+    public static Map<String, DataNode> getLogicTableFirstDataNodeMap(final Map<String, Collection<DataNode>> logicTableDataNodesMap) {

Review comment:
       @Beyondeclipse Please add test case for public method.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {
+    
+    /**
+     * Get mapping between logic-table name and data nodes.
+     * @param materials schema builder materials
+     * @return logic-table to data nodes map
+     */
+    public static Map<String, Collection<DataNode>> getLogicTableDataNodesMap(final SchemaBuilderMaterials materials) {
+        Map<String, Collection<DataNode>> logicTableDataNodesMap = materials.getRules().stream()
+                .filter(each -> each instanceof DataNodeContainedRule)
+                .flatMap(each -> ((DataNodeContainedRule) each).getAllDataNodes().entrySet().stream())
+                .filter(entry -> entry.getValue().size() > 1)
+                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+        if (MapUtils.isEmpty(logicTableDataNodesMap)) {
+            return Collections.emptyMap();
+        }
+        Map<String, Collection<String>> logicActualdataSourceMap = getLogicActualDataSourceMap(materials);
+        if (MapUtils.isNotEmpty(logicActualdataSourceMap)) {

Review comment:
       !logicActualdataSourceMap.isEmpty() is better.

##########
File path: shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderHelper.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.collections4.MapUtils;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+
+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.stream.Collectors;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaBuilderHelper {
+    
+    /**
+     * Get mapping between logic-table name and data nodes.
+     * @param materials schema builder materials
+     * @return logic-table to data nodes map
+     */
+    public static Map<String, Collection<DataNode>> getLogicTableDataNodesMap(final SchemaBuilderMaterials materials) {
+        Map<String, Collection<DataNode>> logicTableDataNodesMap = materials.getRules().stream()
+                .filter(each -> each instanceof DataNodeContainedRule)
+                .flatMap(each -> ((DataNodeContainedRule) each).getAllDataNodes().entrySet().stream())
+                .filter(entry -> entry.getValue().size() > 1)
+                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+        if (MapUtils.isEmpty(logicTableDataNodesMap)) {
+            return Collections.emptyMap();
+        }
+        Map<String, Collection<String>> logicActualdataSourceMap = getLogicActualDataSourceMap(materials);
+        if (MapUtils.isNotEmpty(logicActualdataSourceMap)) {
+            logicTableDataNodesMap = replaceLogicDataNodeWithActualDataNode(logicTableDataNodesMap, logicActualdataSourceMap);
+        }
+        return logicTableDataNodesMap;

Review comment:
       Please change it to result.




-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org