Search in sources :

Example 6 with FieldPathUpdate

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

the class PathUpdateToDocumentTestCase method requireThatNestedStructsAreConverted.

@Test
public void requireThatNestedStructsAreConverted() {
    DocumentType docType = new DocumentType("my_type");
    StructDataType structType = new StructDataType("my_struct");
    structType.addField(new Field("b", DataType.INT));
    docType.addField(new Field("a", structType));
    FieldPathUpdate upd = new AssignFieldPathUpdate(docType, "a.b", "", new IntegerFieldValue(69));
    Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
    assertNotNull(doc);
    FieldValue obj = doc.getFieldValue("a");
    assertTrue(obj instanceof Struct);
    Struct struct = (Struct) obj;
    assertEquals(new IntegerFieldValue(69), struct.getFieldValue("b"));
}
Also used : AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) 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 7 with FieldPathUpdate

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

the class SimpleAdapterFactory method newUpdateAdapterList.

@Override
public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
    List<UpdateAdapter> ret = new ArrayList<>();
    DocumentType docType = upd.getDocumentType();
    DocumentId docId = upd.getId();
    Document complete = new Document(docType, upd.getId());
    for (FieldPathUpdate fieldUpd : upd) {
        if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
            FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
        } else {
            Document partial = FieldPathUpdateHelper.newPartialDocument(docId, fieldUpd);
            ret.add(new FieldPathUpdateAdapter(newDocumentAdapter(partial, true), fieldUpd));
        }
    }
    for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
        Field field = fieldUpd.getField();
        for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
            if (FieldUpdateHelper.isComplete(field, valueUpd)) {
                FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
            } else {
                Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
                ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()), newDocumentAdapter(partial, true), valueUpd));
            }
        }
    }
    ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
    return ret;
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) ArrayList(java.util.ArrayList) FieldUpdate(com.yahoo.document.update.FieldUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate)

Example 8 with FieldPathUpdate

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

the class VespaJsonDocumentReader method addFieldPathUpdates.

private void addFieldPathUpdates(DocumentUpdate update, TokenBuffer buffer, String fieldPath) {
    int localNesting = buffer.nesting();
    buffer.next();
    while (localNesting <= buffer.nesting()) {
        String fieldPathOperation = buffer.currentName().toLowerCase();
        FieldPathUpdate fieldPathUpdate;
        if (fieldPathOperation.equals(UPDATE_ASSIGN)) {
            fieldPathUpdate = readAssignFieldPathUpdate(update.getType(), fieldPath, buffer);
        } else if (fieldPathOperation.equals(UPDATE_ADD)) {
            fieldPathUpdate = readAddFieldPathUpdate(update.getType(), fieldPath, buffer);
        } else if (fieldPathOperation.equals(UPDATE_REMOVE)) {
            fieldPathUpdate = readRemoveFieldPathUpdate(update.getType(), fieldPath, buffer);
        } else if (SingleValueReader.UPDATE_OPERATION_TO_ARITHMETIC_SIGN.containsKey(fieldPathOperation)) {
            fieldPathUpdate = readArithmeticFieldPathUpdate(update.getType(), fieldPath, buffer, fieldPathOperation);
        } else {
            throw new IllegalArgumentException("Field path update type '" + fieldPathOperation + "' not supported.");
        }
        update.addFieldPathUpdate(fieldPathUpdate);
        buffer.next();
    }
}
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)

Example 9 with FieldPathUpdate

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

the class DocumentUpdate method toString.

@Override
public String toString() {
    StringBuilder string = new StringBuilder();
    string.append("update of document '");
    string.append(docId);
    string.append("': ");
    string.append("create-if-non-existent=");
    string.append(createIfNonExistent.orElse(false));
    string.append(": ");
    string.append("[");
    for (Iterator<FieldUpdate> i = fieldUpdates.iterator(); i.hasNext(); ) {
        FieldUpdate fieldUpdate = i.next();
        string.append(fieldUpdate);
        if (i.hasNext()) {
            string.append(", ");
        }
    }
    string.append("]");
    if (fieldPathUpdates.size() > 0) {
        string.append(" [ ");
        for (FieldPathUpdate up : fieldPathUpdates) {
            string.append(up.toString() + " ");
        }
        string.append(" ]");
    }
    return string.toString();
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate)

Example 10 with FieldPathUpdate

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

the class DocumentUpdateTestCase method testFieldUpdatesInDocUp.

public void testFieldUpdatesInDocUp() throws ParseException {
    DocumentType t1 = new DocumentType("doo");
    Field f1 = new Field("field1", DataType.STRING);
    Field f2 = new Field("field2", DataType.STRING);
    t1.addField(f1);
    t1.addField(f2);
    DocumentUpdate documentUpdate = new DocumentUpdate(t1, new DocumentId("doc:this:is:a:test"));
    assertEquals(0, documentUpdate.size());
    FieldUpdate fu1 = FieldUpdate.createAssign(f1, new StringFieldValue("banana"));
    FieldUpdate fu2 = FieldUpdate.createAssign(f2, new StringFieldValue("apple"));
    documentUpdate.addFieldUpdate(fu1);
    assertEquals(1, documentUpdate.size());
    documentUpdate.clearFieldUpdates();
    assertEquals(0, documentUpdate.size());
    documentUpdate.addFieldUpdate(fu1);
    documentUpdate.addFieldUpdate(fu2);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(f1));
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    assertSame(fu2, documentUpdate.getFieldUpdate(1));
    documentUpdate.setFieldUpdate(0, fu2);
    documentUpdate.setFieldUpdate(1, fu1);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(1));
    assertSame(fu2, documentUpdate.getFieldUpdate(0));
    try {
        documentUpdate.setFieldUpdates(null);
        fail("Should have gotten NullPointerException");
    } catch (NullPointerException npe) {
    // ok!
    }
    List<FieldUpdate> fus = new ArrayList<>();
    fus.add(fu1);
    fus.add(fu2);
    documentUpdate.setFieldUpdates(fus);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    assertSame(fu2, documentUpdate.getFieldUpdate(1));
    documentUpdate.removeFieldUpdate(1);
    assertEquals(1, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    documentUpdate.toString();
    assertFalse(documentUpdate.isEmpty());
    Iterator<FieldPathUpdate> fpUpdates = documentUpdate.iterator();
    assertFalse(fpUpdates.hasNext());
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) ArrayList(java.util.ArrayList) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate)

Aggregations

FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)16 AssignFieldPathUpdate (com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate)13 Test (org.junit.Test)11 AddFieldPathUpdate (com.yahoo.document.fieldpathupdate.AddFieldPathUpdate)9 RemoveFieldPathUpdate (com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate)8 FieldUpdate (com.yahoo.document.update.FieldUpdate)4 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)3 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)3 FieldValue (com.yahoo.document.datatypes.FieldValue)2 Struct (com.yahoo.document.datatypes.Struct)2 ArrayList (java.util.ArrayList)2 ValueUpdate (com.yahoo.document.update.ValueUpdate)1