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