Search in sources :

Example 56 with Document

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

the class DocumentRetriever method printReply.

private void printReply(Reply reply) {
    Trace trace = reply.getTrace();
    if (!trace.getRoot().isEmpty()) {
        System.out.println(trace);
    }
    if (reply.hasErrors()) {
        System.err.print("Request failed: ");
        for (int i = 0; i < reply.getNumErrors(); i++) {
            System.err.printf("\n  %s", reply.getError(i));
        }
        System.err.println();
        return;
    }
    if (!(reply instanceof GetDocumentReply)) {
        System.err.printf("Unexpected reply %s: '%s'\n", reply.getType(), reply.toString());
        return;
    }
    GetDocumentReply documentReply = (GetDocumentReply) reply;
    Document document = documentReply.getDocument();
    if (document == null) {
        System.out.println("Document not found.");
        return;
    }
    if (params.showDocSize) {
        System.out.printf("Document size: %d bytes.\n", document.getSerializedSize());
    }
    if (params.printIdsOnly) {
        System.out.println(document.getId());
    } else {
        if (params.jsonOutput) {
            System.out.print(Utf8.toString(JsonWriter.toByteArray(document)));
        } else {
            System.out.print(document.toXML("  "));
        }
    }
}
Also used : Trace(com.yahoo.messagebus.Trace) Document(com.yahoo.document.Document) GetDocumentReply(com.yahoo.documentapi.messagebus.protocol.GetDocumentReply)

Example 57 with Document

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

the class DocumentRetrieverTest method testShowDocSize.

@Test
public void testShowDocSize() throws DocumentRetrieverException {
    ClientParameters params = createParameters().setDocumentIds(asIterator(DOC_ID_1)).setShowDocSize(true).build();
    Document document = new Document(DataType.DOCUMENT, new DocumentId(DOC_ID_1));
    when(mockedSession.syncSend(any())).thenReturn(new GetDocumentReply(document));
    DocumentRetriever documentRetriever = createDocumentRetriever(params);
    documentRetriever.retrieveDocuments();
    assertTrue(outContent.toString().contains(String.format("Document size: %d bytes", document.getSerializedSize())));
}
Also used : DocumentId(com.yahoo.document.DocumentId) Document(com.yahoo.document.Document) GetDocumentReply(com.yahoo.documentapi.messagebus.protocol.GetDocumentReply) Test(org.junit.Test)

Example 58 with Document

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

the class DocumentRetrieverTest method testTrace.

@Test
public void testTrace() throws DocumentRetrieverException {
    final int traceLevel = 9;
    ClientParameters params = createParameters().setDocumentIds(asIterator(DOC_ID_1)).setTraceLevel(traceLevel).build();
    GetDocumentReply reply = new GetDocumentReply(new Document(DataType.DOCUMENT, new DocumentId(DOC_ID_1)));
    reply.getTrace().getRoot().addChild("childnode");
    when(mockedSession.syncSend(any())).thenReturn(reply);
    DocumentRetriever documentRetriever = createDocumentRetriever(params);
    documentRetriever.retrieveDocuments();
    verify(mockedSession, times(1)).setTraceLevel(traceLevel);
    assertTrue(outContent.toString().contains("<trace>"));
}
Also used : DocumentId(com.yahoo.document.DocumentId) Document(com.yahoo.document.Document) GetDocumentReply(com.yahoo.documentapi.messagebus.protocol.GetDocumentReply) Test(org.junit.Test)

Example 59 with Document

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

the class SchemaMappingAndAccessesTest method testMappingSpanTrees.

public void testMappingSpanTrees() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    fieldMap.put("g", "guitarist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    Iterator<SpanTree> itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotDoc = itSpanTreesDoc.next().iterator();
    Iterator<SpanTree> itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "person");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "person");
    assertFalse(itAnnotMapped.hasNext());
    AnnotationType guitaristType = new AnnotationType("guitarist");
    Annotation guitarist = new Annotation(guitaristType);
    StringFieldValue bona = new StringFieldValue("Bonamassa");
    bona.setSpanTree(new SpanTree("mytree").annotate(guitarist));
    StringFieldValue clapton = new StringFieldValue("Clapton");
    mapped.setFieldValue("a", bona);
    mapped.setFieldValue("g", clapton);
    itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotDoc = itSpanTreesDoc.next().iterator();
    itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "guitarist");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "guitarist");
    assertFalse(itAnnotMapped.hasNext());
    assertSame(((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator().next(), ((StringFieldValue) mapped.getFieldValue("a")).getSpanTrees().iterator().next());
// assertSame(clapton, mapped.getFieldValue("g"));
// assertSame(bona, mapped.getFieldValue("a"));
}
Also used : HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Document(com.yahoo.document.Document) Annotation(com.yahoo.document.annotation.Annotation) AnnotationType(com.yahoo.document.annotation.AnnotationType) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 60 with Document

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

the class SchemaMappingAndAccessesTest method testProxyAndSecureSecureFailing.

public void testProxyAndSecureSecureFailing() {
    DocumentProcessor procInsecure = new TestDPInsecure();
    Map<Pair<String, String>, String> fieldMap = new HashMap<>();
    fieldMap.put(new Pair<>("album", "titleMapped"), "title");
    procInsecure.setFieldMap(fieldMap);
    DocumentPut put = new DocumentPut(getDoc());
    Document doc = new Call(procInsecure).configDoc(procInsecure, put).getDocument();
    try {
        procInsecure.process(Processing.of(new DocumentPut(doc)));
        fail("Insecure docproc went through");
    } catch (Exception e) {
        assertTrue(e.getMessage().matches(".*allowed.*"));
    }
// assertEquals(doc.get("title"), "MyTitle");
}
Also used : Call(com.yahoo.docproc.Call) DocumentProcessor(com.yahoo.docproc.DocumentProcessor) HashMap(java.util.HashMap) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document) Pair(com.yahoo.collections.Pair)

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