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

com.github.javaclub.cdl.client.matrix.jdbc.AbstractShardingResultSet Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version

package com.github.javaclub.cdl.client.matrix.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.github.javaclub.cdl.client.matrix.jdbc.adapter.AbstractResultSetAdapter;
import com.github.javaclub.cdl.client.matrix.parser.result.merger.Limit;

import lombok.extern.slf4j.Slf4j;

/**
 * 支持分片的结果集抽象类.
 * 
 */
@Slf4j
public abstract class AbstractShardingResultSet extends AbstractResultSetAdapter {
    
    private final Limit limit;
    
    private boolean offsetSkipped;
    
    private int readCount;
    
    protected AbstractShardingResultSet(final List resultSets, final Limit limit) {
        super(resultSets);
        this.limit = limit;
        setCurrentResultSet(resultSets.get(0));
    }
    
    @Override
    public final boolean next() throws SQLException {
        if (null != limit && !offsetSkipped) {
            skipOffset();
        }
        return null == limit ? nextForSharding() : ++readCount <= limit.getRowCount() && nextForSharding();
    }
    
    private void skipOffset() {
        for (int i = 0; i < limit.getOffset(); i++) {
            try {
                if (!nextForSharding()) {
                    break;
                }
            } catch (final SQLException ignored) {
                log.warn("Skip result set error", ignored);
            }
        }
        offsetSkipped = true;
    }
    
    /**
     * 迭代结果集.
     * 
     * @return true 可以继续访问 false 不能继续访问
     * @throws SQLException
     */
    protected abstract boolean nextForSharding() throws SQLException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy