Search in sources :

Example 21 with DataType

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

the class StringTestCase method parseAnnotationType.

public void parseAnnotationType(AnnotationType t) {
    System.out.println("Type Name: " + t.getName());
    System.out.println("Type ID: " + t.getId());
    DataType dt = t.getDataType();
    String dataTypeStr;
    if (dt == DataType.STRING) {
        dataTypeStr = "String";
    } else if (dt == DataType.INT) {
        dataTypeStr = "Integer";
    } else if (dt == DataType.URI) {
        dataTypeStr = "URL";
    } else {
        dataTypeStr = "UNKNOWN";
    }
    System.out.println("Type DataType: " + dataTypeStr);
}
Also used : DataType(com.yahoo.document.DataType)

Example 22 with DataType

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

the class ArrayTestCase method testCreateIllegalArray.

public void testCreateIllegalArray() {
    ArrayList<FieldValue> arrayList = new ArrayList<>();
    arrayList.add(new StringFieldValue("foo"));
    arrayList.add(new IntegerFieldValue(1000));
    DataType stringType = new ArrayDataType(DataType.STRING);
    try {
        Array<FieldValue> illegalArray = new Array<>(stringType, arrayList);
        fail("Expected an exception");
    } catch (IllegalArgumentException e) {
        assertEquals("FieldValue 1000 is not compatible with datatype " + "Array<string> (code: -1486737430).", e.getMessage());
    }
    DataType intType = new ArrayDataType(DataType.INT);
    Array<IntegerFieldValue> intArray = new Array<>(intType);
    intArray.add(new IntegerFieldValue(42));
    Array<StringFieldValue> stringArray = new Array<>(stringType);
    try {
        stringArray.assign(intArray);
        fail("Expected an exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Incompatible data types. Got datatype int (code: 0)," + " expected datatype string (code: 2)", e.getMessage());
    }
}
Also used : DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) ArrayDataType(com.yahoo.document.ArrayDataType)

Example 23 with DataType

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

the class MapReader method fillMapFromArray.

@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent) {
    JsonToken token = buffer.currentToken();
    int initNesting = buffer.nesting();
    expectArrayStart(token);
    token = buffer.next();
    DataType keyType = parent.getDataType().getKeyType();
    DataType valueType = parent.getDataType().getValueType();
    while (buffer.nesting() >= initNesting) {
        FieldValue key = null;
        FieldValue value = null;
        expectObjectStart(token);
        token = buffer.next();
        for (int i = 0; i < 2; ++i) {
            if (MAP_KEY.equals(buffer.currentName())) {
                key = readSingleValue(buffer, keyType);
            } else if (MAP_VALUE.equals(buffer.currentName())) {
                value = readSingleValue(buffer, valueType);
            }
            token = buffer.next();
        }
        Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
        parent.put(key, value);
        expectObjectEnd(token);
        // array end or next entry
        token = buffer.next();
    }
}
Also used : DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) CollectionDataType(com.yahoo.document.CollectionDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) MapDataType(com.yahoo.document.MapDataType) JsonToken(com.fasterxml.jackson.core.JsonToken) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue)

Example 24 with DataType

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

the class MapReader method fillMapFromObject.

@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) {
    JsonToken token = buffer.currentToken();
    int initNesting = buffer.nesting();
    expectObjectStart(token);
    token = buffer.next();
    DataType keyType = parent.getDataType().getKeyType();
    DataType valueType = parent.getDataType().getValueType();
    while (buffer.nesting() >= initNesting) {
        FieldValue key = readAtomic(buffer.currentName(), keyType);
        FieldValue value = readSingleValue(buffer, valueType);
        Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
        parent.put(key, value);
        token = buffer.next();
    }
    expectObjectEnd(token);
}
Also used : DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) CollectionDataType(com.yahoo.document.CollectionDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) MapDataType(com.yahoo.document.MapDataType) JsonToken(com.fasterxml.jackson.core.JsonToken) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue)

Example 25 with DataType

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

the class FieldUpdate method applyTo.

/**
 * Applies this field update.
 *
 * @param doc the document to apply the update to
 * @return a reference to itself
 */
public FieldUpdate applyTo(Document doc) {
    for (ValueUpdate vupd : valueUpdates) {
        DataType dataType = field.getDataType();
        FieldValue oldValue = doc.getFieldValue(field);
        boolean existed = (oldValue != null);
        if (!existed) {
            oldValue = dataType.createFieldValue();
        }
        FieldValue newValue = vupd.applyTo(oldValue);
        if (newValue == null) {
            if (existed) {
                doc.removeFieldValue(field);
            }
        } else {
            doc.setFieldValue(field, newValue);
        }
    }
    return this;
}
Also used : DataType(com.yahoo.document.DataType) FieldValue(com.yahoo.document.datatypes.FieldValue)

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