Skip to content

Commit

Permalink
for #701: add ShardingConditions to process sharding values
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 10, 2018
1 parent 42e4f3c commit 9e2b1fa
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder;
import io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder;
import io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.type.TableUnit;
import io.shardingjdbc.core.routing.type.complex.CartesianTableReference;
import io.shardingjdbc.core.rule.BindingTableRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.shardingjdbc.core.routing;

import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingjdbc.core.routing;
package io.shardingjdbc.core.routing.condition;

import io.shardingjdbc.core.parsing.parser.context.condition.GeneratedKeyCondition;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingjdbc.core.routing.condition;

import com.google.common.base.Optional;
import io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.ShardingValue;
import io.shardingjdbc.core.parsing.parser.context.condition.Column;
import io.shardingjdbc.core.parsing.parser.context.condition.Condition;
import io.shardingjdbc.core.parsing.parser.context.condition.Conditions;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* Sharding conditions.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class ShardingConditions {

private final Conditions conditions;

private final GeneratedKey generatedKey;

private final List<Object> parameters;

/**
* Get sharding values.
*
* @param logicTableName logic table name
* @param shardingColumns sharding columns
* @return sharding values
*/
public List<ShardingValue> getShardingValues(final String logicTableName, final Collection<String> shardingColumns) {
List<ShardingValue> result = new ArrayList<>(shardingColumns.size());
for (String each : shardingColumns) {
Optional<Condition> condition = conditions.find(new Column(each, logicTableName));
if (condition.isPresent()) {
result.add(condition.get().getShardingValue(parameters));
} else if (null != generatedKey && each.equals(generatedKey.getColumn())) {
Comparable key = null == generatedKey.getValue() ? (Comparable) parameters.get(generatedKey.getIndex()) : (Comparable) generatedKey.getValue();
result.add(new ListShardingValue<>(logicTableName, each, Collections.singletonList(key)));
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken;
import io.shardingjdbc.core.rewrite.SQLBuilder;
import io.shardingjdbc.core.rewrite.SQLRewriteEngine;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.SQLExecutionUnit;
import io.shardingjdbc.core.routing.SQLRouteResult;
import io.shardingjdbc.core.routing.type.RoutingEngine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.ShardingValue;
import io.shardingjdbc.core.hint.HintManagerHolder;
import io.shardingjdbc.core.hint.ShardingKey;
import io.shardingjdbc.core.parsing.parser.context.condition.Column;
import io.shardingjdbc.core.parsing.parser.context.condition.Condition;
import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.condition.ShardingConditions;
import io.shardingjdbc.core.routing.strategy.ShardingStrategy;
import io.shardingjdbc.core.routing.type.RoutingEngine;
import io.shardingjdbc.core.routing.type.RoutingResult;
Expand All @@ -38,7 +36,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -52,13 +49,15 @@ public final class StandardRoutingEngine implements RoutingEngine {

private final ShardingRule shardingRule;

private final List<Object> parameters;

private final String logicTableName;

private final SQLStatement sqlStatement;
private final ShardingConditions shardingConditions;

private final GeneratedKey generatedKey;
public StandardRoutingEngine(final ShardingRule shardingRule, final List<Object> parameters, final String logicTableName, final SQLStatement sqlStatement, final GeneratedKey generatedKey) {
this.shardingRule = shardingRule;
this.logicTableName = logicTableName;
shardingConditions = new ShardingConditions(sqlStatement.getConditions(), generatedKey, parameters);
}

@Override
public RoutingResult route() {
Expand All @@ -75,12 +74,14 @@ public RoutingResult route() {

private List<ShardingValue> getDatabaseShardingValues(final TableRule tableRule) {
ShardingStrategy strategy = shardingRule.getDatabaseShardingStrategy(tableRule);
return HintManagerHolder.isUseShardingHint() ? getDatabaseShardingValuesFromHint(strategy.getShardingColumns()) : getShardingValues(strategy.getShardingColumns());
return HintManagerHolder.isUseShardingHint()
? getDatabaseShardingValuesFromHint(strategy.getShardingColumns()) : shardingConditions.getShardingValues(logicTableName, strategy.getShardingColumns());
}

private List<ShardingValue> getTableShardingValues(final TableRule tableRule) {
ShardingStrategy strategy = shardingRule.getTableShardingStrategy(tableRule);
return HintManagerHolder.isUseShardingHint() ? getTableShardingValuesFromHint(strategy.getShardingColumns()) : getShardingValues(strategy.getShardingColumns());
return HintManagerHolder.isUseShardingHint()
? getTableShardingValuesFromHint(strategy.getShardingColumns()) : shardingConditions.getShardingValues(logicTableName, strategy.getShardingColumns());
}

private List<ShardingValue> getDatabaseShardingValuesFromHint(final Collection<String> shardingColumns) {
Expand All @@ -105,20 +106,6 @@ private List<ShardingValue> getTableShardingValuesFromHint(final Collection<Stri
return result;
}

private List<ShardingValue> getShardingValues(final Collection<String> shardingColumns) {
List<ShardingValue> result = new ArrayList<>(shardingColumns.size());
for (String each : shardingColumns) {
Optional<Condition> condition = sqlStatement.getConditions().find(new Column(each, logicTableName));
if (condition.isPresent()) {
result.add(condition.get().getShardingValue(parameters));
} else if (null != generatedKey && each.equals(generatedKey.getColumn())) {
Comparable key = null == generatedKey.getValue() ? (Comparable) parameters.get(generatedKey.getIndex()) : (Comparable) generatedKey.getValue();
result.add(new ListShardingValue<>(sqlStatement.getTables().getSingleTableName(), each, Collections.singletonList(key)));
}
}
return result;
}

private Collection<String> routeDataSources(final TableRule tableRule, final List<ShardingValue> databaseShardingValues) {
Collection<String> availableTargetDatabases = tableRule.getActualDatasourceNames();
if (databaseShardingValues.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import io.shardingjdbc.core.parsing.parser.token.RowCountToken;
import io.shardingjdbc.core.parsing.parser.token.SchemaToken;
import io.shardingjdbc.core.parsing.parser.token.TableToken;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.type.TableUnit;
import io.shardingjdbc.core.routing.type.complex.CartesianTableReference;
import io.shardingjdbc.core.rule.ShardingRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement;
import io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement;
import io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.PreparedStatementRoutingEngine;
import io.shardingjdbc.core.routing.SQLExecutionUnit;
import io.shardingjdbc.core.routing.SQLRouteResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement;
import io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement;
import io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingjdbc.core.routing.GeneratedKey;
import io.shardingjdbc.core.routing.condition.GeneratedKey;
import io.shardingjdbc.core.routing.SQLExecutionUnit;
import io.shardingjdbc.core.routing.SQLRouteResult;
import io.shardingjdbc.core.routing.StatementRoutingEngine;
Expand Down

0 comments on commit 9e2b1fa

Please sign in to comment.