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.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
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
62
63
64
65
66 public String getSchemaName(final String logicTableName) {
67 return mapping.get(new ShardingSphereIdentifier(logicTableName));
68 }
69
70
71
72
73
74
75
76 public String getSchemaName(final ShardingSphereIdentifier logicTableName) {
77 return mapping.get(logicTableName);
78 }
79
80
81
82
83
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 }