GroupByRow.java 4.9 KB
Newer Older
G
gaohongtao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * 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.
 * </p>
 */

package com.dangdang.ddframe.rdb.sharding.merger.row;

import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.merger.aggregation.AggregationUnit;
import com.dangdang.ddframe.rdb.sharding.merger.aggregation.AggregationUnitFactory;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.IndexColumn;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
G
gaoht 已提交
28 29 30 31 32 33 34 35 36
import lombok.extern.slf4j.Slf4j;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
G
gaohongtao 已提交
37 38 39 40

/**
 * @author gaohongtao
 */
G
gaoht 已提交
41
@Slf4j
G
gaohongtao 已提交
42 43 44 45
public class GroupByRow extends Row {
    
    private final ResultSet resultSet;
    
G
gaoht 已提交
46
    private final List<GroupByColumn> groupByColumns;
G
gaohongtao 已提交
47
    
G
gaoht 已提交
48 49 50
    private final List<AggregationColumn> aggregationColumns;
    
    public GroupByRow(final ResultSet resultSet, final List<GroupByColumn> groupByColumns, final List<AggregationColumn> aggregationColumns) throws SQLException {
G
gaohongtao 已提交
51 52
        super(resultSet);
        this.resultSet = resultSet;
G
gaoht 已提交
53 54
        this.groupByColumns = groupByColumns;
        this.aggregationColumns = aggregationColumns;
G
gaohongtao 已提交
55 56
    }
    
G
gaoht 已提交
57
    public boolean aggregate() throws SQLException {
G
gaohongtao 已提交
58 59 60 61 62
        Map<AggregationColumn, AggregationUnit> aggregationUnitMap = null;
        if (!aggregationColumns.isEmpty()) {
            aggregationUnitMap = new HashMap<>(aggregationColumns.size());
        }
        List<Object> groupByKey = getGroupByKey(groupByColumns);
G
gaoht 已提交
63 64 65 66 67
        log.trace("Group {} start", groupByKey);
        boolean hasNext = false;
        do {
            if (!groupByColumns.isEmpty() && !groupByKey.equals(getGroupByKey(groupByColumns))) {
                log.trace("Group {} finish", groupByKey);
G
gaohongtao 已提交
68 69
                break;
            }
G
gaoht 已提交
70 71
            mergeAggregationColumn(aggregationUnitMap);
        } while (hasNext = resultSet.next());
G
gaohongtao 已提交
72 73 74 75 76 77 78 79 80 81
        if (null == aggregationUnitMap) {
            return hasNext;
        }
        for (AggregationColumn each : aggregationUnitMap.keySet()) {
            setCell(each.getColumnIndex(), aggregationUnitMap.get(each).getResult());
        }
        return hasNext;
    }
    
    private List<Object> getGroupByKey(final List<GroupByColumn> groupByColumns) {
G
gaohongtao 已提交
82 83 84 85 86
        List<Object> result = new ArrayList<>(groupByColumns.size());
        for (GroupByColumn each : groupByColumns) {
            result.add(getValueSafely(each.getColumnIndex()));
        }
        return result;
G
gaohongtao 已提交
87
    }
G
gaoht 已提交
88 89
    
    private void mergeAggregationColumn(final Map<AggregationColumn, AggregationUnit> aggregationUnitMap) {
G
gaohongtao 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        if (null == aggregationUnitMap) {
            return;
        }
        for (AggregationColumn each : aggregationColumns) {
            if (!aggregationUnitMap.containsKey(each)) {
                aggregationUnitMap.put(each, AggregationUnitFactory.create(each.getAggregationType()));
            }
            AggregationUnit unit = aggregationUnitMap.get(each);
            List<AggregationColumn> mergingAggregationColumns;
            if (each.getDerivedColumns().isEmpty()) {
                mergingAggregationColumns = Collections.singletonList(each);
            } else {
                mergingAggregationColumns = Lists.newArrayList(each.getDerivedColumns());
            }
G
gaoht 已提交
104 105 106 107 108 109 110 111
            unit.merge(Lists.transform(mergingAggregationColumns, new Function<IndexColumn, Comparable<?>>() {
                
                @Override
                public Comparable<?> apply(final IndexColumn input) {
                    log.trace("Column Index {} will be merged", input.getColumnIndex());
                    return (Comparable<?>) getValueSafely(input.getColumnIndex());
                }
            }).toArray(new Comparable[mergingAggregationColumns.size()]));
G
gaohongtao 已提交
112 113 114 115 116 117 118 119 120 121
        }
    }
    
    private Object getValueSafely(final int index) {
        try {
            return resultSet.getObject(index);
        } catch (final SQLException ex) {
            throw new ShardingJdbcException(ex);
        }
    }
G
gaoht 已提交
122 123 124 125 126
    
    @Override
    public String toString() {
        return String.format("Group by columns is %s, aggregation column is %s, %s", groupByColumns, aggregationColumns, super.toString());
    }
G
gaohongtao 已提交
127
}