Search in sources :

Example 6 with AddFieldPathUpdate

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

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

the class DocumentPathUpdateTestCase method testApplyAddMultiList.

public void testApplyAddMultiList() throws Exception {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    assertNull(doc.getFieldValue("strarray"));
    Array<StringFieldValue> addList = new Array<StringFieldValue>(doc.getField("strarray").getDataType());
    addList.add(new StringFieldValue("bo"));
    addList.add(new StringFieldValue("ba"));
    addList.add(new StringFieldValue("by"));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AddFieldPathUpdate(doc.getDataType(), "strarray", "", addList));
    docUp.applyTo(doc);
    List<StringFieldValue> values = new ArrayList<>();
    values.add(new StringFieldValue("bo"));
    values.add(new StringFieldValue("ba"));
    values.add(new StringFieldValue("by"));
    assertEquals(values, doc.getFieldValue("strarray"));
}
Also used : AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) ArrayList(java.util.ArrayList)

Example 8 with AddFieldPathUpdate

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

the class DocumentPathUpdateTestCase method createDocumentUpdateForSerialization.

private DocumentUpdate createDocumentUpdateForSerialization() {
    docMan = DocumentTestCase.setUpCppDocType();
    docType = docMan.getDocumentType("serializetest");
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:serialization:xlanguage"));
    AssignFieldPathUpdate ass = new AssignFieldPathUpdate(docType, "intfield", "", "3");
    ass.setCreateMissingPath(false);
    ass.setRemoveIfZero(true);
    docUp.addFieldPathUpdate(ass);
    Array fArray = new Array(docType.getField("arrayoffloatfield").getDataType());
    fArray.add(new FloatFieldValue(12.0f));
    fArray.add(new FloatFieldValue(5.0f));
    AddFieldPathUpdate add = new AddFieldPathUpdate(docType, "arrayoffloatfield", "", fArray);
    docUp.addFieldPathUpdate(add);
    RemoveFieldPathUpdate remove = new RemoveFieldPathUpdate(docType, "intfield", "serializetest.intfield > 0");
    docUp.addFieldPathUpdate(remove);
    return docUp;
}
Also used : RemoveFieldPathUpdate(com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate) AddFieldPathUpdate(com.yahoo.document.fieldpathupdate.AddFieldPathUpdate) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

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