
pers.zr.opensource.magic.dao.shard.DefaultTableShardHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magic-dao Show documentation
Show all versions of magic-dao Show documentation
simplify to access database with java.
package pers.zr.opensource.magic.dao.shard;
import pers.zr.opensource.magic.dao.utils.HashSlotUtil;
/**
* Created by zhurong on 2016-5-31.
*/
public class DefaultTableShardHandler implements TableShardHandler {
@Override
public String getRealTableName(TableShardStrategy shardStrategy, Object... columnValues) {
if(shardStrategy == null) {
throw new RuntimeException("Failed to get actual table name, caused by tableShardStrategy is null!");
}
if(columnValues == null || columnValues.length < 1) {
throw new RuntimeException("Failed to get actual table name, caused by columnValue is empty or null!");
}
StringBuilder sb = new StringBuilder();
for(Object o : columnValues) {
sb.append(String.valueOf(o));
}
int tableIndex = HashSlotUtil.getSlot(sb.toString(), shardStrategy.getShardCount());
return shardStrategy.getShardTable() + shardStrategy.getSeparator() + ((tableIndex < 10) ? "0" + tableIndex : String.valueOf(tableIndex));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy