Search in sources :

Example 41 with FieldValue

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

the class MapValueUpdate method applyTo.

@Override
public FieldValue applyTo(FieldValue fval) {
    if (fval instanceof Array) {
        Array array = (Array) fval;
        FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
        element = update.applyTo(element);
        array.set(((IntegerFieldValue) value).getInteger(), element);
    } else if (fval instanceof WeightedSet) {
        WeightedSet wset = (WeightedSet) fval;
        WeightedSetDataType wtype = wset.getDataType();
        Integer weight = wset.get(value);
        if (weight == null) {
            if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
                weight = 0;
            } else {
                return fval;
            }
        }
        weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
        wset.put(value, weight);
        if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
            wset.remove(value);
        }
    }
    return fval;
}
Also used : Array(com.yahoo.document.datatypes.Array) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 42 with FieldValue

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

the class VespaDocumentDeserializer42 method read.

public void read(Annotation annotation) {
    int annotationTypeId = buf.getInt();
    AnnotationType type = manager.getAnnotationTypeRegistry().getType(annotationTypeId);
    if (type == null) {
        throw new DeserializationException("Cannot deserialize annotation of type " + annotationTypeId + " (unknown type)");
    }
    annotation.setType(type);
    byte features = buf.get();
    int length = buf.getInt1_2_4Bytes();
    if ((features & (byte) 1) == (byte) 1) {
        // we have a span node
        int spanNodeId = buf.getInt1_2_4Bytes();
        try {
            SpanNode node = spanNodes.get(spanNodeId);
            annotation.setSpanNode(node);
        } catch (IndexOutOfBoundsException ioobe) {
            throw new DeserializationException("Could not deserialize annotation, associated span node not found ", ioobe);
        }
    }
    if ((features & (byte) 2) == (byte) 2) {
        // we have a value:
        int dataTypeId = buf.getInt();
        // if this data type ID the same as the one in our config?
        if (dataTypeId != type.getDataType().getId()) {
            // not the same, but we will handle it gracefully, and just skip past the data:
            buf.position(buf.position() + length - 4);
        } else {
            FieldValue value = type.getDataType().createFieldValue();
            value.deserialize(this);
            annotation.setFieldValue(value);
        }
    }
}
Also used : SpanNode(com.yahoo.document.annotation.SpanNode) 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) AnnotationType(com.yahoo.document.annotation.AnnotationType)

Example 43 with FieldValue

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

the class VespaXMLUpdateReader method readAssign.

FieldUpdate readAssign(DocumentUpdate update) throws XMLStreamException {
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        if ("field".equals(reader.getAttributeName(i).toString())) {
            Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
            if (f == null) {
                throw newDeserializeException("Field " + reader.getAttributeValue(i) + " not found.");
            }
            FieldValue value = f.getDataType().createFieldValue();
            value.deserialize(f, this);
            return FieldUpdate.createAssign(f, value);
        }
    }
    throw newDeserializeException("Assignment update without field attribute");
}
Also used : FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue)

Example 44 with FieldValue

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

the class VespaXMLUpdateReader method readRemove.

FieldUpdate readRemove(DocumentUpdate update) throws XMLStreamException {
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        if ("field".equals(reader.getAttributeName(i).toString())) {
            Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
            FieldValue value = f.getDataType().createFieldValue();
            value.deserialize(f, this);
            if (value instanceof Array) {
                List<FieldValue> l = ((Array) value).getValues();
                return FieldUpdate.createRemoveAll(f, l);
            } else if (value instanceof WeightedSet) {
                return FieldUpdate.createRemoveAll(f, ((WeightedSet) value));
            } else {
                throw newDeserializeException("Remove operation only applicable to multivalue lists");
            }
        }
    }
    throw newDeserializeException("Remove update without field attribute");
}
Also used : Array(com.yahoo.document.datatypes.Array) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 45 with FieldValue

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

the class VespaXMLUpdateReader method readArithmetic.

FieldUpdate readArithmetic(DocumentUpdate update, String type, Field f, FieldUpdate fu) throws XMLStreamException {
    Double by = null;
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        if ("by".equals(reader.getAttributeName(i).toString())) {
            by = Double.parseDouble(reader.getAttributeValue(i));
        }
    }
    if (by == null) {
        throw newDeserializeException("Assignment update without \"by\" attribute");
    }
    FieldValue key = null;
    do {
        reader.next();
        if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
            if ("key".equals(reader.getName().toString())) {
                if (f.getDataType() instanceof WeightedSetDataType) {
                    DataType nestedType = ((WeightedSetDataType) f.getDataType()).getNestedType();
                    key = nestedType.createFieldValue();
                    key.deserialize(this);
                } else if (f.getDataType() instanceof MapDataType) {
                    key = ((MapDataType) f.getDataType()).getKeyType().createFieldValue();
                    key.deserialize(this);
                } else if (f.getDataType() instanceof ArrayDataType) {
                    key = new IntegerFieldValue(Integer.parseInt(reader.getElementText()));
                } else {
                    throw newDeserializeException("Key tag only applicable for weighted sets and maps");
                }
                skipToEnd("key");
            } else {
                throw newDeserializeException("\"" + reader.getName() + "\" not appropriate within " + type + " element.");
            }
        }
    } while (reader.getEventType() != XMLStreamReader.END_ELEMENT);
    if (key != null) {
        if ("increment".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createIncrement(key, by));
        }
        if ("decrement".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createDecrement(key, by));
        }
        if ("multiply".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createMultiply(key, by));
        }
        if ("divide".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createDivide(key, by));
        }
    } else {
        if ("increment".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createIncrement(by));
        }
        if ("decrement".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createDecrement(by));
        }
        if ("multiply".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createMultiply(by));
        }
        if ("divide".equals(type)) {
            fu.addValueUpdate(ValueUpdate.createDivide(by));
        }
    }
    return fu;
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue)

Aggregations

FieldValue (com.yahoo.document.datatypes.FieldValue)106 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)69 Test (org.junit.Test)52 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)45 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)30 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)26 Array (com.yahoo.document.datatypes.Array)20 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)20 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)18 StructuredFieldValue (com.yahoo.document.datatypes.StructuredFieldValue)14 Struct (com.yahoo.document.datatypes.Struct)13 DataType (com.yahoo.document.DataType)12 Document (com.yahoo.document.Document)12 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)12 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)12 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)11 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 DocumentPut (com.yahoo.document.DocumentPut)10