aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/DocumentOperationMessageV3.java
blob: 6613f1a5538e34b43843f33f1611ecaf2364295b (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
// Copyright Yahoo. 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.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.vespaxmlparser.FeedOperation;

/**
 * Keeps an operation with its message.
 *
 * @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;
    }

    private static DocumentOperationMessageV3 newUpdateMessage(FeedOperation op, String operationId) {
        var msg = new UpdateDocumentMessage(op.getDocumentUpdate());

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

    static DocumentOperationMessageV3 newRemoveMessage(FeedOperation op, String operationId) {
        var msg = new RemoveDocumentMessage(op.getDocumentRemove());

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

    private static DocumentOperationMessageV3 newPutMessage(FeedOperation op, String operationId) {
        var msg = new PutDocumentMessage(op.getDocumentPut());

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

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

}