Skip to content

Commit

Permalink
Refactor ReadwriteSplittingSQLRouter to remove redundant judgment (#3…
Browse files Browse the repository at this point in the history
…1968)

* Refactor ReadwriteSplittingSQLRouter to remove redundant judgment

* Refactor logic
  • Loading branch information
zhaojinchao95 authored Jul 3, 2024
1 parent ce61540 commit a022bc9
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingOrder;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;

/**
* Readwrite-splitting SQL router.
Expand All @@ -46,14 +44,12 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon
Collection<RouteUnit> toBeRemoved = new LinkedList<>();
Collection<RouteUnit> toBeAdded = new LinkedList<>();
for (RouteUnit each : routeContext.getRouteUnits()) {
String dataSourceName = each.getDataSourceMapper().getLogicName();
Optional<ReadwriteSplittingDataSourceGroupRule> dataSourceGroupRule = rule.findDataSourceGroupRule(dataSourceName);
if (dataSourceGroupRule.isPresent() && dataSourceGroupRule.get().getName().equalsIgnoreCase(each.getDataSourceMapper().getActualName())) {
String logicDataSourceName = each.getDataSourceMapper().getActualName();
rule.findDataSourceGroupRule(logicDataSourceName).ifPresent(optional -> {
toBeRemoved.add(each);
String actualDataSourceName = new ReadwriteSplittingDataSourceRouter(dataSourceGroupRule.get(), connectionContext).route(queryContext.getSqlStatementContext(),
queryContext.getHintValueContext());
toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), actualDataSourceName), each.getTableMappers()));
}
String actualDataSourceName = new ReadwriteSplittingDataSourceRouter(optional, connectionContext).route(queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
toBeAdded.add(new RouteUnit(new RouteMapper(logicDataSourceName, actualDataSourceName), each.getTableMappers()));
});
}
routeContext.getRouteUnits().removeAll(toBeRemoved);
routeContext.getRouteUnits().addAll(toBeAdded);
Expand Down

0 comments on commit a022bc9

Please sign in to comment.