summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/DocumentOperationMessageV3.java
blob: ca2fdd6b3296f8c82371be8d5b313b41e7d4d7cb (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.server;

import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentRemove;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.RemoveDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.UpdateDocumentMessage;
import com.yahoo.jdisc.Metric;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.routing.ErrorDirective;
import com.yahoo.messagebus.routing.Hop;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader;
import com.yahoo.yolean.Exceptions;

/**
 * Keeps an operation with its message.
 *
 * This implementation is based on V2, but the code is restructured.
 *
 * @author dybis
 */
class DocumentOperationMessageV3 {

    private final String operationId;
    private final Message message;

    private DocumentOperationMessageV3(String operationId, Message message) {
        this.operationId = operationId;
        this.message = message;
    }

    Message getMessage() {
        return message;
    }

    String getOperationId() {
        return operationId;
    }

    static DocumentOperationMessageV3 newErrorMessage(String operationId, Exception exception) {
        Message feedErrorMessageV3 = new FeedErrorMessage(operationId);
        DocumentOperationMessageV3 msg = new DocumentOperationMessageV3(operationId, feedErrorMessageV3);
        Hop hop = new Hop();
        hop.addDirective(new ErrorDirective(Exceptions.toMessageString(exception)));
        Route route = new Route();
        route.addHop(hop);
        feedErrorMessageV3.setRoute(route);
        return msg;
    }

    static DocumentOperationMessageV3 newUpdateMessage(VespaXMLFeedReader.Operation op, String operationId) {
        DocumentUpdate update = op.getDocumentUpdate();
        update.setCondition(op.getCondition());
        Message msg = new UpdateDocumentMessage(update);

        String id = (operationId == null) ? update.getId().toString() : operationId;
        return new DocumentOperationMessageV3(id, msg);
    }

    static DocumentOperationMessageV3 newRemoveMessage(VespaXMLFeedReader.Operation op, String operationId) {
        DocumentRemove remove = new DocumentRemove(op.getRemove());
        remove.setCondition(op.getCondition());
        Message msg = new RemoveDocumentMessage(remove);

        String id = (operationId == null) ? remove.getId().toString() : operationId;
        return new DocumentOperationMessageV3(id, msg);
    }

    static DocumentOperationMessageV3 newPutMessage(VespaXMLFeedReader.Operation op, String operationId) {
        DocumentPut put = new DocumentPut(op.getDocument());
        put.setCondition(op.getCondition());
        Message msg = new PutDocumentMessage(put);

        String id = (operationId == null) ? put.getId().toString() : operationId;
        return new DocumentOperationMessageV3(id, msg);
    }

    static DocumentOperationMessageV3 create(VespaXMLFeedReader.Operation operation, String operationId, Metric metric) {
        switch (operation.getType()) {
            case DOCUMENT:
                metric.add(MetricNames.NUM_PUTS, 1, null /*metricContext*/);
                return newPutMessage(operation, operationId);
            case REMOVE:
                metric.add(MetricNames.NUM_REMOVES, 1, null /*metricContext*/);
                return newRemoveMessage(operation, operationId);
            case UPDATE:
                metric.add(MetricNames.NUM_UPDATES, 1, null /*metricContext*/);
                return newUpdateMessage(operation, operationId);
            default:
                // typical end of feed
                return null;
        }
    }

}