Search in sources :

Example 1 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class DocumentUpdateTestCase method requireThatArrayOfStructElementAddIsProcessedCorrectly.

@Test
public void requireThatArrayOfStructElementAddIsProcessedCorrectly() throws ParseException {
    DocumentType docType = new DocumentType("my_input");
    docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
    docType.addField(new Field("my_pos", DataType.getArray(PositionDataType.INSTANCE)));
    DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
    docUpdate.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("6;9")));
    docUpdate = Expression.execute(Expression.fromString("input my_str | for_each { to_pos } | index my_pos"), docUpdate);
    assertNotNull(docUpdate);
    assertEquals(0, docUpdate.getFieldPathUpdates().size());
    assertEquals(1, docUpdate.getFieldUpdates().size());
    FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
    assertNotNull(fieldUpd);
    assertEquals(docType.getField("my_pos"), fieldUpd.getField());
    assertEquals(1, fieldUpd.getValueUpdates().size());
    ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
    assertNotNull(valueUpd);
    assertTrue(valueUpd instanceof AddValueUpdate);
    Object val = valueUpd.getValue();
    assertNotNull(val);
    assertTrue(val instanceof Struct);
    Struct pos = (Struct) val;
    assertEquals(PositionDataType.INSTANCE, pos.getDataType());
    assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(pos));
    assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(pos));
}
Also used : AddValueUpdate(com.yahoo.document.update.AddValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 2 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class GuardTestCase method requireThatInputFieldsAreIncludedByUpdate.

@Test
public void requireThatInputFieldsAreIncludedByUpdate() throws ParseException {
    DocumentType docType = new DocumentType("my_input");
    docType.addField(new Field("my_lng", DataType.LONG));
    docType.addField(new Field("my_str", DataType.STRING));
    DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
    docUpdate.addFieldUpdate(FieldUpdate.createAssign(docType.getField("my_str"), new StringFieldValue("69")));
    assertNotNull(docUpdate = Expression.execute(Expression.fromString("guard { input my_str | to_int | attribute my_lng }"), docUpdate));
    assertEquals(0, docUpdate.getFieldPathUpdates().size());
    assertEquals(1, docUpdate.getFieldUpdates().size());
    FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
    assertNotNull(fieldUpd);
    assertEquals(docType.getField("my_lng"), fieldUpd.getField());
    assertEquals(1, fieldUpd.getValueUpdates().size());
    ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
    assertNotNull(valueUpd);
    assertTrue(valueUpd instanceof AssignValueUpdate);
    assertEquals(new LongFieldValue(69), valueUpd.getValue());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Test(org.junit.Test)

Example 3 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDocUpdateAPI.

public void testMappedDocUpdateAPI() {
    Document doc = getDoc();
    DocumentType type = doc.getDataType();
    DocumentUpdate dud = new DocumentUpdate(type, new DocumentId("doc:map:test:1"));
    FieldUpdate assignSingle = FieldUpdate.createAssign(type.getField("title"), new StringFieldValue("something"));
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    ProxyDocumentUpdate pup = new ProxyDocumentUpdate(dud, fieldMap);
    pup.addFieldUpdate(assignSingle);
    assertEquals(pup.getFieldUpdates(), dud.getFieldUpdates());
    assertEquals(pup.getDocumentType(), dud.getDocumentType());
    assertEquals(pup.getFieldUpdate(new com.yahoo.document.Field("title")).size(), 1);
    assertEquals(pup.getFieldUpdate(0), dud.getFieldUpdate(0));
    assertEquals(pup.getFieldUpdate("title"), dud.getFieldUpdate("title"));
    assertEquals(pup.getId(), dud.getId());
    assertEquals(pup.getType(), dud.getType());
    assertEquals(pup.applyTo(doc), dud);
    assertEquals(doc.getFieldValue("title").getWrappedValue(), "something");
    assertEquals(pup, dud);
    assertEquals(pup.hashCode(), dud.hashCode());
    assertEquals(pup.toString(), dud.toString());
    assertEquals(pup.size(), dud.size());
    assertEquals(pup.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
}
Also used : HashMap(java.util.HashMap) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 4 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class DocumentUpdate method addFieldUpdate.

/**
 * Adds the given {@link FieldUpdate} to this DocumentUpdate. If this DocumentUpdate already contains a FieldUpdate
 * for the named field, the content of the given FieldUpdate is added to the existing one.
 *
 * @param update The FieldUpdate to add to this DocumentUpdate.
 * @return This, to allow chaining.
 * @throws IllegalArgumentException If the {@link DocumentType} of this DocumentUpdate does not have a corresponding
 *                                  field.
 */
public DocumentUpdate addFieldUpdate(FieldUpdate update) {
    String fieldName = update.getField().getName();
    if (!documentType.hasField(fieldName)) {
        throw new IllegalArgumentException("Document type '" + documentType.getName() + "' does not have field '" + fieldName + "'.");
    }
    FieldUpdate prevUpdate = getFieldUpdate(fieldName);
    if (prevUpdate != update) {
        if (prevUpdate != null) {
            prevUpdate.addAll(update);
        } else {
            fieldUpdates.add(update);
        }
    }
    return this;
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 5 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate 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)

Aggregations

FieldUpdate (com.yahoo.document.update.FieldUpdate)37 Test (org.junit.Test)17 DocumentUpdate (com.yahoo.document.DocumentUpdate)16 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)15 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)14 ValueUpdate (com.yahoo.document.update.ValueUpdate)12 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)8 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 HashMap (java.util.HashMap)5 DocumentId (com.yahoo.document.DocumentId)4 DocumentType (com.yahoo.document.DocumentType)4 FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)4 ArrayList (java.util.ArrayList)4 Field (com.yahoo.document.Field)3 Array (com.yahoo.document.datatypes.Array)3 Struct (com.yahoo.document.datatypes.Struct)3 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)3