Search in sources :

Example 41 with DocumentUpdate

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

the class VespaJsonDocumentReader method createDocumentOperation.

public DocumentOperation createDocumentOperation(DocumentType documentType, DocumentParseInfo documentParseInfo) {
    final DocumentOperation documentOperation;
    try {
        switch(documentParseInfo.operationType) {
            case PUT:
                documentOperation = new DocumentPut(new Document(documentType, documentParseInfo.documentId));
                readPut(documentParseInfo.fieldsBuffer, (DocumentPut) documentOperation);
                verifyEndState(documentParseInfo.fieldsBuffer, JsonToken.END_OBJECT);
                break;
            case REMOVE:
                documentOperation = new DocumentRemove(documentParseInfo.documentId);
                break;
            case UPDATE:
                documentOperation = new DocumentUpdate(documentType, documentParseInfo.documentId);
                readUpdate(documentParseInfo.fieldsBuffer, (DocumentUpdate) documentOperation);
                verifyEndState(documentParseInfo.fieldsBuffer, JsonToken.END_OBJECT);
                break;
            default:
                throw new IllegalStateException("Implementation out of sync with itself. This is a bug.");
        }
    } catch (JsonReaderException e) {
        throw JsonReaderException.addDocId(e, documentParseInfo.documentId);
    }
    if (documentParseInfo.create.isPresent()) {
        if (!(documentOperation instanceof DocumentUpdate)) {
            throw new RuntimeException("Could not set create flag on non update operation.");
        }
        DocumentUpdate update = (DocumentUpdate) documentOperation;
        update.setCreateIfNonExistent(documentParseInfo.create.get());
    }
    return documentOperation;
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) DocumentRemove(com.yahoo.document.DocumentRemove) DocumentUpdate(com.yahoo.document.DocumentUpdate) JsonReaderException(com.yahoo.document.json.JsonReaderException) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document)

Example 42 with DocumentUpdate

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

the class SimpleAdapterFactoryTestCase method requireThatFieldUpdateCanHaveManyPartialUpdatesForOneField.

@Test
public void requireThatFieldUpdateCanHaveManyPartialUpdatesForOneField() {
    DocumentType docType = new DocumentType("my_type");
    DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:foo:1");
    Field field = new Field("my_int", DataType.INT);
    docType.addField(field);
    FieldUpdate fieldUpdate = FieldUpdate.create(field);
    fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(1));
    fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(2));
    fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(4));
    docUpdate.addFieldUpdate(fieldUpdate);
    SimpleAdapterFactory factory = new SimpleAdapterFactory();
    List<UpdateAdapter> adapters = factory.newUpdateAdapterList(docUpdate);
    assertEquals(4, adapters.size());
    UpdateAdapter adapter = adapters.get(0);
    assertNotNull(adapter);
    assertEquals(new IntegerFieldValue(1), adapter.getInputValue("my_int"));
    assertNotNull(adapter = adapters.get(1));
    assertEquals(new IntegerFieldValue(2), adapter.getInputValue("my_int"));
    assertNotNull(adapter = adapters.get(2));
    assertEquals(new IntegerFieldValue(4), adapter.getInputValue("my_int"));
    assertNotNull(adapter = adapters.get(3));
    // always add an adapter for complete updates
    assertNull(adapter.getInputValue("my_int"));
}
Also used : Field(com.yahoo.document.Field) DocumentUpdate(com.yahoo.document.DocumentUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Test(org.junit.Test)

Example 43 with DocumentUpdate

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

the class SimpleAdapterFactoryTestCase method requireThatCompleteUpdatesAreCombined.

@Test
public void requireThatCompleteUpdatesAreCombined() {
    DocumentType docType = new DocumentType("my_type");
    DocumentUpdate update = new DocumentUpdate(docType, "doc:foo:1");
    Field field1 = new Field("int1", DataType.INT);
    Field field2 = new Field("int2", DataType.INT);
    Field field3 = new Field("int3", DataType.INT);
    docType.addField(field1);
    docType.addField(field2);
    docType.addField(field3);
    update.addFieldUpdate(FieldUpdate.createAssign(field1, new IntegerFieldValue(10)));
    update.addFieldUpdate(FieldUpdate.createAssign(field2, new IntegerFieldValue(20)));
    update.addFieldUpdate(FieldUpdate.createIncrement(field3, 30));
    SimpleAdapterFactory factory = new SimpleAdapterFactory();
    List<UpdateAdapter> adapters = factory.newUpdateAdapterList(update);
    assertEquals(2, adapters.size());
}
Also used : Field(com.yahoo.document.Field) DocumentUpdate(com.yahoo.document.DocumentUpdate) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Test(org.junit.Test)

Aggregations

DocumentUpdate (com.yahoo.document.DocumentUpdate)43 Test (org.junit.Test)24 FieldUpdate (com.yahoo.document.update.FieldUpdate)16 DocumentType (com.yahoo.document.DocumentType)15 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)12 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)9 Document (com.yahoo.document.Document)8 DocumentOperation (com.yahoo.document.DocumentOperation)8 DocumentPut (com.yahoo.document.DocumentPut)7 DocumentRemove (com.yahoo.document.DocumentRemove)6 DocumentId (com.yahoo.document.DocumentId)5 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 ValueUpdate (com.yahoo.document.update.ValueUpdate)5 HashMap (java.util.HashMap)5