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

com.github.dennisit.vplus.data.criteria.action.HandleAction Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.criteria.action;

import com.github.dennisit.vplus.data.criteria.DataCriteria;
import com.github.dennisit.vplus.data.criteria.HandleCriteria;
import com.github.dennisit.vplus.data.utils.NumberUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by Elon.su on 17/11/14.
 */
public final class HandleAction {

    private static final Logger LOG = LoggerFactory.getLogger(HandleAction.class);

    /**
     * 每次批量处理的数据一次最大500个
     */
    public static final Integer DEFAULT_BATCH_SIZE = 500;


    /**
     * 私有化构造
     */
    private HandleAction(){

    }


    /**
     * 执行全量索引
     * @param dataCriteria 数据加载行为
     * @param handleCriteria 索引数据行为
     * @return 总共处理条数
     */
    public static long fully(DataCriteria dataCriteria, HandleCriteria handleCriteria) {
        return fully(DEFAULT_BATCH_SIZE, dataCriteria, handleCriteria);
    }


    /**
     * 执行全量索引
     * @param size 每个批次索引的数据大小
     * @param dataCriteria 数据加载行为
     * @param handleCriteria 索引数据行为
     * @return 总共处理条数
     */
    public static long fully(int size, DataCriteria dataCriteria, HandleCriteria handleCriteria) {
        // 游标页从第一页开始
        int current = 1;
        // 约束批量处理的大小
        size = NumberUtils.restrainNum(size, 0, DEFAULT_BATCH_SIZE);
        while(true){
            int cursor = cursor(current, size, dataCriteria, handleCriteria);
            if (cursor == current) {
                LOG.info("[全量执行] 执行完成, {}*{}数据", size, cursor);
                break;
            }
            current = cursor;
        }
        return current*size;
    }

    /**
     * 游标扫描处理
     * @param page 游标页
     * @param size 每个批次索引的数据大小
     * @param dataCriteria 数据加载行为
     * @param handleCriteria 索引数据行为
     * @return
     */
    private static int cursor(int page, int size, DataCriteria dataCriteria, HandleCriteria handleCriteria){
        Assert.notNull(dataCriteria, "数据操作不能为空");
        Assert.notNull(handleCriteria, "操作操作不能为空");
        List list = dataCriteria.loader(page, size);
        if(CollectionUtils.isNotEmpty(list)){
            handleCriteria.handler(list);
            ++ page;
        }
        return page;
    }


    /**
     * 指定数据索引
     * @param list 待索引数据
     * @param handleCriteria 索引行为
     */
    public static void point(List list, HandleCriteria handleCriteria){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        Assert.notNull(handleCriteria, "操作数据不能为空");
        handleCriteria.handler(list);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy