Search in sources :

Example 1 with RemoveFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testApplyRemoveMultiList.

public void testApplyRemoveMultiList() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    assertNull(doc.getFieldValue("strarray"));
    Array<StringFieldValue> strArray = new Array<>(doc.getField("strarray").getDataType());
    strArray.add(new StringFieldValue("crouching tiger, hidden value"));
    strArray.add(new StringFieldValue("remove val 1"));
    strArray.add(new StringFieldValue("hello hello"));
    doc.setFieldValue("strarray", strArray);
    assertNotNull(doc.getFieldValue("strarray"));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new RemoveFieldPathUpdate(doc.getDataType(), "strarray[$x]", "foobar.strarray[$x] == \"remove val 1\""));
    docUp.applyTo(doc);
    assertEquals(2, ((List) doc.getFieldValue("strarray")).size());
    List docList = (List) doc.getFieldValue("strarray");
    assertEquals(new StringFieldValue("crouching tiger, hidden value"), docList.get(0));
    assertEquals(new StringFieldValue("hello hello"), docList.get(1));
}
Also used : RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with RemoveFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testRemoveSerialization.

public void testRemoveSerialization() throws Exception {
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    RemoveFieldPathUpdate remove = new RemoveFieldPathUpdate(docType, "num", "foobar.num > 0");
    docUp.addFieldPathUpdate(remove);
    GrowableByteBuffer buffer = new GrowableByteBuffer();
    docUp.serialize(DocumentSerializerFactory.createHead(buffer));
    buffer.flip();
    DocumentUpdate docUp2 = new DocumentUpdate(DocumentDeserializerFactory.createHead(docMan, buffer));
    assertEquals(docUp, docUp2);
}
Also used : RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer)

Example 3 with RemoveFieldPathUpdate

use of com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate 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 4 with RemoveFieldPathUpdate

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

the class FieldPathUpdateAdapter method createUpdatesAt.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createUpdatesAt(List<FieldPathEntry> path, FieldValue value, int idx, DocumentUpdate out) {
    FieldPath updatePath = update.getFieldPath();
    if (idx < updatePath.size()) {
        FieldPathEntry pathEntry = updatePath.get(idx);
        FieldPathEntry.Type type = pathEntry.getType();
        if (type == FieldPathEntry.Type.STRUCT_FIELD) {
            if (!(value instanceof StructuredFieldValue)) {
                throw new IllegalArgumentException("Expected structured field value, got " + value.getClass().getName() + ".");
            }
            for (Iterator<Map.Entry<Field, FieldValue>> it = ((StructuredFieldValue) value).iterator(); it.hasNext(); ) {
                Map.Entry<Field, FieldValue> structEntry = it.next();
                List<FieldPathEntry> nextPath = new ArrayList<>(path);
                nextPath.add(FieldPathEntry.newStructFieldEntry(structEntry.getKey()));
                createUpdatesAt(nextPath, structEntry.getValue(), idx + 1, out);
            }
        } else if (type == FieldPathEntry.Type.MAP_KEY) {
            if (value instanceof WeightedSet) {
                WeightedSet wset = (WeightedSet) value;
                for (Iterator<FieldValue> it = wset.fieldValueIterator(); it.hasNext(); ) {
                    FieldValue wsetEntry = it.next();
                    List<FieldPathEntry> nextPath = new ArrayList<>(path);
                    nextPath.add(FieldPathEntry.newMapLookupEntry(wsetEntry, DataType.INT));
                    createUpdatesAt(nextPath, new IntegerFieldValue(wset.get(wsetEntry)), idx + 1, out);
                }
            } else if (value instanceof MapFieldValue) {
                MapFieldValue<FieldValue, FieldValue> map = (MapFieldValue) value;
                for (Map.Entry<FieldValue, FieldValue> entry : map.entrySet()) {
                    List<FieldPathEntry> nextPath = new ArrayList<>(path);
                    FieldValue nextVal = entry.getValue();
                    nextPath.add(FieldPathEntry.newMapLookupEntry(entry.getKey(), nextVal.getDataType()));
                    createUpdatesAt(nextPath, nextVal, idx + 1, out);
                }
            } else {
                throw new IllegalArgumentException("Expected map or weighted set, got " + value.getClass().getName() + ".");
            }
        } else {
            path.add(pathEntry);
            createUpdatesAt(new ArrayList<>(path), value, idx + 1, out);
        }
    } else if (update instanceof AddFieldPathUpdate) {
        if (!(value instanceof Array)) {
            throw new IllegalStateException("Expected array, got " + value.getClass().getName() + ".");
        }
        out.addFieldPathUpdate(new AddFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause(), (Array) value));
    } else if (update instanceof AssignFieldPathUpdate) {
        out.addFieldPathUpdate(new AssignFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause(), value));
    } else if (update instanceof RemoveFieldPathUpdate) {
        out.addFieldPathUpdate(new RemoveFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause()));
    }
}
Also used : AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) Map(java.util.Map)

Example 5 with RemoveFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testRemoveField.

public void testRemoveField() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    assertNull(doc.getFieldValue("strfoo"));
    doc.setFieldValue("strfoo", "cocacola");
    assertEquals(new StringFieldValue("cocacola"), doc.getFieldValue("strfoo"));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new RemoveFieldPathUpdate(doc.getDataType(), "strfoo", null));
    docUp.applyTo(doc);
    assertNull(doc.getFieldValue("strfoo"));
}
Also used : RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate)

Aggregations

RemoveFieldPathUpdate (com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate)9 AddFieldPathUpdate (com.yahoo.document.fieldpathupdate.AddFieldPathUpdate)4 AssignFieldPathUpdate (com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 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