Search in sources :

Example 6 with ValueUpdate

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

the class ValueUpdateToDocumentTestCase method requireThatStringFieldsAreConverted.

@Test
public void requireThatStringFieldsAreConverted() {
    DocumentType docType = new DocumentType("my_type");
    Field field = new Field("my_str", DataType.STRING);
    docType.addField(field);
    ValueUpdate update = ValueUpdate.createAssign(new StringFieldValue("42"));
    Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("doc:foo:1"), field, update);
    assertNotNull(doc);
    assertEquals("42", ((StringFieldValue) doc.getFieldValue("my_str")).getString());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) Test(org.junit.Test)

Example 7 with ValueUpdate

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

the class SimpleAdapterFactory method newUpdateAdapterList.

@Override
public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
    List<UpdateAdapter> ret = new ArrayList<>();
    DocumentType docType = upd.getDocumentType();
    DocumentId docId = upd.getId();
    Document complete = new Document(docType, upd.getId());
    for (FieldPathUpdate fieldUpd : upd) {
        if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
            FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
        } else {
            Document partial = FieldPathUpdateHelper.newPartialDocument(docId, fieldUpd);
            ret.add(new FieldPathUpdateAdapter(newDocumentAdapter(partial, true), fieldUpd));
        }
    }
    for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
        Field field = fieldUpd.getField();
        for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
            if (FieldUpdateHelper.isComplete(field, valueUpd)) {
                FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
            } else {
                Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
                ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()), newDocumentAdapter(partial, true), valueUpd));
            }
        }
    }
    ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
    return ret;
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) ArrayList(java.util.ArrayList) FieldUpdate(com.yahoo.document.update.FieldUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate)

Example 8 with ValueUpdate

use of com.yahoo.document.update.ValueUpdate 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 9 with ValueUpdate

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

the class JsonReaderTestCase method testArrayIndexing.

@SuppressWarnings("rawtypes")
@Test
public final void testArrayIndexing() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testarray::whee\"," + " \"fields\": { " + "\"actualarray\": {" + " \"match\": {" + " \"element\": 3," + " \"assign\": \"nalle\"}}}}");
    Map<Number, String> matches = new HashMap<>();
    FieldUpdate x = doc.getFieldUpdate("actualarray");
    for (ValueUpdate v : x.getValueUpdates()) {
        MapValueUpdate adder = (MapValueUpdate) v;
        final Number key = ((IntegerFieldValue) adder.getValue()).getNumber();
        String op = ((StringFieldValue) ((AssignValueUpdate) adder.getUpdate()).getValue()).getString();
        matches.put(key, op);
    }
    assertEquals(1, matches.size());
    Number n = Integer.valueOf(3);
    assertEquals("nalle", matches.get(n));
}
Also used : 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) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Test(org.junit.Test)

Example 10 with ValueUpdate

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

the class DocumentUpdateTestCase method testRequireThatAddAllCombinesFieldUpdates.

public void testRequireThatAddAllCombinesFieldUpdates() {
    DocumentType docType = new DocumentType("my_type");
    Field field = new Field("my_int", DataType.INT);
    docType.addField(field);
    FieldUpdate fooField = FieldUpdate.createAssign(field, new IntegerFieldValue(1));
    DocumentUpdate fooUpdate = new DocumentUpdate(docType, new DocumentId("doc:foo:"));
    fooUpdate.addFieldUpdate(fooField);
    FieldUpdate barField = FieldUpdate.createAssign(field, new IntegerFieldValue(2));
    DocumentUpdate barUpdate = new DocumentUpdate(docType, new DocumentId("doc:foo:"));
    barUpdate.addFieldUpdate(barField);
    fooUpdate.addAll(barUpdate);
    assertEquals(1, fooUpdate.getFieldUpdates().size());
    FieldUpdate fieldUpdate = fooUpdate.getFieldUpdate(0);
    assertNotNull(fieldUpdate);
    assertEquals(field, fieldUpdate.getField());
    assertEquals(2, fieldUpdate.getValueUpdates().size());
    ValueUpdate valueUpdate = fieldUpdate.getValueUpdate(0);
    assertNotNull(valueUpdate);
    assertTrue(valueUpdate instanceof AssignValueUpdate);
    assertEquals(new IntegerFieldValue(1), valueUpdate.getValue());
    assertNotNull(valueUpdate = fieldUpdate.getValueUpdate(1));
    assertTrue(valueUpdate instanceof AssignValueUpdate);
    assertEquals(new IntegerFieldValue(2), valueUpdate.getValue());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate)

Aggregations

ValueUpdate (com.yahoo.document.update.ValueUpdate)21 Test (org.junit.Test)16 FieldUpdate (com.yahoo.document.update.FieldUpdate)12 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)10 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)8 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)7 DocumentUpdate (com.yahoo.document.DocumentUpdate)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)3 Struct (com.yahoo.document.datatypes.Struct)3 FieldValue (com.yahoo.document.datatypes.FieldValue)2 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)2 DocumentSerializer (com.yahoo.document.serialization.DocumentSerializer)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)2 HashMap (java.util.HashMap)2 Tuple2 (com.yahoo.collections.Tuple2)1 ArrayDataType (com.yahoo.document.ArrayDataType)1 CollectionDataType (com.yahoo.document.CollectionDataType)1