Skip to content

Commit

Permalink
Refactor ShardingSphereMetaData (#28563)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereMetaData

* Refactor ShardingSphereMetaData
  • Loading branch information
terrymanu authored Sep 24, 2023
1 parent a164726 commit 73a6c78
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public final class ShardingSphereMetaData {
private final TemporaryConfigurationProperties temporaryProps;

public ShardingSphereMetaData() {
this(new LinkedHashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()),
new ConfigurationProperties(new Properties()));
this(new LinkedHashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()));
}

public ShardingSphereMetaData(final Map<String, ShardingSphereDatabase> databases, final ResourceMetaData globalResourceMetaData,
Expand All @@ -65,19 +64,6 @@ public ShardingSphereMetaData(final Map<String, ShardingSphereDatabase> database
temporaryProps = new TemporaryConfigurationProperties(props.getProps());
}

/**
* Add database.
*
* @param databaseName database name
* @param protocolType protocol database type
* @param props configuration properties
*/
public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
putDatabase(database);
globalRuleMetaData.findRules(ResourceHeldRule.class).forEach(each -> each.addResource(database));
}

/**
* Judge contains database from meta data or not.
*
Expand All @@ -99,12 +85,16 @@ public ShardingSphereDatabase getDatabase(final String databaseName) {
}

/**
* Put database.
* Add database.
*
* @param database database
* @param databaseName database name
* @param protocolType protocol database type
* @param props configuration properties
*/
public void putDatabase(final ShardingSphereDatabase database) {
public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
databases.put(database.getName().toLowerCase(), database);
globalRuleMetaData.findRules(ResourceHeldRule.class).forEach(each -> each.addResource(database));
}

/**
Expand All @@ -113,8 +103,7 @@ public void putDatabase(final ShardingSphereDatabase database) {
* @param databaseName database name
*/
public void dropDatabase(final String databaseName) {
ShardingSphereDatabase toBeRemovedDatabase = databases.remove(databaseName.toLowerCase());
closeResources(toBeRemovedDatabase);
closeResources(databases.remove(databaseName.toLowerCase()));
}

private void closeResources(final ShardingSphereDatabase database) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

package org.apache.shardingsphere.infra.metadata;

import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.storage.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand All @@ -36,15 +33,13 @@
import org.mockito.quality.Strictness;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -96,20 +91,4 @@ private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaD
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule)));
return result;
}

@Test
void assertGetPostgreSQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType),
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class));
assertNotNull(actual.getSchema("public"));
}

@Test
void assertGetMySQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType),
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class));
assertNotNull(actual.getSchema("foo_db"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,42 @@

package org.apache.shardingsphere.infra.metadata.database;

import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;

@ExtendWith(AutoMockExtension.class)
@StaticMockSettings(ShardingSphereDatabase.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ShardingSphereDatabaseTest {

@Test
Expand Down Expand Up @@ -73,4 +88,20 @@ void assertReloadRules() {
database.reloadRules(MutableDataNodeRule.class);
assertThat(database.getRuleMetaData().getRules().size(), is(3));
}

@Test
void assertGetPostgreSQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType),
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class));
assertNotNull(actual.getSchema("public"));
}

@Test
void assertGetMySQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType),
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class));
assertNotNull(actual.getSchema("foo_db"));
}
}

0 comments on commit 73a6c78

Please sign in to comment.