blob: adde6d11c307b51fb91c47ab870c390b53402d58 [file] [log] [blame]
package edu.uci.ics.asterix.runtime.evaluators.constructors;
import java.io.DataOutput;
import java.io.IOException;
import edu.uci.ics.asterix.common.functions.FunctionConstants;
import edu.uci.ics.asterix.formats.nontagged.AqlSerializerDeserializerProvider;
import edu.uci.ics.asterix.om.base.AMutablePoint3D;
import edu.uci.ics.asterix.om.base.ANull;
import edu.uci.ics.asterix.om.base.APoint3D;
import edu.uci.ics.asterix.om.types.ATypeTag;
import edu.uci.ics.asterix.om.types.BuiltinType;
import edu.uci.ics.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import edu.uci.ics.hyracks.algebricks.runtime.base.IEvaluator;
import edu.uci.ics.hyracks.algebricks.runtime.base.IEvaluatorFactory;
import edu.uci.ics.hyracks.api.dataflow.value.ISerializerDeserializer;
import edu.uci.ics.hyracks.dataflow.common.data.accessors.ArrayBackedValueStorage;
import edu.uci.ics.hyracks.dataflow.common.data.accessors.IDataOutputProvider;
import edu.uci.ics.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
public class APoint3DConstructorDescriptor extends AbstractScalarFunctionDynamicDescriptor {
private static final long serialVersionUID = 1L;
public final static FunctionIdentifier FID = new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "point3d", 1,
true);
private final static byte SER_STRING_TYPE_TAG = ATypeTag.STRING.serialize();
private final static byte SER_NULL_TYPE_TAG = ATypeTag.NULL.serialize();
@Override
public IEvaluatorFactory createEvaluatorFactory(final IEvaluatorFactory[] args) {
return new IEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IEvaluator createEvaluator(final IDataOutputProvider output) throws AlgebricksException {
return new IEvaluator() {
private DataOutput out = output.getDataOutput();
private ArrayBackedValueStorage outInput = new ArrayBackedValueStorage();
private IEvaluator eval = args[0].createEvaluator(outInput);
private String errorMessage = "This can not be an instance of point3d";
private AMutablePoint3D aPoint3D = new AMutablePoint3D(0, 0, 0);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<APoint3D> point3DSerde = AqlSerializerDeserializerProvider.INSTANCE
.getSerializerDeserializer(BuiltinType.APOINT3D);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ANull> nullSerde = AqlSerializerDeserializerProvider.INSTANCE
.getSerializerDeserializer(BuiltinType.ANULL);
@Override
public void evaluate(IFrameTupleReference tuple) throws AlgebricksException {
try {
outInput.reset();
eval.evaluate(tuple);
byte[] serString = outInput.getBytes();
if (serString[0] == SER_STRING_TYPE_TAG) {
String s = new String(serString, 3, outInput.getLength() - 3, "UTF-8");
int firstCommaIndex = s.indexOf(',');
int secondCommaIndex = s.indexOf(',', firstCommaIndex + 1);
aPoint3D.setValue(Double.parseDouble(s.substring(0, firstCommaIndex)),
Double.parseDouble(s.substring(firstCommaIndex + 1, secondCommaIndex)),
Double.parseDouble(s.substring(secondCommaIndex + 1, s.length())));
point3DSerde.serialize(aPoint3D, out);
} else if (serString[0] == SER_NULL_TYPE_TAG)
nullSerde.serialize(ANull.NULL, out);
else
throw new AlgebricksException(errorMessage);
} catch (IOException e1) {
throw new AlgebricksException(errorMessage);
}
}
};
}
};
}
@Override
public FunctionIdentifier getIdentifier() {
return FID;
}
}