Search in sources :

Example 1 with DocumentPut

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

the class Rot13DocumentProcessor method process.

@Override
public Progress process(Processing processing) {
    int oldVal = counter.getAndIncrement();
    if ((oldVal % 3) != 0) {
        return Progress.LATER;
    }
    for (DocumentOperation op : processing.getDocumentOperations()) {
        if (op instanceof DocumentPut) {
            Document document = ((DocumentPut) op).getDocument();
            StringFieldValue oldTitle = (StringFieldValue) document.getFieldValue(FIELD_NAME);
            if (oldTitle != null) {
                document.setFieldValue(FIELD_NAME, rot13(oldTitle.getString()));
            }
        }
    }
    return Progress.DONE;
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document)

Example 2 with DocumentPut

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

the class JDiscContainerDocprocTest method requireThatLaterDocumentProcessingWorks.

@Test
public void requireThatLaterDocumentProcessingWorks() throws Exception {
    try (Application app = new ApplicationBuilder().servicesXml(getXML(CHAIN_NAME, Rot13DocumentProcessor.class.getCanonicalName())).networking(Networking.disable).documentType("music", DOCUMENT).build()) {
        JDisc container = app.getJDisc("container");
        DocumentProcessing docProc = container.documentProcessing();
        DocumentType type = docProc.getDocumentTypes().get("music");
        ChainRegistry<DocumentProcessor> chains = docProc.getChains();
        assertTrue(chains.allComponentsById().containsKey(new ComponentId(CHAIN_NAME)));
        Document doc = new Document(type, "doc:this:is:a:great:album");
        doc.setFieldValue("title", "Great Album!");
        com.yahoo.docproc.Processing processing;
        DocumentProcessor.Progress progress;
        DocumentPut put = new DocumentPut(doc);
        processing = com.yahoo.docproc.Processing.of(put);
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, instanceOf(DocumentProcessor.LaterProgress.class));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, instanceOf(DocumentProcessor.LaterProgress.class));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Terng Nyohz!"));
    }
}
Also used : Rot13DocumentProcessor(com.yahoo.application.container.docprocs.Rot13DocumentProcessor) Rot13DocumentProcessor(com.yahoo.application.container.docprocs.Rot13DocumentProcessor) DocumentProcessor(com.yahoo.docproc.DocumentProcessor) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) ApplicationBuilder(com.yahoo.application.ApplicationBuilder) Application(com.yahoo.application.Application) ComponentId(com.yahoo.component.ComponentId) Test(org.junit.Test)

Example 3 with DocumentPut

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

the class OperationHandlerImpl method put.

@Override
public void put(RestUri restUri, VespaXMLFeedReader.Operation data, Optional<String> route) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    Response response;
    try {
        Instant startTime = Instant.now();
        DocumentPut put = new DocumentPut(data.getDocument());
        put.setCondition(data.getCondition());
        setRoute(syncSession, route);
        syncSession.put(put);
        metricsHelper.reportSuccessful(DocumentOperationType.PUT, startTime);
        return;
    } catch (DocumentAccessException documentException) {
        response = createErrorResponse(documentException, restUri);
    } catch (Exception e) {
        response = Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.INTERNAL_EXCEPTION);
    } finally {
        syncSessions.free(syncSession);
    }
    metricsHelper.reportFailure(DocumentOperationType.PUT, DocumentOperationStatus.fromHttpStatusCode(response.getStatus()));
    throw new RestApiException(response);
}
Also used : Instant(java.time.Instant) DocumentPut(com.yahoo.document.DocumentPut) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Example 4 with DocumentPut

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

the class DummyVisitorSession method addDocumentReply.

public void addDocumentReply(String docId) {
    Document replyDoc = new Document(documentType, docId);
    autoReplyMessages.add(new PutDocumentMessage(new DocumentPut(replyDoc)));
}
Also used : PutDocumentMessage(com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document)

Example 5 with DocumentPut

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

the class SchemaMappingAndAccessesTest method testMappingArrays.

public void testMappingArrays() {
    Document doc = getDoc();
    DocumentProcessor proc = new TestMappingArrayProcessor();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("label", "labels[0]");
    ProxyDocument mapped = new ProxyDocument(proc, doc, fieldMap);
    Processing p = Processing.of(new DocumentPut(mapped));
    proc.process(p);
    assertEquals(2, ((Array<StringFieldValue>) doc.getFieldValue("labels")).size());
    assertEquals(new StringFieldValue("EMI"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(0));
    assertEquals(new StringFieldValue("tylden"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(1));
    fieldMap.clear();
    fieldMap.put("label", "labels[2]");
    mapped = new ProxyDocument(proc, doc, fieldMap);
    p = Processing.of(new DocumentPut(mapped));
    try {
        proc.process(p);
        fail("Should not have worked");
    } catch (IllegalArgumentException iae) {
    // ok!
    }
    assertEquals(2, ((Array<StringFieldValue>) doc.getFieldValue("labels")).size());
    assertEquals(new StringFieldValue("EMI"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(0));
    assertEquals(new StringFieldValue("tylden"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(1));
}
Also used : DocumentProcessor(com.yahoo.docproc.DocumentProcessor) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document) Processing(com.yahoo.docproc.Processing)

Aggregations

DocumentPut (com.yahoo.document.DocumentPut)67 Document (com.yahoo.document.Document)40 DocumentType (com.yahoo.document.DocumentType)35 Test (org.junit.Test)30 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)23 ByteArrayInputStream (java.io.ByteArrayInputStream)21 InputStream (java.io.InputStream)21 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)16 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)16 DocumentOperation (com.yahoo.document.DocumentOperation)13 DocumentId (com.yahoo.document.DocumentId)12 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 FieldValue (com.yahoo.document.datatypes.FieldValue)10 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)10 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)10 DocumentRemove (com.yahoo.document.DocumentRemove)8 DocumentUpdate (com.yahoo.document.DocumentUpdate)7 Array (com.yahoo.document.datatypes.Array)7 DocumentProcessor (com.yahoo.docproc.DocumentProcessor)6 PutDocumentMessage (com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage)5