Search in sources :

Example 6 with IARecordBuilder

use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.

the class InternalDatasetDetails method writePropertyTypeRecord.

protected void writePropertyTypeRecord(String name, String value, DataOutput out, ARecordType recordType) throws HyracksDataException {
    IARecordBuilder propertyRecordBuilder = new RecordBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    propertyRecordBuilder.reset(recordType);
    AMutableString aString = new AMutableString("");
    @SuppressWarnings("unchecked") ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
    // write field 0
    fieldValue.reset();
    aString.setValue(name);
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    propertyRecordBuilder.addField(0, fieldValue);
    // write field 1
    fieldValue.reset();
    aString.setValue(value);
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    propertyRecordBuilder.addField(1, fieldValue);
    propertyRecordBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString)

Example 7 with IARecordBuilder

use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.

the class TweetParser method writeRecord.

public void writeRecord(JsonNode obj, DataOutput out, ARecordType curRecType) throws IOException {
    IAType[] curTypes = null;
    String[] curFNames = null;
    int fieldN;
    int attrIdx;
    int expectedFieldsCount = 0;
    ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
    ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
    IARecordBuilder recBuilder = getRecordBuilder();
    if (curRecType != null) {
        curTypes = curRecType.getFieldTypes();
        curFNames = curRecType.getFieldNames();
        for (IAType curType : curTypes) {
            if (!(curType instanceof AUnionType)) {
                expectedFieldsCount++;
            }
        }
    }
    recBuilder.reset(curRecType);
    recBuilder.init();
    if (curRecType != null && !curRecType.isOpen()) {
        // closed record type
        fieldN = curFNames.length;
        for (int iter1 = 0; iter1 < fieldN; iter1++) {
            fieldValueBuffer.reset();
            DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
            if (obj.get(curFNames[iter1]).isNull() && !(curTypes[iter1] instanceof AUnionType)) {
                if (curRecType.isClosedField(curFNames[iter1])) {
                    throw new RuntimeDataException(ErrorCode.PARSER_TWEET_PARSER_CLOSED_FIELD_NULL, curFNames[iter1]);
                } else {
                    continue;
                }
            } else {
                if (writeField(obj.get(curFNames[iter1]), curTypes[iter1], fieldOutput)) {
                    recBuilder.addField(iter1, fieldValueBuffer);
                }
            }
        }
    } else {
        //open record type
        int closedFieldCount = 0;
        IAType curFieldType = null;
        String attrName;
        Iterator<String> iter = obj.fieldNames();
        while (iter.hasNext()) {
            attrName = iter.next();
            if (obj.get(attrName) == null || obj.get(attrName).isNull() || obj.size() == 0) {
                continue;
            }
            attrIdx = checkAttrNameIdx(curFNames, attrName);
            if (curRecType != null) {
                curFieldType = curRecType.getFieldType(attrName);
            }
            fieldValueBuffer.reset();
            fieldNameBuffer.reset();
            DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
            if (writeField(obj.get(attrName), curFieldType, fieldOutput)) {
                if (attrIdx == -1) {
                    aString.setValue(attrName);
                    stringSerde.serialize(aString, fieldNameBuffer.getDataOutput());
                    recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
                } else {
                    recBuilder.addField(attrIdx, fieldValueBuffer);
                    closedFieldCount++;
                }
            }
        }
        if (curRecType != null && closedFieldCount < expectedFieldsCount) {
            throw new HyracksDataException("Non-null field is null");
        }
    }
    recBuilder.write(out, true);
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AUnionType(org.apache.asterix.om.types.AUnionType) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 8 with IARecordBuilder

use of org.apache.asterix.builders.IARecordBuilder 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 9 with IARecordBuilder

use of org.apache.asterix.builders.IARecordBuilder 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 10 with IARecordBuilder

use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.

the class ExternalDatasetDetails method writeDatasetDetailsRecordType.

@SuppressWarnings("unchecked")
@Override
public void writeDatasetDetailsRecordType(DataOutput out) throws HyracksDataException {
    IARecordBuilder externalRecordBuilder = new RecordBuilder();
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    externalRecordBuilder.reset(MetadataRecordTypes.EXTERNAL_DETAILS_RECORDTYPE);
    AMutableString aString = new AMutableString("");
    ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
    ISerializerDeserializer<ADateTime> dateTimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
    ISerializerDeserializer<AInt32> intSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
    // write field 0
    fieldValue.reset();
    aString.setValue(this.getAdapter());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_DATASOURCE_ADAPTER_FIELD_INDEX, fieldValue);
    // write field 1
    listBuilder.reset((AOrderedListType) MetadataRecordTypes.EXTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[1]);
    for (Map.Entry<String, String> property : this.properties.entrySet()) {
        String name = property.getKey();
        String value = property.getValue();
        itemValue.reset();
        DatasetUtil.writePropertyTypeRecord(name, value, itemValue.getDataOutput(), MetadataRecordTypes.DATASOURCE_ADAPTER_PROPERTIES_RECORDTYPE);
        listBuilder.addItem(itemValue);
    }
    fieldValue.reset();
    listBuilder.write(fieldValue.getDataOutput(), true);
    externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_PROPERTIES_FIELD_INDEX, fieldValue);
    // write field 2
    fieldValue.reset();
    dateTimeSerde.serialize(new ADateTime(lastRefreshTime.getTime()), fieldValue.getDataOutput());
    externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_LAST_REFRESH_TIME_FIELD_INDEX, fieldValue);
    // write field 3
    fieldValue.reset();
    intSerde.serialize(new AInt32(state.ordinal()), fieldValue.getDataOutput());
    externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_TRANSACTION_STATE_FIELD_INDEX, fieldValue);
    externalRecordBuilder.write(out, true);
}
Also used : OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) AMutableString(org.apache.asterix.om.base.AMutableString) ADateTime(org.apache.asterix.om.base.ADateTime) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) AInt32(org.apache.asterix.om.base.AInt32) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AString(org.apache.asterix.om.base.AString) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)15 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)15 RecordBuilder (org.apache.asterix.builders.RecordBuilder)10 AMutableString (org.apache.asterix.om.base.AMutableString)7 AString (org.apache.asterix.om.base.AString)7 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)5 IAType (org.apache.asterix.om.types.IAType)5 ABoolean (org.apache.asterix.om.base.ABoolean)3 BitSet (java.util.BitSet)2 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)2 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 DataOutput (java.io.DataOutput)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)1 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)1 CaseInsensitiveString (org.apache.asterix.external.classad.CaseInsensitiveString)1 ExprTree (org.apache.asterix.external.classad.ExprTree)1 ADateTime (org.apache.asterix.om.base.ADateTime)1 AInt32 (org.apache.asterix.om.base.AInt32)1