com.mozafaq.extmergesort.Batch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of external-mergesort Show documentation
Show all versions of external-mergesort Show documentation
External merge sort algorithm
package com.mozafaq.extmergesort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
/**
* @author Mozaffar Afaque
*/
class Batch {
private final int size;
public Batch(int size) {
this.size = size;
}
public List readFullBatch(InputStream inputStream, RecordReader reader) throws IOException {
Objects.requireNonNull(reader);
Objects.requireNonNull(inputStream);
List records = new ArrayList<>(size);
int readRecord = 0;
T record;
while (readRecord < size && (record = reader.readRecord(inputStream)) != null) {
readRecord++;
records.add(record);
}
return records;
}
public static void writeFullBatch(Iterator iterator, OutputStream outputStream, RecordWriter writer) throws IOException {
Objects.requireNonNull(writer);
Objects.requireNonNull(iterator);
Objects.requireNonNull(outputStream);
while (iterator.hasNext()) {
writeSingleRecord(outputStream, writer, iterator.next());
}
}
public static void writeSingleRecord(OutputStream outputStream, RecordWriter writer, T record) throws IOException {
writer.writeRecord(outputStream, record);
}
public ComparisionIterator getStreamedIterator(
InputStream inputStream, RecordReader recordReader)
{
return new ComparisionIterator() {
final InputStream stream = inputStream;
final RecordReader reader = recordReader;
boolean isDone = false;
List readBatch ;
Iterator itr ;
T currentlyReadRecord;
{
currentlyReadRecord = resetIterator();
}
T resetIterator() {
try {
if (isDone) {
return null;
}
readBatch = Batch.this.readFullBatch(inputStream, reader);
itr = readBatch.iterator();
T record = itr.hasNext() ? itr.next() : null;
isDone = readBatch.size() < Batch.this.size;
return record;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override
public T current() {
return currentlyReadRecord;
}
@Override
public boolean hasNext() {
return currentlyReadRecord != null;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T t = currentlyReadRecord;
if(!itr.hasNext()){
currentlyReadRecord = resetIterator();
} else if (itr.hasNext() ){
currentlyReadRecord = itr.next();
}
return t;
}
};
}
}