1818package org .apache .shardingsphere .infra .metadata .database .schema .manager ;
1919
2020import com .cedarsoftware .util .CaseInsensitiveMap ;
21- import com .cedarsoftware .util .CaseInsensitiveSet ;
2221import lombok .AccessLevel ;
2322import lombok .NoArgsConstructor ;
24- import org .apache .commons .lang3 .StringUtils ;
23+ import org .apache .commons .lang3 .Strings ;
2524import org .apache .shardingsphere .infra .util .directory .ClasspathResourceDirectoryReader ;
2625
2726import java .io .InputStream ;
2827import java .util .Collection ;
29- import java .util .Collections ;
3028import java .util .LinkedList ;
3129import java .util .List ;
3230import java .util .Map ;
33- import java .util .Map .Entry ;
34- import java .util .Optional ;
3531import java .util .stream .Collectors ;
3632import java .util .stream .Stream ;
3733
4137@ NoArgsConstructor (access = AccessLevel .PRIVATE )
4238public 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