批量处理工具BatchUtil

主要为了处理大量查询数据造成的慢sql

package **;

import java.util.List;

/**
 * 批量处理工具
 */
public class BatchUtil{
    public final static int BATCH_GET_ID_SIZE = 500;
    public final static int BATCH_SAVE_SIZE = 1000;
    public final static int BATCH_QUERY_SIZE = 5000;

    public interface OperateCallback<T>{
        void operate(List<T> list) throws Exception;
    }

    public interface OperateCallbackWithProgress<T>{
        /**
         * 操作方法
         *
         * @param list     要处理的list
         * @param progress 进度
         * @throws Exception
         */
        void operate(List<T> list, int progress) throws Exception;
    }

    public interface QueryCallBack{
        void callBack(Integer start, Integer size) throws Exception;
    }

    public static void batchQuery(int count, QueryCallBack queryCallBack) throws Exception{
        for(int i = 0; (count > 0 && i <= count / BATCH_QUERY_SIZE && count % BATCH_QUERY_SIZE != 0)
                || (count > 0 && i < count / BATCH_QUERY_SIZE && count % BATCH_QUERY_SIZE == 0); i++){
            queryCallBack.callBack(i * BATCH_QUERY_SIZE, BATCH_QUERY_SIZE);
        }
    }

    public static void batchQuery(int count, int batchSize, QueryCallBack queryCallBack) throws Exception{
        for(int i = 0; (count > 0 && i <= count / batchSize && count % batchSize != 0)
                || (count > 0 && i < count / batchSize && count % batchSize == 0); i++){
            queryCallBack.callBack(i * batchSize, batchSize);
        }
    }

    public static <T> void batchOperate(List<T> list, int batchSize, OperateCallback<T> callBack) throws Exception{
        if(null == list || list.isEmpty()){
            return;
        }
        int cardCount = list.size();
        if(cardCount > batchSize){
            for(int i = 0;
                (i <= cardCount / batchSize && cardCount % batchSize != 0) || (i < cardCount / batchSize && cardCount % batchSize == 0);
                i++){
                List<T> longs;
                boolean last = (cardCount % batchSize == 0 && cardCount / batchSize == (i + 1))
                        || (cardCount % batchSize != 0 && cardCount / batchSize == i);
                // 最后一次循环
                if(last){
                    longs = list.subList(i * batchSize, cardCount);
                }
                else{
                    longs = list.subList(i * batchSize, (i + 1) * batchSize);
                }
                callBack.operate(longs);
            }
            return;
        }
        callBack.operate(list);
    }

    public static <T> void batchOperate(List<T> list, int batchSize, OperateCallbackWithProgress<T> callBack) throws Exception{
        if(null == list || list.isEmpty()){
            return;
        }
        int cardCount = list.size();
        if(cardCount > batchSize){
            for(int i = 0; (i <= cardCount / batchSize && cardCount % batchSize != 0) || (i < cardCount / batchSize && cardCount % batchSize == 0); i++){
                List<T> longs;
                boolean last = (cardCount % batchSize == 0 && cardCount / batchSize == (i + 1))
                        || (cardCount % batchSize != 0 && cardCount / batchSize == i);
                // 最后一次循环
                if(last){
                    longs = list.subList(i * batchSize, cardCount);
                    callBack.operate(longs, 100);
                }
                else{
                    longs = list.subList(i * batchSize, (i + 1) * batchSize);
                    callBack.operate(longs, (i + 1) * batchSize * 100 / cardCount);
                }
            }
            return;
        }
        callBack.operate(list, 100);
    }
}

 

转载于:https://my.oschina.net/aulbrother/blog/3030281

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值