Skip to content

Commit

Permalink
Refactor schema builder (apache#12395)
Browse files Browse the repository at this point in the history
* refactor SchemaBuilder

* refactor SchemaBuilder

* refactor SchemaBuilder

* fix review suggestion

* fix review suggestions.

* fix review suggestions.
  • Loading branch information
tuichenchuxin authored Sep 14, 2021
1 parent bbf4a3d commit b27bf35
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.EncryptTable;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.RuleBasedTableMetaDataBuilder;
import org.apache.shardingsphere.infra.metadata.schema.builder.util.TableMetaDataUtil;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.RuleBasedTableMetaDataBuilder;
import org.apache.shardingsphere.infra.metadata.schema.builder.util.TableMetaDataUtil;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
Expand Down Expand Up @@ -62,10 +62,13 @@ public Map<String, TableMetaData> load(final Collection<String> tableNames, fina
return Collections.emptyMap();
}
Collection<TableMetaData> tableMetaDatas = TableMetaDataLoaderEngine.load(tableMetaDataLoaderMaterials, materials.getDatabaseType());
return isCheckingMetaData ? decorateWithCheckTableMetaData(tableMetaDatas, rule) : decorateLogicTableName(tableMetaDatas, rule);
if (isCheckingMetaData) {
checkTableMetaData(tableMetaDatas, rule);
}
return getTableMetaDataMap(tableMetaDatas, rule);
}

private Map<String, TableMetaData> decorateWithCheckTableMetaData(final Collection<TableMetaData> tableMetaDatas, final ShardingRule rule) {
private void checkTableMetaData(final Collection<TableMetaData> tableMetaDatas, final ShardingRule rule) {
Map<String, Collection<TableMetaData>> logicTableMetaDataMap = new LinkedHashMap<>();
for (TableMetaData each : tableMetaDatas) {
Optional<String> logicName = rule.findLogicTableByActualTable(each.getName());
Expand All @@ -78,14 +81,12 @@ private Map<String, TableMetaData> decorateWithCheckTableMetaData(final Collecti
for (Entry<String, Collection<TableMetaData>> entry : logicTableMetaDataMap.entrySet()) {
checkUniformed(entry.getKey(), entry.getValue(), rule);
}
return logicTableMetaDataMap.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().iterator().next(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private Map<String, TableMetaData> decorateLogicTableName(final Collection<TableMetaData> tableMetaDatas, final ShardingRule rule) {
private Map<String, TableMetaData> getTableMetaDataMap(final Collection<TableMetaData> tableMetaDatas, final ShardingRule rule) {
Map<String, TableMetaData> result = new LinkedHashMap<>();
for (TableMetaData each : tableMetaDatas) {
rule.findLogicTableByActualTable(each.getName()).ifPresent(tableName -> result.put(tableName, each));
rule.findLogicTableByActualTable(each.getName()).ifPresent(tableName -> result.putIfAbsent(tableName, each));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.DialectTableMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand All @@ -38,45 +38,43 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SchemaBuilder {

static {
ShardingSphereServiceLoader.register(DialectTableMetaDataLoader.class);
}

/**
* build actual and logic table meta data.
* Build sharding sphere schema.
*
* @param materials schema builder materials
* @return actual and logic table meta data
* @return sharding sphere schema
* @throws SQLException SQL exception
*/
public static Map<TableMetaData, TableMetaData> build(final SchemaBuilderMaterials materials) throws SQLException {
Map<String, TableMetaData> actualTableMetaMap = buildActualTableMetaDataMap(materials);
Map<String, TableMetaData> logicTableMetaMap = buildLogicTableMetaDataMap(materials, actualTableMetaMap);
Map<TableMetaData, TableMetaData> result = new HashMap<>(actualTableMetaMap.size(), 1);
for (Entry<String, TableMetaData> entry : actualTableMetaMap.entrySet()) {
result.put(entry.getValue(), logicTableMetaMap.getOrDefault(entry.getKey(), entry.getValue()));
}
return result;
}

private static Map<String, TableMetaData> buildActualTableMetaDataMap(final SchemaBuilderMaterials materials) throws SQLException {
public static ShardingSphereSchema build(final SchemaBuilderMaterials materials) throws SQLException {
Collection<String> allTableNames = materials.getRules().stream().filter(each -> each instanceof TableContainedRule)
.flatMap(shardingSphereRule -> ((TableContainedRule) shardingSphereRule).getTables().stream()).collect(Collectors.toSet());
return TableMetaDataBuilder.load(allTableNames, materials);
return new ShardingSphereSchema(TableMetaDataBuilder.load(allTableNames, materials));
}

/**
* Decorate sharding sphere schema.
*
* @param schema sharding sphere schema
* @param materials schema builder materials
* @return sharding sphere schema
*/
public static ShardingSphereSchema decorate(final ShardingSphereSchema schema, final SchemaBuilderMaterials materials) {
Map<String, TableMetaData> tableMetaDataMap = schema.getTables().values().stream().collect(Collectors
.toMap(TableMetaData::getName, Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
for (ShardingSphereRule each : materials.getRules()) {
if (each instanceof TableContainedRule) {
decorateByRule(tableMetaDataMap, each);
}
}
return new ShardingSphereSchema(tableMetaDataMap);
}

private static Map<String, TableMetaData> buildLogicTableMetaDataMap(final SchemaBuilderMaterials materials, final Map<String, TableMetaData> tables) {
Map<String, TableMetaData> result = new HashMap<>(materials.getRules().size(), 1);
for (ShardingSphereRule rule : materials.getRules()) {
if (rule instanceof TableContainedRule) {
for (String table : ((TableContainedRule) rule).getTables()) {
if (tables.containsKey(table)) {
TableMetaData metaData = TableMetaDataBuilder.decorate(table, tables.get(table), materials.getRules());
result.put(table, metaData);
}
}
private static void decorateByRule(final Map<String, TableMetaData> tableMetaDataMap, final ShardingSphereRule rule) {
for (String each : ((TableContainedRule) rule).getTables()) {
if (tableMetaDataMap.containsKey(each)) {
TableMetaData metaData = TableMetaDataBuilder.decorate(each, tableMetaDataMap.get(each), Collections.singletonList(rule));
tableMetaDataMap.put(each, metaData);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static Map<String, TableMetaData> load(final Collection<String> tableName
RuleBasedTableMetaDataBuilder<TableContainedRule> builder = entry.getValue();
Collection<String> needLoadTables = tableNames.stream().filter(each -> rule.getTables().contains(each)).filter(each -> !result.containsKey(each)).collect(Collectors.toList());
if (!needLoadTables.isEmpty()) {
result.putAll(decorateTableMetaData(builder.load(tableNames, rule, materials)));
result.putAll(decorateTableMetaData(builder.load(needLoadTables, rule, materials)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.metadata.schema.builder;
package org.apache.shardingsphere.infra.metadata.schema.builder.loader;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.DefaultTableMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.common.TableMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.DialectTableMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
Expand All @@ -43,6 +43,10 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public final class TableMetaDataLoaderEngine {

static {
ShardingSphereServiceLoader.register(DialectTableMetaDataLoader.class);
}

private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ShardingSphere-TableMetaDataLoaderEngine-%d").build());
Expand Down Expand Up @@ -72,7 +76,7 @@ private static Collection<TableMetaData> loadByDefault(final Collection<TableMet
Collection<TableMetaData> result = new LinkedList<>();
for (TableMetaDataLoaderMaterial each : materials) {
for (String tableName : each.getTableNames()) {
DefaultTableMetaDataLoader.load(each.getDataSource(), tableName, databaseType).ifPresent(result::add);
TableMetaDataLoader.load(each.getDataSource(), tableName, databaseType).ifPresent(result::add);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.metadata.schema.builder;
package org.apache.shardingsphere.infra.metadata.schema.builder.loader;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.metadata.schema.builder.loader;
package org.apache.shardingsphere.infra.metadata.schema.builder.loader.common;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.metadata.schema.builder.loader;
package org.apache.shardingsphere.infra.metadata.schema.builder.loader.common;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.metadata.schema.builder.loader;
package org.apache.shardingsphere.infra.metadata.schema.builder.loader.common;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -33,7 +33,7 @@
* Table meta data loader.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DefaultTableMetaDataLoader {
public final class TableMetaDataLoader {

/**
* Load table meta data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodes;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderMaterial;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.fixture.rule.CommonFixtureRule;
import org.apache.shardingsphere.infra.metadata.schema.fixture.rule.DataNodeContainedFixtureRule;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
Expand Down Expand Up @@ -54,11 +55,10 @@ public final class SchemaBuilderTest {

@Test
public void assertBuildOfAllShardingTables() throws SQLException {
Map<TableMetaData, TableMetaData> actual = SchemaBuilder.build(new SchemaBuilderMaterials(
ShardingSphereSchema schema = SchemaBuilder.build(new SchemaBuilderMaterials(
databaseType, Collections.singletonMap("logic_db", dataSource), Arrays.asList(new CommonFixtureRule(), new DataNodeContainedFixtureRule()), props));
assertThat(actual.values().size(), is(2));
assertThat(actual.keySet().size(), is(2));
assertSchemaOfShardingTables(actual.keySet());
assertThat(schema.getTables().keySet().size(), is(2));
assertSchemaOfShardingTables(schema.getTables().values());
}

private void assertSchemaOfShardingTables(final Collection<TableMetaData> actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.metadata.schema.builder.loader;

import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.common.ColumnMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.metadata.schema.builder.loader;

import org.apache.shardingsphere.infra.metadata.schema.builder.loader.common.IndexMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package org.apache.shardingsphere.infra.metadata.schema.builder.loader;

import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
Expand Down Expand Up @@ -49,7 +47,7 @@
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public final class DefaultTableMetaDataLoaderTest {
public final class TableMetaDataLoaderTest {

private static final String TEST_CATALOG = "catalog";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import org.apache.shardingsphere.singletable.constant.SingleTableOrder;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderEngine;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.RuleBasedTableMetaDataBuilder;
import org.apache.shardingsphere.infra.metadata.schema.builder.util.IndexMetaDataUtil;
import org.apache.shardingsphere.infra.metadata.schema.builder.util.TableMetaDataUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilder;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
Expand Down Expand Up @@ -72,10 +73,10 @@ public void assertBuildOfSingleTables() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
SingleTableRule singleTableRule = mockSingleTableRuleLoad(connection);
mockSQLLoad(connection);
Map<TableMetaData, TableMetaData> tableMetaData = SchemaBuilder.build(new SchemaBuilderMaterials(
ShardingSphereSchema schema = SchemaBuilder.build(new SchemaBuilderMaterials(
databaseType, Collections.singletonMap("logic_db", dataSource), Collections.singletonList(singleTableRule), props));
assertThat(tableMetaData.keySet().size(), is(2));
assertActualOfSingleTables(tableMetaData.keySet());
assertThat(schema.getTables().size(), is(2));
assertActualOfSingleTables(schema.getTables().values());
}

@SneakyThrows(SQLException.class)
Expand Down
Loading

0 comments on commit b27bf35

Please sign in to comment.