Search in sources :

Example 1 with Base64Parser

use of org.apache.hyracks.util.bytes.Base64Parser in project asterixdb by apache.

the class ParseBinaryDescriptor 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) {

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ABinary> binarySerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABINARY);

                private AMutableBinary aBinary = new AMutableBinary(new byte[0], 0, 0);

                private final UTF8StringPointable stringPointable = new UTF8StringPointable();

                private final UTF8StringPointable formatPointable = new UTF8StringPointable();

                private final HexParser hexParser = new HexParser();

                private final Base64Parser base64Parser = new Base64Parser();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evaluators[0].evaluate(tuple, pointables[0]);
                    evaluators[1].evaluate(tuple, pointables[1]);
                    ATypeTag binaryTag = ATypeTag.VALUE_TYPE_MAPPING[pointables[0].getByteArray()[pointables[0].getStartOffset()]];
                    ATypeTag formatTag = ATypeTag.VALUE_TYPE_MAPPING[pointables[1].getByteArray()[pointables[1].getStartOffset()]];
                    checkTypeMachingThrowsIfNot(getIdentifier().getName(), EXPECTED_INPUT_TAGS, binaryTag, formatTag);
                    stringPointable.set(pointables[0].getByteArray(), pointables[0].getStartOffset() + 1, pointables[0].getLength());
                    formatPointable.set(pointables[1].getByteArray(), pointables[1].getStartOffset() + 1, pointables[1].getLength());
                    if (HEX_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                        hexParser.generateByteArrayFromHexString(stringPointable.getByteArray(), stringPointable.getCharStartOffset(), stringPointable.getUTF8Length());
                        aBinary.setValue(hexParser.getByteArray(), 0, hexParser.getLength());
                    } else if (BASE64_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                        base64Parser.generatePureByteArrayFromBase64String(stringPointable.getByteArray(), stringPointable.getCharStartOffset(), stringPointable.getUTF8Length());
                        aBinary.setValue(base64Parser.getByteArray(), 0, base64Parser.getLength());
                    } else {
                        throw new UnsupportedItemTypeException(getIdentifier(), formatTag.serialize());
                    }
                    binarySerde.serialize(aBinary, dataOutput);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : UnsupportedItemTypeException(org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException) ATypeTag(org.apache.asterix.om.types.ATypeTag) AMutableBinary(org.apache.asterix.om.base.AMutableBinary) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) HexParser(org.apache.hyracks.util.bytes.HexParser) Base64Parser(org.apache.hyracks.util.bytes.Base64Parser) IPointable(org.apache.hyracks.data.std.api.IPointable) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Aggregations

AMutableBinary (org.apache.asterix.om.base.AMutableBinary)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 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)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 Base64Parser (org.apache.hyracks.util.bytes.Base64Parser)1 HexParser (org.apache.hyracks.util.bytes.HexParser)1