package com.zm.order.tool.server.utils;

import com.zm.order.tool.server.constants.CommonConstants;

/**
 * @author nianchang
 * @date 2022/04/27 11:19 上午
 */
public class RoutingUtils {

    public static String pack(String key, int index) {
        return key + CommonConstants.SHARDING_SPLIT + index;
    }

    public static int routeIndex(long sellerId, int dbSize) {
        if (dbSize < 0) {
            throw new IllegalArgumentException("routing index size can't lt 0");
        }
        if (dbSize == 0) {
            return 0;
        }
        int hashCode = hashcode(sellerId);
        return hashCode & (dbSize - 1);
    }

    private static int hashcode(long sellerId) {
        int hashCode = String.valueOf(sellerId).hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return hashCode;
    }
}
