Search in sources :

Example 6 with FieldUpdate

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

the class ProcessingUpdateTestCase method testProcessingUpdates.

public void testProcessingUpdates() {
    DocumentType articleType = new DocumentType("article");
    articleType.addField(new Field("body", DataType.STRING, true));
    articleType.addField(new Field("title", DataType.STRING, true));
    dtm = new DocumentTypeManager();
    dtm.registerDocumentType(articleType);
    put = new DocumentPut(articleType, "doc:banana:apple");
    put.getDocument().setFieldValue("body", "this is the body of the article, blah blah blah");
    FieldUpdate upd = FieldUpdate.createAssign(articleType.getField("body"), new StringFieldValue("this is the updated body of the article, blahdi blahdi blahdi"));
    update = new DocumentUpdate(articleType, new DocumentId("doc:grape:orange"));
    update.addFieldUpdate(upd);
    DocprocService service = new DocprocService("update");
    DocumentProcessor firstP = new TitleDocumentProcessor();
    service.setCallStack(new CallStack().addLast(firstP));
    service.setInService(true);
    Processing p = new Processing();
    p.addDocumentOperation(put);
    p.addDocumentOperation(update);
    service.process(p);
    while (service.doWork()) {
    }
    List<DocumentOperation> operations = p.getDocumentOperations();
    Document first = ((DocumentPut) operations.get(0)).getDocument();
    assertEquals(new StringFieldValue("this is the body of the article, blah blah blah"), first.getFieldValue("body"));
    assertEquals(new StringFieldValue("body blah blah blah "), first.getFieldValue("title"));
    DocumentUpdate second = (DocumentUpdate) operations.get(1);
    FieldUpdate firstUpd = second.getFieldUpdate(0);
    assertEquals(ValueUpdate.ValueUpdateClassID.ASSIGN, firstUpd.getValueUpdate(0).getValueUpdateClassID());
    assertEquals(new StringFieldValue("this is the updated body of the article, blahdi blahdi blahdi"), firstUpd.getValueUpdate(0).getValue());
    FieldUpdate secondUpd = second.getFieldUpdate(1);
    assertEquals(ValueUpdate.ValueUpdateClassID.ASSIGN, secondUpd.getValueUpdate(0).getValueUpdateClassID());
    assertEquals(new StringFieldValue("body blahdi blahdi blahdi "), secondUpd.getValueUpdate(0).getValue());
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) DocumentPut(com.yahoo.document.DocumentPut) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Field(com.yahoo.document.Field) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 7 with FieldUpdate

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

the class JsonReaderTestCase method readClearField.

@Test
public final void readClearField() {
    InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"update\": \"id:unittest:smoke::whee\"," + " \"fields\": { \"int1\": {" + " \"assign\": null }}" + " }"));
    JsonReader r = new JsonReader(types, rawDoc, parserFactory);
    DocumentUpdate doc = (DocumentUpdate) r.readSingleDocument(DocumentParser.SupportedOperation.UPDATE, "id:unittest:smoke::whee");
    FieldUpdate f = doc.getFieldUpdate("int1");
    assertEquals(1, f.size());
    assertTrue(f.getValueUpdate(0) instanceof ClearValueUpdate);
    assertNull(f.getValueUpdate(0).getValue());
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) Test(org.junit.Test)

Example 8 with FieldUpdate

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

the class JsonReaderTestCase method testUpdateWeighted.

@Test
public final void testUpdateWeighted() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"add\": {" + " \"person\": 37," + " \"another person\": 41}}}}");
    Map<String, Integer> weights = new HashMap<>();
    FieldUpdate x = doc.getFieldUpdate("actualset");
    for (ValueUpdate<?> v : x.getValueUpdates()) {
        AddValueUpdate adder = (AddValueUpdate) v;
        final String s = ((StringFieldValue) adder.getValue()).getString();
        weights.put(s, adder.getWeight());
    }
    assertEquals(2, weights.size());
    final String o = "person";
    final String o2 = "another person";
    assertTrue(weights.containsKey(o));
    assertTrue(weights.containsKey(o2));
    assertEquals(Integer.valueOf(37), weights.get(o));
    assertEquals(Integer.valueOf(41), weights.get(o2));
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) Test(org.junit.Test)

Example 9 with FieldUpdate

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

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

the class JsonReaderTestCase method testOldAssignToArray.

@Test
public final void testOldAssignToArray() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": {" + " \"assign\": [" + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}}}");
    FieldUpdate f = doc.getFieldUpdate("actualMapStringToArrayOfInt");
    assertEquals(1, f.size());
    AssignValueUpdate assign = (AssignValueUpdate) f.getValueUpdate(0);
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) assign.getValue();
    Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
    assertEquals(3, a.size());
    assertEquals(new IntegerFieldValue(1), a.get(0));
    assertEquals(new IntegerFieldValue(2), a.get(1));
    assertEquals(new IntegerFieldValue(3), a.get(2));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Array(com.yahoo.document.datatypes.Array) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) Test(org.junit.Test)

Aggregations

FieldUpdate (com.yahoo.document.update.FieldUpdate)37 Test (org.junit.Test)17 DocumentUpdate (com.yahoo.document.DocumentUpdate)16 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)15 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)14 ValueUpdate (com.yahoo.document.update.ValueUpdate)12 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)8 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 HashMap (java.util.HashMap)5 DocumentId (com.yahoo.document.DocumentId)4 DocumentType (com.yahoo.document.DocumentType)4 FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)4 ArrayList (java.util.ArrayList)4 Field (com.yahoo.document.Field)3 Array (com.yahoo.document.datatypes.Array)3 Struct (com.yahoo.document.datatypes.Struct)3 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)3