1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
61
62
63
64
65 public String getSchemaName(final String logicTableName) {
66 return mapping.get(new CaseInsensitiveIdentifier(logicTableName));
67 }
68
69
70
71
72
73
74
75 public String getSchemaName(final CaseInsensitiveIdentifier logicTableName) {
76 return mapping.get(logicTableName);
77 }
78 }