Search in sources :

Example 1 with IARecordBuilder

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

the class DatatypeTupleTranslator method writeRecordType.

private void writeRecordType(Datatype instance, AbstractComplexType type, DataOutput out) throws HyracksDataException {
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    IARecordBuilder recordRecordBuilder = new RecordBuilder();
    IARecordBuilder fieldRecordBuilder = new RecordBuilder();
    ARecordType recType = (ARecordType) type;
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    listBuilder.reset(new AOrderedListType(MetadataRecordTypes.FIELD_RECORDTYPE, null));
    IAType fieldType = null;
    for (int i = 0; i < recType.getFieldNames().length; i++) {
        fieldType = recType.getFieldTypes()[i];
        boolean fieldIsNullable = false;
        if (NonTaggedFormatUtil.isOptional(fieldType)) {
            fieldIsNullable = true;
            fieldType = ((AUnionType) fieldType).getActualType();
        }
        if (fieldType.getTypeTag().isDerivedType()) {
            handleNestedDerivedType(fieldType.getTypeName(), (AbstractComplexType) fieldType, instance, instance.getDataverseName(), instance.getDatatypeName());
        }
        itemValue.reset();
        fieldRecordBuilder.reset(MetadataRecordTypes.FIELD_RECORDTYPE);
        // write field 0
        fieldValue.reset();
        aString.setValue(recType.getFieldNames()[i]);
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDNAME_FIELD_INDEX, fieldValue);
        // write field 1
        fieldValue.reset();
        aString.setValue(fieldType.getTypeName());
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDTYPE_FIELD_INDEX, fieldValue);
        // write field 2
        fieldValue.reset();
        booleanSerde.serialize(fieldIsNullable ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_ISNULLABLE_FIELD_INDEX, fieldValue);
        // write record
        fieldRecordBuilder.write(itemValue.getDataOutput(), true);
        // add item to the list of fields
        listBuilder.addItem(itemValue);
    }
    recordRecordBuilder.reset(MetadataRecordTypes.RECORD_RECORDTYPE);
    // write field 0
    fieldValue.reset();
    booleanSerde.serialize(recType.isOpen() ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_ISOPEN_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    listBuilder.write(fieldValue.getDataOutput(), true);
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX, fieldValue);
    // write record
    recordRecordBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 2 with IARecordBuilder

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

the class DatasetUtil method writePropertyTypeRecord.

@SuppressWarnings("unchecked")
public static 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("");
    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) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString)

Example 3 with IARecordBuilder

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

the class ADMDataParser method parseRecord.

private void parseRecord(ARecordType recType, DataOutput out) throws IOException {
    ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
    ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
    IARecordBuilder recBuilder = getRecordBuilder();
    BitSet nulls = null;
    if (recType != null) {
        // TODO: use BitSet Pool
        nulls = new BitSet(recType.getFieldNames().length);
        recBuilder.reset(recType);
    } else {
        recBuilder.reset(null);
    }
    recBuilder.init();
    int token;
    boolean inRecord = true;
    boolean expectingRecordField = false;
    boolean first = true;
    Boolean openRecordField = false;
    int fieldId = 0;
    IAType fieldType = null;
    do {
        token = admLexer.next();
        switch(token) {
            case AdmLexer.TOKEN_END_RECORD:
                if (expectingRecordField) {
                    throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_RECORD_END_UNEXPECTED);
                }
                inRecord = false;
                break;
            case AdmLexer.TOKEN_STRING_LITERAL:
                // we've read the name of the field
                // now read the content
                fieldNameBuffer.reset();
                fieldValueBuffer.reset();
                expectingRecordField = false;
                if (recType != null) {
                    String fldName = admLexer.getLastTokenImage().substring(1, admLexer.getLastTokenImage().length() - 1);
                    fieldId = recBuilder.getFieldId(fldName);
                    if ((fieldId < 0) && !recType.isOpen()) {
                        throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_EXTRA_FIELD_IN_CLOSED_RECORD, fldName);
                    } else if ((fieldId < 0) && recType.isOpen()) {
                        aStringFieldName.setValue(admLexer.getLastTokenImage().substring(1, admLexer.getLastTokenImage().length() - 1));
                        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(admLexer.getLastTokenImage().substring(1, admLexer.getLastTokenImage().length() - 1));
                    stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
                    openRecordField = true;
                    fieldType = null;
                }
                token = admLexer.next();
                if (token != AdmLexer.TOKEN_COLON) {
                    throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_UNEXPECTED_TOKEN_WHEN_EXPECT_COMMA, AdmLexer.tokenKindToString(token));
                }
                token = admLexer.next();
                this.admFromLexerStream(token, fieldType, fieldValueBuffer.getDataOutput());
                if (openRecordField) {
                    recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
                } else {
                    recBuilder.addField(fieldId, fieldValueBuffer);
                }
                break;
            case AdmLexer.TOKEN_COMMA:
                if (first) {
                    throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_FOUND_COMMA_WHEN, "before any");
                }
                if (expectingRecordField) {
                    throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_FOUND_COMMA_WHEN, "expecting a");
                }
                expectingRecordField = true;
                break;
            default:
                throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_UNEXPECTED_TOKEN_KIND, AdmLexer.tokenKindToString(token));
        }
        first = false;
    } while (inRecord);
    if (recType != null) {
        final int nullableFieldId = checkOptionalConstraints(recType, nulls);
        if (nullableFieldId != -1) {
            throw new ParseException(ErrorCode.PARSER_ADM_DATA_PARSER_FIELD_NOT_NULL, recType.getFieldNames()[nullableFieldId]);
        }
    }
    recBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) BitSet(java.util.BitSet) ABoolean(org.apache.asterix.om.base.ABoolean) IAType(org.apache.asterix.om.types.IAType)

Example 4 with IARecordBuilder

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

the class InternalDatasetDetails method writeDatasetDetailsRecordType.

@Override
public void writeDatasetDetailsRecordType(DataOutput out) throws HyracksDataException {
    IARecordBuilder internalRecordBuilder = new RecordBuilder();
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    OrderedListBuilder primaryKeyListBuilder = new OrderedListBuilder();
    AOrderedListType stringList = new AOrderedListType(BuiltinType.ASTRING, null);
    AOrderedListType int8List = new AOrderedListType(BuiltinType.AINT8, null);
    AOrderedListType heterogeneousList = new AOrderedListType(BuiltinType.ANY, null);
    internalRecordBuilder.reset(MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE);
    AMutableString aString = new AMutableString("");
    AMutableInt8 aInt8 = new AMutableInt8((byte) 0);
    @SuppressWarnings("unchecked") ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
    @SuppressWarnings("unchecked") ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
    @SuppressWarnings("unchecked") ISerializerDeserializer<AInt8> int8Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
    // write field 0
    fieldValue.reset();
    aString.setValue(getFileStructure().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_FILESTRUCTURE_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    aString.setValue(getPartitioningStrategy().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONSTRATEGY_FIELD_INDEX, fieldValue);
    // write field 2
    primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONKEY_FIELD_INDEX]);
    for (List<String> field : partitioningKeys) {
        listBuilder.reset(stringList);
        for (String subField : field) {
            itemValue.reset();
            aString.setValue(subField);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        itemValue.reset();
        listBuilder.write(itemValue.getDataOutput(), true);
        primaryKeyListBuilder.addItem(itemValue);
    }
    fieldValue.reset();
    primaryKeyListBuilder.write(fieldValue.getDataOutput(), true);
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONKEY_FIELD_INDEX, fieldValue);
    // write field 3
    primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PRIMARYKEY_FIELD_INDEX]);
    for (List<String> field : primaryKeys) {
        listBuilder.reset(stringList);
        for (String subField : field) {
            itemValue.reset();
            aString.setValue(subField);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        itemValue.reset();
        listBuilder.write(itemValue.getDataOutput(), true);
        primaryKeyListBuilder.addItem(itemValue);
    }
    fieldValue.reset();
    primaryKeyListBuilder.write(fieldValue.getDataOutput(), true);
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PRIMARYKEY_FIELD_INDEX, fieldValue);
    // write field 4
    fieldValue.reset();
    ABoolean b = isAutogenerated() ? ABoolean.TRUE : ABoolean.FALSE;
    booleanSerde.serialize(b, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_AUTOGENERATED_FIELD_INDEX, fieldValue);
    // write filter fields if any
    List<String> filterField = getFilterField();
    if (filterField != null) {
        listBuilder.reset(heterogeneousList);
        ArrayBackedValueStorage nameValue = new ArrayBackedValueStorage();
        nameValue.reset();
        aString.setValue(FILTER_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        for (String field : filterField) {
            itemValue.reset();
            aString.setValue(field);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        fieldValue.reset();
        listBuilder.write(fieldValue.getDataOutput(), true);
        internalRecordBuilder.addField(nameValue, fieldValue);
    }
    // write key source indicators if any
    List<Integer> keySourceIndicator = getKeySourceIndicator();
    boolean needSerialization = false;
    if (keySourceIndicator != null) {
        for (int source : keySourceIndicator) {
            if (source != 0) {
                needSerialization = true;
                break;
            }
        }
    }
    if (needSerialization) {
        listBuilder.reset(int8List);
        ArrayBackedValueStorage nameValue = new ArrayBackedValueStorage();
        nameValue.reset();
        aString.setValue(KEY_FILD_SOURCE_INDICATOR_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        for (int source : keySourceIndicator) {
            itemValue.reset();
            aInt8.setValue((byte) source);
            int8Serde.serialize(aInt8, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        fieldValue.reset();
        listBuilder.write(fieldValue.getDataOutput(), true);
        internalRecordBuilder.addField(nameValue, fieldValue);
    }
    internalRecordBuilder.write(out, true);
}
Also used : OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) ABoolean(org.apache.asterix.om.base.ABoolean) 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) AMutableString(org.apache.asterix.om.base.AMutableString) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AInt8(org.apache.asterix.om.base.AInt8) AString(org.apache.asterix.om.base.AString)

Example 5 with IARecordBuilder

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

the class DatasetTupleTranslator method writeDatasetHintRecord.

@SuppressWarnings("unchecked")
protected void writeDatasetHintRecord(String name, String value, DataOutput out) throws HyracksDataException {
    IARecordBuilder propertyRecordBuilder = new RecordBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    propertyRecordBuilder.reset(MetadataRecordTypes.DATASET_HINTS_RECORDTYPE);
    AMutableString aString = new AMutableString("");
    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) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString)

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