com.github.dennisit.vplus.data.dsource.shard.strategy.SimpleModStrategy Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.dsource.shard.strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SimpleModStrategy implements ShardStrategy {
public SimpleModStrategy() {
}
public String getShardType() {
return "mod";
}
public DataTableName handle(String tableName, String dataSourceKey, int tableShardNum, int dbShardNum, Object shardValue) {
log.debug("进入取余算法:handle({},{},{},{},{})", new Object[]{tableShardNum, dbShardNum, tableName, dataSourceKey, shardValue});
long shard_value = Long.valueOf(shardValue.toString());
long tablePosition = shard_value % (long) tableShardNum;
long dbPosition = tablePosition / (long) (tableShardNum / dbShardNum);
String finalTableName = tableName + "_" + tablePosition;
String finalDataSourceKey = dataSourceKey + dbPosition;
return new DataTableName(finalTableName, finalDataSourceKey);
}
}