io.github.dbstarll.utils.lang.line.BatchLineOperateExecutor Maven / Gradle / Ivy
The newest version!
package io.github.dbstarll.utils.lang.line;
import java.util.Arrays;
/**
* 行批处理器执行接口.
*
* @param 行处理返回的类型
* @author dbstar
*/
public class BatchLineOperateExecutor> extends AbstractLineOperateExecutor {
private final BatchLineOperator operator;
private final int batch;
protected BatchLineOperateExecutor(BatchLineOperator operator, int batch) {
this.operator = operator;
this.batch = batch;
}
public static > BatchLineOperateExecutor build(BatchLineOperator operator, int batch) {
return new BatchLineOperateExecutor(operator, batch);
}
@Override
protected final int operate(Iterable lines, long startTime) {
int count = 0;
final String[] ls = new String[batch];
for (String line : lines) {
ls[count++ % batch] = line;
if (count % batch == 0) {
operate(ls);
}
if (count % 10000 == 0) {
report(count, startTime, startTime = System.currentTimeMillis(), true);
}
}
if (count % batch > 0) {
operate(Arrays.copyOf(ls, count % batch));
}
return count;
}
protected void operate(String... lines) {
try {
for (E result : operator.operate(lines)) {
countResult(result);
}
} catch (Throwable ex) {
logger.warn("operate failed for lines: " + Arrays.toString(lines), ex);
}
}
}