Search in sources :

Example 16 with AssignFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testAssignMathFieldNotSet.

public void testAssignMathFieldNotSet() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    doc.setFieldValue(doc.getField("num"), new IntegerFieldValue(10));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "num", "", "100 + foobar.num2"));
    docUp.applyTo(doc);
    assertEquals(new IntegerFieldValue(10), doc.getFieldValue(doc.getField("num")));
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 17 with AssignFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testAssignMapStruct.

public void testAssignMapStruct() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    MapFieldValue mfv = new MapFieldValue((MapDataType) doc.getField("structmap").getDataType());
    Struct fv1 = new Struct(mfv.getDataType().getValueType());
    fv1.setFieldValue("title", new StringFieldValue("thomas"));
    fv1.setFieldValue("rating", new IntegerFieldValue(32));
    mfv.put(new StringFieldValue("foo"), fv1);
    Struct fv2 = new Struct(mfv.getDataType().getValueType());
    fv2.setFieldValue("title", new StringFieldValue("cyril"));
    fv2.setFieldValue("rating", new IntegerFieldValue(16));
    mfv.put(new StringFieldValue("bar"), fv2);
    Struct fv3 = new Struct(mfv.getDataType().getValueType());
    fv3.setFieldValue("title", new StringFieldValue("ulf"));
    fv3.setFieldValue("rating", new IntegerFieldValue(8));
    mfv.put(new StringFieldValue("zoo"), fv3);
    doc.setFieldValue("structmap", mfv);
    Struct fv4 = new Struct(mfv.getDataType().getValueType());
    fv4.setFieldValue("title", new StringFieldValue("cyril"));
    fv4.setFieldValue("rating", new IntegerFieldValue(48));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "structmap{bar}.rating", "", new IntegerFieldValue(48)));
    docUp.applyTo(doc);
    MapFieldValue valueNow = (MapFieldValue) doc.getFieldValue("structmap");
    assertEquals(fv1, valueNow.get(new StringFieldValue("foo")));
    assertEquals(fv4, valueNow.get(new StringFieldValue("bar")));
    assertEquals(fv3, valueNow.get(new StringFieldValue("zoo")));
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 18 with AssignFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testApplyAssignMultiList.

public void testApplyAssignMultiList() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    assertNull(doc.getFieldValue("strarray"));
    Array<StringFieldValue> strArray = new Array<StringFieldValue>(doc.getField("strarray").getDataType());
    strArray.add(new StringFieldValue("hello hello"));
    strArray.add(new StringFieldValue("blah blah"));
    doc.setFieldValue("strarray", strArray);
    assertNotNull(doc.getFieldValue("strarray"));
    Array<StringFieldValue> array = new Array<>(doc.getField("strarray").getDataType());
    array.add(new StringFieldValue("assigned val 0"));
    array.add(new StringFieldValue("assigned val 1"));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "strarray", "", array));
    docUp.applyTo(doc);
    assertEquals(2, ((List) doc.getFieldValue("strarray")).size());
    List docList = (List) doc.getFieldValue("strarray");
    assertEquals(new StringFieldValue("assigned val 0"), docList.get(0));
    assertEquals(new StringFieldValue("assigned val 1"), docList.get(1));
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 19 with AssignFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testApplyAssignMultiWlist.

public void testApplyAssignMultiWlist() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    assertNull(doc.getFieldValue("strwset"));
    WeightedSet<StringFieldValue> strwset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
    strwset.put(new StringFieldValue("hello hello"), 164);
    strwset.put(new StringFieldValue("blahdi blahdi"), 243);
    doc.setFieldValue("strwset", strwset);
    assertNotNull(doc.getFieldValue("strwset"));
    WeightedSet<StringFieldValue> assignWset = new WeightedSet<>(docType.getField("strwset").getDataType());
    assignWset.put(new StringFieldValue("assigned val 0"), 5);
    assignWset.put(new StringFieldValue("assigned val 1"), 10);
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "strwset", "", assignWset));
    docUp.applyTo(doc);
    assertEquals(2, ((WeightedSet) doc.getFieldValue("strwset")).size());
    WeightedSet docWset = (WeightedSet) doc.getFieldValue("strwset");
    assertEquals(new Integer(5), docWset.get(new StringFieldValue("assigned val 0")));
    assertEquals(new Integer(10), docWset.get(new StringFieldValue("assigned val 1")));
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 20 with AssignFieldPathUpdate

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

the class VespaXMLUpdateReader method read.

public void read(DocumentUpdate update) {
    try {
        // First fetch attributes.
        DocumentType doctype = null;
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            final String attributeName = reader.getAttributeName(i).toString();
            final String attributeValue = reader.getAttributeValue(i);
            if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
                update.setId(new DocumentId(attributeValue));
            } else if ("documenttype".equals(attributeName) || "type".equals(attributeName)) {
                doctype = docTypeManager.getDocumentType(attributeValue);
                update.setDocumentType(doctype);
            } else if ("create-if-non-existent".equals(attributeName)) {
                if ("true".equals(attributeValue)) {
                    update.setCreateIfNonExistent(true);
                } else if ("false".equals(attributeValue)) {
                    update.setCreateIfNonExistent(false);
                } else {
                    throw newDeserializeException("'create-if-non-existent' must be either 'true' or 'false', was '" + attributeValue + "'");
                }
            } else if ("condition".equals(attributeName)) {
                condition = Optional.of(attributeValue);
            }
        }
        if (doctype == null) {
            throw newDeserializeException("Must specify document type. " + reader.getLocation());
        }
        // Then fetch fields
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                final String currentName = reader.getName().toString();
                if (hasFieldPath()) {
                    if ("assign".equals(currentName)) {
                        update.addFieldPathUpdate(new AssignFieldPathUpdate(doctype, this));
                        skipToEnd("assign");
                    } else if ("add".equals(currentName)) {
                        update.addFieldPathUpdate(new AddFieldPathUpdate(doctype, this));
                        skipToEnd("add");
                    } else if ("remove".equals(currentName)) {
                        update.addFieldPathUpdate(new RemoveFieldPathUpdate(doctype, this));
                        skipToEnd("remove");
                    } else {
                        throw newDeserializeException("Unknown field path update operation " + reader.getName());
                    }
                } else {
                    if ("assign".equals(currentName)) {
                        update.addFieldUpdate(readAssign(update));
                        skipToEnd("assign");
                    } else if ("add".equals(currentName)) {
                        update.addFieldUpdate(readAdd(update));
                        skipToEnd("add");
                    } else if ("remove".equals(currentName)) {
                        update.addFieldUpdate(readRemove(update));
                        skipToEnd("remove");
                    } else if ("alter".equals(currentName)) {
                        update.addFieldUpdate(readAlter(update));
                        skipToEnd("alter");
                    } else if ("increment".equals(currentName) || "decrement".equals(currentName) || "multiply".equals(currentName) || "divide".equals(currentName)) {
                        update.addFieldUpdate(readArithmeticField(update, currentName));
                        skipToEnd(currentName);
                    } else {
                        throw newDeserializeException("Unknown update operation " + reader.getName());
                    }
                }
            } else if (type == XMLStreamReader.END_ELEMENT) {
                return;
            }
        }
    } catch (XMLStreamException e) {
        throw newException(e);
    }
}
Also used : RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) XMLStreamException(javax.xml.stream.XMLStreamException) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Aggregations

AssignFieldPathUpdate (com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)35 AddFieldPathUpdate (com.yahoo.document.fieldpathupdate.AddFieldPathUpdate)9 FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)9 Test (org.junit.Test)9 RemoveFieldPathUpdate (com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate)8 FieldValue (com.yahoo.document.datatypes.FieldValue)3 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)3 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DocumentType (com.yahoo.document.DocumentType)2 DocumentUpdate (com.yahoo.document.DocumentUpdate)2 Struct (com.yahoo.document.datatypes.Struct)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1