aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/UpdateDocumentMessage.java
blob: b8609cd42b8277252b404c363552f71314da89a1 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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.document.DocumentUpdate;
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 UpdateDocumentMessage extends TestAndSetMessage {

    private DocumentUpdate update = null;
    private long oldTime = 0;
    private long newTime = 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 UpdateDocumentMessage(LazyDecoder decoder, DocumentDeserializer buffer) {
        this.decoder = decoder;
        this.buffer = buffer;
    }

    /**
     * Constructs a new document update message.
     *
     * @param upd The document update to perform.
     */
    public UpdateDocumentMessage(DocumentUpdate upd) {
        super();
        update = upd;
    }

    /**
     * Creates an empty UpdateDocumentMessage
     */
    public static UpdateDocumentMessage createEmpty() {
        return new UpdateDocumentMessage(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 update to perform.
     *
     * @return The update.
     */
    public DocumentUpdate getDocumentUpdate() {
        deserialize();
        return update;
    }

    /**
     * Sets the document update to perform.
     *
     * @param upd The document update to set.
     */
    public void setDocumentUpdate(DocumentUpdate upd) {
        if (upd == null) {
            throw new IllegalArgumentException("Document update can not be null.");
        }
        buffer = null;
        decoder = null;
        update = upd;
    }

    /**
     * Returns the timestamp required for this update to be applied.
     *
     * @return The document timestamp.
     */
    public long getOldTimestamp() {
        deserialize();
        return oldTime;
    }

    /**
     * Sets the timestamp required for this update to be applied.
     *
     * @param time The timestamp to set.
     */
    public void setOldTimestamp(long time) {
        buffer = null;
        decoder = null;
        oldTime = time;
    }

    /**
     * Returns the timestamp to assign to the updated document.
     *
     * @return The document timestamp.
     */
    public long getNewTimestamp() {
        deserialize();
        return newTime;
    }

    /**
     * Sets the timestamp to assign to the updated document.
     *
     * @param time The timestamp to set.
     */
    public void setNewTimestamp(long time) {
        buffer = null;
        decoder = null;
        newTime = 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;
    }

    @Override
    public DocumentReply createReply() {
        return new UpdateDocumentReply();
    }

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

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

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

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

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

    boolean createIfMissing() {
        deserialize();
        return update.getCreateIfNonExistent();
    }

}