
io.datarouter.batchsizeoptimizer.tool.DynamicBatchingIterator Maven / Gradle / Ivy
/**
* Copyright © 2009 HotPads ([email protected])
*
* 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 io.datarouter.batchsizeoptimizer.tool;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import io.datarouter.batchsizeoptimizer.BatchSizeOptimizer;
public class DynamicBatchingIterator implements Iterator>{
private BatchSizeOptimizer optimizer;
private Iterator backingIterator;
private String opName;
private Integer currentBatchSize;
private Long lastCallTimestamp;
public DynamicBatchingIterator(Iterator backingIterator, BatchSizeOptimizer optimizer, String opName){
this.backingIterator = backingIterator;
this.optimizer = optimizer;
this.opName = opName;
}
@Override
public boolean hasNext(){
if(currentBatchSize != null && lastCallTimestamp != null){
long timeSpent = System.currentTimeMillis() - lastCallTimestamp;
optimizer.recordBatchSizeAndTime(opName, currentBatchSize, currentBatchSize, timeSpent);
}
return backingIterator.hasNext();
}
@Override
public List next(){
currentBatchSize = optimizer.getRecommendedBatchSize(opName);
List batch = new ArrayList<>(currentBatchSize);
while(batch.size() < currentBatchSize && backingIterator.hasNext()){
batch.add(backingIterator.next());
}
currentBatchSize = batch.size();
lastCallTimestamp = System.currentTimeMillis();
return batch;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy