Skip to content

Commit

Permalink
Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDat…
Browse files Browse the repository at this point in the history
…aIdentifier (#33862)

* Remove useless getter of ShardingSphereDatabase.schemas

* Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDataIdentifier

* Refactor ShardingSphereDatabase.schemas' key as ShardingSphereMetaDataIdentifier
  • Loading branch information
terrymanu authored Dec 1, 2024
1 parent 1671636 commit 2cdc107
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -62,15 +63,25 @@ public final class ShardingSphereDatabase {

private final RuleMetaData ruleMetaData;

private final Map<String, ShardingSphereSchema> schemas;
@Getter(AccessLevel.NONE)
private final Map<ShardingSphereMetaDataIdentifier, ShardingSphereSchema> schemas;

public ShardingSphereDatabase(final String name, final DatabaseType protocolType, final ResourceMetaData resourceMetaData,
final RuleMetaData ruleMetaData, final Map<String, ShardingSphereSchema> schemas) {
this.name = name;
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<ShardingSphereSchema> 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)));
}

/**
Expand Down Expand Up @@ -154,7 +165,7 @@ public Collection<ShardingSphereSchema> getAllSchemas() {
* @return contains schema from database or not
*/
public boolean containsSchema(final String schemaName) {
return schemas.containsKey(schemaName);
return schemas.containsKey(new ShardingSphereMetaDataIdentifier(schemaName));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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<String, ShardingSphereDatabase> databases = InternalMetaDataFactory.create(mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS),
Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class));
Map<String, ShardingSphereDatabase> 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")));
Expand Down

0 comments on commit 2cdc107

Please sign in to comment.