Search in sources :

Example 6 with Document

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

the class OperationHandlerImpl method get.

@Override
public Optional<String> get(RestUri restUri, Optional<String> fieldSet) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    setRoute(syncSession, Optional.empty());
    try {
        DocumentId id = new DocumentId(restUri.generateFullId());
        final Document document = syncSession.get(id, fieldSet.orElse(restUri.getDocumentType() + ":[document]"), DocumentProtocol.Priority.NORMAL_1);
        if (document == null) {
            return Optional.empty();
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JsonWriter jsonWriter = new JsonWriter(outputStream);
        jsonWriter.write(document);
        return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
    } catch (Exception e) {
        throw new RestApiException(Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED));
    } finally {
        syncSessions.free(syncSession);
    }
}
Also used : DocumentId(com.yahoo.document.DocumentId) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) JsonWriter(com.yahoo.document.json.JsonWriter) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Example 7 with Document

use of com.yahoo.document.Document 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 8 with Document

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

the class MockReader method read.

@Override
public void read(Operation operation) throws Exception {
    if (finished) {
        return;
    }
    byte whatToDo = stream.getNextOperation();
    DocumentId id = new DocumentId("id:banana:banana::doc1");
    DocumentType docType = new DocumentType("banana");
    switch(whatToDo) {
        case 0:
            finished = true;
            break;
        case 1:
            Document doc = new Document(docType, id);
            operation.setDocument(doc);
            break;
        case 2:
            operation.setRemove(id);
            break;
        case 3:
            operation.setDocumentUpdate(new DocumentUpdate(docType, id));
            break;
        case 4:
            throw new RuntimeException("boom");
    }
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document)

Example 9 with Document

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

the class SchemaMappingAndAccessesTest method testMappedDocAPI.

public void testMappedDocAPI() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    assertEquals(mapped.getFieldValue("title"), doc.getFieldValue("title"));
    assertEquals(mapped.getFieldValue(new com.yahoo.document.Field("title")), doc.getFieldValue((new com.yahoo.document.Field("title"))));
    mapped.setFieldValue("title", "foo");
    assertEquals(doc.getFieldValue("title").getWrappedValue(), "foo");
    assertEquals(mapped.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
    assertEquals(doc, mapped);
    assertEquals(doc.toString(), mapped.toString());
    assertEquals(doc.hashCode(), mapped.hashCode());
    assertEquals(doc.clone(), mapped.clone());
    assertEquals(doc.iterator().hasNext(), mapped.iterator().hasNext());
    assertEquals(doc.getId(), mapped.getId());
    assertEquals(doc.getDataType(), mapped.getDataType());
    mapped.setLastModified(56l);
    assertEquals(doc.getLastModified(), (Long) 56l);
    assertEquals(mapped.getLastModified(), (Long) 56l);
    mapped.setId(new DocumentId("doc:map:test:2"));
    assertEquals(mapped.getId().toString(), "doc:map:test:2");
    assertEquals(doc.getId().toString(), "doc:map:test:2");
    assertEquals(doc.getHeader(), mapped.getHeader());
    assertEquals(doc.getBody(), mapped.getBody());
    assertEquals(doc.getSerializedSize(), mapped.getSerializedSize());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    mapped.serialize(bos);
    doc.serialize(bos2);
    assertEquals(bos.toString(), bos2.toString());
    assertEquals(mapped.toXml(), doc.toXml());
    assertEquals(mapped.getFieldCount(), doc.getFieldCount());
    assertTrue(mapped.getDocument() == doc);
    mapped.clear();
    assertNull(mapped.getFieldValue("title"));
    assertNull(doc.getFieldValue("title"));
    mapped.setDataType(new DocumentType("newType"));
    assertEquals(doc.getDataType().getName(), "newType");
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) Field(com.yahoo.docproc.Accesses.Field)

Example 10 with Document

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

the class SchemaMappingAndAccessesTest method getDoc.

private Document getDoc() {
    DocumentType type = new DocumentType("album");
    AnnotationType personType = new AnnotationType("person");
    Annotation person = new Annotation(personType);
    type.addField("title", DataType.STRING);
    type.addField("artist", DataType.STRING);
    type.addField("guitarist", DataType.STRING);
    type.addField("year", DataType.INT);
    type.addField("labels", DataType.getArray(DataType.STRING));
    Document doc = new Document(type, new DocumentId("doc:map:test:1"));
    doc.setFieldValue("title", new StringFieldValue("Black Rock"));
    StringFieldValue joe = new StringFieldValue("Joe Bonamassa");
    joe.setSpanTree(new SpanTree("mytree").annotate(person));
    doc.setFieldValue("artist", joe);
    doc.setFieldValue("year", new IntegerFieldValue(2010));
    Array<StringFieldValue> labels = new Array<>(type.getField("labels").getDataType());
    labels.add(new StringFieldValue("audun"));
    labels.add(new StringFieldValue("tylden"));
    doc.setFieldValue("labels", labels);
    StructDataType personStructType = new StructDataType("artist");
    personStructType.addField(new com.yahoo.document.Field("firstname", DataType.STRING));
    personStructType.addField(new com.yahoo.document.Field("lastname", DataType.STRING));
    type.addField("listeners", DataType.getArray(personStructType));
    Array<Struct> listeners = new Array<>(type.getField("listeners").getDataType());
    Struct listenerOne = new Struct(personStructType);
    listenerOne.setFieldValue("firstname", new StringFieldValue("per"));
    listenerOne.setFieldValue("lastname", new StringFieldValue("olsen"));
    Struct listenerTwo = new Struct(personStructType);
    listenerTwo.setFieldValue("firstname", new StringFieldValue("anders"));
    listenerTwo.setFieldValue("lastname", new StringFieldValue("and"));
    listeners.add(listenerOne);
    listeners.add(listenerTwo);
    doc.setFieldValue("listeners", listeners);
    return doc;
}
Also used : DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Document(com.yahoo.document.Document) AnnotationType(com.yahoo.document.annotation.AnnotationType) Annotation(com.yahoo.document.annotation.Annotation) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) SpanTree(com.yahoo.document.annotation.SpanTree)

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