Search in sources :

Example 1 with AddFieldPathUpdate

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

the class PathUpdateToDocumentTestCase method requireThatAddIsConverted.

@SuppressWarnings({ "unchecked" })
@Test
public void requireThatAddIsConverted() {
    DocumentType docType = new DocumentType("my_type");
    ArrayDataType arrType = DataType.getArray(DataType.INT);
    docType.addField(new Field("my_arr", arrType));
    Array<IntegerFieldValue> arrVal = arrType.createFieldValue();
    arrVal.add(new IntegerFieldValue(6));
    arrVal.add(new IntegerFieldValue(9));
    FieldPathUpdate upd = new AddFieldPathUpdate(docType, "my_arr", "", arrVal);
    Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
    assertNotNull(doc);
    FieldValue obj = doc.getFieldValue("my_arr");
    assertTrue(obj instanceof Array);
    Array arr = (Array) obj;
    assertEquals(2, arr.size());
    assertEquals(new IntegerFieldValue(6), arr.get(0));
    assertEquals(new IntegerFieldValue(9), arr.get(1));
}
Also used : AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate) RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) Test(org.junit.Test)

Example 2 with AddFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testAddAndAssignList.

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

Example 3 with AddFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testAddSerialization.

public void testAddSerialization() throws Exception {
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    Array strArray = new Array(docType.getField("strarray").getDataType());
    strArray.add(new StringFieldValue("hello hello"));
    strArray.add(new StringFieldValue("blah blah"));
    AddFieldPathUpdate add = new AddFieldPathUpdate(docType, "strarray", "", strArray);
    docUp.addFieldPathUpdate(add);
    GrowableByteBuffer buffer = new GrowableByteBuffer();
    docUp.serialize(DocumentSerializerFactory.createHead(buffer));
    buffer.flip();
    DocumentUpdate docUp2 = new DocumentUpdate(DocumentDeserializerFactory.createHead(docMan, buffer));
    assertEquals(docUp, docUp2);
}
Also used : AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer)

Example 4 with AddFieldPathUpdate

use of com.yahoo.document.fieldpathupdate.AddFieldPathUpdate 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)

Example 5 with AddFieldPathUpdate

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

the class VespaJsonDocumentReader method readAddFieldPathUpdate.

private AddFieldPathUpdate readAddFieldPathUpdate(DocumentType documentType, String fieldPath, TokenBuffer buffer) {
    AddFieldPathUpdate fieldPathUpdate = new AddFieldPathUpdate(documentType, fieldPath);
    FieldValue fv = SingleValueReader.readSingleValue(buffer, fieldPathUpdate.getFieldPath().getResultingDataType());
    fieldPathUpdate.setNewValues((Array) fv);
    return fieldPathUpdate;
}
Also used : AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) FieldValue(com.yahoo.document.datatypes.FieldValue)

Aggregations

AddFieldPathUpdate (com.yahoo.document.fieldpathupdate.AddFieldPathUpdate)8 AssignFieldPathUpdate (com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)5 RemoveFieldPathUpdate (com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 FieldValue (com.yahoo.document.datatypes.FieldValue)1 FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)1 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Test (org.junit.Test)1