Search in sources :

Example 1 with ArithmeticValueUpdate

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

the class JsonReaderTestCase method testArithmeticOperators.

@SuppressWarnings({ "cast", "unchecked", "rawtypes" })
@Test
public final void testArithmeticOperators() throws IOException {
    Tuple2[] operations = new Tuple2[] { new Tuple2<String, Operator>(UPDATE_DECREMENT, ArithmeticValueUpdate.Operator.SUB), new Tuple2<String, Operator>(UPDATE_DIVIDE, ArithmeticValueUpdate.Operator.DIV), new Tuple2<String, Operator>(UPDATE_INCREMENT, ArithmeticValueUpdate.Operator.ADD), new Tuple2<String, Operator>(UPDATE_MULTIPLY, ArithmeticValueUpdate.Operator.MUL) };
    for (Tuple2<String, Operator> operator : operations) {
        DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"match\": {" + " \"element\": \"person\"," + " \"" + (String) operator.first + "\": 13}}}}");
        Map<String, Tuple2<Number, Operator>> matches = new HashMap<>();
        FieldUpdate x = doc.getFieldUpdate("actualset");
        for (ValueUpdate v : x.getValueUpdates()) {
            MapValueUpdate adder = (MapValueUpdate) v;
            final String key = ((StringFieldValue) adder.getValue()).getString();
            Operator op = ((ArithmeticValueUpdate) adder.getUpdate()).getOperator();
            Number n = ((ArithmeticValueUpdate) adder.getUpdate()).getOperand();
            matches.put(key, new Tuple2<>(n, op));
        }
        assertEquals(1, matches.size());
        final String o = "person";
        assertSame(operator.second, matches.get(o).second);
        assertEquals(Double.valueOf(13), matches.get(o).first);
    }
}
Also used : Operator(com.yahoo.document.update.ArithmeticValueUpdate.Operator) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) HashMap(java.util.HashMap) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Tuple2(com.yahoo.collections.Tuple2) FieldUpdate(com.yahoo.document.update.FieldUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) Test(org.junit.Test)

Example 2 with ArithmeticValueUpdate

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

the class JsonReaderTestCase method testUpdateMatch.

@Test
public final void testUpdateMatch() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"match\": {" + " \"element\": \"person\"," + " \"increment\": 13}}}}");
    Map<String, Tuple2<Number, String>> matches = new HashMap<>();
    FieldUpdate x = doc.getFieldUpdate("actualset");
    for (ValueUpdate<?> v : x.getValueUpdates()) {
        MapValueUpdate adder = (MapValueUpdate) v;
        final String key = ((StringFieldValue) adder.getValue()).getString();
        String op = ((ArithmeticValueUpdate) adder.getUpdate()).getOperator().toString();
        Number n = ((ArithmeticValueUpdate) adder.getUpdate()).getOperand();
        matches.put(key, new Tuple2<>(n, op));
    }
    assertEquals(1, matches.size());
    final String o = "person";
    assertEquals("ADD", matches.get(o).second);
    assertEquals(Double.valueOf(13), matches.get(o).first);
}
Also used : MapValueUpdate(com.yahoo.document.update.MapValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Tuple2(com.yahoo.collections.Tuple2) FieldUpdate(com.yahoo.document.update.FieldUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) Test(org.junit.Test)

Example 3 with ArithmeticValueUpdate

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

the class VespaDocumentDeserializer42 method getValueUpdate.

public ValueUpdate getValueUpdate(DataType superType, DataType subType) {
    int vuTypeId = getInt(null);
    ValueUpdate.ValueUpdateClassID op = ValueUpdate.ValueUpdateClassID.getID(vuTypeId);
    if (op == null) {
        throw new IllegalArgumentException("Read type " + vuTypeId + " of bytebuffer, but this is not a legal value update type.");
    }
    switch(op) {
        case ADD:
            {
                FieldValue fval = subType.createFieldValue();
                fval.deserialize(this);
                int weight = getInt(null);
                return new AddValueUpdate(fval, weight);
            }
        case ARITHMETIC:
            int opId = getInt(null);
            ArithmeticValueUpdate.Operator operator = ArithmeticValueUpdate.Operator.getID(opId);
            double operand = getDouble(null);
            return new ArithmeticValueUpdate(operator, operand);
        case ASSIGN:
            {
                byte contents = getByte(null);
                FieldValue fval = null;
                if (contents == (byte) 1) {
                    fval = superType.createFieldValue();
                    fval.deserialize(this);
                }
                return new AssignValueUpdate(fval);
            }
        case CLEAR:
            return new ClearValueUpdate();
        case MAP:
            if (superType instanceof ArrayDataType) {
                CollectionDataType type = (CollectionDataType) superType;
                IntegerFieldValue index = new IntegerFieldValue();
                index.deserialize(this);
                ValueUpdate update = getValueUpdate(type.getNestedType(), null);
                return new MapValueUpdate(index, update);
            } else if (superType instanceof WeightedSetDataType) {
                CollectionDataType type = (CollectionDataType) superType;
                FieldValue fval = type.getNestedType().createFieldValue();
                fval.deserialize(this);
                ValueUpdate update = getValueUpdate(DataType.INT, null);
                return new MapValueUpdate(fval, update);
            } else {
                throw new DeserializationException("MapValueUpdate only works for arrays and weighted sets");
            }
        case REMOVE:
            FieldValue fval = ((CollectionDataType) superType).getNestedType().createFieldValue();
            fval.deserialize(this);
            return new RemoveValueUpdate(fval);
        default:
            throw new DeserializationException("Could not deserialize ValueUpdate, unknown valueUpdateClassID type " + vuTypeId);
    }
}
Also used : MapValueUpdate(com.yahoo.document.update.MapValueUpdate) RemoveValueUpdate(com.yahoo.document.update.RemoveValueUpdate) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) CollectionDataType(com.yahoo.document.CollectionDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) RemoveValueUpdate(com.yahoo.document.update.RemoveValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) 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) ArrayDataType(com.yahoo.document.ArrayDataType) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate)

Aggregations

StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)3 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)3 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)3 Tuple2 (com.yahoo.collections.Tuple2)2 DocumentUpdate (com.yahoo.document.DocumentUpdate)2 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)2 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)2 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)2 FieldUpdate (com.yahoo.document.update.FieldUpdate)2 ValueUpdate (com.yahoo.document.update.ValueUpdate)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ArrayDataType (com.yahoo.document.ArrayDataType)1 CollectionDataType (com.yahoo.document.CollectionDataType)1 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)1 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)1 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)1 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)1 FieldValue (com.yahoo.document.datatypes.FieldValue)1 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)1