Search in sources :

Example 46 with DataType

use of com.yahoo.document.DataType in project vespa by vespa-engine.

the class FieldValueConverter method convertArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
private FieldValue convertArray(Array val) {
    List<FieldValue> next = new LinkedList<FieldValue>();
    DataType nextType = null;
    for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext(); ) {
        FieldValue prevVal = it.next();
        FieldValue nextVal = convert(prevVal);
        if (nextVal == null) {
            continue;
        }
        if (nextType == null) {
            nextType = nextVal.getDataType();
        } else if (!nextType.isValueCompatible(nextVal)) {
            throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextVal.getDataType().getName() + ".");
        }
        next.add(nextVal);
    }
    if (nextType == null) {
        return null;
    }
    Array ret = DataType.getArray(nextType).createFieldValue();
    for (FieldValue nextVal : next) {
        ret.add(nextVal);
    }
    return ret;
}
Also used : DataType(com.yahoo.document.DataType)

Example 47 with DataType

use of com.yahoo.document.DataType in project vespa by vespa-engine.

the class SimpleDocumentAdapter method tryOutputType.

@Override
public void tryOutputType(Expression exp, String fieldName, DataType valueType) {
    Field field = output.getDataType().getField(fieldName);
    if (field == null) {
        throw new VerificationException(exp, "Field '" + fieldName + "' not found.");
    }
    DataType fieldType = field.getDataType();
    if (!fieldType.isAssignableFrom(valueType)) {
        throw new VerificationException(exp, "Can not assign " + valueType.getName() + " to field '" + fieldName + "' which is " + fieldType.getName() + ".");
    }
}
Also used : Field(com.yahoo.document.Field) VerificationException(com.yahoo.vespa.indexinglanguage.expressions.VerificationException) DataType(com.yahoo.document.DataType)

Example 48 with DataType

use of com.yahoo.document.DataType in project vespa by vespa-engine.

the class AddRemoveCreator method createAddsOrRemoves.

// yes, this suppresswarnings ugliness is by intention, the code relies on
// the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) {
    FieldValue container = field.getDataType().createFieldValue();
    FieldUpdate singleUpdate;
    int initNesting = buffer.nesting();
    Preconditions.checkState(buffer.currentToken().isStructStart(), "Expected start of composite, got %s", buffer.currentToken());
    if (container instanceof CollectionFieldValue) {
        buffer.next();
        DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType();
        if (container instanceof WeightedSet) {
            // these are objects with string keys (which are the nested
            // types) and values which are the weight
            WeightedSet weightedSet = (WeightedSet) container;
            fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet);
            if (isRemove) {
                singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet);
            } else {
                singleUpdate = FieldUpdate.createAddAll(field, weightedSet);
            }
        } else {
            List<FieldValue> arrayContents = new ArrayList<>();
            ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents);
            if (buffer.currentToken() != JsonToken.END_ARRAY) {
                throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'.");
            }
            if (isRemove) {
                singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents);
            } else {
                singleUpdate = FieldUpdate.createAddAll(field, arrayContents);
            }
        }
    } else {
        throw new UnsupportedOperationException("Trying to add or remove from a field of a type the reader does not know how to handle: " + container.getClass().getName());
    }
    expectCompositeEnd(buffer.currentToken());
    update.addAll(singleUpdate);
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) ArrayList(java.util.ArrayList) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 49 with DataType

use of com.yahoo.document.DataType in project vespa by vespa-engine.

the class CompositeReader method populateComposite.

// TODO createComposite is extremely similar to add/remove, refactor
// yes, this suppresswarnings ugliness is by intention, the code relies on the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes" })
public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue) {
    JsonToken token = buffer.currentToken();
    if ((token != JsonToken.START_OBJECT) && (token != JsonToken.START_ARRAY)) {
        throw new IllegalArgumentException("Expected '[' or '{'. Got '" + token + "'.");
    }
    if (fieldValue instanceof CollectionFieldValue) {
        DataType valueType = ((CollectionFieldValue) fieldValue).getDataType().getNestedType();
        if (fieldValue instanceof WeightedSet) {
            fillWeightedSet(buffer, valueType, (WeightedSet) fieldValue);
        } else {
            fillArray(buffer, (CollectionFieldValue) fieldValue, valueType);
        }
    } else if (fieldValue instanceof MapFieldValue) {
        MapReader.fillMap(buffer, (MapFieldValue) fieldValue);
    } else if (fieldValue instanceof StructuredFieldValue) {
        StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue);
    } else if (fieldValue instanceof TensorFieldValue) {
        TensorReader.fillTensor(buffer, (TensorFieldValue) fieldValue);
    } else {
        throw new IllegalStateException("Has created a composite field" + " value the reader does not know how to handle: " + fieldValue.getClass().getName() + " This is a bug. token = " + token);
    }
    expectCompositeEnd(buffer.currentToken());
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) JsonToken(com.fasterxml.jackson.core.JsonToken) WeightedSet(com.yahoo.document.datatypes.WeightedSet) WeightedSetReader.fillWeightedSet(com.yahoo.document.json.readers.WeightedSetReader.fillWeightedSet)

Example 50 with DataType

use of com.yahoo.document.DataType in project vespa by vespa-engine.

the class VespaDocumentDeserializer42 method read.

public void read(AssignFieldPathUpdate update) {
    byte flags = getByte(null);
    update.setRemoveIfZero((flags & AssignFieldPathUpdate.REMOVE_IF_ZERO) != 0);
    update.setCreateMissingPath((flags & AssignFieldPathUpdate.CREATE_MISSING_PATH) != 0);
    if ((flags & AssignFieldPathUpdate.ARITHMETIC_EXPRESSION) != 0) {
        update.setExpression(getString(null));
    } else {
        DataType dt = update.getFieldPath().getResultingDataType();
        FieldValue fv = dt.createFieldValue();
        fv.deserialize(this);
        update.setNewValue(fv);
    }
}
Also used : DataType(com.yahoo.document.DataType) CollectionDataType(com.yahoo.document.CollectionDataType) MapDataType(com.yahoo.document.MapDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) ArrayDataType(com.yahoo.document.ArrayDataType) StructDataType(com.yahoo.document.StructDataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ReferenceFieldValue(com.yahoo.document.datatypes.ReferenceFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue)

Aggregations

DataType (com.yahoo.document.DataType)62 ArrayDataType (com.yahoo.document.ArrayDataType)20 MapDataType (com.yahoo.document.MapDataType)19 Field (com.yahoo.document.Field)17 StructDataType (com.yahoo.document.StructDataType)17 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)17 ReferenceDataType (com.yahoo.document.ReferenceDataType)13 PositionDataType (com.yahoo.document.PositionDataType)12 CollectionDataType (com.yahoo.document.CollectionDataType)11 FieldValue (com.yahoo.document.datatypes.FieldValue)11 TensorDataType (com.yahoo.document.TensorDataType)10 DocumentType (com.yahoo.document.DocumentType)9 StructuredDataType (com.yahoo.document.StructuredDataType)7 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)6 SDField (com.yahoo.searchdefinition.document.SDField)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)5 TemporaryStructuredDataType (com.yahoo.document.TemporaryStructuredDataType)4 AnnotationReferenceDataType (com.yahoo.document.annotation.AnnotationReferenceDataType)4 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)4 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)4