coderMmw commented on issue #5208:
URL: https://github.com/apache/shardingsphere/issues/5208#issuecomment-616366447
singleKeyHashShardingAlgorithm1 class
`package com.pptv.ucm.dao.sharding;
import
org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import
org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static java.lang.Math.abs;
/**
* 根据单个字段hash来分表的实现
*
*/
//public class SingleKeyHashShardingAlgorithm1 implements
/*SingleKeyTableShardingAlgorithm<String>,*/PreciseShardingAlgorithm,
RangeShardingAlgorithm<Integer> {
public class SingleKeyHashShardingAlgorithm1 implements
PreciseShardingAlgorithm<String>, RangeShardingAlgorithm<Integer> {
@Override
public String doSharding(Collection<String> collection,
PreciseShardingValue<String> preciseShardingValue) {
// 逻辑表名
// 根据比较的值,算出物理分表
String logicTableName =
preciseShardingValue.getLogicTableName();
String actualTableName = logicTableName + "_" +
Math.abs(preciseShardingValue.getValue().hashCode()) % collection.size();
if (collection.contains(actualTableName)) {
return actualTableName;
} else {
// 如果没有匹配到相应的物理表名,那一定是有问题的
throw new UnsupportedOperationException();
}
// @Override
// public String doSharding(Collection collection, PreciseShardingValue
preciseShardingValue) {
// // 逻辑表名
// String logicTableName =
preciseShardingValue.getLogicTableName();
// // 根据比较的值,算出物理分表
//
// String actualTableName = logicTableName + "_" +
Math.abs(preciseShardingValue.getValue().hashCode())% collection.size();
// if (collection.contains(actualTableName)){
// return actualTableName;
// }else{
// // 如果没有匹配到相应的物理表名,那一定是有问题的
// throw new UnsupportedOperationException();
// }
//
//
// // 如果没有匹配到相应的物理表名,那一定是有问题的
//
// }
//
// @Override
// public Collection<String> doSharding(Collection<String>
availableTargetNames, RangeShardingValue<Integer> shardingValue) {
// // in表达式的值对应的数据表
// Set<String> inValueTables = new HashSet<String>();
//
//
// String logicTableName = shardingValue.getLogicTableName();
// for (String value : availableTargetNames) {
// String actualTableName = logicTableName + "_" +
value.hashCode() % availableTargetNames.size();
// if (availableTargetNames.contains(actualTableName)){
// inValueTables.add(actualTableName);
// }
// }
//
// if (inValueTables.size() == 0){
// throw new UnsupportedOperationException();
// }
//
// return inValueTables;
// }
}
public static void main(String[] args) {
String s = null;
int i = Math.abs("00:25:92:EE:6F:EE".hashCode()) % 10;
System.out.println(i);
}
@Override
public Collection<String> doSharding(Collection<String>
availableTargetNames, RangeShardingValue<Integer> shardingValue) {
// in表达式的值对应的数据表
Set<String> inValueTables = new HashSet<String>();
String logicTableName = shardingValue.getLogicTableName();
for (String value : availableTargetNames) {
String actualTableName = logicTableName + "_" +
value.hashCode() % availableTargetNames.size();
if (availableTargetNames.contains(actualTableName)){
inValueTables.add(actualTableName);
}
}
if (inValueTables.size() == 0){
throw new UnsupportedOperationException();
}
return inValueTables;
}
}
`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]