From 2cdc107f74a52f0f29dc7730473a3774e15e62d4 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 1 Dec 2024 17:20:25 +0800 Subject: [PATCH] Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDataIdentifier (#33862) * Remove useless getter of ShardingSphereDatabase.schemas * Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDataIdentifier * Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDataIdentifier --- .../database/ShardingSphereDatabase.java | 25 +++++++++++++------ .../factory/InternalMetaDataFactoryTest.java | 12 ++++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index d3f962eea88a1..204042b884d6f 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.infra.metadata.database; -import com.cedarsoftware.util.CaseInsensitiveMap; +import lombok.AccessLevel; import lombok.Getter; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; @@ -34,6 +34,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial; import org.apache.shardingsphere.infra.metadata.database.schema.builder.SystemSchemaBuilder; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereMetaDataIdentifier; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; @@ -62,7 +63,8 @@ public final class ShardingSphereDatabase { private final RuleMetaData ruleMetaData; - private final Map schemas; + @Getter(AccessLevel.NONE) + private final Map schemas; public ShardingSphereDatabase(final String name, final DatabaseType protocolType, final ResourceMetaData resourceMetaData, final RuleMetaData ruleMetaData, final Map schemas) { @@ -70,7 +72,16 @@ public ShardingSphereDatabase(final String name, final DatabaseType protocolType this.protocolType = protocolType; this.resourceMetaData = resourceMetaData; this.ruleMetaData = ruleMetaData; - this.schemas = new CaseInsensitiveMap<>(schemas, new ConcurrentHashMap<>(schemas.size(), 1F)); + this.schemas = new ConcurrentHashMap<>(schemas.entrySet().stream().collect(Collectors.toMap(entry -> new ShardingSphereMetaDataIdentifier(entry.getKey()), Entry::getValue))); + } + + public ShardingSphereDatabase(final String name, final DatabaseType protocolType, final ResourceMetaData resourceMetaData, + final RuleMetaData ruleMetaData, final Collection schemas) { + this.name = name; + this.protocolType = protocolType; + this.resourceMetaData = resourceMetaData; + this.ruleMetaData = ruleMetaData; + this.schemas = new ConcurrentHashMap<>(schemas.stream().collect(Collectors.toMap(each -> new ShardingSphereMetaDataIdentifier(each.getName()), each -> each))); } /** @@ -154,7 +165,7 @@ public Collection getAllSchemas() { * @return contains schema from database or not */ public boolean containsSchema(final String schemaName) { - return schemas.containsKey(schemaName); + return schemas.containsKey(new ShardingSphereMetaDataIdentifier(schemaName)); } /** @@ -164,7 +175,7 @@ public boolean containsSchema(final String schemaName) { * @return schema */ public ShardingSphereSchema getSchema(final String schemaName) { - return schemas.get(schemaName); + return schemas.get(new ShardingSphereMetaDataIdentifier(schemaName)); } /** @@ -174,7 +185,7 @@ public ShardingSphereSchema getSchema(final String schemaName) { * @param schema schema */ public void addSchema(final String schemaName, final ShardingSphereSchema schema) { - schemas.put(schemaName, schema); + schemas.put(new ShardingSphereMetaDataIdentifier(schemaName), schema); } /** @@ -183,7 +194,7 @@ public void addSchema(final String schemaName, final ShardingSphereSchema schema * @param schemaName schema name */ public void dropSchema(final String schemaName) { - schemas.remove(schemaName); + schemas.remove(new ShardingSphereMetaDataIdentifier(schemaName)); } /** diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactoryTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactoryTest.java index c087bf9d6f329..51a59eb30df89 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactoryTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactoryTest.java @@ -43,8 +43,10 @@ class InternalMetaDataFactoryTest { @Test void assertCreateWithDatabaseName() { - ShardingSphereDatabase database = InternalMetaDataFactory.create("foo_db", - mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS), mock(DatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); + MetaDataPersistService persistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS); + when(persistService.getDatabaseMetaDataFacade().getSchema().load("foo_db")).thenReturn(Collections.emptyMap()); + ShardingSphereDatabase database = InternalMetaDataFactory.create( + "foo_db", persistService, mock(DatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); assertThat(database.getName(), is("foo_db")); assertThat(database.getProtocolType(), is(TypedSPILoader.getService(DatabaseType.class, "MySQL"))); assertTrue(database.getRuleMetaData().getRules().isEmpty()); @@ -64,12 +66,14 @@ void assertCreateWithDatabasesWithoutStorageUnits() { @Test void assertCreateWithDatabasesWithStorageUnits() { + MetaDataPersistService persistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS); + when(persistService.getDatabaseMetaDataFacade().getSchema().load("foo_db")).thenReturn(Collections.emptyMap()); DatabaseConfiguration databaseConfig = mock(DatabaseConfiguration.class); StorageUnit storageUnit = mock(StorageUnit.class); when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); when(databaseConfig.getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); - Map databases = InternalMetaDataFactory.create(mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS), - Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); + Map databases = InternalMetaDataFactory.create( + persistService, Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); assertThat(databases.size(), is(1)); assertThat(databases.get("foo_db").getName(), is("foo_db")); assertThat(databases.get("foo_db").getProtocolType(), is(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")));