Search in sources :

Example 6 with ByteArrayPointable

use of org.apache.hyracks.data.std.primitive.ByteArrayPointable in project asterixdb by apache.

the class PrintBinaryDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new AbstractBinaryScalarEvaluator(ctx, args) {

                private StringBuilder stringBuilder = new StringBuilder();

                private final ByteArrayPointable byteArrayPtr = new ByteArrayPointable();

                private final UTF8StringPointable formatPointable = new UTF8StringPointable();

                private final UTF8StringWriter writer = new UTF8StringWriter();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evaluators[0].evaluate(tuple, pointables[0]);
                    evaluators[1].evaluate(tuple, pointables[1]);
                    try {
                        ATypeTag arg0Tag = ATypeTag.VALUE_TYPE_MAPPING[pointables[0].getByteArray()[pointables[0].getStartOffset()]];
                        ATypeTag arg1Tag = ATypeTag.VALUE_TYPE_MAPPING[pointables[1].getByteArray()[pointables[1].getStartOffset()]];
                        checkTypeMachingThrowsIfNot(getIdentifier().getName(), EXPECTED_INPUT_TAGS, arg0Tag, arg1Tag);
                        byteArrayPtr.set(pointables[0].getByteArray(), pointables[0].getStartOffset() + 1, pointables[0].getLength());
                        formatPointable.set(pointables[1].getByteArray(), pointables[1].getStartOffset() + 1, pointables[1].getLength());
                        int lengthBinary = byteArrayPtr.getContentLength();
                        stringBuilder.setLength(0);
                        if (HEX_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                            HexPrinter.printHexString(byteArrayPtr.getByteArray(), byteArrayPtr.getContentStartOffset(), lengthBinary, stringBuilder);
                        } else if (BASE64_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                            Base64Printer.printBase64Binary(byteArrayPtr.getByteArray(), byteArrayPtr.getContentStartOffset(), lengthBinary, stringBuilder);
                        } else {
                            throw new UnsupportedItemTypeException(getIdentifier(), arg1Tag.serialize());
                        }
                        dataOutput.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        writer.writeUTF8(stringBuilder.toString(), dataOutput);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) UnsupportedItemTypeException(org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException) ByteArrayPointable(org.apache.hyracks.data.std.primitive.ByteArrayPointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Aggregations

ByteArrayPointable (org.apache.hyracks.data.std.primitive.ByteArrayPointable)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ATypeTag (org.apache.asterix.om.types.ATypeTag)1 UnsupportedItemTypeException (org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException)1 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)1 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1 IPointable (org.apache.hyracks.data.std.api.IPointable)1 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)1 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)1 UTF8StringWriter (org.apache.hyracks.util.string.UTF8StringWriter)1