View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.metadata.database.schema.reviser;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData;
22  import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData;
23  import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
24  import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
25  import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
26  import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
27  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
28  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereConstraint;
29  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
30  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
31  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
32  import org.apache.shardingsphere.infra.metadata.database.schema.reviser.schema.SchemaMetaDataReviseEngine;
33  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
34  
35  import java.util.Collection;
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.LinkedList;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  import java.util.stream.Collectors;
42  
43  /**
44   * Meta data revise engine.
45   */
46  @RequiredArgsConstructor
47  public final class MetaDataReviseEngine {
48      
49      private final Collection<ShardingSphereRule> rules;
50      
51      /**
52       * Revise meta data.
53       *
54       * @param schemaMetaDataMap schema meta data map
55       * @param material generic schema builder material
56       * @return ShardingSphere schema map
57       */
58      public Map<String, ShardingSphereSchema> revise(final Map<String, SchemaMetaData> schemaMetaDataMap, final GenericSchemaBuilderMaterial material) {
59          if (schemaMetaDataMap.isEmpty()) {
60              return Collections.singletonMap(material.getDefaultSchemaName(), new ShardingSphereSchema(material.getDefaultSchemaName()));
61          }
62          Map<String, ShardingSphereSchema> result = new HashMap<>(schemaMetaDataMap.size(), 1F);
63          for (Entry<String, SchemaMetaData> entry : schemaMetaDataMap.entrySet()) {
64              SchemaMetaData schemaMetaData = new SchemaMetaDataReviseEngine(rules, material.getProps()).revise(entry.getValue());
65              result.put(entry.getKey(), new ShardingSphereSchema(entry.getKey(), convertToTables(schemaMetaData.getTables()), new LinkedList<>()));
66          }
67          return result;
68      }
69      
70      private Collection<ShardingSphereTable> convertToTables(final Collection<TableMetaData> tableMetaDataList) {
71          return tableMetaDataList.stream().map(each -> new ShardingSphereTable(
72                  each.getName(), convertToColumns(each.getColumns()), convertToIndexes(each.getIndexes()), convertToConstraints(each.getConstraints()), each.getType())).collect(Collectors.toList());
73      }
74      
75      private Collection<ShardingSphereColumn> convertToColumns(final Collection<ColumnMetaData> columnMetaDataList) {
76          return columnMetaDataList.stream().map(ShardingSphereColumn::new).collect(Collectors.toList());
77      }
78      
79      private Collection<ShardingSphereIndex> convertToIndexes(final Collection<IndexMetaData> indexMetaDataList) {
80          return indexMetaDataList.stream().map(ShardingSphereIndex::new).collect(Collectors.toList());
81      }
82      
83      private Collection<ShardingSphereConstraint> convertToConstraints(final Collection<ConstraintMetaData> constraintMetaDataList) {
84          return constraintMetaDataList.stream().map(ShardingSphereConstraint::new).collect(Collectors.toList());
85      }
86  }