Skip to content

Commit 87f6cb7

Browse files
authored
Add DialectSystemSchemaManager (#36992)
* Refactor SystemSchemaManager * Add DialectSystemSchemaManager * Add DialectSystemSchemaManager
1 parent 7a24518 commit 87f6cb7

File tree

2 files changed

+132
-49
lines changed

2 files changed

+132
-49
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.manager;
19+
20+
import com.cedarsoftware.util.CaseInsensitiveMap;
21+
import com.cedarsoftware.util.CaseInsensitiveSet;
22+
23+
import java.io.InputStream;
24+
import java.util.Collection;
25+
import java.util.Collections;
26+
import java.util.Map;
27+
import java.util.stream.Collectors;
28+
29+
/**
30+
* Dialect system schema manager.
31+
*/
32+
public final class DialectSystemSchemaManager {
33+
34+
private final Map<String, Collection<String>> schemaAndTableMap = new CaseInsensitiveMap<>();
35+
36+
private final Map<String, Collection<String>> schemaAndResourceMap = new CaseInsensitiveMap<>();
37+
38+
/**
39+
* Put the table.
40+
*
41+
* @param schemaName schema name
42+
* @param tableName table name
43+
*/
44+
public void putTable(final String schemaName, final String tableName) {
45+
schemaAndTableMap.computeIfAbsent(schemaName, key -> new CaseInsensitiveSet<>()).add(tableName);
46+
}
47+
48+
/**
49+
* Put resource.
50+
*
51+
* @param schemaName schema name
52+
* @param resourcePath resource path
53+
*/
54+
public void putResource(final String schemaName, final String resourcePath) {
55+
schemaAndResourceMap.computeIfAbsent(schemaName, key -> new CaseInsensitiveSet<>()).add(resourcePath);
56+
}
57+
58+
/**
59+
* Judge whether the current table is system table.
60+
*
61+
* @param schemaName schema name
62+
* @param tableName table name
63+
* @return is system table or not
64+
*/
65+
public boolean isSystemTable(final String schemaName, final String tableName) {
66+
return null == schemaName
67+
? schemaAndTableMap.values().stream().anyMatch(each -> each.contains(tableName))
68+
: schemaAndTableMap.getOrDefault(schemaName, Collections.emptyList()).contains(tableName);
69+
}
70+
71+
/**
72+
* Judge whether the current table is system table.
73+
*
74+
* @param schemaName schema name
75+
* @param tableNames table names
76+
* @return is system table or not
77+
*/
78+
public boolean isSystemTable(final String schemaName, final Collection<String> tableNames) {
79+
Collection<String> tables = schemaAndTableMap.getOrDefault(schemaName, Collections.emptyList());
80+
for (String each : tableNames) {
81+
if (!tables.contains(each)) {
82+
return false;
83+
}
84+
}
85+
return true;
86+
}
87+
88+
/**
89+
* Get tables.
90+
*
91+
* @param schemaName schema
92+
* @return got tables
93+
*/
94+
public Collection<String> getTables(final String schemaName) {
95+
return schemaAndTableMap.getOrDefault(schemaName, Collections.emptyList());
96+
}
97+
98+
/**
99+
* Get all input streams.
100+
*
101+
* @param schemaName schema name
102+
* @return input streams
103+
*/
104+
public Collection<InputStream> getAllInputStreams(final String schemaName) {
105+
return schemaAndResourceMap.getOrDefault(schemaName, Collections.emptyList())
106+
.stream().map(each -> DialectSystemSchemaManager.class.getClassLoader().getResourceAsStream(each)).collect(Collectors.toList());
107+
}
108+
}

infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/SystemSchemaManager.java

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@
1818
package org.apache.shardingsphere.infra.metadata.database.schema.manager;
1919

2020
import com.cedarsoftware.util.CaseInsensitiveMap;
21-
import com.cedarsoftware.util.CaseInsensitiveSet;
2221
import lombok.AccessLevel;
2322
import lombok.NoArgsConstructor;
24-
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.commons.lang3.Strings;
2524
import org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader;
2625

2726
import java.io.InputStream;
2827
import java.util.Collection;
29-
import java.util.Collections;
3028
import java.util.LinkedList;
3129
import java.util.List;
3230
import java.util.Map;
33-
import java.util.Map.Entry;
34-
import java.util.Optional;
3531
import java.util.stream.Collectors;
3632
import java.util.stream.Stream;
3733

@@ -41,9 +37,7 @@
4137
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4238
public final class SystemSchemaManager {
4339

44-
private static final Map<String, Map<String, Collection<String>>> DATABASE_TYPE_SCHEMA_TABLE_MAP;
45-
46-
private static final Map<String, Map<String, Collection<String>>> DATABASE_TYPE_SCHEMA_RESOURCE_MAP;
40+
private static final Map<String, DialectSystemSchemaManager> DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP;
4741

4842
private static final String COMMON = "common";
4943

@@ -52,12 +46,19 @@ public final class SystemSchemaManager {
5246
try (Stream<String> resourceNameStream = ClasspathResourceDirectoryReader.read("schema")) {
5347
resourceNames = resourceNameStream.filter(each -> each.endsWith(".yaml")).collect(Collectors.toList());
5448
}
55-
DATABASE_TYPE_SCHEMA_TABLE_MAP = resourceNames.stream().map(resourceName -> resourceName.split("/")).filter(each -> each.length == 4)
56-
.collect(Collectors.groupingBy(path -> path[1], CaseInsensitiveMap::new, Collectors.groupingBy(path -> path[2], CaseInsensitiveMap::new,
57-
Collectors.mapping(path -> StringUtils.removeEnd(path[3], ".yaml"), Collectors.toCollection(CaseInsensitiveSet::new)))));
58-
DATABASE_TYPE_SCHEMA_RESOURCE_MAP = resourceNames.stream().map(resourceName -> resourceName.split("/")).filter(each -> each.length == 4)
59-
.collect(Collectors.groupingBy(path -> path[1], CaseInsensitiveMap::new, Collectors.groupingBy(path -> path[2], CaseInsensitiveMap::new,
60-
Collectors.mapping(path -> String.join("/", path), Collectors.toCollection(CaseInsensitiveSet::new)))));
49+
DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP = new CaseInsensitiveMap<>();
50+
for (String each : resourceNames) {
51+
String[] pathParts = each.split("/");
52+
if (4 == pathParts.length) {
53+
String databaseType = pathParts[1];
54+
String schemaName = pathParts[2];
55+
String tableName = Strings.CS.removeEnd(pathParts[3], ".yaml");
56+
String resourcePath = String.join("/", pathParts);
57+
DialectSystemSchemaManager dialectSystemSchemaManager = DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.computeIfAbsent(databaseType, key -> new DialectSystemSchemaManager());
58+
dialectSystemSchemaManager.putTable(schemaName, tableName);
59+
dialectSystemSchemaManager.putResource(schemaName, resourcePath);
60+
}
61+
}
6162
}
6263

6364
/**
@@ -68,12 +69,7 @@ public final class SystemSchemaManager {
6869
* @return is system table or not
6970
*/
7071
public static boolean isSystemTable(final String schema, final String tableName) {
71-
for (Entry<String, Map<String, Collection<String>>> entry : DATABASE_TYPE_SCHEMA_TABLE_MAP.entrySet()) {
72-
if (Optional.ofNullable(entry.getValue().get(schema)).map(tables -> tables.contains(tableName)).orElse(false)) {
73-
return true;
74-
}
75-
}
76-
return false;
72+
return DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.entrySet().stream().anyMatch(entry -> entry.getValue().getTables(schema).contains(tableName));
7773
}
7874

7975
/**
@@ -85,12 +81,8 @@ public static boolean isSystemTable(final String schema, final String tableName)
8581
* @return is system table or not
8682
*/
8783
public static boolean isSystemTable(final String databaseType, final String schema, final String tableName) {
88-
Map<String, Collection<String>> schemaTableMap = DATABASE_TYPE_SCHEMA_TABLE_MAP.getOrDefault(databaseType, Collections.emptyMap());
89-
Map<String, Collection<String>> commonTableMap = DATABASE_TYPE_SCHEMA_TABLE_MAP.getOrDefault(COMMON, Collections.emptyMap());
90-
if (null == schema) {
91-
return schemaTableMap.values().stream().anyMatch(each -> each.contains(tableName)) || commonTableMap.values().stream().anyMatch(each -> each.contains(tableName));
92-
}
93-
return schemaTableMap.getOrDefault(schema, Collections.emptyList()).contains(tableName) || commonTableMap.getOrDefault(schema, Collections.emptyList()).contains(tableName);
84+
return DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.containsKey(databaseType) && DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(databaseType).isSystemTable(schema, tableName)
85+
|| DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.containsKey(COMMON) && DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(COMMON).isSystemTable(schema, tableName);
9486
}
9587

9688
/**
@@ -102,14 +94,8 @@ public static boolean isSystemTable(final String databaseType, final String sche
10294
* @return is system table or not
10395
*/
10496
public static boolean isSystemTable(final String databaseType, final String schema, final Collection<String> tableNames) {
105-
Collection<String> databaseTypeTables = Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(databaseType)).map(schemas -> schemas.get(schema)).orElse(Collections.emptyList());
106-
Collection<String> commonTables = Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(COMMON)).map(schemas -> schemas.get(schema)).orElse(Collections.emptyList());
107-
for (String each : tableNames) {
108-
if (!databaseTypeTables.contains(each) && !commonTables.contains(each)) {
109-
return false;
110-
}
111-
}
112-
return true;
97+
return DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.containsKey(databaseType) && DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(databaseType).isSystemTable(schema, tableNames)
98+
|| DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.containsKey(COMMON) && DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(COMMON).isSystemTable(schema, tableNames);
11399
}
114100

115101
/**
@@ -121,8 +107,8 @@ public static boolean isSystemTable(final String databaseType, final String sche
121107
*/
122108
public static Collection<String> getTables(final String databaseType, final String schema) {
123109
Collection<String> result = new LinkedList<>();
124-
Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(databaseType)).map(schemas -> schemas.get(schema)).ifPresent(result::addAll);
125-
Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(COMMON)).map(schemas -> schemas.get(schema)).ifPresent(result::addAll);
110+
result.addAll(DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(databaseType).getTables(schema));
111+
result.addAll(DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(COMMON).getTables(schema));
126112
return result;
127113
}
128114

@@ -135,19 +121,8 @@ public static Collection<String> getTables(final String databaseType, final Stri
135121
*/
136122
public static Collection<InputStream> getAllInputStreams(final String databaseType, final String schema) {
137123
Collection<InputStream> result = new LinkedList<>();
138-
result.addAll(getInputStreams(databaseType, schema));
139-
result.addAll(getInputStreams(COMMON, schema));
140-
return result;
141-
}
142-
143-
private static Collection<InputStream> getInputStreams(final String databaseType, final String schema) {
144-
if (!DATABASE_TYPE_SCHEMA_RESOURCE_MAP.containsKey(databaseType) || !DATABASE_TYPE_SCHEMA_RESOURCE_MAP.get(databaseType).containsKey(schema)) {
145-
return Collections.emptyList();
146-
}
147-
Collection<InputStream> result = new LinkedList<>();
148-
for (String each : DATABASE_TYPE_SCHEMA_RESOURCE_MAP.get(databaseType).get(schema)) {
149-
result.add(SystemSchemaManager.class.getClassLoader().getResourceAsStream(each));
150-
}
124+
result.addAll(DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(databaseType).getAllInputStreams(schema));
125+
result.addAll(DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP.get(COMMON).getAllInputStreams(schema));
151126
return result;
152127
}
153128
}

0 commit comments

Comments
 (0)