Search in sources :

Example 81 with ArrayBackedValueStorage

use of org.apache.hyracks.data.std.util.ArrayBackedValueStorage in project asterixdb by apache.

the class ALineConstructorDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

                private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);

                private AMutableLine aLine = new AMutableLine(null, null);

                private AMutablePoint[] aPoint = { new AMutablePoint(0, 0), new AMutablePoint(0, 0) };

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ALine> lineSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ALINE);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int offset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, offset + 1, len - 1);
                            String s = utf8Ptr.toString();
                            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())));
                            aLine.setValue(aPoint[0], aPoint[1]);
                            lineSerde.serialize(aLine, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) AMutablePoint(org.apache.asterix.om.base.AMutablePoint) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) AMutableLine(org.apache.asterix.om.base.AMutableLine) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutablePoint(org.apache.asterix.om.base.AMutablePoint)

Example 82 with ArrayBackedValueStorage

use of org.apache.hyracks.data.std.util.ArrayBackedValueStorage in project asterixdb by apache.

the class ABooleanConstructorDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

                private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);

                private final byte[] TRUE = UTF8StringUtil.writeStringToBytes("true");

                private final byte[] FALSE = UTF8StringUtil.writeStringToBytes("false");

                IBinaryComparator utf8BinaryComparator = BinaryComparatorFactoryProvider.UTF8STRING_POINTABLE_INSTANCE.createBinaryComparator();

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int startOffset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            if (utf8BinaryComparator.compare(serString, startOffset + 1, len - 1, TRUE, 0, TRUE.length) == 0) {
                                booleanSerde.serialize(ABoolean.TRUE, out);
                                result.set(resultStorage);
                                return;
                            } else if (utf8BinaryComparator.compare(serString, startOffset + 1, len - 1, FALSE, 0, FALSE.length) == 0) {
                                booleanSerde.serialize(ABoolean.FALSE, out);
                                result.set(resultStorage);
                                return;
                            } else {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG);
                            }
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[startOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IBinaryComparator(org.apache.hyracks.api.dataflow.value.IBinaryComparator) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 83 with ArrayBackedValueStorage

use of org.apache.hyracks.data.std.util.ArrayBackedValueStorage in project asterixdb by apache.

the class ClassAdParser method parseRecord.

private void parseRecord(ARecordType recType, ClassAd pAd, DataOutput out) throws IOException, AsterixException {
    ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
    ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
    IARecordBuilder recBuilder = getRecordBuilder();
    BitSet nulls = null;
    if (recType != null) {
        nulls = getBitSet();
        recBuilder.reset(recType);
    } else {
        recBuilder.reset(null);
    }
    recBuilder.init();
    Boolean openRecordField = false;
    int fieldId = 0;
    IAType fieldType = null;
    // new stuff
    Map<CaseInsensitiveString, ExprTree> attrs = pAd.getAttrList();
    for (Entry<CaseInsensitiveString, ExprTree> entry : attrs.entrySet()) {
        // reset buffers
        fieldNameBuffer.reset();
        fieldValueBuffer.reset();
        // take care of field name
        String fldName = entry.getKey().get();
        if (recType != null) {
            fieldId = recBuilder.getFieldId(fldName);
            if (fieldId < 0 && !recType.isOpen()) {
                throw new HyracksDataException("This record is closed, you can not add extra fields !!");
            } else if (fieldId < 0 && recType.isOpen()) {
                aStringFieldName.setValue(fldName);
                stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
                openRecordField = true;
                fieldType = null;
            } else {
                // a closed field
                nulls.set(fieldId);
                fieldType = recType.getFieldTypes()[fieldId];
                openRecordField = false;
            }
        } else {
            aStringFieldName.setValue(fldName);
            stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
            openRecordField = true;
            fieldType = null;
        }
        // add field value to value buffer
        writeFieldValueToBuffer(fieldType, fieldValueBuffer.getDataOutput(), fldName, entry.getValue(), pAd);
        if (openRecordField) {
            if (fieldValueBuffer.getByteArray()[0] != ATypeTag.MISSING.serialize()) {
                recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
            }
        } else if (NonTaggedFormatUtil.isOptional(fieldType)) {
            if (fieldValueBuffer.getByteArray()[0] != ATypeTag.MISSING.serialize()) {
                recBuilder.addField(fieldId, fieldValueBuffer);
            }
        } else {
            recBuilder.addField(fieldId, fieldValueBuffer);
        }
    }
    if (recType != null) {
        int optionalFieldId = checkOptionalConstraints(recType, nulls);
        if (optionalFieldId != -1) {
            throw new HyracksDataException("Field: " + recType.getFieldNames()[optionalFieldId] + " can not be optional");
        }
    }
    recBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) BitSet(java.util.BitSet) ExprTree(org.apache.asterix.external.classad.ExprTree) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ABoolean(org.apache.asterix.om.base.ABoolean) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType)

Example 84 with ArrayBackedValueStorage

use of org.apache.hyracks.data.std.util.ArrayBackedValueStorage in project asterixdb by apache.

the class ARecordSerializerDeserializer method serialize.

// This serialize method will NOT work if <code>recordType</code> is not equal to the type of the instance.
@SuppressWarnings("unchecked")
public void serialize(ARecord instance, DataOutput out, boolean writeTypeTag) throws HyracksDataException {
    IARecordBuilder recordBuilder = new RecordBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    recordBuilder.reset(recordType);
    recordBuilder.init();
    if (recordType != null) {
        int fieldIndex = 0;
        for (; fieldIndex < recordType.getFieldNames().length; ++fieldIndex) {
            fieldValue.reset();
            serializers[fieldIndex].serialize(instance.getValueByPos(fieldIndex), fieldValue.getDataOutput());
            recordBuilder.addField(fieldIndex, fieldValue);
        }
        recordBuilder.write(out, writeTypeTag);
    } else {
        serializeSchemalessRecord(instance, out, writeTypeTag);
    }
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder)

Example 85 with ArrayBackedValueStorage

use of org.apache.hyracks.data.std.util.ArrayBackedValueStorage in project asterixdb by apache.

the class TemporalYearAccessor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable argPtr = new VoidPointable();

                private final IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);

                private final GregorianCalendarSystem calSystem = GregorianCalendarSystem.getInstance();

                private final UTF8StringPointable strExprPtr = new UTF8StringPointable();

                private final UTF8StringCharacterIterator strIter = new UTF8StringCharacterIterator();

                // for output: type integer
                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInt64> intSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                private final AMutableInt64 aMutableInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    int len = argPtr.getLength();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationYear(ADurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationYear(AYearMonthDurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        long chrononTimeInMs = 0;
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            chrononTimeInMs = AInt32SerializerDeserializer.getInt(bytes, startOffset + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            chrononTimeInMs = AInt64SerializerDeserializer.getLong(bytes, startOffset + 1);
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            int year;
                            strExprPtr.set(bytes, startOffset + 1, len - 1);
                            strIter.reset(strExprPtr);
                            char firstChar = strIter.next();
                            if (firstChar == '-') {
                                // in case of a negative year
                                year = -1 * ((strIter.next() - '0') * 1000 + (strIter.next() - '0') * 100 + (strIter.next() - '0') * 10 + (strIter.next() - '0'));
                            } else {
                                year = (firstChar - '0') * 1000 + (strIter.next() - '0') * 100 + (strIter.next() - '0') * 10 + (strIter.next() - '0');
                            }
                            aMutableInt64.setValue(year);
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        int year = calSystem.getYear(chrononTimeInMs);
                        aMutableInt64.setValue(year);
                        intSerde.serialize(aMutableInt64, out);
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) UTF8StringCharacterIterator(org.apache.hyracks.data.std.util.UTF8StringCharacterIterator) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Aggregations

ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)190 IPointable (org.apache.hyracks.data.std.api.IPointable)144 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)144 DataOutput (java.io.DataOutput)140 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)138 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)131 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)129 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)129 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)116 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)110 IOException (java.io.IOException)90 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)78 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)48 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)35 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)25 ATypeTag (org.apache.asterix.om.types.ATypeTag)23 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)18 RecordBuilder (org.apache.asterix.builders.RecordBuilder)18 AString (org.apache.asterix.om.base.AString)18 IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)17