| 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.AMutablePoint; |
| import edu.uci.ics.asterix.om.base.AMutableRectangle; |
| import edu.uci.ics.asterix.om.base.ANull; |
| import edu.uci.ics.asterix.om.base.ARectangle; |
| 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.core.algebra.functions.FunctionIdentifier; |
| import edu.uci.ics.hyracks.algebricks.core.api.exceptions.AlgebricksException; |
| 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 ARectangleConstructorDescriptor extends AbstractScalarFunctionDynamicDescriptor { |
| |
| private static final long serialVersionUID = 1L; |
| public final static FunctionIdentifier FID = new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "rectangle", 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 rectangle"; |
| private AMutableRectangle aRectangle = new AMutableRectangle(null, null); |
| private AMutablePoint[] aPoint = { new AMutablePoint(0, 0), new AMutablePoint(0, 0) }; |
| @SuppressWarnings("unchecked") |
| private ISerializerDeserializer<ARectangle> rectangle2DSerde = AqlSerializerDeserializerProvider.INSTANCE |
| .getSerializerDeserializer(BuiltinType.ARECTANGLE); |
| @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 commaIndex = s.indexOf(','); |
| int spaceIndex = s.indexOf(' ', commaIndex + 1); |
| aPoint[0].setValue(Double.parseDouble(s.substring(0, commaIndex)), |
| Double.parseDouble(s.substring(commaIndex + 1, spaceIndex))); |
| commaIndex = s.indexOf(',', spaceIndex + 1); |
| aPoint[1].setValue(Double.parseDouble(s.substring(spaceIndex + 1, commaIndex)), |
| Double.parseDouble(s.substring(commaIndex + 1, s.length()))); |
| if (aPoint[0].getX() > aPoint[1].getX() || aPoint[0].getY() > aPoint[1].getY()) { |
| throw new IllegalArgumentException( |
| "The low point in the rectangle cannot be larger than the high point"); |
| } |
| aRectangle.setValue(aPoint[0], aPoint[1]); |
| rectangle2DSerde.serialize(aRectangle, 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; |
| } |
| |
| } |