Search in sources :

Example 11 with RecordBuilder

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

the class RecordMergeDescriptor 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 {
            final PointableAllocator pa = new PointableAllocator();
            final IVisitablePointable vp0 = pa.allocateRecordValue(inRecType0);
            final IVisitablePointable vp1 = pa.allocateRecordValue(inRecType1);
            final IPointable argPtr0 = new VoidPointable();
            final IPointable argPtr1 = new VoidPointable();
            final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
            final IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
            final List<RecordBuilder> rbStack = new ArrayList<>();
            final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();
            return new IScalarEvaluator() {

                private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();

                private final DeepEqualAssessor deepEqualAssesor = new DeepEqualAssessor();

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    vp0.set(argPtr0);
                    vp1.set(argPtr1);
                    ARecordVisitablePointable rp0 = (ARecordVisitablePointable) vp0;
                    ARecordVisitablePointable rp1 = (ARecordVisitablePointable) vp1;
                    try {
                        mergeFields(outRecType, rp0, rp1, true, 0);
                        rbStack.get(0).write(out, true);
                    } catch (IOException | AsterixException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }

                private void mergeFields(ARecordType combinedType, ARecordVisitablePointable leftRecord, ARecordVisitablePointable rightRecord, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    if (rbStack.size() < (nestedLevel + 1)) {
                        rbStack.add(new RecordBuilder());
                    }
                    rbStack.get(nestedLevel).reset(combinedType);
                    rbStack.get(nestedLevel).init();
                    //Add all fields from left record
                    for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                        IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                        IVisitablePointable leftValue = leftRecord.getFieldValues().get(i);
                        IVisitablePointable leftType = leftRecord.getFieldTypeTags().get(i);
                        boolean foundMatch = false;
                        for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                            IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                            IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                            IVisitablePointable rightType = rightRecord.getFieldTypeTags().get(j);
                            // Check if same fieldname
                            if (PointableHelper.isEqual(leftName, rightName) && !deepEqualAssesor.isEqual(leftValue, rightValue)) {
                                //Field was found on the right and are subrecords, merge them
                                if (PointableHelper.sameType(ATypeTag.OBJECT, rightType) && PointableHelper.sameType(ATypeTag.OBJECT, leftType)) {
                                    //We are merging two sub records
                                    addFieldToSubRecord(combinedType, leftName, leftValue, rightValue, openFromParent, nestedLevel);
                                    foundMatch = true;
                                } else {
                                    throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
                                }
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, leftName, leftValue, null, openFromParent, nestedLevel);
                        }
                    }
                    //Repeat for right side (ignoring duplicates this time)
                    for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                        IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                        IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                        boolean foundMatch = false;
                        for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                            IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                            if (rightName.equals(leftName)) {
                                foundMatch = true;
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, rightName, rightValue, null, openFromParent, nestedLevel);
                        }
                    }
                }

                /*
                     * Takes in a record type, field name, and the field values (which are record) from two records
                     * Merges them into one record of combinedType
                     * And adds that record as a field to the Record in subrb
                     * the second value can be null, indicated that you just add the value of left as a field to subrb
                     *
                     */
                private void addFieldToSubRecord(ARecordType combinedType, IVisitablePointable fieldNamePointable, IVisitablePointable leftValue, IVisitablePointable rightValue, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    runtimeRecordTypeInfo.reset(combinedType);
                    int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
                    //Add the merged field
                    if (combinedType != null && pos >= 0) {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(pos, leftValue);
                        } else {
                            mergeFields((ARecordType) combinedType.getFieldTypes()[pos], (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(pos, tabvs);
                        }
                    } else {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(fieldNamePointable, leftValue);
                        } else {
                            mergeFields(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
                        }
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayList(java.util.ArrayList) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) DeepEqualAssessor(org.apache.asterix.runtime.evaluators.comparisons.DeepEqualAssessor) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) ARecordType(org.apache.asterix.om.types.ARecordType) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 12 with RecordBuilder

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

the class RecordPairsDescriptor 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 {
            // For writing each individual output record.
            final ArrayBackedValueStorage itemStorage = new ArrayBackedValueStorage();
            final DataOutput itemOutput = itemStorage.getDataOutput();
            final RecordBuilder recBuilder = new RecordBuilder();
            recBuilder.reset(RecordUtil.FULLY_OPEN_RECORD_TYPE);
            // For writing the resulting list of records.
            final OrderedListBuilder listBuilder = new OrderedListBuilder();
            final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
            final DataOutput resultOut = resultStorage.getDataOutput();
            // Sets up the constant field names, "name" for the key field, "value" for the value field.
            final ArrayBackedValueStorage nameStorage = new ArrayBackedValueStorage();
            final ArrayBackedValueStorage valueStorage = new ArrayBackedValueStorage();
            AObjectSerializerDeserializer serde = AObjectSerializerDeserializer.INSTANCE;
            try {
                serde.serialize(new AString("name"), nameStorage.getDataOutput());
                serde.serialize(new AString("value"), valueStorage.getDataOutput());
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
            return new IScalarEvaluator() {

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

                private final IPointable argPtr = new VoidPointable();

                private final ARecordVisitablePointable recordVisitablePointable = new ARecordVisitablePointable(recType);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    // Resets the result storage.
                    resultStorage.reset();
                    // Gets the input record.
                    argEvaluator.evaluate(tuple, argPtr);
                    byte inputTypeTag = argPtr.getByteArray()[argPtr.getStartOffset()];
                    if (inputTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, inputTypeTag, ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                    }
                    recordVisitablePointable.set(argPtr);
                    listBuilder.reset(AOrderedListType.FULL_OPEN_ORDEREDLIST_TYPE);
                    List<IVisitablePointable> fieldNames = recordVisitablePointable.getFieldNames();
                    List<IVisitablePointable> fieldValues = recordVisitablePointable.getFieldValues();
                    // Adds each field of the input record as a key-value pair into the result.
                    int numFields = recordVisitablePointable.getFieldNames().size();
                    for (int fieldIndex = 0; fieldIndex < numFields; ++fieldIndex) {
                        itemStorage.reset();
                        recBuilder.init();
                        recBuilder.addField(nameStorage, fieldNames.get(fieldIndex));
                        recBuilder.addField(valueStorage, fieldValues.get(fieldIndex));
                        recBuilder.write(itemOutput, true);
                        listBuilder.addItem(itemStorage);
                    }
                    // Writes the result and sets the result pointable.
                    listBuilder.write(resultOut, true);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) RecordBuilder(org.apache.asterix.builders.RecordBuilder) AObjectSerializerDeserializer(org.apache.asterix.dataflow.data.nontagged.serde.AObjectSerializerDeserializer) IOException(java.io.IOException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AString(org.apache.asterix.om.base.AString)

Example 13 with RecordBuilder

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

the class RecordRemoveFieldsEvalFactory method createScalarEvaluator.

@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
    final PointableAllocator pa = new PointableAllocator();
    final IVisitablePointable vp0 = pa.allocateRecordValue(inputRecType);
    final IVisitablePointable vp1 = pa.allocateListValue(inputListType);
    final IPointable inputArg0 = new VoidPointable();
    final IPointable inputArg1 = new VoidPointable();
    final IScalarEvaluator eval0 = inputRecordEvalFactory.createScalarEvaluator(ctx);
    final IScalarEvaluator eval1 = removeFieldPathsFactory.createScalarEvaluator(ctx);
    return new IScalarEvaluator() {

        private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();

        private final List<RecordBuilder> rbStack = new ArrayList<>();

        private final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();

        private final Deque<IVisitablePointable> recordPath = new ArrayDeque<>();

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private DataOutput out = resultStorage.getDataOutput();

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            resultStorage.reset();
            eval0.evaluate(tuple, inputArg0);
            eval1.evaluate(tuple, inputArg1);
            byte inputTypeTag0 = inputArg0.getByteArray()[inputArg0.getStartOffset()];
            if (inputTypeTag0 != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 0, inputTypeTag0, ATypeTag.SERIALIZED_INT32_TYPE_TAG);
            }
            byte inputTypeTag1 = inputArg1.getByteArray()[inputArg1.getStartOffset()];
            if (inputTypeTag1 != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 1, inputTypeTag1, ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG);
            }
            vp0.set(inputArg0);
            vp1.set(inputArg1);
            ARecordVisitablePointable recordPointable = (ARecordVisitablePointable) vp0;
            AListVisitablePointable listPointable = (AListVisitablePointable) vp1;
            try {
                recordPath.clear();
                rbStack.clear();
                processRecord(requiredRecType, recordPointable, listPointable, 0);
                rbStack.get(0).write(out, true);
            } catch (IOException | AsterixException e) {
                throw new HyracksDataException(e);
            }
            result.set(resultStorage);
        }

        private void processRecord(ARecordType requiredType, ARecordVisitablePointable srp, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
            if (rbStack.size() < (nestedLevel + 1)) {
                rbStack.add(new RecordBuilder());
            }
            rbStack.get(nestedLevel).reset(requiredType);
            rbStack.get(nestedLevel).init();
            List<IVisitablePointable> fieldNames = srp.getFieldNames();
            List<IVisitablePointable> fieldValues = srp.getFieldValues();
            List<IVisitablePointable> fieldTypes = srp.getFieldTypeTags();
            for (int i = 0; i < fieldNames.size(); i++) {
                IVisitablePointable subRecFieldName = fieldNames.get(i);
                recordPath.push(subRecFieldName);
                if (isValidPath(inputList)) {
                    if (requiredType != null && requiredType.getTypeTag() != ATypeTag.ANY) {
                        addKeptFieldToSubRecord(requiredType, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
                    } else {
                        addKeptFieldToSubRecord(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
                    }
                }
                recordPath.pop();
            }
        }

        private void addKeptFieldToSubRecord(ARecordType requiredType, IVisitablePointable fieldNamePointable, IVisitablePointable fieldValuePointable, IVisitablePointable fieldTypePointable, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
            runtimeRecordTypeInfo.reset(requiredType);
            int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
            if (pos >= 0) {
                // Closed field
                if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
                    processRecord((ARecordType) requiredType.getFieldTypes()[pos], (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
                    tabvs.reset();
                    rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                    rbStack.get(nestedLevel).addField(pos, tabvs);
                } else {
                    rbStack.get(nestedLevel).addField(pos, fieldValuePointable);
                }
            } else {
                // Open field
                if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
                    processRecord(null, (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
                    tabvs.reset();
                    rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                    rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
                } else {
                    rbStack.get(nestedLevel).addField(fieldNamePointable, fieldValuePointable);
                }
            }
        }

        private boolean isValidPath(AListVisitablePointable inputList) throws HyracksDataException {
            List<IVisitablePointable> items = inputList.getItems();
            List<IVisitablePointable> typeTags = inputList.getItemTags();
            int pathLen = recordPath.size();
            for (int i = 0; i < items.size(); i++) {
                IVisitablePointable item = items.get(i);
                if (PointableHelper.sameType(ATypeTag.ARRAY, typeTags.get(i))) {
                    List<IVisitablePointable> inputPathItems = ((AListVisitablePointable) item).getItems();
                    if (pathLen == inputPathItems.size()) {
                        boolean match = true;
                        Iterator<IVisitablePointable> fpi = recordPath.iterator();
                        for (int j = inputPathItems.size() - 1; j >= 0; j--) {
                            match &= PointableHelper.isEqual(inputPathItems.get(j), fpi.next());
                            if (!match) {
                                break;
                            }
                        }
                        if (match) {
                            // Not a valid path for the output record
                            return false;
                        }
                    }
                } else {
                    if (PointableHelper.isEqual(recordPath.getFirst(), item)) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) AListVisitablePointable(org.apache.asterix.om.pointables.AListVisitablePointable) AsterixException(org.apache.asterix.common.exceptions.AsterixException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) ArrayList(java.util.ArrayList) List(java.util.List) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IOException(java.io.IOException) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) ARecordType(org.apache.asterix.om.types.ARecordType)

Example 14 with RecordBuilder

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

the class OpenRecordConstructorDescriptor 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 {
            int n = args.length / 2;
            final IScalarEvaluator[] evalNames = new IScalarEvaluator[n];
            final IScalarEvaluator[] evalFields = new IScalarEvaluator[n];
            final IPointable fieldNamePointable = new VoidPointable();
            final IPointable fieldValuePointable = new VoidPointable();
            for (int i = 0; i < n; i++) {
                evalNames[i] = args[2 * i].createScalarEvaluator(ctx);
                evalFields[i] = args[2 * i + 1].createScalarEvaluator(ctx);
            }
            final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
            final DataOutput out = resultStorage.getDataOutput();
            return new IScalarEvaluator() {

                private RecordBuilder recBuilder = new RecordBuilder();

                private int closedFieldId;

                private boolean first = true;

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    closedFieldId = 0;
                    if (first) {
                        first = false;
                        recBuilder.reset(recType);
                    }
                    recBuilder.init();
                    for (int i = 0; i < evalFields.length; i++) {
                        evalFields[i].evaluate(tuple, fieldValuePointable);
                        boolean openField = openFields[i];
                        if (openField) {
                            evalNames[i].evaluate(tuple, fieldNamePointable);
                            recBuilder.addField(fieldNamePointable, fieldValuePointable);
                        } else {
                            recBuilder.addField(closedFieldId, fieldValuePointable);
                            closedFieldId++;
                        }
                    }
                    recBuilder.write(out, true);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 15 with RecordBuilder

use of org.apache.asterix.builders.RecordBuilder 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

RecordBuilder (org.apache.asterix.builders.RecordBuilder)18 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)18 IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)12 AString (org.apache.asterix.om.base.AString)11 AMutableString (org.apache.asterix.om.base.AMutableString)8 DataOutput (java.io.DataOutput)5 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)5 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 IPointable (org.apache.hyracks.data.std.api.IPointable)5 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)5 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)5 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)4 ARecordVisitablePointable (org.apache.asterix.om.pointables.ARecordVisitablePointable)4 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)4 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)4 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)4 IOException (java.io.IOException)3 AsterixException (org.apache.asterix.common.exceptions.AsterixException)3 PointableAllocator (org.apache.asterix.om.pointables.PointableAllocator)3 ARecordType (org.apache.asterix.om.types.ARecordType)3