Skip to content

Commit

Permalink
add init to ShardingTableMetaData's constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 1, 2018
1 parent c778526 commit b08a93b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults
String actualTableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> 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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ public class ShardingTableMetaData {

private final Map<String, TableMetaData> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public ShardingDataSource(final Map<String, DataSource> 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);
}
Expand All @@ -91,8 +90,7 @@ public void renew(final Map<String, DataSource> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@

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;
import io.shardingsphere.core.constant.DatabaseType;
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;
Expand All @@ -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 {

Expand Down Expand Up @@ -74,8 +72,7 @@ public void setUp() {
Map<String, DataSource> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit b08a93b

Please sign in to comment.