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.schema;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.database.connector.core.metadata.data.model.SchemaMetaData;
22  import org.apache.shardingsphere.database.connector.core.metadata.data.model.TableMetaData;
23  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
24  import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry;
25  import org.apache.shardingsphere.infra.metadata.database.schema.reviser.table.TableMetaDataReviseEngine;
26  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
27  import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
28  
29  import java.util.Collection;
30  import java.util.Map.Entry;
31  import java.util.Optional;
32  import java.util.stream.Collectors;
33  
34  /**
35   * Schema meta data revise engine.
36   */
37  @RequiredArgsConstructor
38  public final class SchemaMetaDataReviseEngine {
39      
40      private final Collection<ShardingSphereRule> rules;
41      
42      private final ConfigurationProperties props;
43      
44      /**
45       * Revise schema meta data.
46       *
47       * @param originalMetaData original schema meta data
48       * @return revised schema meta data
49       */
50      @SuppressWarnings({"rawtypes", "unchecked"})
51      public SchemaMetaData revise(final SchemaMetaData originalMetaData) {
52          SchemaMetaData result = originalMetaData;
53          for (Entry<ShardingSphereRule, MetaDataReviseEntry> entry : OrderedSPILoader.getServices(MetaDataReviseEntry.class, rules).entrySet()) {
54              result = revise(result, entry.getKey(), entry.getValue());
55          }
56          return result;
57      }
58      
59      private <T extends ShardingSphereRule> SchemaMetaData revise(final SchemaMetaData originalMetaData, final T rule, final MetaDataReviseEntry<T> reviseEntry) {
60          TableMetaDataReviseEngine<T> tableMetaDataReviseEngine = new TableMetaDataReviseEngine<>(rule, reviseEntry);
61          Optional<? extends SchemaTableAggregationReviser<T>> aggregationReviser = reviseEntry.getSchemaTableAggregationReviser(props);
62          if (!aggregationReviser.isPresent()) {
63              return new SchemaMetaData(originalMetaData.getName(), originalMetaData.getTables().stream().map(tableMetaDataReviseEngine::revise).collect(Collectors.toList()));
64          }
65          for (TableMetaData each : originalMetaData.getTables()) {
66              aggregationReviser.get().add(tableMetaDataReviseEngine.revise(each));
67          }
68          return new SchemaMetaData(originalMetaData.getName(), aggregationReviser.get().aggregate(rule));
69      }
70  }