Search in sources :

Example 51 with Document

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

the class AbstractDocumentApiTestCase method requireThatAsyncHandlerWorks.

@Test
public void requireThatAsyncHandlerWorks() throws InterruptedException {
    MyHandler handler = new MyHandler();
    AsyncSession session = access().createAsyncSession(new AsyncParameters().setResponseHandler(handler));
    DocumentType type = access().getDocumentTypeManager().getDocumentType("music");
    Document doc1 = new Document(type, new DocumentId("doc:music:1"));
    assertTrue(session.put(doc1).isSuccess());
    assertTrue(handler.latch.await(60, TimeUnit.SECONDS));
    assertNotNull(handler.response);
    session.destroy();
}
Also used : DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Test(org.junit.Test)

Example 52 with Document

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

the class VespaXMLFeedReader method read.

/* (non-Javadoc)
     * @see com.yahoo.vespaxmlparser.FeedReader#read(com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation)
     */
@Override
public void read(Operation operation) throws Exception {
    String startTag = null;
    operation.setInvalid();
    try {
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                startTag = reader.getName().toString();
                if ("document".equals(startTag)) {
                    VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(reader, docTypeManager);
                    Document document = new Document(documentReader);
                    operation.setDocument(document);
                    operation.setCondition(TestAndSetCondition.fromConditionString(documentReader.getCondition()));
                    return;
                } else if ("update".equals(startTag)) {
                    VespaXMLUpdateReader updateReader = new VespaXMLUpdateReader(reader, docTypeManager);
                    DocumentUpdate update = new DocumentUpdate(updateReader);
                    operation.setDocumentUpdate(update);
                    operation.setCondition(TestAndSetCondition.fromConditionString(updateReader.getCondition()));
                    return;
                } else if ("remove".equals(startTag)) {
                    boolean documentIdFound = false;
                    Optional<String> condition = Optional.empty();
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        final String attributeName = reader.getAttributeName(i).toString();
                        if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
                            operation.setRemove(new DocumentId(reader.getAttributeValue(i)));
                            documentIdFound = true;
                        } else if ("condition".equals(attributeName)) {
                            condition = Optional.of(reader.getAttributeValue(i));
                        }
                    }
                    if (!documentIdFound) {
                        throw newDeserializeException("Missing \"documentid\" attribute for remove operation");
                    }
                    operation.setCondition(TestAndSetCondition.fromConditionString(condition));
                    return;
                } else {
                    throw newDeserializeException("Element \"" + startTag + "\" not allowed in this context");
                }
            }
        }
    } catch (XMLStreamException e) {
        throw (e);
    // Skip to end of current tag with other exceptions.
    } catch (Exception e) {
        try {
            if (startTag != null) {
                skipToEnd(startTag);
            }
        } catch (Exception ignore) {
        }
        throw (e);
    }
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentId(com.yahoo.document.DocumentId) Document(com.yahoo.document.Document) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 53 with Document

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

the class ApplicationTest method client.

@Test
public void client() throws Exception {
    try (ApplicationFacade app = new ApplicationFacade(Application.fromBuilder(new Application.Builder().documentType("test", IOUtils.toString(this.getClass().getResourceAsStream("/test.sd"))).container("default", new Application.Builder.Container().client("mbus://*/*", MockClient.class).documentProcessor(MockDispatchDocproc.class))))) {
        Map<String, DocumentType> typeMap = app.application().getJDisc("jdisc").documentProcessing().getDocumentTypes();
        assertNotNull(typeMap);
        DocumentType docType = typeMap.get("test");
        Document doc = new Document(docType, "id:foo:test::bar");
        doc.setFieldValue("title", "hello");
        assertEquals(DocumentProcessor.Progress.DONE, app.process(new DocumentPut(doc)));
        MockClient client = (MockClient) app.getClientById(MockClient.class.getName());
        assertNotNull(client);
        assertEquals(1, client.getCounter());
        MockDispatchDocproc docproc = (MockDispatchDocproc) app.getComponentById(MockDispatchDocproc.class.getName() + "@default");
        assertNotNull(docproc);
        assertEquals(1, docproc.getResponses().size());
        assertEquals(200, docproc.getResponses().get(0).getStatus());
    }
}
Also used : DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) MockDispatchDocproc(com.yahoo.application.container.docprocs.MockDispatchDocproc) Document(com.yahoo.document.Document) MockClient(com.yahoo.application.container.MockClient) Test(org.junit.Test)

Example 54 with Document

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

the class JDiscContainerDocprocTest method requireThatBasicDocumentProcessingWorks.

@Test
public void requireThatBasicDocumentProcessingWorks() throws Exception {
    try (Application app = new ApplicationBuilder().servicesXml(getXML(CHAIN_NAME, Rot13DocumentProcessor.class.getCanonicalName())).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.process(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Terng Nyohz!"));
        processing = com.yahoo.docproc.Processing.of(put);
        progress = docProc.process(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
    }
}
Also used : 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 55 with Document

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

the class GetSearcher method handleFieldFiltering.

private void handleFieldFiltering(GetResponse response, Result result, String fieldName, String contentType, boolean headersOnly) {
    if (response.getDocumentHits().isEmpty()) {
        result.hits().addError(ErrorMessage.createNotFound("Document not found, could not return field '" + fieldName + "'"));
        return;
    }
    if (result.hits().getErrorHit() == null) {
        Document doc = response.getDocumentHits().get(0).getDocument();
        Field field = doc.getDataType().getField(fieldName);
        boolean wrapXml = false;
        if (field == null) {
            result.hits().addError(ErrorMessage.createIllegalQuery("Field '" + fieldName + "' not found in document type"));
            return;
        }
        FieldValue value = doc.getFieldValue(field);
        // content will be null. We treat this as an error.
        if (value == null) {
            if (!field.isHeader() && headersOnly) {
                // TODO(vekterli): make this work with field sets as well.
                result.hits().addError(ErrorMessage.createInvalidQueryParameter("Field '" + fieldName + "' is located in document body, but headersonly " + "prevents it from being retrieved in " + doc.getId().toString()));
            } else {
                result.hits().addError(ErrorMessage.createNotFound("Field '" + fieldName + "' found in document type, but had " + "no content in " + doc.getId().toString()));
            }
            return;
        }
        String encoding = null;
        if (field.getDataType() == DataType.RAW) {
            if (contentType == null) {
                contentType = "application/octet-stream";
            }
            encoding = "ISO-8859-1";
        } else {
            // By default, return field wrapped in a blanket of vespa XML
            contentType = "text/xml";
            wrapXml = true;
        }
        if (encoding == null) {
            // Encoding doesn't matter for binary content, since we're always
            // writing directly to the byte buffer and not through a charset
            // encoder. Presumably, the client is intelligent enough to not
            // attempt to UTF-8 decode binary data.
            encoding = "UTF-8";
        }
        // Add hit now that we know there aren't any field errors. Otherwise,
        // there would be both an error hit and a document hit in the result
        response.addHitsToResult(result, false);
        // Override Vespa XML template
        result.getTemplating().setTemplates(new DocumentFieldTemplate(field, contentType, encoding, wrapXml));
    }
// else: return with error hit, invoking regular Vespa XML error template
}
Also used : Field(com.yahoo.document.Field) FieldValue(com.yahoo.document.datatypes.FieldValue) Document(com.yahoo.document.Document)

Aggregations

Document (com.yahoo.document.Document)109 Test (org.junit.Test)63 DocumentType (com.yahoo.document.DocumentType)41 DocumentPut (com.yahoo.document.DocumentPut)40 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 InputStream (java.io.InputStream)18 DocumentId (com.yahoo.document.DocumentId)16 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)16 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)16 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)13 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)13 HashMap (java.util.HashMap)13 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)12 FieldValue (com.yahoo.document.datatypes.FieldValue)12 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)12 DocumentOperation (com.yahoo.document.DocumentOperation)10 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)10 DocumentUpdate (com.yahoo.document.DocumentUpdate)8 Array (com.yahoo.document.datatypes.Array)8