summaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java
blob: 8012ebafb13571d077943f6d2b29e8134e9d3e85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json;

import com.fasterxml.jackson.core.JsonFactory;
import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.document.json.document.DocumentParser;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader;

import java.io.IOException;
import java.io.InputStream;

/**
 * Parser that supports parsing PUT operation and UPDATE operation.
 *
 * @author dybis
 */
public class SingleDocumentParser {
    private static final JsonFactory jsonFactory = new JsonFactory().disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
    private DocumentTypeManager docMan;

    public SingleDocumentParser(DocumentTypeManager docMan) {
        this.docMan = docMan;
    }

    public VespaXMLFeedReader.Operation parsePut(InputStream inputStream, String docId) {
        return parse(inputStream, docId, DocumentParser.SupportedOperation.PUT);
    }

    public VespaXMLFeedReader.Operation parseUpdate(InputStream inputStream, String docId)  {
        return parse(inputStream, docId, DocumentParser.SupportedOperation.UPDATE);
    }

    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;
    }
}