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.data.pipeline.core.ingest.dumper.mapper;
19  
20  import lombok.ToString;
21  import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
22  import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
23  
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  import java.util.stream.Collectors;
30  
31  /**
32   * Table and schema name mapper.
33   */
34  @ToString
35  public final class TableAndSchemaNameMapper {
36      
37      private final Map<ShardingSphereIdentifier, String> mapping;
38      
39      public TableAndSchemaNameMapper(final Map<String, String> tableSchemaMap) {
40          mapping = null == tableSchemaMap ? Collections.emptyMap() : getLogicTableNameMap(tableSchemaMap);
41      }
42      
43      public TableAndSchemaNameMapper(final Collection<String> tableNames) {
44          Map<String, String> tableNameSchemaMap = tableNames.stream().map(each -> each.split("\\.")).filter(split -> split.length > 1).collect(Collectors.toMap(split -> split[1], split -> split[0]));
45          mapping = getLogicTableNameMap(tableNameSchemaMap);
46      }
47      
48      private Map<ShardingSphereIdentifier, String> getLogicTableNameMap(final Map<String, String> tableSchemaMap) {
49          Map<ShardingSphereIdentifier, String> result = new HashMap<>(tableSchemaMap.size(), 1F);
50          for (Entry<String, String> entry : tableSchemaMap.entrySet()) {
51              String tableName = entry.getKey();
52              String schemaName = entry.getValue();
53              if (null != schemaName) {
54                  result.put(new ShardingSphereIdentifier(tableName), schemaName);
55              }
56          }
57          return result;
58      }
59      
60      /**
61       * Get schema name.
62       *
63       * @param logicTableName logic table name
64       * @return schema name
65       */
66      public String getSchemaName(final String logicTableName) {
67          return mapping.get(new ShardingSphereIdentifier(logicTableName));
68      }
69      
70      /**
71       * Get schema name.
72       *
73       * @param logicTableName logic table name
74       * @return schema name
75       */
76      public String getSchemaName(final ShardingSphereIdentifier logicTableName) {
77          return mapping.get(logicTableName);
78      }
79      
80      /**
81       * Get qualified tables.
82       *
83       * @return qualified tables
84       */
85      public Collection<QualifiedTable> getQualifiedTables() {
86          return mapping.entrySet().stream().map(entry -> new QualifiedTable(entry.getValue(), entry.getKey().getValue())).collect(Collectors.toList());
87      }
88  }