Search in sources :

Example 21 with DocumentOperation

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

the class JsonReader method readSingleDocument.

/**
 * Reads a single operation. The operation is not expected to be part of an array.
 * @param operationType the type of operation (update or put)
 * @param docIdString document ID.
 * @return the document
 */
public DocumentOperation readSingleDocument(DocumentParser.SupportedOperation operationType, String docIdString) {
    DocumentId docId = new DocumentId(docIdString);
    final DocumentParseInfo documentParseInfo;
    try {
        DocumentParser documentParser = new DocumentParser(parser);
        documentParseInfo = documentParser.parse(Optional.of(docId)).get();
    } catch (IOException e) {
        state = END_OF_FEED;
        throw new RuntimeException(e);
    }
    documentParseInfo.operationType = operationType;
    VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader();
    DocumentOperation operation = vespaJsonDocumentReader.createDocumentOperation(getDocumentTypeFromString(documentParseInfo.documentId.getDocType(), typeManager), documentParseInfo);
    operation.setCondition(TestAndSetCondition.fromConditionString(documentParseInfo.condition));
    return operation;
}
Also used : DocumentParser(com.yahoo.document.json.document.DocumentParser) DocumentOperation(com.yahoo.document.DocumentOperation) DocumentId(com.yahoo.document.DocumentId) IOException(java.io.IOException) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader)

Example 22 with DocumentOperation

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

the class SingleDocumentParser method parse.

private VespaXMLFeedReader.Operation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) {
    final JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory);
    final DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId);
    VespaXMLFeedReader.Operation operation = new VespaXMLFeedReader.Operation();
    try {
        inputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (supportedOperation == DocumentParser.SupportedOperation.PUT) {
        operation.setDocument(((DocumentPut) documentOperation).getDocument());
    } else {
        operation.setDocumentUpdate((DocumentUpdate) documentOperation);
    }
    // (A potentially empty) test-and-set condition is always set by JsonReader
    operation.setCondition(documentOperation.getCondition());
    return operation;
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) VespaXMLFeedReader(com.yahoo.vespaxmlparser.VespaXMLFeedReader) DocumentOperation(com.yahoo.document.DocumentOperation) IOException(java.io.IOException)

Example 23 with DocumentOperation

use of com.yahoo.document.DocumentOperation 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)

Aggregations

DocumentOperation (com.yahoo.document.DocumentOperation)23 DocumentPut (com.yahoo.document.DocumentPut)13 Document (com.yahoo.document.Document)10 DocumentUpdate (com.yahoo.document.DocumentUpdate)8 Test (org.junit.Test)7 DocumentRemove (com.yahoo.document.DocumentRemove)5 DocumentType (com.yahoo.document.DocumentType)5 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DocumentId (com.yahoo.document.DocumentId)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Processing (com.yahoo.docproc.Processing)2 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)2 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)2 FieldUpdate (com.yahoo.document.update.FieldUpdate)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 SplitterDocumentProcessor.doProcessOuterDocument (com.yahoo.docproc.util.SplitterDocumentProcessor.doProcessOuterDocument)1 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)1 Field (com.yahoo.document.Field)1