aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java
blob: b57b55c9d73125594e5c0d99e17ab2ae63d4180d (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
// 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.DocumentFeedOperation;
import com.yahoo.vespaxmlparser.DocumentUpdateFeedOperation;
import com.yahoo.vespaxmlparser.FeedOperation;

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 FeedOperation parsePut(InputStream inputStream, String docId) {
        return parse(inputStream, docId, DocumentParser.SupportedOperation.PUT);
    }

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

    private FeedOperation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation)  {
        JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory);
        DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId);
        try {
            inputStream.close();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        if (supportedOperation == DocumentParser.SupportedOperation.PUT) {
            return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition());
        } else {
            return new DocumentUpdateFeedOperation((DocumentUpdate) documentOperation, documentOperation.getCondition());
        }
    }

}