Skip to content

Commit

Permalink
Cherry-pick c6dd048 and 1025a5e (#24817)
Browse files Browse the repository at this point in the history
* Avoid repeat call findRules method (#24797)

* Avoid repeat call findRules method

* Fix chekystyle

* Avoid repeat call findRules method

* Adjust single table to isTransparentStatement

* Change method name

* Refactor DatabaseConnector avoid repeat call findRules method (#24816)

* Refactor DatabaseConnector avoid repeat call findRules method

* Update

* Fix index
  • Loading branch information
zhaojinchao95 authored Mar 24, 2023
1 parent 1668c93 commit aa697cc
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Collection;

//TODO Consider introducing transparent table and enhance table.
/**
* ShardingSphere rule which contains column.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,26 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat
@Getter
private final Statement statement;

private final boolean transparentStatement;

private boolean closed;

private final ForceExecuteTemplate<ResultSet> forceExecuteTemplate = new ForceExecuteTemplate<>();

@Getter
private final ExecutionContext executionContext;

protected AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement, final ExecutionContext executionContext) {
protected AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement, final boolean transparentStatement, final ExecutionContext executionContext) {
Preconditions.checkArgument(!resultSets.isEmpty());
this.resultSets = resultSets;
this.statement = statement;
this.transparentStatement = transparentStatement;
this.executionContext = executionContext;
}

@Override
public final ResultSetMetaData getMetaData() throws SQLException {
return new ShardingSphereResultSetMetaData(resultSets.get(0).getMetaData(), getDatabase(), executionContext.getSqlStatementContext());
return new ShardingSphereResultSetMetaData(resultSets.get(0).getMetaData(), getDatabase(), transparentStatement, executionContext.getSqlStatementContext());
}

private ShardingSphereDatabase getDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ public final class ShardingSphereResultSet extends AbstractResultSetAdapter {

private final Map<String, Integer> columnLabelAndIndexMap;

public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final ExecutionContext executionContext) throws SQLException {
super(resultSets, statement, executionContext);
public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final boolean transparentStatement,
final ExecutionContext executionContext) throws SQLException {
super(resultSets, statement, transparentStatement, executionContext);
this.mergeResultSet = mergeResultSet;
columnLabelAndIndexMap = ShardingSphereResultSetUtil.createColumnLabelAndIndexMap(executionContext.getSqlStatementContext(), resultSets.get(0).getMetaData());
}

public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final ExecutionContext executionContext,
final Map<String, Integer> columnLabelAndIndexMap) {
super(resultSets, statement, executionContext);
public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final boolean transparentStatement,
final ExecutionContext executionContext, final Map<String, Integer> columnLabelAndIndexMap) {
super(resultSets, statement, transparentStatement, executionContext);
this.mergeResultSet = mergeResultSet;
this.columnLabelAndIndexMap = columnLabelAndIndexMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.ColumnContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand All @@ -47,12 +45,14 @@ public final class ShardingSphereResultSetMetaData extends WrapperAdapter implem

private final ShardingSphereDatabase database;

private final boolean transparentStatement;

private final SQLStatementContext<?> sqlStatementContext;

@Override
public int getColumnCount() throws SQLException {
if (sqlStatementContext instanceof SelectStatementContext) {
if (isAllSingleTable()) {
if (transparentStatement) {
return resultSetMetaData.getColumnCount();
}
if (hasSelectExpandProjections()) {
Expand Down Expand Up @@ -100,7 +100,7 @@ public int getColumnDisplaySize(final int column) throws SQLException {

@Override
public String getColumnLabel(final int column) throws SQLException {
if (isAllSingleTable()) {
if (transparentStatement) {
return resultSetMetaData.getColumnLabel(column);
}
if (hasSelectExpandProjections()) {
Expand All @@ -115,7 +115,7 @@ public String getColumnLabel(final int column) throws SQLException {

@Override
public String getColumnName(final int column) throws SQLException {
if (isAllSingleTable()) {
if (transparentStatement) {
return resultSetMetaData.getColumnName(column);
}
if (hasSelectExpandProjections()) {
Expand All @@ -135,20 +135,6 @@ private boolean hasSelectExpandProjections() {
return sqlStatementContext instanceof SelectStatementContext && !((SelectStatementContext) sqlStatementContext).getProjectionsContext().getExpandProjections().isEmpty();
}

private boolean isAllSingleTable() {
return sqlStatementContext.getTablesContext().getTableNames().stream().allMatch(each -> !containsInImmutableDataNodeContainedRule(each)
&& !containsInColumnContainedRule(each));
}

private boolean containsInImmutableDataNodeContainedRule(final String tableName) {
return database.getRuleMetaData().findRules(DataNodeContainedRule.class).stream()
.filter(each -> !(each instanceof MutableDataNodeRule)).anyMatch(each -> each.getAllTables().contains(tableName));
}

private boolean containsInColumnContainedRule(final String tableName) {
return database.getRuleMetaData().findRules(ColumnContainedRule.class).stream().anyMatch(each -> each.getTables().contains(tableName));
}

private void checkColumnIndex(final int column) throws SQLException {
List<Projection> actualProjections = ((SelectStatementContext) sqlStatementContext).getProjectionsContext().getExpandProjections();
if (column > actualProjections.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.ColumnContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RawExecutionRule;
import org.apache.shardingsphere.infra.rule.identifier.type.StorageConnectorReusableRule;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
Expand Down Expand Up @@ -152,6 +154,9 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
@Getter(AccessLevel.PROTECTED)
private final StatementManager statementManager;

@Getter
private final boolean transparentStatement;

private ExecutionContext executionContext;

private Map<String, Integer> columnLabelAndIndexMap;
Expand Down Expand Up @@ -207,13 +212,39 @@ private ShardingSpherePreparedStatement(final ShardingSphereConnection connectio
kernelProcessor = new KernelProcessor();
statementsCacheable = isStatementsCacheable(metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getRuleMetaData());
trafficRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
transparentStatement = isTransparentStatement(metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getRuleMetaData());
statementManager = new StatementManager();
}

private boolean isStatementsCacheable(final ShardingSphereRuleMetaData databaseRuleMetaData) {
return databaseRuleMetaData.findRules(StorageConnectorReusableRule.class).size() == databaseRuleMetaData.getRules().size() && !HintManager.isInstantiated();
}

private boolean isTransparentStatement(final ShardingSphereRuleMetaData ruleMetaData) {
Optional<DataNodeContainedRule> dataNodeContainedRule = getDataNodeContainedRuleForShardingRule(ruleMetaData.findRules(DataNodeContainedRule.class));
Collection<ColumnContainedRule> columnContainedRules = ruleMetaData.findRules(ColumnContainedRule.class);
for (String each : sqlStatementContext.getTablesContext().getTableNames()) {
return (!dataNodeContainedRule.isPresent() || !dataNodeContainedRule.get().getAllTables().contains(each)) && !containsInColumnContainedRule(each, columnContainedRules);
}
return true;
}

private Optional<DataNodeContainedRule> getDataNodeContainedRuleForShardingRule(final Collection<DataNodeContainedRule> dataNodeContainedRules) {
for (DataNodeContainedRule each : dataNodeContainedRules) {
if (!(each instanceof MutableDataNodeRule)) {
return Optional.of(each);
}
}
return Optional.empty();
}

private boolean containsInColumnContainedRule(final String tableName, final Collection<ColumnContainedRule> columnContainedRules) {
for (ColumnContainedRule each : columnContainedRules) {
return each.getTables().contains(tableName);
}
return false;
}

@Override
public ResultSet executeQuery() throws SQLException {
ResultSet result;
Expand All @@ -240,7 +271,7 @@ public ResultSet executeQuery() throws SQLException {
List<ResultSet> resultSets = getResultSets();
Map<String, Integer> columnLabelAndIndexMap = null != this.columnLabelAndIndexMap ? this.columnLabelAndIndexMap
: (this.columnLabelAndIndexMap = ShardingSphereResultSetUtil.createColumnLabelAndIndexMap(sqlStatementContext, resultSets.get(0).getMetaData()));
result = new ShardingSphereResultSet(resultSets, mergedResult, this, executionContext, columnLabelAndIndexMap);
result = new ShardingSphereResultSet(resultSets, mergedResult, this, transparentStatement, executionContext, columnLabelAndIndexMap);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
Expand Down Expand Up @@ -521,7 +552,7 @@ public ResultSet getResultSet() throws SQLException {
MergedResult mergedResult = mergeQuery(getQueryResults(resultSets));
Map<String, Integer> columnLabelAndIndexMap = null != this.columnLabelAndIndexMap ? this.columnLabelAndIndexMap
: (this.columnLabelAndIndexMap = ShardingSphereResultSetUtil.createColumnLabelAndIndexMap(sqlStatementContext, resultSets.get(0).getMetaData()));
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, executionContext, columnLabelAndIndexMap);
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, transparentStatement, executionContext, columnLabelAndIndexMap);
}
return currentResultSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.rule.identifier.type.ColumnContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RawExecutionRule;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
Expand Down Expand Up @@ -177,7 +179,8 @@ public ResultSet executeQuery(final String sql) throws SQLException {
executionContext = createExecutionContext(queryContext);
List<QueryResult> queryResults = executeQuery0();
MergedResult mergedResult = mergeQuery(queryResults);
result = new ShardingSphereResultSet(getResultSets(), mergedResult, this, executionContext);
result = new ShardingSphereResultSet(getResultSets(), mergedResult, this, isTransparentStatement(queryContext.getSqlStatementContext(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getRuleMetaData()), executionContext);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
Expand Down Expand Up @@ -242,6 +245,31 @@ private DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> createDriver
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResourceMetaData().getStorageTypes());
}

private boolean isTransparentStatement(final SQLStatementContext<?> sqlStatementContext, final ShardingSphereRuleMetaData ruleMetaData) {
Optional<DataNodeContainedRule> dataNodeContainedRule = getDataNodeContainedRuleForShardingRule(ruleMetaData.findRules(DataNodeContainedRule.class));
Collection<ColumnContainedRule> columnContainedRules = ruleMetaData.findRules(ColumnContainedRule.class);
for (String each : sqlStatementContext.getTablesContext().getTableNames()) {
return (!dataNodeContainedRule.isPresent() || !dataNodeContainedRule.get().getAllTables().contains(each)) && !containsInColumnContainedRule(each, columnContainedRules);
}
return true;
}

private Optional<DataNodeContainedRule> getDataNodeContainedRuleForShardingRule(final Collection<DataNodeContainedRule> dataNodeContainedRules) {
for (DataNodeContainedRule each : dataNodeContainedRules) {
if (!(each instanceof MutableDataNodeRule)) {
return Optional.of(each);
}
}
return Optional.empty();
}

private boolean containsInColumnContainedRule(final String tableName, final Collection<ColumnContainedRule> columnContainedRules) {
for (ColumnContainedRule each : columnContainedRules) {
return each.getTables().contains(tableName);
}
return false;
}

@Override
public int executeUpdate(final String sql) throws SQLException {
try {
Expand Down Expand Up @@ -615,7 +643,8 @@ public ResultSet getResultSet() throws SQLException {
return currentResultSet;
}
MergedResult mergedResult = mergeQuery(getQueryResults(resultSets));
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, executionContext);
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, isTransparentStatement(executionContext.getSqlStatementContext(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getRuleMetaData()), executionContext);
}
return currentResultSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -113,16 +111,7 @@ private ShardingSphereResultSet mockShardingSphereResultSet(final ResultSet resu
when(resultSetMetaData.getColumnLabel(1)).thenReturn("col");
when(resultSetMetaData.getColumnCount()).thenReturn(1);
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mock(MergedResult.class), mock(ShardingSphereStatement.class, RETURNS_DEEP_STUBS), createExecutionContext());
}

private ExecutionContext createExecutionContext() {
ExecutionContext result = mock(ExecutionContext.class);
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class);
TablesContext tablesContext = mock(TablesContext.class);
when(tablesContext.getTableNames()).thenReturn(Collections.emptyList());
when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
when(result.getSqlStatementContext()).thenReturn(sqlStatementContext);
return result;
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mock(MergedResult.class), mock(ShardingSphereStatement.class, RETURNS_DEEP_STUBS),
true, mock(ExecutionContext.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,6 @@ private ShardingSphereResultSet mockShardingSphereResultSet(final MergedResult m
ResultSet resultSet = mock(ResultSet.class);
when(resultSetMetaData.getColumnCount()).thenReturn(1);
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mergedResult, mock(Statement.class), mock(ExecutionContext.class));
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mergedResult, mock(Statement.class), true, mock(ExecutionContext.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final class ShardingSphereResultSetTest {
@BeforeEach
public void setUp() throws SQLException {
mergeResultSet = mock(MergedResult.class);
shardingSphereResultSet = new ShardingSphereResultSet(getResultSets(), mergeResultSet, getShardingSphereStatement(), createExecutionContext());
shardingSphereResultSet = new ShardingSphereResultSet(getResultSets(), mergeResultSet, getShardingSphereStatement(), true, createExecutionContext());
}

private ExecutionContext createExecutionContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class UnsupportedOperationResultSetTest {
@BeforeEach
public void init() throws SQLException {
shardingSphereResultSet = new ShardingSphereResultSet(
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), mock(ExecutionContext.class));
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(ExecutionContext.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class UnsupportedUpdateOperationResultSetTest {
@BeforeEach
public void init() throws SQLException {
shardingSphereResultSet = new ShardingSphereResultSet(
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), mock(ExecutionContext.class));
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(ExecutionContext.class));
}

@Test
Expand Down
Loading

0 comments on commit aa697cc

Please sign in to comment.