All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yuweix.tripod.dao.sharding.ModStrategy Maven / Gradle / Ivy

package com.yuweix.tripod.dao.sharding;


/**
 * 分片策略-取模
 * @author yuwei
 */
public class ModStrategy implements Strategy {
    @Override
    public String getShardingIndex(String tableName, Object shardingVal) {
        Config conf = TABLE_CONF_MAP.get(tableName);
        if (conf == null) {
            throw new RuntimeException("[" + tableName + "]'s sharding-conf is required.");
        }
        return String.format("%0" + conf.getSuffixLength() + "d", hash(shardingVal) % conf.getShardingSize());
    }

    private int hash(Object str) {
        if (str == null) {
            return 0;
        }
        return Math.abs(str.hashCode());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy