Skip to content

Commit

Permalink
Add ShardingSphereDatabaseFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 19, 2025
1 parent dc6c5ae commit 4eb9545
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
Expand Down Expand Up @@ -109,7 +110,7 @@ public ShardingSphereDatabase getDatabase(final String databaseName) {
* @param props configuration properties
*/
public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
ShardingSphereDatabase database = ShardingSphereDatabaseFactory.create(databaseName, protocolType, props);
databases.put(new ShardingSphereIdentifier(database.getName()), database);
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases.values(), GlobalRuleChangedType.DATABASE_CHANGED));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,16 @@

import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder;
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.ShardingSphereIdentifier;
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;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -73,58 +63,6 @@ public ShardingSphereDatabase(final String name, final DatabaseType protocolType
this.schemas = new ConcurrentHashMap<>(schemas.stream().collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each)));
}

/**
* Create system database.
*
* @param name system database name
* @param protocolType protocol database type
* @param props configuration properties
* @return created system database
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final ConfigurationProperties props) {
DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(new LinkedHashMap<>(), new LinkedList<>());
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(new LinkedList<>()), SystemSchemaBuilder.build(name, protocolType, props).values());
}

/**
* Create database.
*
* @param name database name
* @param protocolType database protocol type
* @param databaseConfig database configuration
* @param props configuration properties
* @param computeNodeInstanceContext compute node instance context
* @return created database
* @throws SQLException SQL exception
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException {
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
Collection<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType,
new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name))));
SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent);
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values());
}

/**
* Create database.
*
* @param name database name
* @param protocolType database protocol type
* @param databaseConfig database configuration
* @param computeNodeInstanceContext compute node instance context
* @param schemas schemas
* @return created database
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
final ComputeNodeInstanceContext computeNodeInstanceContext, final Collection<ShardingSphereSchema> schemas) {
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
Collection<ShardingSphereRule> rules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(rules), schemas);
}

/**
* Get all schemas.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
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.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder;
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.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* ShardingSphere database factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingSphereDatabaseFactory {

/**
* Create system database.
*
* @param name system database name
* @param protocolType protocol database type
* @param props configuration properties
* @return created system database
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final ConfigurationProperties props) {
DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(new LinkedHashMap<>(), new LinkedList<>());
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(new LinkedList<>()), SystemSchemaBuilder.build(name, protocolType, props).values());
}

/**
* Create database.
*
* @param name database name
* @param protocolType database protocol type
* @param databaseConfig database configuration
* @param props configuration properties
* @param computeNodeInstanceContext compute node instance context
* @return created database
* @throws SQLException SQL exception
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException {
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
Collection<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType,
new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name))));
SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent);
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values());
}

/**
* Create database.
*
* @param name database name
* @param protocolType database protocol type
* @param databaseConfig database configuration
* @param computeNodeInstanceContext compute node instance context
* @param schemas schemas
* @return created database
*/
public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
final ComputeNodeInstanceContext computeNodeInstanceContext, final Collection<ShardingSphereSchema> schemas) {
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
Collection<ShardingSphereRule> rules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(rules), schemas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ private static ShardingSphereDatabase create(final String databaseName, final Da
final Collection<ShardingSphereSchema> schemas, final ConfigurationProperties props,
final ComputeNodeInstanceContext computeNodeInstanceContext) {
return databaseConfig.getStorageUnits().isEmpty()
? ShardingSphereDatabase.create(databaseName, protocolType, props)
: ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, computeNodeInstanceContext, schemas);
? ShardingSphereDatabaseFactory.create(databaseName, protocolType, props)
: ShardingSphereDatabaseFactory.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, computeNodeInstanceContext, schemas);
}

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ private static Collection<ShardingSphereDatabase> createGenericDatabases(final M
for (Entry<String, DatabaseConfiguration> entry : databaseConfigMap.entrySet()) {
String databaseName = entry.getKey();
if (!entry.getValue().getStorageUnits().isEmpty() || !systemDatabase.getSystemSchemas().contains(databaseName)) {
result.add(ShardingSphereDatabase.create(databaseName, protocolType, entry.getValue(), props, instanceContext));
result.add(ShardingSphereDatabaseFactory.create(databaseName, protocolType, entry.getValue(), props, instanceContext));
}
}
return result;
Expand All @@ -103,7 +103,7 @@ private static Collection<ShardingSphereDatabase> createSystemDatabases(final Ma
Collection<ShardingSphereDatabase> result = new HashSet<>(systemDatabase.getSystemDatabaseSchemaMap().size(), 1F);
for (String each : systemDatabase.getSystemDatabaseSchemaMap().keySet()) {
if (!databaseConfigMap.containsKey(each) || databaseConfigMap.get(each).getStorageUnits().isEmpty()) {
result.add(ShardingSphereDatabase.create(each, protocolType, props));
result.add(ShardingSphereDatabaseFactory.create(each, protocolType, props));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand Down Expand Up @@ -68,7 +69,7 @@ void assertAddDatabase() {
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule);
DatabaseType databaseType = mock(DatabaseType.class);
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
when(ShardingSphereDatabase.create("foo_db", databaseType, configProps)).thenReturn(database);
when(ShardingSphereDatabaseFactory.create("foo_db", databaseType, configProps)).thenReturn(database);
Collection<ShardingSphereDatabase> databases = new LinkedList<>(Collections.singleton(database));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps);
metaData.addDatabase("foo_db", databaseType, configProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ void assertReloadRules() {
@Test
void assertGetPostgreSQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType,
ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("foo_db", databaseType,
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class));
assertNotNull(actual.getSchema("public"));
}

@Test
void assertGetMySQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType,
ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("foo_db", databaseType,
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class));
assertNotNull(actual.getSchema("foo_db"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.fixture.FixtureGlobalRule;
import org.apache.shardingsphere.infra.rule.builder.fixture.FixtureGlobalRuleConfiguration;
Expand Down Expand Up @@ -51,6 +52,6 @@ void assertBuildSingleRules() {
}

private ShardingSphereDatabase buildDatabase() {
return ShardingSphereDatabase.create("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), new ConfigurationProperties(new Properties()));
return ShardingSphereDatabaseFactory.create("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), new ConfigurationProperties(new Properties()));
}
}
Loading

0 comments on commit 4eb9545

Please sign in to comment.