lcn.module.batch.web.guide.support.ColumnRangePartitioner.java Source code

Java tutorial

Introduction

Here is the source code for lcn.module.batch.web.guide.support.ColumnRangePartitioner.java

Source

/*
 * Copyright 2006-2007 the original author or authors. *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lcn.module.batch.web.guide.support;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

/**
 * DB    ??  ??  ??? ?  ? Data ? 
 * ? ?  ?? ? ?? 
 * 
 * @author 
 * @since 2012. 07.30
 * @version 1.0
 * @see
 * 
 *      <pre>
 * << ?(Modification Information) >>
 *   
 *   ?               ?               
 *  -------      --------     ---------------------------
 *  2012. 07.30       ?
 * 
 * </pre>
 */
public class ColumnRangePartitioner implements Partitioner {
    // SimpleJdbcTemplate
    private SimpleJdbcTemplate jdbcTemplate;

    // table
    private String table;

    // column
    private String column;

    /**
     * Data   SQL ? ? 
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * ?   
     */
    public void setColumn(String column) {
        this.column = column;
    }

    /**
     * DB ?  DataSource
     */
    public void setDataSource(DataSource dataSource) {
        jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    /**
     * DB??  ? ? Data? "? " ?  . ExecutionContext ?
     * <code>minValue</code>  <code>maxValue</code> ?  , ?   ???
     * ?  ?  .
     */
    public Map<String, ExecutionContext> partition(int gridSize) {
        // ?? ?  ? 
        int min = jdbcTemplate.queryForInt("SELECT MIN(" + column + ") from " + table);

        //  ?? ?  ? 
        int max = jdbcTemplate.queryForInt("SELECT MAX(" + column + ") from " + table);

        // ? Execution? ? Data? (?)
        int targetSize = (max - min) / gridSize + 1;

        Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();
        int number = 0;
        int start = min;
        // targetSize ? ? Data
        int end = start + targetSize - 1;

        // ? ? ? ExecutionContext ? minVlaue  maxValue  
        while (start <= max) {

            ExecutionContext value = new ExecutionContext();
            result.put("partition" + number, value);

            if (end >= max) {
                end = max;
            }
            value.putInt("minValue", start);
            value.putInt("maxValue", end);
            start += targetSize;
            end += targetSize;
            number++;
        }

        return result;

    }

}