Search in sources :

Example 21 with AssignFieldPathUpdate

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

the class VespaJsonDocumentReader method readArithmeticFieldPathUpdate.

private AssignFieldPathUpdate readArithmeticFieldPathUpdate(DocumentType documentType, String fieldPath, TokenBuffer buffer, String fieldPathOperation) {
    AssignFieldPathUpdate fieldPathUpdate = new AssignFieldPathUpdate(documentType, fieldPath);
    String arithmeticSign = SingleValueReader.UPDATE_OPERATION_TO_ARITHMETIC_SIGN.get(fieldPathOperation);
    double value = Double.valueOf(buffer.currentText());
    String expression = String.format("$value %s %s", arithmeticSign, value);
    fieldPathUpdate.setExpression(expression);
    return fieldPathUpdate;
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 22 with AssignFieldPathUpdate

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

the class VespaJsonDocumentReader method readAssignFieldPathUpdate.

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

Example 23 with AssignFieldPathUpdate

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

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

the class DocumentPathUpdateTestCase method testKeyWithEscapedChars.

public void testKeyWithEscapedChars() {
    Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
    MapFieldValue mfv = new MapFieldValue((MapDataType) doc.getField("strmap").getDataType());
    mfv.put(new StringFieldValue("here is a \"fancy\" :-} map key :-{"), new StringFieldValue("bar"));
    mfv.put(new StringFieldValue("baz"), new StringFieldValue("bananas"));
    doc.setFieldValue("strmap", mfv);
    // Select on map value, not key
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:hargl:bargl"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "strmap{\"here is a \\\"fancy\\\" :-} map key :-{\"}", "", new StringFieldValue("shinyvalue")));
    docUp.applyTo(doc);
    MapFieldValue valueNow = (MapFieldValue) doc.getFieldValue("strmap");
    assertEquals(2, valueNow.size());
    assertEquals(new StringFieldValue("shinyvalue"), valueNow.get(new StringFieldValue("here is a \"fancy\" :-} map key :-{")));
    assertEquals(new StringFieldValue("bananas"), valueNow.get(new StringFieldValue("baz")));
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)

Example 25 with AssignFieldPathUpdate

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

the class DocumentPathUpdateTestCase method testAssignMap.

public void testAssignMap() 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("tor brede"));
    fv4.setFieldValue("rating", new IntegerFieldValue(48));
    DocumentUpdate docUp = new DocumentUpdate(docType, new DocumentId("doc:foo:bar"));
    docUp.addFieldPathUpdate(new AssignFieldPathUpdate(doc.getDataType(), "structmap{bar}", "", fv4));
    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)

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