aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/PutDocumentMessage.java
blob: 7ce25011ec1ebec5d268a38cdfb28960388258c9 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;

import com.yahoo.api.annotations.Beta;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.TestAndSetCondition;
import com.yahoo.document.serialization.DocumentDeserializer;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @author Simon Thoresen Hult
 */
public class PutDocumentMessage extends TestAndSetMessage {

    private DocumentPut put = null;
    private long time = 0;
    // TODO Vespa 9: remove. Inherently tied to legacy protocol version.
    private DocumentDeserializer buffer = null;
    private LazyDecoder decoder = null;

    /**
     * Constructs a new message from a byte buffer.
     *
     * @param decoder The decoder to use for deserialization.
     * @param buffer  A byte buffer that contains a serialized message.
     */
    public PutDocumentMessage(LazyDecoder decoder, DocumentDeserializer buffer) {
        this.decoder = decoder;
        this.buffer = buffer;
    }

    /** Constructs a new document put message */
    public PutDocumentMessage(DocumentPut put) {
        this.put = put;
    }

    /**
     * Creates an empty PutDocumentMessage
     */
    public static PutDocumentMessage createEmpty() {
        return new PutDocumentMessage(null);
    }

    /**
     * This method will make sure that any serialized content is deserialized into proper message content on first
     * entry. Any subsequent entry into this function will do nothing.
     */
    private void deserialize() {
        if (decoder != null && buffer != null) {
            decoder.decode(this, buffer);
            decoder = null;
            buffer = null;
        }
    }

    /** Returns the document put operation */
    public DocumentPut getDocumentPut() {
        deserialize();
        return put;
    }

    /** Sets the document to put */
    public void setDocumentPut(DocumentPut put) {
        buffer = null;
        decoder = null;
        this.put = put;
    }

    /** Returns the timestamp of the document to put */
    public long getTimestamp() {
        deserialize();
        return time;
    }

    /** Sets the timestamp of the document to put */
    public void setTimestamp(long time) {
        buffer = null;
        decoder = null;
        this.time = time;
    }

    /**
     * Returns the raw serialized buffer. This buffer is stored as the message is received from accross the network, and
     * deserialized from as soon as a member is requested. This method will return null if the buffer has been decoded.
     *
     * @return the buffer containing the serialized data for this message, or null
     */
    ByteBuffer getSerializedBuffer() {
        return buffer != null ? buffer.getBuf().getByteBuffer() : null; // TODO: very dirty. Must make interface.
    }

    @Override
    public DocumentReply createReply() {
        return new WriteDocumentReply(DocumentProtocol.REPLY_PUTDOCUMENT);
    }

    @Override
    public int getApproxSize() {
        if (buffer != null) {
            return buffer.getBuf().remaining();
        }
        return put.getDocument().getApproxSize();
    }

    @Override
    public boolean hasSequenceId() {
        return true;
    }

    @Override
    public long getSequenceId() {
        deserialize();
        return Arrays.hashCode(put.getId().getGlobalId());
    }

    @Override
    public int getType() {
        return DocumentProtocol.MESSAGE_PUTDOCUMENT;
    }

    @Override
    public TestAndSetCondition getCondition() {
        deserialize();
        return put.getCondition();
    }

    @Override
    public void setCondition(TestAndSetCondition condition) {
        put.setCondition(condition);
    }

    @Beta
    public void setCreateIfNonExistent(boolean value) {
        put.setCreateIfNonExistent(value);
    }

    @Beta
    public boolean getCreateIfNonExistent() {
        deserialize();
        return put.getCreateIfNonExistent();
    }
}