Search in sources :

Example 26 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class IndexTupleTranslator method getTupleFromMetadataEntity.

@Override
public ITupleReference getTupleFromMetadataEntity(Index instance) throws HyracksDataException {
    // write the key in the first 3 fields of the tuple
    tupleBuilder.reset();
    aString.setValue(instance.getDataverseName());
    stringSerde.serialize(aString, tupleBuilder.getDataOutput());
    tupleBuilder.addFieldEndOffset();
    aString.setValue(instance.getDatasetName());
    stringSerde.serialize(aString, tupleBuilder.getDataOutput());
    tupleBuilder.addFieldEndOffset();
    aString.setValue(instance.getIndexName());
    stringSerde.serialize(aString, tupleBuilder.getDataOutput());
    tupleBuilder.addFieldEndOffset();
    // write the payload in the fourth field of the tuple
    recordBuilder.reset(MetadataRecordTypes.INDEX_RECORDTYPE);
    // write field 0
    fieldValue.reset();
    aString.setValue(instance.getDataverseName());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATAVERSENAME_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    aString.setValue(instance.getDatasetName());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATASETNAME_FIELD_INDEX, fieldValue);
    // write field 2
    fieldValue.reset();
    aString.setValue(instance.getIndexName());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXNAME_FIELD_INDEX, fieldValue);
    // write field 3
    fieldValue.reset();
    aString.setValue(instance.getIndexType().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXSTRUCTURE_FIELD_INDEX, fieldValue);
    // write field 4
    primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INDEX_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INDEX_ARECORD_SEARCHKEY_FIELD_INDEX]);
    List<List<String>> searchKey = instance.getKeyFieldNames();
    for (List<String> field : searchKey) {
        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);
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_SEARCHKEY_FIELD_INDEX, fieldValue);
    // write field 5
    fieldValue.reset();
    if (instance.isPrimaryIndex()) {
        booleanSerde.serialize(ABoolean.TRUE, fieldValue.getDataOutput());
    } else {
        booleanSerde.serialize(ABoolean.FALSE, fieldValue.getDataOutput());
    }
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_ISPRIMARY_FIELD_INDEX, fieldValue);
    // write field 6
    fieldValue.reset();
    aString.setValue(Calendar.getInstance().getTime().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_TIMESTAMP_FIELD_INDEX, fieldValue);
    // write field 7
    fieldValue.reset();
    intSerde.serialize(new AInt32(instance.getPendingOp()), fieldValue.getDataOutput());
    recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_PENDINGOP_FIELD_INDEX, fieldValue);
    // write optional field 8
    if (instance.getGramLength() > 0) {
        fieldValue.reset();
        nameValue.reset();
        aString.setValue(GRAM_LENGTH_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        intSerde.serialize(new AInt32(instance.getGramLength()), fieldValue.getDataOutput());
        recordBuilder.addField(nameValue, fieldValue);
    }
    if (instance.isEnforcingKeyFileds()) {
        // write optional field 9
        OrderedListBuilder typeListBuilder = new OrderedListBuilder();
        typeListBuilder.reset(new AOrderedListType(BuiltinType.ANY, null));
        nameValue.reset();
        aString.setValue(INDEX_SEARCHKEY_TYPE_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        List<IAType> searchKeyType = instance.getKeyFieldTypes();
        for (IAType type : searchKeyType) {
            itemValue.reset();
            aString.setValue(type.getTypeName());
            stringSerde.serialize(aString, itemValue.getDataOutput());
            typeListBuilder.addItem(itemValue);
        }
        fieldValue.reset();
        typeListBuilder.write(fieldValue.getDataOutput(), true);
        recordBuilder.addField(nameValue, fieldValue);
        // write optional field 10
        fieldValue.reset();
        nameValue.reset();
        aString.setValue(INDEX_ISENFORCED_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        booleanSerde.serialize(ABoolean.TRUE, fieldValue.getDataOutput());
        recordBuilder.addField(nameValue, fieldValue);
    }
    List<Integer> keySourceIndicator = instance.getKeyFieldSourceIndicators();
    boolean needSerialization = false;
    if (keySourceIndicator != null) {
        for (int source : keySourceIndicator) {
            if (source != 0) {
                needSerialization = true;
                break;
            }
        }
    }
    if (needSerialization) {
        listBuilder.reset(int8List);
        nameValue.reset();
        aString.setValue(INDEX_SEARCHKEY_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);
        recordBuilder.addField(nameValue, fieldValue);
    }
    // write record
    recordBuilder.write(tupleBuilder.getDataOutput(), true);
    tupleBuilder.addFieldEndOffset();
    tuple.reset(tupleBuilder.getFieldEndOffsets(), tupleBuilder.getByteArray());
    return tuple;
}
Also used : OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AString(org.apache.asterix.om.base.AString) AInt32(org.apache.asterix.om.base.AInt32) AOrderedList(org.apache.asterix.om.base.AOrderedList) ArrayList(java.util.ArrayList) List(java.util.List) IAType(org.apache.asterix.om.types.IAType)

Aggregations

AOrderedListType (org.apache.asterix.om.types.AOrderedListType)26 IAType (org.apache.asterix.om.types.IAType)16 ARecordType (org.apache.asterix.om.types.ARecordType)13 ArrayList (java.util.ArrayList)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)7 DataOutput (java.io.DataOutput)6 AString (org.apache.asterix.om.base.AString)6 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)6 AOrderedList (org.apache.asterix.om.base.AOrderedList)5 AUnionType (org.apache.asterix.om.types.AUnionType)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 ATypeTag (org.apache.asterix.om.types.ATypeTag)4 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)4 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)4 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)4 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)4 IPointable (org.apache.hyracks.data.std.api.IPointable)4