com.twitter.elephantbird.pig.util.AbstractLazyTuple Maven / Gradle / Ivy
package com.twitter.elephantbird.pig.util;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
@SuppressWarnings("serial")
/**
* This is base class for Tuple implementations that delay parsing until
* individual fields are requested.
*/
public abstract class AbstractLazyTuple implements Tuple {
private static TupleFactory tf = TupleFactory.getInstance();
protected Tuple realTuple;
protected boolean isRef; // i.e. reference() is invoked.
protected BitSet idxBits;
protected void initRealTuple(int tupleSize) {
realTuple = tf.newTuple(tupleSize);
idxBits = new BitSet(tupleSize);
isRef = false;
}
/**
* Returns object for the given index. This is invoked only
* once for each instance.
*/
protected abstract Object getObjectAt(int index);
@Override
public void append(Object obj) {
realTuple.append(obj);
}
@Override
public Object get(int idx) throws ExecException {
if (!isRef && !idxBits.get(idx)) {
realTuple.set(idx, getObjectAt(idx));
idxBits.set(idx);
}
return realTuple.get(idx);
}
@Override
public List