summaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/GetDocumentReply.java
blob: 0690168f298bc6f6161293af3eef2b7b5836c78f (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
// 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.Document;
import com.yahoo.document.serialization.DocumentDeserializer;

import java.nio.ByteBuffer;

/**
 * @author Simon Thoresen Hult
 */
public class GetDocumentReply extends DocumentAcceptedReply {

    private DocumentDeserializer buffer = null;
    private Document document = null;
    private long lastModified = 0;
    private LazyDecoder decoder = null;

    /**
     * Constructs a new reply for deserialization.
     */
    GetDocumentReply() {
        super(DocumentProtocol.REPLY_GETDOCUMENT);
    }

    /**
     * Constructs a new reply to lazily deserialize from a byte buffer.
     *
     * @param decoder The decoder to use for deserialization.
     * @param buf     A byte buffer that contains a serialized reply.
     */
    GetDocumentReply(LazyDecoder decoder, DocumentDeserializer buf) {
        super(DocumentProtocol.REPLY_GETDOCUMENT);
        this.decoder = decoder;
        buffer = buf;
    }

    /**
     * Constructs a new document get reply.
     *
     * @param doc The document requested.
     */
    public GetDocumentReply(Document doc) {
        super(DocumentProtocol.REPLY_GETDOCUMENT);
        document = doc;
    }

    /**
     * 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 retrieved.
     *
     * @return The document.
     */
    public Document getDocument() {
        deserialize();
        return document;
    }

    /**
     * Sets the document of this reply.
     *
     * @param doc The document to set.
     */
    public void setDocument(Document doc) {
        buffer = null;
        decoder = null;
        document = doc;
        lastModified = document != null && document.getLastModified() != null ? document.getLastModified() : 0;
    }

    /**
     * Returns the date the document was last modified.
     *
     * @return The date.
     */
    public long getLastModified() {
        deserialize();
        return lastModified;
    }

    /**
     * Set the date the document was last modified.
     *
     * @param modified The date.
     */
    void setLastModified(long modified) {
        lastModified = modified;
    }

    /**
     * Returns the internal buffer to deserialize from, may be null.
     *
     * @return The buffer.
     */
    public ByteBuffer getSerializedBuffer() {
        return buffer != null ? buffer.getBuf().getByteBuffer() : null;
    }
}