From b08a93ba15f47459703c610c741e150580cd5dad Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 1 Aug 2018 17:53:50 +0800 Subject: [PATCH] add init to ShardingTableMetaData's constructor --- .../core/merger/dal/show/ShowTablesMergedResult.java | 2 +- .../core/metadata/table/ShardingTableMetaData.java | 10 +++------- .../core/jdbc/core/datasource/ShardingDataSource.java | 6 ++---- .../jdbc/core/connection/ShardingConnectionTest.java | 7 ++----- .../io/shardingsphere/proxy/config/RuleRegistry.java | 5 +---- 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java b/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java index 6e73c2e22064d..41d8ed32fddb6 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java @@ -72,7 +72,7 @@ private Iterator init(final List queryResults String actualTableName = memoryResultSetRow.getCell(1).toString(); Optional tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName); if (!tableRule.isPresent()) { - if (shardingTableMetaData.containsTable(actualTableName) && tableNames.add(actualTableName)) { + if (shardingRule.getTableRules().isEmpty() || shardingTableMetaData.containsTable(actualTableName) && tableNames.add(actualTableName)) { result.add(memoryResultSetRow); } } else if (tableNames.add(tableRule.get().getLogicTable())) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java index c87031dc84c62..809aa8c09cd8a 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java @@ -46,17 +46,13 @@ public class ShardingTableMetaData { private final Map tableMetaDataMap = new ConcurrentHashMap<>(); - public ShardingTableMetaData(final ListeningExecutorService executorService, final TableMetaDataExecutorAdapter executorAdapter) { + public ShardingTableMetaData(final ShardingRule shardingRule, final ListeningExecutorService executorService, final TableMetaDataExecutorAdapter executorAdapter) { tableLoader = new TableLoader(executorAdapter); tableMetaDataLoader = new TableMetaDataLoader(executorService, executorAdapter); + init(shardingRule); } - /** - * Initialize sharding meta data. - * - * @param shardingRule sharding rule - */ - public void init(final ShardingRule shardingRule) { + private void init(final ShardingRule shardingRule) { try { initLogicTables(shardingRule); initDefaultTables(shardingRule); diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSource.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSource.java index 78e1a145963c6..eabc3edeb0530 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSource.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSource.java @@ -68,8 +68,7 @@ public ShardingDataSource(final Map dataSourceMap, final Sha shardingProperties = new ShardingProperties(null == props ? new Properties() : props); int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE); executorEngine = new ExecutorEngine(executorSize); - ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(executorEngine.getExecutorService(), new DataSourceMapTableMetaDataExecutorAdapter(dataSourceMap)); - shardingTableMetaData.init(shardingRule); + ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(shardingRule, executorEngine.getExecutorService(), new DataSourceMapTableMetaDataExecutorAdapter(dataSourceMap)); boolean showSQL = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW); shardingContext = new ShardingContext(dataSourceMap, shardingRule, getDatabaseType(), executorEngine, shardingTableMetaData, showSQL); } @@ -91,8 +90,7 @@ public void renew(final Map newDataSourceMap, final Sharding originalExecutorEngine.close(); } boolean newShowSQL = newShardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW); - ShardingTableMetaData shardingMetaData = new ShardingTableMetaData(executorEngine.getExecutorService(), new DataSourceMapTableMetaDataExecutorAdapter(newDataSourceMap)); - shardingMetaData.init(newShardingRule); + ShardingTableMetaData shardingMetaData = new ShardingTableMetaData(newShardingRule, executorEngine.getExecutorService(), new DataSourceMapTableMetaDataExecutorAdapter(newDataSourceMap)); shardingProperties = newShardingProperties; shardingContext = new ShardingContext(newDataSourceMap, newShardingRule, getDatabaseType(), executorEngine, shardingMetaData, newShowSQL); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java index a26a826007bd0..2b24a6bda4604 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java @@ -17,7 +17,6 @@ package io.shardingsphere.core.jdbc.core.connection; -import com.google.common.util.concurrent.ListeningExecutorService; import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; import io.shardingsphere.core.api.config.ShardingRuleConfiguration; import io.shardingsphere.core.api.config.TableRuleConfiguration; @@ -25,14 +24,12 @@ import io.shardingsphere.core.fixture.TestDataSource; import io.shardingsphere.core.jdbc.core.ShardingContext; import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; -import io.shardingsphere.core.jdbc.metadata.DataSourceMapTableMetaDataExecutorAdapter; import io.shardingsphere.core.metadata.table.ShardingTableMetaData; import io.shardingsphere.core.rule.ShardingRule; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mockito; import javax.sql.DataSource; import java.sql.Connection; @@ -44,6 +41,7 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; public final class ShardingConnectionTest { @@ -74,8 +72,7 @@ public void setUp() { Map dataSourceMap = new HashMap<>(1, 1); dataSourceMap.put(DS_NAME, masterSlaveDataSource); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()); - ListeningExecutorService executorService = Mockito.mock(ListeningExecutorService.class); - ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(executorService, new DataSourceMapTableMetaDataExecutorAdapter(dataSourceMap)); + ShardingTableMetaData shardingTableMetaData = mock(ShardingTableMetaData.class); ShardingContext shardingContext = new ShardingContext(dataSourceMap, shardingRule, DatabaseType.H2, null, shardingTableMetaData, false); connection = new ShardingConnection(shardingContext); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 771bc956b85b1..a4a82c08df33c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -134,10 +134,7 @@ public synchronized void init(final OrchestrationProxyConfiguration config) { */ public void initShardingMetaData(final ExecutorService executorService) { ShardingDataSourceMetaData shardingDataSourceMetaData = new ShardingDataSourceMetaData(getDataSourceURLs(dataSourceConfigurationMap), shardingRule, DatabaseType.MySQL); - ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(MoreExecutors.listeningDecorator(executorService), new ProxyTableMetaDataExecutorAdapter(backendDataSource)); - if (!isMasterSlaveOnly()) { - shardingTableMetaData.init(shardingRule); - } + ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(shardingRule, MoreExecutors.listeningDecorator(executorService), new ProxyTableMetaDataExecutorAdapter(backendDataSource)); metaData = new ShardingMetaData(shardingDataSourceMetaData, shardingTableMetaData); }